CompositeFunction.cpp 24.5 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
    const auto localAttr = this->getLocalAttributeNames();
72
    IFunction_sptr fun = getFunction(i);
Hahn, Steven's avatar
Hahn, Steven committed
73
74
    bool isComp =
        boost::dynamic_pointer_cast<CompositeFunction>(fun) != nullptr;
75
76
    if (isComp)
      ostr << '(';
77
    ostr << fun->asString();
78
79
80
81
82
83
84
85
    for (const auto &localAttName : localAttr) {
      const std::string localAttValue =
          this->getLocalAttribute(i, localAttName).value();
      if (!localAttValue.empty()) {
        // local attribute names are prefixed by dollar sign
        ostr << ',' << '$' << localAttName << '=' << localAttValue;
      }
    }
86
87
88
    if (isComp)
      ostr << ')';
    if (i < nFunctions() - 1) {
89
90
91
92
      ostr << ';';
    }
  }
  std::string ties;
93
94
95
  for (size_t i = 0; i < nParams(); i++) {
    const ParameterTie *tie = getTie(i);
    if (tie) {
96
97
      IFunction_sptr fun = getFunction(functionIndex(i));
      std::string tmp = tie->asString(fun.get());
98
      if (tmp.empty()) {
99
        tmp = tie->asString(this);
100
101
        if (!tmp.empty()) {
          if (!ties.empty()) {
102
103
104
105
106
107
108
            ties += ",";
          }
          ties += tmp;
        }
      }
    }
  }
109
  if (!ties.empty()) {
110
    ostr << ";ties=(" << ties << ")";
111
112
113
114
  }
  return ostr.str();
}

115
116
117
/**
 * @param ws A pointer to the workspace being fitted
 */
118
void CompositeFunction::setWorkspace(boost::shared_ptr<const Workspace> ws) {
119
120
  // Pass it on to each member
  auto iend = m_functions.end();
121
  for (auto it = m_functions.begin(); it != iend; ++it) {
122
123
124
125
    (*it)->setWorkspace(ws);
  }
}

126
127
128
129
130
131
/**
 * @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.
 */
132
133
134
135
136
137
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);
  }
138
139
}

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

172
/** Sets a new value to the i-th parameter.
173
174
 *  @param i :: The parameter index
 *  @param value :: The new value
175
176
 *  @param explicitlySet :: A boolean falgging the parameter as explicitly set
 * (by user)
177
 */
178
179
void CompositeFunction::setParameter(size_t i, const double &value,
                                     bool explicitlySet) {
180
  size_t iFun = functionIndex(i);
181
182
  m_functions[iFun]->setParameter(i - m_paramOffsets[iFun], value,
                                  explicitlySet);
183
184
}

185
186
187
188
/** Sets a new description to the i-th parameter.
 *  @param i :: The parameter index
 *  @param description :: The new description
 */
189
190
void CompositeFunction::setParameterDescription(
    size_t i, const std::string &description) {
191
  size_t iFun = functionIndex(i);
192
193
  m_functions[iFun]->setParameterDescription(i - m_paramOffsets[iFun],
                                             description);
194
195
}

196
/** Get the i-th parameter.
197
 *  @param i :: The parameter index
198
 *  @return value of the requested parameter
199
 */
200
double CompositeFunction::getParameter(size_t i) const {
Peterson, Peter's avatar
Peterson, Peter committed
201
  size_t iFun = functionIndex(i);
202
  return m_functions[iFun]->getParameter(i - m_paramOffsets[iFun]);
203
204
}

205
206
/**
 * Sets a new value to a parameter by name.
207
208
 * @param name :: The name of the parameter.
 * @param value :: The new value
209
210
 * @param explicitlySet :: A boolean falgging the parameter as explicitly set
 * (by user)
211
 */
212
213
void CompositeFunction::setParameter(const std::string &name,
                                     const double &value, bool explicitlySet) {
214
  std::string pname;
215
  size_t index;
216
217
  parseName(name, index, pname);
  getFunction(index)->setParameter(pname, value, explicitlySet);
218
219
}

