OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ossimPolynom< T, DIM > Class Template Reference

template class for multivariate polynom algebra More...

#include <ossimPolynom.h>

Classes

struct  EXP_TUPLE_LESSTHAN
 type to store multivariate input More...
 

Public Types

typedef std::vector< int > EXP_TUPLE
 inner types More...
 
typedef std::vector< T > VAR_TUPLE
 type to store exponent tuples More...
 
typedef std::map< EXP_TUPLE, T, EXP_TUPLE_LESSTHANMONOM_MAP
 
typedef std::set< EXP_TUPLE, EXP_TUPLE_LESSTHANEXPT_SET
 for storing polynom More...
 

Public Member Functions

 ossimPolynom (const T &epsilon=0)
 for storing set of exponent tuples More...
 
 ossimPolynom (const ossimPolynom &p)
 
 ~ossimPolynom ()
 
const ossimPolynomoperator= (const ossimPolynom< T, DIM > &pt)
 
void setMonom (const EXP_TUPLE &m, const T &v)
 
void setMonom (const int mexp[DIM], const T &v)
 
void delMonom (const EXP_TUPLE &m)
 
getCoeff (const EXP_TUPLE &m) const
 
void nullify ()
 
bool isNull () const
 
const MONOM_MAPgetMonoms () const
 
const T & getEpsilon () const
 no setEpsilon beacause might erase monoms More...
 
bool operator== (const ossimPolynom &pt) const
 comparison operators -don't compare theEpsilon values -use my own epsilon in comparisons (not the compared to's epsilon) More...
 
bool operator!= (const ossimPolynom &pt) const
 
bool isNegligible (const T &v) const
 can v be considered as zero? More...
 
int getOrder (int d) const
 orders More...
 
int getTotalOrder () const
 returns maximum order of monoms More...
 
eval (const VAR_TUPLE &v) const
 evaluation : needs DIM values as input More...
 
void pdiff (int pdim, ossimPolynom &result) const
 partial differentiation polynom More...
 
const ossimPolynomoperator*= (const T &k)
 operations with scalar More...
 
ossimPolynom operator+ (const ossimPolynom &p) const
 arithmetic operators More...
 
ossimPolynom operator- (const ossimPolynom &p) const
 
const ossimPolynomoperator+= (const ossimPolynom &p)
 
const ossimPolynomoperator-= (const ossimPolynom &p)
 
ossimPolynom operator* (const ossimPolynom &p) const
 product operator : use epsilon from left side More...
 
const ossimPolynomoperator*= (const ossimPolynom &p)
 
std::ostream & print (std::ostream &os) const
 I/O. More...
 
std::ostream & printNice (std::ostream &os, const char symbols[DIM])
 classic representation (bad accuracy, for display only) More...
 
std::istream & import (std::istream &is)
 note that it can only import for the template type T and dimesnion DIM More...
 
EXPT_SET builExpSet (const EXP_TUPLE &orders) const
 constructs simple exponent tuples set for using LMSfit need order for each dimension More...
 
void addExpTupleRight (int newdim, int totalOrder, EXPT_SET &eset) const
 concatenate exponents (at the right) to existing tuple set, for a given maximum total order eg: with eset={(0,1),(0,0)} , then addExpTuple(2,1,eset) = {(0,1,0,0),(0,1,0,1),(0,1,1,0), (0,0,0,0),(0,0,0,1),(0,0,1,0)} More...
 
bool LMSfit (const EXPT_SET &expset, const std::vector< VAR_TUPLE > obs_input, const std::vector< T > obs_output, T *prms=NULL)
 fits the polynom to the observations using Least Mean Squares returns true on success (can fail if not enough observations) More...
 
bool SLSfit (const EXPT_SET &expset, const std::vector< VAR_TUPLE > obs_input, const std::vector< T > obs_output, T *prms=NULL)
 Standard least squares Modified version of LMSfit that uses standard NEWMAT inverse as alternative to SVD solution. More...
 

Static Public Member Functions

static void addExpTuple (EXP_TUPLE &m1, const EXP_TUPLE &m2)
 

Protected Member Functions

void discardNullMonoms ()
 positive values below epsilon are considered 0 More...
 
void addMonomQuick (const EXP_TUPLE &m, const T &v)
 add value without testing for negligible More...
 
NEWMAT::Matrix invert (const NEWMAT::Matrix &m) const
 invert stolen from ossimRpcSolver More...
 

Protected Attributes

MONOM_MAP theMonoms
 protected data members More...
 
theEpsilon
 associate a scalar to each tuple of orders : monom More...
 

Detailed Description

template<class T, int DIM = 1>
class ossimPolynom< T, DIM >

template class for multivariate polynom algebra

T : the storage type, constraints: must have 0 (zero) as value, support ops fabs + - * / DIM : the dimension of the input space, integer (>=1, default : 1)

stores a set of monoms, a monom is (exponent tuples + coefficient) requires a precion (epsilon) for comparisons note: monoms absolute values below epsilon are removed from the map

Definition at line 38 of file ossimPolynom.h.

Member Typedef Documentation

◆ EXP_TUPLE

template<class T, int DIM = 1>
typedef std::vector< int > ossimPolynom< T, DIM >::EXP_TUPLE

inner types

Definition at line 44 of file ossimPolynom.h.

◆ EXPT_SET

template<class T, int DIM = 1>
typedef std::set< EXP_TUPLE, EXP_TUPLE_LESSTHAN > ossimPolynom< T, DIM >::EXPT_SET

for storing polynom

Definition at line 68 of file ossimPolynom.h.

◆ MONOM_MAP

