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
35
/// Destructor
CompositeFunction::~CompositeFunction() {}
36
37

/// Function initialization. Declare function parameters in this method.
38
void CompositeFunction::init() {}
39

40
/**
41
42
43
44
45
46
47
48
49
50
 * 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=(" << ... <<")";
 *   }
51
 * @return the string representation of the composite function
52
 */
53
std::string CompositeFunction::asString() const {
54
  std::ostringstream ostr;
55

56
  // if empty just return function name
57
  if (nFunctions() == 0) {
58
59
60
    return "name=" + name();
  }

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

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

119
120
121
122
123
124
/**
 * @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.
 */
125
126
127
128
129
130
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);
  }
131
132
}

133
/** Function you want to fit to.
134
 *  @param domain :: An instance of FunctionDomain with the function arguments.
135
136
 *  @param values :: A FunctionValues instance for storing the calculated
 * values.
137
 */
138
139
void CompositeFunction::function(const FunctionDomain &domain,
                                 FunctionValues &values) const {
140
  FunctionValues tmp(domain);
141
  values.zeroCalculated();
142
143
  for (size_t iFun = 0; iFun < nFunctions(); ++iFun) {
    m_functions[iFun]->function(domain, tmp);
144
145
146
147
148
149
150
151
152
    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.
 */
153
154
155
void CompositeFunction::functionDeriv(const FunctionDomain &domain,
                                      Jacobian &jacobian) {
  if (getAttribute("NumDeriv").asBool()) {
156
    calNumericalDeriv(domain, jacobian);
157
158
159
160
  } else {
    for (size_t iFun = 0; iFun < nFunctions(); ++iFun) {
      PartialJacobian J(&jacobian, paramOffset(iFun));
      getFunction(iFun)->functionDeriv(domain, J);
161
    }
162
163
164
  }
}

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

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

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

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

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

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

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

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

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

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

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

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

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

301
302
303
/// 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) {
304
  size_t iFun = functionIndex(i);
305
  m_functions[iFun]->setActiveParameter(i - m_paramOffsets[iFun], value);
306
307
308
}

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

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

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

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

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

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

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

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

371
  for (auto f : functions) {
372
373
374
375
    CompositeFunction_sptr cf =
        boost::dynamic_pointer_cast<CompositeFunction>(f);
    if (cf)
      cf->checkFunction();
376
377
378
379
    addFunction(f);
  }
}

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

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

405
  IFunction_sptr fun = getFunction(i);
406

407
  size_t dnp = fun->nParams();
408

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

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

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

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

439
  m_functions.erase(m_functions.begin() + i);
440
441
}

442
/** Replace a function with a new one. The old function is deleted.
443
 *  The new function must have already its workspace set.
444
 * @param f_old :: The pointer to the function to replace. If it's not
445
 *  a member of this composite function nothing happens
446
 * @param f_new :: A pointer to the new function
447
 */
448
449
450
451
452
453
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;
454
  std::vector<IFunction_sptr>::difference_type iFun = it - m_functions.begin();
455
  replaceFunction(iFun, f_new);
456
457
}

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

466
  IFunction_sptr fun = getFunction(i);
467
  size_t np_old = fun->nParams();
468

469
  size_t np_new = f->nParams();
470

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

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

  m_functions[i] = f;
}

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

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

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

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

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

547
    name = varName.substr(i + 1);
548
  }
549
550
}

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

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

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

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

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

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

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

/**
 * Declare a new parameter. To used in the implementation'c constructor.
618
619
 * @param name :: The parameter name.
 * @param initValue :: The initial value for the parameter
620
 * @param description :: Parameter documentation
621
 */
622
623
624
625
626
627
628
629
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.");
630
631
632
}

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

641
642
643
/**
 * Prepare the function for a fit.
 */
644
void CompositeFunction::setUpForFit() {
645
  // set up the member functions
646
  for (size_t i = 0; i < nFunctions(); i++) {
647
    getFunction(i)->setUpForFit();
648
  }
649
650
651
652
  // 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
  //
653
  // if parameters have non-constant ties enable numerical derivatives
654
  // for(size_t i = 0; i < nParams(); ++i)
655
656
657
658
659
660
661
662
663
664
665
  //{
  //  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
666
667
668
669
670
671
  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;
672
673
        break;
      }
674
    }
675
676
677
  }
}

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

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

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

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

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

741
742
} // namespace API
} // namespace Mantid