220
221
222
/**
 * Sets a new description to a parameter by name.
 * @param name :: The name of the parameter.
223
 * @param description :: The new description
224
 */
225
226
void CompositeFunction::setParameterDescription(
    const std::string &name, const std::string &description) {
227
  std::string pname;
228
  size_t index;
229
230
  parseName(name, index, pname);
  getFunction(index)->setParameterDescription(pname, description);
231
232
}

233
234
/**
 * Parameters by name.
235
 * @param name :: The name of the parameter.
236
 * @return value of the requested named parameter
237
 */
238
double CompositeFunction::getParameter(const std::string &name) const {
239
  std::string pname;
240
  size_t index;
241
  parseName(name, index, pname);
242
  return getFunction(index)->getParameter(pname);
243
244
245
}

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

248
/**
249
 *
250
 * @param name :: The name of a parameter
251
 * @return index of the requested named parameter
252
 */
253
size_t CompositeFunction::parameterIndex(const std::string &name) const {
254
  std::string pname;
255
  size_t index;
256
  parseName(name, index, pname);
257
  return getFunction(index)->parameterIndex(pname) + m_paramOffsets[index];
258
259
}

Nick Draper's avatar
re #100    
Nick Draper committed
260
/// Returns the name of parameter
261
/// @param i :: The index
Nick Draper's avatar
re #100    
Nick Draper committed
262
/// @return The name of the parameter
263
std::string CompositeFunction::parameterName(size_t i) const {
264
  size_t iFun = functionIndex(i);
265
  std::ostringstream ostr;
266
267
  ostr << 'f' << iFun << '.'
       << m_functions[iFun]->parameterName(i - m_paramOffsets[iFun]);
268
  return ostr.str();
269
270
}

271
272
273
/// Returns the description of parameter
/// @param i :: The index
/// @return The description of the parameter
274
std::string CompositeFunction::parameterDescription(size_t i) const {
275
  size_t iFun = functionIndex(i);
276
  std::ostringstream ostr;
277
  ostr << m_functions[iFun]->parameterDescription(i - m_paramOffsets[iFun]);
278
279
280
  return ostr.str();
}

281
/**
282
283
284
285
 * Get the fitting error for a parameter
 * @param i :: The index of a parameter
 * @return :: the error
 */
286
double CompositeFunction::getError(size_t i) const {
287
  size_t iFun = functionIndex(i);
288
  return m_functions[iFun]->getError(i - m_paramOffsets[iFun]);
289
290
}

291
/**
292
293
294
295
 * Set the fitting error for a parameter
 * @param i :: The index of a parameter
 * @param err :: The error value to set
 */
296
void CompositeFunction::setError(size_t i, double err) {
297
  size_t iFun = functionIndex(i);
298
  m_functions[iFun]->setError(i - m_paramOffsets[iFun], err);
299
300
}

301
302
303
/// Value of i-th active parameter. Override this method to make fitted
/// parameters different from the declared
double CompositeFunction::activeParameter(size_t i) const {
304
  size_t iFun = functionIndex(i);
305
  return m_functions[iFun]->activeParameter(i - m_paramOffsets[iFun]);
306
307
}

308
309
310
/// 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) {
311
  size_t iFun = functionIndex(i);
312
  m_functions[iFun]->setActiveParameter(i - m_paramOffsets[iFun], value);
313
314
315
}

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

/// Returns the description of active parameter i
325
std::string CompositeFunction::descriptionOfActive(size_t i) const {
326
  size_t iFun = functionIndex(i);
327
  std::ostringstream ostr;
328
  ostr << m_functions[iFun]->descriptionOfActive(i - m_paramOffsets[iFun]);
329
  return ostr.str();
330
331
}

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

342
/**
343
 * query to see in the function is active
344
 * @param i :: The index of a declared parameter
345
 * @return true if parameter i is active
346
 */
