IFunction.h 26 KB
Newer Older
1
2
3
4
5
6
7
8
#ifndef MANTID_API_IFUNCTION_H_
#define MANTID_API_IFUNCTION_H_

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
#include "MantidAPI/FunctionDomain.h"
9
#include "MantidAPI/FunctionValues.h"
10
#include "MantidAPI/IConstraint.h"
Roman Tolchenov's avatar
Roman Tolchenov committed
11
#include "MantidAPI/Jacobian.h"
12
#include "MantidAPI/ParameterTie.h"
13
#include "MantidKernel/Matrix.h"
14
15
#include "MantidKernel/Unit.h"

16
#ifndef Q_MOC_RUN
17
18
#include <boost/shared_ptr.hpp>
#include <boost/variant.hpp>
19
20
#endif

21
22
23
24
#include <string>
#include <vector>

#ifdef _WIN32
25
#pragma warning(disable : 4250)
26
27
#endif

28
29
namespace Mantid {
namespace Kernel {
30
31
32
//----------------------------------------------------------------------
// Forward declaration
//----------------------------------------------------------------------
33
34
class ProgressBase;
}
35
namespace API {
36
class Workspace;
37
class MatrixWorkspace;
38
class FunctionHandler;
39

40
41
42
43
/** This is an interface to a fitting function - a semi-abstarct class.
    Functions derived from IFunction can be used with the Fit algorithm.
    IFunction defines the structure of a fitting funtion.

44
45
46
47
48
49
    A function has a number of named parameters (not arguments), type double, on
   which it depends.
    Parameters must be declared either in the constructor or in the init()
   method
    of a derived class with method declareParameter(...). Method nParams()
   returns
50
    the number of parameters. A parameter can be accessed either by its name
51
52
    or the index. For example in case of Gaussian the parameters can be
   "Height",
53
54
    "PeakCentre" and "Sigma".

55
56
57
58
59
60
    The main method of IFunction is called function(const
   FunctionDomain&,FunctionValues&).
    It takes a set of function arguments via interface FunctionDomain,
   calculates the values, and
    returns them via the FunctionValues. The derived classes must implement this
   method.
61
62

    Implement functionDeriv method for the function to be used with
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    fitting algorithms using derivatives. functionDeriv calculates patrial
   derivatives of the
    function with respect to the fitting parameters. The default implementation
   uses numeric differentiation.

    To fit a function to a set of data its parameters must be adjusted so that
   the difference
    between the data and the corresponding function values were minimized. This
   is the aim
    of the Fit algorithm. But Fit does not work with the declared parameters
   directly.
    Instead it uses other - active - parameters. In simple case the active
   parameters are the
    same as the declared ones. But they can be overidden if the declared
   parameters make fit unstable.
    There are as many active parameters as there are the declared ones. A
   one-to-one transformation
    must exist between the active and the declared parameters. Overide
   activeParameter and
    setActiveParameter methods to implement this transformation. An example is
   Gaussian where
    "Sigma" makes the fit unstable. So in the fit it is replaced with variable
   Weight = 1 / Sigma
86
    which is more efficient.
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    The active parameters can be accessed by their index. The implementations of
   the access method
    for both active and declared parameters must ensure that any changes to one
   of them
    immediately reflected on the other so that the two sets are consistent at
   any moment.

    IFunction declares method nameOfActive(int i) which returns the name of the
   declared parameter
    corresponding to the i-th active parameter. I am not completely sure in the
   usefulness of it.

    The declared parameters can be made fixed in a fit with method fix(). If a
   parameter is fixed a fit
    shouldn't change its value unless it is also tied to values of other
   parameters. Implementations of
104
    active parameters must ensure this behaviour.
105

106
107
108
109
110
111
    When a declared parameter is made fixed one of the active parameters must
   become inactive. isActive(i)
    method must return false for it. In case of declared == active the fixed
   parameter becomes inactive.
    Classes overriding active parameters must ensure that number of inactive
   parameters == number of
112
    fixed declared ones at any moment.
113

114
115
116
117
    IFunction provides methods for tying and untying parameters. Only the
   declared parameters can be
    tied. isFixed() method returns true for a tied parameter. The value of a
   tied parameter is defined
118
119
    by its tie and can change in a fit.

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    Method addConstraint adds constraints on possible values of a declared
   parameter. A constrained parameter
    is not fixed and can vary within its constraint. Constraints and ties are
   used only in fitting.

    Any non-fitting parameters can be implemented as attributes (class
   IFunction::Attribute).
    An attribute can have one of three types: std::string, int, or double. The
   type is set at construction
    and cannot be changed later. To read or write the attributes there are two
   ways. If the type
    is known the type specific accessors can be used, e.g. asString(), asInt().
   Otherwise the
    IFunction::AttributeVisitor can be used. It provides alternative virtual
   methods to access
    attributes of each type. When creating a function from a string (using
   FunctionFactory::creaeInitialized(...))
    the attributes must be set first, before any fitting parameter, as the
   number and names of the parameters
139
140
141
142
143
144
    can depend on the attributes.

    @author Roman Tolchenov, Tessella Support Services plc
    @date 16/10/2009
    @date 22/12/2010

145
146
    Copyright &copy; 2009 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
   National Laboratory & European Spallation Source
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

    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/>.

163
    File change history is stored at: <https://github.com/mantidproject/mantid>.
164
165
    Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
166
class MANTID_API_DLL IFunction {
167
168
169
170
171
public:
  /**
   * Atribute visitor class. It provides a separate access method
   * for each attribute type. When applied to a particular attribue
   * the appropriate method will be used. The child classes must
172
   * implement the virtual AttributeVisitor::apply methods. See
173
174
   * implementation of Attribute::value() method for an example.
   */
175
176
  template <typename T = void>
  class DLLExport AttributeVisitor : public boost::static_visitor<T> {
177
178
  public:
    /// Virtual destructor
179
    virtual ~AttributeVisitor() = default;
180
    /// implements static_visitor's operator() for std::string
181
    T operator()(std::string &str) const { return apply(str); }
182
    /// implements static_visitor's operator() for double
183
    T operator()(double &d) const { return apply(d); }
184
    /// implements static_visitor's operator() for int
185
    T operator()(int &i) const { return apply(i); }
186
    /// implements static_visitor's operator() for bool
187
    T operator()(bool &b) const { return apply(b); }
188
    /// implements static_visitor's operator() for vector
189
190
    T operator()(std::vector<double> &v) const { return apply(v); }

191
192
  protected:
    /// Implement this mathod to access attribute as string
193
    virtual T apply(std::string &) const = 0;
194
    /// Implement this mathod to access attribute as double
195
    virtual T apply(double &) const = 0;
196
    /// Implement this mathod to access attribute as int
197
    virtual T apply(int &) const = 0;
198
    /// Implement this mathod to access attribute as bool
199
    virtual T apply(bool &) const = 0;
200
    /// Implement this mathod to access attribute as vector
201
    virtual T apply(std::vector<double> &) const = 0;
202
203
204
  };

