1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

root/trunk/casadi/casadi_math.hpp @ 1500

Revision 1500, 29.5 KB (checked in by joelandersson, 23 months ago)

hard coded which function are zero when evaluated with zero as one or both of the arguments

Line 
1/*
2 *    This file is part of CasADi.
3 *
4 *    CasADi -- A symbolic framework for dynamic optimization.
5 *    Copyright (C) 2010 by Joel Andersson, Moritz Diehl, K.U.Leuven. All rights reserved.
6 *
7 *    CasADi is free software; you can redistribute it and/or
8 *    modify it under the terms of the GNU Lesser General Public
9 *    License as published by the Free Software Foundation; either
10 *    version 3 of the License, or (at your option) any later version.
11 *
12 *    CasADi is distributed in the hope that it will be useful,
13 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 *    Lesser General Public License for more details.
16 *
17 *    You should have received a copy of the GNU Lesser General Public
18 *    License along with CasADi; if not, write to the Free Software
19 *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 *
21 */
22
23#ifndef CASADI_MATH_HPP
24#define CASADI_MATH_HPP
25
26#include <iostream>
27#include <string>
28#include <cmath>
29#include <vector>
30#include "casadi_exception.hpp"
31#include "pre_c99_support.hpp"
32
33namespace CasADi{
34
35template<typename T>
36T timesTwo(const T& x){
37  return x+x;
38}
39 
40template<typename T>
41T square(const T& x){
42  return x*x;
43}
44 
45template<int I>
46class UnaryOperation{
47  public:
48    /// Print
49    static void print(std::ostream &stream, const std::string& x);
50
51    /// Does evaluating the function with a zero give a zero
52    static bool f0_is_zero(){return false;}
53
54    /// Function evaluation
55    template<typename T> static void fcn(const T& x, T& f);
56   
57    /// Partial derivatives
58    template<typename T> static void der(const T& x, const T& f, T* d);
59};
60
61template<int I>
62class BinaryOperation{
63  public:
64    /// Print
65    static void print(std::ostream &stream, const std::string& x, const std::string& y){ UnaryOperation<I>::print(stream,x); }
66
67    /// Does evaluating the function with a zero give a zero
68    static bool f00_is_zero(){return UnaryOperation<I>::f0_is_zero();}
69    static bool f0x_is_zero(){return UnaryOperation<I>::f0_is_zero();}
70    static bool fx0_is_zero(){return false;}
71
72    /// Function evaluation
73    template<typename T> static void fcn(const T& x, const T& y, T& f){ UnaryOperation<I>::fcn(x,f);}
74   
75    /// Partial derivatives - binary function
76    template<typename T> static void der(const T& x, const T& y, const T& f, T* d){ UnaryOperation<I>::der(x,f,d); d[1]=0; }
77};
78
79template<int I>
80class BinaryOperationE{
81  public:
82    /// Function evaluation
83    template<typename T> static T fcn(const T& x, const T& y){ 
84      T ret;
85      BinaryOperation<I>::fcn(x,y,ret);
86      return ret;
87    }
88};
89
90/// Enum for quick access to any node
91enum Operation{
92  ADD,  SUB,  MUL,  DIV,
93  NEG,  EXP,  LOG,  POW, CONSTPOW,
94  SQRT,  SIN,  COS,  TAN, 
95  ASIN,  ACOS,  ATAN, 
96  STEP, 
97  FLOOR,  CEIL,  EQUALITY, 
98  ERF,  FMIN,  FMAX,
99  NUM_BUILT_IN_OPS
100};
101
102/// Addition
103template<>
104class BinaryOperation<ADD>{
105  public:
106    static void print(std::ostream &stream, const std::string& x, const std::string& y){ stream << "(" << x << "+" << y << ")"; }
107    static bool f00_is_zero(){return true;}
108    static bool f0x_is_zero(){return false;}
109    static bool fx0_is_zero(){return false;}
110    template<typename T> static void fcn(const T& x, const T& y, T& f){ f = x+y;}
111    template<typename T> static void der(const T& x, const T& y, const T& f, T* d){ d[0]=d[1]=1;}
112};
113
114/// Subtraction
115template<>
116class BinaryOperation<SUB>{
117  public:
118    static void print(std::ostream &stream, const std::string& x, const std::string& y){ stream << "(" << x << "-" << y << ")"; }
119    static bool f00_is_zero(){return true;}
120    static bool f0x_is_zero(){return false;}
121    static bool fx0_is_zero(){return false;}
122    template<typename T> static void fcn(const T& x, const T& y, T& f){ f = x-y;}
123    template<typename T> static void der(const T& x, const T& y, const T& f, T* d){ d[0]=1; d[1]=-1;}
124};
125
126/// Multiplication
127template<>
128class BinaryOperation<MUL>{
129  public:
130    static void print(std::ostream &stream, const std::string& x, const std::string& y){ stream << "(" << x << "*" << y << ")"; }
131    static bool f00_is_zero(){return true;}
132    static bool f0x_is_zero(){return true;}
133    static bool fx0_is_zero(){return true;}
134    template<typename T> static void fcn(const T& x, const T& y, T& f){ f = x*y;}
135    template<typename T> static void der(const T& x, const T& y, const T& f, T* d){ d[0]=y; d[1]=x;}
136};
137
138/// Division
139template<>
140class BinaryOperation<DIV>{
141  public:
142    static void print(std::ostream &stream, const std::string& x, const std::string& y){ stream << "(" << x << "/" << y << ")"; }
143    static bool f00_is_zero(){return false;}
144    static bool f0x_is_zero(){return true;}
145    static bool fx0_is_zero(){return false;}
146    template<typename T> static void fcn(const T& x, const T& y, T& f){ f = x/y;}
147    template<typename T> static void der(const T& x, const T& y, const T& f, T* d){ d[0]=1/y; d[1]=-f/y;}
148};
149
150/// Negation
151template<>
152class UnaryOperation<NEG>{
153  public:
154    static void print(std::ostream &stream, const std::string& x){ stream << "(-" << x << ")"; }
155    static bool f0_is_zero(){return true;}
156    template<typename T> static void fcn(const T& x, T& f){ f = -x;}
157    template<typename T> static void der(const T& x, const T& f, T* d){ d[0]=-1;}
158};
159
160/// Natural exponent
161template<>
162class UnaryOperation<EXP>{
163  public:
164    static void print(std::ostream &stream, const std::string& x){ stream << "exp(" << x << ")"; }
165    static bool f0_is_zero(){return false;}
166    template<typename T> static void fcn(const T& x, T& f){ f = std::exp(x);}
167    template<typename T> static void der(const T& x, const T& f, T* d){ d[0]=f;}
168};
169
170/// Natural logarithm
171template<>
172class UnaryOperation<LOG>{
173  public:
174    static void print(std::ostream &stream, const std::string& x){ stream << "log(" << x << ")"; }
175    static bool f0_is_zero(){return false;}
176    template<typename T> static void fcn(const T& x, T& f){ f = std::log(x);}
177    template<typename T> static void der(const T& x, const T& f, T* d){ d[0]=1/x;}
178};
179
180/// Power, defined only for x>=0
181template<>
182class BinaryOperation<POW>{
183  public:
184    static void print(std::ostream &stream, const std::string& x, const std::string& y){ stream << "pow(" << x << "," << y << ")"; }
185    static bool f00_is_zero(){return false;}
186    static bool f0x_is_zero(){return false;}
187    static bool fx0_is_zero(){return false;}
188    template<typename T> static void fcn(const T& x, const T& y, T& f){ f = std::pow(x,y);}
189    // See issue #104 why d[0] is no longer y*f/x
190    template<typename T> static void der(const T& x, const T& y, const T& f, T* d){ d[0]=y*std::pow(x,y-1); d[1]=std::log(x)*f;}
191};
192
193/// Power, defined only for y constant
194template<>
195class BinaryOperation<CONSTPOW>{
196  public:
197    static void print(std::ostream &stream, const std::string& x, const std::string& y){ stream << "constpow(" << x << "," << y << ")"; }
198    static bool f00_is_zero(){return false;}
199    static bool f0x_is_zero(){return false;}
200    static bool fx0_is_zero(){return false;}
201    template<typename T> static void fcn(const T& x, const T& y, T& f){ f = std::pow(x,y);}
202    template<typename T> static void der(const T& x, const T& y, const T& f, T* d){ d[0]=y*std::pow(x,y-1); d[1]=0;}
203};
204
205/// Square root
206template<>
207class UnaryOperation<SQRT>{
208  public:
209    static void print(std::ostream &stream, const std::string& x){ stream << "sqrt(" << x << ")"; }
210    static bool f0_is_zero(){return true;}
211    template<typename T> static void fcn(const T& x, T& f){ f = std::sqrt(x);}
212    template<typename T> static void der(const T& x, const T& f, T* d){ d[0]=1/(timesTwo(f));}
213};
214
215/// Sine
216template<>
217class UnaryOperation<SIN>{
218  public:
219    static void print(std::ostream &stream, const std::string& x){ stream << "sin(" << x << ")"; }
220    static bool f0_is_zero(){return true;}
221    template<typename T> static void fcn(const T& x, T& f){ f = std::sin(x);}
222    template<typename T> static void der(const T& x, const T& f, T* d){ d[0]=std::cos(x);}
223};
224
225/// Cosine
226template<>
227class UnaryOperation<COS>{
228  public:
229    static void print(std::ostream &stream, const std::string& x){ stream << "cos(" << x << ")"; }
230    static bool f0_is_zero(){return false;}
231    template<typename T> static void fcn(const T& x, T& f){ f = std::cos(x);}
232    template<typename T> static void der(const T& x, const T& f, T* d){ d[0]=-std::sin(x);}
233};
234
235/// Tangent
236template<>
237class UnaryOperation<TAN>{
238  public:
239    static void print(std::ostream &stream, const std::string& x){ stream << "tan(" << x << ")"; }
240    static bool f0_is_zero(){return true;}
241    template<typename T> static void fcn(const T& x, T& f){ f = std::tan(x);}
242    template<typename T> static void der(const T& x, const T& f, T* d){ d[0] = 1/square(std::cos(x));}
243};
244
245/// Arcus sine
246template<>
247class UnaryOperation<ASIN>{
248  public:
249    static void print(std::ostream &stream, const std::string& x){ stream << "asin(" << x << ")"; }
250    static bool f0_is_zero(){return true;}
251    template<typename T> static void fcn(const T& x, T& f){ f = std::asin(x);}
252    template<typename T> static void der(const T& x, const T& f, T* d){ d[0]=1/std::sqrt(1-x*x);}
253};
254
255/// Arcus cosine
256template<>
257class UnaryOperation<ACOS>{
258  public:
259    static void print(std::ostream &stream, const std::string& x){ stream << "acos(" << x << ")"; }
260    static bool f0_is_zero(){return false;}
261    template<typename T> static void fcn(const T& x, T& f){ f = std::acos(x);}
262    template<typename T> static void der(const T& x, const T& f, T* d){ d[0]=-1/std::sqrt(1-x*x);}
263};
264
265/// Arcus tangent
266template<>
267class UnaryOperation<ATAN>{
268  public:
269    static void print(std::ostream &stream, const std::string& x){ stream << "atan(" << x << ")"; }
270    static bool f0_is_zero(){return true;}
271    template<typename T> static void fcn(const T& x, T& f){ f = std::atan(x);}
272    template<typename T> static void der(const T& x, const T& f, T* d){ d[0] = 1/(1+x*x);}
273};
274
275/// Step function
276template<>
277class UnaryOperation<STEP>{
278  public:
279    static void print(std::ostream &stream, const std::string& x){ stream << "(" << x << ">=0)"; }
280    static bool f0_is_zero(){return false;}
281    template<typename T> static void fcn(const T& x, T& f){ f = x >= T(0.);}
282    template<typename T> static void der(const T& x, const T& f, T* d){ d[0] = 0;}
283};
284
285/// Floor function
286template<>
287class UnaryOperation<FLOOR>{
288  public:
289    static void print(std::ostream &stream, const std::string& x){ stream << "floor(" << x << ")"; }
290    static bool f0_is_zero(){return true;}
291    template<typename T> static void fcn(const T& x, T& f){ f = std::floor(x);}
292    template<typename T> static void der(const T& x, const T& f, T* d){ d[0] = 0;}
293};
294
295/// Ceil function
296template<>
297class UnaryOperation<CEIL>{
298  public:
299    static void print(std::ostream &stream, const std::string& x){ stream << "ceil(" << x << ")"; }
300    static bool f0_is_zero(){return true;}
301    template<typename T> static void fcn(const T& x, T& f){ f = std::ceil(x);}
302    template<typename T> static void der(const T& x, const T& f, T* d){ d[0] = 0;}
303};
304
305/// Equality
306template<>
307class BinaryOperation<EQUALITY>{
308  public:
309    static void print(std::ostream &stream, const std::string& x, const std::string& y){ stream << "(" << x << "==" << y << ")"; }
310    static bool f00_is_zero(){return false;}
311    static bool f0x_is_zero(){return false;}
312    static bool fx0_is_zero(){return false;}
313    template<typename T> static void fcn(const T& x, const T& y, T& f){ f = x==y;}
314    template<typename T> static void der(const T& x, const T& y, const T& f, T* d){ d[0]=d[1]=0;}
315};
316
317/// Minimum
318template<>
319class BinaryOperation<FMIN>{
320  public:
321    static void print(std::ostream &stream, const std::string& x, const std::string& y){ stream << "fmin(" << x << "," << y << ")"; }
322    static bool f00_is_zero(){return true;}
323    static bool f0x_is_zero(){return false;}
324    static bool fx0_is_zero(){return false;}
325    template<typename T> static void fcn(const T& x, const T& y, T& f){ f = fmin(x,y);}
326    template<typename T> static void der(const T& x, const T& y, const T& f, T* d){ d[0]=x<=y; d[1]=!d[0];}
327};
328
329/// Maximum
330template<>
331class BinaryOperation<FMAX>{
332  public:
333    static void print(std::ostream &stream, const std::string& x, const std::string& y){ stream << "fmax(" << x << "," << y << ")"; }
334    static bool f00_is_zero(){return true;}
335    static bool f0x_is_zero(){return false;}
336    static bool fx0_is_zero(){return false;}
337    template<typename T> static void fcn(const T& x, const T& y, T& f){ f = fmax(x,y);}
338    template<typename T> static void der(const T& x, const T& y, const T& f, T* d){ d[0]=x>=y; d[1]=!d[0];}
339};
340
341/// Error function
342template<>
343class UnaryOperation<ERF>{
344  public:
345    static void print(std::ostream &stream, const std::string& x){ stream << "erf(" << x << ")"; }
346    static bool f0_is_zero(){return true;}
347    template<typename T> static void fcn(const T& x, T& f){ f = erf(x);}
348    template<typename T> static void der(const T& x, const T& f, T* d){ d[0] = (2/std::sqrt(M_PI))*std::exp(-x*x);}
349};
350
351/// Easy access to all the functions for a particular type
352template<typename T>
353class casadi_math{
354  public:
355
356    /** \brief Printing operation typedef */
357    typedef void (*printFunT)(std::ostream &stream, const std::string& x, const std::string& y);
358
359    /** \brief Function typedef */
360    typedef void (*funT)(const T&, const T&, T&);
361    typedef T (*funTE)(const T&, const T&);
362       
363    /** \brief Derivative typedef */
364    typedef void (*derT)(const T& x, const T& y, const T& f, T* d);
365
366    /** \brief Vector of printing functions */
367    static std::vector<printFunT> print;
368
369    /** \brief Vector of function pointers to all the built in functions */
370    static std::vector<funT> fun;
371    static std::vector<funTE> funE;
372   
373    /** \brief Vector of function derivative pointers to all the built in functions */
374    static std::vector<derT> der;
375   
376    /** \brief Vector of booleans indicating which functions are zero when evaluating with both arguments zero */
377    static std::vector<bool> f00_is_zero;
378   
379    /** \brief Vector of booleans indicating which functions are zero when evaluating with the first arguments zero */
380    static std::vector<bool> f0x_is_zero;
381   
382    /** \brief Vector of booleans indicating which functions are zero when evaluating with the second arguments zero */
383    static std::vector<bool> fx0_is_zero;
384   
385  protected:
386   
387    /** \brief Create print */
388    static std::vector<printFunT> getPrintFun();
389
390    /** \brief Create fun */
391    static std::vector<funT> getFun();
392    static std::vector<funTE> getFunE();
393 
394    /** \brief Create der */
395    static std::vector<derT> getDer();
396   
397    /** \brief Create zero indicator vectors */
398    static std::vector<bool> getF00_is_zero();
399    static std::vector<bool> getF0x_is_zero();
400    static std::vector<bool> getFx0_is_zero();
401   
402   
403};
404
405
406// Template implementations
407
408template<typename T>
409std::vector<typename casadi_math<T>::printFunT> casadi_math<T>::print = casadi_math<T>::getPrintFun();
410
411template<typename T>
412std::vector<typename casadi_math<T>::funT> casadi_math<T>::fun = casadi_math<T>::getFun();
413
414template<typename T>
415std::vector<typename casadi_math<T>::funTE> casadi_math<T>::funE = casadi_math<T>::getFunE();
416
417template<typename T>
418std::vector<typename casadi_math<T>::derT> casadi_math<T>::der = casadi_math<T>::getDer();
419
420template<typename T>
421std::vector<bool> casadi_math<T>::f00_is_zero = casadi_math<T>::getF00_is_zero();
422
423template<typename T>
424std::vector<bool> casadi_math<T>::f0x_is_zero = casadi_math<T>::getF0x_is_zero();
425
426template<typename T>
427std::vector<bool> casadi_math<T>::fx0_is_zero = casadi_math<T>::getFx0_is_zero();
428
429template<typename T>
430std::vector<typename casadi_math<T>::printFunT> casadi_math<T>::getPrintFun(){
431  // Create return object
432  std::vector<typename casadi_math<T>::printFunT> ret(NUM_BUILT_IN_OPS,0);
433 
434  // Specify operations
435  ret[ADD] = BinaryOperation<ADD>::print;
436  ret[SUB] = BinaryOperation<SUB>::print;
437  ret[MUL] = BinaryOperation<MUL>::print;
438  ret[DIV] = BinaryOperation<DIV>::print;
439 
440  ret[NEG] = BinaryOperation<NEG>::print;
441  ret[EXP] = BinaryOperation<EXP>::print;
442  ret[LOG] = BinaryOperation<LOG>::print;
443  ret[POW] = BinaryOperation<POW>::print;
444  ret[CONSTPOW] = BinaryOperation<CONSTPOW>::print;
445
446  ret[SQRT] = BinaryOperation<SQRT>::print;
447  ret[SIN] = BinaryOperation<SIN>::print;
448  ret[COS] = BinaryOperation<COS>::print;
449  ret[TAN] = BinaryOperation<TAN>::print;
450
451  ret[ASIN] = BinaryOperation<ASIN>::print;
452  ret[ACOS] = BinaryOperation<ACOS>::print;
453  ret[ATAN] = BinaryOperation<ATAN>::print;
454
455  ret[STEP] = BinaryOperation<STEP>::print;
456  ret[FLOOR] = BinaryOperation<FLOOR>::print;
457  ret[CEIL] = BinaryOperation<CEIL>::print;
458
459  ret[EQUALITY] = BinaryOperation<EQUALITY>::print;
460
461  ret[ERF] = BinaryOperation<ERF>::print;
462  ret[FMIN] = BinaryOperation<FMIN>::print;
463  ret[FMAX] = BinaryOperation<FMAX>::print;
464
465  // Make sure that all functions were specified
466  for(int i=0; i<ret.size(); ++i){
467    casadi_assert(ret[i]!=0);
468  }
469 
470  return ret;
471 
472}
473
474template<typename T>
475std::vector<typename casadi_math<T>::funT> casadi_math<T>::getFun(){
476 
477  // Create return object
478  std::vector<typename casadi_math<T>::funT> ret(NUM_BUILT_IN_OPS,0);
479 
480#ifdef _MSC_VER
481  // Specify operations
482  ret[ADD] = reinterpret_cast<funT>(BinaryOperation<ADD>::fcn);
483  ret[SUB] = reinterpret_cast<funT>(BinaryOperation<SUB>::fcn);
484  ret[MUL] = reinterpret_cast<funT>(BinaryOperation<MUL>::fcn);
485  ret[DIV] = reinterpret_cast<funT>(BinaryOperation<DIV>::fcn);
486   
487  ret[NEG] = reinterpret_cast<funT>(BinaryOperation<NEG>::fcn);
488  ret[EXP] = reinterpret_cast<funT>(BinaryOperation<EXP>::fcn);
489  ret[LOG] = reinterpret_cast<funT>(BinaryOperation<LOG>::fcn);
490  ret[POW] = reinterpret_cast<funT>(BinaryOperation<POW>::fcn);
491  ret[CONSTPOW] = reinterpret_cast<funT>(BinaryOperation<CONSTPOW>::fcn);
492
493  ret[SQRT] = reinterpret_cast<funT>(BinaryOperation<SQRT>::fcn);
494  ret[SIN] = reinterpret_cast<funT>(BinaryOperation<SIN>::fcn);
495  ret[COS] = reinterpret_cast<funT>(BinaryOperation<COS>::fcn);
496  ret[TAN] = reinterpret_cast<funT>(BinaryOperation<TAN>::fcn);
497
498  ret[ASIN] = reinterpret_cast<funT>(BinaryOperation<ASIN>::fcn);
499  ret[ACOS] = reinterpret_cast<funT>(BinaryOperation<ACOS>::fcn);
500  ret[ATAN] = reinterpret_cast<funT>(BinaryOperation<ATAN>::fcn);
501
502  ret[STEP] = reinterpret_cast<funT>(BinaryOperation<STEP>::fcn);
503  ret[FLOOR] = reinterpret_cast<funT>(BinaryOperation<FLOOR>::fcn);
504  ret[CEIL] = reinterpret_cast<funT>(BinaryOperation<CEIL>::fcn);
505
506  ret[EQUALITY] = reinterpret_cast<funT>(BinaryOperation<EQUALITY>::fcn);
507
508  ret[ERF] = reinterpret_cast<funT>(BinaryOperation<ERF>::fcn);
509  ret[FMIN] = reinterpret_cast<funT>(BinaryOperation<FMIN>::fcn);
510  ret[FMAX] = reinterpret_cast<funT>(BinaryOperation<FMAX>::fcn);
511#else // _MSC_VER
512 
513  // Specify operations
514  ret[ADD] = BinaryOperation<ADD>::fcn;
515  ret[SUB] = BinaryOperation<SUB>::fcn;
516  ret[MUL] = BinaryOperation<MUL>::fcn;
517  ret[DIV] = BinaryOperation<DIV>::fcn;
518   
519  ret[NEG] = BinaryOperation<NEG>::fcn;
520  ret[EXP] = BinaryOperation<EXP>::fcn;
521  ret[LOG] = BinaryOperation<LOG>::fcn;
522  ret[POW] = BinaryOperation<POW>::fcn;
523  ret[CONSTPOW] = BinaryOperation<CONSTPOW>::fcn;
524
525  ret[SQRT] = BinaryOperation<SQRT>::fcn;
526  ret[SIN] = BinaryOperation<SIN>::fcn;
527  ret[COS] = BinaryOperation<COS>::fcn;
528  ret[TAN] = BinaryOperation<TAN>::fcn;
529
530  ret[ASIN] = BinaryOperation<ASIN>::fcn;
531  ret[ACOS] = BinaryOperation<ACOS>::fcn;
532  ret[ATAN] = BinaryOperation<ATAN>::fcn;
533
534  ret[STEP] = BinaryOperation<STEP>::fcn;
535  ret[FLOOR] = BinaryOperation<FLOOR>::fcn;
536  ret[CEIL] = BinaryOperation<CEIL>::fcn;
537
538  ret[EQUALITY] = BinaryOperation<EQUALITY>::fcn;
539
540  ret[ERF] = BinaryOperation<ERF>::fcn;
541  ret[FMIN] = BinaryOperation<FMIN>::fcn;
542  ret[FMAX] = BinaryOperation<FMAX>::fcn;
543 
544#endif // _MSC_VER
545
546  // Make sure that all functions were specified
547  for(int i=0; i<ret.size(); ++i){
548    casadi_assert(ret[i]!=0);
549  }
550 
551  return ret;
552}
553
554template<typename T>
555std::vector<typename casadi_math<T>::funTE> casadi_math<T>::getFunE(){
556 
557  // Create return object
558  std::vector<typename casadi_math<T>::funTE> ret(NUM_BUILT_IN_OPS,0);
559 
560#ifdef _MSC_VER
561  // Specify operations
562  ret[ADD] = reinterpret_cast<funT>(BinaryOperationE<ADD>::fcn);
563  ret[SUB] = reinterpret_cast<funT>(BinaryOperationE<SUB>::fcn);
564  ret[MUL] = reinterpret_cast<funT>(BinaryOperationE<MUL>::fcn);
565  ret[DIV] = reinterpret_cast<funT>(BinaryOperationE<DIV>::fcn);
566   
567  ret[NEG] = reinterpret_cast<funT>(BinaryOperationE<NEG>::fcn);
568  ret[EXP] = reinterpret_cast<funT>(BinaryOperationE<EXP>::fcn);
569  ret[LOG] = reinterpret_cast<funT>(BinaryOperationE<LOG>::fcn);
570  ret[POW] = reinterpret_cast<funT>(BinaryOperationE<POW>::fcn);
571  ret[CONSTPOW] = reinterpret_cast<funT>(BinaryOperationE<CONSTPOW>::fcn);
572
573  ret[SQRT] = reinterpret_cast<funT>(BinaryOperationE<SQRT>::fcn);
574  ret[SIN] = reinterpret_cast<funT>(BinaryOperationE<SIN>::fcn);
575  ret[COS] = reinterpret_cast<funT>(BinaryOperationE<COS>::fcn);
576  ret[TAN] = reinterpret_cast<funT>(BinaryOperationE<TAN>::fcn);
577
578  ret[ASIN] = reinterpret_cast<funT>(BinaryOperationE<ASIN>::fcn);
579  ret[ACOS] = reinterpret_cast<funT>(BinaryOperationE<ACOS>::fcn);
580  ret[ATAN] = reinterpret_cast<funT>(BinaryOperationE<ATAN>::fcn);
581
582  ret[STEP] = reinterpret_cast<funT>(BinaryOperationE<STEP>::fcn);
583  ret[FLOOR] = reinterpret_cast<funT>(BinaryOperationE<FLOOR>::fcn);
584  ret[CEIL] = reinterpret_cast<funT>(BinaryOperationE<CEIL>::fcn);
585
586  ret[EQUALITY] = reinterpret_cast<funT>(BinaryOperationE<EQUALITY>::fcn);
587
588  ret[ERF] = reinterpret_cast<funT>(BinaryOperationE<ERF>::fcn);
589  ret[FMIN] = reinterpret_cast<funT>(BinaryOperationE<FMIN>::fcn);
590  ret[FMAX] = reinterpret_cast<funT>(BinaryOperationE<FMAX>::fcn);
591#else // _MSC_VER
592 
593  // Specify operations
594  ret[ADD] = BinaryOperationE<ADD>::fcn;
595  ret[SUB] = BinaryOperationE<SUB>::fcn;
596  ret[MUL] = BinaryOperationE<MUL>::fcn;
597  ret[DIV] = BinaryOperationE<DIV>::fcn;
598   
599  ret[NEG] = BinaryOperationE<NEG>::fcn;
600  ret[EXP] = BinaryOperationE<EXP>::fcn;
601  ret[LOG] = BinaryOperationE<LOG>::fcn;
602  ret[POW] = BinaryOperationE<POW>::fcn;
603  ret[CONSTPOW] = BinaryOperationE<CONSTPOW>::fcn;
604
605  ret[SQRT] = BinaryOperationE<SQRT>::fcn;
606  ret[SIN] = BinaryOperationE<SIN>::fcn;
607  ret[COS] = BinaryOperationE<COS>::fcn;
608  ret[TAN] = BinaryOperationE<TAN>::fcn;
609
610  ret[ASIN] = BinaryOperationE<ASIN>::fcn;
611  ret[ACOS] = BinaryOperationE<ACOS>::fcn;
612  ret[ATAN] = BinaryOperationE<ATAN>::fcn;
613
614  ret[STEP] = BinaryOperationE<STEP>::fcn;
615  ret[FLOOR] = BinaryOperationE<FLOOR>::fcn;
616  ret[CEIL] = BinaryOperationE<CEIL>::fcn;
617
618  ret[EQUALITY] = BinaryOperationE<EQUALITY>::fcn;
619
620  ret[ERF] = BinaryOperationE<ERF>::fcn;
621  ret[FMIN] = BinaryOperationE<FMIN>::fcn;
622  ret[FMAX] = BinaryOperationE<FMAX>::fcn;
623 
624#endif // _MSC_VER
625
626  // Make sure that all functions were specified
627  for(int i=0; i<ret.size(); ++i){
628    casadi_assert(ret[i]!=0);
629  }
630 
631  return ret;
632}
633
634template<typename T>
635std::vector<typename casadi_math<T>::derT> casadi_math<T>::getDer(){
636  // Create return object
637  std::vector<typename casadi_math<T>::derT> ret(NUM_BUILT_IN_OPS,0);
638 
639  // Specify operations
640#ifdef _MSC_VER
641  ret[ADD] = reinterpret_cast<derT>(BinaryOperation<ADD>::der);
642  ret[SUB] = reinterpret_cast<derT>(BinaryOperation<SUB>::der);
643  ret[MUL] = reinterpret_cast<derT>(BinaryOperation<MUL>::der);
644  ret[DIV] = reinterpret_cast<derT>(BinaryOperation<DIV>::der);
645   
646  ret[NEG] = reinterpret_cast<derT>(BinaryOperation<NEG>::der);
647  ret[EXP] = reinterpret_cast<derT>(BinaryOperation<EXP>::der);
648  ret[LOG] = reinterpret_cast<derT>(BinaryOperation<LOG>::der);
649  ret[POW] = reinterpret_cast<derT>(BinaryOperation<POW>::der);
650  ret[CONSTPOW] = reinterpret_cast<derT>(BinaryOperation<CONSTPOW>::der);
651
652  ret[SQRT] = reinterpret_cast<derT>(BinaryOperation<SQRT>::der);
653  ret[SIN] = reinterpret_cast<derT>(BinaryOperation<SIN>::der);
654  ret[COS] = reinterpret_cast<derT>(BinaryOperation<COS>::der);
655  ret[TAN] = reinterpret_cast<derT>(BinaryOperation<TAN>::der);
656
657  ret[ASIN] = reinterpret_cast<derT>(BinaryOperation<ASIN>::der);
658  ret[ACOS] = reinterpret_cast<derT>(BinaryOperation<ACOS>::der);
659  ret[ATAN] = reinterpret_cast<derT>(BinaryOperation<ATAN>::der);
660
661  ret[STEP] = reinterpret_cast<derT>(BinaryOperation<STEP>::der);
662  ret[FLOOR] = reinterpret_cast<derT>(BinaryOperation<FLOOR>::der);
663  ret[CEIL] = reinterpret_cast<derT>(BinaryOperation<CEIL>::der);
664
665  ret[EQUALITY] = reinterpret_cast<derT>(BinaryOperation<EQUALITY>::der);
666
667  ret[ERF] = reinterpret_cast<derT>(BinaryOperation<ERF>::der);
668  ret[FMIN] = reinterpret_cast<derT>(BinaryOperation<FMIN>::der);
669  ret[FMAX] = reinterpret_cast<derT>(BinaryOperation<FMAX>::der);
670
671#else // _MSC_VER
672  ret[ADD] = BinaryOperation<ADD>::der;
673  ret[SUB] = BinaryOperation<SUB>::der;
674  ret[MUL] = BinaryOperation<MUL>::der;
675  ret[DIV] = BinaryOperation<DIV>::der;
676   
677  ret[NEG] = BinaryOperation<NEG>::der;
678  ret[EXP] = BinaryOperation<EXP>::der;
679  ret[LOG] = BinaryOperation<LOG>::der;
680  ret[POW] = BinaryOperation<POW>::der;
681  ret[CONSTPOW] = BinaryOperation<CONSTPOW>::der;
682
683  ret[SQRT] = BinaryOperation<SQRT>::der;
684  ret[SIN] = BinaryOperation<SIN>::der;
685  ret[COS] = BinaryOperation<COS>::der;
686  ret[TAN] = BinaryOperation<TAN>::der;
687
688  ret[ASIN] = BinaryOperation<ASIN>::der;
689  ret[ACOS] = BinaryOperation<ACOS>::der;
690  ret[ATAN] = BinaryOperation<ATAN>::der;
691
692  ret[STEP] = BinaryOperation<STEP>::der;
693  ret[FLOOR] = BinaryOperation<FLOOR>::der;
694  ret[CEIL] = BinaryOperation<CEIL>::der;
695
696  ret[EQUALITY] = BinaryOperation<EQUALITY>::der;
697
698  ret[ERF] = BinaryOperation<ERF>::der;
699  ret[FMIN] = BinaryOperation<FMIN>::der;
700  ret[FMAX] = BinaryOperation<FMAX>::der;
701#endif // _MSC_VER
702
703  // Make sure that all functions were specified
704  for(int i=0; i<ret.size(); ++i){
705    casadi_assert(ret[i]!=0);
706  }
707 
708  return ret;
709}
710
711template<typename T>
712std::vector<bool> casadi_math<T>::getF00_is_zero(){
713   // Create return object
714  std::vector<bool> ret(NUM_BUILT_IN_OPS,false);
715 
716  // Specify operations
717  ret[ADD] = BinaryOperation<ADD>::f00_is_zero();
718  ret[SUB] = BinaryOperation<SUB>::f00_is_zero();
719  ret[MUL] = BinaryOperation<MUL>::f00_is_zero();
720  ret[DIV] = BinaryOperation<DIV>::f00_is_zero();
721 
722  ret[NEG] = BinaryOperation<NEG>::f00_is_zero();
723  ret[EXP] = BinaryOperation<EXP>::f00_is_zero();
724  ret[LOG] = BinaryOperation<LOG>::f00_is_zero();
725  ret[POW] = BinaryOperation<POW>::f00_is_zero();
726  ret[CONSTPOW] = BinaryOperation<CONSTPOW>::f00_is_zero();
727
728  ret[SQRT] = BinaryOperation<SQRT>::f00_is_zero();
729  ret[SIN] = BinaryOperation<SIN>::f00_is_zero();
730  ret[COS] = BinaryOperation<COS>::f00_is_zero();
731  ret[TAN] = BinaryOperation<TAN>::f00_is_zero();
732
733  ret[ASIN] = BinaryOperation<ASIN>::f00_is_zero();
734  ret[ACOS] = BinaryOperation<ACOS>::f00_is_zero();
735  ret[ATAN] = BinaryOperation<ATAN>::f00_is_zero();
736
737  ret[STEP] = BinaryOperation<STEP>::f00_is_zero();
738  ret[FLOOR] = BinaryOperation<FLOOR>::f00_is_zero();
739  ret[CEIL] = BinaryOperation<CEIL>::f00_is_zero();
740
741  ret[EQUALITY] = BinaryOperation<EQUALITY>::f00_is_zero();
742
743  ret[ERF] = BinaryOperation<ERF>::f00_is_zero();
744  ret[FMIN] = BinaryOperation<FMIN>::f00_is_zero();
745  ret[FMAX] = BinaryOperation<FMAX>::f00_is_zero();
746
747  return ret;
748}
749
750template<typename T>
751std::vector<bool> casadi_math<T>::getF0x_is_zero(){
752   // Create return object
753  std::vector<bool> ret(NUM_BUILT_IN_OPS,false);
754 
755  // Specify operations
756  ret[ADD] = BinaryOperation<ADD>::f0x_is_zero();
757  ret[SUB] = BinaryOperation<SUB>::f0x_is_zero();
758  ret[MUL] = BinaryOperation<MUL>::f0x_is_zero();
759  ret[DIV] = BinaryOperation<DIV>::f0x_is_zero();
760 
761  ret[NEG] = BinaryOperation<NEG>::f0x_is_zero();
762  ret[EXP] = BinaryOperation<EXP>::f0x_is_zero();
763  ret[LOG] = BinaryOperation<LOG>::f0x_is_zero();
764  ret[POW] = BinaryOperation<POW>::f0x_is_zero();
765  ret[CONSTPOW] = BinaryOperation<CONSTPOW>::f0x_is_zero();
766
767  ret[SQRT] = BinaryOperation<SQRT>::f0x_is_zero();
768  ret[SIN] = BinaryOperation<SIN>::f0x_is_zero();
769  ret[COS] = BinaryOperation<COS>::f0x_is_zero();
770  ret[TAN] = BinaryOperation<TAN>::f0x_is_zero();
771
772  ret[ASIN] = BinaryOperation<ASIN>::f0x_is_zero();
773  ret[ACOS] = BinaryOperation<ACOS>::f0x_is_zero();
774  ret[ATAN] = BinaryOperation<ATAN>::f0x_is_zero();
775
776  ret[STEP] = BinaryOperation<STEP>::f0x_is_zero();
777  ret[FLOOR] = BinaryOperation<FLOOR>::f0x_is_zero();
778  ret[CEIL] = BinaryOperation<CEIL>::f0x_is_zero();
779
780  ret[EQUALITY] = BinaryOperation<EQUALITY>::f0x_is_zero();
781
782  ret[ERF] = BinaryOperation<ERF>::f0x_is_zero();
783  ret[FMIN] = BinaryOperation<FMIN>::f0x_is_zero();
784  ret[FMAX] = BinaryOperation<FMAX>::f0x_is_zero();
785
786  return ret;
787}
788
789template<typename T>
790std::vector<bool> casadi_math<T>::getFx0_is_zero(){
791   // Create return object
792  std::vector<bool> ret(NUM_BUILT_IN_OPS,false);
793 
794  // Specify operations
795  ret[ADD] = BinaryOperation<ADD>::fx0_is_zero();
796  ret[SUB] = BinaryOperation<SUB>::fx0_is_zero();
797  ret[MUL] = BinaryOperation<MUL>::fx0_is_zero();
798  ret[DIV] = BinaryOperation<DIV>::fx0_is_zero();
799 
800  ret[NEG] = BinaryOperation<NEG>::fx0_is_zero();
801  ret[EXP] = BinaryOperation<EXP>::fx0_is_zero();
802  ret[LOG] = BinaryOperation<LOG>::fx0_is_zero();
803  ret[POW] = BinaryOperation<POW>::fx0_is_zero();
804  ret[CONSTPOW] = BinaryOperation<CONSTPOW>::fx0_is_zero();
805
806  ret[SQRT] = BinaryOperation<SQRT>::fx0_is_zero();
807  ret[SIN] = BinaryOperation<SIN>::fx0_is_zero();
808  ret[COS] = BinaryOperation<COS>::fx0_is_zero();
809  ret[TAN] = BinaryOperation<TAN>::fx0_is_zero();
810
811  ret[ASIN] = BinaryOperation<ASIN>::fx0_is_zero();
812  ret[ACOS] = BinaryOperation<ACOS>::fx0_is_zero();
813  ret[ATAN] = BinaryOperation<ATAN>::fx0_is_zero();
814
815  ret[STEP] = BinaryOperation<STEP>::fx0_is_zero();
816  ret[FLOOR] = BinaryOperation<FLOOR>::fx0_is_zero();
817  ret[CEIL] = BinaryOperation<CEIL>::fx0_is_zero();
818
819  ret[EQUALITY] = BinaryOperation<EQUALITY>::fx0_is_zero();
820
821  ret[ERF] = BinaryOperation<ERF>::fx0_is_zero();
822  ret[FMIN] = BinaryOperation<FMIN>::fx0_is_zero();
823  ret[FMAX] = BinaryOperation<FMAX>::fx0_is_zero();
824
825  return ret;
826}
827
828
829} // namespace CasADi
830
831#endif //CASADI_MATH_HPP
Note: See TracBrowser for help on using the browser.