template<class T, int DIM = 1>
typedef std::map< EXP_TUPLE, T , EXP_TUPLE_LESSTHAN > ossimPolynom< T, DIM >::MONOM_MAP

Definition at line 66 of file ossimPolynom.h.

◆ VAR_TUPLE

template<class T, int DIM = 1>
typedef std::vector< T > ossimPolynom< T, DIM >::VAR_TUPLE

type to store exponent tuples

Definition at line 45 of file ossimPolynom.h.

Constructor & Destructor Documentation

◆ ossimPolynom() [1/2]

template<class T, int DIM = 1>
ossimPolynom< T, DIM >::ossimPolynom ( const T &  epsilon = 0)
inline

for storing set of exponent tuples

construction : must supply epsilon value, default 0

Definition at line 75 of file ossimPolynom.h.

76  : theEpsilon(epsilon)
77  {}
T theEpsilon
associate a scalar to each tuple of orders : monom
Definition: ossimPolynom.h:783

◆ ossimPolynom() [2/2]

template<class T, int DIM = 1>
ossimPolynom< T, DIM >::ossimPolynom ( const ossimPolynom< T, DIM > &  p)
inline

Definition at line 79 of file ossimPolynom.h.

79  :
80  theMonoms(p.getMonoms()),
82  {}
const T & getEpsilon() const
no setEpsilon beacause might erase monoms
Definition: ossimPolynom.h:150
T theEpsilon
associate a scalar to each tuple of orders : monom
Definition: ossimPolynom.h:783
MONOM_MAP theMonoms
protected data members
Definition: ossimPolynom.h:781
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145

◆ ~ossimPolynom()

template<class T, int DIM = 1>
ossimPolynom< T, DIM >::~ossimPolynom ( )
inline

Definition at line 84 of file ossimPolynom.h.

85  {}

Member Function Documentation

◆ addExpTuple()

template<class T, int DIM = 1>
static void ossimPolynom< T, DIM >::addExpTuple ( EXP_TUPLE m1,
const EXP_TUPLE m2 
)
inlinestatic

Definition at line 344 of file ossimPolynom.h.

Referenced by ossimPolynom< ossim_float64, 3 >::operator*().

345  {
346  for(int i=0;i<DIM;++i) {
347  m1[i] += m2[i];
348  }
349  }

◆ addExpTupleRight()

template<class T, int DIM = 1>
void ossimPolynom< T, DIM >::addExpTupleRight ( int  newdim,
int  totalOrder,
EXPT_SET eset 
) const
inline

concatenate exponents (at the right) to existing tuple set, for a given maximum total order eg: with eset={(0,1),(0,0)} , then addExpTuple(2,1,eset) = {(0,1,0,0),(0,1,0,1),(0,1,1,0), (0,0,0,0),(0,0,0,1),(0,0,1,0)}

Definition at line 569 of file ossimPolynom.h.

Referenced by ossimPolynom< ossim_float64, 3 >::addExpTupleRight(), and ossimPolynomProjection::setupDesiredExponents().

570  {
571  EXPT_SET newset;
572  // add a copy off eset for each order with the specific last dim
573  for(int o=0; o<=totalOrder ; ++o)
574  {
575  EXPT_SET extset; //extended set
576  if (eset.size()==0)
577  {
578  EXP_TUPLE tu(1);
579  tu[0]=o;
580  extset.insert(tu);
581  } else {
582  //we have to construct a new set from eset, extending dimension
583  // cause: stored tuples cannot be compared at different dimensions
584  for(typename EXPT_SET::iterator sit = eset.begin(); sit != eset.end(); ++sit)
585  {
586  EXP_TUPLE tu(*sit);
587  tu.push_back(o);
588  extset.insert(tu);
589  }
590  }
591  //recursively add remaining dimensions
592  if (newdim>1)
593  {
594  addExpTupleRight(newdim-1, totalOrder-o, extset); //only dimension decreases
595  }
596  //add full set for the specific order
597  newset.insert(extset.begin(), extset.end());
598  }
599  eset=newset; //overwrite
600  }
void addExpTupleRight(int newdim, int totalOrder, EXPT_SET &eset) const
concatenate exponents (at the right) to existing tuple set, for a given maximum total order eg: with ...
Definition: ossimPolynom.h:569
std::set< EXP_TUPLE, EXP_TUPLE_LESSTHAN > EXPT_SET
for storing polynom
Definition: ossimPolynom.h:68
std::vector< int > EXP_TUPLE
inner types
Definition: ossimPolynom.h:44

◆ addMonomQuick()

template<class T, int DIM = 1>
void ossimPolynom< T, DIM >::addMonomQuick ( const EXP_TUPLE m,
const T &  v 
)
inlineprotected

add value without testing for negligible

Definition at line 807 of file ossimPolynom.h.

Referenced by ossimPolynom< ossim_float64, 3 >::operator*().

808  {
809  typename MONOM_MAP::iterator it = theMonoms.find(m);
810  if (it != theMonoms.end())
811  {
812  it->second += v;
813  } else {
814  theMonoms.insert( MONOM_MAP::value_type(m,v));
815  }
816  }
MONOM_MAP theMonoms
protected data members
Definition: ossimPolynom.h:781

◆ builExpSet()

template<class T, int DIM = 1>
EXPT_SET ossimPolynom< T, DIM >::builExpSet ( const EXP_TUPLE orders) const
inline

constructs simple exponent tuples set for using LMSfit need order for each dimension

Definition at line 534 of file ossimPolynom.h.