  /**
205
   * Const version of AttributeVisitor.
206
   */
207
208
  template <typename T = void>
  class DLLExport ConstAttributeVisitor : public boost::static_visitor<T> {
209
210
  public:
    /// Virtual destructor
211
    virtual ~ConstAttributeVisitor() = default;
212
    /// implements static_visitor's operator() for std::string
213
    T operator()(std::string &str) const { return apply(str); }
214
    /// implements static_visitor's operator() for double
215
    T operator()(double &d) const { return apply(d); }
216
    /// implements static_visitor's operator() for int
217
    T operator()(int &i) const { return apply(i); }
218
    /// implements static_visitor's operator() for bool
219
    T operator()(bool &b) const { return apply(b); }
220
    /// implements static_visitor's operator() for vector
221
222
    T operator()(std::vector<double> &v) const { return apply(v); }

223
224
  protected:
    /// Implement this mathod to access attribute as string
225
    virtual T apply(const std::string &str) const = 0;
226
    /// Implement this mathod to access attribute as double
227
    virtual T apply(const double &d) const = 0;
228
    /// Implement this mathod to access attribute as int
229
    virtual T apply(const int &i) const = 0;
230
    /// Implement this mathod to access attribute as bool
231
    virtual T apply(const bool &i) const = 0;
232
    /// Implement this mathod to access attribute as vector
233
    virtual T apply(const std::vector<double> &) const = 0;
234
235
236
237
238
  };

