CompositeFunction.cpp 22.1 KB
Newer Older
1
2
3
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
4
5
#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/ParameterTie.h"
6
#include "MantidAPI/IConstraint.h"
7
#include "MantidAPI/FunctionFactory.h"
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
8
9
10
#include "MantidKernel/Exception.h"
#include "MantidKernel/Logger.h"

11
#include <boost/lexical_cast.hpp>
12
13
#include <boost/shared_array.hpp>
#include <sstream>
14
#include <algorithm>
15

16
17
namespace Mantid {
namespace API {
18

19
20
21
22
namespace {
/// static logger
Kernel::Logger g_log("CompositeFunction");
}
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
23

Peterson, Peter's avatar
Peterson, Peter committed
24
25
using std::size_t;

26
DECLARE_FUNCTION(CompositeFunction)
27

28
/// Default constructor
29
30
CompositeFunction::CompositeFunction()
    : IFunction(), m_nParams(0), m_iConstraintFunction(false) {
31
32
33
  declareAttribute("NumDeriv", Attribute(false));
}

34
/// Function initialization. Declare function parameters in this method.
35
void CompositeFunction::init() {}
36

37
/**
38
39
40
41
42
43
44
45
46
47
 * Writes itself into a string. Functions derived from CompositeFunction must
 * override this method with something like this:
 *   std::string NewFunction::asString()const
 *   {
 *      ostr << "composite=" << this->name() << ';';
 *      // write NewFunction's own attributes and parameters
 *      ostr << CompositeFunction::asString();
 *      // write NewFunction's own ties and constraints
 *      // ostr << ";constraints=(" << ... <<")";
 *   }
48
 * @return the string representation of the composite function
49
 */
50
std::string CompositeFunction::asString() const {
51
  std::ostringstream ostr;
52

53
  // if empty just return function name
54
  if (nFunctions() == 0) {
55
56
57
    return "name=" + name();
  }

58
  if (name() != "CompositeFunction" || nAttributes() > 1 ||
59
      getAttribute("NumDeriv").asBool()) {
60
    ostr << "composite=" << name();
61
    std::vector<std::string> attr = this->getAttributeNames();
62
63
    for (const auto &attName : attr) {
      std::string attValue = this->getAttribute(attName).value();
64
65
      if (!attValue.empty()) {
        ostr << ',' << attName << '=' << attValue;
66
67
68
      }
    }
    ostr << ';';
69
  }
70
  for (size_t i = 0; i < nFunctions(); i++) {
71
    IFunction_sptr fun = getFunction(i);
Hahn, Steven's avatar
Hahn, Steven committed
72
73
    bool isComp =
        boost::dynamic_pointer_cast<CompositeFunction>(fun) != nullptr;
74
75
    if (isComp)
      ostr << '(';
76
    ostr << fun->asString();
77
78
79
    if (isComp)
      ostr << ')';
    if (i < nFunctions() - 1) {
80
81
82
83
      ostr << ';';
    }
  }
  std::string ties;
84
85
86
  for (size_t i = 0; i < nParams(); i++) {
    const ParameterTie *tie = getTie(i);
    if (tie) {
87
88
      IFunction_sptr fun = getFunction(functionIndex(i));
      std::string tmp = tie->asString(fun.get());
89
      if (tmp.empty()) {
90
        tmp = tie->asString(this);
91
92
        if (!tmp.empty()) {
          if (!ties.empty()) {
93
94
95
96
97
98
99
            ties += ",";
          }
          ties += tmp;
        }
      }
    }
  }
100
  if (!ties.empty()) {
101
    ostr << ";ties=(" << ties << ")";
102
103
104
105
  }
  return ostr.str();
}

106
107
108
/**
 * @param ws A pointer to the workspace being fitted
 */
109
void CompositeFunction::setWorkspace(boost::shared_ptr<const Workspace> ws) {
110
111
  // Pass it on to each member
  auto iend = m_functions.end();
112
  for (auto it = m_functions.begin(); it != iend; ++it) {
113
114
115
116
    (*it)->setWorkspace(ws);
  }
}

117
118
119
120
121
122
/**
 * @param workspace :: A workspace to fit to.
 * @param wi :: An index of a spectrum to fit to.
 * @param startX :: A start of the fitting region.
 * @param endX :: An end of the fitting region.
 */
123
124
125
126
127
128
void CompositeFunction::setMatrixWorkspace(
    boost::shared_ptr<const MatrixWorkspace> workspace, size_t wi,
    double startX, double endX) {
  for (size_t iFun = 0; iFun < nFunctions(); ++iFun) {
    m_functions[iFun]->setMatrixWorkspace(workspace, wi, startX, endX);
  }
129
130
}

131
/** Function you want to fit to.
132
 *  @param domain :: An instance of FunctionDomain with the function arguments.
133
134
 *  @param values :: A FunctionValues instance for storing the calculated
 * values.
135
 */
136
137
void CompositeFunction::function(const FunctionDomain &domain,
                                 FunctionValues &values) const {
138
  FunctionValues tmp(domain);
139
  values.zeroCalculated();
140
141
  for (size_t iFun = 0; iFun < nFunctions(); ++iFun) {
    m_functions[iFun]->function(domain, tmp);
142
143
144
145
146
147
148
149
150
    values += tmp;
  }
}

/**
 * Derivatives of function with respect to active parameters
 * @param domain :: Function domain to get the arguments from.
 * @param jacobian :: A Jacobian to store the derivatives.
 */
151
152
153
void CompositeFunction::functionDeriv(const FunctionDomain &domain,
                                      Jacobian &jacobian) {
  if (getAttribute("NumDeriv").asBool()) {
154
    calNumericalDeriv(domain, jacobian);
155
156
157
158
  } else {
    for (size_t iFun = 0; iFun < nFunctions(); ++iFun) {
      PartialJacobian J(&jacobian, paramOffset(iFun));
      getFunction(iFun)->functionDeriv(domain, J);
159
    }
160
161
162
  }
}

163
/** Sets a new value to the i-th parameter.
164
165
 *  @param i :: The parameter index
 *  @param value :: The new value
166
167
 *  @param explicitlySet :: A boolean falgging the parameter as explicitly set
 * (by user)
168
 */
169
170
void CompositeFunction::setParameter(size_t i, const double &value,
                                     bool explicitlySet) {
171
  size_t iFun = functionIndex(i);
172
173
  m_functions[iFun]->setParameter(i - m_paramOffsets[iFun], value,
                                  explicitlySet);
174
175
}

176
177
178
179
/** Sets a new description to the i-th parameter.
 *  @param i :: The parameter index
 *  @param description :: The new description
 */
180
181
void CompositeFunction::setParameterDescription(
    size_t i, const std::string &description) {
182
  size_t iFun = functionIndex(i);
183
184
  m_functions[iFun]->setParameterDescription(i - m_paramOffsets[iFun],
                                             description);
185
186
}

187
/** Get the i-th parameter.
188
 *  @param i :: The parameter index
189
 *  @return value of the requested parameter
190
 */
191
double CompositeFunction::getParameter(size_t i) const {
Peterson, Peter's avatar
Peterson, Peter committed
192
  size_t iFun = functionIndex(i);
193
  return m_functions[iFun]->getParameter(i - m_paramOffsets[iFun]);
194
195
}

196
197
/**
 * Sets a new value to a parameter by name.
198
199
 * @param name :: The name of the parameter.
 * @param value :: The new value
200
201
 * @param explicitlySet :: A boolean falgging the parameter as explicitly set
 * (by user)
202
 */
203
204
void CompositeFunction::setParameter(const std::string &name,
                                     const double &value, bool explicitlySet) {
205
  std::string pname;
206
  size_t index;
207
208
  parseName(name, index, pname);
  getFunction(index)->setParameter(pname, value, explicitlySet);
209
210
}

211
212
213
/**
 * Sets a new description to a parameter by name.
 * @param name :: The name of the parameter.
214
 * @param description :: The new description
215
 */
216
217
void CompositeFunction::setParameterDescription(
    const std::string &name, const std::string &description) {
218
  std::string pname;
219
  size_t index;
220
221
  parseName(name, index, pname);
  getFunction(index)->setParameterDescription(pname, description);
222
223
}

224
225
/**
 * Parameters by name.
226
 * @param name :: The name of the parameter.
227
 * @return value of the requested named parameter
228
 */
229
double CompositeFunction::getParameter(const std::string &name) const {
230
  std::string pname;
231
  size_t index;
232
  parseName(name, index, pname);
233
  return getFunction(index)->getParameter(pname);
234
235
236
}

/// Total number of parameters
237
size_t CompositeFunction::nParams() const { return m_nParams; }
238

239
/**
240
 *
241
 * @param name :: The name of a parameter
242
 * @return index of the requested named parameter
243
 */
244
size_t CompositeFunction::parameterIndex(const std::string &name) const {
245
  std::string pname;
246
  size_t index;
247
  parseName(name, index, pname);
248
  return getFunction(index)->parameterIndex(pname) + m_paramOffsets[index];
249
250
}

Nick Draper's avatar
re #100    
Nick Draper committed
251
/// Returns the name of parameter
252
/// @param i :: The index
Nick Draper's avatar
re #100    
Nick Draper committed
253
/// @return The name of the parameter
254
std::string CompositeFunction::parameterName(size_t i) const {
255
  size_t iFun = functionIndex(i);
256
  std::ostringstream ostr;
257
258
  ostr << 'f' << iFun << '.'
       << m_functions[iFun]->parameterName(i - m_paramOffsets[iFun]);
259
  return ostr.str();
260
261
}

262
263
264
/// Returns the description of parameter
/// @param i :: The index
/// @return The description of the parameter
265
std::string CompositeFunction::parameterDescription(size_t i) const {
266
  size_t iFun = functionIndex(i);
267
  std::ostringstream ostr;
268
  ostr << m_functions[iFun]->parameterDescription(i - m_paramOffsets[iFun]);
269
270
271
  return ostr.str();
}

272
/**
273
274
275
276
 * Get the fitting error for a parameter
 * @param i :: The index of a parameter
 * @return :: the error
 */
277
double CompositeFunction::getError(size_t i) const {
278
  size_t iFun = functionIndex(i);
279
  return m_functions[iFun]->getError(i - m_paramOffsets[iFun]);
280
281
}

282
/**
283
284
285
286
 * Set the fitting error for a parameter
 * @param i :: The index of a parameter
 * @param err :: The error value to set
 */
287
void CompositeFunction::setError(size_t i, double err) {
288
  size_t iFun = functionIndex(i);
289
  m_functions[iFun]->setError(i - m_paramOffsets[iFun], err);
290
291
}

292
293
294
/// Value of i-th active parameter. Override this method to make fitted
/// parameters different from the declared
double CompositeFunction::activeParameter(size_t i) const {
295
  size_t iFun = functionIndex(i);
296
  return m_functions[iFun]->activeParameter(i - m_paramOffsets[iFun]);
297
298
}

299
300
301
/// Set new value of i-th active parameter. Override this method to make fitted
/// parameters different from the declared
void CompositeFunction::setActiveParameter(size_t i, double value) {
302
  size_t iFun = functionIndex(i);
303
  m_functions[iFun]->setActiveParameter(i - m_paramOffsets[iFun], value);
304
305
306
}

/// Returns the name of active parameter i
307
std::string CompositeFunction::nameOfActive(size_t i) const {
308
  size_t iFun = functionIndex(i);
309
  std::ostringstream ostr;
310
311
  ostr << 'f' << iFun << '.'
       << m_functions[iFun]->nameOfActive(i - m_paramOffsets[iFun]);
312
313
314
315
  return ostr.str();
}

/// Returns the description of active parameter i
316
std::string CompositeFunction::descriptionOfActive(size_t i) const {
317
  size_t iFun = functionIndex(i);
318
  std::ostringstream ostr;
319
  ostr << m_functions[iFun]->descriptionOfActive(i - m_paramOffsets[iFun]);
320
  return ostr.str();
321
322
}

323
324
325
326
327
/**
 * query to see in the function is active
 * @param i :: The index of a declared parameter
 * @return true if parameter i is active
 */
328
bool CompositeFunction::isActive(size_t i) const {
329
  size_t iFun = functionIndex(i);
330
  return m_functions[iFun]->isActive(i - m_paramOffsets[iFun]);
331
332
}

333
/**
334
 * query to see in the function is active
335
 * @param i :: The index of a declared parameter
336
 * @return true if parameter i is active
337
 */
338
bool CompositeFunction::isFixed(size_t i) const {
339
  size_t iFun = functionIndex(i);
340
  return m_functions[iFun]->isFixed(i - m_paramOffsets[iFun]);
341
342
343
}

/**
344
 * @param i :: A declared parameter index to be removed from active
345
 */
346
void CompositeFunction::fix(size_t i) {
347
  size_t iFun = functionIndex(i);
348
  m_functions[iFun]->fix(i - m_paramOffsets[iFun]);
349
350
}

351
/** Makes a parameter active again. It doesn't change the parameter's tie.
352
 * @param i :: A declared parameter index to be restored to active
353
 */
354
void CompositeFunction::unfix(size_t i) {
355
  size_t iFun = functionIndex(i);
356
  m_functions[iFun]->unfix(i - m_paramOffsets[iFun]);
357
358
}

359
/** Makes sure that the function is consistent.
360
 */
361
void CompositeFunction::checkFunction() {
362
363
  m_nParams = 0;
  m_paramOffsets.clear();
364
  m_IFunction.clear();
365

366
  std::vector<IFunction_sptr> functions(m_functions.begin(), m_functions.end());
367
368
  m_functions.clear();

Hahn, Steven's avatar
Hahn, Steven committed
369
  for (auto &f : functions) {
370
371
372
373
    CompositeFunction_sptr cf =
        boost::dynamic_pointer_cast<CompositeFunction>(f);
    if (cf)
      cf->checkFunction();
374
375
376
377
    addFunction(f);
  }
}

378
/** Add a function
379
 * @param f :: A pointer to the added function
380
 * @return The function index
381
 */
382
383
size_t CompositeFunction::addFunction(IFunction_sptr f) {
  m_IFunction.insert(m_IFunction.end(), f->nParams(), m_functions.size());
384
385
  m_functions.push_back(f);
  //?f->init();
386
  if (m_paramOffsets.empty()) {
387
388
    m_paramOffsets.push_back(0);
    m_nParams = f->nParams();
389
  } else {
390
391
392
    m_paramOffsets.push_back(m_nParams);
    m_nParams += f->nParams();
  }
393
  return m_functions.size() - 1;
394
395
}

396
/** Remove a function
397
 * @param i :: The index of the function to remove
398
 */
399
400
void CompositeFunction::removeFunction(size_t i) {
  if (i >= nFunctions())
401
402
    throw std::out_of_range("Function index out of range.");

403
  IFunction_sptr fun = getFunction(i);
404

405
  size_t dnp = fun->nParams();
406

407
408
409
  for (size_t j = 0; j < nParams();) {
    ParameterTie *tie = getTie(j);
    if (tie && tie->findParametersOf(fun.get())) {
410
      removeTie(j);
411
    } else {
412
413
414
415
416
      j++;
    }
  }

  // Shift down the function indeces for parameters
417
  for (auto it = m_IFunction.begin(); it != m_IFunction.end();) {
418

419
    if (*it == i) {
420
      it = m_IFunction.erase(it);
421
422
    } else {
      if (*it > i) {
423
424
        *it -= 1;
      }
425
      ++it;
426
427
428
429
    }
  }

  m_nParams -= dnp;
430
431
432
  // Shift the parameter offsets down by the total number of i-th function's
  // params
  for (size_t j = i + 1; j < nFunctions(); j++) {
433
434
    m_paramOffsets[j] -= dnp;
  }
435
  m_paramOffsets.erase(m_paramOffsets.begin() + i);
436

437
  m_functions.erase(m_functions.begin() + i);
438
439
}

440
/** Replace a function with a new one. The old function is deleted.
441
 *  The new function must have already its workspace set.
442
 * @param f_old :: The pointer to the function to replace. If it's not
443
 *  a member of this composite function nothing happens
444
 * @param f_new :: A pointer to the new function
445
 */
446
447
448
449
450
451
void CompositeFunction::replaceFunctionPtr(const IFunction_sptr f_old,
                                           IFunction_sptr f_new) {
  std::vector<IFunction_sptr>::const_iterator it =
      std::find(m_functions.begin(), m_functions.end(), f_old);
  if (it == m_functions.end())
    return;
452
  std::vector<IFunction_sptr>::difference_type iFun = it - m_functions.begin();
453
  replaceFunction(iFun, f_new);
454
455
}

456
/** Replace a function with a new one. The old function is deleted.
457
458
 * @param i :: The index of the function to replace
 * @param f :: A pointer to the new function
459
 */
460
461
void CompositeFunction::replaceFunction(size_t i, IFunction_sptr f) {
  if (i >= nFunctions())
462
463
    throw std::out_of_range("Function index out of range.");

464
  IFunction_sptr fun = getFunction(i);
465
  size_t np_old = fun->nParams();
466

467
  size_t np_new = f->nParams();
468

469
470
  // Modify function indeces: The new function may have different number of
  // parameters
471
  {
472
    auto itFun = std::find(m_IFunction.begin(), m_IFunction.end(), i);
473
    if (itFun != m_IFunction.end()) // functions must have at least 1 parameter
474
    {
475
476
477
478
      if (np_old > np_new) {
        m_IFunction.erase(itFun, itFun + np_old - np_new);
      } else if (np_old < np_new) {
        m_IFunction.insert(itFun, np_new - np_old, i);
479
      }
480
481
    } else if (np_new > 0) // it could happen if the old function is an empty
                           // CompositeFunction
482
    {
483
484
485
      itFun = std::find_if(m_IFunction.begin(), m_IFunction.end(),
                           std::bind2nd(std::greater<size_t>(), i));
      m_IFunction.insert(itFun, np_new, i);
486
    }
487
488
  }

489
  size_t dnp = np_new - np_old;
490
  m_nParams += dnp;
491
492
493
  // Shift the parameter offsets down by the total number of i-th function's
  // params
  for (size_t j = i + 1; j < nFunctions(); j++) {
494
495
496
497
498
499
    m_paramOffsets[j] += dnp;
  }

  m_functions[i] = f;
}

500
/**
501
 * @param i :: The index of the function
502
 * @return function at the requested index
503
 */
504
505
IFunction_sptr CompositeFunction::getFunction(std::size_t i) const {
  if (i >= nFunctions()) {
506
    throw std::out_of_range("Function index out of range.");
507
  }
508
509
510
  return m_functions[i];
}

511
512
/**
 * Get the index of the function to which parameter i belongs
513
 * @param i :: The parameter index
514
 * @return function index of the requested parameter
515
 */
516
517
size_t CompositeFunction::functionIndex(std::size_t i) const {
  if (i >= nParams()) {
518
    throw std::out_of_range("Function parameter index out of range.");
519
  }
520
  return m_IFunction[i];
521
522
523
}

/**
524
525
* @param varName :: The variable name which may contain function index (
* [f<index.>]name )
526
* @param index :: Receives function index or throws std::invalid_argument
527
* @param name :: Receives the parameter name
528
*/
529
530
void CompositeFunction::parseName(const std::string &varName, size_t &index,
                                  std::string &name) {
531
  size_t i = varName.find('.');
532
  if (i == std::string::npos) {
533
    throw std::invalid_argument("Parameter " + varName + " not found.");
534
  } else {
535
    if (varName[0] != 'f')
536
537
      throw std::invalid_argument(
          "External function parameter name must start with 'f'");
538

539
    std::string sindex = varName.substr(1, i - 1);
540
541
542
543
544
    index = boost::lexical_cast<int>(sindex);

    if (i == varName.size() - 1)
      throw std::invalid_argument("Name cannot be empty");

545
    name = varName.substr(i + 1);
546
  }
547
548
}

549
/** Returns the index of parameter i as it declared in its function
550
 * @param i :: The parameter index
551
552
 * @return The local index of the parameter
 */
553
size_t CompositeFunction::parameterLocalIndex(size_t i) const {
554
  size_t iFun = functionIndex(i);
555
556
557
  return i - m_paramOffsets[iFun];
}

558
/** Returns the name of parameter i as it declared in its function
559
 * @param i :: The parameter index
560
561
 * @return The pure parameter name (without the function identifier f#.)
 */
562
std::string CompositeFunction::parameterLocalName(size_t i) const {
563
  size_t iFun = functionIndex(i);
564
  return m_functions[iFun]->parameterName(i - m_paramOffsets[iFun]);
565
566
}

567
/**
568
 * Apply the ties.
569
 */
570
571
void CompositeFunction::applyTies() {
  for (size_t i = 0; i < nFunctions(); i++) {
572
573
574
575
576
    getFunction(i)->applyTies();
  }
}

/**
577
 * Clear the ties.
578
 */
579
580
void CompositeFunction::clearTies() {
  for (size_t i = 0; i < nFunctions(); i++) {
581
582
583
584
585
    getFunction(i)->clearTies();
  }
}

/** Removes i-th parameter's tie if it is tied or does nothing.
586
 * @param i :: The index of the tied parameter.
587
588
 * @return True if successfull
 */
589
bool CompositeFunction::removeTie(size_t i) {
590
  size_t iFun = functionIndex(i);
591
  bool res = m_functions[iFun]->removeTie(i - m_paramOffsets[iFun]);
592
593
594
595
  return res;
}

/** Get the tie of i-th parameter
596
 * @param i :: The parameter index
597
 * @return A pointer to the tie.
598
 */
599
ParameterTie *CompositeFunction::getTie(size_t i) const {
600
  size_t iFun = functionIndex(i);
601
  return m_functions[iFun]->getTie(i - m_paramOffsets[iFun]);
602
603
604
605
}

/**
 * Attaches a tie to this function. The attached tie is owned by the function.
606
 * @param tie :: A pointer to a new tie
607
 */
608
void CompositeFunction::addTie(ParameterTie *tie) {
609
610
  size_t i = getParameterIndex(*tie);
  size_t iFun = functionIndex(i);
611
612
613
614
615
  m_functions[iFun]->addTie(tie);
}

/**
 * Declare a new parameter. To used in the implementation'c constructor.
616
617
 * @param name :: The parameter name.
 * @param initValue :: The initial value for the parameter
618
 * @param description :: Parameter documentation
619
 */
620
621
622
623
624
625
626
627
void CompositeFunction::declareParameter(const std::string &name,
                                         double initValue,
                                         const std::string &description) {
  (void)name;        // Avoid compiler warning
  (void)initValue;   // Avoid compiler warning
  (void)description; // Avoid compiler warning
  throw Kernel::Exception::NotImplementedError(
      "CompositeFunction cannot not have its own parameters.");
628
629
630
}

/** Add a constraint
631
 *  @param ic :: Pointer to a constraint.
632
 */
633
void CompositeFunction::addConstraint(IConstraint *ic) {
634
635
  size_t i = getParameterIndex(*ic);
  size_t iFun = functionIndex(i);
636
  getFunction(iFun)->addConstraint(ic);
637
638
}

639
640
641
/**
 * Prepare the function for a fit.
 */
642
void CompositeFunction::setUpForFit() {
643
  // set up the member functions
644
  for (size_t i = 0; i < nFunctions(); i++) {
645
    getFunction(i)->setUpForFit();
646
  }
647
648
649
650
  // unfortuately the code below breaks some system tests (IRISFuryAndFuryFit)
  // it looks as if using numeric derivatives can give different fit results
  // to fit with analytical ones
  //
651
  // if parameters have non-constant ties enable numerical derivatives
652
  // for(size_t i = 0; i < nParams(); ++i)
653
654
655
656
657
658
659
660
661
662
663
  //{
  //  ParameterTie* tie = getTie( i );
  //  if ( tie && !tie->isConstant() )
  //  {
  //    useNumericDerivatives( true );
  //    break;
  //  }
  //}

  // instead of automatically switching to numeric derivatives
  // log a warning about a danger of not using it
664
665
666
667
668
669
  if (!getAttribute("NumDeriv").asBool()) {
    for (size_t i = 0; i < nParams(); ++i) {
      ParameterTie *tie = getTie(i);
      if (tie && !tie->isConstant()) {
        g_log.warning() << "Numeric derivatives should be used when "
                           "non-constant ties defined." << std::endl;
670
671
        break;
      }
672
    }
673
674
675
  }
}

Nick Draper's avatar
re #100    
Nick Draper committed
676
/// Get constraint
677
/// @param i :: the index
Nick Draper's avatar
re #100    
Nick Draper committed
678
/// @return A pointer to the constraint
679
IConstraint *CompositeFunction::getConstraint(size_t i) const {
680
  size_t iFun = functionIndex(i);
681
  return m_functions[iFun]->getConstraint(i - m_paramOffsets[iFun]);
682
683
}

684
/** Remove a constraint
685
 * @param parName :: The name of a parameter which constarint to remove.
686
 */
687
void CompositeFunction::removeConstraint(const std::string &parName) {
688
689
  size_t iPar = parameterIndex(parName);
  size_t iFun = functionIndex(iPar);
690
691
692
  getFunction(iFun)->removeConstraint(parameterLocalName(iPar));
}

693
/** Checks if a constraint has been explicitly set
694
 *  @param i :: The parameter index
695
 *  @return true if the function is explicitly set
696
 */
697
bool CompositeFunction::isExplicitlySet(size_t i) const {
698
  size_t iFun = functionIndex(i);
699
  return m_functions[iFun]->isExplicitlySet(i - m_paramOffsets[iFun]);
700
701
}

702
/**
703
704
 * Returns the index of parameter if the ref points to one of the member
 * function
705
 * @param ref :: A reference to a parameter
706
 * @return Parameter index or number of nParams() if parameter not found
707
 */
708
709
710
size_t
CompositeFunction::getParameterIndex(const ParameterReference &ref) const {
  if (ref.getFunction() == this && ref.getIndex() < nParams()) {
711
    return ref.getIndex();
712
  }
713
  for (size_t iFun = 0; iFun < nFunctions(); iFun++) {
714
    IFunction_sptr fun = getFunction(iFun);
715
    size_t iLocalIndex = fun->getParameterIndex(ref);
716
    if (iLocalIndex < fun->nParams()) {
717
718
719
      return m_paramOffsets[iFun] + iLocalIndex;
    }
  }
720
  return nParams();
721
722
}

723
/**
724
 * Returns the shrared pointer to the function conataining a parameter
725
 * @param ref :: The reference
726
727
 * @return A function containing parameter pointed to by ref
 */
728
729
730
IFunction_sptr
CompositeFunction::getContainingFunction(const ParameterReference &ref) const {
  for (size_t iFun = 0; iFun < nFunctions(); iFun++) {
731
    IFunction_sptr fun = getFunction(iFun);
732
    if (fun->getParameterIndex(ref) < fun->nParams()) {
733
      return fun;
734
735
    }
  }
736
737
738
  return IFunction_sptr();
}

739
740
} // namespace API
} // namespace Mantid