constant_mx.cpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #include "constant_mx.hpp"
27 #include <vector>
28 #include <algorithm>
29 #include "casadi_misc.hpp"
30 #include "serializing_stream.hpp"
31 
32 namespace casadi {
33 
35  set_sparsity(sp);
36  }
37 
39  }
40 
42  return nnz()==0;
43  }
44 
45  casadi_int ConstantMX::n_primitives() const {
46  if (nnz()==0) {
47  return 0;
48  } else {
49  return MXNode::n_primitives();
50  }
51  }
52 
53  void ConstantMX::primitives(std::vector<MX>::iterator& it) const {
54  if (nnz()!=0) {
56  }
57  }
58 
59  template<typename T>
60  void ConstantMX::split_primitives_gen(const T& x, typename std::vector<T>::iterator& it) const {
61  if (nnz()!=0) {
63  }
64  }
65 
66  void ConstantMX::split_primitives(const MX& x, std::vector<MX>::iterator& it) const {
67  split_primitives_gen<MX>(x, it);
68  }
69 
70  void ConstantMX::split_primitives(const SX& x, std::vector<SX>::iterator& it) const {
71  split_primitives_gen<SX>(x, it);
72  }
73 
74  void ConstantMX::split_primitives(const DM& x, std::vector<DM>::iterator& it) const {
75  split_primitives_gen<DM>(x, it);
76  }
77 
78  template<typename T>
79  T ConstantMX::join_primitives_gen(typename std::vector<T>::const_iterator& it) const {
80  if (nnz()==0) {
81  return T(sparsity());
82  } else {
83  return MXNode::join_primitives(it);
84  }
85  }
86 
87  MX ConstantMX::join_primitives(std::vector<MX>::const_iterator& it) const {
88  if (nnz()==0) {
89  return shared_from_this<MX>();
90  } else {
91  return MXNode::join_primitives(it);
92  }
93  }
94 
95  SX ConstantMX::join_primitives(std::vector<SX>::const_iterator& it) const {
96  return join_primitives_gen<SX>(it);
97  }
98 
99  DM ConstantMX::join_primitives(std::vector<DM>::const_iterator& it) const {
100  return join_primitives_gen<DM>(it);
101  }
102 
103  void ConstantMX::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
104  const std::vector<bool>& unique) const {
105  res[0] = shared_from_this<MX>();
106  }
107 
108  void ConstantMX::ad_forward(const std::vector<std::vector<MX> >& fseed,
109  std::vector<std::vector<MX> >& fsens) const {
110  MX zero_sens(size1(), size2());
111  for (casadi_int d=0; d<fsens.size(); ++d) {
112  fsens[d][0] = zero_sens;
113  }
114  }
115 
116  void ConstantMX::ad_reverse(const std::vector<std::vector<MX> >& aseed,
117  std::vector<std::vector<MX> >& asens) const {
118  }
119 
120  int ConstantMX::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
121  std::fill_n(res[0], nnz(), 0);
122  return 0;
123  }
124 
125  int ConstantMX::eval_activity(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
126  // Sound default for an unknown constant flavor; value-bearing subclasses refine per nonzero
127  std::fill_n(res[0], nnz(), ~static_cast<bvec_t>(0));
128  return 0;
129  }
130 
131  void ConstantMX::nonzeros_to_activity(const double* v, bvec_t* res) const {
132  // Helper: set bit per nonzero where the stored value is nonzero
133  for (casadi_int k=0; k<nnz(); ++k) res[k] = (v[k]!=0) ? ~static_cast<bvec_t>(0) : 0;
134  }
135 
136  int ConstantMX::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
137  std::fill_n(res[0], nnz(), 0);
138  return 0;
139  }
140 
142  const std::vector<casadi_int>& arg,
143  const std::vector<casadi_int>& res,
144  const std::vector<bool>& arg_is_ref,
145  std::vector<bool>& res_is_ref) const {
146  // Print the constant
147  std::string ind = g.constant(x_.nonzeros());
148 
149  if (g.elide_copy(nnz())) {
150  g << g.work(res[0], nnz(), true) << " = " << ind << ";\n";
151  res_is_ref[0] = true;
152  } else {
153  // Copy the constant to the work vector
154  g << g.copy(ind, nnz(), g.work(res[0], nnz(), false)) << '\n';
155  }
156  }
157 
158  bool ConstantMX::__nonzero__() const {
159  if (numel()!=1) casadi_error("Can only determine truth value of scalar MX.");
160  if (nnz()!=1) casadi_error("Can only determine truth value of dense scalar MX.");
161  return !is_zero();
162  }
163 
164  ConstantMX* ConstantMX::create(const Sparsity& sp, casadi_int val) {
165  if (sp.is_empty(true)) {
166  return ZeroByZero::getInstance();
167  } else {
168  switch (val) {
169  case 0: return new Constant<CompiletimeConst<0> >(sp);
170  case 1: return new Constant<CompiletimeConst<1> >(sp);
171  case -1: return new Constant<CompiletimeConst<(-1)> >(sp);
172  default: return new Constant<RuntimeConst<casadi_int> >(sp, val);
173  }
174  }
175  }
176 
177  ConstantMX* ConstantMX::create(const Sparsity& sp, double val) {
178  if (sp.is_empty(true)) {
179  return ZeroByZero::getInstance();
180  } else {
181  // Range check avoids UB in the cast (also rejects nan/inf)
182  const double lo = static_cast<double>(std::numeric_limits<casadi_int>::min());
183  if (val >= lo && val < -lo &&
184  val == static_cast<double>(static_cast<casadi_int>(val))) {
185  return create(sp, static_cast<casadi_int>(val));
186  } else {
187  return new Constant<RuntimeConst<double> >(sp, val);
188  }
189  }
190  }
191 
193  if (val.nnz()==0) {
194  return create(val.sparsity(), 0);
195  } else if (val.is_scalar()) {
196  return create(val.sparsity(), val.scalar());
197  } else {
198  // Check if all values are the same
199  const std::vector<double> vdata = val.nonzeros();
200  double v = vdata[0];
201  for (auto&& i : vdata) {
202  if (i!=v) {
203  // Values not all the same
204  return new ConstantDM(val);
205  }
206  }
207 
208  // All values identical if reached this point
209  return create(val.sparsity(), v);
210  }
211  }
212 
213  ConstantMX* ConstantMX::create(const Sparsity& sp, const std::string& fname) {
214  if (sp.nnz()==0) {
215  return create(sp, 0);
216  } else {
217  return new ConstantFile(sp, fname);
218  }
219  }
220 
221  ConstantMX* ConstantMX::create(const DM& x, const std::string& name) {
222  return new ConstantPool(x, name);
223  }
224 
225  bool ConstantDM::is_zero() const {
226  return x_.is_zero();
227  }
228 
229  bool ConstantDM::is_one() const {
230  return x_.is_one();
231  }
232 
234  return x_.is_minus_one();
235  }
236 
237  bool ConstantDM::is_half() const {
238  return x_.is_half();
239  }
240 
241  bool ConstantDM::is_value(double val) const {
242  return x_.is_value(val);
243  }
244 
245  bool ConstantDM::is_inf() const {
246  return x_.is_inf();
247  }
248 
250  return x_.is_minus_inf();
251  }
252 
253  bool ConstantDM::is_integer() const {
254  return x_.is_integer();
255  }
256 
258  return x_.is_nonnegative();
259  }
260 
261  bool ConstantDM::is_eye() const {
262  return x_.is_eye();
263  }
264 
265  // MX ConstantMX::get_mac(const MX& y) const {
266  // if (y.is_constant()) {
267  // // Constant folding
268  // DM xv = get_DM();
269  // DM yv = y->get_DM();
270  // return mul(xv, yv);
271  // } else {
272  // return MXNode::get_mac(y);
273  // }
274  // }
275 
276  MX ConstantMX::get_dot(const MX& y) const {
277  if (y.is_constant()) {
278  // Constant folding
279  DM xv = get_DM();
280  DM yv = y->get_DM();
281  return dot(xv, yv);
282  } else {
283  return MXNode::get_dot(y);
284  }
285  }
286 
287  bool ConstantDM::is_equal(const MXNode* node, casadi_int depth) const {
288  // Check if same node
289  const ConstantDM* n = dynamic_cast<const ConstantDM*>(node);
290  if (n==nullptr) return false;
291 
292  // Check sparsity
293  if (this->sparsity()!=node->sparsity()) return false;
294 
295  // Check indices
296  if (!std::equal(x_->begin(), x_->end(), n->x_->begin())) return false;
297 
298  return true;
299  }
300 
301  std::string ZeroByZero::disp(const std::vector<std::string>& arg) const {
302  return "0x0";
303  }
304 
305  MX ZeroByZero::get_project(const Sparsity& sp, bool unique) const {
306  return shared_from_this<MX>();
307  }
308 
309  MX ZeroByZero::get_nzref(const Sparsity& sp, const std::vector<casadi_int>& nz,
310  bool unique) const {
311  casadi_assert_dev(nz.empty());
312  return MX::zeros(sp);
313  }
314 
315  MX ZeroByZero::get_nzassign(const MX& y, const std::vector<casadi_int>& nz) const {
316  return shared_from_this<MX>();
317  }
318 
320  return shared_from_this<MX>();
321  }
322 
323  MX ZeroByZero::get_unary(casadi_int op, bool unique) const {
324  return shared_from_this<MX>();
325  }
326 
327  MX ZeroByZero::_get_binary(casadi_int op, const MX& y, bool ScX, bool ScY,
328  bool unique_x, bool unique_y) const {
329  return shared_from_this<MX>();
330  }
331 
333  casadi_assert_dev(sp.is_empty());
334  return MX::zeros(sp);
335  }
336 
339  s.pack("ConstantMX::type", 'a');
340  }
341 
344  DM v = get_DM();
345  s.pack("ConstantMX::nonzeros", v.nonzeros());
346  }
347 
349  std::vector<double> v;
350  s.unpack("ConstantMX::nonzeros", v);
351  x_ = DM(sparsity_, v);
352  }
353 
356  s.pack("ConstantMX::type", 'z');
357  }
358 
360  // No need to serialize body at all. All info is in header.
361  }
362 
364  char t;
365  s.unpack("ConstantMX::type", t);
366  switch (t) {
367  case 'a': return new ConstantDM(s);
368  case 'f': return new ConstantFile(s);
369  case 'p': return new ConstantPool(s);
370  case 'z': return ZeroByZero::getInstance();
371  case 'D':
373  case 'I':
374  return new Constant<RuntimeConst<casadi_int> >(s,
376  case '0':
378  case '1':
380  case 'm':
381  return new Constant<CompiletimeConst<(-1)> >(s, CompiletimeConst<(-1)>::deserialize(s));
382  default:
383  casadi_error("Error deserializing");
384  }
385  }
386 
389  s.pack("ConstantFile::type", 'f');
390  }
391 
394  s.pack("ConstantFile::fname", fname_);
395  s.pack("ConstantFile::x", x_);
396  }
397 
399  s.unpack("ConstantFile::fname", fname_);
400  s.unpack("ConstantFile::x", x_);
401  }
402 
403  ConstantFile::ConstantFile(const Sparsity& sp, const std::string& fname) :
404  ConstantMX(sp), fname_(fname) {
405  x_.resize(sp.nnz());
406  int ret = casadi_file_slurp(fname_.c_str(), nnz(), get_ptr(x_));
407  if (ret==1) casadi_error("Cannot open file '" + str(fname) + "'.");
408  if (ret==2) casadi_error("Failed to read a double from '" + str(fname) + "'. "
409  "Expected " + str(sp.nnz()) + " doubles.");
410  }
411 
412  std::string ConstantFile::disp(const std::vector<std::string>& arg) const {
413  return "from_file('" + fname_ + "'): " + DM(sparsity(), x_, false).get_str();
414  }
415 
416  double ConstantFile::to_double() const {
417  casadi_error("Not defined for ConstantFile");
418  }
419 
420  casadi_int ConstantFile::to_int() const {
421  casadi_error("Not defined for ConstantFile");
422  }
423 
425  casadi_error("Not defined for ConstantFile");
426  }
427 
428  void ConstantFile::codegen_incref(CodeGenerator& g, std::set<void*>& added) const {
429  g << g.file_slurp(fname_, nnz(), g.rom_double(this)) << ";\n";
430  }
431 
433  g.define_rom_double(this, nnz());
434  }
435 
437  const std::vector<casadi_int>& arg,
438  const std::vector<casadi_int>& res,
439  const std::vector<bool>& arg_is_ref,
440  std::vector<bool>& res_is_ref) const {
441  if (nnz()==1) {
442  g << g.workel(res[0]) << " = " << g.rom_double(this) << "[0];\n";
443  } else if (g.elide_copy(nnz())) {
444  g << g.work(res[0], nnz(), true) << " = " << g.rom_double(this) << ";\n";
445  res_is_ref[0] = true;
446  } else {
447  g << g.copy(g.rom_double(this), nnz(), g.work(res[0], nnz(), false)) << '\n';
448  }
449  }
450 
451  ConstantPool::ConstantPool(const DM& x, const std::string& name) :
452  ConstantMX(x.sparsity()), name_(name), x_(x.nonzeros()) {
453  }
454 
455  std::string ConstantPool::disp(const std::vector<std::string>& arg) const {
456  return "constant_pool('" + name_ + "'): " + DM(sparsity(), x_, false).get_str();
457  }
458 
459  double ConstantPool::to_double() const {
460  casadi_error("Not defined for ConstantPool");
461  }
462 
463  casadi_int ConstantPool::to_int() const {
464  casadi_error("Not defined for ConstantPool");
465  }
466 
468  casadi_error("Not defined for ConstantPool");
469  }
470 
472  const std::vector<casadi_int>& arg,
473  const std::vector<casadi_int>& res,
474  const std::vector<bool>& arg_is_ref,
475  std::vector<bool>& res_is_ref) const {
476  if (nnz()==1) {
477  g << g.workel(res[0]) << " = " << g.pool_double(name_) << "[0];\n";
478  } else if (g.elide_copy(nnz())) {
479  g << g.work(res[0], nnz(), true) << " = " << g.pool_double(name_) << ";\n";
480  res_is_ref[0] = true;
481  } else {
482  g << g.copy(g.pool_double(name_), nnz(), g.work(res[0], nnz(), false)) << '\n';
483  }
484  }
485 
488  g.add_include("string.h");
489  }
490 
493  s.pack("ConstantPool::name", name_);
494  s.pack("ConstantPool::x", x_);
495  }
496 
499  s.pack("ConstantPool::type", 'p');
500  }
501 
503  s.unpack("ConstantPool::name", name_);
504  s.unpack("ConstantPool::x", x_);
505  }
506 
507 } // namespace casadi
Helper class for C code generation.
void define_pool_double(const std::string &name, const std::vector< double > &def)
Allocate file scope double writeable memory.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string pool_double(const std::string &name) const
Access file scope double writeable memory.
std::string copy(const std::string &arg, std::size_t n, const std::string &res)
Create a copy operation.
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.
std::string rom_double(const void *id) const
Access file scope double read-only memory.
std::string workel(casadi_int n) const
void define_rom_double(const void *id, casadi_int size)
Allocate file scope double read-only memory.
void add_include(const std::string &new_include, bool relative_path=false, const std::string &use_ifdef=std::string())
Add an include file optionally using a relative path "..." instead of an absolute path <....
std::string file_slurp(const std::string &fname, casadi_int n, const std::string &a)
Slurp a file.
bool elide_copy(casadi_int sz)
A constant given as a DM.
void serialize_type(SerializingStream &s) const override
Serialize type information.
bool is_equal(const MXNode *node, casadi_int depth) const override
Check if two nodes are equivalent up to a given depth.
bool is_integer() const override
Check if integer.
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
bool is_one() const override
Check if identically one.
bool is_half() const override
Check if identically 0.5.
Matrix< double > x_
data member
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
bool is_value(double val) const override
Check if a certain value.
bool is_minus_inf() const override
Check if identically -inf.
bool is_zero() const override
Check if a particular integer value.
ConstantDM(const Matrix< double > &x)
Constructor.
bool is_inf() const override
Check if identically inf.
bool is_eye() const override
Check if identity matrix.
bool is_nonnegative() const override
Check if not negative.
bool is_minus_one() const override
Check if identically minus one.
Matrix< double > get_DM() const override
Get the value (only for constant nodes)
A constant to be read from a file.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
Matrix< double > get_DM() const override
Get the value (only for constant nodes)
double to_double() const override
Get the value (only for scalar constant nodes)
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
casadi_int to_int() const override
Get the value (only for scalar constant nodes)
void add_dependency(CodeGenerator &g) const override
Add a dependent function.
void serialize_type(SerializingStream &s) const override
Serialize type information.
ConstantFile(const Sparsity &x, const std::string &fname)
Constructor.
std::vector< double > x_
nonzeros
void codegen_incref(CodeGenerator &g, std::set< void * > &added) const override
Codegen incref.
std::string fname_
file to read from
Represents an MX that is only composed of a constant.
Definition: constant_mx.hpp:48
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
T join_primitives_gen(typename std::vector< T >::const_iterator &it) const
Join an expression along symbolic primitives (template)
Definition: constant_mx.cpp:79
void primitives(std::vector< MX >::iterator &it) const override
Get symbolic primitives.
Definition: constant_mx.cpp:53
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
void nonzeros_to_activity(const double *v, bvec_t *res) const
Set per-nonzero activity from a raw value buffer.
Matrix< double > get_DM() const override=0
Get the value (only for constant nodes)
MX join_primitives(std::vector< MX >::const_iterator &it) const override
Join an expression along symbolic primitives.
Definition: constant_mx.cpp:87
void split_primitives(const MX &x, std::vector< MX >::iterator &it) const override
Split up an expression along symbolic primitives.
Definition: constant_mx.cpp:66
~ConstantMX() override=0
Destructor.
Definition: constant_mx.cpp:38
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
casadi_int n_primitives() const override
Get the number of symbolic primitives.
Definition: constant_mx.cpp:45
static MXNode * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
int eval_activity(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate signal activity forward (sound default; subclasses refine)
static ConstantMX * create(const Sparsity &sp, casadi_int val)
MX get_dot(const MX &y) const override
Matrix multiplication.
ConstantMX(const Sparsity &sp)
Destructor.
Definition: constant_mx.cpp:34
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
bool __nonzero__() const override
Return truth value of an MX.
void split_primitives_gen(const T &x, typename std::vector< T >::iterator &it) const
Split up an expression along primitives (template)
Definition: constant_mx.cpp:60
bool is_valid_input() const override
Check if valid function input.
Definition: constant_mx.cpp:41
A constant to be managed by a pool.
std::vector< double > x_
nonzeros
Matrix< double > get_DM() const override
Get the value (only for constant nodes)
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
casadi_int to_int() const override
Get the value (only for scalar constant nodes)
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
double to_double() const override
Get the value (only for scalar constant nodes)
void add_dependency(CodeGenerator &g) const override
Add a dependent function.
ConstantPool(const DM &x, const std::string &name)
Constructor.
void serialize_type(SerializingStream &s) const override
Serialize type information.
std::string name_
pool identifier
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
A constant with all entries identical.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
casadi_int nnz() const
Get the number of structural nonzeros.
static MX zeros(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries zero.
bool is_scalar(bool scalar_and_dense=false) const
Check if the matrix is scalar, i.e. 1-by-1.
Node class for MX objects.
Definition: mx_node.hpp:51
virtual void serialize_type(SerializingStream &s) const
Serialize type information.
Definition: mx_node.cpp:536
virtual const std::string & name() const
Get the name.
Definition: mx_node.cpp:196
virtual casadi_int n_primitives() const
Get the number of symbolic primitives.
Definition: mx_node.cpp:144
virtual bool is_zero() const
Check if identically zero.
Definition: mx_node.hpp:71
virtual DM get_DM() const
Get the value (only for constant nodes)
Definition: mx_node.cpp:488
virtual casadi_int ind() const
Definition: mx_node.cpp:213
virtual MX join_primitives(std::vector< MX >::const_iterator &it) const
Join an expression along symbolic primitives.
Definition: mx_node.cpp:184
virtual MX get_dot(const MX &y) const
Inner product.
Definition: mx_node.cpp:1094
Sparsity sparsity_
The sparsity pattern.
Definition: mx_node.hpp:829
casadi_int numel() const
Get shape.
Definition: mx_node.hpp:426
const Sparsity & sparsity() const
Get the sparsity.
Definition: mx_node.hpp:410
casadi_int size2() const
Definition: mx_node.hpp:429
casadi_int nnz(casadi_int i=0) const
Definition: mx_node.hpp:427
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
Definition: mx_node.cpp:531
virtual void primitives(std::vector< MX >::iterator &it) const
Get symbolic primitives.
Definition: mx_node.cpp:156
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
Definition: mx_node.cpp:225
casadi_int size1() const
Definition: mx_node.hpp:428
virtual void split_primitives(const MX &x, std::vector< MX >::iterator &it) const
Split up an expression along symbolic primitives.
Definition: mx_node.cpp:160
MX - Matrix expression.
Definition: mx.hpp:145
bool is_constant() const
Check if the node is a constant.
Definition: mx.cpp:802
Sparse matrix class. SX and DM are specializations.
std::vector< Scalar > & nonzeros()
bool is_inf() const
check if the matrix is inf (note that false negative answers are possible)
bool is_one() const
check if the matrix is 1 (note that false negative answers are possible)
bool is_value(double val) const
check if the matrix is value
const Sparsity & sparsity() const
Const access the sparsity - reference to data member.
bool is_minus_one() const
check if the matrix is -1 (note that false negative answers are possible)
bool is_half() const
check if the matrix is 0.5 (note that false negative answers are possible)
bool is_eye() const
check if the matrix is an identity matrix (note that false negative answers
bool is_minus_inf() const
check if the matrix is -inf (note that false negative answers are possible)
bool is_zero() const
check if the matrix is 0 (note that false negative answers are possible)
bool is_nonnegative() const
check if the matrix is not negative
bool is_integer() const
Check if the matrix is integer-valued.
std::string get_str(bool more=false) const
Get the string representation that disp prints.
const Scalar scalar() const
Convert to scalar type.
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
General sparsity class.
Definition: sparsity.hpp:200
casadi_int nnz() const
Number of structural nonzeros.
Definition: sparsity.cpp:150
bool is_empty(bool both=false) const
Check whether the pattern has a zero dimension.
Definition: sparsity.cpp:146
MX _get_binary(casadi_int op, const MX &y, bool ScX, bool ScY, bool unique_x=false, bool unique_y=false) const override
Get a binary operation operation.
MX get_nzassign(const MX &y, const std::vector< casadi_int > &nz) const override
Assign the nonzeros of a matrix to another matrix.
MX get_transpose() const override
Transpose.
MX get_reshape(const Sparsity &sp) const override
Reshape.
static ZeroByZero * getInstance()
Get a pointer to the singleton.
MX get_unary(casadi_int op, bool unique) const override
Get a unary operation.
void serialize_body(SerializingStream &s) const override
Serialize type information.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz, bool unique=false) const override
Get the nonzeros of matrix.
void serialize_type(SerializingStream &s) const override
Serialize specific part of node.
MX get_project(const Sparsity &sp, bool unique=false) const override
Get densification.
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t
std::string str(const T &v)
String representation, any type.
T dot(const std::vector< T > &a, const std::vector< T > &b)
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
Matrix< double > DM
Definition: dm_fwd.hpp:33
static CompiletimeConst deserialize(DeserializingStream &s)
static RuntimeConst deserialize(DeserializingStream &s)