22 #ifndef _nmrPolynomialContainer_h
23 #define _nmrPolynomialContainer_h
27 #define MAP_CONTAINER 1
28 #define LIST_CONTAINER 2
29 #define CONTAINER_TYPE LIST_CONTAINER
31 #if (CONTAINER_TYPE == MAP_CONTAINER)
33 #elif (CONTAINER_TYPE == LIST_CONTAINER)
58 template<
class _TermInfo>
66 #if (CONTAINER_TYPE == MAP_CONTAINER)
68 #elif (CONTAINER_TYPE == LIST_CONTAINER)
73 typedef typename TermContainerType::value_type
TermType;
78 #if (CONTAINER_TYPE == LIST_CONTAINER)
93 return (Target.Compare(element.first) == 0);
113 return (Target.Compare(element.first) <= 0);
123 :
BaseType(numVariables, minDegree, maxDegree)
139 return Terms.empty();
145 if (newMin > this->MaxDegree) {
146 throw std::runtime_error(
"nmrPolynomialContainer: Attempt to set the min degree higher than max");
149 this->MinDegree = newMin;
152 const TermType & firstTerm = *(Terms.begin());
155 if (newMin > firstDegree) {
156 throw std::runtime_error(
"nmrPolynomialContainer: Attempt to set the min degree above existing term");
164 if (newMax < this->MinDegree) {
165 throw std::runtime_error(
"nmrPolynomialContainer: Attempt to set the max degree lower than min");
168 this->MaxDegree = newMax;
171 const TermType & lastTerm = *(Terms.rbegin());
174 if (newMax < lastDegree) {
175 throw std::runtime_error(
"nmrPolynomialContainer: Attempt to set the max degree below existing term");
189 if (termIterator == EndTermIterator())
199 #if (CONTAINER_TYPE == MAP_CONTAINER)
200 return Terms.find(target) != Terms.end();
201 #elif (CONTAINER_TYPE == LIST_CONTAINER)
202 return std::find_if(Terms.begin(), Terms.end(), EqualityTester(target)) != Terms.end();
219 for (; termIterator != endTermIterator; ++termIterator) {
220 if ( (termIterator->first).Compare(term) == 0 ) {
235 for (; (index < position) && (result != endTermIterator); ++index, ++result)
243 {
return Terms.begin(); }
245 {
return Terms.begin(); }
250 {
return Terms.end(); }
252 {
return Terms.end(); }
258 #if (CONTAINER_TYPE == MAP_CONTAINER)
259 return Terms.find(target);
260 #elif (CONTAINER_TYPE == LIST_CONTAINER)
261 return std::find_if(Terms.begin(), Terms.end(), EqualityTester(target));
266 #if (CONTAINER_TYPE == MAP_CONTAINER)
267 return Terms.find(target);
268 #elif (CONTAINER_TYPE == LIST_CONTAINER)
269 return std::find_if(Terms.begin(), Terms.end(), EqualityTester(target));
279 if (foundTerm != EndTermIterator()) {
301 virtual InsertStatus
SetCoefficient(TermIteratorType & where, CoefficientType coefficient) = 0;
304 virtual CoefficientType
GetCoefficient(
const TermConstIteratorType & where)
const = 0;
305 virtual CoefficientType
GetCoefficient(
const TermIteratorType & where)
const = 0;
311 int coefficientIndex = 0;
312 for (; termIterator != EndTermIterator(); ++termIterator, ++coefficientIndex) {
322 int coefficientIndex = 0;
323 for (; termIterator != EndTermIterator(); ++termIterator, ++coefficientIndex) {
329 {
return where->first; }
331 {
return where->first; }
333 #if INCLUDE_DEPRECATED_POLYNOMIAL_CODE
338 ValueType
EvaluateBasis(
const TermConstIteratorType & where)
const
342 ValueType
EvaluateBasis(
const TermIteratorType & where)
const
346 #endif // INCLUDE_DEPRECATED_POLYNOMIAL_CODE
347 virtual ValueType
EvaluateBasis(
const TermConstIteratorType & where,
349 virtual ValueType
EvaluateBasis(
const TermIteratorType & where,
379 while (termIt != endIt) {
395 for (; termIt != endIt; ++termIt, ++valuePtr) {
410 for (; termIt != endIt; ++termIt, ++coefficientIndex) {
411 polyVal += BaseType::EvaluateTerm(termIt->first,
412 variables, *coefficientIndex);
423 for (; termIt != endIt; ++termIt) {
431 virtual void AddConstant(CoefficientType shiftAmount) = 0;
435 CoefficientType shiftAmount)
const = 0;
438 #if DEFINE_FORMATTED_OUTPUT
439 virtual char * Format(
char * buffer)
const
441 TermConstIteratorType termIterator = FirstTermIterator();
442 TermConstIteratorType endIterator = EndTermIterator();
443 while (termIterator != endIterator) {
445 buffer += strlen(buffer);
446 sprintf(buffer,
"{");
447 buffer+=strlen(buffer);
448 buffer = GetTermPowerIndex(termIterator).FormatPowers(buffer);
449 sprintf(buffer,
"}\n");
450 buffer += strlen(buffer);
468 virtual void SerializeTermInfo(std::ostream & output,
const TermConstIteratorType & termIterator)
const = 0;
478 virtual void DeserializeTermInfo(std::istream & input, TermIteratorType & termIterator) = 0;
487 BaseType::SerializeRaw(output);
489 output.write( (
const char *)&numTerms,
sizeof(numTerms));
491 for (; termIterator != EndTermIterator(); ++termIterator) {
492 GetTermPowerIndex(termIterator).SerializeIndexRaw(output);
493 SerializeTermInfo(output, termIterator);
505 BaseType::DeserializeRaw(input);
507 input.read( (
char *)&numTermsLeft,
sizeof(numTermsLeft));
508 for (; numTermsLeft > 0; --numTermsLeft) {
512 #if (CONTAINER_TYPE == MAP_CONTAINER)
513 Container[termIndex] = termInfo;
514 #elif (CONTAINER_TYPE == LIST_CONTAINER)
518 DeserializeTermInfo(input, termIterator);
528 return termIterator->second;
534 return termIterator->second;
540 #endif // _nmrPolynomialContainer_h
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
const nmrPolynomialTermPowerIndex & GetTermPowerIndex(const TermIteratorType &where) const
Definition: nmrPolynomialContainer.h:330
virtual bool IncludesIndex(const nmrPolynomialTermPowerIndex &target) const
Definition: nmrPolynomialContainer.h:197
virtual void SetMinDegree(PowerType newMin)
Definition: nmrPolynomialContainer.h:143
TermContainerType::iterator TermIteratorType
Definition: nmrPolynomialContainer.h:74
nmrPolynomialContainer(VariableIndexType numVariables, PowerType minDegree, PowerType maxDegree)
Definition: nmrPolynomialContainer.h:122
virtual bool IsEmpty() const =0
virtual void DeserializeRaw(std::istream &input)
Definition: nmrPolynomialContainer.h:502
nmrPolynomialTermPowerIndex::MultinomialCoefficientType TermCounterType
Definition: nmrPolynomialBase.h:59
VariableIndexType GetNumVariables() const
Definition: nmrPolynomialBase.h:69
_TermInfo TermInfoType
Definition: nmrPolynomialContainer.h:64
virtual TermCounterType GetNumberOfTerms() const =0
double ValueType
Definition: nmrPolynomialBase.h:54
virtual ValueType EvaluateForCoefficients(const nmrMultiVariablePowerBasis &variables, const CoefficientType coefficients[]) const
Definition: nmrPolynomialContainer.h:401
virtual CoefficientType GetCoefficient(const nmrPolynomialTermPowerIndex &CMN_UNUSED(where)) const
Definition: nmrPolynomialBase.h:191
ValueType EvaluateTerm(const TermConstIteratorType &where, const nmrMultiVariablePowerBasis &variables) const
Definition: nmrPolynomialContainer.h:361
LessOrEqualityTester(const nmrPolynomialTermPowerIndex &target)
Definition: nmrPolynomialContainer.h:107
std::list< ContainerElementType > TermContainerType
Definition: nmrPolynomialContainer.h:70
virtual ValueType Evaluate(const nmrMultiVariablePowerBasis &variables) const
Definition: nmrPolynomialContainer.h:373
const nmrPolynomialTermPowerIndex & GetTermPowerIndex(const TermConstIteratorType &where) const
Definition: nmrPolynomialContainer.h:328
ValueType EvaluateTerm(const TermConstIteratorType &where) const
Definition: nmrPolynomialContainer.h:353
virtual void FillAllTerms()
Definition: nmrPolynomialContainer.h:182
Definition: nmrPolynomialBase.h:51
TermIteratorType FirstTermIterator()
Definition: nmrPolynomialContainer.h:242
const TermInfoType & GetTermInfo(const TermConstIteratorType &termIterator) const
Definition: nmrPolynomialContainer.h:525
virtual ~nmrPolynomialContainer()
Definition: nmrPolynomialContainer.h:126
nmrPolynomialTermPowerIndex::PowerType PowerType
Definition: nmrPolynomialBase.h:58
TermConstIteratorType FindTerm(const nmrPolynomialTermPowerIndex &target) const
Definition: nmrPolynomialContainer.h:264
TermContainerType::const_iterator TermConstIteratorType
Definition: nmrPolynomialContainer.h:75
virtual void RestoreCoefficients(const CoefficientType source[])
Definition: nmrPolynomialContainer.h:319
Definition: nmrPolynomialContainer.h:84
virtual ValueType EvaluateBasis(const nmrPolynomialTermPowerIndex &where, const nmrMultiVariablePowerBasis &variables) const =0
virtual void AddConstantToCoefficients(CoefficientType coefficients[], CoefficientType shiftAmount) const =0
virtual void AddConstant(CoefficientType shiftAmount)=0
ValueType EvaluateTerm(const TermIteratorType &where, const nmrMultiVariablePowerBasis &variables) const
Definition: nmrPolynomialContainer.h:366
TermConstIteratorType FirstTermIterator() const
Definition: nmrPolynomialContainer.h:244
virtual TermCounterType GetNumberOfTerms() const
Definition: nmrPolynomialContainer.h:131
TermContainerType Terms
Definition: nmrPolynomialContainer.h:523
void DeserializeIndexRaw(std::istream &input)
PowerType GetMaxDegree() const
Definition: nmrPolynomialBase.h:79
Definition: nmrPolynomialContainer.h:104
virtual InsertStatus SetCoefficient(const nmrPolynomialTermPowerIndex &where, CoefficientType coefficient)=0
virtual void EvaluateBasisVector(const nmrMultiVariablePowerBasis &variables, ValueType termBaseValues[]) const
Definition: nmrPolynomialContainer.h:388
Definition: nmrPolynomialContainer.h:59
virtual void SetMaxDegree(PowerType newMax)
Definition: nmrPolynomialContainer.h:162
int GetDegree() const
Definition: nmrPolynomialTermPowerIndex.h:121
TermInfoType & GetTermInfo(const TermConstIteratorType &termIterator)
Definition: nmrPolynomialContainer.h:531
TermConstIteratorType EndTermIterator() const
Definition: nmrPolynomialContainer.h:251
TermIteratorType FindTerm(const nmrPolynomialTermPowerIndex &target)
Definition: nmrPolynomialContainer.h:256
double CoefficientType
Definition: nmrPolynomialBase.h:56
ValueType EvaluateTerm(const nmrPolynomialTermPowerIndex &where, const nmrMultiVariablePowerBasis &variables) const
Definition: nmrPolynomialBase.h:304
ValueType EvaluateTerm(const TermIteratorType &where) const
Definition: nmrPolynomialContainer.h:357
TermIteratorType GetTermIteratorForPosition(TermCounterType position)
Definition: nmrPolynomialContainer.h:230
virtual void SerializeRaw(std::ostream &output) const
Definition: nmrPolynomialContainer.h:485
TermContainerType::value_type TermType
Definition: nmrPolynomialContainer.h:73
nmrPolynomialBase BaseType
Definition: nmrPolynomialContainer.h:62
nmrPolynomialTermPowerIndex::VariableIndexType VariableIndexType
Definition: nmrPolynomialBase.h:57
PowerType GetMinDegree() const
Definition: nmrPolynomialBase.h:74
bool IsValid() const
Definition: nmrPolynomialTermPowerIndex.h:161
virtual bool IsEmpty() const
Definition: nmrPolynomialContainer.h:137
virtual void Clear()
Definition: nmrPolynomialContainer.h:291
virtual void RemoveTerm(TermIteratorType &where)
Definition: nmrPolynomialContainer.h:286
std::pair< nmrPolynomialTermPowerIndex, TermInfoType > ContainerElementType
Definition: nmrPolynomialContainer.h:69
Definition: nmrMultiVariablePowerBasis.h:37
virtual void Scale(CoefficientType scaleFactor)
Definition: nmrPolynomialContainer.h:419
virtual TermCounterType GetIndexPosition(const nmrPolynomialTermPowerIndex &term) const
Definition: nmrPolynomialContainer.h:210
virtual void RemoveTerm(const nmrPolynomialTermPowerIndex &where)
Definition: nmrPolynomialContainer.h:276
Represents the power index of a single term in a multi-variable polynomial.
Definition: nmrPolynomialTermPowerIndex.h:89
virtual void RemoveTerm(const nmrPolynomialTermPowerIndex &where)=0
TermIteratorType EndTermIterator()
Definition: nmrPolynomialContainer.h:249
EqualityTester(const nmrPolynomialTermPowerIndex &target)
Definition: nmrPolynomialContainer.h:87
virtual void CollectCoefficients(CoefficientType target[]) const
Definition: nmrPolynomialContainer.h:308