535  {
536  EXPT_SET eset;
537  if (orders.size() != DIM)
538  {
539  std::cerr<<"ossimPolynom::import ERROR bad dimension for parameter, need "<<DIM<<" elements"<<std::endl;
540  return eset;
541  }
542  //initialise variable exponent tuple
543  EXP_TUPLE et(DIM);
544  for(int d=0;d<DIM;++d) et[d]=0;
545  while (et[0] <= orders[0])
546  {
547  //add tuple to set
548  eset.insert(et);
549 
550  //increment tuple within bounds
551  et[DIM-1]++;
552  for(int d=DIM-1 ; d>=0 ; --d)
553  {
554  if ((et[d] > orders[d]) && (d>0))
555  {
556  et[d] = 0;
557  et[d-1]++;
558  }
559  }
560  }
561  return eset;
562  }
std::set< EXP_TUPLE, EXP_TUPLE_LESSTHAN > EXPT_SET
for storing polynom
Definition: ossimPolynom.h:68
std::vector< int > EXP_TUPLE
inner types
Definition: ossimPolynom.h:44

◆ delMonom()

template<class T, int DIM = 1>
void ossimPolynom< T, DIM >::delMonom ( const EXP_TUPLE m)
inline

Definition at line 119 of file ossimPolynom.h.

120  {
121  theMonoms.erase(m); //TBC TBD: what happens if m is not in the map?
122  }
MONOM_MAP theMonoms
protected data members
Definition: ossimPolynom.h:781

◆ discardNullMonoms()

template<class T, int DIM = 1>
void ossimPolynom< T, DIM >::discardNullMonoms ( )
inlineprotected

positive values below epsilon are considered 0

method to erase all negligible monoms : user don't need that (automatic)

Definition at line 789 of file ossimPolynom.h.

Referenced by ossimPolynom< ossim_float64, 3 >::operator*(), and ossimPolynom< ossim_float64, 3 >::operator*=().

790  {
791  std::vector< typename MONOM_MAP::iterator > erasev; //storage for iterators on elements to erase
792  for (typename MONOM_MAP::iterator it = theMonoms.begin(); it != theMonoms.end() ; ++it )
793  {
794  if (isNegligible(it->second)) erasev.push_back(it);
795  }
796  //erase all elements afterwards
797  for ( typename std::vector< typename MONOM_MAP::iterator >::const_iterator vit = erasev.begin(); vit != erasev.end() ; ++vit )
798  {
799  theMonoms.erase(*vit); //*vit is an iterator in theMonoms
800  }
801  }
bool isNegligible(const T &v) const
can v be considered as zero?
Definition: ossimPolynom.h:178
MONOM_MAP theMonoms
protected data members
Definition: ossimPolynom.h:781

◆ eval()

template<class T, int DIM = 1>
T ossimPolynom< T, DIM >::eval ( const VAR_TUPLE v) const
inline

evaluation : needs DIM values as input

Definition at line 219 of file ossimPolynom.h.

Referenced by ossimPolynomProjection::lineSampleHeightToWorld(), and ossimPolynomProjection::worldToLineSample().

220  {
221  T ev = 0;
222  //loop on monoms. TBD optimize powers using map order
223  typename MONOM_MAP::const_iterator it;
224  int p;
225  for ( it = getMonoms().begin(); it != getMonoms().end() ; ++it )
226  {
227  //compute powers
228  T mv = it->second;
229  for(int d=0;d<DIM;++d)
230  {
231  p = (it->first)[d];
232  if (p != 0)
233  {
234  mv *= std::pow( v[d], p );
235  }
236  }
237  //add momom value
238  ev += mv;
239  }
240  return ev;
241  }
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145

◆ getCoeff()

template<class T, int DIM = 1>
T ossimPolynom< T, DIM >::getCoeff ( const EXP_TUPLE m) const
inline

Definition at line 124 of file ossimPolynom.h.

Referenced by ossimPolynom< ossim_float64, 3 >::operator+=(), ossimPolynom< ossim_float64, 3 >::operator-=(), and ossimPolynom< ossim_float64, 3 >::operator==().

125  {
126  typename MONOM_MAP::const_iterator it = theMonoms.find(m);
127  if (it != theMonoms.end())
128  {
129  return it->second;
130  } else {
131  return 0;
132  }
133  }
MONOM_MAP theMonoms
protected data members
Definition: ossimPolynom.h:781

◆ getEpsilon()

template<class T, int DIM = 1>
const T& ossimPolynom< T, DIM >::getEpsilon ( ) const
inline

no setEpsilon beacause might erase monoms

Definition at line 150 of file ossimPolynom.h.

Referenced by ossimPolynom< ossim_float64, 3 >::invert(), ossimPolynom< ossim_float64, 3 >::operator*(), ossimPolynom< ossim_float64, 3 >::operator=(), and ossimPolynom< ossim_float64, 3 >::print().

151  {
152  return theEpsilon;
153  }
T theEpsilon
associate a scalar to each tuple of orders : monom
Definition: ossimPolynom.h:783

◆ getMonoms()

template<class T, int DIM = 1>
const MONOM_MAP& ossimPolynom< T, DIM >::getMonoms ( ) const
inline

◆ getOrder()

template<class T, int DIM = 1>
int ossimPolynom< T, DIM >::getOrder ( int  d) const
inline

orders

returns maximum order of monoms for a specific dimension (d starts at 0)

Definition at line 186 of file ossimPolynom.h.

187  {
188  if ((d>=DIM) || (d<0)) return -1; //error = no dimension
189 
190  // loop on monoms
191  int order = -1; //for null polynom
192  int corder;
193  typename MONOM_MAP::const_iterator it;
194  for ( it = getMonoms().begin(); it != getMonoms().end() ; ++it )
195  {
196  corder = (it->first)[d];
197  if ( corder > order ) order = corder;
198  }
199  return order;
200  }
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145

