callback_internal.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_CALLBACK_INTERNAL_HPP
26 #define CASADI_CALLBACK_INTERNAL_HPP
27 
28 #include "callback.hpp"
29 #include "function_internal.hpp"
30 
31 namespace casadi {
32 
33  class CASADI_EXPORT CallbackInternal : public FunctionInternal {
34  friend class CallbackFunction;
35  public:
36 
40  explicit CallbackInternal(const std::string& name, Callback* self);
41 
45  ~CallbackInternal() override;
46 
50  std::string class_name() const override {return "CallbackInternal";}
51 
53 
56  size_t get_n_in() override;
57  size_t get_n_out() override;
59 
61 
64  Sparsity get_sparsity_in(casadi_int i) override;
65  Sparsity get_sparsity_out(casadi_int i) override;
67 
69 
72  std::string get_name_in(casadi_int i) override;
73  std::string get_name_out(casadi_int i) override;
75 
79  void init(const Dict& opts) override;
80 
84  void finalize() override;
85 
87 
90  std::vector<DM> eval_dm(const std::vector<DM>& arg) const override;
91  bool has_eval_dm() const override { return !has_eval_buffer_;}
93 
97  virtual int eval(const double** arg, double** res,
98  casadi_int* iw, double* w, void* mem) const override;
99  bool has_eval_buffer() const;
100 
104  bool uses_output() const override;
105 
107 
110  bool has_jacobian() const override;
111  Function get_jacobian(const std::string& name,
112  const std::vector<std::string>& inames,
113  const std::vector<std::string>& onames,
114  const Dict& opts) const override;
116 
118 
121  bool has_jac_sparsity(casadi_int oind, casadi_int iind) const override;
122  Sparsity get_jac_sparsity(casadi_int oind, casadi_int iind, bool symmetric) const override;
124 
126 
129  bool has_forward(casadi_int nfwd) const override;
130  Function get_forward(casadi_int nfwd, const std::string& name,
131  const std::vector<std::string>& inames,
132  const std::vector<std::string>& onames,
133  const Dict& opts) const override;
135 
137 
140  bool has_reverse(casadi_int nadj) const override;
141  Function get_reverse(casadi_int nadj, const std::string& name,
142  const std::vector<std::string>& inames,
143  const std::vector<std::string>& onames,
144  const Dict& opts) const override;
146 
151 
152  // For buffered evaluation
153  std::vector<casadi_int> sizes_arg_, sizes_res_;
155  };
156 
157 } // namespace casadi
158 
159 #endif // CASADI_CALLBACK_INTERNAL_HPP
void finalize() override
Finalize the object creation.
bool has_jacobian() const override
Return Jacobian of all input elements with respect to all output elements.
void init(const Dict &opts) override
Initialize.
std::string get_name_out(casadi_int i) override
Names of function input and outputs.
std::string get_name_in(casadi_int i) override
Names of function input and outputs.
size_t get_n_out() override
Number of function inputs and outputs.
Callback * self_
Pointer to the public class.
bool has_forward(casadi_int nfwd) const override
Return function that calculates forward derivatives.
std::vector< casadi_int > sizes_arg_
bool has_jac_sparsity(casadi_int oind, casadi_int iind) const override
Return sparsity of Jacobian of an output respect to an input.
Function get_jacobian(const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const override
Return Jacobian of all input elements with respect to all output elements.
~CallbackInternal() override
Destructor.
size_t get_n_in() override
Number of function inputs and outputs.
virtual int eval(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const override
Evaluate numerically.
std::vector< DM > eval_dm(const std::vector< DM > &arg) const override
Evaluate with DM matrices.
bool has_eval_dm() const override
Evaluate with DM matrices.
bool uses_output() const override
Do the derivative functions need nondifferentiated outputs?
Function get_reverse(casadi_int nadj, const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const override
Return function that calculates adjoint derivatives.
Function get_forward(casadi_int nfwd, const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const override
Return function that calculates forward derivatives.
Sparsity get_sparsity_out(casadi_int i) override
Sparsities of function inputs and outputs.
bool has_reverse(casadi_int nadj) const override
Return function that calculates adjoint derivatives.
std::string class_name() const override
Get type name.
Sparsity get_jac_sparsity(casadi_int oind, casadi_int iind, bool symmetric) const override
Return sparsity of Jacobian of an output respect to an input.
Sparsity get_sparsity_in(casadi_int i) override
Sparsities of function inputs and outputs.
bool has_eval_buffer() const
CallbackInternal(const std::string &name, Callback *self)
Constructor.
Callback function functionality.
Function object.
Definition: function.hpp:60
General sparsity class.
Definition: sparsity.hpp:99
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.