einstein.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 
26 #ifndef CASADI_EINSTEIN_HPP
27 #define CASADI_EINSTEIN_HPP
28 
29 #include "mx_node.hpp"
30 
32 
33 namespace casadi {
40  class CASADI_EXPORT Einstein : public MXNode {
41  public:
42 
46  Einstein(const MX& C, const MX& A, const MX& B,
47  const std::vector<casadi_int>& dim_c, const std::vector<casadi_int>& dim_a,
48  const std::vector<casadi_int>& dim_b,
49  const std::vector<casadi_int>& c, const std::vector<casadi_int>& a,
50  const std::vector<casadi_int>& b);
51 
55  ~Einstein() override {}
56 
60  std::string disp(const std::vector<std::string>& arg) const override;
61 
65  void generate(CodeGenerator& g,
66  const std::vector<casadi_int>& arg,
67  const std::vector<casadi_int>& res,
68  const std::vector<bool>& arg_is_ref,
69  std::vector<bool>& res_is_ref) const override;
70 
72  template<typename T>
73  int eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const;
74 
76  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
77 
79  int eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const override;
80 
84  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
85  const std::vector<bool>& unique={}) const override;
86 
90  void ad_forward(const std::vector<std::vector<MX> >& fseed,
91  std::vector<std::vector<MX> >& fsens) const override;
92 
96  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
97  std::vector<std::vector<MX> >& asens) const override;
98 
102  int sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
103 
107  int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
108 
112  casadi_int op() const override { return OP_EINSTEIN;}
113 
115  casadi_int n_inplace() const override { return 1;}
116 
120  bool is_equal(const MXNode* node, casadi_int depth) const override {
121  return sameOpAndDeps(node, depth) && dynamic_cast<const Einstein*>(node)!=nullptr;
122  }
123 
127  void serialize_body(SerializingStream& s) const override;
128 
132  static MXNode* deserialize(DeserializingStream& s) { return new Einstein(s); }
133 
134  protected:
138  explicit Einstein(DeserializingStream& s);
139 
140  public:
141 
145  size_t sz_w() const override { return sparsity().size1();}
146 
148  Dict info() const override {
149  return {{"dim_a", dim_a_}, {"dim_b", dim_b_}, {"dim_c", dim_c_},
150  {"a", a_}, {"b", b_}, {"c", c_},
151  {"iter_dims", iter_dims_},
152  {"strides_a", strides_a_}, {"strides_b", strides_b_}, {"strides_c", strides_c_},
153  {"n_iter", n_iter_}};
154  }
155 
157  std::vector<casadi_int> dim_c_, dim_a_, dim_b_;
159  std::vector<casadi_int> c_, a_, b_;
160 
161  std::vector<casadi_int> iter_dims_;
162 
163  std::vector<casadi_int> strides_a_;
164  std::vector<casadi_int> strides_b_;
165  std::vector<casadi_int> strides_c_;
166 
167  casadi_int n_iter_;
168 
169  };
170 
171 
172 } // namespace casadi
174 
175 #endif // CASADI_EINSTEIN_HPP
Helper class for C code generation.
Helper class for Serialization.
An MX atomic for an Einstein product,.
Definition: einstein.hpp:40
size_t sz_w() const override
Get required length of w field.
Definition: einstein.hpp:145
casadi_int n_iter_
Definition: einstein.hpp:167
~Einstein() override
Destructor.
Definition: einstein.hpp:55
Dict info() const override
Definition: einstein.hpp:148
std::vector< casadi_int > dim_a_
Definition: einstein.hpp:157
std::vector< casadi_int > strides_a_
Definition: einstein.hpp:163
casadi_int op() const override
Get the operation.
Definition: einstein.hpp:112
std::vector< casadi_int > iter_dims_
Definition: einstein.hpp:161
casadi_int n_inplace() const override
Can the operation be performed inplace (i.e. overwrite the result)
Definition: einstein.hpp:115
std::vector< casadi_int > strides_c_
Definition: einstein.hpp:165
bool is_equal(const MXNode *node, casadi_int depth) const override
Check if two nodes are equivalent up to a given depth.
Definition: einstein.hpp:120
std::vector< casadi_int > a_
Definition: einstein.hpp:159
std::vector< casadi_int > strides_b_
Definition: einstein.hpp:164
static MXNode * deserialize(DeserializingStream &s)
Deserialize without type information.
Definition: einstein.hpp:132
Node class for MX objects.
Definition: mx_node.hpp:51
MX - Matrix expression.
Definition: mx.hpp:92
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
Helper class for Serialization.
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
@ OP_EINSTEIN
Definition: calculus.hpp:193