function.hpp
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 #ifndef CASADI_FUNCTION_HPP
26 #define CASADI_FUNCTION_HPP
27 
28 #include "sx_elem.hpp"
29 #include "mx.hpp"
30 #include "printable.hpp"
31 #include <exception>
32 #include <stack>
33 
34 namespace casadi {
35 
36 #ifndef SWIG
38  class FunctionInternal;
39  class SerializingStream;
40  class DeserializingStream;
41 #endif // SWIG
42 
58  class CASADI_EXPORT Function :
59  public SharedObject,
60  public SWIG_IF_ELSE(PrintableCommon, Printable<Function>) {
61  public:
65  static std::string type_name() {return "Function";}
66 
71 
75  Function(const std::string& fname);
76 
78 
81  Function(const std::string& name,
82  const std::vector<SX>& ex_in,
83  const std::vector<SX>& ex_out,
84  const Dict& opts=Dict());
85  Function(const std::string& name,
86  const std::vector<SX>& ex_in,
87  const std::vector<SX>& ex_out,
88  const std::vector<std::string>& name_in,
89  const std::vector<std::string>& name_out,
90  const Dict& opts=Dict());
91  Function(const std::string& name, const std::map<std::string, SX>& dict,
92  const std::vector<std::string>& name_in,
93  const std::vector<std::string>& name_out,
94  const Dict& opts=Dict());
96 
98 
101  Function(const std::string& name,
102  const std::vector<MX>& ex_in,
103  const std::vector<MX>& ex_out,
104  const Dict& opts=Dict());
105  Function(const std::string& name,
106  const std::vector<MX>& ex_in,
107  const std::vector<MX>& ex_out,
108  const std::vector<std::string>& name_in,
109  const std::vector<std::string>& name_out,
110  const Dict& opts=Dict());
111  Function(const std::string& name, const std::map<std::string, MX>& dict,
112  const std::vector<std::string>& name_in,
113  const std::vector<std::string>& name_out,
114  const Dict& opts=Dict());
116 
118 
121 #ifndef SWIG
122  Function(const std::string& name, SXIList ex_in,
123  const SXVector& ex_out, const Dict& opts=Dict());
124  Function(const std::string& name, const SXVector& ex_in,
125  SXIList ex_out, const Dict& opts=Dict());
126  Function(const std::string& name, SXIList ex_in,
127  SXIList ex_out, const Dict& opts=Dict());
128  Function(const std::string& name, SXIList ex_in, const SXVector& ex_out,
129  const StringVector& name_in, const StringVector& name_out,
130  const Dict& opts=Dict());
131  Function(const std::string& name, const SXVector& ex_in, SXIList ex_out,
132  const StringVector& name_in, const StringVector& name_out,
133  const Dict& opts=Dict());
134  Function(const std::string& name, SXIList ex_in, SXIList ex_out,
135  const StringVector& name_in, const StringVector& name_out,
136  const Dict& opts=Dict());
137  Function(const std::string& name, MXIList ex_in, const MXVector& ex_out,
138  const Dict& opts=Dict());
139  Function(const std::string& name, const MXVector& ex_in, MXIList ex_out,
140  const Dict& opts=Dict());
141  Function(const std::string& name, MXIList ex_in, MXIList ex_out,
142  const Dict& opts=Dict());
143  Function(const std::string& name, MXIList ex_in, const MXVector& ex_out,
144  const StringVector& name_in, const StringVector& name_out,
145  const Dict& opts=Dict());
146  Function(const std::string& name, const MXVector& ex_in, MXIList ex_out,
147  const StringVector& name_in, const StringVector& name_out,
148  const Dict& opts=Dict());
149  Function(const std::string& name, MXIList ex_in, MXIList ex_out,
150  const StringVector& name_in, const StringVector& name_out,
151  const Dict& opts=Dict());
152 #endif // SWIG
154 
156 
185  static Function jit(const std::string& name, const std::string& body,
186  const std::vector<std::string>& name_in,
187  const std::vector<std::string>& name_out,
188  const Dict& opts=Dict());
189  static Function jit(const std::string& name, const std::string& body,
190  const std::vector<std::string>& name_in,
191  const std::vector<std::string>& name_out,
192  const std::vector<Sparsity>& sparsity_in,
193  const std::vector<Sparsity>& sparsity_out,
194  const Dict& opts=Dict());
196 
201 
206  Function expand() const;
207  Function expand(const std::string& name,
208  const Dict& opts=Dict()) const;
210 
212 #ifndef SWIG
216  static Function create(FunctionInternal* node);
217 
221  static Function create(FunctionInternal* node, const Dict& opts);
222 #endif // SWIG
224 
228  casadi_int n_in() const;
229 
233  casadi_int n_out() const;
234 
236 
239  casadi_int size1_in(casadi_int ind) const;
240  casadi_int size1_in(const std::string& iname) const {return size1_in(index_in(iname));}
241  casadi_int size2_in(casadi_int ind) const;
242  casadi_int size2_in(const std::string& iname) const {return size2_in(index_in(iname));}
243  std::pair<casadi_int, casadi_int> size_in(casadi_int ind) const;
244  std::pair<casadi_int, casadi_int> size_in(const std::string& iname) const {
245  return size_in(index_in(iname));
246  }
248 
250 
253  casadi_int size1_out(casadi_int ind) const;
254  casadi_int size1_out(const std::string& oname) const {return size1_out(index_out(oname));}
255  casadi_int size2_out(casadi_int ind) const;
256  casadi_int size2_out(const std::string& oname) const {return size2_out(index_out(oname));}
257  std::pair<casadi_int, casadi_int> size_out(casadi_int ind) const;
258  std::pair<casadi_int, casadi_int> size_out(const std::string& oname) const {
259  return size_out(index_out(oname));
260  }
262 
264 
269  casadi_int nnz_in() const;
270  casadi_int nnz_in(casadi_int ind) const;
271  casadi_int nnz_in(const std::string& iname) const {return nnz_in(index_in(iname));}
273 
275 
280  casadi_int nnz_out() const;
281  casadi_int nnz_out(casadi_int ind) const;
282  casadi_int nnz_out(const std::string& oname) const {return nnz_out(index_out(oname));}
284 
286 
291  casadi_int numel_in() const;
292  casadi_int numel_in(casadi_int ind) const;
293  casadi_int numel_in(const std::string& iname) const {return numel_in(index_in(iname));}
295 
297 
302  casadi_int numel_out() const;
303  casadi_int numel_out(casadi_int ind) const;
304  casadi_int numel_out(const std::string& oname) const {return numel_out(index_out(oname));}
306 
310  const std::vector<std::string>& name_in() const;
311 
315  const std::vector<std::string>& name_out() const;
316 
320  const std::string& name_in(casadi_int ind) const;
321 
325  const std::string& name_out(casadi_int ind) const;
326 
333  casadi_int index_in(const std::string &name) const;
334 
341  casadi_int index_out(const std::string &name) const;
342 
346  double default_in(casadi_int ind) const;
347 
351  double max_in(casadi_int ind) const;
352 
356  double min_in(casadi_int ind) const;
357 
361  std::vector<double> nominal_in(casadi_int ind) const;
362 
366  std::vector<double> nominal_out(casadi_int ind) const;
367 
372  const Sparsity& sparsity_in(casadi_int ind) const;
373  const Sparsity& sparsity_in(const std::string& iname) const;
375 
380  const Sparsity& sparsity_out(casadi_int ind) const;
381  const Sparsity& sparsity_out(const std::string& iname) const;
383 
388  bool is_diff_in(casadi_int ind) const;
389  bool is_diff_out(casadi_int ind) const;
390  std::vector<bool> is_diff_in() const;
391  std::vector<bool> is_diff_out() const;
393 
394  // A linear combination of inputs
395  typedef std::map<std::string, std::vector<std::string> > AuxOut;
396 
397  // Factory
398  Function factory(const std::string& name,
399  const std::vector<std::string>& s_in,
400  const std::vector<std::string>& s_out,
401  const AuxOut& aux=AuxOut(),
402  const Dict& opts=Dict()) const;
403 
407  Function oracle() const;
408 
412  Function wrap() const;
413 
417  Function wrap_as_needed(const Dict& opts) const;
418 
425  std::vector<bool> which_depends(const std::string& s_in,
426  const std::vector<std::string>& s_out,
427  casadi_int order=1, bool tr=false) const;
428 
432  void print_dimensions(std::ostream &stream=casadi::uout()) const;
433 
437  void print_options(std::ostream &stream=casadi::uout()) const;
438 
442  void print_option(const std::string &name, std::ostream &stream = casadi::uout()) const;
443 
447  bool has_option(const std::string &option_name) const;
448 
455  void change_option(const std::string& option_name, const GenericType& option_value);
456 
460  bool uses_output() const;
461 
462 #ifdef WITH_DEPRECATED_FEATURES
466  Function jacobian_old(casadi_int iind, casadi_int oind) const;
467 
471  Function hessian_old(casadi_int iind, casadi_int oind) const;
472 
475  const Sparsity sparsity_jac(casadi_int iind, casadi_int oind,
476  bool compact=false, bool symmetric=false) const;
477  const Sparsity sparsity_jac(const std::string &iind, casadi_int oind=0,
478  bool compact=false, bool symmetric=false) const {
479  return sparsity_jac(index_in(iind), oind, compact, symmetric);
480  }
481  const Sparsity sparsity_jac(casadi_int iind, const std::string &oind,
482  bool compact=false, bool symmetric=false) const {
483  return sparsity_jac(iind, index_out(oind), compact, symmetric);
484  }
485  const Sparsity sparsity_jac(const std::string &iind, const std::string &oind,
486  bool compact=false, bool symmetric=false) const {
487  return sparsity_jac(index_in(iind), index_out(oind), compact, symmetric);
488  }
490 #endif // WITH_DEPRECATED_FEATURES
491 
509 
511 
514  void call(const std::vector<DM> &arg, std::vector<DM>& SWIG_OUTPUT(res),
515  bool always_inline=false, bool never_inline=false) const;
516  void call(const std::vector<SX> &arg, std::vector<SX>& SWIG_OUTPUT(res),
517  bool always_inline=false, bool never_inline=false) const;
518  void call(const std::vector<MX> &arg, std::vector<MX>& SWIG_OUTPUT(res),
519  bool always_inline=false, bool never_inline=false) const;
520  void call(const DMDict& arg, DMDict& SWIG_OUTPUT(res),
521  bool always_inline=false, bool never_inline=false) const;
522  void call(const SXDict& arg, SXDict& SWIG_OUTPUT(res),
523  bool always_inline=false, bool never_inline=false) const;
524  void call(const MXDict& arg, MXDict& SWIG_OUTPUT(res),
525  bool always_inline=false, bool never_inline=false) const;
527 
528 #ifndef SWIG
530  bool operator==(const Function& f) const;
531 
534  std::vector<DM> operator()(const std::vector<DM>& arg) const;
535  std::vector<SX> operator()(const std::vector<SX>& arg) const;
536  std::vector<MX> operator()(const std::vector<MX>& arg) const;
537  const DMDict operator()(const DMDict& arg) const;
538  const SXDict operator()(const SXDict& arg) const;
539  const MXDict operator()(const MXDict& arg) const;
541 
543 
546  void operator()(std::vector<const double*> arg, std::vector<double*> res) const;
547  void operator()(std::vector<const bvec_t*> arg, std::vector<bvec_t*> res) const;
548  void operator()(std::vector<const SXElem*> arg, std::vector<SXElem*> res) const;
549  template<typename D> void call_gen(std::vector<const D*> arg, std::vector<D*> res) const;
551 
553 
556  typedef const std::vector<std::vector<double>>& VecArg;
557  std::vector<const double*> buf_in(VecArg arg) const;
558  typedef std::vector<std::vector<double>>& VecRes;
559  std::vector<double*> buf_out(VecRes res) const;
560  typedef std::vector<std::vector<double>*> VPrRes;
561  std::vector<double*> buf_out(VPrRes res) const;
562 
563  typedef const std::map<std::string, std::vector<double>>& MapArg;
564  std::vector<const double*> buf_in(MapArg arg) const;
565  typedef std::map<std::string, std::vector<double>>& MapRes;
566  std::vector<double*> buf_out(MapRes res) const;
567  typedef std::map<std::string, std::vector<double>*> MPrRes;
568  std::vector<double*> buf_out(MPrRes res) const;
570 
572 
575  void operator()(VecArg arg, VecRes res) const { (*this)(buf_in(arg), buf_out(res)); }
576  void operator()(VecArg arg, MapRes res) const { (*this)(buf_in(arg), buf_out(res)); }
577  void operator()(VecArg arg, VPrRes res) const { (*this)(buf_in(arg), buf_out(res)); }
578  void operator()(VecArg arg, MPrRes res) const { (*this)(buf_in(arg), buf_out(res)); }
579 
580  void operator()(MapArg arg, VecRes res) const { (*this)(buf_in(arg), buf_out(res)); }
581  void operator()(MapArg arg, MapRes res) const { (*this)(buf_in(arg), buf_out(res)); }
582  void operator()(MapArg arg, VPrRes res) const { (*this)(buf_in(arg), buf_out(res)); }
583  void operator()(MapArg arg, MPrRes res) const { (*this)(buf_in(arg), buf_out(res)); }
585 
588  std::vector<DM> operator()(const DM& arg0) const {
589  return operator()(std::vector<DM>{arg0});
590  }
591  std::vector<SX> operator()(const SX& arg0) const {
592  return operator()(std::vector<SX>{arg0});
593  }
594  std::vector<MX> operator()(const MX& arg0) const {
595  return operator()(std::vector<MX>{arg0});
596  }
598 
602  int operator()(const double** arg, double** res,
603  casadi_int* iw, double* w, int mem) const;
604 
608  int operator()(const double** arg, double** res,
609  casadi_int* iw, double* w) const;
610 
616  int operator()(const SXElem** arg, SXElem** res,
617  casadi_int* iw, SXElem* w, int mem=0) const;
618 
622  int operator()(const bvec_t** arg, bvec_t** res,
623  casadi_int* iw, bvec_t* w, int mem=0) const;
624 
628  int rev(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w, int mem=0) const;
629 
633  int rev(std::vector<bvec_t*> arg, std::vector<bvec_t*> res) const;
634 
635 #endif // SWIG
636 
642  std::vector<MX> mapsum(const std::vector<MX > &x,
643  const std::string& parallelization="serial") const;
644 
646 
686  Function mapaccum(const std::string& name, casadi_int N, const Dict& opts = Dict()) const;
687  Function mapaccum(const std::string& name, casadi_int N, casadi_int n_accum,
688  const Dict& opts = Dict()) const;
689  Function mapaccum(const std::string& name, casadi_int n,
690  const std::vector<casadi_int>& accum_in,
691  const std::vector<casadi_int>& accum_out,
692  const Dict& opts=Dict()) const;
693  Function mapaccum(const std::string& name, casadi_int n,
694  const std::vector<std::string>& accum_in,
695  const std::vector<std::string>& accum_out,
696  const Dict& opts=Dict()) const;
697  Function mapaccum(casadi_int N, const Dict& opts = Dict()) const;
698  Function fold(casadi_int N, const Dict& opts = Dict()) const;
700 
726  Function map(casadi_int n, const std::string& parallelization="serial") const;
727  Function map(casadi_int n, const std::string& parallelization,
728  casadi_int max_num_threads) const;
729 
731 
737  Function map(const std::string& name, const std::string& parallelization, casadi_int n,
738  const std::vector<casadi_int>& reduce_in,
739  const std::vector<casadi_int>& reduce_out,
740  const Dict& opts=Dict()) const;
741  Function map(const std::string& name, const std::string& parallelization, casadi_int n,
742  const std::vector<std::string>& reduce_in,
743  const std::vector<std::string>& reduce_out,
744  const Dict& opts=Dict()) const;
745  Function map(casadi_int n,
746  const std::vector<bool>& reduce_in,
747  const std::vector<bool>& reduce_out=std::vector<bool>(),
748  const Dict& opts=Dict()) const;
750 
754  Function slice(const std::string& name, const std::vector<casadi_int>& order_in,
755  const std::vector<casadi_int>& order_out, const Dict& opts=Dict()) const;
756 
760  static Function conditional(const std::string& name, const std::vector<Function>& f,
761  const Function& f_def, const Dict& opts=Dict());
762 
766  static Function conditional(const std::string& name,
767  const Function& f, const Dict& opts=Dict());
768 
774  static Function bspline(const std::string &name,
775  const std::vector< std::vector<double> >& knots, const std::vector<double>& coeffs,
776  const std::vector<casadi_int>& degree, casadi_int m=1, const Dict& opts=Dict());
777 
781  static Function if_else(const std::string& name, const Function& f_true,
782  const Function& f_false, const Dict& opts=Dict());
783 
800  Function forward(casadi_int nfwd) const;
801 
820  Function reverse(casadi_int nadj) const;
821 
825  const std::vector<Sparsity>& jac_sparsity(bool compact = false) const;
826 
830  Sparsity jac_sparsity(casadi_int oind, casadi_int iind, bool compact = false) const;
831 
835  std::string generate(const std::string& fname, const Dict& opts=Dict()) const;
836 
840  std::string generate(const Dict& opts=Dict()) const;
841 
845  std::string generate_dependencies(const std::string& fname, const Dict& opts=Dict()) const;
846 
854  void generate_in(const std::string& fname, const std::vector<DM>& arg);
855  std::vector<DM> generate_in(const std::string& fname);
857 
865  void generate_out(const std::string& fname, const std::vector<DM>& arg);
866  std::vector<DM> generate_out(const std::string& fname);
868 
875  void export_code(const std::string& lang,
876  const std::string &fname, const Dict& options=Dict()) const;
877 
878 #ifndef SWIG
882  void serialize(std::ostream &stream, const Dict& opts=Dict()) const;
883 
887  void serialize(SerializingStream &s) const;
888 #endif
889 
893  std::string serialize(const Dict& opts=Dict()) const;
894 
900  void save(const std::string &fname, const Dict& opts=Dict()) const;
901 
902  std::string export_code(const std::string& lang, const Dict& options=Dict()) const;
903 #ifndef SWIG
904  void export_code(const std::string& lang,
905  std::ostream &stream, const Dict& options=Dict()) const;
906 #endif // SWIG
908 #ifndef SWIG
911  FunctionInternal* get() const;
912 
914  template<typename T>
915  T* get() const {
916  T* ret = dynamic_cast<T*>(get());
917  casadi_assert_dev(ret!=nullptr);
918  return ret;
919  }
920 
924  FunctionInternal* operator->() const;
925 
927  static bool test_cast(const SharedObjectInternal* ptr);
929 #endif // SWIG
930 
932  Dict stats(int mem=0) const;
933 
935 
940  const SX sx_in(casadi_int iind) const;
941  const SX sx_in(const std::string& iname) const {
942  return sx_in(index_in(iname));
943  }
944  const std::vector<SX> sx_in() const;
945  const MX mx_in(casadi_int ind) const;
946  const MX mx_in(const std::string & iname) const {
947  return mx_in(index_in(iname));
948  }
949  const std::vector<MX> mx_in() const;
951 
953 
958  const SX sx_out(casadi_int oind) const;
959  const SX sx_out(const std::string& oname) const {
960  return sx_out(index_out(oname));
961  }
962  const std::vector<SX> sx_out() const;
963  const MX mx_out(casadi_int ind) const;
964  const MX mx_out(const std::string& oname) const {
965  return mx_out(index_out(oname));
966  }
967  const std::vector<MX> mx_out() const;
969 
974  std::vector<double> nz_from_in(const std::vector<DM>& arg) const;
975  std::vector<double> nz_from_out(const std::vector<DM>& arg) const;
976  std::vector<DM> nz_to_in(const std::vector<double>& arg) const;
977  std::vector<DM> nz_to_out(const std::vector<double>& arg) const;
979 
987  DMDict convert_in(const std::vector<DM>& arg) const;
988  std::vector<DM> convert_in(const DMDict& arg) const;
989  DMDict convert_out(const std::vector<DM>& arg) const;
990  std::vector<DM> convert_out(const DMDict& arg) const;
991  SXDict convert_in(const std::vector<SX>& arg) const;
992  std::vector<SX> convert_in(const SXDict& arg) const;
993  SXDict convert_out(const std::vector<SX>& arg) const;
994  std::vector<SX> convert_out(const SXDict& arg) const;
995  MXDict convert_in(const std::vector<MX>& arg) const;
996  std::vector<MX> convert_in(const MXDict& arg) const;
997  MXDict convert_out(const std::vector<MX>& arg) const;
998  std::vector<MX> convert_out(const MXDict& arg) const;
1000 
1004  bool has_free() const;
1005 
1009  std::vector<std::string> get_free() const;
1010 
1014  std::vector<SX> free_sx() const;
1015 
1019  std::vector<MX> free_mx() const;
1020 
1024  void generate_lifted(Function& SWIG_OUTPUT(vdef_fcn),
1025  Function& SWIG_OUTPUT(vinit_fcn)) const;
1026 
1030  casadi_int n_nodes() const;
1031 
1035  casadi_int n_instructions() const;
1036 
1040  casadi_int instruction_id(casadi_int k) const;
1041 
1047  std::vector<casadi_int> instruction_input(casadi_int k) const;
1048 
1052  double instruction_constant(casadi_int k) const;
1053 
1059  std::vector<casadi_int> instruction_output(casadi_int k) const;
1060 
1064  MX instruction_MX(casadi_int k) const;
1065 
1073 
1075 
1078  bool has_spfwd() const;
1079  bool has_sprev() const;
1081 
1085  size_t sz_arg() const;
1086 
1090  size_t sz_res() const;
1091 
1095  size_t sz_iw() const;
1096 
1100  size_t sz_w() const;
1101 
1102 #ifndef SWIG
1106  void sz_work(size_t& sz_arg, size_t& sz_res, size_t& sz_iw, size_t& sz_w) const;
1107 
1111  void set_work(const double**& arg, double**& res,
1112  casadi_int*& iw, double*& w, int mem=0) const;
1113 
1117  void set_temp(const double** arg, double** res,
1118  casadi_int* iw, double* w, int mem=0) const;
1119 
1123  void setup(const double** arg, double** res, casadi_int* iw, double* w, int mem=0) const;
1124 
1128  template<typename M>
1129  void call_gen(const std::map<std::string, M>& arg, std::map<std::string, M>& res,
1130  bool always_inline, bool never_inline) const;
1131 #endif // SWIG
1133 
1137  const std::string& name() const;
1138 
1144  bool is_a(const std::string& type, bool recursive=true) const;
1145 
1158  static bool check_name(const std::string& name);
1159 
1166  static std::string fix_name(const std::string& name);
1167 
1171  static Function deserialize(std::istream& stream);
1172 
1176  static Function deserialize(const std::string& s);
1177 
1181  static Function load(const std::string& filename);
1182 
1187 
1189  void assert_size_in(casadi_int i, casadi_int nrow, casadi_int ncol) const;
1190 
1192  void assert_size_out(casadi_int i, casadi_int nrow, casadi_int ncol) const;
1193 
1195  void assert_sparsity_out(casadi_int i, const Sparsity& sp,
1196  casadi_int n = 1, bool allow_all_zero_sparse = true) const;
1197 
1199  casadi_int checkout() const;
1200 
1202  void release(int mem) const;
1203 
1204 #ifndef SWIG
1206  void* memory(int ind) const;
1207 #endif // SWIG
1208 
1212  Dict cache() const;
1213 
1217  std::vector<std::string> get_function() const;
1218 
1222  Function get_function(const std::string &name) const;
1223 
1227  bool has_function(const std::string& fname) const;
1228 
1234  std::vector<Function> find_functions(casadi_int max_depth = -1) const;
1235 
1242  Function find_function(const std::string &name, casadi_int max_depth=-1) const;
1243 
1245  Dict info() const;
1246 
1247 #ifndef SWIG
1248  protected:
1250 
1253  void construct(const std::string& name,
1254  const std::vector<SX>& ex_in, const std::vector<SX>& ex_out,
1255  const std::vector<std::string>& name_in,
1256  const std::vector<std::string>& name_out,
1257  const Dict& opts);
1258  void construct(const std::string& name,
1259  const std::vector<MX>& ex_in, const std::vector<MX>& ex_out,
1260  const std::vector<std::string>& name_in,
1261  const std::vector<std::string>& name_out,
1262  const Dict& opts);
1263  template<typename M>
1264  void construct(const std::string& name, const std::map<std::string, M>& dict,
1265  const std::vector<std::string>& name_in,
1266  const std::vector<std::string>& name_out,
1267  const Dict& opts);
1269 
1271  static bool proceed_to(std::istream& file, const std::string& str);
1272 
1274  Function mapaccum(const std::string& name, const std::vector<Function>& chain,
1275  casadi_int n_accum=1, const Dict& opts = Dict()) const;
1276 
1277 #ifdef WITH_EXTRA_CHECKS
1278  public:
1279  // How many times have we passed through
1280  // operator()(const double** arg, double** res, casadi_int* iw, double* w, int mem)?
1281  static thread_local casadi_int call_depth_;
1282 #endif
1283 
1284 
1285 #endif // SWIG
1286 
1287 
1288 
1289  };
1290 
1291 
1295 class CASADI_EXPORT FunctionBuffer {
1296  Function f_;
1297  std::vector<double> w_;
1298  std::vector<casadi_int> iw_;
1299  std::vector<const double*> arg_;
1300  std::vector<double*> res_;
1301  FunctionInternal* f_node_;
1302  casadi_int mem_;
1303  void *mem_internal_;
1304  int ret_;
1305 public:
1310 #ifndef SWIG
1311  ~FunctionBuffer();
1312  FunctionBuffer(const FunctionBuffer& f);
1313  FunctionBuffer& operator=(const FunctionBuffer& f);
1314 #endif // SWIG
1315 
1323  void set_arg(casadi_int i, const double* a, casadi_int size);
1324 
1332  void set_res(casadi_int i, double* a, casadi_int size);
1334  int ret();
1335  void _eval();
1336  void* _self() { return this; }
1337 };
1338 
1339 void CASADI_EXPORT _function_buffer_eval(void* raw);
1340 
1341 
1342 } // namespace casadi
1343 
1344 #include "casadi_interrupt.hpp"
1345 #include "runtime/shared.hpp"
1346 
1347 #endif // CASADI_FUNCTION_HPP
Helper class for Serialization.
Class to achieve minimal overhead function evaluations.
Definition: function.hpp:1295
void set_res(casadi_int i, double *a, casadi_int size)
Set output buffer for ouput i.
void set_arg(casadi_int i, const double *a, casadi_int size)
Set input buffer for input i.
FunctionBuffer(const Function &f)
Main constructor.
int ret()
Get last return value.
Function object.
Definition: function.hpp:60
const MX mx_in(const std::string &iname) const
Get symbolic primitives equivalent to the input expressions.
Definition: function.hpp:946
bool has_sprev() const
Is the class able to propagate seeds through the algorithm?
casadi_int numel_in() const
Get number of input elements.
Function map(casadi_int n, const std::vector< bool > &reduce_in, const std::vector< bool > &reduce_out=std::vector< bool >(), const Dict &opts=Dict()) const
Map with reduction.
std::vector< bool > is_diff_in() const
Get differentiability of inputs/output.
Function forward(casadi_int nfwd) const
Get a function that calculates nfwd forward derivatives.
casadi_int nnz_out() const
Get number of output nonzeros.
const std::vector< std::string > & name_in() const
Get input scheme.
Function(const std::string &fname)
Construct from a file.
void print_options(std::ostream &stream=casadi::uout()) const
Print options to a stream.
casadi_int n_instructions() const
Number of instruction in the algorithm (SXFunction/MXFunction)
std::string export_code(const std::string &lang, const Dict &options=Dict()) const
Export function in specific language.
size_t sz_res() const
Get required length of res field.
void generate_in(const std::string &fname, const std::vector< DM > &arg)
Export an input file that can be passed to generate C code with a main.
void call(const std::vector< SX > &arg, std::vector< SX > &res, bool always_inline=false, bool never_inline=false) const
Evaluate the function symbolically or numerically.
void print_option(const std::string &name, std::ostream &stream=casadi::uout()) const
Print all information there is to know about a certain option.
std::pair< casadi_int, casadi_int > size_out(const std::string &oname) const
Get output dimension.
Definition: function.hpp:258
std::string generate_dependencies(const std::string &fname, const Dict &opts=Dict()) const
Export / Generate C code for the dependency function.
std::vector< double > nz_from_out(const std::vector< DM > &arg) const
Convert from/to flat vector of input/output nonzeros.
void save(const std::string &fname, const Dict &opts=Dict()) const
Save Function to a file.
const std::string & name() const
Name of the function.
void call(const std::vector< MX > &arg, std::vector< MX > &res, bool always_inline=false, bool never_inline=false) const
Evaluate the function symbolically or numerically.
void assert_size_in(casadi_int i, casadi_int nrow, casadi_int ncol) const
Assert that an input dimension is equal so some given value.
casadi_int size2_out(casadi_int ind) const
Get output dimension.
std::vector< bool > which_depends(const std::string &s_in, const std::vector< std::string > &s_out, casadi_int order=1, bool tr=false) const
Which variables enter with some order.
void call(const MXDict &arg, MXDict &res, bool always_inline=false, bool never_inline=false) const
Evaluate the function symbolically or numerically.
const Sparsity & sparsity_out(casadi_int ind) const
Get sparsity of a given output.
Function map(const std::string &name, const std::string &parallelization, casadi_int n, const std::vector< casadi_int > &reduce_in, const std::vector< casadi_int > &reduce_out, const Dict &opts=Dict()) const
Map with reduction.
const MX mx_in(casadi_int ind) const
Get symbolic primitives equivalent to the input expressions.
Function(const std::string &name, const std::vector< MX > &ex_in, const std::vector< MX > &ex_out, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts=Dict())
Construct an MX function.
bool has_spfwd() const
Is the class able to propagate seeds through the algorithm?
std::pair< casadi_int, casadi_int > size_out(casadi_int ind) const
Get output dimension.
std::vector< DM > generate_out(const std::string &fname)
Export an output file that can be checked with generated C code output.
double max_in(casadi_int ind) const
Get largest input value.
std::vector< DM > convert_out(const DMDict &arg) const
Convert from/to input/output lists/map.
const Sparsity sparsity_jac(const std::string &iind, casadi_int oind=0, bool compact=false, bool symmetric=false) const
Definition: function.hpp:477
Function(const std::string &name, const std::vector< MX > &ex_in, const std::vector< MX > &ex_out, const Dict &opts=Dict())
Construct an MX function.
casadi_int nnz_out(const std::string &oname) const
Get number of output nonzeros.
Definition: function.hpp:282
casadi_int size1_in(casadi_int ind) const
Get input dimension.
Function fold(casadi_int N, const Dict &opts=Dict()) const
Create a mapaccumulated version of this function.
casadi_int nnz_in(casadi_int ind) const
Get number of input nonzeros.
std::vector< SX > convert_out(const SXDict &arg) const
Convert from/to input/output lists/map.
Function mapaccum(const std::string &name, casadi_int N, const Dict &opts=Dict()) const
Create a mapaccumulated version of this function.
Function expand() const
Expand a function to SX.
MXDict convert_in(const std::vector< MX > &arg) const
Convert from/to input/output lists/map.
std::pair< casadi_int, casadi_int > size_in(casadi_int ind) const
Get input dimension.
casadi_int nnz_out(casadi_int ind) const
Get number of output nonzeros.
Dict info() const
static bool check_name(const std::string &name)
Check if a string is a valid function name.
Dict cache() const
Get all functions in the cache.
static Function conditional(const std::string &name, const std::vector< Function > &f, const Function &f_def, const Dict &opts=Dict())
Constuct a switch function.
std::vector< casadi_int > instruction_input(casadi_int k) const
Locations in the work vector for the inputs of the instruction.
const std::vector< Sparsity > & jac_sparsity(bool compact=false) const
Get, if necessary generate, the sparsity of all Jacobian blocks.
casadi_int numel_out() const
Get number of output elements.
casadi_int size2_in(const std::string &iname) const
Get input dimension.
Definition: function.hpp:242
std::vector< MX > convert_in(const MXDict &arg) const
Convert from/to input/output lists/map.
std::vector< SX > convert_in(const SXDict &arg) const
Convert from/to input/output lists/map.
Function wrap() const
Wrap in an Function instance consisting of only one MX call.
std::vector< double > nominal_in(casadi_int ind) const
Get nominal input value.
casadi_int numel_in(const std::string &iname) const
Get number of input elements.
Definition: function.hpp:293
casadi_int nnz_in(const std::string &iname) const
Get number of input nonzeros.
Definition: function.hpp:271
const MX mx_out(casadi_int ind) const
Get symbolic primitives equivalent to the output expressions.
Function reverse(casadi_int nadj) const
Get a function that calculates nadj adjoint derivatives.
std::vector< DM > nz_to_in(const std::vector< double > &arg) const
Convert from/to flat vector of input/output nonzeros.
casadi_int size1_out(const std::string &oname) const
Get output dimension.
Definition: function.hpp:254
static Function deserialize(const std::string &s)
Build function from serialization.
Function oracle() const
Get oracle.
std::vector< MX > convert_out(const MXDict &arg) const
Convert from/to input/output lists/map.
const Sparsity sparsity_jac(const std::string &iind, const std::string &oind, bool compact=false, bool symmetric=false) const
Definition: function.hpp:485
Function jacobian() const
Calculate all Jacobian blocks.
casadi_int numel_in(casadi_int ind) const
Get number of input elements.
const std::vector< MX > mx_in() const
Get symbolic primitives equivalent to the input expressions.
void release(int mem) const
Release a memory object.
casadi_int index_in(const std::string &name) const
Find the index for a string describing a particular entry of an input scheme.
Function expand(const std::string &name, const Dict &opts=Dict()) const
Expand a function to SX.
std::vector< std::string > get_function() const
Get a list of all functions.
const MX mx_out(const std::string &oname) const
Get symbolic primitives equivalent to the output expressions.
Definition: function.hpp:964
std::vector< Function > find_functions(casadi_int max_depth=-1) const
Get all functions embedded in the expression graphs.
std::vector< DM > nz_to_out(const std::vector< double > &arg) const
Convert from/to flat vector of input/output nonzeros.
const SX sx_out(const std::string &oname) const
Get symbolic primitives equivalent to the output expressions.
Definition: function.hpp:959
static Function deserialize(std::istream &stream)
Build function from serialization.
static std::string fix_name(const std::string &name)
Turn a string into a valid function name as defined by "check_name".
std::vector< bool > is_diff_out() const
Get differentiability of inputs/output.
casadi_int checkout() const
Checkout a memory object.
bool is_diff_out(casadi_int ind) const
Get differentiability of inputs/output.
Function hessian_old(casadi_int iind, casadi_int oind) const
[DEPRECATED] Replaced by Function::factory.
std::vector< DM > convert_in(const DMDict &arg) const
Convert from/to input/output lists/map.
Function mapaccum(const std::string &name, casadi_int n, const std::vector< std::string > &accum_in, const std::vector< std::string > &accum_out, const Dict &opts=Dict()) const
Create a mapaccumulated version of this function.
casadi_int n_nodes() const
Number of nodes in the algorithm.
const Sparsity sparsity_jac(casadi_int iind, casadi_int oind, bool compact=false, bool symmetric=false) const
Function(const std::string &name, const std::vector< SX > &ex_in, const std::vector< SX > &ex_out, const Dict &opts=Dict())
Construct an SX function.
const std::string & name_in(casadi_int ind) const
Get input scheme name by index.
~Function()
Destructor.
Function find_function(const std::string &name, casadi_int max_depth=-1) const
Get a specific function embedded in the expression graphs.
Function get_function(const std::string &name) const
Get a dependency function.
SX instructions_sx() const
Get the SX node corresponding to all instructions (SXFunction)
void assert_sparsity_out(casadi_int i, const Sparsity &sp, casadi_int n=1, bool allow_all_zero_sparse=true) const
Assert that an output sparsity is a multiple of some given sparsity.
MX instruction_MX(casadi_int k) const
Get the MX node corresponding to an instruction (MXFunction)
const SX sx_in(casadi_int iind) const
Get symbolic primitives equivalent to the input expressions.
Function mapaccum(casadi_int N, const Dict &opts=Dict()) const
Create a mapaccumulated version of this function.
const std::vector< SX > sx_out() const
Get symbolic primitives equivalent to the output expressions.
casadi_int numel_out(casadi_int ind) const
Get number of output elements.
size_t sz_iw() const
Get required length of iw field.
void call(const DMDict &arg, DMDict &res, bool always_inline=false, bool never_inline=false) const
Evaluate the function symbolically or numerically.
casadi_int n_out() const
Get the number of function outputs.
std::vector< casadi_int > instruction_output(casadi_int k) const
Location in the work vector for the output of the instruction.
const Sparsity & sparsity_in(const std::string &iname) const
Get sparsity of a given input.
std::string serialize(const Dict &opts=Dict()) const
Serialize.
static Function conditional(const std::string &name, const Function &f, const Dict &opts=Dict())
Conditional call to a function.
casadi_int n_in() const
Get the number of function inputs.
Function(const std::string &name, const std::map< std::string, SX > &dict, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts=Dict())
Construct an SX function.
static Function load(const std::string &filename)
Build function from serialization.
casadi_int size1_in(const std::string &iname) const
Get input dimension.
Definition: function.hpp:240
bool is_diff_in(casadi_int ind) const
Get differentiability of inputs/output.
DMDict convert_in(const std::vector< DM > &arg) const
Convert from/to input/output lists/map.
Function(const std::string &name, const std::vector< SX > &ex_in, const std::vector< SX > &ex_out, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts=Dict())
Construct an SX function.
Function map(const std::string &name, const std::string &parallelization, casadi_int n, const std::vector< std::string > &reduce_in, const std::vector< std::string > &reduce_out, const Dict &opts=Dict()) const
Map with reduction.
const std::string & name_out(casadi_int ind) const
Get output scheme name by index.
Function map(casadi_int n, const std::string &parallelization="serial") const
Create a mapped version of this function.
DMDict convert_out(const std::vector< DM > &arg) const
Convert from/to input/output lists/map.
static Function deserialize(DeserializingStream &s)
Build function from serialization.
casadi_int size2_out(const std::string &oname) const
Get output dimension.
Definition: function.hpp:256
size_t sz_w() const
Get required length of w field.
casadi_int numel_out(const std::string &oname) const
Get number of output elements.
Definition: function.hpp:304
Function mapaccum(const std::string &name, casadi_int N, casadi_int n_accum, const Dict &opts=Dict()) const
Create a mapaccumulated version of this function.
Function(const std::string &name, const std::map< std::string, MX > &dict, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts=Dict())
Construct an MX function.
SXDict convert_out(const std::vector< SX > &arg) const
Convert from/to input/output lists/map.
size_t sz_arg() const
Get required length of arg field.
void generate_out(const std::string &fname, const std::vector< DM > &arg)
Export an output file that can be checked with generated C code output.
std::string generate(const std::string &fname, const Dict &opts=Dict()) const
Export / Generate C code for the function.
bool is_a(const std::string &type, bool recursive=true) const
Check if the function is of a particular type.
double min_in(casadi_int ind) const
Get smallest input value.
static std::string type_name()
Get type name.
Definition: function.hpp:65
std::vector< double > nominal_out(casadi_int ind) const
Get nominal output value.
Function slice(const std::string &name, const std::vector< casadi_int > &order_in, const std::vector< casadi_int > &order_out, const Dict &opts=Dict()) const
returns a new function with a selection of inputs/outputs of the original
bool has_free() const
Does the function have free variables.
Function wrap_as_needed(const Dict &opts) const
Wrap in a Function with options.
casadi_int nnz_in() const
Get number of input nonzeros.
Function mapaccum(const std::string &name, casadi_int n, const std::vector< casadi_int > &accum_in, const std::vector< casadi_int > &accum_out, const Dict &opts=Dict()) const
Create a mapaccumulated version of this function.
void generate_lifted(Function &vdef_fcn, Function &vinit_fcn) const
Extract the functions needed for the Lifted Newton method.
double instruction_constant(casadi_int k) const
Get the floating point output argument of an instruction (SXFunction)
std::vector< std::string > get_free() const
Get free variables as a string.
Sparsity jac_sparsity(casadi_int oind, casadi_int iind, bool compact=false) const
Get, if necessary generate, the sparsity of a single Jacobian block.
bool uses_output() const
Do the derivative functions need nondifferentiated outputs?
bool has_function(const std::string &fname) const
Check if a particular dependency exists.
Function()
Default constructor, null pointer.
std::vector< MX > free_mx() const
Get all the free variables of the function.
const std::vector< MX > mx_out() const
Get symbolic primitives equivalent to the output expressions.
std::string generate(const Dict &opts=Dict()) const
Export / Generate C code for the function.
const std::vector< std::string > & name_out() const
Get output scheme.
void print_dimensions(std::ostream &stream=casadi::uout()) const
Print dimensions of inputs and outputs.
std::vector< MX > mapsum(const std::vector< MX > &x, const std::string &parallelization="serial") const
Evaluate symbolically in parallel and sum (matrix graph)
void call(const SXDict &arg, SXDict &res, bool always_inline=false, bool never_inline=false) const
Evaluate the function symbolically or numerically.
casadi_int size1_out(casadi_int ind) const
Get output dimension.
const Sparsity sparsity_jac(casadi_int iind, const std::string &oind, bool compact=false, bool symmetric=false) const
Definition: function.hpp:481
const SX sx_in(const std::string &iname) const
Get symbolic primitives equivalent to the input expressions.
Definition: function.hpp:941
const Sparsity & sparsity_in(casadi_int ind) const
Get sparsity of a given input.
void export_code(const std::string &lang, const std::string &fname, const Dict &options=Dict()) const
Export function in specific language.
void call(const std::vector< DM > &arg, std::vector< DM > &res, bool always_inline=false, bool never_inline=false) const
Evaluate the function symbolically or numerically.
static Function jit(const std::string &name, const std::string &body, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts=Dict())
To resolve ambiguity on some compilers.
casadi_int index_out(const std::string &name) const
Find the index for a string describing a particular entry of an output scheme.
std::vector< SX > free_sx() const
Get all the free variables of the function.
Dict stats(int mem=0) const
Get all statistics obtained at the end of the last evaluate call.
static Function if_else(const std::string &name, const Function &f_true, const Function &f_false, const Dict &opts=Dict())
Constructor (if-else)
SXDict convert_in(const std::vector< SX > &arg) const
Convert from/to input/output lists/map.
std::map< std::string, std::vector< std::string > > AuxOut
Definition: function.hpp:395
const SX sx_out(casadi_int oind) const
Get symbolic primitives equivalent to the output expressions.
std::vector< double > nz_from_in(const std::vector< DM > &arg) const
Convert from/to flat vector of input/output nonzeros.
Function map(casadi_int n, const std::string &parallelization, casadi_int max_num_threads) const
Function factory(const std::string &name, const std::vector< std::string > &s_in, const std::vector< std::string > &s_out, const AuxOut &aux=AuxOut(), const Dict &opts=Dict()) const
casadi_int size2_in(casadi_int ind) const
Get input dimension.
casadi_int instruction_id(casadi_int k) const
Identifier index of the instruction (SXFunction/MXFunction)
const Sparsity & sparsity_out(const std::string &iname) const
Get sparsity of a given output.
MXDict convert_out(const std::vector< MX > &arg) const
Convert from/to input/output lists/map.
const std::vector< SX > sx_in() const
Get symbolic primitives equivalent to the input expressions.
void assert_size_out(casadi_int i, casadi_int nrow, casadi_int ncol) const
Assert that an output dimension is equal so some given value.
void change_option(const std::string &option_name, const GenericType &option_value)
Change option after object creation for debugging.
std::vector< DM > generate_in(const std::string &fname)
Export an input file that can be passed to generate C code with a main.
Function jacobian_old(casadi_int iind, casadi_int oind) const
[DEPRECATED] Replaced by Function::factory.
static Function jit(const std::string &name, const std::string &body, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const std::vector< Sparsity > &sparsity_in, const std::vector< Sparsity > &sparsity_out, const Dict &opts=Dict())
To resolve ambiguity on some compilers.
static Function bspline(const std::string &name, const std::vector< std::vector< double > > &knots, const std::vector< double > &coeffs, const std::vector< casadi_int > &degree, casadi_int m=1, const Dict &opts=Dict())
BSpline evaluator function.
double default_in(casadi_int ind) const
Get default input value.
std::pair< casadi_int, casadi_int > size_in(const std::string &iname) const
Get input dimension.
Definition: function.hpp:244
bool has_option(const std::string &option_name) const
Does a particular option exist.
Generic data type, can hold different types such as bool, casadi_int, std::string etc.
MX - Matrix expression.
Definition: mx.hpp:84
Sparse matrix class. SX and DM are specializations.
Definition: matrix_decl.hpp:92
Helper class for Serialization.
SharedObject implements a reference counting framework similar for efficient and.
General sparsity class.
Definition: sparsity.hpp:99
The casadi namespace.
std::map< std::string, MX > MXDict
Definition: mx.hpp:948
CASADI_EXPORT std::ostream & uout()
std::vector< SX > SXVector
Definition: sx_fwd.hpp:37
std::vector< MX > MXVector
Definition: mx.hpp:945
std::map< std::string, SX > SXDict
Definition: sx_fwd.hpp:40
Matrix< SXElem > SX
Definition: sx_fwd.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::initializer_list< SX > SXIList
Definition: sx_fwd.hpp:38
std::initializer_list< MX > MXIList
Definition: mx.hpp:946
Matrix< double > DM
Definition: dm_fwd.hpp:33
void CASADI_EXPORT _function_buffer_eval(void *raw)
std::map< std::string, DM > DMDict
Definition: dm_fwd.hpp:36