347
bool CompositeFunction::isFixed(size_t i) const {
348
  size_t iFun = functionIndex(i);
349
  return m_functions[iFun]->isFixed(i - m_paramOffsets[iFun]);
350
351
352
}

/**
353
 * @param i :: A declared parameter index to be removed from active
354
 */
355
void CompositeFunction::fix(size_t i) {
356
  size_t iFun = functionIndex(i);
357
  m_functions[iFun]->fix(i - m_paramOffsets[iFun]);
358
359
}

360
/** Makes a parameter active again. It doesn't change the parameter's tie.
361
 * @param i :: A declared parameter index to be restored to active
362
 */
363
void CompositeFunction::unfix(size_t i) {
364
  size_t iFun = functionIndex(i);
365
  m_functions[iFun]->unfix(i - m_paramOffsets[iFun]);
366
367
}

368
/** Makes sure that the function is consistent.
369
 */
370
void CompositeFunction::checkFunction() {
371
372
  m_nParams = 0;
  m_paramOffsets.clear();
373
  m_IFunction.clear();
374

375
  std::vector<IFunction_sptr> functions(m_functions.begin(), m_functions.end());
376
377
  m_functions.clear();

Hahn, Steven's avatar
Hahn, Steven committed
378
  for (auto &f : functions) {
379
380
381
382
    CompositeFunction_sptr cf =
        boost::dynamic_pointer_cast<CompositeFunction>(f);
    if (cf)
      cf->checkFunction();
383
384
385
386
    addFunction(f);
  }
}

387
/**
388
389
390
391
392
393
394
395
396
 * Remove all member functions
 */
void CompositeFunction::clear() {
  m_nParams = 0;
  m_paramOffsets.clear();
  m_IFunction.clear();
  m_functions.clear();
}

397
/** Add a function
398
 * @param f :: A pointer to the added function
399
 * @return The function index
400
 */
401
402
size_t CompositeFunction::addFunction(IFunction_sptr f) {
  m_IFunction.insert(m_IFunction.end(), f->nParams(), m_functions.size());
403
404
  m_functions.push_back(f);
  //?f->init();
405
  if (m_paramOffsets.empty()) {
406
407
    m_paramOffsets.push_back(0);
    m_nParams = f->nParams();
408
  } else {
409
410
411
    m_paramOffsets.push_back(m_nParams);
    m_nParams += f->nParams();
  }
412
  return m_functions.size() - 1;
413
414
}

415
/** Remove a function
416
 * @param i :: The index of the function to remove
417
 */
418
void CompositeFunction::removeFunction(size_t i) {
419
420
421
422
  if (i >= nFunctions()) {
    throw std::out_of_range("Function index (" + std::to_string(i) +
                            ") out of range (" + std::to_string(nFunctions()) + ").");
  }
423

424
  IFunction_sptr fun = getFunction(i);
425

426
  size_t dnp = fun->nParams();
427

428
429
430
  for (size_t j = 0; j < nParams();) {
    ParameterTie *tie = getTie(j);
    if (tie && tie->findParametersOf(fun.get())) {
431
      removeTie(j);
432
    } else {
433
434
435
436
437
      j++;
    }
  }

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

440
    if (*it == i) {
441
      it = m_IFunction.erase(it);
442
443
    } else {
      if (*it > i) {
444
445
        *it -= 1;
      }
446
      ++it;
447
448
449
450
    }
  }

  m_nParams -= dnp;
451
452
453
  // Shift the parameter offsets down by the total number of i-th function's
  // params
  for (size_t j = i + 1; j < nFunctions(); j++) {
454
455
    m_paramOffsets[j] -= dnp;
  }
456
  m_paramOffsets.erase(m_paramOffsets.begin() + i);
457

458
  m_functions.erase(m_functions.begin() + i);
459
460
}

461
/** Replace a function with a new one. The old function is deleted.
462
 *  The new function must have already its workspace set.
463
 * @param f_old :: The pointer to the function to replace. If it's not
464
 *  a member of this composite function nothing happens
465
 * @param f_new :: A pointer to the new function
466
 */