◆ getTotalOrder()

template<class T, int DIM = 1>
int ossimPolynom< T, DIM >::getTotalOrder ( ) const
inline

returns maximum order of monoms

Definition at line 202 of file ossimPolynom.h.

203  {
204  int order = -1; //for null polynom
205  int sorder;
206  typename MONOM_MAP::const_iterator it;
207  for ( it = getMonoms().begin(); it != getMonoms().end() ; ++it )
208  {
209  sorder = 0;
210  for (int d=0;d<DIM;++d) sorder+=(it->first)[d];
211  if ( sorder > order ) order = sorder;
212  }
213  return order;
214  }
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145

◆ import()

template<class T, int DIM = 1>
std::istream& ossimPolynom< T, DIM >::import ( std::istream &  is)
inline

note that it can only import for the template type T and dimesnion DIM

Definition at line 440 of file ossimPolynom.h.

Referenced by operator>>().

441  {
442  nullify();
443 
444  //extract data in brackets [ ]
445  //swallow bracket
446  ossimString tempString;
447  char tempChar;
448  is.get(tempChar);
449  if (!is)
450  {
451  std::cerr<<"ossimPolynom::import ERROR, cannot read left bracket ["<<std::endl;
452  return is;
453  }
454  if (tempChar != '[')
455  {
456  std::cerr<<"ossimPolynom::import ERROR, missing left bracket ["<<std::endl;
457  return is;
458  }
459  const int BUFSIZE=32768; //should be enough fro most apps (TBC TBD : allow loops if not enough)
460  char buffer[BUFSIZE];
461  is.getline(buffer, BUFSIZE, ']');
462  if (!is)
463  {
464  std::cerr<<"ossimPolynom::import ERROR, cannot read after left bracket ["<<std::endl;
465  return is;
466  }
467  if (is.gcount() >= BUFSIZE-1)
468  {
469  std::cerr<<"ossimPolynom::import ERROR, cannot find right bracket ] after "<<BUFSIZE-1<<"characters"<<std::endl;
470  return is;
471  }
472  tempString = buffer; //no more brackets
473 
474  //split string using semicolons
475  std::vector< ossimString > subparts = tempString.explode(";");
476  //loop on subparts
477  for (typename std::vector< ossimString >::const_iterator it=subparts.begin() ; it!=subparts.end() ; ++it )
478  {
479  ossimString sp = it->trim();
480  //check for epsilon
481  ossimString aft_eps=sp.after("eps=");
482  if (aft_eps.size()>0)
483  {
484  //get epsilon value
485  aft_eps.trim();
486  theEpsilon = static_cast< T >(aft_eps.toDouble());
487  } else {
488  //no epsilon : must be a monom
489  ossimString scalpart=sp.before("(");
490  if (scalpart.size() < sp.size())
491  {
492  T coeff = static_cast< T >(scalpart.toDouble());
493  ossimString expopart = ((sp.before(")")).after("(")).trim();
494  if (expopart.size() == 0)
495  {
496  std::cerr<<"ossimPolynom::import ERROR, cannot find ) in monom or empty monom"<<std::endl;
497  return is;
498  }
499  std::vector< ossimString > vexpo = expopart.explode(",");
500  if (vexpo.size() != DIM)
501  {
502  std::cerr<<"ossimPolynom::import ERROR, bad number of exponents in monom (need "<<DIM<<"): "<<vexpo.size()<<std::endl;
503  return is;
504  }
505  //store all exponents
506  EXP_TUPLE expt(DIM);
507  int d;
508  std::vector< ossimString >::const_iterator eit;
509  for (eit=vexpo.begin() , d=0 ; eit != vexpo.end() ; ++eit, ++d )
510  {
511  expt[d] = eit->toInt(); //TBD : could check that value is integer, but how?
512  }
513  //add new monom (if duplicate...error)
514  if (getMonoms().find(expt) != getMonoms().end())
515  {
516  std::cerr<<"ossimPolynom::import ERROR, duplicate exponent tuple in polynom"<<std::endl;
517  return is;
518  }
519  theMonoms[expt] = coeff;
520 
521  } else {
522  std::cerr<<"ossimPolynom::import ERROR, cannot find left parenthesis ( in monom "<<std::endl;
523  return is;
524  }
525  }
526  }
527 
528  return is;
529  }
ossimString before(const ossimString &str, std::string::size_type pos=0) const
METHOD: before(str, pos) Returns string beginning at pos and ending one before the token str If strin...
T theEpsilon
associate a scalar to each tuple of orders : monom
Definition: ossimPolynom.h:783
std::string::size_type size() const
Definition: ossimString.h:405
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
double toDouble() const
std::vector< int > EXP_TUPLE
inner types
Definition: ossimPolynom.h:44
MONOM_MAP theMonoms
protected data members
Definition: ossimPolynom.h:781
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145
std::vector< ossimString > explode(const ossimString &delimeter) const
friend OSSIM_DLL std::istream & getline(std::istream &is, ossimString &str, char delim)
Reads a string from the input stream is, stopping when it reaches delim.
Definition: ossimString.h:916
ossimString after(const ossimString &str, std::string::size_type pos=0) const
METHOD: after(str, pos) Returns string immediately after the token str.
void nullify()
Definition: ossimPolynom.h:135

◆ invert()

template<class T, int DIM = 1>
NEWMAT::Matrix ossimPolynom< T, DIM >::invert ( const NEWMAT::Matrix &  m) const
inlineprotected

invert stolen from ossimRpcSolver

Definition at line 822 of file ossimPolynom.h.

