determinant.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 "determinant.hpp"
27 
28 namespace casadi {
29 
31  casadi_assert(x.is_square(), "Dimension mismatch. Matrix must be square, "
32  "but got " + x.dim() + " instead.");
33  set_dep(x);
35  }
36 
37  std::string Determinant::disp(const std::vector<std::string>& arg) const {
38  return "det(" + arg.at(0) + ")";
39  }
40 
41  void Determinant::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res) const {
42  res[0] = det(arg[0]);
43  }
44 
45  void Determinant::ad_forward(const std::vector<std::vector<MX> >& fseed,
46  std::vector<std::vector<MX> >& fsens) const {
47  const MX& X = dep();
48  MX det_X = shared_from_this<MX>();
49  MX trans_inv_X = inv(X).T();
50  for (casadi_int d=0; d<fsens.size(); ++d) {
51  fsens[d][0] = det_X * dot(trans_inv_X, fseed[d][0]);
52  }
53  }
54 
55  void Determinant::ad_reverse(const std::vector<std::vector<MX> >& aseed,
56  std::vector<std::vector<MX> >& asens) const {
57  const MX& X = dep();
58  MX det_X = shared_from_this<MX>();
59  MX trans_inv_X = inv(X).T();
60  for (casadi_int d=0; d<aseed.size(); ++d) {
61  asens[d][0] += aseed[d][0]*det_X * trans_inv_X;
62  }
63  }
64 
65 } // namespace casadi
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Definition: determinant.cpp:41
Determinant(const MX &x)
Constructor.
Definition: determinant.cpp:30
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: determinant.cpp:55
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: determinant.cpp:45
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: determinant.cpp:37
std::string dim(bool with_nz=false) const
Get string representation of dimensions.
bool is_square() const
Check if the matrix expression is square.
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
Definition: mx_node.hpp:354
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
T dot(const std::vector< T > &a, const std::vector< T > &b)