  /// Attribute is a non-fitting parameter.
  /// It can be one of the types: std::string, int, or double
  /// Examples: file name, polinomial order
239
  class MANTID_API_DLL Attribute {
240
  public:
241
    /// Create empty string attribute
242
    explicit Attribute() : m_data(std::string()), m_quoteValue(false) {}
243
    /// Create string attribute
244
245
    explicit Attribute(const std::string &str, bool quoteValue = false)
        : m_data(str), m_quoteValue(quoteValue) {}
246
    /// Create int attribute
247
    explicit Attribute(const int &i) : m_data(i), m_quoteValue(false) {}
248
    /// Create double attribute
249
    explicit Attribute(const double &d) : m_data(d), m_quoteValue(false) {}
250
    /// Create bool attribute
251
    explicit Attribute(const bool &b) : m_data(b), m_quoteValue(false) {}
252
    /// Create string attribute
253
254
    explicit Attribute(const char *c)
        : m_data(std::string(c)), m_quoteValue(false) {}
255
    /// Create vector attribute
256
257
    explicit Attribute(const std::vector<double> &v)
        : m_data(v), m_quoteValue(false) {}
258
    /// Copy assignment
259
    Attribute &operator=(const Attribute &attr);
260

261
262
263
264
    /// Apply an attribute visitor
    template <typename T> T apply(AttributeVisitor<T> &v) {
      return boost::apply_visitor(v, m_data);
    }
265
    /// Apply a const attribute visitor
266
267
268
    template <typename T> T apply(ConstAttributeVisitor<T> &v) const {
      return boost::apply_visitor(v, m_data);
    }
269

270
    /// Returns type of the attribute
271
    std::string type() const;
272
    /// Returns the attribute value as a string
273
274
275
276
277
278
    std::string value() const;
    /// Returns string value if attribute is a string, throws exception
    /// otherwise
    std::string asString() const;
    /// Returns a string value that is guarenteed to be quoted for use in places
    /// where the string is used as the displayed value.
279
280
281
282
    std::string asQuotedString() const;
    /// Returns a string value that is guarenteed to be unquoted.
    std::string asUnquotedString() const;
    /// Returns int value if attribute is a int, throws exception otherwise
283
284
285
286
    int asInt() const;
    /// Returns double value if attribute is a double, throws exception
    /// otherwise
    double asDouble() const;
287
    /// Returns bool value if attribute is a bool, throws exception otherwise
288
    bool asBool() const;
289
    /// Returns bool value if attribute is a vector, throws exception otherwise
290
    std::vector<double> asVector() const;
291

292
    /// Sets new value if attribute is a string
293
    void setString(const std::string &str);
294
    /// Sets new value if attribute is a double
295
    void setDouble(const double &);
296
    /// Sets new value if attribute is a int
297
    void setInt(const int &);
298
    /// Sets new value if attribute is a bool
299
    void setBool(const bool &);
300
    /// Sets new value if attribute is a vector
301
    void setVector(const std::vector<double> &);
302
    /// Set value from a string.
303
304
    void fromString(const std::string &str);

305
306
  private:
    /// The data holder as boost variant
307
308
    mutable boost::variant<std::string, int, double, bool, std::vector<double>>
        m_data;
309
310
311
312
313
314
315
    /// Flag indicating if the string value should be returned quoted
    bool m_quoteValue;
  };