Referenced by ossimPolynom< ossim_float64, 3 >::LMSfit().

823  {
824  ossim_uint32 idx = 0;
825  NEWMAT::DiagonalMatrix d;
826  NEWMAT::Matrix u;
827  NEWMAT::Matrix v;
828 
829  // decompose m.t*m which is stored in Temp into the singular values and vectors.
830  //
831  NEWMAT::SVD(m, d, u, v, true, true);
832 
833  // invert the diagonal
834  // this is just doing the reciprical fo all diagonal components and store back int
835  // d. ths compute d inverse.
836  //
837  for(idx=0; idx < (ossim_uint32)d.Ncols(); ++idx)
838  {
839  if(d[idx] > getEpsilon()) //adpated here for epsilon
840  {
841  d[idx] = 1.0/d[idx];
842  }
843  else
844  {
845  d[idx] = 0.0;
846  }
847  }
848 
849  //compute inverse of decomposed m;
850  return v*d*u.t();
851  }
const T & getEpsilon() const
no setEpsilon beacause might erase monoms
Definition: ossimPolynom.h:150
unsigned int ossim_uint32
void SVD(const Matrix &, DiagonalMatrix &, Matrix &, Matrix &, bool=true, bool=true)
Definition: svd.cpp:26

◆ isNegligible()

template<class T, int DIM = 1>
bool ossimPolynom< T, DIM >::isNegligible ( const T &  v) const
inline

can v be considered as zero?

Definition at line 178 of file ossimPolynom.h.

Referenced by ossimPolynom< ossim_float64, 3 >::discardNullMonoms(), ossimPolynom< ossim_float64, 3 >::operator==(), and ossimPolynom< ossim_float64, 3 >::setMonom().

179  {
180  return ( fabs(v) <= theEpsilon );
181  }
T theEpsilon
associate a scalar to each tuple of orders : monom
Definition: ossimPolynom.h:783

◆ isNull()

template<class T, int DIM = 1>
bool ossimPolynom< T, DIM >::isNull ( ) const
inline

Definition at line 140 of file ossimPolynom.h.

141  {
142  return (theMonoms.size() == 0);
143  }
MONOM_MAP theMonoms
protected data members
Definition: ossimPolynom.h:781

◆ LMSfit()

template<class T, int DIM = 1>
bool ossimPolynom< T, DIM >::LMSfit ( const EXPT_SET expset,
const std::vector< VAR_TUPLE obs_input,
const std::vector< T >  obs_output,
T *  prms = NULL 
)
inline

fits the polynom to the observations using Least Mean Squares returns true on success (can fail if not enough observations)

  • also updates rms error(root mean square) NOTES: inputs must have same size and must be ordered the same way use builExpSet() to construct classic polynoms TODO: add weights to observations

Definition at line 611 of file ossimPolynom.h.

Referenced by ossimPolynomProjection::optimizeFit().

615 {
616  //init
617  nullify();
618 
619  //check size
620  int nobs = (int)obs_input.size();
621  if (nobs != (int)obs_output.size())
622  {
623  std::cerr<<"ossimPolynom::LMSfit ERROR observation input/output must have the same size"<<std::endl;
624  return false;
625  }
626  if (nobs<=0)
627  {
628  std::cerr<<"ossimPolynom::LMSfit ERROR observation count is zero"<<std::endl;
629  return false;
630  }
631  int ncoeff = (int)expset.size();
632  if (ncoeff<=0)
633  {
634  std::cerr<<"ossimPolynom::LMSfit ERROR exponent count is zero"<<std::endl;
635  return false;
636  }
637 
638  //construct LMS linear system (using OSSIM matrices)
639  // M.k = v
640  // M : monom matrix
641  // k : polynbm coefficients
642  // v : output_obs
643  NEWMAT::Matrix M(nobs, ncoeff);
644  double elt;
645  int p;
646  typename EXPT_SET::const_iterator cit;
647  typename std::vector< VAR_TUPLE >::const_iterator oit;
648  int i,j;
649  for (cit=expset.begin(), j=1; cit != expset.end() ; ++cit, ++j) //loop on exponent tuples
650  {
651  for(oit=obs_input.begin(), i=1; oit!=obs_input.end();++oit, ++i) //loop on observations
652  {
653  //compute powers using observation position
654  elt=1.0;
655  for(int d=0;d<DIM;++d)
656  {
657  p = (*cit)[d];
658  if (p != 0)
659  {
660  elt *= std::pow( (*oit)[d], p );
661  }
662  }
663  //init M
664  M(i,j) = elt; //NEWMAT indices start at 1
665  }
666  }
667 
668  NEWMAT::ColumnVector v(nobs);
669  for(int o=0;o<nobs;++o)
670  {
671  v(o+1) = obs_output[o];
672  }
673 
674  //build LMS symmetric matrix tM.M
675  //build best fit
676  NEWMAT::ColumnVector bfit = invert(M.t()*M)*M.t()*v; //TBD : check inversion
677 
678  //compute RMS (optional, if rms non null)
679  if (prms!=NULL)
680  {
681  NEWMAT::ColumnVector delta = M*bfit - v;
682  *prms = std::sqrt( delta.SumSquare() / nobs);
683  }
684 
685  //init polynom
686  for (cit=expset.begin(), j=1; cit != expset.end() ; ++cit, ++j) //loop on exponent tuples
687  {
688  setMonom(*cit, bfit(j));
689  }
690  return true;
691 }
NEWMAT::Matrix invert(const NEWMAT::Matrix &m) const
invert stolen from ossimRpcSolver
Definition: ossimPolynom.h:822
void setMonom(const EXP_TUPLE &m, const T &v)
Definition: ossimPolynom.h:97
void nullify()
Definition: ossimPolynom.h:135

