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);
75 }
else if (isinf(val)) {
80 int intval =
static_cast<int>(val);
81 if (val-
static_cast<double>(intval) == 0) {
99 if (--node->
count == 0)
delete node;
103 if (
this == &scalar)
return *
this;
106 if (node == scalar.node)
return *
this;
109 if (--node->
count == 0)
delete node;
118 casadi_assert_dev(depth>=1);
129 if (node == scalar.node)
return ret;
143 return *
this =
SXElem(scalar);
147 node->
disp(stream, more);
165 casadi_error(
"Cannot compute the truth value of a CasADi SXElem symbolic expression.");
190 bool unique_x,
bool unique_y) {
222 return (x*2)*y.
dep();
224 return (2*y)*x.
dep();
247 return (2*x.
dep(0))*x.
dep(1);
249 return (2*x.
dep(1))*x.
dep(0);
266 if (!node)
return true;
271 casadi_assert(
n_dep(),
"SX::is_commutative: must be binary");
272 return operation_checker<CommChecker>(
op());
357 if (x_node==y_node) {
359 }
else if (depth>0) {
360 return x_node->
is_equal(y_node, depth);
368 return static_cast<double>(*this)>=0;
370 return operation_checker<NonnegativeChecker>(node->
op());
376 SXElem::operator double()
const {
377 return node->to_double();
380 SXElem::operator casadi_int()
const {
381 return node->to_int();
385 casadi_assert_dev(ch >= 0 || ch <
n_dep());
386 return node->
dep(ch);
390 return node->
n_dep();
398 return reinterpret_cast<casadi_int
>(node);
465 SXElem::operator
SX()
const {
470 return (*this)->temp;
478 return (*this)->marked();
489 casadi_error(
"Cannot check regularity for symbolic SXElem");
494 (*this)->serialize(s);
501 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
502 std::mutex SXElem::mutex_temp;
509 SXElem std::numeric_limits<SXElem>::infinity()
throw() {
513 SXElem std::numeric_limits<SXElem>::quiet_NaN()
throw() {
517 SXElem std::numeric_limits<SXElem>::min()
throw() {
518 return SXElem(std::numeric_limits<double>::min());
521 SXElem std::numeric_limits<SXElem>::max()
throw() {
522 return SXElem(std::numeric_limits<double>::max());
525 SXElem std::numeric_limits<SXElem>::epsilon()
throw() {
526 return SXElem(std::numeric_limits<double>::epsilon());
529 SXElem std::numeric_limits<SXElem>::round_error()
throw() {
530 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_value(double val) const
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.
static SXElem unary(casadi_int op, const SXElem &x, bool unique=false)
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.
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,.
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.
static SXElem binary(casadi_int op, const SXElem &x, const SXElem &y, bool unique_x=false, bool unique_y=false)
Perform operations by ID.
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_half() 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_value(double val) const
check properties of a node
virtual bool is_nan() const
check properties of a 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 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_nonnegative(const T &val)
static bool is_equal(const T &x, const T &y, casadi_int depth)
static bool is_half(const T &val)
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_value(const T &val, T v)
static bool is_zero(const T &val)
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)
T common_simp_unary(casadi_int op, const T &x, casadi_int depth, SU &&gen_unary, bool unique, bool &hit)
Operation
Enum for quick access to any node.
Easy access to all the functions for a particular type.