inverse.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 "inverse.hpp"
27 
28 namespace casadi {
29 
30  Inverse::Inverse(const MX& x) {
31  casadi_assert(x.size1()==x.size2(),
32  "Inverse: matrix must be square, but you supllied " + x.dim());
33  set_dep(x);
35  }
36 
37  std::string Inverse::disp(const std::vector<std::string>& arg) const {
38  return "inv(" + arg.at(0) + ")";
39  }
40 
41  void Inverse::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res) const {
42  res[0] = inv(arg[0]);
43  }
44 
45  void Inverse::ad_forward(const std::vector<std::vector<MX> >& fseed,
46  std::vector<std::vector<MX> >& fsens) const {
47  MX inv_X = shared_from_this<MX>();
48  for (casadi_int d=0; d<fsens.size(); ++d) {
49  fsens[d][0] = -mtimes(inv_X, mtimes(fseed[d][0], inv_X));
50  }
51  }
52 
53  void Inverse::ad_reverse(const std::vector<std::vector<MX> >& aseed,
54  std::vector<std::vector<MX> >& asens) const {
55  MX inv_X = shared_from_this<MX>();
56  MX trans_inv_X = inv_X.T();
57  for (casadi_int d=0; d<aseed.size(); ++d) {
58  asens[d][0] -= mtimes(trans_inv_X, mtimes(aseed[d][0], trans_inv_X));
59  }
60  }
61 
62 } // namespace casadi
casadi_int size2() const
Get the second dimension (i.e. number of columns)
casadi_int size1() const
Get the first dimension (i.e. number of rows)
std::string dim(bool with_nz=false) const
Get string representation of dimensions.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Definition: inverse.cpp:41
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: inverse.cpp:53
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: inverse.cpp:45
Inverse(const MX &x)
Constructor.
Definition: inverse.cpp:30
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: inverse.cpp:37
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
Definition: mx_node.cpp:222
void set_dep(const MX &dep)
Set unary dependency.
Definition: mx_node.cpp:226
MX - Matrix expression.
Definition: mx.hpp:92
MX T() const
Transpose the matrix.
Definition: mx.cpp:1029
static Sparsity dense(casadi_int nrow, casadi_int ncol=1)
Create a dense rectangular sparsity pattern *.
Definition: sparsity.cpp:1012
The casadi namespace.
Definition: archiver.cpp:28