cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nmrPolynomialBase.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Ofri Sadowsky
7  Created on: 2001-10-16
8 
9  (C) Copyright 2001-2007 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 */
20 
21 
22 #ifndef _nmrPolynomialBase_h
23 #define _nmrPolynomialBase_h
24 
25 
26 #include <vector>
29 
31 
33 
34 // This control macro defines, when set to one, the inclusion of deprecated
35 // code in the polynomial classes. We everything that depends on a 'true'
36 // value of this macro to be erased from future releases of the cisst library.
37 #define INCLUDE_DEPRECATED_POLYNOMIAL_CODE 0
38 
39 // class nmrPolynomialBase is an abstract base class for various polynomial classes.
40 // Since different polynomial classes store different information for the terms (for
41 // example, a standard polynomial stores a coefficient, a Bernstein polynomial stores a
42 // coefficient and a multinomial factor), the base class defined the common subset of
43 // operations for a polynomial, but does not implement the storage for the terms, or
44 // the rules that relate the variables (e.g., the sum of variables is always one for
45 // a Bernstein polynomial).
46 //
47 // A term in the polynomial is identified by an index, which is the set of powers for
48 // all the variables of the polynomial in the term. See class nmrPolynomialTermPowerIndex.
49 //
50 // An implementation of a multi-variable polynomial is in class nmrPolynomialContainer.
52 {
53 public:
54  typedef double ValueType;
55  typedef double VariableType;
56  typedef double CoefficientType;
60 
61  enum InsertStatus { INSERT_FAIL = -1, INSERT_NEW = 0, INSERT_REPLACE = 1 };
62 
63  // Ctor determines the number of variables and the degree of the polynomial
64  nmrPolynomialBase(VariableIndexType numVariables, PowerType minDegree, PowerType maxDegree);
65 
67  {}
68 
70  {
71  return NumVariables;
72  }
73 
75  {
76  return MinDegree;
77  }
78 
80  {
81  return MaxDegree;
82  }
83 
84  // implemented in nmrPolynomialContainer
85  virtual TermCounterType GetNumberOfTerms() const = 0;
86 
87  // return the maximum possible number of terms for this polynomial, based on
88  // the degrees.
90  {
91  return (nmrPolynomialTermPowerIndex(*this)).CountPowerCombinations();
92  }
93 
94 #if INCLUDE_DEPRECATED_POLYNOMIAL_CODE
95  // Set the values of the polynomial variables to a specific n-dimensional
96  // point. This enables to pre-calculate the power basis for future
97  // evaluations.
98  // Implemented for each concrete polynomial class
99  virtual void CISST_DEPRECATED SetVariables(const VariableType vars[]) = 0;
100 
101  // Set the value of one variable
102  // Implemented for each concrete polynomial class.
103  virtual void CISST_DEPRECATED SetVariable(VariableIndexType varIndex, VariableType value) = 0;
104 
105  // Returns true if the value of variable indicated by the index can be set
106  // using SetVariable().
107  virtual bool CISST_DEPRECATED CanSetVariable(VariableIndexType varIndex) const = 0;
108 
109 #ifdef CISST_COMPILER_IS_MSVC
110 #pragma warning(push)
111 #pragma warning(disable:4996)
112 #endif // ifdef CISST_COMPILER_IS_MSVC
113  // Retrieve the values of the variables at the current point
114  void CISST_DEPRECATED GetVariables(VariableType vars[]) const
115  {
116  VariableIndexType v;
117  for (v = 0; v < GetNumVariables(); ++v)
118  vars[v] = GetVariable(v);
119  }
120 
121  // Retrieve the value of one variable
122  VariableType CISST_DEPRECATED GetVariable(VariableIndexType var) const
123  {
124  return VariablePowers->GetVariable(var);
125  }
126 
127  // Return a specific power of a specific variable
128  VariableType CISST_DEPRECATED GetVariablePower(VariableIndexType var, PowerType power) const
129  {
130  return VariablePowers->GetVariablePower(var, power);
131  }
132 
133 #ifdef CISST_COMPILER_IS_MSVC
134 #pragma warning(pop)
135 #endif // ifdef CISST_COMPILER_IS_MSVC
136 #endif // INCLUDE_DEPRECATED_POLYNOMIAL_CODE
137 
138  //: Tell if there are any nonzero terms in the polynomial.
139  // Implemented in nmrPolynomialContainer.
140  virtual bool IsEmpty() const = 0;
141 
142  //: Update the minimum allowable degree of this polynomial to the given
143  // input degree. The method verifies that the polynomial currently contains
144  // only terms of degree greater than or equal to the input, by looking at the
145  // degree of the first term, which is presumably the lowest. Note that terms
146  // already contained in this polynomial aren't modified.
147  virtual void SetMinDegree(PowerType newMin) throw(std::runtime_error) = 0;
148 
149 
150  //: Update the maximum allowable degree of this polynomial to the given
151  // input degree. The method verifies that the polynomial currently contains
152  // only terms of degree lower than or equal to the input by looking at the
153  // degree of the last term, which is presumably the highest. Note that terms
154  // already contained in this polynomial aren't modified.
155  virtual void SetMaxDegree(PowerType newMax) throw(std::runtime_error) = 0;
156 
157  //: Generate all possible terms for this polynomial, and store them for later
158  // use.
159  // Implemented in nmrPolynomialContainer.
160  virtual void FillAllTerms() = 0;
161 
162  // Tells if this polynomial can have the given term as one of its terms.
163  // In other words, if it's a valid term of this polynomial.
164  bool CanIncludeIndex(const nmrPolynomialTermPowerIndex& term) const;
165 
166  // Find a term by its coefficients. Return true is the term is found.
167  // return 'false' otherwise.
168  // Implemented in nmrPolynomialContainer.
169  virtual bool IncludesIndex(const nmrPolynomialTermPowerIndex& target) const = 0;
170 
171  // Find a term by its coefficients. If the term is found, return the sequential
172  // position (zero-based) of the term in the sequence of terms for this polynomial.
173  // If the term is not found, return a number greater than or equal to the current number
174  // of terms in the sequence.
175  virtual TermCounterType GetIndexPosition(const nmrPolynomialTermPowerIndex & term) const = 0;
176 
177  // Remove a term from the polynomial.
178  // Implemented in nmrPolynomialContainer.
179  virtual void RemoveTerm(const nmrPolynomialTermPowerIndex & where) = 0;
180 
181  // Remove all the terms to make an empty (zero) polynomial
182  // Implemented in nmrPolynomialContainer.
183  virtual void Clear() = 0;
184 
185  // Set a coefficient for the given term.
186  // Implemented for each concrete polynomial class.
187  virtual InsertStatus SetCoefficient(const nmrPolynomialTermPowerIndex & where, CoefficientType coefficient) = 0;
188 
189  // Retrieve the value of the user defined coefficient for a given term.
190  // Implemented for each concrete polynomial class.
192  {
193  return 0;
194  }
195 
196  // Store all the coefficients into the target array, ordered the same
197  // way as the terms in this polynomial. The user has to make sure that
198  // the target array has enough space to contain all the coefficients,
199  // and note that if this polynomial is modified then the target array is
200  // no longer valid.
201  virtual void CollectCoefficients(CoefficientType source[]) const = 0;
202 
203  // Restore all the coefficients from the source array, ordered the same
204  // way as the terms in this polynomial. The user has to make sure that
205  // the target array is ordered properly -- typically it's generated using
206  // CollectCoefficients().
207  virtual void RestoreCoefficients(const CoefficientType source[]) = 0;
208 
209 #if INCLUDE_DEPRECATED_POLYNOMIAL_CODE
210 #ifdef CISST_COMPILER_IS_MSVC
211 #pragma warning(push)
212 #pragma warning(disable:4996)
213 #endif // ifdef CISST_COMPILER_IS_MSVC
214  // Evaluate the product of powers for a given term index and the variables
215  // set in this object.
216  ValueType CISST_DEPRECATED EvaluatePowerProduct(const nmrPolynomialTermPowerIndex & where) const
217  {
218  CMN_ASSERT(this->CanIncludeIndex(where));
219  return VariablePowers->EvaluatePowerProduct(where.GetPowers());
220  }
221 
222  /* The function has been factored out to nmrMultiVariablePowerBasis */
223  ValueType CISST_DEPRECATED EvaluatePowerProduct(const PowerType powers[]) const
224  {
225  return VariablePowers->EvaluatePowerProduct(powers);
226  }
227 
236  ValueType CISST_DEPRECATED EvaluateBasis(const nmrPolynomialTermPowerIndex & where) const
237  {
238  return EvaluateBasis(where, *VariablePowers);
239  }
240 
241  // Evaluate a single term at the currently specified point, including
242  // the basis function and the term's. This function uses the internally
243  // stored coefficient.
244  ValueType CISST_DEPRECATED EvaluateTerm(const nmrPolynomialTermPowerIndex & where) const
245  {
246  CoefficientType coefficient = GetCoefficient(where);
247  return EvaluateTerm(where, coefficient);
248  }
249 
250  // Evaluate a single term at the currently specified point, including
251  // the basis function and the term's coefficient. This function takes
252  // the coefficient as a parameter, and does not use internally stored
253  // coefficients.
254  ValueType CISST_DEPRECATED EvaluateTerm(const nmrPolynomialTermPowerIndex & where, CoefficientType coefficient) const
255  {
256  return EvaluateTerm(where, *VariablePowers, coefficient);
257  }
258 
262  void CISST_DEPRECATED EvaluateBasisVector(ValueType termBaseValues[]) const
263  {
264  EvaluateBasisVector(*VariablePowers, termBaseValues);
265  }
266 
267  // Evaluate the polynomial at the currently specified point.
268  // Implemented in nmrPolynomialContainer.
269  ValueType CISST_DEPRECATED Evaluate()
270  {
271  return Evaluate(*VariablePowers);
272  }
273 
274  // Evaluate the polynomial at the currently specified point, using
275  // externally defined coefficients. This lets the user create many
276  // polynomials over the same set of basis functions without having to
277  // duplicate the terms.
278  // The input coefficients must be ordered the same way as the terms in this
279  // object. It is best to order them using CollectCoefficients()
280  ValueType CISST_DEPRECATED EvaluateForCoefficients(const CoefficientType coefficients[]) const
281  {
282  return EvaluateForCoefficients(*VariablePowers, coefficients);
283  }
284 #ifdef CISST_COMPILER_IS_MSVC
285 #pragma warning(pop)
286 #endif // ifdef CISST_COMPILER_IS_MSVC
287 #endif // INCLUDE_DEPRECATED_POLYNOMIAL_CODE
288 
297  virtual ValueType EvaluateBasis(const nmrPolynomialTermPowerIndex & where,
298  const nmrMultiVariablePowerBasis & variables) const = 0;
299 
305  const nmrMultiVariablePowerBasis & variables) const
306  {
307  CoefficientType coefficient = GetCoefficient(where);
308  return EvaluateTerm(where, variables, coefficient);
309  }
310 
316  const nmrMultiVariablePowerBasis & variables, CoefficientType coefficient) const
317  {
318  if (coefficient == 0)
319  return 0;
320  return coefficient * EvaluateBasis(where, variables);
321  }
322 
327  virtual void EvaluateBasisVector(const nmrMultiVariablePowerBasis & variables,
328  ValueType termBaseValues[]) const = 0;
329 
338  virtual ValueType EvaluateForCoefficients(const nmrMultiVariablePowerBasis & variables,
339  const CoefficientType coefficients[]) const = 0;
340 
342  virtual ValueType Evaluate(const nmrMultiVariablePowerBasis & variables)
343  const = 0;
344 
345 
349  virtual void Scale(CoefficientType scaleFactor) = 0;
350 
369  void CISST_DEPRECATED ScaleCoefficients(CoefficientType coefficients[], CoefficientType scaleFactor) const;
370 
382  virtual void AddConstant(CoefficientType shiftAmount) = 0;
383 
403  virtual void AddConstantToCoefficients(CoefficientType coefficients[],
404  CoefficientType shiftAmount) const = 0;
405 
406 #if DEFINE_FORMATTED_OUTPUT
407  // Format the terms of the polynomial as a string.
408  // Return: a pointer to the character immediately following the formatted output
409  virtual char * Format(char * buffer) const = 0;
410 #endif
411 
412 #if DEFINE_FORMATTED_OUTPUT
413  int CalculateFormatLength() const
414  {
415  int termPowerLength = nmrPolynomialTermPowerIndex(*this).CalculateFormatPowerLength();
416  int coefficientLength = 15;
417  // <coefficient>< ><{><powers>}\n
418  int termLength = coefficientLength + 1 + 2 + termPowerLength + 3;
419  int numTerms = GetNumberOfTerms();
420  return numTerms * termLength;
421  }
422 #endif
423 
425  virtual void SerializeRaw(std::ostream & output) const;
426 
428  virtual void DeserializeRaw(std::istream & input);
429 
430 protected:
434 
435 #if INCLUDE_DEPRECATED_POLYNOMIAL_CODE
437 #endif // INCLUDE_DEPRECATED_POLYNOMIAL_CODE
438 
439 };
440 
441 
442 
443 #endif // _nmrPolynomialBase_h
444 
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
#define CMN_ASSERT(expr)
Definition: cmnAssert.h:90
nmrPolynomialTermPowerIndex::MultinomialCoefficientType TermCounterType
Definition: nmrPolynomialBase.h:59
#define CISST_DEPRECATED
Definition: cmnPortability.h:310
VariableIndexType GetNumVariables() const
Definition: nmrPolynomialBase.h:69
TermCounterType GetMaxNumberOfTerms() const
Definition: nmrPolynomialBase.h:89
Portability across compilers and operating systems tools.
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
PowerType MinDegree
Definition: nmrPolynomialBase.h:432
double ValueType
Definition: nmrPolynomialBase.h:54
virtual CoefficientType GetCoefficient(const nmrPolynomialTermPowerIndex &CMN_UNUSED(where)) const
Definition: nmrPolynomialBase.h:191
int VariableIndexType
Definition: nmrPolynomialTermPowerIndex.h:95
Definition: nmrPolynomialBase.h:51
nmrPolynomialTermPowerIndex::PowerType PowerType
Definition: nmrPolynomialBase.h:58
PowerType MaxDegree
Definition: nmrPolynomialBase.h:433
int PowerType
Definition: nmrPolynomialTermPowerIndex.h:98
PowerType GetMaxDegree() const
Definition: nmrPolynomialBase.h:79
double VariableType
Definition: nmrPolynomialBase.h:55
VariableIndexType NumVariables
Definition: nmrPolynomialBase.h:431
ValueType EvaluateTerm(const nmrPolynomialTermPowerIndex &where, const nmrMultiVariablePowerBasis &variables, CoefficientType coefficient) const
Definition: nmrPolynomialBase.h:315
double CoefficientType
Definition: nmrPolynomialBase.h:56
ValueType EvaluateTerm(const nmrPolynomialTermPowerIndex &where, const nmrMultiVariablePowerBasis &variables) const
Definition: nmrPolynomialBase.h:304
virtual ~nmrPolynomialBase()
Definition: nmrPolynomialBase.h:66
nmrPolynomialTermPowerIndex::VariableIndexType VariableIndexType
Definition: nmrPolynomialBase.h:57
PowerType GetMinDegree() const
Definition: nmrPolynomialBase.h:74
const PowerType * GetPowers() const
Definition: nmrPolynomialTermPowerIndex.h:181
unsigned long MultinomialCoefficientType
Definition: nmrPolynomialTermPowerIndex.h:101
Definition: nmrMultiVariablePowerBasis.h:37
Declare class nmrPolynomialTermPowerIndex to represent the power index of a single term in a multi-va...
Represents the power index of a single term in a multi-variable polynomial.
Definition: nmrPolynomialTermPowerIndex.h:89
Rules of exporting.
InsertStatus
Definition: nmrPolynomialBase.h:61