Skip to content

Commit

Permalink
Re #4158. Made Fit work via AlgorithmProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Apr 2, 2012
1 parent 5f54043 commit c4d5887
Show file tree
Hide file tree
Showing 28 changed files with 543 additions and 135 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ set ( INC_FILES
inc/MantidAPI/IFunctionMD.h
inc/MantidAPI/IFunction1D.h
inc/MantidAPI/IFunctionMW.h
inc/MantidAPI/IFunctionValues.h
inc/MantidAPI/IFunctionWithLocation.h
inc/MantidAPI/ILiveListener.h
inc/MantidAPI/ILocatedData.h
Expand Down
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ namespace Mantid
//@{
/// Set the property value
void setPropertyValue(const std::string& name, const std::string &value);
/// Do something after a property was set
void afterPropertySet(const std::string&);
//@}

void cancel() const;
Expand Down
53 changes: 41 additions & 12 deletions Code/Mantid/Framework/API/inc/MantidAPI/FunctionValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
#include "MantidAPI/FunctionDomain.h"
#include "MantidAPI/IFunctionValues.h"

#include <vector>

Expand Down Expand Up @@ -42,7 +43,7 @@ namespace API
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class MANTID_API_DLL FunctionValues
class MANTID_API_DLL FunctionValues: public IFunctionValues
{
public:
/// Default constructor.
Expand All @@ -51,27 +52,45 @@ class MANTID_API_DLL FunctionValues
FunctionValues(const FunctionDomain& domain);
/// Copy constructor.
FunctionValues(const FunctionValues& values);
/// Reset the values to match a new domain.
void reset(const FunctionDomain& domain);

/// Return the number of values
size_t size() const {return m_calculated.size();}
/// store i-th calculated value. 0 <= i < size()
void setCalculated(size_t i,double value) {m_calculated[i] = value;}
/// Expand values to a new size, preserve stored values.
void expand(size_t n);

/// Get a pointer to calculated data at index i
double* getPointerToCalculated(size_t i);
/// Set all calculated values to zero
void zeroCalculated();
/// set all calculated values to same number
void setCalculated(double value);

/// Reset the values to match a new domain.
void reset(const FunctionDomain& domain);
/// store i-th calculated value. 0 <= i < size()
void setCalculated(size_t i,double value) {m_calculated[i] = value;}
/// get i-th calculated value. 0 <= i < size()
double getCalculated(size_t i) const {return m_calculated[i];}
double operator[](size_t i) const {return m_calculated[i];}
void addToCalculated(size_t i, double value) {m_calculated[i] += value;}
void addToCalculated(size_t i, const FunctionValues& values);
/// Get a pointer to calculated data at index i
double* getPointerToCalculated(size_t i);

/// Add other calculated values
FunctionValues& operator+=(const FunctionValues& values);
FunctionValues& operator+=(const FunctionValues& values)
{
values.add(getPointerToCalculated(0));
return *this;
}
/// Multiply by other calculated values
FunctionValues& operator*=(const FunctionValues& values);
/// Set all calculated values to zero
void zeroCalculated();
FunctionValues& operator*=(const FunctionValues& values)
{
values.multiply(getPointerToCalculated(0));
return *this;
}
/// Add other calculated values to these values starting with i.
void addToCalculated(size_t i, const FunctionValues& values)
{
values.add(getPointerToCalculated(i));
}

/// set a fitting data value
void setFitData(size_t i,double value);
Expand All @@ -86,6 +105,16 @@ class MANTID_API_DLL FunctionValues
double getFitWeight(size_t i) const;
void setFitDataFromCalculated(const FunctionValues& values);
protected:
/// Copy calculated values to a buffer
/// @param to :: Pointer to the buffer
void copyTo(double* to) const;
/// Add calculated values to values in a buffer and save result to the buffer
/// @param to :: Pointer to the buffer, it must be large enough
void add(double* to) const;
/// Multiply calculated values by values in a buffer and save result to the buffer
/// @param to :: Pointer to the buffer, it must be large enough
void multiply(double* to) const;

std::vector<double> m_calculated; ///< buffer for calculated values
std::vector<double> m_data; ///< buffer for fit data
std::vector<double> m_weights; ///< buffer for fitting weights (reciprocal errors)
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/IFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "MantidAPI/DllConfig.h"
#include "MantidAPI/FunctionDomain.h"
#include "MantidAPI/FunctionValues.h"
#include "MantidAPI/FunctionValues.h"
#include "MantidAPI/Jacobian.h"
#include "MantidKernel/Matrix.h"
#include "MantidKernel/Logger.h"
Expand Down
74 changes: 74 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/IFunctionValues.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#ifndef MANTID_API_IFUNCTIONVALUES_H_
#define MANTID_API_IFUNCTIONVALUES_H_

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"

#include <vector>

namespace Mantid
{
namespace API
{
/** Base class that represents values of a function.
@author Roman Tolchenov, Tessella plc
@date 23/03/2012
Copyright &copy; 2009 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class MANTID_API_DLL IFunctionValues
{
public:

/// Default constructor.
virtual ~IFunctionValues(){}
/// Return the number of values
virtual size_t size() const = 0;
/// Get a pointer to calculated data at index i
virtual double* getPointerToCalculated(size_t i) = 0;
/// Set all calculated values to zero
virtual void zeroCalculated() = 0;
/// set all calculated values to same number
virtual void setCalculated(double value) = 0;

protected:
/// Copy calculated values to a buffer
/// @param to :: Pointer to the buffer, it must be large enough
virtual void copyTo(double* to) const = 0;
/// Add calculated values to values in a buffer and save result to the buffer
/// @param to :: Pointer to the buffer, it must be large enough
virtual void add(double* to) const = 0;
/// Multiply calculated values by values in a buffer and save result to the buffer
/// @param to :: Pointer to the buffer, it must be large enough
virtual void multiply(double* to) const = 0;

};

/// typedef for a shared pointer
typedef boost::shared_ptr<IFunctionValues> IFunctionValues_sptr;

} // namespace API
} // namespace Mantid

#endif /*MANTID_API_IFUNCTIONVALUES_H_*/
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/API/inc/MantidAPI/MultiDomainFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ class MANTID_API_DLL MultiDomainFunction : public CompositeFunction

/// Associate a function and a domain
void setDomainIndex(size_t funIndex, size_t domainIndex);

/// Associate a function and a list of domains
void setDomainIndices(size_t funIndex, const std::vector<size_t>& domainIndices);

/// Clear all domain indices
void clearDomainIndices();
/// Get the largest domain index
size_t getMaxIndex() const {return m_maxIndex;}

/// Returns the number of attributes associated with the function
virtual size_t nLocalAttributes()const {return 1;}
Expand Down
12 changes: 12 additions & 0 deletions Code/Mantid/Framework/API/src/AlgorithmProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ namespace Mantid
m_alg.reset();
}

/**
* Do something after a property was set
* @param name :: The name of the property
*/
void AlgorithmProxy::afterPropertySet(const std::string& name)
{
createConcreteAlg(true);
m_alg->getPointerToProperty(name)->setValue(*this->getPointerToProperty(name));
m_alg->afterPropertySet(name);
copyPropertiesFrom(*m_alg);
m_alg.reset();
}

//----------------------------------------------------------------------
// Private methods
Expand Down
76 changes: 37 additions & 39 deletions Code/Mantid/Framework/API/src/FunctionValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ namespace API
m_calculated.resize(domain.size());
}

/**
* Expand to a new size. Preserve old values. Do not contract.
* @param n :: A new size, must be greater than the current size.
*/
void FunctionValues::expand(size_t n)
{
if (n < size())
{
throw std::invalid_argument("Cannot make FunctionValues smaller");
}
m_calculated.resize(n);
if (!m_data.empty())
{
m_data.resize(n);
}
if (!m_weights.empty())
{
m_weights.resize(n);
}
}

/// set all calculated values to same number
void FunctionValues::setCalculated(double value)
Expand All @@ -59,56 +79,34 @@ namespace API
throw std::out_of_range("FunctionValue index out of range.");
}

/** Add other calculated values.
* @param values :: Some other values to be added to this calculated values.
*/
FunctionValues& FunctionValues::operator+=(const FunctionValues& values)

/// Set all calculated values to zero
void FunctionValues::zeroCalculated()
{
if (values.size() != size())
{
throw std::runtime_error("Cannot add function values: different sizes.");
}
std::transform(m_calculated.begin(),m_calculated.end(),values.m_calculated.begin(),m_calculated.begin(),
std::plus<double>());
return *this;
setCalculated(0.0);
}

/** Multiply this calculated values by others.
* @param values :: Some other values to be added to this calculated values.
/**
* Copy calculated values to a buffer
* @param to :: Pointer to the buffer
*/
FunctionValues& FunctionValues::operator*=(const FunctionValues& values)
void FunctionValues::copyTo(double* to) const
{
if (values.size() != size())
{
throw std::runtime_error("Cannot multiply function values: different sizes.");
}
std::transform(m_calculated.begin(),m_calculated.end(),values.m_calculated.begin(),m_calculated.begin(),
std::multiplies<double>());
return *this;
std::copy(m_calculated.begin(),m_calculated.end(),to);
}

/// Set all calculated values to zero
void FunctionValues::zeroCalculated()
/// Add calculated values to values in a buffer and save result to the buffer
/// @param to :: Pointer to the buffer, it must be large enough
void FunctionValues::add(double* to) const
{
setCalculated(0.0);
std::transform(m_calculated.begin(),m_calculated.end(),to,to,std::plus<double>());
}

/**
* Add values starting at index i.
*/
void FunctionValues::addToCalculated(size_t i, const FunctionValues& values)
/// Multiply calculated values by values in a buffer and save result to the buffer
/// @param to :: Pointer to the buffer, it must be large enough
void FunctionValues::multiply(double* to) const
{
if (i + values.size() > size())
{
throw std::runtime_error("Cannot add function values: different sizes.");
}
std::transform(
m_calculated.begin() + i,
m_calculated.begin() + i + values.size(),
values.m_calculated.begin(),
m_calculated.begin() + i,
std::plus<double>()
);
std::transform(m_calculated.begin(),m_calculated.end(),to,to,std::multiplies<double>());
}

/**
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/CurveFitting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set ( SRC_FILES
src/LogNormal.cpp
src/Lorentzian.cpp
src/Lorentzian1D.cpp
src/MultiDomainCreator.cpp
src/PRConjugateGradientMinimizer.cpp
src/PlotPeakByLogValue.cpp
src/ProductFunction.cpp
Expand Down Expand Up @@ -69,6 +70,7 @@ set ( INC_FILES
inc/MantidCurveFitting/BivariateNormal.h
inc/MantidCurveFitting/BoundaryConstraint.h
inc/MantidCurveFitting/Chebyshev.h
inc/MantidCurveFitting/CompositeValues.h
inc/MantidCurveFitting/Convolution.h
inc/MantidCurveFitting/CostFuncFitting.h
# inc/MantidCurveFitting/CostFuncIgnorePosPeaks.h
Expand Down Expand Up @@ -109,6 +111,7 @@ set ( INC_FILES
inc/MantidCurveFitting/Lorentzian.h
inc/MantidCurveFitting/Lorentzian1D.h
inc/MantidCurveFitting/MuonFInteraction.h
inc/MantidCurveFitting/MultiDomainCreator.h
inc/MantidCurveFitting/PRConjugateGradientMinimizer.h
inc/MantidCurveFitting/PlotPeakByLogValue.h
inc/MantidCurveFitting/ProductFunction.h
Expand Down

0 comments on commit c4d5887

Please sign in to comment.