467
468
469
470
471
472
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;
473
  std::vector<IFunction_sptr>::difference_type iFun = it - m_functions.begin();
474
  replaceFunction(iFun, f_new);
475
476
}

477
/** Replace a function with a new one. The old function is deleted.
478
479
 * @param i :: The index of the function to replace
 * @param f :: A pointer to the new function
480
 */
481
void CompositeFunction::replaceFunction(size_t i, IFunction_sptr f) {
482
483
484
485
486
  if (i >= nFunctions()) {
    throw std::out_of_range("Function index (" + std::to_string(i) +
                        ") out of range (" + std::to_string(nFunctions()) + ").");

  }
487

488
  IFunction_sptr fun = getFunction(i);
489
  size_t np_old = fun->nParams();
490

491
  size_t np_new = f->nParams();
492

493
494
  // Modify function indeces: The new function may have different number of
  // parameters
495
  {
496
    auto itFun = std::find(m_IFunction.begin(), m_IFunction.end(), i);
497
    if (itFun != m_IFunction.end()) // functions must have at least 1 parameter
498
    {
499
500
501
502
      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);
503
      }
504
505
    } else if (np_new > 0) // it could happen if the old function is an empty
                           // CompositeFunction
506
    {
507
508
509
      itFun = std::find_if(m_IFunction.begin(), m_IFunction.end(),
                           std::bind2nd(std::greater<size_t>(), i));
      m_IFunction.insert(itFun, np_new, i);
510
    }
511
512
  }

513
  size_t dnp = np_new - np_old;
514
  m_nParams += dnp;
515
516
517
  // Shift the parameter offsets down by the total number of i-th function's
  // params
  for (size_t j = i + 1; j < nFunctions(); j++) {
518
519
520
521
522
523
    m_paramOffsets[j] += dnp;
  }

  m_functions[i] = f;
}

524
/**
525
 * @param i :: The index of the function
526
 * @return function at the requested index
527
 */
528
529
IFunction_sptr CompositeFunction::getFunction(std::size_t i) const {
  if (i >= nFunctions()) {
530
531
    throw std::out_of_range("Function index (" + std::to_string(i) +
                            ") out of range (" + std::to_string(nFunctions()) + ").");
532
  }
533
534
535
  return m_functions[i];
}

536
537
/**
 * Get the index of the function to which parameter i belongs
538
 * @param i :: The parameter index
539
 * @return function index of the requested parameter
540
 */
541
542
size_t CompositeFunction::functionIndex(std::size_t i) const {
  if (i >= nParams()) {
543
544
    throw std::out_of_range("Function parameter index (" + std::to_string(i) +
                            ") out of range (" + std::to_string(nParams()) + ").");
545
  }
546
  return m_IFunction[i];
547
548
549
}

/**
550
551
* @param varName :: The variable name which may contain function index (
* [f<index.>]name )
552
* @param index :: Receives function index or throws std::invalid_argument
553
* @param name :: Receives the parameter name
554
*/
555
556
void CompositeFunction::parseName(const std::string &varName, size_t &index,
                                  std::string &name) {
557
  size_t i = varName.find('.');
558
  if (i == std::string::npos) {
559
    throw std::invalid_argument("Parameter " + varName + " not found.");
560
  } else {
561
    if (varName[0] != 'f')
562
563
      throw std::invalid_argument(
          "External function parameter name must start with 'f'");
564

565
    std::string sindex = varName.substr(1, i - 1);
566
567
568
569
570
    index = boost::lexical_cast<int>(sindex);

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

571
    name = varName.substr(i + 1);
572
  }
573
574
}

575
/** Returns the index of parameter i as it declared in its function
576
 * @param i :: The parameter index
577
578
 * @return The local index of the parameter
 */
579
size_t CompositeFunction::parameterLocalIndex(size_t i) const {
580
  size_t iFun = functionIndex(i);
581
582
583
  return i - m_paramOffsets[iFun];
}