  //---------------------------------------------------------//

  /// Constructor
Roman Tolchenov's avatar
Roman Tolchenov committed
316
  IFunction() : m_isParallel(false), m_handler(nullptr), m_chiSquared(0.0) {}
317
318
  /// Virtual destructor
  virtual ~IFunction();
319
320
321
322
  /// No copying
  IFunction(const IFunction &) = delete;
  /// No copying
  IFunction &operator=(const IFunction &) = delete;
323
324

  /// Returns the function's name
325
  virtual std::string name() const = 0;
326
  /// Writes itself into a string
Roman Tolchenov's avatar
Roman Tolchenov committed
327
328
  virtual std::string
  asString(const std::string &localAttributesStr = "") const;
329
330
  /// Virtual copy constructor
  virtual boost::shared_ptr<IFunction> clone() const;
331
  /// Set the workspace.
332
  /// @param ws :: Shared pointer to a workspace
333
334
335
  virtual void setWorkspace(boost::shared_ptr<const Workspace> ws) {
    UNUSED_ARG(ws);
  }
336
  /// Set matrix workspace
337
338
339
  virtual void
  setMatrixWorkspace(boost::shared_ptr<const API::MatrixWorkspace> workspace,
                     size_t wi, double startX, double endX);
340
  /// Iinialize the function
341
342
343
  virtual void initialize() { this->init(); }
  /// Returns an estimate of the number of progress reports a single evaluation
  /// of the function will have. For backwards compatibility default=1
344
345
346
  virtual int64_t estimateNoProgressCalls() const { return 1; }

  /// Attach a progress reporter
Roman Tolchenov's avatar
Roman Tolchenov committed
347
  void setProgressReporter(boost::shared_ptr<Kernel::ProgressBase> reporter);
348
  /// Reports progress with an optional message
349
350
351
  void reportProgress(const std::string &msg = "") const;
  /// Returns true if a progress reporter is set & evalaution has been requested
  /// to stop
352
  bool cancellationRequestReceived() const;
353
354

