DACE 2.0 API Manual
Differential Algebra Core Engine
AlgebraicVector.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  * AlgebraicVector.h
24  *
25  * Created on: Sep. 10, 2014
26  * Author: Dinamica Srl
27  */
28 
29 #ifndef DINAMICA_ALGEBRAICVECTOR_H_
30 #define DINAMICA_ALGEBRAICVECTOR_H_
31 
32 // C++ stdlib classes required for interface definition
33 #include <vector>
34 #include <initializer_list>
35 
36 // DACE classes required for interface definition (DA.h needed for DA::getMaxOrder(), DA::getMaxVariables() default arguments)
37 #include "dace/PromotionTrait.h"
38 #include "dace/DA.h"
39 
40 namespace DACE{
41 
42 // forward declarations
43 #ifdef WITH_ALGEBRAICMATRIX
44 template<typename T> class AlgebraicMatrix;
45 #endif
46 
48 template<typename T> class AlgebraicVector : public std::vector<T>
49 {
50 public:
51  /***********************************************************************************
52  * Constructors
53  ************************************************************************************/
54  AlgebraicVector();
55  explicit AlgebraicVector(const size_t size);
56  AlgebraicVector(const size_t size, const T &d);
57  AlgebraicVector(const std::vector<T> &v);
58  AlgebraicVector(const std::vector<T> &v, const size_t first, const size_t last);
59  AlgebraicVector(std::initializer_list<T> l);
60 
61  /***********************************************************************************
62  * Element and coefficient access / extraction routines
63  ************************************************************************************/
64  AlgebraicVector<T> extract(const size_t first, const size_t last) const;
65  template<typename U> AlgebraicVector<typename PromotionTrait<T,U>::returnType> concat(const std::vector<U> &obj) const;
67  AlgebraicVector<double> cons() const;
68 #ifdef WITH_ALGEBRAICMATRIX
69  AlgebraicMatrix<double> linear() const;
70 #else
71  std::vector< std::vector<double> > linear() const;
72 #endif /* WITH_ALGEBRAICMATRIX */
73 
74  /***********************************************************************************
75  * Operator overloads
76  ************************************************************************************/
77  AlgebraicVector<T> operator-() const;
78  template<typename U> AlgebraicVector<T>& operator+=(const AlgebraicVector<U> &obj);
79  template<typename U> AlgebraicVector<T>& operator+=(const U &obj);
80  template<typename U> AlgebraicVector<T>& operator-=(const AlgebraicVector<U> &obj);
81  template<typename U> AlgebraicVector<T>& operator-=(const U &obj);
82  template<typename U> AlgebraicVector<T>& operator*=(const AlgebraicVector<U> &obj);
83  template<typename U> AlgebraicVector<T>& operator*=(const U &obj);
84  template<typename U> AlgebraicVector<T>& operator/=(const AlgebraicVector<U> &obj);
85  template<typename U> AlgebraicVector<T>& operator/=(const U &obj);
86  template<typename U> AlgebraicVector<T>& operator<<(const std::vector<U> &obj);
87 
88  /***********************************************************************************
89  * Math routines
90  ************************************************************************************/
91  // Included also in the std library cmath
92  AlgebraicVector<T> pow(const int p) const;
93  AlgebraicVector<T> sqrt() const;
94  AlgebraicVector<T> exp() const;
95  AlgebraicVector<T> log() const;
96  AlgebraicVector<T> sin() const;
97  AlgebraicVector<T> cos() const;
98  AlgebraicVector<T> tan() const;
99  AlgebraicVector<T> asin() const;
100  AlgebraicVector<T> acos() const;
101  AlgebraicVector<T> atan() const;
102  AlgebraicVector<T> atan2(const AlgebraicVector<T> &obj) const;
103  AlgebraicVector<T> sinh() const;
104  AlgebraicVector<T> cosh() const;
105  AlgebraicVector<T> tanh() const;
106 
107  // Available in cmath for double only with C++11
108  AlgebraicVector<T> asinh() const;
109  AlgebraicVector<T> acosh() const;
110  AlgebraicVector<T> atanh() const;
111 
112  // Our own math extension routines not included in cmath
113  AlgebraicVector<T> logb(const double b = 10.0) const;
114  AlgebraicVector<T> isrt() const;
115  AlgebraicVector<T> sqr() const;
116  AlgebraicVector<T> minv() const;
117  AlgebraicVector<T> root(const int p = 2) const;
118 
119  /***********************************************************************************
120  * Vector routines
121  ************************************************************************************/
122  template<typename V> typename PromotionTrait<T,V>::returnType dot(const AlgebraicVector<V> &obj) const;
124  template<typename V> AlgebraicVector<typename PromotionTrait<T,V>::returnType> cross(const AlgebraicVector<V> &obj) const;
126  T vnorm() const;
127  AlgebraicVector<T> normalize() const;
128 
129  /***********************************************************************************
130  * Special routines (DA related)
131  ************************************************************************************/
132  AlgebraicVector<T> deriv(const unsigned int p) const;
133  AlgebraicVector<T> integ(const unsigned int p) const;
134  template<typename V> V eval(const V &args) const;
135  template<typename U> AlgebraicVector<U> eval(const std::initializer_list<U> l) const;
136  template<typename U> AlgebraicVector<U> evalScalar(const U &arg) const;
137  compiledDA compile() const;
138  AlgebraicVector<T> plug(const unsigned int var, const double val = 0.0) const;
139  AlgebraicVector<T> trim(const unsigned int min, const unsigned int max = DA::getMaxOrder()) const;
141  AlgebraicVector<T> invert() const;
142  // XXX: define and add the norm estimation routines from DA including abs(), norm(p), convergence radius estimation
143  // XXX: add jacobian routine (returns a DA matrix containing the jacobian)
144  /*
145  double abs() const; //!< Maximum absolute value of all coefficients
146  double norm(const unsigned int type = 0) const; //!< Different types of norms over all coefficients
147  std::vector<double> orderNorm(const unsigned int var = 0, const unsigned int type = 0) const;
149  std::vector<double> estimNorm(const unsigned int var = 0, const unsigned int type = 0, const unsigned int nc = DA::getMaxOrder()) const;
151  Interval bound() const; //!< Estimate range bound over [-1,1] for each independent variable
152  double convRadius(const double eps, const unsigned int type = 1) const; //!< Estimate the convergence radius of the current DA.
153  */
154 
155  /********************************************************************************
156  * Static factory routines
157  *********************************************************************************/
158  static AlgebraicVector<DA> identity(const size_t n = DA::getMaxVariables());
159 
160  /***********************************************************************************
161  * Input/Output routines
162  ************************************************************************************/
163  std::string toString() const;
164 
165 private:
166 #ifndef WITH_ALGEBRAICMATRIX
167  static void matrix_inverse(std::vector< std::vector<double> > &A); // Private helper routine for double precision matrix inversion
168 #endif /* WITH_ALGEBRAICMATRIX */
169 };
170 
171 // operators
172 template<typename U,typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> operator+( const AlgebraicVector<U> &obj1, const AlgebraicVector<V> &obj2);
173 template<typename U,typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> operator+( const AlgebraicVector<U> &obj1, const V &obj2);
174 template<typename U,typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> operator+( const U &obj1, const AlgebraicVector<V> &obj2);
175 
176 template<typename U,typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> operator-( const AlgebraicVector<U> &obj1, const AlgebraicVector<V> &obj2);
177 template<typename U,typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> operator-( const AlgebraicVector<U> &obj1, const V &obj2);
178 template<typename U,typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> operator-( const U &obj1, const AlgebraicVector<V> &obj2);
179 
180 template<typename U,typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> operator*( const AlgebraicVector<U> &obj1, const AlgebraicVector<V> &obj2);
181 template<typename U,typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> operator*( const AlgebraicVector<U> &obj1, const V &obj2);
182 template<typename U,typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> operator*( const U &obj1, const AlgebraicVector<V> &obj2);
183 
184 template<typename U,typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> operator/( const AlgebraicVector<U> &obj1, const AlgebraicVector<V> &obj2);
185 template<typename U,typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> operator/( const AlgebraicVector<U> &obj1, const V &obj2);
186 template<typename U,typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> operator/( const U &obj1, const AlgebraicVector<V> &obj2);
187 
188 template<typename U> std::ostream& operator<<(std::ostream &out, const AlgebraicVector<U> &obj);
189 template<typename U> std::istream& operator>>(std::istream &in, AlgebraicVector<U> &obj);
190 
191 // Declaration of external functional style wrappers to access AlgebraicVector functions
192 template<typename T> AlgebraicVector<double> cons(const AlgebraicVector<T> &obj);
193 #ifdef WITH_ALGEBRAICMATRIX
194 template<typename T> AlgebraicMatrix<double> linear(const AlgebraicVector<T> &obj);
195 #else
196 template<typename T> std::vector< std::vector<double> > linear(const AlgebraicVector<T> &obj);
197 #endif /* WITH_ALGEBRAICMATRIX */
198 template<typename T> AlgebraicVector<T> deriv(const AlgebraicVector<T> &obj, const unsigned int p);
199 template<typename T> AlgebraicVector<T> integ(const AlgebraicVector<T> &obj, const unsigned int p);
200 template<typename T> AlgebraicVector<T> pow(const AlgebraicVector<T> &obj, const int p);
201 template<typename T> AlgebraicVector<T> root(const AlgebraicVector<T> &obj, const int p = 2);
202 template<typename T> AlgebraicVector<T> minv(const AlgebraicVector<T> &obj);
203 template<typename T> AlgebraicVector<T> sqr(const AlgebraicVector<T> &obj);
204 template<typename T> AlgebraicVector<T> sqrt(const AlgebraicVector<T> &obj);
205 template<typename T> AlgebraicVector<T> isrt(const AlgebraicVector<T> &obj);
206 template<typename T> AlgebraicVector<T> exp(const AlgebraicVector<T> &obj);
207 template<typename T> AlgebraicVector<T> log(const AlgebraicVector<T> &obj);
208 template<typename T> AlgebraicVector<T> logb(const AlgebraicVector<T> &obj, const double b = 10.0);
209 template<typename T> AlgebraicVector<T> sin(const AlgebraicVector<T> &obj);
210 template<typename T> AlgebraicVector<T> cos(const AlgebraicVector<T> &obj);
211 template<typename T> AlgebraicVector<T> tan(const AlgebraicVector<T> &obj);
212 template<typename T> AlgebraicVector<T> asin(const AlgebraicVector<T> &obj);
213 template<typename T> AlgebraicVector<T> acos(const AlgebraicVector<T> &obj);
214 template<typename T> AlgebraicVector<T> atan(const AlgebraicVector<T> &obj);
215 template<typename T> AlgebraicVector<T> atan2(const AlgebraicVector<T> &obj1, const AlgebraicVector<T> &obj2);
216 template<typename T> AlgebraicVector<T> sinh(const AlgebraicVector<T> &obj);
217 template<typename T> AlgebraicVector<T> cosh(const AlgebraicVector<T> &obj);
218 template<typename T> AlgebraicVector<T> tanh(const AlgebraicVector<T> &obj);
219 template<typename T> AlgebraicVector<T> asinh(const AlgebraicVector<T> &obj);
220 template<typename T> AlgebraicVector<T> acosh(const AlgebraicVector<T> &obj);
221 template<typename T> AlgebraicVector<T> atanh(const AlgebraicVector<T> &obj);
222 template<typename U, typename V> typename PromotionTrait<U,V>::returnType dot(const AlgebraicVector<U> &obj1, const AlgebraicVector<V> &obj2);
223 template<typename U, typename V> AlgebraicVector<typename PromotionTrait<U,V>::returnType> cross(const AlgebraicVector<U> &obj1, const AlgebraicVector<V> &obj2);
224 template<typename T> T vnorm(const AlgebraicVector<T> &obj);
225 template<typename T> AlgebraicVector<T> normalize(const AlgebraicVector<T> &obj);
226 template<typename T> AlgebraicVector<T> trim(const AlgebraicVector<T> &obj, unsigned int min, unsigned int max = DA::getMaxOrder());
227 template<typename T, typename V> V eval(const AlgebraicVector<T> &obj, const V &args);
228 template<typename T, typename U> AlgebraicVector<U> eval(const AlgebraicVector<T> &obj, const std::initializer_list<U> l);
229 template<typename T, typename U> AlgebraicVector<U> evalScalar(const AlgebraicVector<T> &obj, const U &arg);
230 template<typename T> compiledDA compile(const AlgebraicVector<T> &obj);
231 template<typename T> AlgebraicVector<T> plug(const AlgebraicVector<T> &obj, const unsigned int var, const double val = 0.0);
232 
233 // specializations for various DA specific routines implemented and instantiated directly in the library instead of in a template
234 #ifdef WITH_ALGEBRAICMATRIX
235 template<> DACE_API AlgebraicMatrix<double> AlgebraicVector<DA>::linear() const;
236 template<> DACE_API AlgebraicMatrix<double> linear(const AlgebraicVector<DA> &obj);
237 #else
238 template<> DACE_API std::vector< std::vector<double> > AlgebraicVector<DA>::linear() const;
239 template<> DACE_API void AlgebraicVector<DA>::matrix_inverse(std::vector< std::vector<double> > &A);
240 template<> DACE_API std::vector< std::vector<double> > linear(const AlgebraicVector<DA> &obj);
241 #endif /* WITH_ALGEBRAICMATRIX */
242 template<> DACE_API AlgebraicVector<DA> AlgebraicVector<DA>::trim(const unsigned int min, const unsigned int max) const;
243 template<> DACE_API AlgebraicVector<DA> AlgebraicVector<DA>::deriv(const unsigned int p) const;
244 template<> DACE_API AlgebraicVector<DA> AlgebraicVector<DA>::integ(const unsigned int p) const;
245 template<> DACE_API compiledDA AlgebraicVector<DA>::compile() const;
246 template<> DACE_API AlgebraicVector<DA> AlgebraicVector<DA>::plug(const unsigned int var, const double val) const;
247 template<> DACE_API AlgebraicVector<DA> AlgebraicVector<DA>::invert() const;
248 template<> DACE_API AlgebraicVector<DA> AlgebraicVector<DA>::identity(const size_t n);
249 template<> DACE_API AlgebraicVector<DA> trim(const AlgebraicVector<DA> &obj, unsigned int min, unsigned int max);
250 template<> DACE_API AlgebraicVector<DA> deriv(const AlgebraicVector<DA> &obj, const unsigned int p);
251 template<> DACE_API AlgebraicVector<DA> integ(const AlgebraicVector<DA> &obj, const unsigned int p);
252 template<> DACE_API compiledDA compile(const AlgebraicVector<DA> &obj);
253 template<> DACE_API AlgebraicVector<DA> plug(const AlgebraicVector<DA> &obj, const unsigned int var, const double val);
254 
255 // shortcuts for common vector types
258 }
259 
260 #endif /* DINAMICA_ALGEBRAICVECTOR_H_ */
T vnorm() const
Euclidean vector norm (length).
Definition: AlgebraicVector_t.h:824
AlgebraicVector< T > acosh() const
Element-wise hyperbolic arccosine.
Definition: AlgebraicVector_t.h:791
std::istream & operator>>(std::istream &in, AlgebraicMatrix< DA > &obj)
DA specialization of input stream operator.
Definition: AlgebraicMatrix.cpp:65
DA operator/(const DA &da1, const DA &da2)
Definition: DA.cpp:804
AlgebraicVector< T > integ(const unsigned int p) const
Integration of each element with respect to given variable. DA only.
AlgebraicVector< double > cons() const
Return vector containing only the costant parts of each element.
Definition: AlgebraicVector_t.h:99
static unsigned int getMaxOrder()
Get the maximum order.
Definition: DA.cpp:111
AlgebraicVector< T > atanh() const
Element-wise hyperbolic arctangent.
Definition: AlgebraicVector_t.h:806
AlgebraicVector< T > log() const
Element-wise natural logarithm.
Definition: AlgebraicVector_t.h:528
AlgebraicVector< U > evalScalar(const U &arg) const
Generic evaluation of a AlgebraicVector<DA> with single argument. DA only.
AlgebraicVector< double > vectordb
Shorthand notation for AlgebraicVector<double>.
Definition: AlgebraicVector.h:257
AlgebraicVector< T > acos() const
Element-wise arccosine.
Definition: AlgebraicVector_t.h:603
AlgebraicVector< typename PromotionTrait< T, U >::returnType > concat(const std::vector< U > &obj) const
Return a new vector containing the elements of this vector followed by those of obj.
AlgebraicVector< T > normalize() const
Normalized vector of unit length along this vector.
Definition: AlgebraicVector_t.h:839
double returnType
Definition: PromotionTrait.h:42
AlgebraicVector< T > minv() const
Element-wise multiplicative inverse.
Definition: AlgebraicVector_t.h:745
AlgebraicVector< T > isrt() const
Element-wise inverse square root.
Definition: AlgebraicVector_t.h:715
AlgebraicVector< T > & operator*=(const AlgebraicVector< U > &obj)
Definition: AlgebraicVector_t.h:210
DA operator*(const DA &da1, const DA &da2)
Definition: DA.cpp:759
AlgebraicVector< T > extract(const size_t first, const size_t last) const
Return the subvector containing the elements between first and last, inclusively. ...
Definition: AlgebraicVector_t.h:115
std::string toString() const
Convert the vector into a human readable string.
Definition: AlgebraicVector_t.h:953
static unsigned int getMaxVariables()
Get the maximum number of variables.
Definition: DA.cpp:158
AlgebraicVector< T > & operator-=(const AlgebraicVector< U > &obj)
Definition: AlgebraicVector_t.h:184
AlgebraicVector< T > plug(const unsigned int var, const double val=0.0) const
Partial evaluation to replace given independent DA variable by value val. DA only.
PromotionTrait< T, V >::returnType dot(const AlgebraicVector< V > &obj) const
Dot product (scalar product, inner product) of two vectors.
Definition: AlgebraicVector_t.h:444
AlgebraicVector< T > atan() const
Element-wise arctangent.
Definition: AlgebraicVector_t.h:618
AlgebraicVector< T > sinh() const
Element-wise hyperbolic sine.
Definition: AlgebraicVector_t.h:654
AlgebraicVector< T > atan2(const AlgebraicVector< T > &obj) const
Element-wise arctangent in [-pi, pi].
Definition: AlgebraicVector_t.h:633
AlgebraicVector< T > root(const int p=2) const
Element-wise p-th root.
Definition: AlgebraicVector_t.h:760
Definition: AlgebraicMatrix.h:43
AlgebraicVector< T > asin() const
Element-wise arcsine.
Definition: AlgebraicVector_t.h:588
AlgebraicVector< T > tan() const
Element-wise tangent.
Definition: AlgebraicVector_t.h:573
AlgebraicVector< T > exp() const
Element-wise exponential.
Definition: AlgebraicVector_t.h:513
V eval(const V &args) const
Generic evaluation of a AlgebraicVector<DA> with arguments. DA only.
AlgebraicVector< DA > vectorDA
Shorthand notation for AlgebraicVector<DA>.
Definition: AlgebraicVector.h:256
AlgebraicVector< T > cos() const
Element-wise cosine.
Definition: AlgebraicVector_t.h:558
AlgebraicVector< T > cosh() const
Element-wise hyperbolic cosine.
Definition: AlgebraicVector_t.h:669
AlgebraicVector< T > sqrt() const
Element-wise square root.
Definition: AlgebraicVector_t.h:498
AlgebraicVector< typename PromotionTrait< T, V >::returnType > cross(const AlgebraicVector< V > &obj) const
Cross product of two vectors of length 3.
Definition: AlgebraicVector_t.h:461
compiledDA compile() const
Compile current DA for efficient repeated evaluation. DA only.
AlgebraicVector< T > tanh() const
Element-wise hyperbolic tangent.
Definition: AlgebraicVector_t.h:684
AlgebraicVector< T > sin() const
Element-wise sine.
Definition: AlgebraicVector_t.h:543
AlgebraicVector< T > & operator/=(const AlgebraicVector< U > &obj)
Definition: AlgebraicVector_t.h:235
AlgebraicVector< T > deriv(const unsigned int p) const
Derivative of each element with respect to given variable. DA only.
#define DACE_API
Definition: dace_s.h:33
Definition: AlgebraicMatrix.cpp:39
AlgebraicVector< T > pow(const int p) const
Element-wise exponentiation to (integer) power.
Definition: AlgebraicVector_t.h:482
AlgebraicVector< T > operator-() const
Definition: AlgebraicVector_t.h:151
static AlgebraicVector< DA > identity(const size_t n=DA::getMaxVariables())
Create an AlgebraicVector<DA> containing the identity in n dimensions. DA only.
AlgebraicVector< T > trim(const unsigned int min, const unsigned int max=DA::getMaxOrder()) const
Trim the coefficients of each components to particular orders. DA only.
AlgebraicVector< T > & operator+=(const AlgebraicVector< U > &obj)
Definition: AlgebraicVector_t.h:158
unsigned int size(const DA &da)
Definition: DA.cpp:2549
AlgebraicVector< T > asinh() const
Element-wise hyperbolic arcsine.
Definition: AlgebraicVector_t.h:776
#define min(a, b)
Definition: f2c.h:159
AlgebraicVector< T > invert() const
Inverse function of the AlgebraicVector<DA>. DA only.
std::vector< std::vector< double > > linear() const
Return the linear parts in the form of a vector of vectors.
AlgebraicVector()
Default constructor.
Definition: AlgebraicVector_t.h:53
AlgebraicVector< T > sqr() const
Element-wise square.
Definition: AlgebraicVector_t.h:730
DA operator+(const DA &da1, const DA &da2)
Definition: DA.cpp:669
AlgebraicVector< T > logb(const double b=10.0) const
Element-wise logarithm wrt a given base.
Definition: AlgebraicVector_t.h:699
#define max(a, b)
Definition: f2c.h:160