List of all members | Public Member Functions | Static Public Member Functions | Protected Member Functions | Friends
casadi::SXElem Class Reference

The basic scalar symbolic class of CasADi. More...

#include <sx_elem.hpp>

Detailed Description

SXElem is exposed only as an empty struct to SWIG

Author
Joel Andersson
Date
2010-2014

Extra doc: https://github.com/casadi/casadi/wiki/L_10i

Definition at line 74 of file sx_elem.hpp.

Inheritance diagram for casadi::SXElem:
Inheritance graph
[legend]
Collaboration diagram for casadi::SXElem:
Collaboration graph
[legend]

Public Member Functions

 SXElem ()
 Default constructor (not-a-number) More...
 
 SXElem (double val)
 Numerical constant constructor. More...
 
 SXElem (SXNode *node, bool dummy)
 
 SXElem (const SXElem &scalar)
 Copy constructor. More...
 
 ~SXElem ()
 Destructor. More...
 
SXElem & operator= (const SXElem &scalar)
 Assignment. More...
 
SXElem & operator= (double scalar)
 
 operator Matrix< SXElem > () const
 Convert to a 1-by-1 Matrix. More...
 
void disp (std::ostream &stream, bool more=false) const
 Print a description of the object. More...
 
SXNode * get () const
 Get a pointer to the node. More...
 
const SXNode * operator-> () const
 Access functions of the node. More...
 
SXNode * operator-> ()
 
bool __nonzero__ () const
 Check the truth value of this node. More...
 
bool is_leaf () const
 check if this SXElem is a leaf of the SX graph More...
 
bool is_constant () const
 
bool is_integer () const
 
bool is_symbolic () const
 
bool is_commutative () const
 Check whether a binary SXElem is commutative. More...
 
bool is_zero () const
 
bool is_almost_zero (double tol) const
 
bool is_one () const
 
bool is_minus_one () const
 
bool is_half () const
 
bool is_value (double val) const
 
bool is_nan () const
 
bool is_inf () const
 
bool is_minus_inf () const
 
const std::string & name () const
 
casadi_int op () const
 
bool is_op (casadi_int op) const
 
bool is_call () const
 
bool is_output () const
 
bool has_output () const
 
Function which_function () const
 
casadi_int which_output () const
 
bool is_regular () const
 Checks if expression does not contain NaN or Inf. More...
 
bool is_nonnegative () const
 Check if a value is always nonnegative (false negatives are allowed) More...
 
bool is_boolean () const
 Check if a value is always 0 or 1 (false negatives are allowed) More...
 
SXElem dep (casadi_int ch=0) const
 
 operator double () const
 Type conversion to double. More...
 
 operator casadi_int () const
 Type conversion to casadi_int. More...
 
bool is_doubled () const
 Check if the node is the sum of two equal expressions. More...
 
casadi_int n_dep () const
 Get the number of dependencies of a binary SXElem. More...
 
SXElem get_output (casadi_int oind) const
 Get an output. More...
 
std::vector< SXElem > get_output (const std::vector< casadi_int > &oind) const
 Get a batch of outputs. More...
 
casadi_int __hash__ () const
 Returns a number that is unique for a given SXNode. More...
 
SXElem operator- () const
 Negation. More...
 
SXElem inv () const
 Element-wise inverse. More...
 
int get_temp () const
 
void set_temp (int t) const
 Set the temporary variable. More...
 
bool marked () const
 Check if marked (i.e. temporary is negative) More...
 
void mark () const
 Mark by flipping the sign of the temporary and decreasing by one. More...
 
void assignIfDuplicate (const SXElem &scalar, casadi_int depth=1)
 Assign to another expression, if a duplicate. More...
 
SXNode * assignNoDelete (const SXElem &scalar)
 Assign the node to something, without invoking the deletion of the node,. More...
 
bool is_null ()
 SXElem nodes are not allowed to be null. More...
 
void serialize (SerializingStream &s) const
 Serialize an object. More...
 
SXElem & operator+= (const SXElem &y)
 

Static Public Member Functions

static SXElem sym (const std::string &name)
 Create a symbolic primitive. More...
 
static SXElem create (SXNode *node)
 
static std::string type_name ()
 Type name. More...
 
static SXElem binary (casadi_int op, const SXElem &x, const SXElem &y, bool unique_x=false, bool unique_y=false)
 Perform operations by ID. More...
 
static SXElem unary (casadi_int op, const SXElem &x, bool unique=false)
 
static std::vector< SXElem > call (const Function &f, const std::vector< SXElem > &deps)
 
static bool is_equal (const SXElem &x, const SXElem &y, casadi_int depth=0)
 Check equality up to a given depth. More...
 
static SXElem deserialize (DeserializingStream &s)
 
static SXElem plus (const SXElem &x, const SXElem &y)
 Addition: (x,y) -> x + y. More...
 
static SXElem lt (const SXElem &x, const SXElem &y)
 Logical less than: (x,y) -> x < y. More...
 
static SXElem le (const SXElem &x, const SXElem &y)
 Logical less or equal to: (x,y) -> x <= y. More...
 
static SXElem gt (const SXElem &x, const SXElem &y)
 Logical greater than: (x,y) -> x > y. More...
 
static SXElem ge (const SXElem &x, const SXElem &y)
 Logical greater or equal to: (x,y) -> x >= y. More...
 
static SXElem eq (const SXElem &x, const SXElem &y)
 Logical equal to: (x,y) -> x == y. More...
 
static SXElem ne (const SXElem &x, const SXElem &y)
 Logical not equal to: (x,y) -> x != y. More...
 
static SXElem logic_and (const SXElem &x, const SXElem &y)
 Logical and More...
 
static SXElem logic_or (const SXElem &x, const SXElem &y)
 Logical or More...
 
static SXElem abs (const SXElem &x)
 Absolute value: x -> abs(x) More...
 
static SXElem sqrt (const SXElem &x)
 Square root: x -> sqrt(x) More...
 
static SXElem sq (const SXElem &x)
 Square: x -> x^2. More...
 
static SXElem sin (const SXElem &x)
 Sine: x -> sin(x) More...
 
static SXElem cos (const SXElem &x)
 Cosine: x -> cos(x) More...
 
static SXElem tan (const SXElem &x)
 Tangent: x -> tan(x) More...
 
static SXElem atan (const SXElem &x)
 Arc tangent: x -> atan(x) More...
 
static SXElem asin (const SXElem &x)
 Arc sine: x -> asin(x) More...
 
static SXElem acos (const SXElem &x)
 Arc cosine: x -> acos(x) More...
 
static SXElem tanh (const SXElem &x)
 Hyperbolic tangent: x -> tanh(x) More...
 
static SXElem sinh (const SXElem &x)
 Hyperbolic sin: x -> sinh(x) More...
 
static SXElem cosh (const SXElem &x)
 Hyperbolic cosine: x -> cosh(x) More...
 
static SXElem atanh (const SXElem &x)
 Inverse hyperbolic tangent: x -> atanh(x) More...
 
static SXElem asinh (const SXElem &x)
 Inverse hyperbolic sin: x -> asinh(x) More...
 
static SXElem acosh (const SXElem &x)
 Inverse hyperbolic cosine: x -> acosh(x) More...
 
static SXElem exp (const SXElem &x)
 Elementwise exponential: x -> exp(x) More...
 
static SXElem log (const SXElem &x)
 Natural logarithm: x -> log(x) More...
 
static SXElem log10 (const SXElem &x)
 Base-10 logarithm: x -> log10(x) More...
 
static SXElem log1p (const SXElem &x)
 Precision variant for natural logarithm: x -> log(x+1) More...
 
static SXElem expm1 (const SXElem &x)
 Precision variant for elementwise exponential: x -> exp(x)-1. More...
 
static SXElem floor (const SXElem &x)
 Round down to nearest integer: x -> floor(x) More...
 
static SXElem ceil (const SXElem &x)
 Round up to nearest integer: x -> ceil(x) More...
 
static SXElem erf (const SXElem &x)
 Error function: x -> erf(x) More...
 
static SXElem erfinv (const SXElem &x)
 Inverse error function: x -> erfinv(x) More...
 
static SXElem sign (const SXElem &x)
 Sign function: More...
 
static SXElem pow (const SXElem &x, const SXElem &y)
 Elementwise power: (x,y) -> x.^y. More...
 
static SXElem mod (const SXElem &x, const SXElem &y)
 Remainder after division: (x,y) -> fmod(x,y) More...
 
static SXElem remainder (const SXElem &x, const SXElem &y)
 Remainder after division: (x,y) -> remainder(x,y) More...
 
static SXElem atan2 (const SXElem &y, const SXElem &x)
 Two argument arc tangent: (y,x) -> atan2(y,x) More...
 
static SXElem if_else_zero (const SXElem &x, const SXElem &y)
 Conditional assignment: (x,y) -> x ? y : 0. More...
 
static SXElem fmin (const SXElem &x, const SXElem &y)
 Smallest of two values: (x,y) -> min(x,y) More...
 
static SXElem fmax (const SXElem &x, const SXElem &y)
 Largest of two values: (x,y) -> max(x,y) More...
 
static SXElem copysign (const SXElem &x, const SXElem &y)
 Copy sign: (x,y) -> copysign(x,y) More...
 
static SXElem constpow (const SXElem &x, const SXElem &y)
 Elementwise power with const power: (x,y) -> x^y with y constant. More...
 
static SXElem printme (const SXElem &x, const SXElem &y)
 Debug printing: (x,y) -> x, printing x when evaluated. More...
 
static SXElem hypot (const SXElem &x, const SXElem &y)
 Precision variant for 2 norm: (x,y) -> sqrt(x^2+y^2) More...
 

Protected Member Functions

const SXElem & self () const
 
SXElem & self ()
 

Friends

class SXNode
 
class BinarySXNode
 
class Matrix< SXElem >
 
SXElem if_else (const SXElem &x, const SXElem &y, const SXElem &z)
 Ternary if_else: x ? y : z. More...
 
SXElem & operator-= (const SXElem &y)
 Subtraction: (x,y) -> x - y. More...
 
static SXElem minus (const SXElem &x, const SXElem &y)
 Subtraction: (x,y) -> x - y. More...
 
SXElem & operator*= (const SXElem &y)
 Elementwise multiplication: (x,y) -> x .* y. More...
 
static SXElem times (const SXElem &x, const SXElem &y)
 Elementwise multiplication: (x,y) -> x .* y. More...
 
SXElem & operator/= (const SXElem &y)
 Elementwise division: (x,y) -> x ./ y. More...
 
static SXElem rdivide (const SXElem &x, const SXElem &y)
 Elementwise division: (x,y) -> x ./ y. More...
 
SXElem operator! () const
 Logical not x -> !x. More...
 
static SXElem logic_not (const SXElem &x)
 Logical not x -> !x. More...
 

Constructor & Destructor Documentation

◆ SXElem() [1/4]

casadi::SXElem::SXElem ( )

Object is initialized as not-a-number.

Extra doc: https://github.com/casadi/casadi/wiki/L_10j

Definition at line 53 of file sx_elem.cpp.

53  {
54  node = casadi_limits<SXElem>::nan.node;
55  node->count++;
56  }
unsigned int count
Definition: sx_node.hpp:202
casadi_limits class

References casadi::SXNode::count.

Referenced by create(), and operator=().

◆ SXElem() [2/4]

casadi::SXElem::SXElem ( double  val)
Parameters
valNumerical value

Extra doc: https://github.com/casadi/casadi/wiki/L_10k

Definition at line 71 of file sx_elem.cpp.

71  {
72  // Check for nan/inf first to avoid undefined behavior from static_cast<int>
73  if (isnan(val)) {
74  node = casadi_limits<SXElem>::nan.node;
75  } else if (isinf(val)) {
76  node = val > 0 ? casadi_limits<SXElem>::inf.node :
78  } else {
79  // Only ints fit here, not casadi_int; range check avoids UB in the cast
80  const double lo = static_cast<double>(std::numeric_limits<int>::min());
81  if (val >= lo && val < -lo && val == static_cast<double>(static_cast<int>(val))) {
82  int intval = static_cast<int>(val);
83  if (intval == 0) node = casadi_limits<SXElem>::zero.node;
84  else if (intval == 1) node = casadi_limits<SXElem>::one.node;
85  else if (intval == 2) node = casadi_limits<SXElem>::two.node;
86  else if (intval == -1) node = casadi_limits<SXElem>::minus_one.node;
87  else node = IntegerSX::create(intval);
88  } else {
89  node = RealtypeSX::create(val);
90  }
91  }
92  node->count++;
93  }
static IntegerSX * create(casadi_int value)
Static creator function (use instead of constructor)
static RealtypeSX * create(double value)
Static creator function (use instead of constructor)

References casadi::SXNode::count, casadi::IntegerSX::create(), and casadi::RealtypeSX::create().

◆ SXElem() [3/4]

casadi::SXElem::SXElem ( SXNode *  node,
bool  dummy 
)

Create an expression from a node: extra dummy argument to avoid ambiguity for 0/NULL

Definition at line 58 of file sx_elem.cpp.