  /// The categories the Fit function belong to.
355
  /// Categories must be listed as a semi colon separated list.
356
  /// For example: "General, Muon\\Custom" which adds
357
  /// a function to the category "General" and the sub-category
358
359
  /// "Muon\\Custom"
  virtual const std::string category() const { return "General"; }
360
361
  /// Function to return all of the categories that contain this algorithm
  virtual const std::vector<std::string> categories() const;
362
363
364
  /// Function to return the sperator token for the category string. A default
  /// implementation ';' is provided
  virtual const std::string categorySeparator() const { return ";"; }
365

366
367
  /// Evaluates the function for all arguments in the domain.
  /// @param domain :: Provides arguments for the function.
368
369
370
371
  /// @param values :: A buffer to store the function values. It must be large
  /// enogh to store domain.size() values.
  virtual void function(const FunctionDomain &domain,
                        FunctionValues &values) const = 0;
372
  /// Derivatives of function with respect to active parameters.
373
  virtual void functionDeriv(const FunctionDomain &domain, Jacobian &jacobian);
374

375
376
  /* @name Callbacks to perform work at various points other than in the
   * function */
377
378
379
380
381
  /// Called at the start of each iteration
  virtual void iterationStarting() {}
  /// Called at the end of an iteration
  virtual void iterationFinished() {}

382
383
  /** @name Function parameters */
  //@{
384
  /// Set i-th parameter
385
386
  virtual void setParameter(size_t, const double &value,
                            bool explicitlySet = true) = 0;
387
  /// Set i-th parameter description
388
389
  virtual void setParameterDescription(size_t,
                                       const std::string &description) = 0;
390
  /// Get i-th parameter
391
  virtual double getParameter(size_t i) const = 0;
392
  /// Set parameter by name.
393
394
  virtual void setParameter(const std::string &name, const double &value,
                            bool explicitlySet = true) = 0;
395
  /// Set description of parameter by name.
396
397
  virtual void setParameterDescription(const std::string &name,
                                       const std::string &description) = 0;
398
  /// Get parameter by name.
399
  virtual double getParameter(const std::string &name) const = 0;
400
  /// Total number of parameters
401
  virtual size_t nParams() const = 0;
402
  /// Returns the index of parameter name
403
  virtual size_t parameterIndex(const std::string &name) const = 0;
404
  /// Returns the name of parameter i
405
  virtual std::string parameterName(size_t i) const = 0;
406
  /// Returns the description of parameter i
407
  virtual std::string parameterDescription(size_t i) const = 0;
408
  /// Checks if a parameter has been set explicitly
409
  virtual bool isExplicitlySet(size_t i) const = 0;
410
411
412
413
  /// Get the fitting error for a parameter
  virtual double getError(size_t i) const = 0;
  /// Set the fitting error for a parameter
  virtual void setError(size_t i, double err) = 0;
414

415
  /// Check if a parameter i is fixed
416
  bool isFixed(size_t i) const;
417
418
419
  /// Check if a parameter i is fixed by default (not by user).
  bool isFixedByDefault(size_t i) const;
  /// Removes a parameter i from the list of active
420
  void fix(size_t i, bool isDefault = false);
421
  /// Restores a declared parameter i to the active status
422
  void unfix(size_t i);
423
  /// Fix a parameter
424
  void fixParameter(const std::string &name, bool isDefault = false);
425
426
427
  /// Free a parameter
  void unfixParameter(const std::string &name);
  /// Fix all parameters
428
  void fixAll(bool isDefault = false);
429
430
  /// Free all parameters
  void unfixAll();
431
432
433
434
  /// Free all parameters fixed by default
  void unfixAllDefault();
  /// Fix all active parameters
  void fixAllActive(bool isDefault = false);
435

436
437
438
  /// Return parameter index from a parameter reference. Usefull for constraints
  /// and ties in composite functions
  virtual size_t getParameterIndex(const ParameterReference &ref) const = 0;
439
  /// Return a vector with all parameter names
440
  std::vector<std::string> getParameterNames() const;
441
442
443
444
  //@}