584
/** Returns the name of parameter i as it declared in its function
585
 * @param i :: The parameter index
586
587
 * @return The pure parameter name (without the function identifier f#.)
 */
588
std::string CompositeFunction::parameterLocalName(size_t i) const {
589
  size_t iFun = functionIndex(i);
590
  return m_functions[iFun]->parameterName(i - m_paramOffsets[iFun]);
591
592
}

593
/**
594
 * Apply the ties.
595
 */
596
597
void CompositeFunction::applyTies() {
  for (size_t i = 0; i < nFunctions(); i++) {
598
599
600
601
602
    getFunction(i)->applyTies();
  }
}

/**
603
 * Clear the ties.
604
 */
605
606
void CompositeFunction::clearTies() {
  for (size_t i = 0; i < nFunctions(); i++) {
607
608
609
610
611
    getFunction(i)->clearTies();
  }
}

/** Removes i-th parameter's tie if it is tied or does nothing.
612
 * @param i :: The index of the tied parameter.
613
614
 * @return True if successfull
 */
615
bool CompositeFunction::removeTie(size_t i) {
616
  size_t iFun = functionIndex(i);
617
  bool res = m_functions[iFun]->removeTie(i - m_paramOffsets[iFun]);
618
619
620
621
  return res;
}

/** Get the tie of i-th parameter
622
 * @param i :: The parameter index
623
 * @return A pointer to the tie.
624
 */
625
ParameterTie *CompositeFunction::getTie(size_t i) const {
626
  size_t iFun = functionIndex(i);
627
  return m_functions[iFun]->getTie(i - m_paramOffsets[iFun]);
628
629
630
631
}

/**
 * Attaches a tie to this function. The attached tie is owned by the function.
632
 * @param tie :: A pointer to a new tie
633
 */
634
void CompositeFunction::addTie(ParameterTie *tie) {
635
636
  size_t i = getParameterIndex(*tie);
  size_t iFun = functionIndex(i);
637
638
639
640
641
  m_functions[iFun]->addTie(tie);
}

/**
 * Declare a new parameter. To used in the implementation'c constructor.
642
643
 * @param name :: The parameter name.
 * @param initValue :: The initial value for the parameter
644
 * @param description :: Parameter documentation
645
 */
646
647
648
649
650
651
652
653
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.");
654
655
656
}

/** Add a constraint
657
 *  @param ic :: Pointer to a constraint.
658
 */
659
void CompositeFunction::addConstraint(IConstraint *ic) {
660
661
  size_t i = getParameterIndex(*ic);
  size_t iFun = functionIndex(i);
662
  getFunction(iFun)->addConstraint(ic);
663
664
}

665
666
667
/**
 * Prepare the function for a fit.
 */
668
void CompositeFunction::setUpForFit() {
669
  // set up the member functions
670
  for (size_t i = 0; i < nFunctions(); i++) {
671
    getFunction(i)->setUpForFit();
672
  }
673
674
675
676
  // 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
  //
677
  // if parameters have non-constant ties enable numerical derivatives
678
  // for(size_t i = 0; i < nParams(); ++i)
679
680
681
682
683
684
685
686
687
688
689
  //{
  //  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
690
691
692
693
694
  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 "
695
                           "non-constant ties defined.\n";
696
697
        break;
      }
698
    }
699
700
701
  }
}

Nick Draper's avatar
re #100    
Nick Draper committed
702
/// Get constraint
703
/// @param i :: the index
Nick Draper's avatar
re #100    
Nick Draper committed
704
/// @return A pointer to the constraint
705
IConstraint *CompositeFunction::getConstraint(size_t i) const {
706
  size_t iFun = functionIndex(i);
707
  return m_functions[iFun]->getConstraint(i - m_paramOffsets[iFun]);
708
709
}

710
/** Remove a constraint
711
 * @param parName :: The name of a parameter which constarint to remove.
712
 */
