DACE 2.0 API Manual
Differential Algebra Core Engine
compiledDA_t.h
Go to the documentation of this file.
1 /******************************************************************************
2 * *
3 * DIFFERENTIAL ALGEBRA CORE ENGINE *
4 * *
5 *******************************************************************************
6 * *
7 * Copyright 2016 Politecnico di Milano (2014 Dinamica Srl) *
8 * Licensed under the Apache License, Version 2.0 (the "License"); *
9 * you may not use this file except in compliance with the License. *
10 * You may obtain a copy of the License at *
11 * *
12 * http://www.apache.org/licenses/LICENSE-2.0 *
13 * *
14 * Unless required by applicable law or agreed to in writing, software *
15 * distributed under the License is distributed on an "AS IS" BASIS, *
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
17 * See the License for the specific language governing permissions and *
18 * limitations under the License. *
19 * *
20 *******************************************************************************/
21 
22 /*
23  * compiledDA_t.h
24  *
25  * Created on: Apr 07, 2014
26  * Author: Dinamica Srl
27  */
28 
29 #ifndef DINAMICA_COMPILEDDA_T_H_
30 #define DINAMICA_COMPILEDDA_T_H_
31 
32 // DACE classes
33 #include "dace/compiledDA.h"
34 
35 namespace DACE{
36 
37 /********************************************************************************
38 * compiledDA evaluation routines
39 *********************************************************************************/
40 template<class V> V compiledDA::eval(const V &args) const{
51  V res(dim);
52  eval(args, res);
53 
54  return res;
55 }
56 
57 template<class T> std::vector<T> compiledDA::eval(const std::initializer_list<T> l) const{
70  std::vector<T> res(dim);
71  eval(std::vector<T>(l), res);
72 
73  return res;
74 }
75 
76 template<class T> std::vector<T> compiledDA::eval(const T args[], const unsigned int length) const{
87  std::vector<T> arg(args,args+length);
88  std::vector<T> res(dim);
89  eval(arg, res);
90 
91  return res;
92 }
93 
94 template<class T> std::vector<T> compiledDA::evalScalar(const T &arg) const{
102  std::vector<T> args(1);
103  std::vector<T> res(dim);
104  args[0] = arg;
105  eval(args, res);
106 
107  return res;
108 }
109 
110 /* Actual evaluation with a std::vector<T> of algebraic type T.
111  T must support at least:
112  - default constructor T::T()
113  - assignment T::operator=(const T& t)
114  - assignment & addition T::operator+=(const T& t)
115  - double multiplication T::operator*(const double d)
116  - double addition T::operator+(const double d)
117  */
118 template<class T> void compiledDA::eval(const std::vector<T> &args, std::vector<T> &res) const{
119  const unsigned int narg = args.size();
120  unsigned int jlskip = ord+1;
121  double *p = ac+2;
122  T *xm = new T[ord+1];
123 
124  // prepare temporary powers
125  xm[0] = args[0]*0.0 + 1.0;
126  // constant part
127  for(unsigned int i=0; i<dim; i++, p++)
128  res[i] = args[0]*0.0 + (*p);
129  // higher order terms
130  for(unsigned int i=1; i<terms; i++){
131  unsigned int jl = (unsigned int)(*p); p++;
132  unsigned int jv = (unsigned int)(*p)-1; p++;
133  if(jl > jlskip)
134  {
135  p += dim;
136  continue;
137  }
138  if(jv >= narg)
139  {
140  jlskip = jl;
141  p += dim;
142  continue;
143  }
144  jlskip = ord+1;
145  xm[jl] = xm[jl-1]*args[jv];
146  for(unsigned int j=0; j<dim; j++, p++)
147  if((*p)!=0.0) res[j] += xm[jl]*(*p);
148  }
149 
150  delete[] xm;
151 }
152 
153 }
154 #endif /* DINAMICA_COMPILEDDA_T_H_ */
Definition: AlgebraicMatrix.cpp:39
std::vector< T > evalScalar(const T &arg) const
Evaluate the compiled polynomial with a single variable of arithmetic type and return vector of resul...
Definition: compiledDA_t.h:94
V eval(const V &args) const
Evaluate the compiled polynomial with a vector of any arithmetic type and return vector of results...
Definition: compiledDA_t.h:40