26 #include "sx_node.hpp"
27 #include "serializing_stream.hpp"
28 #include "unary_sx.hpp"
29 #include "binary_sx.hpp"
30 #include "constant_sx.hpp"
31 #include "symbolic_sx.hpp"
32 #include "call_sx.hpp"
33 #include "output_sx.hpp"
46 #ifdef WITH_REFCOUNT_WARNINGS
50 std::cerr <<
"Reference counting failure." <<
51 "Possible cause: Circular dependency in user code." << std::endl;
57 return std::numeric_limits<double>::quiet_NaN();
61 casadi_error(
"to_int not defined for " +
class_name());
65 casadi_error(
"'which_function' not defined for class " +
class_name());
69 casadi_error(
"'which_output' not defined for class " +
class_name());
77 casadi_error(
"'name' not defined for " +
class_name());
81 casadi_error(
"'dep' not defined for " +
class_name());
85 casadi_error(
"'dep' not defined for " +
class_name());
90 std::map<const SXNode*, casadi_int> nodeind;
94 std::vector<std::string> intermed;
98 for (casadi_int i=0; i<intermed.size(); ++i)
99 stream <<
"@" << (i+1) <<
"=" << intermed[i] <<
", ";
115 std::map<const SXNode*, casadi_int>::iterator it=nodeind.find(
this);
116 if (it==nodeind.end()) {
118 nodeind.insert(it, std::make_pair(
this, 0));
121 for (casadi_int i=0; i<
n_dep(); ++i) {
131 std::vector<std::string>& intermed)
const {
133 casadi_int& ind = nodeind[
this];
137 std::stringstream ss;
149 for (casadi_int i=0; i<f.
n_in(); ++i) {
150 if (f.
nnz_in(i)>1) s +=
"[";
151 for (casadi_int j=0; j<f.
nnz_in(i); ++j) {
153 if (j<f.
nnz_in(i)-1) s+=
",";
155 if (f.
nnz_in(i)>1) s +=
"]";
156 if (i<f.
n_in()-1) s+=
",";
162 for (casadi_int i=0; i<
n_dep(); ++i) {
167 s =
print(arg[0], arg[1]);
176 intermed.push_back(s);
177 ind = intermed.size();
178 std::stringstream ss;
186 if (n->
count>0)
return;
193 std::stack<SXNode*> deletion_stack;
195 deletion_stack.push(n);
197 while (!deletion_stack.empty()) {
199 SXNode *t = deletion_stack.top();
201 bool added_to_stack =
false;
202 for (casadi_int c2=0; c2<t->
n_dep(); ++c2) {
207 if (n2->
count == 0) {
214 deletion_stack.push(n2);
215 added_to_stack =
true;
220 if (!added_to_stack) {
221 delete deletion_stack.top();
222 deletion_stack.pop();
228 casadi_assert(oind==0,
"Output index out of bounds");
235 casadi_error(
"'serialize_node' not defined for class " +
class_name());
239 s.
pack(
"SXNode::op",
op());
255 casadi_error(
"Not implemented op " +
str(casadi_int(
op)));
257 return it->second(s);
262 return SXElem(
this,
false);
static SXNode * deserialize(DeserializingStream &s, casadi_int op)
Deserialize without type information.
static SXNode * deserialize(DeserializingStream &s)
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
const std::string & name() const
Name of the function.
casadi_int n_in() const
Get the number of function inputs.
casadi_int nnz_in() const
Get number of input nonzeros.
static SXNode * deserialize(DeserializingStream &s)
The basic scalar symbolic class of CasADi.
SXNode * assignNoDelete(const SXElem &scalar)
Assign the node to something, without invoking the deletion of the node,.
Internal node class for SX.
void serialize(SerializingStream &s) const
Serialize an object.
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.
SXElem shared_from_this()
Get a shared object from the current internal object.
static std::map< casadi_int, SXNode *(*)(DeserializingStream &)> deserialize_map
static SXNode * deserialize(DeserializingStream &s)
virtual casadi_int n_dep() const
Number of dependencies.
static void safe_delete(SXNode *n)
Non-recursive delete.
virtual casadi_int to_int() const
Get value of a constant 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 std::string print(const std::string &arg1, const std::string &arg2) const =0
Print expression.
virtual ~SXNode()
destructor
virtual double to_double() const
Get value of a constant node.
static casadi_int eq_depth_
virtual casadi_int op() const =0
get the operation
virtual void serialize_node(SerializingStream &s) const
std::string print_compact(std::map< const SXNode *, casadi_int > &nodeind, std::vector< std::string > &intermed) const
Print compact.
void can_inline(std::map< const SXNode *, casadi_int > &nodeind) const
Find out which nodes can be inlined.
virtual void disp(std::ostream &stream, bool more) const
print
virtual casadi_int which_output() const
Get function output.
virtual std::string class_name() const =0
Get type name.
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
static SXNode * deserialize(DeserializingStream &s)
static SXNode * deserialize(DeserializingStream &s, casadi_int op)
std::string str(const T &v)
String representation, any type.
SXNode * ConstantSX_deserialize(DeserializingStream &s)
Easy access to all the functions for a particular type.