713
void CompositeFunction::removeConstraint(const std::string &parName) {
714
715
  size_t iPar = parameterIndex(parName);
  size_t iFun = functionIndex(iPar);
716
717
718
  getFunction(iFun)->removeConstraint(parameterLocalName(iPar));
}

719
/** Checks if a constraint has been explicitly set
720
 *  @param i :: The parameter index
721
 *  @return true if the function is explicitly set
722
 */
723
bool CompositeFunction::isExplicitlySet(size_t i) const {
724
  size_t iFun = functionIndex(i);
725
  return m_functions[iFun]->isExplicitlySet(i - m_paramOffsets[iFun]);
726
727
}

728
/**
729
730
 * Returns the index of parameter if the ref points to one of the member
 * function
731
 * @param ref :: A reference to a parameter
732
 * @return Parameter index or number of nParams() if parameter not found
733
 */
734
735
736
size_t
CompositeFunction::getParameterIndex(const ParameterReference &ref) const {
  if (ref.getFunction() == this && ref.getIndex() < nParams()) {
737
    return ref.getIndex();
738
  }
739
  for (size_t iFun = 0; iFun < nFunctions(); iFun++) {
740
    IFunction_sptr fun = getFunction(iFun);
741
    size_t iLocalIndex = fun->getParameterIndex(ref);
742
    if (iLocalIndex < fun->nParams()) {
743
744
745
      return m_paramOffsets[iFun] + iLocalIndex;
    }
  }
746
  return nParams();
747
748
}

749
/**
750
 * Returns the shrared pointer to the function conataining a parameter
751
 * @param ref :: The reference
752
753
 * @return A function containing parameter pointed to by ref
 */
754
755
756
IFunction_sptr
CompositeFunction::getContainingFunction(const ParameterReference &ref) const {
  for (size_t iFun = 0; iFun < nFunctions(); iFun++) {
757
    IFunction_sptr fun = getFunction(iFun);
758
    if (fun->getParameterIndex(ref) < fun->nParams()) {
759
      return fun;
760
761
    }
  }
762
763
764
  return IFunction_sptr();
}

765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/// Get number of domains required by this function
size_t CompositeFunction::getNumberDomains() const {
  auto n = nFunctions();
  if (n == 0) {
    return 1;
  }
  size_t nd = getFunction(0)->getNumberDomains();
  for (size_t iFun = 1; iFun < n; ++iFun) {
    if (getFunction(0)->getNumberDomains() != nd) {
      throw std::runtime_error("CompositeFunction has members with "
                               "inconsistent domain numbers.");
    }
  }
  return nd;
}

/// Split this function (if needed) into a list of independent functions.
/// The number of functions must be the number of domains this function is
/// working on (== getNumberDomains()). The result of evaluation of the
/// created functions on their domains must be the same as if this function
/// was evaluated on the composition of those domains.
Roman Tolchenov's avatar
Roman Tolchenov committed
786
787
std::vector<IFunction_sptr>
CompositeFunction::createEquivalentFunctions() const {
788
789
790
791
792
793
794
795
796
  auto nd = getNumberDomains();
  if (nd == 1) {
    return std::vector<IFunction_sptr>(
        1, FunctionFactory::Instance().createInitialized(asString()));
  }

  auto nf = nFunctions();
  std::vector<std::vector<IFunction_sptr>> equiv;
  equiv.reserve(nf);
Roman Tolchenov's avatar
Roman Tolchenov committed
797
  for (size_t i = 0; i < nf; ++i) {
798
799
800
801
802
    equiv.push_back(getFunction(i)->createEquivalentFunctions());
  }

  std::vector<IFunction_sptr> funs;
  funs.reserve(nd);
Roman Tolchenov's avatar
Roman Tolchenov committed
803
  for (size_t i = 0; i < nd; ++i) {
804
805
    auto comp = new CompositeFunction;
    funs.push_back(IFunction_sptr(comp));
Roman Tolchenov's avatar
Roman Tolchenov committed
806
    for (size_t j = 0; j < nf; ++j) {