58  : node(node_) {
59  node->count++;
60  }

References casadi::SXNode::count.

◆ SXElem() [4/4]

casadi::SXElem::SXElem ( const SXElem &  scalar)

Extra doc: https://github.com/casadi/casadi/wiki/L_10m

Definition at line 66 of file sx_elem.cpp.

66  {
67  node = scalar.node;
68  node->count++;
69  }

References casadi::SXNode::count.

◆ ~SXElem()

casadi::SXElem::~SXElem ( )

Definition at line 99 of file sx_elem.cpp.

99  {
100  if (--node->count == 0) delete node;
101  }

References casadi::SXNode::count.

Member Function Documentation

◆ __hash__()

casadi_int casadi::SXElem::__hash__ ( ) const

If the SXElem does not point to any node, 0 is returned.

Extra doc: https://github.com/casadi/casadi/wiki/L_10w

Definition at line 412 of file sx_elem.cpp.

412  {
413  return reinterpret_cast<casadi_int>(node);
414  }

◆ __nonzero__()

bool casadi::SXElem::__nonzero__ ( ) const

Introduced to catch bool(x) situations in python

Extra doc: https://github.com/casadi/casadi/wiki/L_10q

Definition at line 164 of file sx_elem.cpp.

164  {
165  if (is_constant()) return !is_zero();
166  casadi_error("Cannot compute the truth value of a CasADi SXElem symbolic expression.");
167  }
bool is_zero() const
Definition: sx_elem.cpp:311
bool is_constant() const
Definition: sx_elem.cpp:279

References is_constant(), and is_zero().

◆ abs()

static SXElem casadi::GenericExpression< SXElem >::abs ( const SXElem &  x)
inlinestaticinherited

Elementwise; structural zeros are preserved. The unary functions of this group operate on every entry, keep the shape and build a symbolic expression for SX and MX. Those that map 0 to 0 (abs, sqrt, sin, tan, atan, asin, sinh, tanh, asinh, atanh, floor, ceil, sign, erf, erfinv, log1p, expm1) keep the sparsity pattern; the others (cos, cosh, exp, log, log10, acos, acosh, logic_not) return a dense result. Named fabs in Python, where the built-in abs also applies.

>>> fabs(DM([-1, 0, 2]))
DM([1, 0, 2])
>>> fabs(DM.eye(2) * -1)
DM(
[[1, 00],
 [00, 1]])
>>> abs(SX.sym("x"))
SX(fabs(x))

>> abs(DM([-1; 0; 2]))
[1, 0, 2]
>> abs(-DM.eye(2))
[[1, 00],
 [00, 1]]
>> abs(SX.sym('x'))
fabs(x)

julia> abs.(ca.DM([-1, 0, 2]))
DM([1, 0, 2])
julia> abs(-ca.DM.eye(2))
DM(
[[1, 00],
 [00, 1]])
julia> abs(ca.SX.sym("x"))
SX(fabs(x))

See also
sign, copysign

Extra doc: https://github.com/casadi/casadi/wiki/L_p0

Definition at line 925 of file generic_expression.hpp.

925  {
926  return ExType::unary(OP_FABS, x);
927  }

◆ acos()

static SXElem casadi::GenericExpression< SXElem >::acos ( const SXElem &  x)
inlinestaticinherited

Elementwise; the result is dense (acos(0) == pi/2). Arguments outside [-1, 1] give NaN.

>>> acos(DM([0, 1]))
DM([1.5708, 0])

>> acos(DM([0; 1]))
[1.5708, 0]

julia> acos.(ca.DM([0, 1]))
DM([1.5708, 0])

See also
cos, asin

Extra doc: https://github.com/casadi/casadi/wiki/L_p8

Definition at line 1261 of file generic_expression.hpp.

1261  {
1262  return ExType::unary(OP_ACOS, x);
1263  }

◆ acosh()

static SXElem casadi::GenericExpression< SXElem >::acosh ( const SXElem &  x)
inlinestaticinherited

Elementwise; the result is dense. Defined for x >= 1, NaN below.

>>> acosh(DM([1, 2]))
DM([0, 1.31696])

>> acosh(DM([1; 2]))
[0, 1.31696]

julia> acosh.(ca.DM([1, 2]))
DM([0, 1.31696])

See also
cosh

Extra doc: https://github.com/casadi/casadi/wiki/L_pe

Definition at line 1483 of file generic_expression.hpp.

1483  {
1484  return ExType::unary(OP_ACOSH, x);
1485  }

◆ asin()

static SXElem casadi::GenericExpression< SXElem >::asin ( const SXElem &  x)
inlinestaticinherited

Elementwise; sparsity preserved. Arguments outside [-1, 1] give NaN.

>>> asin(DM([0, 1]))
DM([0, 1.5708])

>> asin(DM([0; 1]))
[0, 1.5708]

julia> asin.(ca.DM([0, 1]))
DM([0, 1.5708])

See also
sin, acos

Extra doc: https://github.com/casadi/casadi/wiki/L_p7

Definition at line 1224 of file generic_expression.hpp.

1224  {
1225  return ExType::unary(OP_ASIN, x);
1226  }

◆ asinh()

static SXElem casadi::GenericExpression< SXElem >::asinh ( const SXElem &  x)
inlinestaticinherited

Elementwise; sparsity preserved.

>>> asinh(DM([-1, 1]))
DM([-0.881374, 0.881374])

>> asinh(DM([-1; 1]))
[-0.881374, 0.881374]

julia> asinh.(ca.DM([-1, 1]))
DM([-0.881374, 0.881374])

See also
sinh

Extra doc: https://github.com/casadi/casadi/wiki/L_pd

Definition at line 1446 of file generic_expression.hpp.

1446  {
1447  return ExType::unary(OP_ASINH, x);
1448  }

◆ assignIfDuplicate()

void casadi::SXElem::assignIfDuplicate ( const SXElem &  scalar,
casadi_int  depth = 1 
)

Check for equality up to a given depth

Extra doc: https://github.com/casadi/casadi/wiki/L_110

Definition at line 118 of file sx_elem.cpp.

118  {
119  casadi_assert_dev(depth>=1);
120  if (!is_equal(*this, scalar, 0) && is_equal(*this, scalar, depth)) {
121  *this = scalar;
122  }
123  }
static bool is_equal(const SXElem &x, const SXElem &y, casadi_int depth=0)
Check equality up to a given depth.
Definition: sx_elem.cpp:359

References is_equal().

Referenced by casadi::SXFunction::eval_sx().

◆ assignNoDelete()

SXNode * casadi::SXElem::assignNoDelete ( const SXElem &  scalar)

if the count reaches 0

Extra doc: https://github.com/casadi/casadi/wiki/L_111

Definition at line 125 of file sx_elem.cpp.

125  {
126  // Return value
127  SXNode* ret = node;
128 
129  // quick return if the old and new pointers point to the same object
130  if (node == scalar.node) return ret;
131 
132  // decrease the counter but do not delete if this was the last pointer
133  --node->count;
134 
135  // save the new pointer
136  node = scalar.node;
137  node->count++;
138 
139  // Return a pointer to the old node
140  return ret;
141  }
Internal node class for SX.
Definition: sx_node.hpp:49

References casadi::SXNode::count.

Referenced by casadi::SXNode::safe_delete(), casadi::BinarySX::~BinarySX(), and casadi::UnarySX::~UnarySX().

◆ atan()

static SXElem casadi::GenericExpression< SXElem >::atan ( const SXElem &  x)
inlinestaticinherited

Elementwise; sparsity preserved. Result in (-pi/2, pi/2); use atan2 for a quadrant-aware angle.

>>> atan(DM([0, 1]))
DM([0, 0.785398])

>> atan(DM([0; 1]))
[0, 0.785398]

julia> atan.(ca.DM([0, 1]))
DM([0, 0.785398])

See also
atan2, tan

Extra doc: https://github.com/casadi/casadi/wiki/L_p6

Definition at line 1187 of file generic_expression.hpp.

1187  {
1188  return ExType::unary(OP_ATAN, x);
1189  }

◆ atan2()

static SXElem casadi::GenericExpression< SXElem >::atan2 ( const SXElem &  y,
const SXElem &  x 
)
inlinestaticinherited

theta = atan2(y,x) corresponds to x = r cos(theta), y = r sin(theta)

Angle in (-pi, pi] of the point (x, y), taking the quadrant into account; the first argument is the y coordinate, as in C and numpy. Elementwise, shape rules as for plus. atan2(0, 0) is 0.

 >>> atan2(DM([1, -1, 0]), DM([-1, -1, 1]))
 DM([2.35619, -2.35619, 0])
 >>> atan2(SX.sym("y"), SX.sym("x"))
 SX(atan2(y,x))

 >> atan2(DM([1; -1; 0]), DM([-1; -1; 1]))
 [2.35619, -2.35619, 0]
 >> atan2(SX.sym('y'), SX.sym('x'))
 atan2(y,x)

 julia> atan.(ca.DM([1, -1, 0]), ca.DM([-1, -1, 1]))
 DM([2.35619, -2.35619, 0])
 julia> atan(ca.SX.sym("y"), ca.SX.sym("x"))
 SX(atan2(y,x))

See also
atan

Extra doc: https://github.com/casadi/casadi/wiki/L_pr

Definition at line 2146 of file generic_expression.hpp.

2146  {
2147  return ExType::binary(OP_ATAN2, y, x);
2148  }

◆ atanh()

static SXElem casadi::GenericExpression< SXElem >::atanh ( const SXElem &  x)
inlinestaticinherited

Elementwise; sparsity preserved. Defined for |x| < 1, infinite at +-1.

>>> atanh(DM([0, 0.5]))
DM([0, 0.549306])

>> atanh(DM([0; 0.5]))
[0, 0.549306]

julia> atanh.(ca.DM([0, 0.5]))
DM([0, 0.549306])

See also
tanh

Extra doc: https://github.com/casadi/casadi/wiki/L_pc

Definition at line 1409 of file generic_expression.hpp.

1409  {
1410  return ExType::unary(OP_ATANH, x);
1411  }

◆ binary()

SXElem casadi::SXElem::binary ( casadi_int  op,
const SXElem &  x,
const SXElem &  y,
bool  unique_x = false,
bool  unique_y = false 
)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_10p

Definition at line 190 of file sx_elem.cpp.

191  {
192  // If-else-zero nodes are always simplified at top level to avoid NaN propagation
193  if (y.op() == OP_IF_ELSE_ZERO) {
194  if (op == OP_MUL) {
195  // (Rule 1.) x * if_else_zero(c, y), simplified to if_else_zero(c, x * y)
196  // Background: x is often a partial derivative and may evaluate to INF or NAN.
197  // The simplification ensures that the zero seed corresponding to an inactive branch does
198  // not give rise to any NaN contribution to the derivative due to NaN * 0 == NaN.
199  return if_else_zero(y.dep(0), x * y.dep(1));
200  } else if (op == OP_ADD && x.op() == OP_IF_ELSE_ZERO && is_equal(x.dep(0), y.dep(0))) {
201  // (Rule 2.) if_else_zero(c, x) + if_else_zero(c, y) is simplified to if_else_zero(c, x + y)
202  // Background: During the backward propagation, seeds are added together. Without this rule,
203  // the addition node can prevent rule (1.) from working in subsequent steps.
204  return if_else_zero(y.dep(0), x.dep(1) + y.dep(1));
205  }
206  } else if (x.op() == OP_IF_ELSE_ZERO && op == OP_MUL) {
207  // Same as Rule 1. above, but with factors swapped. For symmetry.
208  return if_else_zero(x.dep(0), x.dep(1) * y);
209  }
210  // Simplifications
212  // Annihilation by a zero, unless constant folding applies (as in MX)
213  if ((operation_checker<F0XChecker>(op) && x.is_zero() && !y.is_constant()) ||
214  (operation_checker<FX0Checker>(op) && y.is_zero() && !x.is_constant())) return 0;
215  bool hit;
217  [](casadi_int op, const SXElem& a) { return unary(op, a);},
218  [](casadi_int op, const SXElem& a, const SXElem& b) { return binary(op, a, b);},
219  unique_x,
220  unique_y,
221  hit);
222  if (hit) return ret;
223  switch (op) {
224  case OP_MUL:
225  if (x.is_constant() && y.is_op(OP_TWICE))
226  return (x*2)*y.dep(); // a*(2*x) -> (a*2)*x
227  else if (y.is_constant() && x.is_op(OP_TWICE))
228  return (2*y)*x.dep(); // (2*x)*b -> (2*b)*x
229  break;
230  }
231 
232  }
233  return BinarySX::create(Operation(op), x, y);
234  }
static SXElem create(unsigned char op, const SXElem &dep0, const SXElem &dep1)
Create a binary expression.
Definition: binary_sx.hpp:55
static bool simplification_on_the_fly
Indicates whether simplifications should be made on the fly.
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
SXElem dep(casadi_int ch=0) const
Definition: sx_elem.cpp:395
static SXElem unary(casadi_int op, const SXElem &x, bool unique=false)
Definition: sx_elem.cpp:241
casadi_int op() const
Definition: sx_elem.cpp:351
bool is_op(casadi_int op) const
Definition: sx_elem.cpp:355
static SXElem binary(casadi_int op, const SXElem &x, const SXElem &y, bool unique_x=false, bool unique_y=false)
Perform operations by ID.
Definition: sx_elem.cpp:190
static casadi_int eq_depth_
Definition: sx_node.hpp:184
static SXElem if_else_zero(const SXElem &x, const SXElem &y)
Conditional assignment: (x,y) -> x ? y : 0.
T common_simp_binary(casadi_int op, const T &x, const T &y, casadi_int depth, SU &&gen_unary, SB &&gen_binary, bool unique_x, bool unique_y, bool &hit)
Definition: calculus.hpp:1891
Operation
Enum for quick access to any node.
Definition: calculus.hpp:60
@ OP_IF_ELSE_ZERO
Definition: calculus.hpp:71
@ OP_TWICE
Definition: calculus.hpp:67
@ OP_ADD
Definition: calculus.hpp:65
@ OP_MUL
Definition: calculus.hpp:65