  /** @name Active parameters */
  //@{
445
446
447
448
449
  /// Value of i-th active parameter. Override this method to make fitted
  /// parameters different from the declared
  virtual double activeParameter(size_t i) const;
  /// Set new value of i-th active parameter. Override this method to make
  /// fitted parameters different from the declared
450
451
  virtual void setActiveParameter(size_t i, double value);
  /// Returns the name of active parameter i
452
  virtual std::string nameOfActive(size_t i) const;
453
  /// Returns the name of active parameter i
454
  virtual std::string descriptionOfActive(size_t i) const;
455
  /// Check if an active parameter i is actually active
456
  bool isActive(size_t i) const;
457
458
  //@}

459
460
  /** @name Ties */
  //@{
461
  /// Tie a parameter to other parameters (or a constant)
462
  virtual void tie(const std::string &parName, const std::string &expr,
Roman Tolchenov's avatar
Roman Tolchenov committed
463
                   bool isDefault = false);
464
  /// Add several ties
465
  virtual void addTies(const std::string &ties, bool isDefault = false);
466
  /// Apply the ties
467
  virtual void applyTies();
468
  /// Removes the tie off a parameter
469
  virtual void removeTie(const std::string &parName);
470
  /// Remove all ties
471
  virtual void clearTies();
472
  /// Removes i-th parameter's tie
473
  virtual bool removeTie(size_t i);
474
  /// Get the tie of i-th parameter
475
  virtual ParameterTie *getTie(size_t i) const;
476
  /// Write a parameter tie to a string
477
  std::string writeTies() const;
478
  //@}
479

480
481
  /** @name Constraints */
  //@{
482
  /// Add a list of conatraints from a string
483
  virtual void addConstraints(const std::string &str, bool isDefault = false);
484
  /// Add a constraint to function
485
  virtual void addConstraint(std::unique_ptr<IConstraint> ic);
486
  /// Get constraint of i-th parameter
487
  virtual IConstraint *getConstraint(size_t i) const;
488
  /// Remove a constraint
489
  virtual void removeConstraint(const std::string &parName);
490
  /// Write a parameter constraint to a string
491
  std::string writeConstraints() const;
492
493
  /// Remove all constraints.
  virtual void clearConstraints();
494
  //@}
495

496
497
  /** @name Attributes */
  //@{
498
  /// Returns the number of attributes associated with the function
499
  virtual size_t nAttributes() const;
500
  /// Returns a list of attribute names
501
  virtual std::vector<std::string> getAttributeNames() const;
502
  /// Return a value of attribute attName
503
  virtual Attribute getAttribute(const std::string &name) const;
504
  /// Set a value to attribute attName
505
  virtual void setAttribute(const std::string &name, const Attribute &);
506
  /// Check if attribute attName exists
507
  virtual bool hasAttribute(const std::string &name) const;
508
  /// Set an attribute value
509
510
511
512
513
514
  template <typename T>
  void setAttributeValue(const std::string &attName, const T &value) {
    setAttribute(attName, Attribute(value));
  }
  void setAttributeValue(const std::string &attName, const char *value);
  void setAttributeValue(const std::string &attName, const std::string &value);
515
516
517
  //@}

  /// Set up the function for a fit.
518
  virtual void setUpForFit();
519
520
  /// Get number of values for a given domain.
  virtual size_t getValuesSize(const FunctionDomain &domain) const;
521
522
523
  /// Get number of domains required by this function
  virtual size_t getNumberDomains() const;
  /// Split this function (if needed) into a list of independent functions.
524
525
  virtual std::vector<boost::shared_ptr<IFunction>>
  createEquivalentFunctions() const;
526
  /// Calculate numerical derivatives
527
  void calNumericalDeriv(const FunctionDomain &domain, Jacobian &jacobian);
528
529
530
  /// Set the covariance matrix
  void setCovarianceMatrix(boost::shared_ptr<Kernel::Matrix<double>> covar);
  /// Get the covariance matrix
531
532
533
  boost::shared_ptr<const Kernel::Matrix<double>> getCovarianceMatrix() const {
    return m_covar;
  }
534
  /// Set the chi^2
535
  void setChiSquared(double chi2) { m_chiSquared = chi2; }
536
  /// Get the chi^2
537
  double getChiSquared() const { return m_chiSquared; }
538

539
  /// Set the parallel hint
540
  void setParallel(bool on) { m_isParallel = on; }
541
  /// Get the parallel hint
542
  bool isParallel() const { return m_isParallel; }
543

544
  /// Set a function handler
545
  void setHandler(FunctionHandler *handler);
546
  /// Return the handler
547
  FunctionHandler *getHandler() const { return m_handler; }
548

549
550
551
  /// Describe parameter status in relation to fitting:
  /// Active: Fit varies such parameter directly.
  /// Fixed:  Value doesn't change during fit.
552
553
  /// FixedByDefault:  Fixed by default, don't show in ties of
  ///         the output string.
554
  /// Tied:   Value depends on values of other parameters.
555
  enum ParameterStatus { Active, Fixed, FixedByDefault, Tied };
556
557
558
559
560
561
  /// Change status of parameter
  virtual void setParameterStatus(size_t i, ParameterStatus status) = 0;
  /// Get status of parameter
  virtual ParameterStatus getParameterStatus(size_t i) const = 0;

protected:
562
  /// Function initialization. Declare function parameters in this method.
563
  virtual void init();
564
  /// Declare a new parameter
565
566
  virtual void declareParameter(const std::string &name, double initValue = 0,
                                const std::string &description = "") = 0;
567

568
  /// Convert a value from one unit (inUnit) to unit defined in workspace (ws)
569
  double convertValue(double value, Kernel::Unit_sptr &outUnit,
570
                      boost::shared_ptr<const MatrixWorkspace> ws,
571
572
573
574
575
                      size_t wsIndex) const;