◆ nullify()

template<class T, int DIM = 1>
void ossimPolynom< T, DIM >::nullify ( )
inline

◆ operator!=()

template<class T, int DIM = 1>
bool ossimPolynom< T, DIM >::operator!= ( const ossimPolynom< T, DIM > &  pt) const
inline

Definition at line 173 of file ossimPolynom.h.

174  {
175  return !operator==(pt);
176  }
bool operator==(const ossimPolynom &pt) const
comparison operators -don&#39;t compare theEpsilon values -use my own epsilon in comparisons (not the com...
Definition: ossimPolynom.h:160

◆ operator*()

template<class T, int DIM = 1>
ossimPolynom ossimPolynom< T, DIM >::operator* ( const ossimPolynom< T, DIM > &  p) const
inline

product operator : use epsilon from left side

Definition at line 315 of file ossimPolynom.h.

316  {
317  //do a stupid multiplication (sum all monom pairs)
319  T coeff;
320  //loop on p monoms
321  for ( typename MONOM_MAP::const_iterator it = getMonoms().begin(); it != getMonoms().end() ; ++it )
322  {
323  for ( typename MONOM_MAP::const_iterator pit = p.getMonoms().begin(); pit != p.getMonoms().end() ; ++pit )
324  {
325  coeff = it->second * pit->second;
326  if (coeff!=0)
327  {
328  EXP_TUPLE prodExp(it->first);
329  addExpTuple(prodExp, pit->first);
330  prod.addMonomQuick(prodExp, coeff);
331  }
332  }
333  }
334  //scan for null monoms and discard
335  prod.discardNullMonoms();
336  return prod;
337  }
const T & getEpsilon() const
no setEpsilon beacause might erase monoms
Definition: ossimPolynom.h:150
template class for multivariate polynom algebra
Definition: ossimPolynom.h:38
std::vector< int > EXP_TUPLE
inner types
Definition: ossimPolynom.h:44
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145
static void addExpTuple(EXP_TUPLE &m1, const EXP_TUPLE &m2)
Definition: ossimPolynom.h:344

◆ operator*=() [1/2]

template<class T, int DIM = 1>
const ossimPolynom& ossimPolynom< T, DIM >::operator*= ( const T &  k)
inline

operations with scalar

Definition at line 266 of file ossimPolynom.h.

267  {
268  //loop on monoms
269  for ( typename MONOM_MAP::const_iterator it = getMonoms().begin(); it != getMonoms().end() ; ++it )
270  {
271  it->second *= k;
272  }
274  }
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145
void discardNullMonoms()
positive values below epsilon are considered 0
Definition: ossimPolynom.h:789

◆ operator*=() [2/2]

template<class T, int DIM = 1>
const ossimPolynom& ossimPolynom< T, DIM >::operator*= ( const ossimPolynom< T, DIM > &  p)
inline

Definition at line 339 of file ossimPolynom.h.

340  {
341  return operator=( this->operator*(p) );
342  }
const ossimPolynom & operator=(const ossimPolynom< T, DIM > &pt)
Definition: ossimPolynom.h:87

◆ operator+()

template<class T, int DIM = 1>
ossimPolynom ossimPolynom< T, DIM >::operator+ ( const ossimPolynom< T, DIM > &  p) const
inline

arithmetic operators

Definition at line 279 of file ossimPolynom.h.

280  {
281  ossimPolynom< T , DIM > sum(*this);
282  return (sum+=p);
283  }
template class for multivariate polynom algebra
Definition: ossimPolynom.h:38

◆ operator+=()

template<class T, int DIM = 1>
const ossimPolynom& ossimPolynom< T, DIM >::operator+= ( const ossimPolynom< T, DIM > &  p)
inline

Definition at line 290 of file ossimPolynom.h.

291  {
292  typename MONOM_MAP::const_iterator it;
293  //loop on p monoms
294  for ( it = p.getMonoms().begin(); it != p.getMonoms().end() ; ++it )
295  {
296  setMonom( it->first, getCoeff(it->first) + it->second );
297  }
298  return *this;
299  }
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145
void setMonom(const EXP_TUPLE &m, const T &v)
Definition: ossimPolynom.h:97
T getCoeff(const EXP_TUPLE &m) const
Definition: ossimPolynom.h:124

◆ operator-()

template<class T, int DIM = 1>
ossimPolynom ossimPolynom< T, DIM >::operator- ( const ossimPolynom< T, DIM > &  p) const
inline

Definition at line 284 of file ossimPolynom.h.

285  {
286  ossimPolynom< T , DIM > diff(*this);
287  return (diff-=p);
288  }
template class for multivariate polynom algebra
Definition: ossimPolynom.h:38

◆ operator-=()

template<class T, int DIM = 1>
const ossimPolynom& ossimPolynom< T, DIM >::operator-= ( const ossimPolynom< T, DIM > &  p)
inline

Definition at line 301 of file ossimPolynom.h.

302  {
303  typename MONOM_MAP::const_iterator it;
304  //loop on p monoms
305  for ( it = p.getMonoms().begin(); it != p.getMonoms().end() ; ++it )
306  {
307  setMonom( it->first, getCoeff(it->first) - it->second );
308  }
309  return *this;
310  }
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145
void setMonom(const EXP_TUPLE &m, const T &v)
Definition: ossimPolynom.h:97
T getCoeff(const EXP_TUPLE &m) const
Definition: ossimPolynom.h:124

◆ operator=()

template<class T, int DIM = 1>
const ossimPolynom& ossimPolynom< T, DIM >::operator= ( const ossimPolynom< T, DIM > &  pt)
inline

