26 #ifndef CASADI_CALCULUS_HPP
27 #define CASADI_CALCULUS_HPP
34 #include "casadi_common.hpp"
44 const double pi = M_PI;
46 const double pi = 3.14159265358979323846;
50 const double inf = std::numeric_limits<double>::infinity();
53 const double nan = std::numeric_limits<double>::quiet_NaN();
56 const double eps = std::numeric_limits<double>::epsilon();
65 OP_ADD, OP_SUB, OP_MUL, OP_DIV,
66 OP_NEG, OP_EXP, OP_LOG, OP_POW, OP_CONSTPOW,
67 OP_SQRT, OP_SQ, OP_TWICE,
68 OP_SIN, OP_COS, OP_TAN,
69 OP_ASIN, OP_ACOS, OP_ATAN,
70 OP_LT, OP_LE, OP_EQ, OP_NE, OP_NOT, OP_AND, OP_OR,
71 OP_FLOOR, OP_CEIL, OP_FMOD, OP_FABS, OP_SIGN, OP_COPYSIGN, OP_IF_ELSE_ZERO,
72 OP_ERF, OP_FMIN, OP_FMAX,
74 OP_SINH, OP_COSH, OP_TANH,
75 OP_ASINH, OP_ACOSH, OP_ATANH,
154 OP_GETNONZEROS_PARAM,
160 OP_ADDNONZEROS_PARAM,
166 OP_SETNONZEROS_PARAM,
178 OP_NORM2, OP_NORM1, OP_NORMINF, OP_NORMF,
213 #define NUM_BUILT_IN_OPS (OP_REMAINDER+1)
243 using std::remainder;
264 inline double sign(
double x) {
return x<0 ? -1 : x>0 ? 1 : x;}
271 inline double simplify(
double x) {
return x;}
272 inline double constpow(
double x,
double y) {
return pow(x, y);}
273 inline double printme(
double x,
double y) {
274 std::ios::fmtflags f(
uout().flags());
275 uout() <<
"|> " << y <<
" : ";
276 uout() << std::setprecision(std::numeric_limits<double>::digits10 + 1) << std::scientific;
277 uout() << x << std::endl;
281 inline bool is_equal(
double x,
double y, casadi_int depth=0) {
return x==y;}
285 inline casadi_int casadi_max(casadi_int x, casadi_int y) {
return std::max(x, y);}
286 inline casadi_int casadi_min(casadi_int x, casadi_int y) {
return std::min(x, y);}
289 inline double if_else_zero(
double x,
double y) {
return x==0 ? 0 : y;}
290 inline double if_else(
double x,
double y,
double z) {
return x==0 ? z : y;}
294 inline double erfinv(
double x)
throw() {
297 return x==1 ? inf : nan;
299 return x==-1 ? -inf : nan;
301 double z = sqrt(-log((1.0+x)/2.0));
302 return -(((1.641345311*z+3.429567803)*z-1.624906493)*z-1.970840454)/
303 ((1.637067800*z+3.543889200)*z+1.0);
308 y = x*(((-0.140543331*z+0.914624893)*z-1.645349621)*z+0.886226899)/
309 ((((-0.329097515*z+0.012229801)*z+1.442710462)*z-2.118377725)*z+1.0);
311 double z = sqrt(-log((1.0-x)/2.0));
312 y = (((1.641345311*z+3.429567803)*z-1.624906493)*z-1.970840454)/
313 ((1.637067800*z+3.543889200)*z+1.0);
317 y = y - (erf(y) - x) / (2.0/sqrt(pi) * exp(-y*y));
318 y = y - (erf(y) - x) / (2.0/sqrt(pi) * exp(-y*y));
326 T twice(
const T& x) {
335 template<casadi_
int I>
336 struct UnaryOperation {
338 template<
typename T>
static inline void fcn(
const T& x, T& f);
341 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d);
344 template<casadi_
int I>
345 struct BinaryOperation {
347 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) {
348 UnaryOperation<I>::fcn(x, f);}
351 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
352 UnaryOperation<I>::der(x, f, d); d[1]=0; }
355 template<casadi_
int I>
356 struct BinaryOperationE {
358 template<
typename T>
static inline T fcn(
const T& x,
const T& y) {
360 BinaryOperation<I>::fcn(x, y, ret);
366 template<casadi_
int I>
367 struct DerBinaryOperation {
369 template<
typename T>
static inline void derf(
const T& x,
const T& y, T& f, T* d) {
377 BinaryOperation<I>::fcn(x, y, tmp);
380 BinaryOperation<I>::der(x, y, tmp, d);
388 template<casadi_
int I>
389 struct BinaryOperationSS {
391 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f, casadi_int n) {
392 BinaryOperation<I>::fcn(x, y, f);
396 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d,
398 BinaryOperation<I>::der(x, y, f, d);
404 template<casadi_
int I>
405 struct BinaryOperationVV {
407 template<
typename T>
static inline void fcn(
const T* x,
const T* y, T* f, casadi_int n) {
408 for (casadi_int i=0; i<n; ++i) {
409 BinaryOperation<I>::fcn(*x++, *y++, *f++);
414 template<
typename T>
static inline void der(
const T* x,
const T* y,
415 const T* f, T* d, casadi_int n) {
416 for (casadi_int i=0; i<n; ++i, d+=2) {
417 BinaryOperation<I>::der(*x++, *y++, *f++, d);
423 template<casadi_
int I>
424 struct BinaryOperationVS {
426 template<
typename T>
static inline void fcn(
const T* x,
const T& y, T* f, casadi_int n) {
427 for (casadi_int i=0; i<n; ++i) {
428 BinaryOperation<I>::fcn(*x++, y, *f++);
433 template<
typename T>
static inline void der(
const T* x,
const T& y,
434 const T* f, T* d, casadi_int n) {
435 for (casadi_int i=0; i<n; ++i, d+=2) {
436 BinaryOperation<I>::der(*x++, y, *f++, d);
442 template<casadi_
int I>
443 struct BinaryOperationSV {
445 template<
typename T>
static inline void fcn(
const T& x,
const T* y, T* f, casadi_int n) {
446 for (casadi_int i=0; i<n; ++i) {
447 BinaryOperation<I>::fcn(x, *y++, *f++);
452 template<
typename T>
static inline void der(
const T& x,
const T* y,
453 const T* f, T* d, casadi_int n) {
454 for (casadi_int i=0; i<n; ++i, d+=2) {
455 BinaryOperation<I>::der(x, *y++, *f++, d);
462 template<casadi_
int I>
struct SmoothChecker {
static const bool check=
true;};
463 template<>
struct SmoothChecker<OP_LT>{
static const bool check=
false;};
464 template<>
struct SmoothChecker<OP_LE>{
static const bool check=
false;};
465 template<>
struct SmoothChecker<OP_FLOOR>{
static const bool check=
false;};
466 template<>
struct SmoothChecker<OP_CEIL>{
static const bool check=
false;};
467 template<>
struct SmoothChecker<OP_FMOD>{
static const bool check=
false;};
468 template<>
struct SmoothChecker<OP_REMAINDER>{
static const bool check=
false;};
469 template<>
struct SmoothChecker<OP_EQ>{
static const bool check=
false;};
470 template<>
struct SmoothChecker<OP_NE>{
static const bool check=
false;};
471 template<>
struct SmoothChecker<OP_SIGN>{
static const bool check=
false;};
472 template<>
struct SmoothChecker<OP_COPYSIGN>{
static const bool check=
false;};
473 template<>
struct SmoothChecker<OP_NOT>{
static const bool check=
false;};
474 template<>
struct SmoothChecker<OP_AND>{
static const bool check=
false;};
475 template<>
struct SmoothChecker<OP_OR>{
static const bool check=
false;};
476 template<>
struct SmoothChecker<OP_IF_ELSE_ZERO>{
static const bool check=
false;};
481 template<casadi_
int I>
struct F0XChecker {
static const bool check=
false;};
482 template<>
struct F0XChecker<OP_ASSIGN>{
static const bool check=
true;};
483 template<>
struct F0XChecker<OP_MUL>{
static const bool check=
true;};
484 template<>
struct F0XChecker<OP_DIV>{
static const bool check=
true;};
485 template<>
struct F0XChecker<OP_NEG>{
static const bool check=
true;};
486 template<>
struct F0XChecker<OP_POW>{
static const bool check=
true;};
487 template<>
struct F0XChecker<OP_CONSTPOW>{
static const bool check=
true;};
488 template<>
struct F0XChecker<OP_SQRT>{
static const bool check=
true;};
489 template<>
struct F0XChecker<OP_SQ>{
static const bool check=
true;};
490 template<>
struct F0XChecker<OP_TWICE>{
static const bool check=
true;};
491 template<>
struct F0XChecker<OP_SIN>{
static const bool check=
true;};
492 template<>
struct F0XChecker<OP_TAN>{
static const bool check=
true;};
493 template<>
struct F0XChecker<OP_ATAN>{
static const bool check=
true;};
494 template<>
struct F0XChecker<OP_ASIN>{
static const bool check=
true;};
495 template<>
struct F0XChecker<OP_FLOOR>{
static const bool check=
true;};
496 template<>
struct F0XChecker<OP_CEIL>{
static const bool check=
true;};
497 template<>
struct F0XChecker<OP_FMOD>{
static const bool check=
true;};
498 template<>
struct F0XChecker<OP_REMAINDER>{
static const bool check=
true;};
499 template<>
struct F0XChecker<OP_FABS>{
static const bool check=
true;};
500 template<>
struct F0XChecker<OP_SIGN>{
static const bool check=
true;};
501 template<>
struct F0XChecker<OP_COPYSIGN>{
static const bool check=
true;};
502 template<>
struct F0XChecker<OP_ERF>{
static const bool check=
true;};
503 template<>
struct F0XChecker<OP_SINH>{
static const bool check=
true;};
504 template<>
struct F0XChecker<OP_TANH>{
static const bool check=
true;};
505 template<>
struct F0XChecker<OP_ASINH>{
static const bool check=
true;};
506 template<>
struct F0XChecker<OP_ATANH>{
static const bool check=
true;};
507 template<>
struct F0XChecker<OP_ERFINV>{
static const bool check=
true;};
508 template<>
struct F0XChecker<OP_AND>{
static const bool check=
true;};
509 template<>
struct F0XChecker<OP_IF_ELSE_ZERO>{
static const bool check=
true;};
510 template<>
struct F0XChecker<OP_LOG1P>{
static const bool check=
true;};
511 template<>
struct F0XChecker<OP_EXPM1>{
static const bool check=
true;};
516 template<casadi_
int I>
struct FX0Checker {
static const bool check=
false;};
517 template<>
struct FX0Checker<OP_MUL>{
static const bool check=
true;};
518 template<>
struct FX0Checker<OP_AND>{
static const bool check=
true;};
519 template<>
struct FX0Checker<OP_IF_ELSE_ZERO>{
static const bool check=
true;};
524 template<casadi_
int I>
struct F00Checker {
525 static const bool check=F0XChecker<I>::check || FX0Checker<I>::check;
527 template<>
struct F00Checker<OP_ADD>{
static const bool check=
true;};
528 template<>
struct F00Checker<OP_SUB>{
static const bool check=
true;};
529 template<>
struct F00Checker<OP_FMIN>{
static const bool check=
true;};
530 template<>
struct F00Checker<OP_FMAX>{
static const bool check=
true;};
531 template<>
struct F00Checker<OP_AND>{
static const bool check=
true;};
532 template<>
struct F00Checker<OP_OR>{
static const bool check=
true;};
533 template<>
struct F00Checker<OP_COPYSIGN>{
static const bool check=
true;};
534 template<>
struct F00Checker<OP_LT>{
static const bool check=
true;};
535 template<>
struct F00Checker<OP_HYPOT>{
static const bool check=
true;};
540 template<casadi_
int I>
struct CommChecker {
static const bool check=
false;};
541 template<>
struct CommChecker<OP_ADD>{
static const bool check=
true;};
542 template<>
struct CommChecker<OP_MUL>{
static const bool check=
true;};
543 template<>
struct CommChecker<OP_EQ>{
static const bool check=
true;};
544 template<>
struct CommChecker<OP_NE>{
static const bool check=
true;};
545 template<>
struct CommChecker<OP_AND>{
static const bool check=
true;};
546 template<>
struct CommChecker<OP_OR>{
static const bool check=
true;};
547 template<>
struct CommChecker<OP_HYPOT>{
static const bool check=
true;};
552 template<casadi_
int I>
struct NonnegativeChecker {
static const bool check=
false;};
553 template<>
struct NonnegativeChecker<OP_SQRT>{
static const bool check=
true;};
554 template<>
struct NonnegativeChecker<OP_SQ>{
static const bool check=
true;};
555 template<>
struct NonnegativeChecker<OP_EXP>{
static const bool check=
true;};
556 template<>
struct NonnegativeChecker<OP_LT>{
static const bool check=
true;};
557 template<>
struct NonnegativeChecker<OP_LE>{
static const bool check=
true;};
558 template<>
struct NonnegativeChecker<OP_EQ>{
static const bool check=
true;};
559 template<>
struct NonnegativeChecker<OP_NE>{
static const bool check=
true;};
560 template<>
struct NonnegativeChecker<OP_NOT>{
static const bool check=
true;};
561 template<>
struct NonnegativeChecker<OP_AND>{
static const bool check=
true;};
562 template<>
struct NonnegativeChecker<OP_OR>{
static const bool check=
true;};
563 template<>
struct NonnegativeChecker<OP_HYPOT>{
static const bool check=
true;};
568 template<casadi_
int I>
struct NargChecker {
static const casadi_int check=1;};
569 template<>
struct NargChecker<OP_ADD>{
static const casadi_int check=2;};
570 template<>
struct NargChecker<OP_SUB>{
static const casadi_int check=2;};
571 template<>
struct NargChecker<OP_MUL>{
static const casadi_int check=2;};
572 template<>
struct NargChecker<OP_DIV>{
static const casadi_int check=2;};
573 template<>
struct NargChecker<OP_POW>{
static const casadi_int check=2;};
574 template<>
struct NargChecker<OP_CONSTPOW>{
static const casadi_int check=2;};
575 template<>
struct NargChecker<OP_EQ>{
static const casadi_int check=2;};
576 template<>
struct NargChecker<OP_NE>{
static const casadi_int check=2;};
577 template<>
struct NargChecker<OP_AND>{
static const casadi_int check=2;};
578 template<>
struct NargChecker<OP_OR>{
static const casadi_int check=2;};
579 template<>
struct NargChecker<OP_FMIN>{
static const casadi_int check=2;};
580 template<>
struct NargChecker<OP_FMAX>{
static const casadi_int check=2;};
581 template<>
struct NargChecker<OP_PRINTME>{
static const casadi_int check=2;};
582 template<>
struct NargChecker<OP_ATAN2>{
static const casadi_int check=2;};
583 template<>
struct NargChecker<OP_IF_ELSE_ZERO>{
static const casadi_int check=2;};
584 template<>
struct NargChecker<OP_FMOD>{
static const casadi_int check=2;};
585 template<>
struct NargChecker<OP_REMAINDER>{
static const casadi_int check=2;};
586 template<>
struct NargChecker<OP_COPYSIGN>{
static const casadi_int check=2;};
587 template<>
struct NargChecker<OP_CONST>{
static const casadi_int check=0;};
588 template<>
struct NargChecker<OP_PARAMETER>{
static const casadi_int check=0;};
589 template<>
struct NargChecker<OP_INPUT>{
static const casadi_int check=0;};
590 template<>
struct NargChecker<OP_HYPOT>{
static const casadi_int check=2;};
595 struct UnaryOperation<OP_ASSIGN>{
597 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = x;}
598 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0] = 1; }
603 struct BinaryOperation<OP_ADD>{
605 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = x+y;}
606 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
612 struct BinaryOperation<OP_SUB>{
614 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = x-y;}
615 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
621 struct BinaryOperation<OP_MUL>{
623 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = x*y;}
624 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
630 struct BinaryOperation<OP_DIV>{
632 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = x/y;}
633 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
634 d[0]=1/y; d[1]=-f/y;}
639 struct UnaryOperation<OP_NEG>{
641 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = -x;}
642 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0]=-1;}
647 struct UnaryOperation<OP_EXP>{
649 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = exp(x);}
650 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0]=f;}
655 struct UnaryOperation<OP_LOG>{
657 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = log(x);}
658 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0]=1/x;}
663 struct BinaryOperation<OP_POW>{
665 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = pow(x, y);}
667 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
668 d[0]=y*pow(x, y-1); d[1]=log(x)*f;}
673 struct BinaryOperation<OP_CONSTPOW>{
675 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = pow(x, y);}
676 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
677 d[0]=y*pow(x, y-1); d[1]=0;}
682 struct UnaryOperation<OP_SQRT>{
684 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = sqrt(x);}
685 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0]=1/(twice(f));}
690 struct UnaryOperation<OP_SQ>{
692 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = sq(x);}
693 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0]=twice(x);}
698 struct UnaryOperation<OP_TWICE>{
699 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = 2.*x;}
700 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0] = 2; }
705 struct UnaryOperation<OP_SIN>{
707 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = sin(x);}
708 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0]=cos(x);}
713 struct UnaryOperation<OP_COS>{
715 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = cos(x);}
716 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0]=-sin(x);}
721 struct UnaryOperation<OP_TAN>{
723 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = tan(x);}
724 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d)
725 { d[0] = 1/sq(cos(x));}
730 struct UnaryOperation<OP_ASIN>{
732 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = asin(x);}
733 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0]=1/sqrt(1-x*x);}
738 struct UnaryOperation<OP_ACOS>{
740 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = acos(x);}
741 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d)
742 { d[0]=-1/sqrt(1-x*x);}
747 struct UnaryOperation<OP_ATAN>{
749 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = atan(x);}
750 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0] = 1/(1+x*x);}
755 struct BinaryOperation<OP_LT>{
757 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = x < y;}
758 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
764 struct BinaryOperation<OP_LE>{
766 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = x <= y;}
767 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
773 struct UnaryOperation<OP_FLOOR>{
775 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = floor(x);}
776 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0] = 0;}
781 struct UnaryOperation<OP_CEIL>{
783 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = ceil(x);}
784 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0] = 0;}
789 struct BinaryOperation<OP_FMOD>{
790 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = fmod(x, y);}
791 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
792 d[0]=1; d[1]=(f-x)/y;}
797 struct BinaryOperation<OP_REMAINDER>{
798 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) {
799 f = remainder(x, y);}
800 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
801 d[0]=1; d[1]=(f-x)/y;}
806 struct BinaryOperation<OP_EQ>{
807 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = x==y;}
808 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
814 struct BinaryOperation<OP_NE>{
815 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = x!=y;}
816 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
822 struct UnaryOperation<OP_NOT>{
824 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = !x;}
825 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0] = 0;}
830 struct BinaryOperation<OP_AND>{
831 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = x && y;}
832 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
838 struct BinaryOperation<OP_OR>{
839 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = x || y;}
840 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
846 struct UnaryOperation<OP_ERF>{
847 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = erf(x);}
848 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) {
849 d[0] = (2/sqrt(pi))*exp(-x*x);}
854 struct UnaryOperation<OP_FABS>{
855 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = fabs(x);}
856 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) {
862 struct UnaryOperation<OP_SIGN>{
863 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = sign(x);}
864 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0]=0;}
869 struct BinaryOperation<OP_COPYSIGN>{
870 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = copysign(x, y);}
871 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
872 T e = 1; d[0]=copysign(e, y); d[1]=0;}
877 struct BinaryOperation<OP_FMIN>{
878 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = fmin(x, y);}
879 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
888 struct BinaryOperation<OP_FMAX>{
889 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = fmax(x, y);}
890 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
899 struct UnaryOperation<OP_INV>{
900 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = 1./x;}
901 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0] = -f*f; }
906 struct UnaryOperation<OP_SINH>{
907 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = sinh(x);}
908 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0] = cosh(x); }
913 struct UnaryOperation<OP_COSH>{
914 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = cosh(x);}
915 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0] = sinh(x); }
920 struct UnaryOperation<OP_TANH>{
921 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = tanh(x);}
922 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0] = 1-f*f; }
927 struct UnaryOperation<OP_ASINH>{
928 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = asinh(x);}
929 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) {
930 d[0] = 1/sqrt(1+x*x); }
935 struct UnaryOperation<OP_ACOSH>{
936 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = acosh(x);}
937 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) {
938 d[0] = 1/sqrt(x-1)/sqrt(x+1); }
943 struct UnaryOperation<OP_ATANH>{
944 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = atanh(x);}
945 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) { d[0] = 1/(1-x*x); }
950 struct UnaryOperation<OP_ERFINV>{
951 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = erfinv(x);}
952 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) {
953 d[0] = (sqrt(pi)/2)*exp(f*f); }
958 struct UnaryOperation<OP_LOG1P>{
959 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = log1p(x);}
960 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) {
966 struct UnaryOperation<OP_EXPM1>{
967 template<
typename T>
static inline void fcn(
const T& x, T& f) { f = expm1(x);}
968 template<
typename T>
static inline void der(
const T& x,
const T& f, T* d) {
974 struct BinaryOperation<OP_PRINTME>{
975 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) {f = printme(x, y); }
976 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
982 struct BinaryOperation<OP_ATAN2>{
984 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = atan2(x, y);}
985 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
986 T t = x*x+y*y; d[0]=y/t; d[1]=-x/t;}
991 struct BinaryOperation<OP_IF_ELSE_ZERO>{
993 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) {
994 f = if_else_zero(x, y);}
995 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
1001 struct BinaryOperation<OP_LIFT>{
1002 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = x;}
1003 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
1004 d[0] = 1; d[1] = 0; }
1009 struct BinaryOperation<OP_HYPOT>{
1010 template<
typename T>
static inline void fcn(
const T& x,
const T& y, T& f) { f = hypot(x, y);}
1011 template<
typename T>
static inline void der(
const T& x,
const T& y,
const T& f, T* d) {
1012 d[0] = x/f; d[1] = y/f; }
1015 template<
template<casadi_
int>
class F,
typename T>
1016 T operation_getter(casadi_int op) {
1017 switch (
static_cast<Operation
>(op)) {
1018 case OP_ASSIGN:
return F<OP_ASSIGN>::check;
1019 case OP_ADD:
return F<OP_ADD>::check;
1020 case OP_SUB:
return F<OP_SUB>::check;
1021 case OP_MUL:
return F<OP_MUL>::check;
1022 case OP_DIV:
return F<OP_DIV>::check;
1023 case OP_NEG:
return F<OP_NEG>::check;
1024 case OP_EXP:
return F<OP_EXP>::check;
1025 case OP_LOG:
return F<OP_LOG>::check;
1026 case OP_POW:
return F<OP_POW>::check;
1027 case OP_CONSTPOW:
return F<OP_CONSTPOW>::check;
1028 case OP_SQRT:
return F<OP_SQRT>::check;
1029 case OP_SQ:
return F<OP_SQ>::check;
1030 case OP_TWICE:
return F<OP_TWICE>::check;
1031 case OP_SIN:
return F<OP_SIN>::check;
1032 case OP_COS:
return F<OP_COS>::check;
1033 case OP_TAN:
return F<OP_TAN>::check;
1034 case OP_ASIN:
return F<OP_ASIN>::check;
1035 case OP_ACOS:
return F<OP_ACOS>::check;
1036 case OP_ATAN:
return F<OP_ATAN>::check;
1037 case OP_LT:
return F<OP_LT>::check;
1038 case OP_LE:
return F<OP_LE>::check;
1039 case OP_EQ:
return F<OP_EQ>::check;
1040 case OP_NE:
return F<OP_NE>::check;
1041 case OP_NOT:
return F<OP_NOT>::check;
1042 case OP_AND:
return F<OP_AND>::check;
1043 case OP_OR:
return F<OP_OR>::check;
1044 case OP_FLOOR:
return F<OP_FLOOR>::check;
1045 case OP_CEIL:
return F<OP_CEIL>::check;
1046 case OP_FMOD:
return F<OP_FMOD>::check;
1047 case OP_REMAINDER:
return F<OP_REMAINDER>::check;
1048 case OP_FABS:
return F<OP_FABS>::check;
1049 case OP_SIGN:
return F<OP_SIGN>::check;
1050 case OP_COPYSIGN:
return F<OP_COPYSIGN>::check;
1051 case OP_IF_ELSE_ZERO:
return F<OP_IF_ELSE_ZERO>::check;
1052 case OP_ERF:
return F<OP_ERF>::check;
1053 case OP_FMIN:
return F<OP_FMIN>::check;
1054 case OP_FMAX:
return F<OP_FMAX>::check;
1055 case OP_INV:
return F<OP_INV>::check;
1056 case OP_SINH:
return F<OP_SINH>::check;
1057 case OP_COSH:
return F<OP_COSH>::check;
1058 case OP_TANH:
return F<OP_TANH>::check;
1059 case OP_ASINH:
return F<OP_ASINH>::check;
1060 case OP_ACOSH:
return F<OP_ACOSH>::check;
1061 case OP_ATANH:
return F<OP_ATANH>::check;
1062 case OP_ATAN2:
return F<OP_ATAN2>::check;
1063 case OP_CONST:
return F<OP_CONST>::check;
1064 case OP_INPUT:
return F<OP_INPUT>::check;
1065 case OP_OUTPUT:
return F<OP_OUTPUT>::check;
1066 case OP_PARAMETER:
return F<OP_PARAMETER>::check;
1067 case OP_CALL:
return F<OP_CALL>::check;
1068 case OP_FIND:
return F<OP_FIND>::check;
1069 case OP_LOW:
return F<OP_LOW>::check;
1070 case OP_MAP:
return F<OP_MAP>::check;
1071 case OP_MTIMES:
return F<OP_MTIMES>::check;
1072 case OP_SOLVE:
return F<OP_SOLVE>::check;
1073 case OP_TRANSPOSE:
return F<OP_TRANSPOSE>::check;
1074 case OP_DETERMINANT:
return F<OP_DETERMINANT>::check;
1075 case OP_INVERSE:
return F<OP_INVERSE>::check;
1076 case OP_DOT:
return F<OP_DOT>::check;
1077 case OP_BILIN:
return F<OP_BILIN>::check;
1078 case OP_RANK1:
return F<OP_RANK1>::check;
1079 case OP_HORZCAT:
return F<OP_HORZCAT>::check;
1080 case OP_VERTCAT:
return F<OP_VERTCAT>::check;
1081 case OP_DIAGCAT:
return F<OP_DIAGCAT>::check;
1082 case OP_HORZSPLIT:
return F<OP_HORZSPLIT>::check;
1083 case OP_VERTSPLIT:
return F<OP_VERTSPLIT>::check;
1084 case OP_DIAGSPLIT:
return F<OP_DIAGSPLIT>::check;
1085 case OP_RESHAPE:
return F<OP_RESHAPE>::check;
1086 case OP_SPARSITY_CAST:
return F<OP_SPARSITY_CAST>::check;
1087 case OP_SUBREF:
return F<OP_SUBREF>::check;
1088 case OP_SUBASSIGN:
return F<OP_SUBASSIGN>::check;
1089 case OP_GETNONZEROS:
return F<OP_GETNONZEROS>::check;
1090 case OP_GETNONZEROS_PARAM:
return F<OP_GETNONZEROS_PARAM>::check;
1091 case OP_ADDNONZEROS:
return F<OP_ADDNONZEROS>::check;
1092 case OP_ADDNONZEROS_PARAM:
return F<OP_ADDNONZEROS>::check;
1093 case OP_SETNONZEROS:
return F<OP_SETNONZEROS>::check;
1094 case OP_SETNONZEROS_PARAM:
return F<OP_SETNONZEROS>::check;
1095 case OP_PROJECT:
return F<OP_PROJECT>::check;
1096 case OP_ASSERTION:
return F<OP_ASSERTION>::check;
1097 case OP_MONITOR:
return F<OP_MONITOR>::check;
1098 case OP_NORM2:
return F<OP_NORM2>::check;
1099 case OP_NORM1:
return F<OP_NORM1>::check;
1100 case OP_NORMINF:
return F<OP_NORMINF>::check;
1101 case OP_NORMF:
return F<OP_NORMF>::check;
1102 case OP_MMIN:
return F<OP_MMIN>::check;
1103 case OP_MMAX:
return F<OP_MMAX>::check;
1104 case OP_HORZREPMAT:
return F<OP_HORZREPMAT>::check;
1105 case OP_HORZREPSUM:
return F<OP_HORZREPSUM>::check;
1106 case OP_ERFINV:
return F<OP_ERFINV>::check;
1107 case OP_PRINTME:
return F<OP_PRINTME>::check;
1108 case OP_LIFT:
return F<OP_LIFT>::check;
1109 case OP_EINSTEIN:
return F<OP_EINSTEIN>::check;
1110 case OP_BSPLINE:
return F<OP_BSPLINE>::check;
1111 case OP_CONVEXIFY:
return F<OP_CONVEXIFY>::check;
1112 case OP_LOG1P:
return F<OP_LOG1P>::check;
1113 case OP_EXPM1:
return F<OP_EXPM1>::check;
1114 case OP_HYPOT:
return F<OP_HYPOT>::check;
1115 case OP_LOGSUMEXP:
return F<OP_LOGSUMEXP>::check;
1120 template<
template<casadi_
int>
class F>
1121 bool operation_checker(casadi_int op) {
1122 return operation_getter<F, bool>(op);
1126 template<
typename T>
1127 struct casadi_math {
1132 static inline void fun(
unsigned char op,
const T& x,
const T& y, T& f);
1137 static inline void fun(
unsigned char op,
const T* x,
const T* y, T* f, casadi_int n);
1142 static inline void fun(
unsigned char op,
const T* x,
const T& y, T* f, casadi_int n);
1147 static inline void fun(
unsigned char op,
const T& x,
const T* y, T* f, casadi_int n);
1152 static inline void der(
unsigned char op,
const T& x,
const T& y,
const T& f, T* d);
1157 static inline void derF(
unsigned char op,
const T& x,
const T& y, T& f, T* d);
1162 static inline bool is_binary(
unsigned char op);
1167 static inline bool is_unary(
unsigned char op);
1172 static inline casadi_int ndeps(
unsigned char op);
1177 static inline std::string print(
unsigned char op,
const std::string& x,
1178 const std::string& y);
1179 static inline std::string print(
unsigned char op,
const std::string& x);
1180 static inline std::string name(
unsigned char op);
1181 static inline std::string pre(
unsigned char op);
1182 static inline std::string sep(
unsigned char op);
1183 static inline std::string post(
unsigned char op);
1188 struct casadi_math<casadi_int>{
1193 static inline void fun(
unsigned char op,
const casadi_int& x,
1194 const casadi_int& y, casadi_int& f) {
1196 casadi_math<double>::fun(op,
static_cast<double>(x),
static_cast<double>(y), ff);
1197 f =
static_cast<casadi_int
>(ff);
1200 static inline void fun(
unsigned char op,
const casadi_int* x,
const casadi_int* y,
1201 casadi_int* f, casadi_int n) {
1202 for (casadi_int i=0; i<n; ++i) {
1204 casadi_math<double>::fun(op,
static_cast<double>(*x++),
static_cast<double>(*y++), ff);
1205 *f++ =
static_cast<casadi_int
>(ff);
1209 static inline void fun(
unsigned char op,
const casadi_int* x,
const casadi_int& y,
1210 casadi_int* f, casadi_int n) {
1211 for (casadi_int i=0; i<n; ++i) {
1213 casadi_math<double>::fun(op,
static_cast<double>(*x++),
static_cast<double>(y), ff);
1214 *f++ =
static_cast<casadi_int
>(ff);
1218 static inline void fun(
unsigned char op,
const casadi_int& x,
const casadi_int* y,
1219 casadi_int* f, casadi_int n) {
1220 for (casadi_int i=0; i<n; ++i) {
1222 casadi_math<double>::fun(op,
static_cast<double>(x),
static_cast<double>(*y++), ff);
1223 *f++ =
static_cast<casadi_int
>(ff);
1230 static inline void der(
unsigned char op,
const casadi_int& x,
const casadi_int& y,
1231 const casadi_int& f, casadi_int* d) {
1232 double d_real[2] = {
static_cast<double>(d[0]),
static_cast<double>(d[1])};
1233 casadi_math<double>::der(op,
static_cast<double>(x),
static_cast<double>(y),
1234 static_cast<double>(f), d_real);
1235 d[0] =
static_cast<casadi_int
>(d_real[0]);
1236 d[1] =
static_cast<casadi_int
>(d_real[1]);
1242 static inline void derF(
unsigned char op,
const casadi_int& x,
const casadi_int& y,
1243 casadi_int& f, casadi_int* d) {
1244 double d_real[2] = {
static_cast<double>(d[0]),
static_cast<double>(d[1])};
1245 double f_real =
static_cast<double>(f);
1246 casadi_math<double>::derF(op,
static_cast<double>(x),
static_cast<double>(y), f_real, d_real);
1247 f =
static_cast<casadi_int
>(f_real);
1248 d[0] =
static_cast<casadi_int
>(d_real[0]);
1249 d[1] =
static_cast<casadi_int
>(d_real[1]);
1255 static inline casadi_int ndeps(
unsigned char op) {
1256 return casadi_math<double>::ndeps(op);
1262 static inline std::string print(
unsigned char op,
const std::string& x,
1263 const std::string& y) {
1264 return casadi_math<double>::print(op, x, y);
1266 static inline std::string print(
unsigned char op,
const std::string& x) {
1267 return casadi_math<double>::print(op, x);
1269 static inline std::string pre(
unsigned char op) {
1270 return casadi_math<double>::pre(op);
1272 static inline std::string name(
unsigned char op) {
1273 return casadi_math<double>::name(op);
1275 static inline std::string sep(
unsigned char op) {
1276 return casadi_math<double>::sep(op);
1278 static inline std::string post(
unsigned char op) {
1279 return casadi_math<double>::post(op);
1285 template<
typename T>
1286 inline void casadi_math<T>::fun(
unsigned char op,
const T& x,
const T& y, T& f) {
1289 #define CASADI_MATH_FUN_BUILTIN_GEN(CNAME, X, Y, F, N) \
1290 case OP_ASSIGN: CNAME<OP_ASSIGN>::fcn(X, Y, F, N); break; \
1291 case OP_ADD: CNAME<OP_ADD>::fcn(X, Y, F, N); break; \
1292 case OP_SUB: CNAME<OP_SUB>::fcn(X, Y, F, N); break; \
1293 case OP_MUL: CNAME<OP_MUL>::fcn(X, Y, F, N); break; \
1294 case OP_DIV: CNAME<OP_DIV>::fcn(X, Y, F, N); break; \
1295 case OP_NEG: CNAME<OP_NEG>::fcn(X, Y, F, N); break; \
1296 case OP_EXP: CNAME<OP_EXP>::fcn(X, Y, F, N); break; \
1297 case OP_LOG: CNAME<OP_LOG>::fcn(X, Y, F, N); break; \
1298 case OP_POW: CNAME<OP_POW>::fcn(X, Y, F, N); break; \
1299 case OP_CONSTPOW: CNAME<OP_CONSTPOW>::fcn(X, Y, F, N); break; \
1300 case OP_SQRT: CNAME<OP_SQRT>::fcn(X, Y, F, N); break; \
1301 case OP_SQ: CNAME<OP_SQ>::fcn(X, Y, F, N); break; \
1302 case OP_TWICE: CNAME<OP_TWICE>::fcn(X, Y, F, N); break; \
1303 case OP_SIN: CNAME<OP_SIN>::fcn(X, Y, F, N); break; \
1304 case OP_COS: CNAME<OP_COS>::fcn(X, Y, F, N); break; \
1305 case OP_TAN: CNAME<OP_TAN>::fcn(X, Y, F, N); break; \
1306 case OP_ASIN: CNAME<OP_ASIN>::fcn(X, Y, F, N); break; \
1307 case OP_ACOS: CNAME<OP_ACOS>::fcn(X, Y, F, N); break; \
1308 case OP_ATAN: CNAME<OP_ATAN>::fcn(X, Y, F, N); break; \
1309 case OP_LT: CNAME<OP_LT>::fcn(X, Y, F, N); break; \
1310 case OP_LE: CNAME<OP_LE>::fcn(X, Y, F, N); break; \
1311 case OP_EQ: CNAME<OP_EQ>::fcn(X, Y, F, N); break; \
1312 case OP_NE: CNAME<OP_NE>::fcn(X, Y, F, N); break; \
1313 case OP_NOT: CNAME<OP_NOT>::fcn(X, Y, F, N); break; \
1314 case OP_AND: CNAME<OP_AND>::fcn(X, Y, F, N); break; \
1315 case OP_OR: CNAME<OP_OR>::fcn(X, Y, F, N); break; \
1316 case OP_IF_ELSE_ZERO: CNAME<OP_IF_ELSE_ZERO>::fcn(X, Y, F, N); break; \
1317 case OP_FLOOR: CNAME<OP_FLOOR>::fcn(X, Y, F, N); break; \
1318 case OP_CEIL: CNAME<OP_CEIL>::fcn(X, Y, F, N); break; \
1319 case OP_FMOD: CNAME<OP_FMOD>::fcn(X, Y, F, N); break; \
1320 case OP_REMAINDER: CNAME<OP_REMAINDER>::fcn(X, Y, F, N); break; \
1321 case OP_FABS: CNAME<OP_FABS>::fcn(X, Y, F, N); break; \
1322 case OP_SIGN: CNAME<OP_SIGN>::fcn(X, Y, F, N); break; \
1323 case OP_COPYSIGN: CNAME<OP_COPYSIGN>::fcn(X, Y, F, N); break; \
1324 case OP_ERF: CNAME<OP_ERF>::fcn(X, Y, F, N); break; \
1325 case OP_FMIN: CNAME<OP_FMIN>::fcn(X, Y, F, N); break; \
1326 case OP_FMAX: CNAME<OP_FMAX>::fcn(X, Y, F, N); break; \
1327 case OP_INV: CNAME<OP_INV>::fcn(X, Y, F, N); break; \
1328 case OP_SINH: CNAME<OP_SINH>::fcn(X, Y, F, N); break; \
1329 case OP_COSH: CNAME<OP_COSH>::fcn(X, Y, F, N); break; \
1330 case OP_TANH: CNAME<OP_TANH>::fcn(X, Y, F, N); break; \
1331 case OP_ASINH: CNAME<OP_ASINH>::fcn(X, Y, F, N); break; \
1332 case OP_ACOSH: CNAME<OP_ACOSH>::fcn(X, Y, F, N); break; \
1333 case OP_ATANH: CNAME<OP_ATANH>::fcn(X, Y, F, N); break; \
1334 case OP_ATAN2: CNAME<OP_ATAN2>::fcn(X, Y, F, N); break; \
1335 case OP_ERFINV: CNAME<OP_ERFINV>::fcn(X, Y, F, N); break; \
1336 case OP_LIFT: CNAME<OP_LIFT>::fcn(X, Y, F, N); break; \
1337 case OP_PRINTME: CNAME<OP_PRINTME>::fcn(X, Y, F, N); break; \
1338 case OP_LOG1P: CNAME<OP_LOG1P>::fcn(X, Y, F, N); break; \
1339 case OP_EXPM1: CNAME<OP_EXPM1>::fcn(X, Y, F, N); break; \
1340 case OP_HYPOT: CNAME<OP_HYPOT>::fcn(X, Y, F, N); break;
1342 #define CASADI_MATH_FUN_BUILTIN(X, Y, F) CASADI_MATH_FUN_BUILTIN_GEN(BinaryOperationSS, X, Y, F, 1)
1345 CASADI_MATH_FUN_BUILTIN(x, y, f)
1349 template<
typename T>
1350 inline void casadi_math<T>::fun(
unsigned char op,
const T* x,
const T* y, T* f, casadi_int n) {
1352 CASADI_MATH_FUN_BUILTIN_GEN(BinaryOperationVV, x, y, f, n)
1356 template<
typename T>
1357 inline void casadi_math<T>::fun(
unsigned char op,
const T* x,
const T& y, T* f, casadi_int n) {
1359 CASADI_MATH_FUN_BUILTIN_GEN(BinaryOperationVS, x, y, f, n)
1363 template<
typename T>
1364 inline void casadi_math<T>::fun(
unsigned char op,
const T& x,
const T* y, T* f, casadi_int n) {
1366 CASADI_MATH_FUN_BUILTIN_GEN(BinaryOperationSV, x, y, f, n)
1371 template<
typename T>
1372 inline void casadi_math<T>::der(
unsigned char op,
const T& x,
const T& y,
const T& f, T* d) {
1375 #define CASADI_MATH_DER_BUILTIN(X, Y, F, D) \
1376 case OP_ASSIGN: BinaryOperation<OP_ASSIGN>::der(X, Y, F, D); break; \
1377 case OP_ADD: BinaryOperation<OP_ADD>::der(X, Y, F, D); break; \
1378 case OP_SUB: BinaryOperation<OP_SUB>::der(X, Y, F, D); break; \
1379 case OP_MUL: BinaryOperation<OP_MUL>::der(X, Y, F, D); break; \
1380 case OP_DIV: BinaryOperation<OP_DIV>::der(X, Y, F, D); break; \
1381 case OP_NEG: BinaryOperation<OP_NEG>::der(X, Y, F, D); break; \
1382 case OP_EXP: BinaryOperation<OP_EXP>::der(X, Y, F, D); break; \
1383 case OP_LOG: BinaryOperation<OP_LOG>::der(X, Y, F, D); break; \
1384 case OP_POW: BinaryOperation<OP_POW>::der(X, Y, F, D); break; \
1385 case OP_CONSTPOW: BinaryOperation<OP_CONSTPOW>::der(X, Y, F, D); break; \
1386 case OP_SQRT: BinaryOperation<OP_SQRT>::der(X, Y, F, D); break; \
1387 case OP_SQ: BinaryOperation<OP_SQ>::der(X, Y, F, D); break; \
1388 case OP_TWICE: BinaryOperation<OP_TWICE>::der(X, Y, F, D); break; \
1389 case OP_SIN: BinaryOperation<OP_SIN>::der(X, Y, F, D); break; \
1390 case OP_COS: BinaryOperation<OP_COS>::der(X, Y, F, D); break; \
1391 case OP_TAN: BinaryOperation<OP_TAN>::der(X, Y, F, D); break; \
1392 case OP_ASIN: BinaryOperation<OP_ASIN>::der(X, Y, F, D); break; \
1393 case OP_ACOS: BinaryOperation<OP_ACOS>::der(X, Y, F, D); break; \
1394 case OP_ATAN: BinaryOperation<OP_ATAN>::der(X, Y, F, D); break; \
1395 case OP_LT: BinaryOperation<OP_LT>::der(X, Y, F, D); break; \
1396 case OP_LE: BinaryOperation<OP_LE>::der(X, Y, F, D); break; \
1397 case OP_EQ: BinaryOperation<OP_EQ>::der(X, Y, F, D); break; \
1398 case OP_NE: BinaryOperation<OP_NE>::der(X, Y, F, D); break; \
1399 case OP_NOT: BinaryOperation<OP_NOT>::der(X, Y, F, D); break; \
1400 case OP_AND: BinaryOperation<OP_AND>::der(X, Y, F, D); break; \
1401 case OP_OR: BinaryOperation<OP_OR>::der(X, Y, F, D); break; \
1402 case OP_IF_ELSE_ZERO: BinaryOperation<OP_IF_ELSE_ZERO>::der(X, Y, F, D); break; \
1403 case OP_FLOOR: BinaryOperation<OP_FLOOR>::der(X, Y, F, D); break; \
1404 case OP_CEIL: BinaryOperation<OP_CEIL>::der(X, Y, F, D); break; \
1405 case OP_FMOD: BinaryOperation<OP_FMOD>::der(X, Y, F, D); break; \
1406 case OP_REMAINDER: BinaryOperation<OP_REMAINDER>::der(X, Y, F, D); break; \
1407 case OP_FABS: BinaryOperation<OP_FABS>::der(X, Y, F, D); break; \
1408 case OP_SIGN: BinaryOperation<OP_SIGN>::der(X, Y, F, D); break; \
1409 case OP_COPYSIGN: BinaryOperation<OP_COPYSIGN>::der(X, Y, F, D); break; \
1410 case OP_ERF: BinaryOperation<OP_ERF>::der(X, Y, F, D); break; \
1411 case OP_FMIN: BinaryOperation<OP_FMIN>::der(X, Y, F, D); break; \
1412 case OP_FMAX: BinaryOperation<OP_FMAX>::der(X, Y, F, D); break; \
1413 case OP_INV: BinaryOperation<OP_INV>::der(X, Y, F, D); break; \
1414 case OP_SINH: BinaryOperation<OP_SINH>::der(X, Y, F, D); break; \
1415 case OP_COSH: BinaryOperation<OP_COSH>::der(X, Y, F, D); break; \
1416 case OP_TANH: BinaryOperation<OP_TANH>::der(X, Y, F, D); break; \
1417 case OP_ASINH: BinaryOperation<OP_ASINH>::der(X, Y, F, D); break; \
1418 case OP_ACOSH: BinaryOperation<OP_ACOSH>::der(X, Y, F, D); break; \
1419 case OP_ATANH: BinaryOperation<OP_ATANH>::der(X, Y, F, D); break; \
1420 case OP_ATAN2: BinaryOperation<OP_ATAN2>::der(X, Y, F, D); break; \
1421 case OP_ERFINV: BinaryOperation<OP_ERFINV>::der(X, Y, F, D); break; \
1422 case OP_LIFT: BinaryOperation<OP_LIFT>::der(X, Y, F, D); break; \
1423 case OP_PRINTME: BinaryOperation<OP_PRINTME>::der(X, Y, F, D); break; \
1424 case OP_LOG1P: BinaryOperation<OP_LOG1P>::der(X, Y, F, D); break; \
1425 case OP_EXPM1: BinaryOperation<OP_EXPM1>::der(X, Y, F, D); break; \
1426 case OP_HYPOT: BinaryOperation<OP_HYPOT>::der(X, Y, F, D); break;
1428 CASADI_MATH_DER_BUILTIN(x, y, f, d)
1433 template<
typename T>
1434 inline void casadi_math<T>::derF(
unsigned char op,
const T& x,
const T& y, T& f, T* d) {
1437 #define CASADI_MATH_DERF_BUILTIN(X, Y, F, D) \
1438 case OP_ASSIGN: DerBinaryOperation<OP_ASSIGN>::derf(X, Y, F, D); break; \
1439 case OP_ADD: DerBinaryOperation<OP_ADD>::derf(X, Y, F, D); break; \
1440 case OP_SUB: DerBinaryOperation<OP_SUB>::derf(X, Y, F, D); break; \
1441 case OP_MUL: DerBinaryOperation<OP_MUL>::derf(X, Y, F, D); break; \
1442 case OP_DIV: DerBinaryOperation<OP_DIV>::derf(X, Y, F, D); break; \
1443 case OP_NEG: DerBinaryOperation<OP_NEG>::derf(X, Y, F, D); break; \
1444 case OP_EXP: DerBinaryOperation<OP_EXP>::derf(X, Y, F, D); break; \
1445 case OP_LOG: DerBinaryOperation<OP_LOG>::derf(X, Y, F, D); break; \
1446 case OP_POW: DerBinaryOperation<OP_POW>::derf(X, Y, F, D); break; \
1447 case OP_CONSTPOW: DerBinaryOperation<OP_CONSTPOW>::derf(X, Y, F, D); break; \
1448 case OP_SQRT: DerBinaryOperation<OP_SQRT>::derf(X, Y, F, D); break; \
1449 case OP_SQ: DerBinaryOperation<OP_SQ>::derf(X, Y, F, D); break; \
1450 case OP_TWICE: DerBinaryOperation<OP_TWICE>::derf(X, Y, F, D); break; \
1451 case OP_SIN: DerBinaryOperation<OP_SIN>::derf(X, Y, F, D); break; \
1452 case OP_COS: DerBinaryOperation<OP_COS>::derf(X, Y, F, D); break; \
1453 case OP_TAN: DerBinaryOperation<OP_TAN>::derf(X, Y, F, D); break; \
1454 case OP_ASIN: DerBinaryOperation<OP_ASIN>::derf(X, Y, F, D); break; \
1455 case OP_ACOS: DerBinaryOperation<OP_ACOS>::derf(X, Y, F, D); break; \
1456 case OP_ATAN: DerBinaryOperation<OP_ATAN>::derf(X, Y, F, D); break; \
1457 case OP_LT: DerBinaryOperation<OP_LT>::derf(X, Y, F, D); break; \
1458 case OP_LE: DerBinaryOperation<OP_LE>::derf(X, Y, F, D); break; \
1459 case OP_EQ: DerBinaryOperation<OP_EQ>::derf(X, Y, F, D); break; \
1460 case OP_NE: DerBinaryOperation<OP_NE>::derf(X, Y, F, D); break; \
1461 case OP_NOT: DerBinaryOperation<OP_NOT>::derf(X, Y, F, D); break; \
1462 case OP_AND: DerBinaryOperation<OP_AND>::derf(X, Y, F, D); break; \
1463 case OP_OR: DerBinaryOperation<OP_OR>::derf(X, Y, F, D); break; \
1464 case OP_IF_ELSE_ZERO: DerBinaryOperation<OP_IF_ELSE_ZERO>::derf(X, Y, F, D); break; \
1465 case OP_FLOOR: DerBinaryOperation<OP_FLOOR>::derf(X, Y, F, D); break; \
1466 case OP_CEIL: DerBinaryOperation<OP_CEIL>::derf(X, Y, F, D); break; \
1467 case OP_FMOD: DerBinaryOperation<OP_FMOD>::derf(X, Y, F, D); break; \
1468 case OP_REMAINDER: DerBinaryOperation<OP_REMAINDER>::derf(X, Y, F, D); break; \
1469 case OP_FABS: DerBinaryOperation<OP_FABS>::derf(X, Y, F, D); break; \
1470 case OP_SIGN: DerBinaryOperation<OP_SIGN>::derf(X, Y, F, D); break; \
1471 case OP_COPYSIGN: DerBinaryOperation<OP_COPYSIGN>::derf(X, Y, F, D); break; \
1472 case OP_ERF: DerBinaryOperation<OP_ERF>::derf(X, Y, F, D); break; \
1473 case OP_FMIN: DerBinaryOperation<OP_FMIN>::derf(X, Y, F, D); break; \
1474 case OP_FMAX: DerBinaryOperation<OP_FMAX>::derf(X, Y, F, D); break; \
1475 case OP_INV: DerBinaryOperation<OP_INV>::derf(X, Y, F, D); break; \
1476 case OP_SINH: DerBinaryOperation<OP_SINH>::derf(X, Y, F, D); break; \
1477 case OP_COSH: DerBinaryOperation<OP_COSH>::derf(X, Y, F, D); break; \
1478 case OP_TANH: DerBinaryOperation<OP_TANH>::derf(X, Y, F, D); break; \
1479 case OP_ASINH: DerBinaryOperation<OP_ASINH>::derf(X, Y, F, D); break; \
1480 case OP_ACOSH: DerBinaryOperation<OP_ACOSH>::derf(X, Y, F, D); break; \
1481 case OP_ATANH: DerBinaryOperation<OP_ATANH>::derf(X, Y, F, D); break; \
1482 case OP_ATAN2: DerBinaryOperation<OP_ATAN2>::derf(X, Y, F, D); break; \
1483 case OP_ERFINV: DerBinaryOperation<OP_ERFINV>::derf(X, Y, F, D); break; \
1484 case OP_LIFT: DerBinaryOperation<OP_LIFT>::derf(X, Y, F, D); break; \
1485 case OP_PRINTME: DerBinaryOperation<OP_PRINTME>::derf(X, Y, F, D); break; \
1486 case OP_LOG1P: DerBinaryOperation<OP_LOG1P>::derf(X, Y, F, D); break; \
1487 case OP_EXPM1: DerBinaryOperation<OP_EXPM1>::derf(X, Y, F, D); break; \
1488 case OP_HYPOT: DerBinaryOperation<OP_HYPOT>::derf(X, Y, F, D); break;
1490 CASADI_MATH_DERF_BUILTIN(x, y, f, d)
1494 #define CASADI_MATH_BINARY_BUILTIN \
1509 case OP_REMAINDER: \
1517 #define CASADI_MATH_UNARY_BUILTIN \
1548 template<
typename T>
1549 bool casadi_math<T>::is_binary(
unsigned char op) {
1551 CASADI_MATH_BINARY_BUILTIN
1552 case OP_IF_ELSE_ZERO:
1559 template<
typename T>
1560 bool casadi_math<T>::is_unary(
unsigned char op) {
1562 CASADI_MATH_UNARY_BUILTIN
1569 template<
typename T>
1570 inline casadi_int casadi_math<T>::ndeps(
unsigned char op) {
1576 CASADI_MATH_BINARY_BUILTIN
1577 case OP_IF_ELSE_ZERO:
1584 template<
typename T>
1586 casadi_math<T>::print(
unsigned char op,
1587 const std::string& x,
const std::string& y) {
1588 casadi_assert_dev(ndeps(op)==2);
1589 return pre(op) + x + sep(op) + y + post(op);
1592 template<
typename T>
1594 casadi_math<T>::print(
unsigned char op,
const std::string& x) {
1595 casadi_assert_dev(ndeps(op)==1);
1596 return pre(op) + x + post(op);
1599 template<
typename T>
1600 inline std::string casadi_math<T>::name(
unsigned char op) {
1602 case OP_ASSIGN:
return "assign";
1603 case OP_ADD:
return "add";
1604 case OP_SUB:
return "sub";
1605 case OP_MUL:
return "mul";
1606 case OP_DIV:
return "div";
1607 case OP_NEG:
return "neg";
1608 case OP_EXP:
return "exp";
1609 case OP_LOG:
return "log";
1611 case OP_POW:
return "pow";
1612 case OP_SQRT:
return "sqrt";
1613 case OP_SQ:
return "sq";
1614 case OP_TWICE:
return "twice";
1615 case OP_SIN:
return "sin";
1616 case OP_COS:
return "cos";
1617 case OP_TAN:
return "tan";
1618 case OP_ASIN:
return "asin";
1619 case OP_ACOS:
return "acos";
1620 case OP_ATAN:
return "atan";
1621 case OP_LT:
return "lt";
1622 case OP_LE:
return "le";
1623 case OP_EQ:
return "eq";
1624 case OP_NE:
return "ne";
1625 case OP_NOT:
return "not";
1626 case OP_AND:
return "and";
1627 case OP_OR:
return "or";
1628 case OP_FLOOR:
return "floor";
1629 case OP_CEIL:
return "ceil";
1630 case OP_FMOD:
return "fmod";
1631 case OP_REMAINDER:
return "remainder";
1632 case OP_FABS:
return "fabs";
1633 case OP_SIGN:
return "sign";
1634 case OP_COPYSIGN:
return "copysign";
1635 case OP_IF_ELSE_ZERO:
return "if_else_zero";
1636 case OP_ERF:
return "erf";
1637 case OP_FMIN:
return "fmin";
1638 case OP_FMAX:
return "fmax";
1639 case OP_INV:
return "inv";
1640 case OP_SINH:
return "sinh";
1641 case OP_COSH:
return "cosh";
1642 case OP_TANH:
return "tanh";
1643 case OP_ASINH:
return "asinh";
1644 case OP_ACOSH:
return "acosh";
1645 case OP_ATANH:
return "atanh";
1646 case OP_ATAN2:
return "atan2";
1647 case OP_CONST:
return "const";
1648 case OP_INPUT:
return "input";
1649 case OP_OUTPUT:
return "output";
1650 case OP_PARAMETER:
return "parameter";
1651 case OP_CALL:
return "call";
1652 case OP_MTIMES:
return "mtimes";
1653 case OP_SOLVE:
return "solve";
1654 case OP_TRANSPOSE:
return "transpose";
1655 case OP_DETERMINANT:
return "determinant";
1656 case OP_INVERSE:
return "inverse";
1657 case OP_DOT:
return "dot";
1658 case OP_HORZCAT:
return "horzcat";
1659 case OP_VERTCAT:
return "vertcat";
1660 case OP_DIAGCAT:
return "diagcat";
1661 case OP_HORZSPLIT:
return "horzsplit";
1662 case OP_VERTSPLIT:
return "vertsplit";
1663 case OP_DIAGSPLIT:
return "diagsplit";
1664 case OP_RESHAPE:
return "reshape";
1665 case OP_SPARSITY_CAST:
return "sparsity_cast";
1666 case OP_SUBREF:
return "subref";
1667 case OP_SUBASSIGN:
return "subassign";
1668 case OP_GETNONZEROS:
return "getnonzeros";
1669 case OP_GETNONZEROS_PARAM:
return "getnonzeros_param";
1670 case OP_ADDNONZEROS:
return "addnonzeros";
1671 case OP_ADDNONZEROS_PARAM:
return "addnonzeros_param";
1672 case OP_SETNONZEROS:
return "setnonzeros";
1673 case OP_SETNONZEROS_PARAM:
return "setnonzeros_param";
1674 case OP_PROJECT:
return "project";
1675 case OP_ASSERTION:
return "assertion";
1676 case OP_NORM2:
return "norm2";
1677 case OP_NORM1:
return "norm1";
1678 case OP_NORMINF:
return "norminf";
1679 case OP_NORMF:
return "normf";
1680 case OP_ERFINV:
return "erfinv";
1681 case OP_PRINTME:
return "printme";
1682 case OP_LIFT:
return "lift";
1683 case OP_EINSTEIN:
return "einstein";
1684 case OP_BSPLINE:
return "bspline";
1685 case OP_CONVEXIFY:
return "convexify";
1686 case OP_LOG1P:
return "log1p";
1687 case OP_EXPM1:
return "expm1";
1688 case OP_HYPOT:
return "hypot";
1689 case OP_LOGSUMEXP:
return "logsumexp";
1691 return "<invalid-op>";
1694 template<
typename T>
1695 inline std::string casadi_math<T>::pre(
unsigned char op) {
1697 case OP_ASSIGN:
return "";
1698 case OP_ADD:
return "(";
1699 case OP_SUB:
return "(";
1700 case OP_MUL:
return "(";
1701 case OP_DIV:
return "(";
1702 case OP_NEG:
return "(-";
1703 case OP_TWICE:
return "(2.*";
1704 case OP_LT:
return "(";
1705 case OP_LE:
return "(";
1706 case OP_EQ:
return "(";
1707 case OP_NE:
return "(";
1708 case OP_NOT:
return "(!";
1709 case OP_AND:
return "(";
1710 case OP_OR:
return "(";
1711 case OP_IF_ELSE_ZERO:
return "(";
1712 case OP_INV:
return "(1./";
1713 default:
return name(op) +
"(";
1717 template<
typename T>
1718 inline std::string casadi_math<T>::sep(
unsigned char op) {
1720 case OP_ADD:
return "+";
1721 case OP_SUB:
return "-";
1722 case OP_MUL:
return "*";
1723 case OP_DIV:
return "/";
1724 case OP_LT:
return "<";
1725 case OP_LE:
return "<=";
1726 case OP_EQ:
return "==";
1727 case OP_NE:
return "!=";
1728 case OP_AND:
return "&&";
1729 case OP_OR:
return "||";
1730 case OP_IF_ELSE_ZERO:
return "?";
1731 default:
return ",";
1735 template<
typename T>
1736 inline std::string casadi_math<T>::post(
unsigned char op) {
1738 case OP_ASSIGN:
return "";
1739 case OP_IF_ELSE_ZERO:
return ":0)";
1740 default:
return ")";
CASADI_EXPORT std::ostream & uout()