References casadi::common_simp_binary(), casadi::BinarySX::create(), dep(), casadi::SXNode::eq_depth_, casadi::GenericExpression< SXElem >::if_else_zero(), is_constant(), is_equal(), is_op(), is_zero(), op(), casadi::OP_ADD, casadi::OP_IF_ELSE_ZERO, casadi::OP_MUL, casadi::OP_TWICE, casadi::GlobalOptions::simplification_on_the_fly, and unary().

◆ call()

std::vector< SXElem > casadi::SXElem::call ( const Function &  f,
const std::vector< SXElem > &  deps 
)
static

Definition at line 236 of file sx_elem.cpp.

236  {
237  SXElem c = CallSX::create(f, deps);
238  return OutputSX::split(c, f.nnz_out());
239  }
static SXElem create(const Function &f, const std::vector< SXElem > &dep)
Create a binary expression.
Definition: call_sx.hpp:57
casadi_int nnz_out() const
Get the number of structural nonzeros of one output, or of all outputs.
Definition: function.cpp:1007
static std::vector< SXElem > split(const SXElem &e, casadi_int n)
Definition: output_sx.hpp:139

References casadi::CallSX::create(), casadi::Function::nnz_out(), and casadi::OutputSX::split().

Referenced by casadi::SXFunction::ad_forward(), casadi::SXFunction::ad_reverse(), casadi::CallSX::eval_sx(), and casadi::SXFunction::eval_sx().

◆ ceil()

static SXElem casadi::GenericExpression< SXElem >::ceil ( const SXElem &  x)
inlinestaticinherited

Elementwise, towards plus infinity; sparsity preserved. The derivative is zero everywhere.

>>> ceil(DM([-1.5, 1.5]))
DM([-1, 2])

>> ceil(DM([-1.5; 1.5]))
[-1, 2]

julia> ceil.(ca.DM([-1.5, 1.5]))
DM([-1, 2])

See also
floor

Extra doc: https://github.com/casadi/casadi/wiki/L_pl

Definition at line 1789 of file generic_expression.hpp.

1789  {
1790  return ExType::unary(OP_CEIL, x);
1791  }

◆ constpow()

static SXElem casadi::GenericExpression< SXElem >::constpow ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Same value as pow, but the exponent is assumed constant: the derivative with respect to y is taken as zero. pow itself produces this operation for a non-integer constant exponent, so it is rarely needed directly. Passing a symbolic exponent gives a wrong derivative for MX; SX falls back to pow.

>>> constpow(DM(2), 3)
DM(8)
>>> x = MX.sym("x")
>>> constpow(x, 2.5), jacobian(constpow(x, 2.5), x)
(MX(pow(x,2.5)), MX((2.5*pow(x,1.5))))

>> constpow(DM(2), 3)
8
>> x = MX.sym('x');
>> constpow(x, 2.5), jacobian(constpow(x, 2.5), x)
pow(x,2.5)
(2.5*pow(x,1.5))

julia> ca.constpow(ca.DM(2), 3)
DM(8)
julia> x = ca.MX.sym("x");
julia> ca.constpow(x, 2.5), ca.jacobian(ca.constpow(x, 2.5), x)
(pow(x,2.5), (2.5*pow(x,1.5)))

See also
pow

Extra doc: https://github.com/casadi/casadi/wiki/L_2om

Definition at line 2467 of file generic_expression.hpp.

2467  {
2468  return ExType::binary(OP_CONSTPOW, x, y);
2469  }

◆ copysign()

static SXElem casadi::GenericExpression< SXElem >::copysign ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise value with the magnitude of x and the sign of y, as C copysign; a negative zero y counts as negative. Shape rules as for plus. The derivative with respect to y is zero.

>>> copysign(DM([1, -2]), DM([-1, 1]))
DM([-1, 2])
>>> copysign(DM(3), DM(-0.0))
DM(-3)
>>> copysign(SX.sym("x"), SX.sym("y"))
SX(copysign(x,y))

>> copysign(DM([1; -2]), DM([-1; 1]))
[-1, 2]
>> copysign(DM(3), DM(-0.0))
-3
>> copysign(SX.sym('x'), SX.sym('y'))
copysign(x,y)

julia> copysign.(ca.DM([1, -2]), ca.DM([-1, 1]))
DM([-1, 2])
julia> copysign.(ca.DM(3), ca.DM(-0.0))
DM(-3)
julia> ca.copysign(ca.SX.sym("x"), ca.SX.sym("y"))
SX(copysign(x,y))

See also
sign, abs

Extra doc: https://github.com/casadi/casadi/wiki/L_2ol

Definition at line 2417 of file generic_expression.hpp.

2417  {
2418  return ExType::binary(OP_COPYSIGN, x, y);
2419  }

◆ cos()

static SXElem casadi::GenericExpression< SXElem >::cos ( const SXElem &  x)
inlinestaticinherited

Elementwise; structural zeros become 1, so the result is dense.

>>> cos(DM.eye(2))
DM(
[[0.540302, 1],
 [1, 0.540302]])
>>> cos(SX.sym("s", Sparsity.diag(2)))
SX(@1=1,
[[cos(s_0), @1],
 [@1, cos(s_1)]])

>> cos(DM.eye(2))
[[0.540302, 1],
 [1, 0.540302]]
>> cos(SX.sym('s', Sparsity.diag(2)))
@1=1,
[[cos(s_0), @1],
 [@1, cos(s_1)]]

julia> cos.(ca.DM.eye(2))
DM(
[[0.540302, 1],
 [1, 0.540302]])
julia> cos.(ca.SX.sym("s", ca.Sparsity.diag(2)))
SX(@1=1,
[[cos(s_0), @1],
 [@1, cos(s_1)]])

See also
sin, tan, acos

Extra doc: https://github.com/casadi/casadi/wiki/L_p4

Definition at line 1112 of file generic_expression.hpp.

1112  {
1113  return ExType::unary(OP_COS, x);
1114  }

◆ cosh()

static SXElem casadi::GenericExpression< SXElem >::cosh ( const SXElem &  x)
inlinestaticinherited

Elementwise; the result is dense (cosh(0) == 1).

>>> cosh(DM([0, 1]))
DM([1, 1.54308])

>> cosh(DM([0; 1]))
[1, 1.54308]

julia> cosh.(ca.DM([0, 1]))
DM([1, 1.54308])

See also
acosh, sinh, tanh

Extra doc: https://github.com/casadi/casadi/wiki/L_pb

Definition at line 1372 of file generic_expression.hpp.

1372  {
1373  return ExType::unary(OP_COSH, x);
1374  }

◆ create()

SXElem casadi::SXElem::create ( SXNode *  node)
static

Create an object given a node

Definition at line 62 of file sx_elem.cpp.

62  {
63  return SXElem(node, false);
64  }
SXElem()
Default constructor (not-a-number)
Definition: sx_elem.cpp:53

References SXElem().

Referenced by casadi::CallSX::create(), casadi::UnarySX::create(), casadi::BinarySX::create(), deserialize(), casadi::CallSX::get_output(), casadi::SXFunction::init(), casadi::SXFunction::order(), and sym().

◆ dep()

SXElem casadi::SXElem::dep ( casadi_int  ch = 0) const

Definition at line 395 of file sx_elem.cpp.

395  {
396  casadi_assert_dev(ch >= 0 || ch < n_dep());
397  return node->dep(ch);
398  }
casadi_int n_dep() const
Get the number of dependencies of a binary SXElem.
Definition: sx_elem.cpp:400
virtual const SXElem & dep(casadi_int i) const
get the reference of a child
Definition: sx_node.cpp:80

References casadi::SXNode::dep(), and n_dep().

Referenced by binary(), is_doubled(), operator-(), and unary().

◆ deserialize()

SXElem casadi::SXElem::deserialize ( DeserializingStream &  s)
static

Definition at line 512 of file sx_elem.cpp.

512  {
514  }
static SXElem create(SXNode *node)
Definition: sx_elem.cpp:62
static SXNode * deserialize(DeserializingStream &s)
Definition: sx_node.cpp:250

References create(), and casadi::SXNode::deserialize().

◆ disp()

void casadi::SXElem::disp ( std::ostream &  stream,
bool  more = false 
) const

Definition at line 147 of file sx_elem.cpp.

147  {
148  node->disp(stream, more);
149  }
virtual void disp(std::ostream &stream, bool more) const
print
Definition: sx_node.cpp:88

References casadi::SXNode::disp().

◆ eq()

static SXElem casadi::GenericExpression< SXElem >::eq ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise comparison with 0/1 result, see lt: this is value equality of the entries, not identity of expressions; use is_equal to test whether two symbolic expressions are the same. For symbolic operands x == x with the same node simplifies to 1. NaN is not equal to anything, including itself.

>>> eq(DM([1, 2, 3]), 2)
DM([0, 1, 0])
>>> DM.eye(2) == DM([[1, 0], [0, 1]])
DM(
[[1, 1],
 [1, 1]])
>>> x = SX.sym("x")
>>> x == x, x == SX.sym("x")
(SX(1), SX((x==x)))
>>> DM(float("nan")) == DM(float("nan"))
DM(0)

>> eq(DM([1; 2; 3]), 2)
[0, 1, 0]
>> DM.eye(2) == DM([1 0; 0 1])
[[1, 1],
 [1, 1]]
>> x = SX.sym('x');
>> x == x, x == SX.sym('x')
1
(x==x)
>> DM(nan) == DM(nan)
0

julia> ca.eq(ca.DM([1, 2, 3]), 2)
DM([0, 1, 0])
julia> ca.DM.eye(2) == ca.DM([1 0; 0 1])
DM(
[[1, 1],
 [1, 1]])
julia> x = ca.SX.sym("x");
julia> x == x, x == ca.SX.sym("x")
(1, (x==x))
julia> ca.DM(NaN) == ca.DM(NaN)
DM(0)

See also
ne, is_equal

Extra doc: https://github.com/casadi/casadi/wiki/L_ov

Definition at line 641 of file generic_expression.hpp.

641  {
642  return ExType::binary(OP_EQ, x, y);
643  }

◆ erf()

static SXElem casadi::GenericExpression< SXElem >::erf ( const SXElem &  x)
inlinestaticinherited

Elementwise; sparsity preserved (erf(0) == 0). Range (-1, 1).

>>> erf(DM([0, 1]))
DM([0, 0.842701])

>> erf(DM([0; 1]))
[0, 0.842701]

julia> ca.erf(ca.DM([0, 1]))
DM([0, 0.842701])

See also
erfinv

Extra doc: https://github.com/casadi/casadi/wiki/L_pm

Definition at line 1826 of file generic_expression.hpp.

1826  {
1827  return ExType::unary(OP_ERF, x);
1828  }

◆ erfinv()

static SXElem casadi::GenericExpression< SXElem >::erfinv ( const SXElem &  x)
inlinestaticinherited

Elementwise; sparsity preserved. Defined on [-1, 1] with erfinv(+-1) infinite; arguments outside give NaN.

>>> erfinv(DM([0, 0.5, 1]))
DM([0, 0.476936, inf])

>> erfinv(DM([0; 0.5; 1]))
[0, 0.476936, inf]

julia> ca.erfinv(ca.DM([0, 0.5, 1]))
DM([0, 0.476936, inf])

See also
erf

Extra doc: https://github.com/casadi/casadi/wiki/L_pn

Definition at line 1864 of file generic_expression.hpp.

1864  {
1865  return ExType::unary(OP_ERFINV, x);
1866  }

◆ exp()

static SXElem casadi::GenericExpression< SXElem >::exp ( const SXElem &  x)
inlinestaticinherited

Elementwise; the result is dense (exp(0) == 1).

>>> exp(DM.eye(2))
DM(
[[2.71828, 1],
 [1, 2.71828]])
>>> exp(MX.sym("m", Sparsity.diag(2)))
MX(exp(dense(m)))

>> exp(DM.eye(2))
[[2.71828, 1],
 [1, 2.71828]]
>> exp(MX.sym('m', Sparsity.diag(2)))
exp(dense(m))

julia> exp.(ca.DM.eye(2))
DM(
[[2.71828, 1],
 [1, 2.71828]])
julia> exp.(ca.MX.sym("m", ca.Sparsity.diag(2)))
MX(exp(dense(m)))

See also
log, expm1

Extra doc: https://github.com/casadi/casadi/wiki/L_pf

Definition at line 1531 of file generic_expression.hpp.

1531  {
1532  return ExType::unary(OP_EXP, x);
1533  }

◆ expm1()

static SXElem casadi::GenericExpression< SXElem >::expm1 ( const SXElem &  x)
inlinestaticinherited

Elementwise; accurate for small x where exp(x) - 1 would lose digits. Sparsity preserved (expm1(0) == 0).

>>> expm1(DM([0, 1e-10]))
DM([0, 1e-10])

>> expm1(DM([0; 1e-10]))
[0, 1e-10]

julia> expm1.(ca.DM([0, 1e-10]))
DM([0, 1e-10])

See also
exp, log1p

Extra doc: https://github.com/casadi/casadi/wiki/L_pj

Definition at line 1701 of file generic_expression.hpp.

1701  {
1702  return ExType::unary(OP_EXPM1, x);
1703  }

◆ floor()

static SXElem casadi::GenericExpression< SXElem >::floor ( const SXElem &  x)
inlinestaticinherited

Elementwise, towards minus infinity; sparsity preserved. The derivative is zero everywhere. Floor division x // y in Python is floor(x / y).

>>> floor(DM([-1.5, 1.5]))
DM([-2, 1])
>>> DM([7, -7]) // 2
DM([3, -4])
>>> SX.sym("x") // 2
SX(floor((x/2)))

>> floor(DM([-1.5; 1.5]))
[-2, 1]
>> floor(DM([7; -7]) ./ 2)
[3, -4]
>> floor(SX.sym('x') / 2)
floor((x/2))

julia> floor.(ca.DM([-1.5, 1.5]))
DM([-2, 1])
julia> fld.(ca.DM([7, -7]), 2)
DM([3, -4])
julia> floor(ca.SX.sym("x") / 2)
SX(floor((x/2)))

See also
ceil, sign

Extra doc: https://github.com/casadi/casadi/wiki/L_pk

Definition at line 1751 of file generic_expression.hpp.

1751  {
1752  return ExType::unary(OP_FLOOR, x);
1753  }

◆ fmax()

static SXElem casadi::GenericExpression< SXElem >::fmax ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise maximum; shape rules as for plus. Follows C fmax: when one operand is NaN the other is returned. The derivative is 1 with respect to the larger operand (split evenly on ties). For the maximum over the entries of one matrix use mmax.

>>> fmax(DM([1, 5]), DM([3, 2]))
DM([3, 5])
>>> fmax(DM([1, 5]), 3)
DM([3, 5])
>>> fmax(DM([1, float("nan")]), DM([float("nan"), 2]))
DM([1, 2])
>>> fmax(MX.sym("x"), MX.sym("y"))
MX(fmax(x,y))

>> fmax(DM([1; 5]), DM([3; 2]))
[3, 5]
>> max(DM([1; 5]), 3)
[3, 5]
>> fmax(DM([1; nan]), DM([nan; 2]))
[1, 2]
>> max(MX.sym('x'), MX.sym('y'))
fmax(x,y)

julia> max.(ca.DM([1, 5]), ca.DM([3, 2]))
DM([3, 5])
julia> max.(ca.DM([1, 5]), 3)
DM([3, 5])
julia> max.(ca.DM([1, NaN]), ca.DM([NaN, 2]))
DM([1, 2])
julia> max(ca.MX.sym("x"), ca.MX.sym("y"))
MX(fmax(x,y))

See also
fmin, mmax

Extra doc: https://github.com/casadi/casadi/wiki/L_pu

Definition at line 2289 of file generic_expression.hpp.

2289  {
2290  return ExType::binary(OP_FMAX, x, y);
2291  }

◆ fmin()

static SXElem casadi::GenericExpression< SXElem >::fmin ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise minimum; shape rules as for plus. Follows C fmin: when one operand is NaN the other is returned, NaN results only when both are NaN. The derivative is 1 with respect to the smaller operand (split evenly on ties). The sparsity pattern is kept where both operands are structurally zero; fmin with a negative scalar densifies. For the minimum over the entries of one matrix use mmin.

>>> fmin(DM([1, 5]), DM([3, 2]))
DM([1, 2])
>>> fmin(DM([1, float("nan")]), DM([float("nan"), 2]))
DM([1, 2])
>>> fmin(DM.eye(2), 1)
DM(
[[1, 00],
 [00, 1]])
>>> fmin(SX.sym("x"), SX.sym("y"))
SX(fmin(x,y))

>> fmin(DM([1; 5]), DM([3; 2]))
[1, 2]
>> min(DM([1; 5]), DM([3; 2]))
[1, 2]
>> fmin(DM([1; nan]), DM([nan; 2]))
[1, 2]
>> fmin(DM.eye(2), 1)
[[1, 00],
 [00, 1]]
>> min(SX.sym('x'), SX.sym('y'))
fmin(x,y)

julia> min.(ca.DM([1, 5]), ca.DM([3, 2]))
DM([1, 2])
julia> min.(ca.DM([1, NaN]), ca.DM([NaN, 2]))
DM([1, 2])
julia> min.(ca.DM.eye(2), 1)
DM(
[[1, 00],
 [00, 1]])
julia> min(ca.SX.sym("x"), ca.SX.sym("y"))
SX(fmin(x,y))

See also
fmax, mmin

Extra doc: https://github.com/casadi/casadi/wiki/L_pt

Definition at line 2232 of file generic_expression.hpp.

2232  {
2233  return ExType::binary(OP_FMIN, x, y);
2234  }

◆ ge()

static SXElem casadi::GenericExpression< SXElem >::ge ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise comparison with 0/1 result, see lt. Implemented as y <= x.

>>> ge(DM([1, 2, 3]), 2)
DM([0, 1, 1])
>>> ge(MX.sym("x"), MX.sym("y"))
MX((y<=x))

>> ge(DM([1; 2; 3]), 2)
[0, 1, 1]
>> MX.sym('x') >= MX.sym('y')
(y<=x)

julia> ca.ge(ca.DM([1, 2, 3]), 2)
DM([0, 1, 1])
julia> ca.ge(ca.MX.sym("x"), ca.MX.sym("y"))
MX((y<=x))

See also
lt

Extra doc: https://github.com/casadi/casadi/wiki/L_ou

Definition at line 571 of file generic_expression.hpp.

571  {
572  return ExType::le(y, x);
573  }

◆ get()

SXNode * casadi::SXElem::get ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_10n

Definition at line 178 of file sx_elem.cpp.

178  {
179  return node;
180  }

Referenced by casadi::SXFunction::init(), is_equal(), and casadi::register_symbol().

◆ get_output() [1/2]

SXElem casadi::SXElem::get_output ( casadi_int  oind) const

Extra doc: https://github.com/casadi/casadi/wiki/L_29f

Definition at line 404 of file sx_elem.cpp.

404  {
405  return node->get_output(oind);
406  }
virtual SXElem get_output(casadi_int oind) const
Get an output.
Definition: sx_node.cpp:227

References casadi::SXNode::get_output().

Referenced by casadi::OutputSX::split().

◆ get_output() [2/2]

std::vector< SXElem > casadi::SXElem::get_output ( const std::vector< casadi_int > &  oind) const

Definition at line 408 of file sx_elem.cpp.

408  {
409  return node->get_output(oind);
410  }

References casadi::SXNode::get_output().

◆ get_temp()

int casadi::SXElem::get_temp ( ) const

Get the temporary variable

Definition at line 484 of file sx_elem.cpp.

484  {
485  return (*this)->temp;
486  }

◆ gt()

static SXElem casadi::GenericExpression< SXElem >::gt ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise comparison with 0/1 result, see lt. Implemented as y < x, which is how a symbolic expression prints.

>>> gt(DM([1, 2, 3]), 2)
DM([0, 0, 1])
>>> gt(SX.sym("x"), SX.sym("y"))
SX((y<x))

>> gt(DM([1; 2; 3]), 2)
[0, 0, 1]
>> SX.sym('x') > SX.sym('y')
(y<x)

julia> ca.gt(ca.DM([1, 2, 3]), 2)
DM([0, 0, 1])
julia> ca.gt(ca.SX.sym("x"), ca.SX.sym("y"))
SX((y<x))

See also
lt

Extra doc: https://github.com/casadi/casadi/wiki/L_ot

Definition at line 525 of file generic_expression.hpp.

525  {
526  return ExType::lt(y, x);
527  }

◆ has_output()

bool casadi::SXElem::has_output ( ) const

Definition at line 291 of file sx_elem.cpp.

291  {
292  return node->has_output();
293  }
virtual bool has_output() const
check properties of a node
Definition: sx_node.hpp:84

References casadi::SXNode::has_output().

◆ hypot()

static SXElem casadi::GenericExpression< SXElem >::hypot ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise; avoids the overflow and underflow of forming the squares, as C hypot. Shape rules as for plus; the result is structurally zero where both operands are.

>>> hypot(DM(3), DM(4))
DM(5)
>>> hypot(DM(1e200), DM(1e200))
DM(1.41421e+200)
>>> hypot(SX.sym("x"), SX.sym("y"))
SX(hypot(x,y))

>> hypot(DM(3), DM(4))
5
>> hypot(DM(1e200), DM(1e200))
1.41421e+200
>> hypot(SX.sym('x'), SX.sym('y'))
hypot(x,y)

julia> hypot(ca.DM(3), ca.DM(4))
DM(5)
julia> hypot(ca.DM(1e200), ca.DM(1e200))
DM(1.41421e+200)
julia> hypot(ca.SX.sym("x"), ca.SX.sym("y"))
SX(hypot(x,y))

See also
norm_2

Extra doc: https://github.com/casadi/casadi/wiki/L_pw

Definition at line 2565 of file generic_expression.hpp.

2565  {
2566  return ExType::binary(OP_HYPOT, x, y);
2567  }

◆ if_else_zero()

static SXElem casadi::GenericExpression< SXElem >::if_else_zero ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise: y where x is nonzero, 0 elsewhere; the result is structurally zero wherever either operand is. The value of y in the inactive branch does not propagate, so a NaN or inf there yields 0 rather than NaN, and the derivatives are treated accordingly. This is the building block of if_else; it is not exposed as a function in Python, use if_else(x, y, 0) there.

See also
if_else

Extra doc: https://github.com/casadi/casadi/wiki/L_ps

Definition at line 2166 of file generic_expression.hpp.

2166  {
2167  return ExType::binary(OP_IF_ELSE_ZERO, x, y);
2168  }

◆ inv()

SXElem casadi::SXElem::inv ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_10y

Definition at line 174 of file sx_elem.cpp.

174  {
175  return unary(OP_INV, *this);
176  }
@ OP_INV
Definition: calculus.hpp:73

References casadi::OP_INV, and unary().

◆ is_almost_zero()

bool casadi::SXElem::is_almost_zero ( double  tol) const

Definition at line 315 of file sx_elem.cpp.

315  {
316  return node->is_almost_zero(tol);
317  }
virtual bool is_almost_zero(double tol) const
check properties of a node
Definition: sx_node.hpp:74

References casadi::SXNode::is_almost_zero().

Referenced by casadi::casadi_limits< SXElem >::is_almost_zero().

◆ is_boolean()

bool casadi::SXElem::is_boolean ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_2kj

Definition at line 370 of file sx_elem.cpp.

370  {
371  if (is_constant()) {
372  double v = static_cast<double>(*this);
373  return v==0 || v==1;
374  } else {
375  return operation_checker<BooleanChecker>(node->op());
376  }
377  }
virtual casadi_int op() const =0
get the operation

References is_constant(), and casadi::SXNode::op().

Referenced by unary().

◆ is_call()

bool casadi::SXElem::is_call ( ) const

Definition at line 283 of file sx_elem.cpp.

283  {
284  return node->is_call();
285  }
virtual bool is_call() const
check properties of a node
Definition: sx_node.hpp:82

References casadi::SXNode::is_call().

◆ is_commutative()

bool casadi::SXElem::is_commutative ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_10s

Definition at line 274 of file sx_elem.cpp.

274  {
275  casadi_assert(n_dep(), "SX::is_commutative: must be binary");
276  return operation_checker<CommChecker>(op());
277  }

References n_dep(), and op().

◆ is_constant()

bool casadi::SXElem::is_constant ( ) const

◆ is_doubled()

bool casadi::SXElem::is_doubled ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_10u

Definition at line 169 of file sx_elem.cpp.

169  {
170  return (is_op(OP_ADD) && is_equal(dep(0), dep(1), SXNode::eq_depth_)) ||
171  (is_op(OP_TWICE));
172  }

References dep(), casadi::SXNode::eq_depth_, is_equal(), is_op(), casadi::OP_ADD, and casadi::OP_TWICE.

◆ is_equal()

bool casadi::SXElem::is_equal ( const SXElem &  x,
const SXElem &  y,
casadi_int  depth = 0 
)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_10z

Definition at line 359 of file sx_elem.cpp.

359  {
360  SXNode *x_node = x.get(), *y_node = y.get();
361  if (x_node==y_node) {
362  return true;
363  } else if (depth>0) {
364  return x_node->is_equal(y_node, depth);
365  } else {
366  return false;
367  }
368  }
SXNode * get() const
Get a pointer to the node.
Definition: sx_elem.cpp:178
virtual bool is_equal(const SXNode *node, casadi_int depth) const
Check if two nodes are equivalent up to a given depth.
Definition: sx_node.cpp:72

References get(), and casadi::SXNode::is_equal().

Referenced by assignIfDuplicate(), binary(), casadi::SXFunction::eval_sx(), is_doubled(), casadi::casadi_limits< SXElem >::is_equal(), casadi::BinarySX::is_equal(), and casadi::UnarySX::is_equal().

◆ is_half()

bool casadi::SXElem::is_half ( ) const

Definition at line 327 of file sx_elem.cpp.

327  {
328  return node->is_half();
329  }
virtual bool is_half() const
check properties of a node
Definition: sx_node.hpp:77

References casadi::SXNode::is_half().

Referenced by casadi::casadi_limits< SXElem >::is_half().

◆ is_inf()

bool casadi::SXElem::is_inf ( ) const

Definition at line 339 of file sx_elem.cpp.

339  {
340  return node->is_inf();
341  }
virtual bool is_inf() const
check properties of a node
Definition: sx_node.hpp:80

References casadi::SXNode::is_inf().

Referenced by casadi::casadi_limits< SXElem >::is_inf(), casadi::Matrix< Scalar >::is_regular(), and is_regular().

◆ is_integer()

bool casadi::SXElem::is_integer ( ) const

Definition at line 303 of file sx_elem.cpp.

303  {
304  return node->is_integer();
305  }
virtual bool is_integer() const
check properties of a node
Definition: sx_node.hpp:70

References casadi::SXNode::is_integer().

Referenced by casadi::casadi_limits< SXElem >::is_integer().

◆ is_leaf()

bool casadi::SXElem::is_leaf ( ) const

An SXElem qualifies as leaf when it has no dependencies.

Extra doc: https://github.com/casadi/casadi/wiki/L_10r

Definition at line 269 of file sx_elem.cpp.

269  {
270  if (!node) return true;
271  return is_constant() || is_symbolic();
272  }
bool is_symbolic() const
Definition: sx_elem.cpp:307

References is_constant(), and is_symbolic().

◆ is_minus_inf()

bool casadi::SXElem::is_minus_inf ( ) const

Definition at line 343 of file sx_elem.cpp.

343  {
344  return node->is_minus_inf();
345  }
virtual bool is_minus_inf() const
check properties of a node
Definition: sx_node.hpp:81

References casadi::SXNode::is_minus_inf().

Referenced by casadi::casadi_limits< SXElem >::is_minus_inf(), casadi::Matrix< Scalar >::is_regular(), and is_regular().

◆ is_minus_one()

bool casadi::SXElem::is_minus_one ( ) const

Definition at line 323 of file sx_elem.cpp.

323  {
324  return node->is_minus_one();
325  }
virtual bool is_minus_one() const
check properties of a node
Definition: sx_node.hpp:76

References casadi::SXNode::is_minus_one().

Referenced by casadi::casadi_limits< SXElem >::is_minus_one(), and operator-().

◆ is_nan()

bool casadi::SXElem::is_nan ( ) const

Definition at line 335 of file sx_elem.cpp.

335  {
336  return node->is_nan();
337  }
virtual bool is_nan() const
check properties of a node
Definition: sx_node.hpp:79

References casadi::SXNode::is_nan().

Referenced by casadi::casadi_limits< SXElem >::is_nan(), casadi::Matrix< Scalar >::is_regular(), and is_regular().

◆ is_nonnegative()

bool casadi::SXElem::is_nonnegative ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_10t

Definition at line 379 of file sx_elem.cpp.

379  {
380  if (is_constant()) {
381  return static_cast<double>(*this)>=0;
382  } else {
383  return operation_checker<NonnegativeChecker>(node->op());
384  }
385  }

References is_constant(), and casadi::SXNode::op().

Referenced by casadi::casadi_limits< SXElem >::is_nonnegative().

◆ is_null()

bool casadi::SXElem::is_null ( )
inline

Extra doc: https://github.com/casadi/casadi/wiki/L_112

Definition at line 291 of file sx_elem.hpp.

291 {return false;}

◆ is_one()

bool casadi::SXElem::is_one ( ) const

Definition at line 319 of file sx_elem.cpp.

319  {
320  return node->is_one();
321  }
virtual bool is_one() const
check properties of a node
Definition: sx_node.hpp:75

References casadi::SXNode::is_one().

Referenced by casadi::casadi_limits< SXElem >::is_one(), and operator-().

◆ is_op()

bool casadi::SXElem::is_op ( casadi_int  op) const

Definition at line 355 of file sx_elem.cpp.

355  {
356  return node->is_op(op);
357  }
virtual bool is_op(casadi_int op) const
check properties of a node
Definition: sx_node.hpp:73

References casadi::SXNode::is_op(), and op().

Referenced by binary(), is_doubled(), operator-(), and unary().

◆ is_output()

bool casadi::SXElem::is_output ( ) const

Definition at line 287 of file sx_elem.cpp.

287  {
288  return node->is_output();
289  }
virtual bool is_output() const
check properties of a node
Definition: sx_node.hpp:83

References casadi::SXNode::is_output().

◆ is_regular()

bool casadi::SXElem::is_regular ( ) const

Definition at line 500 of file sx_elem.cpp.

500  {
501  if (is_constant()) {
502  return !(is_nan() || is_inf() || is_minus_inf());
503  } else {
504  casadi_error("Cannot check regularity for symbolic SXElem");
505  }
506  }
bool is_nan() const
Definition: sx_elem.cpp:335
bool is_minus_inf() const
Definition: sx_elem.cpp:343
bool is_inf() const
Definition: sx_elem.cpp:339

References is_constant(), is_inf(), is_minus_inf(), and is_nan().

◆ is_symbolic()

bool casadi::SXElem::is_symbolic ( ) const

Definition at line 307 of file sx_elem.cpp.

307  {
308  return node->is_symbolic();
309  }
virtual bool is_symbolic() const
check properties of a node
Definition: sx_node.hpp:71

References casadi::SXNode::is_symbolic().

Referenced by is_leaf(), and casadi::register_symbol().

◆ is_value()

bool casadi::SXElem::is_value ( double  val) const

Definition at line 331 of file sx_elem.cpp.

331  {
332  return node->is_value(val);
333  }
virtual bool is_value(double val) const
check properties of a node
Definition: sx_node.hpp:78

References casadi::SXNode::is_value().

Referenced by casadi::casadi_limits< SXElem >::is_value().

◆ is_zero()

bool casadi::SXElem::is_zero ( ) const

Definition at line 311 of file sx_elem.cpp.

311  {
312  return node->is_zero();
313  }
virtual bool is_zero() const
check properties of a node
Definition: sx_node.hpp:72

References casadi::SXNode::is_zero().

Referenced by __nonzero__(), binary(), casadi::casadi_limits< SXElem >::is_zero(), and operator-().

◆ le()

static SXElem casadi::GenericExpression< SXElem >::le ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise comparison with 0/1 result, see lt. Chained inequalities such as lb <= (x <= ub) are recognised by Opti as bound constraints.

>>> le(DM([1, 2, 3]), 2)
DM([1, 1, 0])
>>> x = SX.sym("x")
>>> x <= x
SX(1)

>> le(DM([1; 2; 3]), 2)
[1, 1, 0]
>> x = SX.sym('x');
>> x <= x
1

julia> ca.le(ca.DM([1, 2, 3]), 2)
DM([1, 1, 0])
julia> x = ca.SX.sym("x");
julia> x <= x
SX(1)

See also
lt

Extra doc: https://github.com/casadi/casadi/wiki/L_os

Definition at line 478 of file generic_expression.hpp.

478  {
479  return ExType::binary(OP_LE, x, y);
480  }

◆ log()

static SXElem casadi::GenericExpression< SXElem >::log ( const SXElem &  x)
inlinestaticinherited

Elementwise; the result is dense with -inf at structural zeros. Negative arguments give NaN; no error is raised.

>>> log(DM([1, 0]))
DM([0, -inf])
>>> log(SX.sym("x"))
SX(log(x))

>> log(DM([1; 0]))
[0, -inf]
>> log(SX.sym('x'))
log(x)

julia> log.(ca.DM([1, 0]))
DM([0, -inf])
julia> log(ca.SX.sym("x"))
SX(log(x))

See also
exp, log10, log1p

Extra doc: https://github.com/casadi/casadi/wiki/L_pg

Definition at line 1575 of file generic_expression.hpp.

1575  {
1576  return ExType::unary(OP_LOG, x);
1577  }

◆ log10()

static SXElem casadi::GenericExpression< SXElem >::log10 ( const SXElem &  x)
inlinestaticinherited

Computed as log(x) * (1 / log(10)); there is no dedicated operation, which is visible in symbolic expressions. Dense result like log.

>>> log10(DM([1, 10, 1000]))
DM([0, 1, 3])
>>> log10(SX.sym("x"))
SX((0.434294*log(x)))

>> log10(DM([1; 10; 1000]))
[0, 1, 3]
>> log10(SX.sym('x'))
(0.434294*log(x))

julia> log10.(ca.DM([1, 10, 1000]))
DM([0, 1, 3])
julia> log10(ca.SX.sym("x"))
SX((0.434294*log(x)))

See also
log

Extra doc: https://github.com/casadi/casadi/wiki/L_ph

Definition at line 1619 of file generic_expression.hpp.

1619  {
1620  return log(x)*(1/std::log(10.));
1621  }
static SXElem log(const SXElem &x)
Natural logarithm: x -> log(x)

◆ log1p()

static SXElem casadi::GenericExpression< SXElem >::log1p ( const SXElem &  x)
inlinestaticinherited

Elementwise; accurate for small x where log(1 + x) would lose digits. Sparsity preserved (log1p(0) == 0).

>>> log1p(DM([0, 1e-10]))
DM([0, 1e-10])
>>> log1p(SX.sym("x"))
SX(log1p(x))

>> log1p(DM([0; 1e-10]))
[0, 1e-10]
>> log1p(SX.sym('x'))
log1p(x)

julia> log1p.(ca.DM([0, 1e-10]))
DM([0, 1e-10])
julia> log1p(ca.SX.sym("x"))
SX(log1p(x))

See also
log, expm1

Extra doc: https://github.com/casadi/casadi/wiki/L_pi

Definition at line 1663 of file generic_expression.hpp.

1663  {
1664  return ExType::unary(OP_LOG1P, x);
1665  }

◆ logic_and()

static SXElem casadi::GenericExpression< SXElem >::logic_and ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Returns (an expression evaluating to) 1 where both entries are nonzero and 0 otherwise, elementwise

Any nonzero value, including NaN, counts as true. Shape rules as for plus; the result is structurally zero wherever either operand is. The Python keyword and does not work on matrices (it needs a single truth value); use this function.

 >>> logic_and(DM([1, 2, 0]), DM([3, 0, 0]))
 DM([1, 0, 0])
 >>> logic_and(SX.sym("x"), SX.sym("y"))
 SX((x&&y))
 >>> logic_and(DM.eye(2), DM.ones(2, 2))
 DM(
 [[1, 00],
  [00, 1]])

 >> DM([1; 2; 0]) & DM([3; 0; 0])
 [1, 0, 0]
 >> SX.sym('x') & SX.sym('y')
 (x&&y)
 >> and(DM.eye(2), DM.ones(2, 2))
 [[1, 00],
  [00, 1]]

 julia> ca.logic_and(ca.DM([1, 2, 0]), ca.DM([3, 0, 0]))
 DM([1, 0, 0])
 julia> ca.logic_and(ca.SX.sym("x"), ca.SX.sym("y"))
 SX((x&&y))
 julia> ca.logic_and(ca.DM.eye(2), ca.DM.ones(2, 2))
 DM(
 [[1, 00],
  [00, 1]])

See also
logic_or, logic_not

Extra doc: https://github.com/casadi/casadi/wiki/L_ox

Definition at line 753 of file generic_expression.hpp.

753  {
754  return ExType::binary(OP_AND, x, y);
755  }

◆ logic_not()

static SXElem casadi::GenericExpression< SXElem >::logic_not ( const SXElem &  x)
inlinestaticinherited

Returns (an expression evaluating to) 1 where the entry is zero and 0 otherwise, elementwise

Structural zeros map to 1, so a sparse input gives a dense result. The Python keyword not does not work on matrices; use this function.

>>> logic_not(DM([1, 0, -1]))
DM([0, 1, 0])
>>> logic_not(DM.eye(2))
DM(
[[0, 1],
 [1, 0]])
>>> logic_not(SX.sym("x"))
SX((!x))

>> ~DM([1; 0; -1])
[0, 1, 0]
>> not(DM.eye(2))
[[0, 1],
 [1, 0]]
>> ~SX.sym('x')
(!x)

julia> ca.logic_not(ca.DM([1, 0, -1]))
DM([0, 1, 0])
julia> ca.logic_not(ca.DM.eye(2))
DM(
[[0, 1],
 [1, 0]])
julia> ca.logic_not(ca.SX.sym("x"))
SX((!x))

See also
logic_and, logic_or

Extra doc: https://github.com/casadi/casadi/wiki/L_oz

Definition at line 863 of file generic_expression.hpp.

863  {
864  return ExType::unary(OP_NOT, x);
865  }

◆ logic_or()

static SXElem casadi::GenericExpression< SXElem >::logic_or ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

returns (an expression evaluating to) 1 where at least one entry is nonzero and 0 otherwise, elementwise

Shape rules as for plus; the result is structurally zero where both operands are.

 >>> logic_or(DM([1, 0, 0]), DM([0, 0, 2]))
 DM([1, 0, 1])
 >>> logic_or(MX.sym("x"), MX.sym("y"))
 MX((x||y))

 >> DM([1; 0; 0]) | DM([0; 0; 2])
 [1, 0, 1]
 >> or(MX.sym('x'), MX.sym('y'))
 (x||y)

 julia> ca.logic_or(ca.DM([1, 0, 0]), ca.DM([0, 0, 2]))
 DM([1, 0, 1])
 julia> ca.logic_or(ca.MX.sym("x"), ca.MX.sym("y"))
 MX((x||y))

See also
logic_and, logic_not

Extra doc: https://github.com/casadi/casadi/wiki/L_oy

Definition at line 802 of file generic_expression.hpp.

802  {
803  return ExType::binary(OP_OR, x, y);
804  }

◆ lt()

static SXElem casadi::GenericExpression< SXElem >::lt ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise comparison; shape rules as for plus. Every comparison in this group returns a matrix of the same type as the operands with entries 1 (true) and 0 (false): for DM a numeric 0/1 matrix, not a boolean, and for SX and MX a symbolic expression that is only decided at evaluation time. Comparisons with NaN are false. The result is sparse where both operands are structurally zero (0 < 0 is false).

>>> lt(DM([1, 2, 3]), 2)
DM([1, 0, 0])
>>> DM([1, 2, 3]) < DM([3, 2, 1])
DM([1, 0, 0])
>>> SX.sym("x", 2) < SX.sym("y", 2)
SX([(x_0<y_0), (x_1<y_1)])
>>> DM.eye(2) < 1
DM(
[[0, 1],
 [1, 0]])

>> lt(DM([1; 2; 3]), 2)
[1, 0, 0]
>> DM([1; 2; 3]) < DM([3; 2; 1])
[1, 0, 0]
>> SX.sym('x', 2) < SX.sym('y', 2)
[(x_0<y_0), (x_1<y_1)]
>> DM.eye(2) < 1
[[0, 1],
 [1, 0]]

julia> ca.lt(ca.DM([1, 2, 3]), 2)
DM([1, 0, 0])
julia> ca.DM([1, 2, 3]) < ca.DM([3, 2, 1])
DM([1, 0, 0])
julia> ca.SX.sym("x", 2) < ca.SX.sym("y", 2)
SX([(x_0<y_0), (x_1<y_1)])
julia> ca.DM.eye(2) < 1
DM(
[[0, 1],
 [1, 0]])

See also
le, gt, ge, eq, ne, if_else

Extra doc: https://github.com/casadi/casadi/wiki/L_or

Definition at line 428 of file generic_expression.hpp.

428  {
429  return ExType::binary(OP_LT, x, y);
430  }

◆ mark()

void casadi::SXElem::mark ( ) const

Definition at line 496 of file sx_elem.cpp.

496  {
497  (*this)->mark();
498  }

◆ marked()

bool casadi::SXElem::marked ( ) const

Definition at line 492 of file sx_elem.cpp.

492  {
493  return (*this)->marked();
494  }

◆ minus()

static SXElem casadi::GenericExpression< SXElem >::minus ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise difference; shape and sparsity rules as for plus. For symbolic arguments, x - x with the same node simplifies to 0.

>>> minus(DM([5, 6]), DM([1, 2]))
DM([4, 4])
>>> DM.eye(2) - 1
DM(
[[0, -1],
 [-1, 0]])
>>> x = SX.sym("x")
>>> x - x
SX(0)

>> minus(DM([5; 6]), DM([1; 2]))
[4, 4]
>> DM.eye(2) - 1
[[0, -1],
 [-1, 0]]
>> x = SX.sym('x');
>> x - x
0

julia> ca.minus(ca.DM([5, 6]), ca.DM([1, 2]))
DM([4, 4])
julia> ca.DM.eye(2) - 1
DM(
[[0, -1],
 [-1, 0]])
julia> x = ca.SX.sym("x");
julia> x - x
SX(0)

Extra doc: https://github.com/casadi/casadi/wiki/L_oo

Definition at line 221 of file generic_expression.hpp.

221  {
222  return ExType::binary(OP_SUB, x, y);
223  }

◆ mod()

static SXElem casadi::GenericExpression< SXElem >::mod ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

This Function follows the convention of https://en.cppreference.com/w/c/numeric/math/fmod: the result has the sign of x and magnitude below |y|.

This is equivalent to Python's numpy.fmod and Matlab's rem. It differs from the Python operator x % y on these objects, which follows the Python convention x - y*floor(x/y) (result with the sign of y). Elementwise, shape rules as for plus; structural zeros of x stay zero. A zero divisor gives NaN.

>>> fmod(DM([5, 5, -5, -5]), DM([3, -3, 3, -3]))
DM([2, 2, -2, -2])
>>> DM([5, 5, -5, -5]) % DM([3, -3, 3, -3])
DM([2, -1, 1, -2])
>>> fmod(SX.sym("x"), 2), SX.sym("x") % 2
(SX(fmod(x,2)), SX((x-(2.*floor((x/2))))))

>> x = DM([5; 5; -5; -5]); y = DM([3; -3; 3; -3]);
>> fmod(x, y)
[2, 2, -2, -2]
>> rem(x, y)
[2, 2, -2, -2]
>> x - y .* floor(x ./ y)
[2, -1, 1, -2]
>> rem(SX.sym('x'), 2)
fmod(x,2)

julia> ca.fmod(ca.DM([5, 5, -5, -5]), ca.DM([3, -3, 3, -3]))
DM([2, 2, -2, -2])
julia> mod.(ca.DM([5, 5, -5, -5]), ca.DM([3, -3, 3, -3]))
DM([2, -1, 1, -2])
julia> ca.fmod(ca.SX.sym("x"), 2)
SX(fmod(x,2))

See also
remainder, floor
Extra doc: https://github.com/casadi/casadi/wiki/L_pq 

Definition at line 2036 of file generic_expression.hpp.

2036  {
2037  return ExType::binary(OP_FMOD, x, y);
2038  }

◆ n_dep()

casadi_int casadi::SXElem::n_dep ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_10v

Definition at line 400 of file sx_elem.cpp.

400  {
401  return node->n_dep();
402  }
virtual casadi_int n_dep() const
Number of dependencies.
Definition: sx_node.hpp:126

References casadi::SXNode::n_dep().

Referenced by dep(), and is_commutative().

◆ name()

const std::string & casadi::SXElem::name ( ) const

Definition at line 347 of file sx_elem.cpp.

347  {
348  return node->name();
349  }
virtual const std::string & name() const
Definition: sx_node.cpp:76

References casadi::SXNode::name().

Referenced by sym().

◆ ne()

static SXElem casadi::GenericExpression< SXElem >::ne ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise comparison with 0/1 result, see lt and eq. For symbolic operands x != x with the same node simplifies to 0, so it cannot be used to detect NaN symbolically.

>>> ne(DM([1, 2, 3]), 2)
DM([1, 0, 1])
>>> x = MX.sym("x")
>>> x != x
MX(0)

>> ne(DM([1; 2; 3]), 2)
[1, 0, 1]
>> x = MX.sym('x');
>> x ~= x
0

julia> ca.ne(ca.DM([1, 2, 3]), 2)
DM([1, 0, 1])
julia> x = ca.MX.sym("x");
julia> x != x
MX(0)

See also
eq

Extra doc: https://github.com/casadi/casadi/wiki/L_ow

Definition at line 691 of file generic_expression.hpp.

691  {
692  return ExType::binary(OP_NE, x, y);
693  }

◆ op()

casadi_int casadi::SXElem::op ( ) const

Definition at line 351 of file sx_elem.cpp.

351  {
352  return node->op();
353  }

References casadi::SXNode::op().

Referenced by binary(), is_commutative(), is_op(), and unary().

◆ operator casadi_int()

casadi::SXElem::operator casadi_int ( ) const
explicit

Definition at line 391 of file sx_elem.cpp.

391  {
392  return node->to_int();
393  }
virtual casadi_int to_int() const
Get value of a constant node.
Definition: sx_node.cpp:60

◆ operator double()

casadi::SXElem::operator double ( ) const
explicit

Definition at line 387 of file sx_elem.cpp.

387  {
388  return node->to_double();
389  }
virtual double to_double() const
Get value of a constant node.
Definition: sx_node.cpp:56

◆ operator Matrix< SXElem >()

casadi::SXElem::operator Matrix< SXElem > ( ) const

◆ operator!()

SXElem casadi::GenericExpression< SXElem >::operator! ( ) const
inlineinherited

Returns (an expression evaluating to) 1 where the entry is zero and 0 otherwise, elementwise

Structural zeros map to 1, so a sparse input gives a dense result. The Python keyword not does not work on matrices; use this function.

>>> logic_not(DM([1, 0, -1]))
DM([0, 1, 0])
>>> logic_not(DM.eye(2))
DM(
[[0, 1],
 [1, 0]])
>>> logic_not(SX.sym("x"))
SX((!x))

>> ~DM([1; 0; -1])
[0, 1, 0]
>> not(DM.eye(2))
[[0, 1],
 [1, 0]]
>> ~SX.sym('x')
(!x)

julia> ca.logic_not(ca.DM([1, 0, -1]))
DM([0, 1, 0])
julia> ca.logic_not(ca.DM.eye(2))
DM(
[[0, 1],
 [1, 0]])
julia> ca.logic_not(ca.SX.sym("x"))
SX((!x))

See also
logic_and, logic_or

Extra doc: https://github.com/casadi/casadi/wiki/L_oz

Definition at line 869 of file generic_expression.hpp.

869  {
870  return logic_not(self());
871  }
static SXElem logic_not(const SXElem &x)
Logical not x -> !x.

◆ operator*=()

SXElem & casadi::GenericExpression< SXElem >::operator*= ( const SXElem &  y)
inlineinherited

Elementwise (Hadamard) product, not the matrix product; use mtimes for that. Shape rules as for plus. The result is structurally zero wherever either operand is, so the sparsity pattern is the intersection of the patterns.

>>> times(DM([1, 2]), DM([3, 4]))
DM([3, 8])
>>> DM([[1, 2], [3, 4]]) * DM([[1, 2], [3, 4]])
DM(
[[1, 4],
 [9, 16]])
>>> DM.eye(2) * DM.ones(2, 2)
DM(
[[1, 00],
 [00, 1]])

>> times(DM([1; 2]), DM([3; 4]))
[3, 8]
>> DM([1 2; 3 4]) .* DM([1 2; 3 4])
[[1, 4],
 [9, 16]]
>> DM([1 2; 3 4]) * DM([1 2; 3 4])
[[7, 10],
 [15, 22]]
>> DM.eye(2) .* DM.ones(2, 2)
[[1, 00],
 [00, 1]]

julia> ca.times(ca.DM([1, 2]), ca.DM([3, 4]))
DM([3, 8])
julia> ca.DM([1 2; 3 4]) .* ca.DM([1 2; 3 4])
DM(
[[1, 4],
 [9, 16]])
julia> ca.DM.eye(2) .* ca.DM.ones(2, 2)
DM(
[[1, 00],
 [00, 1]])

See also
mtimes

Extra doc: https://github.com/casadi/casadi/wiki/L_op

Definition at line 298 of file generic_expression.hpp.

298 {return self() = self() * y;}

◆ operator+=()

SXElem & casadi::GenericExpression< SXElem >::operator+= ( const SXElem &  y)
inlineinherited

Definition at line 170 of file generic_expression.hpp.

170 { return self() = self() + y; }

◆ operator-()

SXElem casadi::SXElem::operator- ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_10x

Definition at line 151 of file sx_elem.cpp.

151  {
152  if (is_op(OP_NEG))
153  return dep();
154  else if (is_zero())
155  return 0;
156  else if (is_minus_one())
157  return 1;
158  else if (is_one())
159  return -1;
160  else
161  return unary(OP_NEG, *this);
162  }
bool is_minus_one() const
Definition: sx_elem.cpp:323
bool is_one() const
Definition: sx_elem.cpp:319
@ OP_NEG
Definition: calculus.hpp:66

References dep(), is_minus_one(), is_one(), is_op(), is_zero(), casadi::OP_NEG, and unary().

◆ operator-=()

SXElem & casadi::GenericExpression< SXElem >::operator-= ( const SXElem &  y)
inlineinherited

Elementwise difference; shape and sparsity rules as for plus. For symbolic arguments, x - x with the same node simplifies to 0.

>>> minus(DM([5, 6]), DM([1, 2]))
DM([4, 4])
>>> DM.eye(2) - 1
DM(
[[0, -1],
 [-1, 0]])
>>> x = SX.sym("x")
>>> x - x
SX(0)

>> minus(DM([5; 6]), DM([1; 2]))
[4, 4]
>> DM.eye(2) - 1
[[0, -1],
 [-1, 0]]
>> x = SX.sym('x');
>> x - x
0

julia> ca.minus(ca.DM([5, 6]), ca.DM([1, 2]))
DM([4, 4])
julia> ca.DM.eye(2) - 1
DM(
[[0, -1],
 [-1, 0]])
julia> x = ca.SX.sym("x");
julia> x - x
SX(0)

Extra doc: https://github.com/casadi/casadi/wiki/L_oo

Definition at line 230 of file generic_expression.hpp.

230 { return self() = self() - y; }

◆ operator->() [1/2]

SXNode * casadi::SXElem::operator-> ( )

Definition at line 186 of file sx_elem.cpp.

186  {
187  return node;
188  }

◆ operator->() [2/2]

const SXNode * casadi::SXElem::operator-> ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_10o

Definition at line 182 of file sx_elem.cpp.

182  {
183  return node;
184  }

◆ operator/=()

SXElem & casadi::GenericExpression< SXElem >::operator/= ( const SXElem &  y)
inlineinherited

Elementwise quotient, not a linear solve; use solve or mldivide for that. Shape rules as for plus. Division by zero follows IEEE arithmetic (inf or nan), no error is raised. Structural zeros of x stay structural zeros; dividing by a sparse y densifies.

>>> rdivide(DM([1, 2]), 4)
DM([0.25, 0.5])
>>> DM([1, 2]) / DM([4, 8])
DM([0.25, 0.25])
>>> 1 / DM.eye(2)
DM(
[[1, inf],
 [inf, 1]])
>>> DM.eye(2) / 2
DM(
[[0.5, 00],
 [00, 0.5]])

>> rdivide(DM([1; 2]), 4)
[0.25, 0.5]
>> DM([1; 2]) ./ DM([4; 8])
[0.25, 0.25]
>> 1 ./ DM.eye(2)
[[1, inf],
 [inf, 1]]
>> DM.eye(2) ./ 2
[[0.5, 00],
 [00, 0.5]]

julia> ca.rdivide(ca.DM([1, 2]), 4)
DM([0.25, 0.5])
julia> ca.DM([1, 2]) ./ ca.DM([4, 8])
DM([0.25, 0.25])
julia> 1 ./ ca.DM.eye(2)
DM(
[[1, inf],
 [inf, 1]])
julia> ca.DM.eye(2) / 2
DM(
[[0.5, 00],
 [00, 0.5]])

See also
mldivide, mrdivide, solve

Extra doc: https://github.com/casadi/casadi/wiki/L_oq

Definition at line 369 of file generic_expression.hpp.

369 {return self() = self() / y;}

◆ operator=() [1/2]

SXElem & casadi::SXElem::operator= ( const SXElem &  scalar)

Definition at line 103 of file sx_elem.cpp.

103  {
104  if (this == &scalar) return *this;
105 
106  // quick return if the old and new pointers point to the same object
107  if (node == scalar.node) return *this;
108 
109  // decrease the counter and delete if this was the last pointer
110  if (--node->count == 0) delete node;
111 
112  // save the new pointer
113  node = scalar.node;
114  node->count++;
115  return *this;
116  }

References casadi::SXNode::count.

◆ operator=() [2/2]

SXElem & casadi::SXElem::operator= ( double  scalar)

Definition at line 143 of file sx_elem.cpp.

143  {
144  return *this = SXElem(scalar);
145  }

References SXElem().

◆ plus()

static SXElem casadi::GenericExpression< SXElem >::plus ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise sum. The shape rules below hold for every binary operation in this group (minus, times, rdivide, the comparisons, logic_and, ...):

  • both operands have the same shape, or
  • one operand is a scalar (1-by-1), which is combined with every element, or
  • the operands have the same number of rows and the column count of one is a multiple of the other's; the narrower one is repeated horizontally (so a column vector can be added to every column of a matrix, but a row vector cannot be added to every row). Any other combination raises an error; there is no further broadcasting. Empty matrices follow the same rules and give an empty result.

The result is sparse where both operands are structurally zero; an entry that is a structural zero in only one operand becomes an explicit value. Operations that map (0, y) or (x, 0) to a nonzero, such as x + 1 or cos, densify the result. Numeric (DM) operands are evaluated, symbolic ones (SX, MX) build an expression; DM and SX or MX can be mixed, SX and MX cannot.

>>> plus(DM([1, 2]), DM([10, 20]))
DM([11, 22])
>>> DM([1, 2]) + 1
DM([2, 3])
>>> DM.eye(2) + DM.eye(2)
DM(
[[2, 00],
 [00, 2]])
>>> DM.eye(2) + 1
DM(
[[2, 1],
 [1, 2]])
>>> SX.sym("x", 2, 2) + SX.sym("y", 2)
SX(
[[(x_0+y_0), (x_2+y_0)],
 [(x_1+y_1), (x_3+y_1)]])
>>> (MX.sym("x", 2, 0) + 1).shape
(2, 0)
>>> DM.ones(2, 2) + DM.ones(1, 2)
Traceback (most recent call last):
  ...
RuntimeError: ...Dimension mismatch...

>> plus(DM([1; 2]), DM([10; 20]))
[11, 22]
>> DM([1; 2]) + 1
[2, 3]
>> DM.eye(2) + DM.eye(2)
[[2, 00],
 [00, 2]]
>> DM.eye(2) + 1
[[2, 1],
 [1, 2]]
>> SX.sym('x', 2, 2) + SX.sym('y', 2)
[[(x_0+y_0), (x_2+y_0)],
 [(x_1+y_1), (x_3+y_1)]]
>> size(MX.sym('x', 2, 0) + 1)
2 0
>> try, DM.ones(2, 2) + DM.ones(1, 2); catch e, disp(e.message); end
...Dimension mismatch...

julia> ca.plus(ca.DM([1, 2]), ca.DM([10, 20]))
DM([11, 22])
julia> ca.DM([1, 2]) + 1
DM([2, 3])
julia> ca.DM.eye(2) + ca.DM.eye(2)
DM(
[[2, 00],
 [00, 2]])
julia> ca.DM.eye(2) + 1
DM(
[[2, 1],
 [1, 2]])
julia> ca.SX.sym("x", 2, 2) + ca.SX.sym("y", 2)
SX(
[[(x_0+y_0), (x_2+y_0)],
 [(x_1+y_1), (x_3+y_1)]])
julia> size(ca.MX.sym("x", 2, 0) + 1)
(2, 0)
julia> try
           ca.DM.ones(2, 2) + ca.DM.ones(1, 2)
       catch e
           println(occursin("Dimension mismatch", sprint(showerror, e)))
       end
true

Extra doc: https://github.com/casadi/casadi/wiki/L_on

Definition at line 161 of file generic_expression.hpp.

161  {
162  return ExType::binary(OP_ADD, x, y);
163  }

◆ pow()

static SXElem casadi::GenericExpression< SXElem >::pow ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise; shape rules as for plus. Named power in Python, where the ** operator maps to it as well. A constant exponent is simplified symbolically: integer powers become repeated squaring and multiplication, 0.5 becomes sqrt, -1 a reciprocal, other constants a constpow operation. Structural zeros of x stay zero for a nonzero exponent; x**0 is dense (0**0 == 1).

>>> power(DM([1, 2, 3]), 2)
DM([1, 4, 9])
>>> DM([1, 2, 3])**DM([3, 2, 1]), 2**DM([1, 2, 3])
(DM([1, 4, 3]), DM([2, 4, 8]))
>>> x = SX.sym("x")
>>> x**2, x**3, x**-1, x**x
(SX(sq(x)), SX((x*sq(x))), SX((1./x)), SX(pow(x,x)))
>>> x**2.5
SX(pow(x,2.5))

>> power(DM([1; 2; 3]), 2)
[1, 4, 9]
>> DM([1; 2; 3]).^DM([3; 2; 1]), 2.^DM([1; 2; 3])
[1, 4, 3]
[2, 4, 8]
>> x = SX.sym('x');
>> x.^2, x.^3, x.^-1, x.^x
sq(x)
(x*sq(x))
(1./x)
pow(x,x)
>> x.^2.5
pow(x,2.5)

julia> ca.power(ca.DM([1, 2, 3]), 2)
DM([1, 4, 9])
julia> ca.DM([1, 2, 3]) .^ ca.DM([3, 2, 1]), 2 .^ ca.DM([1, 2, 3])
([1, 4, 3], [2, 4, 8])
julia> x = ca.SX.sym("x");
julia> x^2, x^3, x^-1, x^x
(sq(x), (x*sq(x)), (1./x), pow(x,x))
julia> x^2.5
SX(pow(x,2.5))

See also
sq, sqrt, constpow, mpower

Extra doc: https://github.com/casadi/casadi/wiki/L_pp

Definition at line 1978 of file generic_expression.hpp.

1978  {
1979  return ExType::binary(OP_POW, x, y);
1980  }

◆ printme()

static SXElem casadi::GenericExpression< SXElem >::printme ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Identity operation with a side effect: whenever the expression is evaluated inside a Function (and hence inside a solver), every nonzero of x is printed on its own line as "|> y : value" in storage (column-major) order, with y an identifier of choice to tell several printme nodes apart. Available as a method x.printme(y) in Python; the derivative is that of x. Use monitor to print a whole matrix at once.

>>> x = MX.sym("x")
>>> f = Function("f", [x], [x.printme(7) * 2])
>>> f(3)
|> 7 : 3.0000000000000000e+00
DM(6)

>> x = MX.sym('x');
>> f = Function('f', {x}, {x.printme(7) * 2});
>> f(3)
|> 7 : 3.0000000000000000e+00
6

julia> x = ca.MX.sym("x");
julia> f = ca.Function("f", [x], [ca.printme(x, 7) * 2]);
julia> f(3)
|> 7 : 3.0000000000000000e+00
DM(6)

Extra doc: https://github.com/casadi/casadi/wiki/L_2on

Definition at line 2515 of file generic_expression.hpp.

2515  {
2516  return ExType::binary(OP_PRINTME, x, y);
2517  }

◆ rdivide()

static SXElem casadi::GenericExpression< SXElem >::rdivide ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise quotient, not a linear solve; use solve or mldivide for that. Shape rules as for plus. Division by zero follows IEEE arithmetic (inf or nan), no error is raised. Structural zeros of x stay structural zeros; dividing by a sparse y densifies.

>>> rdivide(DM([1, 2]), 4)
DM([0.25, 0.5])
>>> DM([1, 2]) / DM([4, 8])
DM([0.25, 0.25])
>>> 1 / DM.eye(2)
DM(
[[1, inf],
 [inf, 1]])
>>> DM.eye(2) / 2
DM(
[[0.5, 00],
 [00, 0.5]])

>> rdivide(DM([1; 2]), 4)
[0.25, 0.5]
>> DM([1; 2]) ./ DM([4; 8])
[0.25, 0.25]
>> 1 ./ DM.eye(2)
[[1, inf],
 [inf, 1]]
>> DM.eye(2) ./ 2
[[0.5, 00],
 [00, 0.5]]

julia> ca.rdivide(ca.DM([1, 2]), 4)
DM([0.25, 0.5])
julia> ca.DM([1, 2]) ./ ca.DM([4, 8])
DM([0.25, 0.25])
julia> 1 ./ ca.DM.eye(2)
DM(
[[1, inf],
 [inf, 1]])
julia> ca.DM.eye(2) / 2
DM(
[[0.5, 00],
 [00, 0.5]])

See also
mldivide, mrdivide, solve

Extra doc: https://github.com/casadi/casadi/wiki/L_oq

Definition at line 360 of file generic_expression.hpp.

360  {
361  return ExType::binary(OP_DIV, x, y);
362  }

◆ remainder()

static SXElem casadi::GenericExpression< SXElem >::remainder ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

This Function follows the convention of https://en.cppreference.com/w/c/numeric/math/remainder: x - n*y with n the integer nearest to x/y (ties to even), so the result lies in [-|y|/2, |y|/2].

Notably:

  • remainder(5,3) -> -1
  • remainder(5,-3) -> -1
  • remainder(-5,3) -> 1
  • remainder(-5,-3) -> 1

This is equivalent to Python's math.remainder. There is no equivalence in Matlab. Elementwise, shape rules as for plus; structural zeros of x stay zero.

>>> remainder(DM([5, 5, -5, -5]), DM([3, -3, 3, -3]))
DM([-1, -1, 1, 1])
>>> remainder(DM([2.5, 3.5]), 2)
DM([0.5, -0.5])
>>> remainder(MX.sym("x"), 2)
MX(remainder(x,2))

>> remainder(DM([5; 5; -5; -5]), DM([3; -3; 3; -3]))
[-1, -1, 1, 1]
>> remainder(DM([2.5; 3.5]), 2)
[0.5, -0.5]
>> remainder(MX.sym('x'), 2)
remainder(x,2)

julia> ca.remainder(ca.DM([5, 5, -5, -5]), ca.DM([3, -3, 3, -3]))
DM([-1, -1, 1, 1])
julia> ca.remainder(ca.DM([2.5, 3.5]), 2)
DM([0.5, -0.5])
julia> ca.remainder(ca.MX.sym("x"), 2)
MX(remainder(x,2))

See also
fmod
Extra doc: https://github.com/casadi/casadi/wiki/L_24x 

Definition at line 2099 of file generic_expression.hpp.

2099  {
2100  return ExType::binary(OP_REMAINDER, x, y);
2101  }

◆ self() [1/2]

SXElem & casadi::GenericExpression< SXElem >::self ( )
inlineprotectedinherited

Definition at line 55 of file generic_expression.hpp.

55 { return static_cast<ExType&>(*this); }

◆ self() [2/2]

const SXElem & casadi::GenericExpression< SXElem >::self ( ) const
inlineprotectedinherited

Definition at line 54 of file generic_expression.hpp.

54 { return static_cast<const ExType&>(*this); }

◆ serialize()

void casadi::SXElem::serialize ( SerializingStream &  s) const

Extra doc: https://github.com/casadi/casadi/wiki/L_114

Definition at line 508 of file sx_elem.cpp.

508  {
509  (*this)->serialize(s);
510  }

◆ set_temp()

void casadi::SXElem::set_temp ( int  t) const

Definition at line 488 of file sx_elem.cpp.

488  {
489  (*this)->temp = t;
490  }

◆ sign()

static SXElem casadi::GenericExpression< SXElem >::sign ( const SXElem &  x)
inlinestaticinherited

sign(x) := -1 for x<0 sign(x) := 1 for x>0, sign(0) := 0 sign(NaN) := NaN

Elementwise; sparsity preserved. The derivative is zero everywhere.

>>> sign(DM([-2, 0, 3]))
DM([-1, 0, 1])
>>> sign(SX.sym("x"))
SX(sign(x))

>> sign(DM([-2; 0; 3]))
[-1, 0, 1]
>> sign(SX.sym('x'))
sign(x)

julia> sign.(ca.DM([-2, 0, 3]))
DM([-1, 0, 1])
julia> sign(ca.SX.sym("x"))
SX(sign(x))

See also
abs, copysign

Extra doc: https://github.com/casadi/casadi/wiki/L_po

Definition at line 1912 of file generic_expression.hpp.

1912  {
1913  return ExType::unary(OP_SIGN, x);
1914  }

◆ sin()

static SXElem casadi::GenericExpression< SXElem >::sin ( const SXElem &  x)
inlinestaticinherited

Elementwise; sparsity preserved (sin(0) == 0).

>>> sin(DM.eye(2))
DM(
[[0.841471, 00],
 [00, 0.841471]])
>>> sin(MX.sym("x", 2))
MX(sin(x))

>> sin(DM.eye(2))
[[0.841471, 00],
 [00, 0.841471]]
>> sin(MX.sym('x', 2))
sin(x)

julia> sin.(ca.DM.eye(2))
DM(
[[0.841471, 00],
 [00, 0.841471]])
julia> sin.(ca.MX.sym("x", 2))
MX(sin(x))

See also
cos, tan, asin

Extra doc: https://github.com/casadi/casadi/wiki/L_p3

Definition at line 1058 of file generic_expression.hpp.

1058  {
1059  return ExType::unary(OP_SIN, x);
1060  }

◆ sinh()

static SXElem casadi::GenericExpression< SXElem >::sinh ( const SXElem &  x)
inlinestaticinherited

Elementwise; sparsity preserved.

>>> sinh(DM([0, 1]))
DM([0, 1.1752])

>> sinh(DM([0; 1]))
[0, 1.1752]

julia> sinh.(ca.DM([0, 1]))
DM([0, 1.1752])

See also
asinh, cosh, tanh

Extra doc: https://github.com/casadi/casadi/wiki/L_pa

Definition at line 1335 of file generic_expression.hpp.

1335  {
1336  return ExType::unary(OP_SINH, x);
1337  }

◆ sq()

static SXElem casadi::GenericExpression< SXElem >::sq ( const SXElem &  x)
inlinestaticinherited

Elementwise; sparsity preserved. Not exposed as a function in Python; a symbolic power with exponent 2 is turned into this operation.

>>> SX.sym("x")**2
SX(sq(x))

>> SX.sym('x').^2
sq(x)

julia> ca.SX.sym("x")^2
SX(sq(x))

See also
sqrt, pow

Extra doc: https://github.com/casadi/casadi/wiki/L_p2

Definition at line 1010 of file generic_expression.hpp.

1010  {
1011  return ExType::unary(OP_SQ, x);
1012  }

◆ sqrt()

static SXElem casadi::GenericExpression< SXElem >::sqrt ( const SXElem &  x)
inlinestaticinherited

Elementwise; sparsity preserved. A negative argument gives NaN, no error is raised. A symbolic power x**0.5 simplifies to sqrt(x).

>>> sqrt(DM([4, 9]))
DM([2, 3])
>>> SX.sym("x")**0.5
SX(sqrt(x))

>> sqrt(DM([4; 9]))
[2, 3]
>> SX.sym('x').^0.5
sqrt(x)

julia> sqrt.(ca.DM([4, 9]))
DM([2, 3])
julia> ca.SX.sym("x")^0.5
SX(sqrt(x))

See also
sq, pow

Extra doc: https://github.com/casadi/casadi/wiki/L_p1

Definition at line 972 of file generic_expression.hpp.

972  {
973  return ExType::unary(OP_SQRT, x);
974  }

◆ sym()

SXElem casadi::SXElem::sym ( const std::string &  name)
static
Parameters
nameName of the symbolic primitive

This is the name that will be used by the "operator<<" and "str" methods. The name is not used as identifier; you may construct distinct SXElem objects with non-unique names.

Extra doc: https://github.com/casadi/casadi/wiki/L_10l

Definition at line 95 of file sx_elem.cpp.

95  {
96  return create(new SymbolicSX(name));
97  }
const std::string & name() const
Definition: sx_elem.cpp:347
Represents a scalar symbolic expression.
Definition: symbolic_sx.hpp:42

References create(), and name().

Referenced by casadi::Matrix< Scalar >::_sym(), and casadi::register_symbol().

◆ tan()

static SXElem casadi::GenericExpression< SXElem >::tan ( const SXElem &  x)
inlinestaticinherited

Elementwise; sparsity preserved.

>>> tan(DM([0, 1]))
DM([0, 1.55741])

>> tan(DM([0; 1]))
[0, 1.55741]

julia> tan.(ca.DM([0, 1]))
DM([0, 1.55741])

See also
atan, sin, cos

Extra doc: https://github.com/casadi/casadi/wiki/L_p5

Definition at line 1149 of file generic_expression.hpp.

1149  {
1150  return ExType::unary(OP_TAN, x);
1151  }

◆ tanh()

static SXElem casadi::GenericExpression< SXElem >::tanh ( const SXElem &  x)
inlinestaticinherited

Elementwise; sparsity preserved.

>>> tanh(DM([0, 1]))
DM([0, 0.761594])

>> tanh(DM([0; 1]))
[0, 0.761594]

julia> tanh.(ca.DM([0, 1]))
DM([0, 0.761594])

See also
atanh, sinh, cosh

Extra doc: https://github.com/casadi/casadi/wiki/L_p9

Definition at line 1298 of file generic_expression.hpp.

1298  {
1299  return ExType::unary(OP_TANH, x);
1300  }

◆ times()

static SXElem casadi::GenericExpression< SXElem >::times ( const SXElem &  x,
const SXElem &  y 
)
inlinestaticinherited

Elementwise (Hadamard) product, not the matrix product; use mtimes for that. Shape rules as for plus. The result is structurally zero wherever either operand is, so the sparsity pattern is the intersection of the patterns.

>>> times(DM([1, 2]), DM([3, 4]))
DM([3, 8])
>>> DM([[1, 2], [3, 4]]) * DM([[1, 2], [3, 4]])
DM(
[[1, 4],
 [9, 16]])
>>> DM.eye(2) * DM.ones(2, 2)
DM(
[[1, 00],
 [00, 1]])

>> times(DM([1; 2]), DM([3; 4]))
[3, 8]
>> DM([1 2; 3 4]) .* DM([1 2; 3 4])
[[1, 4],
 [9, 16]]
>> DM([1 2; 3 4]) * DM([1 2; 3 4])
[[7, 10],
 [15, 22]]
>> DM.eye(2) .* DM.ones(2, 2)
[[1, 00],
 [00, 1]]

julia> ca.times(ca.DM([1, 2]), ca.DM([3, 4]))
DM([3, 8])
julia> ca.DM([1 2; 3 4]) .* ca.DM([1 2; 3 4])
DM(
[[1, 4],
 [9, 16]])
julia> ca.DM.eye(2) .* ca.DM.ones(2, 2)
DM(
[[1, 00],
 [00, 1]])

See also
mtimes

Extra doc: https://github.com/casadi/casadi/wiki/L_op

Definition at line 289 of file generic_expression.hpp.

289  {
290  return ExType::binary(OP_MUL, x, y);
291  }

◆ type_name()

static std::string casadi::SXElem::type_name ( )
inlinestatic

Definition at line 135 of file sx_elem.hpp.

135 {return "SXElem";}

◆ unary()

SXElem casadi::SXElem::unary ( casadi_int  op,
const SXElem &  x,
bool  unique = false 
)
static

Definition at line 241 of file sx_elem.cpp.

241  {
242  // Simplifications
244  switch (op) {
245  case OP_NOT:
246  if (x.is_op(OP_NOT) && x.dep().is_boolean())
247  return x.dep(); // !!x -> x only if x is 0 or 1
248  break;
249  case OP_TWICE:
250  if (x.is_op(OP_MUL) && x.dep(0).is_constant())
251  return (2*x.dep(0))*x.dep(1); // (2*a)*x = (2*a)*x
252  else if (x.is_op(OP_MUL) && x.dep(1).is_constant())
253  return (2*x.dep(1))*x.dep(0); // x*(2*a) = (2*a)*x
254  else if (x.is_op(OP_TWICE))
255  return 4*x.dep(); // 2*(2*x) = 4*x
256  break;
257  }
258  // Simplifications
259  bool hit;
261  [](casadi_int op, const SXElem& a) { return unary(op, a);},
262  unique,
263  hit);
264  if (hit) return ret;
265  }
266  return UnarySX::create(Operation(op), x);
267  }
bool is_boolean() const
Check if a value is always 0 or 1 (false negatives are allowed)
Definition: sx_elem.cpp:370
static SXElem create(unsigned char op, const SXElem &dep)
Create a unary expression.
Definition: unary_sx.hpp:55
T common_simp_unary(casadi_int op, const T &x, casadi_int depth, SU &&gen_unary, bool unique, bool &hit)
Definition: calculus.hpp:1827
@ OP_NOT
Definition: calculus.hpp:70

References casadi::common_simp_unary(), casadi::UnarySX::create(), dep(), casadi::SXNode::eq_depth_, is_boolean(), is_constant(), is_op(), op(), casadi::OP_MUL, casadi::OP_NOT, casadi::OP_TWICE, and casadi::GlobalOptions::simplification_on_the_fly.

Referenced by binary(), inv(), and operator-().

◆ which_function()

Function casadi::SXElem::which_function ( ) const

Definition at line 295 of file sx_elem.cpp.

295  {
296  return node->which_function();
297  }
virtual Function which_function() const
Get called function.
Definition: sx_node.cpp:64

References casadi::SXNode::which_function().

◆ which_output()

casadi_int casadi::SXElem::which_output ( ) const

Definition at line 299 of file sx_elem.cpp.

299  {
300  return node->which_output();
301  }
virtual casadi_int which_output() const
Get function output.
Definition: sx_node.cpp:68

References casadi::SXNode::which_output().

Friends And Related Function Documentation

◆ BinarySXNode

friend class BinarySXNode
friend

Definition at line 77 of file sx_elem.hpp.

◆ if_else

SXElem if_else ( const SXElem &  x,
const SXElem &  y,
const SXElem &  z 
)
friend

Extra doc: https://github.com/casadi/casadi/wiki/L_113

Definition at line 296 of file sx_elem.hpp.

296  {
297  return if_else_zero(x, y) + if_else_zero(!x, z);
298  }

◆ Matrix< SXElem >

friend class Matrix< SXElem >
friend

Definition at line 77 of file sx_elem.hpp.

◆ SXNode

friend class SXNode
friend

Definition at line 76 of file sx_elem.hpp.


The documentation for this class was generated from the following files: