ROOT  6.06/09
Reference Guide
MinimTransformFunction.cxx
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Author: L. Moneta June 2009
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Implementation file for class MinimTransformFunction
12 
14 
15 //#include <iostream>
16 #include <cmath>
17 #include <cassert>
18 
19 namespace ROOT {
20 
21  namespace Math {
22 
23 MinimTransformFunction::MinimTransformFunction ( const IMultiGradFunction * f, const std::vector<EMinimVariableType> & types,
24  const std::vector<double> & values,
25  const std::map<unsigned int, std::pair<double, double> > & bounds) :
26  fX( values ),
27  fFunc(f)
28 {
29  // constructor of the class from a pointer to the function (which is managed)
30  // vector specifying the variable types (free, bounded or fixed, defined in enum EMinimVariableTypes )
31  // variable values (used for the fixed ones) and a map with the bounds (for the bounded variables)
32 
33  unsigned int ntot = NTot(); // NTot is fFunc->NDim()
34  assert ( types.size() == ntot );
35  fVariables.reserve(ntot);
36  fIndex.reserve(ntot);
37  for (unsigned int i = 0; i < ntot; ++i ) {
38  if (types[i] == kFix )
39  fVariables.push_back( MinimTransformVariable( values[i]) );
40  else {
41  fIndex.push_back(i);
42 
43  if ( types[i] == kDefault)
44  fVariables.push_back( MinimTransformVariable() );
45  else {
46  std::map<unsigned int, std::pair<double,double> >::const_iterator itr = bounds.find(i);
47  assert ( itr != bounds.end() );
48  double low = itr->second.first;
49  double up = itr->second.second;
50  if (types[i] == kBounds )
51  fVariables.push_back( MinimTransformVariable( low, up, new SinVariableTransformation()));
52  else if (types[i] == kLowBound)
54  else if (types[i] == kUpBound)
56  }
57  }
58  }
59 }
60 
61 
62 void MinimTransformFunction::Transformation( const double * x, double * xext) const {
63  // transform from internal to external
64 
65  unsigned int nfree = fIndex.size();
66 
67 // std::cout << "Transform: internal ";
68 // for (int i = 0; i < nfree; ++i) std::cout << x[i] << " ";
69 // std::cout << "\t\t";
70 
71  for (unsigned int i = 0; i < nfree; ++i ) {
72  unsigned int extIndex = fIndex[i];
73  const MinimTransformVariable & var = fVariables[ extIndex ];
74  if (var.IsLimited() )
75  xext[ extIndex ] = var.InternalToExternal( x[i] );
76  else
77  xext[ extIndex ] = x[i];
78  }
79 
80 // std::cout << "Transform: external ";
81 // for (int i = 0; i < fX.size(); ++i) std::cout << fX[i] << " ";
82 // std::cout << "\n";
83 
84 }
85 
86 void MinimTransformFunction::InvTransformation(const double * xExt, double * xInt) const {
87  // inverse function transformation (external -> internal)
88  for (unsigned int i = 0; i < NDim(); ++i ) {
89  unsigned int extIndex = fIndex[i];
90  const MinimTransformVariable & var = fVariables[ extIndex ];
91  assert ( !var.IsFixed() );
92  if (var.IsLimited() )
93  xInt[ i ] = var.ExternalToInternal( xExt[extIndex] );
94  else
95  xInt[ i ] = xExt[extIndex];
96  }
97 }
98 
99 void MinimTransformFunction::InvStepTransformation(const double * x, const double * sExt, double * sInt) const {
100  // inverse function transformation for steps (external -> internal)
101  for (unsigned int i = 0; i < NDim(); ++i ) {
102  unsigned int extIndex = fIndex[i];
103  const MinimTransformVariable & var = fVariables[ extIndex ];
104  assert ( !var.IsFixed() );
105  if (var.IsLimited() ) {
106  // bound variables
107  double x2 = x[extIndex] + sExt[extIndex];
108  if (var.HasUpperBound() && x2 >= var.UpperBound() )
109  x2 = x[extIndex] - sExt[extIndex];
110  // transform x and x2
111  double xint = var.ExternalToInternal ( x[extIndex] );
112  double x2int = var.ExternalToInternal( x2 );
113  sInt[i] = std::abs( x2int - xint);
114  }
115  else
116  sInt[ i ] = sExt[extIndex];
117  }
118 }
119 
120 void MinimTransformFunction::GradientTransformation(const double * x, const double *gExt, double * gInt) const {
121  //transform gradient vector (external -> internal) at internal point x
122  unsigned int nfree = fIndex.size();
123  for (unsigned int i = 0; i < nfree; ++i ) {
124  unsigned int extIndex = fIndex[i];
125  const MinimTransformVariable & var = fVariables[ extIndex ];
126  assert (!var.IsFixed() );
127  if (var.IsLimited() )
128  gInt[i] = gExt[ extIndex ] * var.DerivativeIntToExt( x[i] );
129  else
130  gInt[i] = gExt[ extIndex ];
131  }
132 }
133 
134 
135 void MinimTransformFunction::MatrixTransformation(const double * x, const double *covInt, double * covExt) const {
136  //transform covariance matrix (internal -> external) at internal point x
137  // use row storages for matrices m(i,j) = rep[ i * dim + j]
138  // ignore fixed points
139  unsigned int nfree = fIndex.size();
140  unsigned int ntot = NTot();
141  for (unsigned int i = 0; i < nfree; ++i ) {
142  unsigned int iext = fIndex[i];
143  const MinimTransformVariable & ivar = fVariables[ iext ];
144  assert (!ivar.IsFixed());
145  double ddi = ( ivar.IsLimited() ) ? ivar.DerivativeIntToExt( x[i] ) : 1.0;
146  // loop on j variables for not fixed i variables (forget that matrix is symmetric) - could be optimized
147  for (unsigned int j = 0; j < nfree; ++j ) {
148  unsigned int jext = fIndex[j];
149  const MinimTransformVariable & jvar = fVariables[ jext ];
150  double ddj = ( jvar.IsLimited() ) ? jvar.DerivativeIntToExt( x[j] ) : 1.0;
151  assert (!jvar.IsFixed() );
152  covExt[ iext * ntot + jext] = ddi * ddj * covInt[ i * nfree + j];
153  }
154  }
155 }
156 
157 
158  } // end namespace Math
159 
160 } // end namespace ROOT
161 
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition: IFunction.h:322
void InvTransformation(const double *xext, double *xint) const
inverse transformation (external -> internal)
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
#define assert(cond)
Definition: unittest.h:542
void InvStepTransformation(const double *x, const double *sext, double *sint) const
inverse transformation for steps (external -> internal) at external point x
static const double x2[5]
Double_t x[n]
Definition: legend1.C:17
static Vc_ALWAYS_INLINE Vector< T > abs(const Vector< T > &x)
Definition: vector.h:450
Sqrt Transformation class for dealing with upper bounded variables.
MinimTransformVariable class Contains meta information of the variables such as bounds, fix flags and deals with transformation of the variable The class does not contain the values and the step size (error) of the variable This is an internal class used by the MinimTransformFunction class.
void MatrixTransformation(const double *x, const double *covInt, double *covExt) const
transform covariance matrix (internal -> external) at internal point x use row storages for matrices ...
unsigned int NDim() const
Retrieve the dimension of the function.
double f(double x)
std::vector< MinimTransformVariable > fVariables
Namespace for new Math classes and functions.
const double * Transformation(const double *x) const
transform from internal to external result is cached also inside the class
MinimTransformFunction(const IMultiGradFunction *f, const std::vector< ROOT::Math::EMinimVariableType > &types, const std::vector< double > &values, const std::map< unsigned int, std::pair< double, double > > &bounds)
Constructor from a IMultiGradFunction interface (which is managed by the class) vector specifying the...
void GradientTransformation(const double *x, const double *gExt, double *gInt) const
transform gradient vector (external -> internal) at internal point x
Sin Transformation class for dealing with double bounded variables.
Sqrt Transformation class for dealing with lower bounded variables.