  void convertValue(std::vector<double> &values, Kernel::Unit_sptr &outUnit,
                    boost::shared_ptr<const MatrixWorkspace> ws,
                    size_t wsIndex) const;
576

577
578
579
580
581
582
  /// Override to declare function attributes
  virtual void declareAttributes() {}
  /// Override to declare function parameters
  virtual void declareParameters() {}

  /// Declare a single attribute
583
584
  void declareAttribute(const std::string &name,
                        const API::IFunction::Attribute &defaultValue);
585
  /// Store an attribute's value
586
587
  void storeAttributeValue(const std::string &name,
                           const API::IFunction::Attribute &value);
588
589
590
  /// A read-only ("mutable") attribute can be stored in a const method
  void storeReadOnlyAttribute(const std::string &name,
                              const API::IFunction::Attribute &value) const;
591
592
  /// Add a new tie. Derived classes must provide storage for ties
  virtual void addTie(std::unique_ptr<ParameterTie> tie);
593

594
595
  friend class ParameterTie;
  friend class CompositeFunction;
596
  friend class FunctionParameterDecorator;
597
  friend class FunctionGenerator;
598

599
600
  /// Flag to hint that the function is being used in parallel computations
  bool m_isParallel;
601

602
  /// Pointer to a function handler
603
  FunctionHandler *m_handler;
604

605
  /// Pointer to the progress handler
Roman Tolchenov's avatar
Roman Tolchenov committed
606
  boost::shared_ptr<Kernel::ProgressBase> m_progReporter;
607

608
609
610
private:
  /// The declared attributes
  std::map<std::string, API::IFunction::Attribute> m_attrs;
611
612
  /// The covariance matrix of the fitting parameters
  boost::shared_ptr<Kernel::Matrix<double>> m_covar;
613
614
  /// The chi-squared of the last fit
  double m_chiSquared;
615
616
617
618
  /// Holds parameter ties as <parameter index,tie pointer>
  std::vector<std::unique_ptr<ParameterTie>> m_ties;
  /// Holds the constraints added to function
  std::vector<std::unique_ptr<IConstraint>> m_constraints;
619
620
};

621
/// shared pointer to the function base class
622
typedef boost::shared_ptr<IFunction> IFunction_sptr;
623
/// shared pointer to the function base class (const version)
624
625
typedef boost::shared_ptr<const IFunction> IFunction_const_sptr;

626
627
628
629
630
631
632
/**
 * Classes inherited from FunctionHandler will handle the function.
 * The intended purpose is to help with displaying nested composite
 * functions in a tree view. This way a display handler shows only
 * single function and there is no need to duplicate the function tree
 * structure.
 */
633
class FunctionHandler {
634
635
public:
  /// Constructor
636
  FunctionHandler(IFunction_sptr fun) : m_fun(fun) {}
637
  /// Virtual destructor
638
  virtual ~FunctionHandler() = default;
639
640
641
  /// abstract init method. It is called after setting handler to the function
  virtual void init() = 0;
  /// Return the handled function
642
643
  IFunction_sptr function() const { return m_fun; }

644
protected:
645
  IFunction_sptr m_fun; ///< pointer to the handled function
646
647
};

648
649
650
651
} // namespace API
} // namespace Mantid

#endif /*MANTID_API_IFUNCTION_H_*/