Definition at line 87 of file ossimPolynom.h.

Referenced by ossimPolynom< ossim_float64, 3 >::operator*=().

88  {
89  if (this != &pt)
90  {
91  theEpsilon = pt.getEpsilon();
92  theMonoms = pt.getMonoms();
93  }
94  return *this;
95  }
const T & getEpsilon() const
no setEpsilon beacause might erase monoms
Definition: ossimPolynom.h:150
T theEpsilon
associate a scalar to each tuple of orders : monom
Definition: ossimPolynom.h:783
MONOM_MAP theMonoms
protected data members
Definition: ossimPolynom.h:781
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145

◆ operator==()

template<class T, int DIM = 1>
bool ossimPolynom< T, DIM >::operator== ( const ossimPolynom< T, DIM > &  pt) const
inline

comparison operators -don't compare theEpsilon values -use my own epsilon in comparisons (not the compared to's epsilon)

Definition at line 160 of file ossimPolynom.h.

Referenced by ossimPolynom< ossim_float64, 3 >::operator!=().

161  {
162  if (getMonoms().size() != pt.getMonoms().size()) return false;
163 
164  // loop on my monoms
165  typename MONOM_MAP::const_iterator it;
166  for ( it = getMonoms().begin(); it != getMonoms().end() ; ++it )
167  {
168  if ( !isNegligible(it->second - pt.getCoeff(it->first)) ) return false;
169  }
170  return true; //same number of identical monoms
171  }
bool isNegligible(const T &v) const
can v be considered as zero?
Definition: ossimPolynom.h:178
yy_size_t size
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145
T getCoeff(const EXP_TUPLE &m) const
Definition: ossimPolynom.h:124

◆ pdiff()

template<class T, int DIM = 1>
void ossimPolynom< T, DIM >::pdiff ( int  pdim,
ossimPolynom< T, DIM > &  result 
) const
inline

partial differentiation polynom

Definition at line 246 of file ossimPolynom.h.

Referenced by ossimPolynomProjection::buildLineDerivatives(), and ossimPolynomProjection::buildSampDerivatives().

247  {
248  result.nullify();
249  int ord;
250  //loop on monoms
251  for ( typename MONOM_MAP::const_iterator it = getMonoms().begin(); it != getMonoms().end() ; ++it )
252  {
253  ord = it->first[pdim];
254  if (ord>=1)
255  {
256  EXP_TUPLE expDiff(it->first);
257  expDiff[pdim] -= 1;
258  result.setMonom(expDiff, it->second * ord);
259  }
260  }
261  }
std::vector< int > EXP_TUPLE
inner types
Definition: ossimPolynom.h:44
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145
void setMonom(const EXP_TUPLE &m, const T &v)
Definition: ossimPolynom.h:97
void nullify()
Definition: ossimPolynom.h:135

◆ print()

template<class T, int DIM = 1>
std::ostream& ossimPolynom< T, DIM >::print ( std::ostream &  os) const
inline

I/O.

stream serialization format : [ k1 (e1_1,e1_2,...,e1_DIM) ; k2 (e2_1,e2_2,..,e2_DIM) ; kN (eN_1,...eN_DIM)]

N is the number of monoms ei_j is exponent for dimension j and monom i

order is not important all monoms should have the same dimension : DIM you should add eps=xxxx at the beginning, separated by semi-colon ; (by default epsilon is 0)

examples: [ ] is the null polynom, [ eps=1.0e-5 ] too [ 1.0 (0) 3.5 (1) ] is polynom 1.0 + 3.5*x, with epsilon = 0 [ eps=1.0E-12 ; 2.0 (1,1,0) ; -1.0 (0,0,1) ] is polynom 2*x*y-z, with epsilon=10^-12

Definition at line 370 of file ossimPolynom.h.

371  {
372  static const char* monom_sep = " ; ";
373  static const char* no_sep = "";
374 
375  const char* use_sep = no_sep;
376 
377  os<<"[";
378  os<<std::setprecision(16); //16 for double, TBD TBC: adapt to epsilon
379  os<<std::setiosflags(std::ios_base::scientific);
380  //output epsilon if not null
381  if (getEpsilon() > 0)
382  {
383  os<<use_sep<<"eps="<<getEpsilon();
384  use_sep=monom_sep;
385  }
386  //loop on monoms
387  for ( typename MONOM_MAP::const_iterator it = getMonoms().begin(); it != getMonoms().end() ; ++it )
388  {
389  os<<use_sep<<it->second<<"(";
390  for(int d=0 ; d<DIM ; ++d)
391  {
392  /* if (d>0) */
393  /* { */
394  /* os<<","; */
395  /* } */
396  os<<(it->first)[d];
397  }
398  os<<")";
399  use_sep=monom_sep;
400  }
401  os<<"]";
402  return os;
403  }
const T & getEpsilon() const
no setEpsilon beacause might erase monoms
Definition: ossimPolynom.h:150
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145

◆ printNice()

template<class T, int DIM = 1>
std::ostream& ossimPolynom< T, DIM >::printNice ( std::ostream &  os,
const char  symbols[DIM] 
)
inline

classic representation (bad accuracy, for display only)

Definition at line 406 of file ossimPolynom.h.

407  {
408  if (getMonoms().size() == 0)
409  {
410  os<<"0"; //zero
411  } else {
412  os<<std::setiosflags(std::ios_base::fixed);
413  os<<std::setprecision(14); //14 for double, TBD TBC: adapt to epsilon
414  //loop on monoms (map order)
415  for ( typename MONOM_MAP::const_iterator it = getMonoms().begin(); it != getMonoms().end() ; ++it )
416  {
417  T coeff = it->second;
418  if (coeff>0)
419  {
420  os<<"+";
421  }
422  os<<coeff;
423  for(int d=0;d<DIM;++d)
424  {
425  int ord=(it->first)[d];
426  if (ord>0)
427  {
428  os<<""<<symbols[d];
429  if (ord != 1)
430  {
431  os<<ord;
432  }
433  }
434  }
435  }
436  }
437  return os;
438  }
yy_size_t size
const MONOM_MAP & getMonoms() const
Definition: ossimPolynom.h:145

◆ setMonom() [1/2]

template<class T, int DIM = 1>
void ossimPolynom< T, DIM >::setMonom ( const EXP_TUPLE m,
const T &  v 
)
inline

Definition at line 97 of file ossimPolynom.h.

Referenced by ossimPolynom< ossim_float64, 3 >::LMSfit(), ossimPolynom< ossim_float64, 3 >::operator+=(), ossimPolynom< ossim_float64, 3 >::operator-=(), ossimPolynom< ossim_float64, 3 >::pdiff(), and ossimPolynom< ossim_float64, 3 >::SLSfit().

98  {
99  if (isNegligible(v))
100  {
101  theMonoms.erase(m); //TBC TBD: what happens if m is not in the map?
102  } else {
103  theMonoms[m] = v;
104  }
105  }
bool isNegligible(const T &v) const
can v be considered as zero?
Definition: ossimPolynom.h:178
MONOM_MAP theMonoms
protected data members
Definition: ossimPolynom.h:781

◆ setMonom() [2/2]

template<class T, int DIM = 1>
void ossimPolynom< T, DIM >::setMonom ( const int  mexp[DIM],
const T &  v 
)
inline

Definition at line 107 of file ossimPolynom.h.

108  {
109  EXP_TUPLE mexpV(mexp,mexp+DIM);
110  if (isNegligible(v))
111  {
112  theMonoms.erase(mexpV); //TBC TBD: what happens if m is not in the map?
113  } else {
114  theMonoms[mexpV] = v;
115  }
116  }
bool isNegligible(const T &v) const
can v be considered as zero?
Definition: ossimPolynom.h:178
std::vector< int > EXP_TUPLE
inner types
Definition: ossimPolynom.h:44
MONOM_MAP theMonoms
protected data members
Definition: ossimPolynom.h:781

◆ SLSfit()

template<class T, int DIM = 1>
bool ossimPolynom< T, DIM >::SLSfit ( const EXPT_SET expset,
const std::vector< VAR_TUPLE obs_input,
const std::vector< T >  obs_output,
T *  prms = NULL 
)
inline

Standard least squares Modified version of LMSfit that uses standard NEWMAT inverse as alternative to SVD solution.

Definition at line 700 of file ossimPolynom.h.

704 {
705  //init
706  nullify();
707 
708  //check size
709  int nobs = (int)obs_input.size();
710  if (nobs != (int)obs_output.size())
711  {
712  std::cerr<<"ossimPolynom::LMSfit ERROR observation input/output must have the same size"<<std::endl;
713  return false;
714  }
715  if (nobs<=0)
716  {
717  std::cerr<<"ossimPolynom::LMSfit ERROR observation count is zero"<<std::endl;
718  return false;
719  }
720  int ncoeff = (int)expset.size();
721  if (ncoeff<=0)
722  {
723  std::cerr<<"ossimPolynom::LMSfit ERROR exponent count is zero"<<std::endl;
724  return false;
725  }
726 
727  // M : monom matrix
728  // k : polynomial coefficients
729  // v : output_obs
730  NEWMAT::Matrix M(nobs, ncoeff);
731  double elt;
732  int p;
733  typename EXPT_SET::const_iterator cit;
734  typename std::vector< VAR_TUPLE >::const_iterator oit;
735  int i,j;
736  for (cit=expset.begin(), j=1; cit != expset.end() ; ++cit, ++j) //loop on exponent tuples
737  {
738  for(oit=obs_input.begin(), i=1; oit!=obs_input.end();++oit, ++i) //loop on observations
739  {
740  elt=1.0;
741  for(int d=0;d<DIM;++d)
742  {
743  p = (*cit)[d];
744  if (p != 0)
745  {
746  elt *= std::pow( (*oit)[d], p );
747  }
748  }
749  M(i,j) = elt;
750  }
751  }
752 
753  NEWMAT::ColumnVector v(nobs);
754  for(int o=0;o<nobs;++o)
755  {
756  v(o+1) = obs_output[o];
757  }
758 
759  //perform solution
760  NEWMAT::ColumnVector bfit = (M.t()*M).i()*M.t()*v;
761 
762  //compute RMS (optional, if rms non null)
763  if (prms!=NULL)
764  {
765  NEWMAT::ColumnVector delta = M*bfit - v;
766  *prms = std::sqrt( delta.SumSquare() / nobs);
767  }
768 
769  //init polynom
770  for (cit=expset.begin(), j=1; cit != expset.end() ; ++cit, ++j) //loop on exponent tuples
771  {
772  setMonom(*cit, bfit(j));
773  }
774  return true;
775 }
void setMonom(const EXP_TUPLE &m, const T &v)
Definition: ossimPolynom.h:97
void nullify()
Definition: ossimPolynom.h:135

Member Data Documentation

◆ theEpsilon

template<class T, int DIM = 1>
T ossimPolynom< T, DIM >::theEpsilon
protected

◆ theMonoms

template<class T, int DIM = 1>
MONOM_MAP ossimPolynom< T, DIM >::theMonoms
protected

The documentation for this class was generated from the following file: