26 #include "sx_elem.hpp"
30 #include "calculus.hpp"
31 #include "constant_sx.hpp"
32 #include "symbolic_sx.hpp"
33 #include "unary_sx.hpp"
34 #include "binary_sx.hpp"
35 #include "call_sx.hpp"
36 #include "output_sx.hpp"
37 #include "global_options.hpp"
38 #include "sx_function.hpp"
48 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
49 std::mutex IntegerSX::mutex_cached_constants;
50 std::mutex RealtypeSX::mutex_cached_constants;
63 return SXElem(node,
false);
73 int intval =
static_cast<int>(val);
74 if (val-
static_cast<double>(intval) == 0) {
95 if (--node->
count == 0)
delete node;
100 if (node == scalar.node)
return *
this;
103 if (--node->
count == 0)
delete node;
112 casadi_assert_dev(depth>=1);
123 if (node == scalar.node)
return ret;
137 return *
this =
SXElem(scalar);
141 node->
disp(stream, more);
159 casadi_error(
"Cannot compute the truth value of a CasADi SXElem symbolic expression.");
255 return -(x.
dep() + y);
277 static_cast<double>(x)*
static_cast<double>(y.
dep(0))==1)
280 static_cast<double>(x)==
static_cast<double>(y.
dep(1)))
288 return -(x.
dep() * y);
290 return -(x * y.
dep());
314 return x.
dep(0) / y->
dep(0);
316 static_cast<double>(y)*
static_cast<double>(x.
dep(1))==1)
333 return -(x.
dep() / y);
335 return -(x / y.
dep());
340 casadi_int nn = y->
to_int();
343 }
else if (nn>100 || nn<-100) {
346 return 1/
pow(x, -nn);
347 }
else if (nn%2 == 1) {
348 return x*
pow(x, nn-1);
350 SXElem rt =
pow(x,
static_cast<casadi_int
>(nn/2));
373 if (x.
is_inf() || y.
is_inf())
return std::numeric_limits<double>::infinity();
392 if (
static_cast<double>(x)!=0) {
448 if (!node)
return true;
453 casadi_assert(
n_dep(),
"SX::is_commutative: must be binary");
454 return operation_checker<CommChecker>(
op());
531 if (x_node==y_node) {
533 }
else if (depth>0) {
534 return x_node->
is_equal(y_node, depth);
542 return static_cast<double>(*this)>=0;
550 SXElem::operator double()
const {
551 return node->to_double();
554 SXElem::operator casadi_int()
const {
555 return node->to_int();
559 casadi_assert_dev(ch >= 0 || ch <
n_dep());
560 return node->
dep(ch);
564 return node->
n_dep();
572 return reinterpret_cast<casadi_int
>(node);
627 SXElem::operator
SX()
const {
632 return (*this)->temp;
640 return (*this)->marked();
651 casadi_error(
"Cannot check regularity for symbolic SXElem");
656 return (*this)->serialize(s);
663 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
664 std::mutex SXElem::mutex_temp;
671 SXElem std::numeric_limits<SXElem>::infinity() throw() {
675 SXElem std::numeric_limits<SXElem>::quiet_NaN() throw() {
679 SXElem std::numeric_limits<SXElem>::min() throw() {
680 return SXElem(std::numeric_limits<double>::min());
683 SXElem std::numeric_limits<SXElem>::max() throw() {
684 return SXElem(std::numeric_limits<double>::max());
687 SXElem std::numeric_limits<SXElem>::epsilon() throw() {
688 return SXElem(std::numeric_limits<double>::epsilon());
691 SXElem std::numeric_limits<SXElem>::round_error() throw() {
692 return SXElem(std::numeric_limits<double>::round_error());
static SXElem create(unsigned char op, const SXElem &dep0, const SXElem &dep1)
Create a binary expression.
static SXElem create(const Function &f, const std::vector< SXElem > &dep)
Create a binary expression.
Helper class for Serialization.
casadi_int nnz_out() const
Get number of output nonzeros.
static bool simplification_on_the_fly
Indicates whether simplifications should be made on the fly.
static InfSX * singleton()
static CACHING_MAP< casadi_int, IntegerSX * > cached_constants_
Hash map of all constants currently allocated.
static IntegerSX * create(casadi_int value)
Static creator function (use instead of constructor)
static MinusInfSX * singleton()
static MinusOneSX * singleton()
static NanSX * singleton()
static OneSX * singleton()
static std::vector< SXElem > split(const SXElem &e, casadi_int n)
static CACHING_MAP< double, RealtypeSX * > cached_constants_
Hash map of all constants currently allocated.
static RealtypeSX * create(double value)
Static creator function (use instead of constructor)
The basic scalar symbolic class of CasADi.
void serialize(SerializingStream &s) const
Serialize an object.
void set_temp(int t) const
Set the temporary variable.
void assignIfDuplicate(const SXElem &scalar, casadi_int depth=1)
Assign to another expression, if a duplicate.
bool is_regular() const
Checks if expression does not contain NaN or Inf.
SXElem dep(casadi_int ch=0) const
bool marked() const
Check if marked (i.e. temporary is negative)
void disp(std::ostream &stream, bool more=false) const
Print a description of the object.
bool __nonzero__() const
Check the truth value of this node.
static std::vector< SXElem > call(const Function &f, const std::vector< SXElem > &deps)
bool is_minus_inf() const
bool is_leaf() const
check if this SXElem is a leaf of the SX graph
SXElem inv() const
Element-wise inverse.
static SXElem create(SXNode *node)
bool is_minus_one() const
const SXNode * operator->() const
Access functions of the node.
SXElem operator-() const
Negation.
void mark() const
Mark by flipping the sign of the temporary and decreasing by one.
Function which_function() const
SXElem get_output(casadi_int oind) const
Get an output.
bool is_doubled() const
Check if the node is the sum of two equal expressions.
static SXElem unary(casadi_int op, const SXElem &x)
bool is_commutative() const
Check whether a binary SXElem is commutative.
SXNode * assignNoDelete(const SXElem &scalar)
Assign the node to something, without invoking the deletion of the node,.
static SXElem binary(casadi_int op, const SXElem &x, const SXElem &y)
Perform operations by ID.
bool is_almost_zero(double tol) const
bool is_op(casadi_int op) const
SXNode * get() const
Get a pointer to the node.
static SXElem deserialize(DeserializingStream &s)
casadi_int which_output() const
const std::string & name() const
casadi_int __hash__() const
Returns a number that is unique for a given SXNode.
casadi_int n_dep() const
Get the number of dependencies of a binary SXElem.
static bool is_equal(const SXElem &x, const SXElem &y, casadi_int depth=0)
Check equality up to a given depth.
SXElem()
Default constructor (not-a-number)
static SXElem sym(const std::string &name)
Create a symbolic primitive.
SXElem & operator=(const SXElem &scalar)
Assignment.
bool is_nonnegative() const
Check if a value is always nonnegative (false negatives are allowed)
Internal node class for SX.
virtual SXElem get_output(casadi_int oind) const
Get an output.
virtual const SXElem & dep(casadi_int i) const
get the reference of a child
virtual Function which_function() const
Get called function.
virtual bool is_zero() const
check properties of a node
virtual bool is_minus_one() const
check properties of a node
static SXNode * deserialize(DeserializingStream &s)
virtual bool is_almost_zero(double tol) const
check properties of a node
virtual bool is_one() const
check properties of a node
virtual casadi_int n_dep() const
Number of dependencies.
virtual bool is_nan() const
check properties of a node
virtual casadi_int to_int() const
Get value of a constant node.
virtual bool is_call() const
check properties of a node
virtual bool is_equal(const SXNode *node, casadi_int depth) const
Check if two nodes are equivalent up to a given depth.
virtual const std::string & name() const
virtual bool is_integer() const
check properties of a node
virtual double to_double() const
Get value of a constant node.
virtual bool is_inf() const
check properties of a node
virtual bool is_symbolic() const
check properties of a node
virtual bool is_op(casadi_int op) const
check properties of a node
virtual bool is_output() const
check properties of a node
static casadi_int eq_depth_
virtual casadi_int op() const =0
get the operation
virtual bool has_output() const
check properties of a node
virtual bool is_constant() const
check properties of a node
virtual bool is_minus_inf() const
check properties of a node
virtual void disp(std::ostream &stream, bool more) const
print
virtual casadi_int which_output() const
Get function output.
Helper class for Serialization.
static Sparsity scalar(bool dense_scalar=true)
Create a scalar sparsity pattern *.
Represents a scalar symbolic expression.
static SXElem create(unsigned char op, const SXElem &dep)
Create a unary expression.
static ZeroSX * singleton()
static bool is_inf(const T &val)
static bool is_almost_zero(const T &val, double tol)
static bool is_constant(const T &val)
static bool is_equal(const T &x, const T &y, casadi_int depth)
static bool is_one(const T &val)
static bool is_minus_inf(const T &val)
static bool is_integer(const T &val)
static bool is_minus_one(const T &val)
static bool is_nan(const T &val)
static bool is_zero(const T &val)
Operation
Enum for quick access to any node.