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  * Copyright (C) 2018 Robert Bosch GmbH
9  *
10  * CasADi is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * CasADi is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with CasADi; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
27 #include "determinant.hpp"
28 #include "linsol_internal.hpp"
29 
30 namespace casadi {
31 
32  Determinant::Determinant(const MX& x, const Linsol& linsol) : linsol_(linsol) {
33  casadi_assert(x.is_square(), "Dimension mismatch. Matrix must be square, "
34  "but got " + x.dim() + " instead.");
35  set_dep(x);
37  }
38 
39  std::string Determinant::disp(const std::vector<std::string>& arg) const {
40  return "det(" + arg.at(0) + ")";
41  }
42 
43  int Determinant::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
54 
55  // Peform LU decomposition
56  if (linsol_.sfact(arg[0], mem)) return 1;
57  if (linsol_.nfact(arg[0], mem)) return 1;
58 
59  // Compute determinant
60  res[0][0] = linsol_.det(arg[0], mem);
61 
62  return 0;
63  }
64 
65  int Determinant::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
66  // Symbolic determinant via the sparse-QR plugin (cheap O(n^3), unlike cofactor)
67  SX A = SX::zeros(dep(0).sparsity());
68  std::copy(arg[0], arg[0]+dep(0).nnz(), A.ptr());
69  *res[0] = det(A, "symbolicqr").scalar();
70  return 0;
71  }
72 
73  size_t Determinant::codegen_sz_w() const {
74  return linsol_->sz_w_fact();
75  }
76 
78  const std::vector<casadi_int>& arg,
79  const std::vector<casadi_int>& res,
80  const std::vector<bool>& arg_is_ref,
81  std::vector<bool>& res_is_ref) const {
82  linsol_->generate_det(g, g.work(arg[0], dep(0).nnz(), arg_is_ref[0]),
83  g.workel(res[0]));
84  }
85 
86  void Determinant::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
87  const std::vector<bool>& unique) const {
88  res[0] = linsol_.det(arg[0]);
89  }
90 
91  void Determinant::ad_forward(const std::vector<std::vector<MX> >& fseed,
92  std::vector<std::vector<MX> >& fsens) const {
93  const MX& X = dep();
94  MX det_X = shared_from_this<MX>();
95  MX trans_inv_X = inv(X).T();
96  for (casadi_int d=0; d<fsens.size(); ++d) {
97  fsens[d][0] = det_X * dot(trans_inv_X, fseed[d][0]);
98  }
99  }
100 
101  void Determinant::ad_reverse(const std::vector<std::vector<MX> >& aseed,
102  std::vector<std::vector<MX> >& asens) const {
103  const MX& X = dep();
104  MX det_X = shared_from_this<MX>();
105  MX trans_inv_X = inv(X).T();
106  for (casadi_int d=0; d<aseed.size(); ++d) {
107  asens[d][0] += aseed[d][0]*det_X * trans_inv_X;
108  }
109  }
110 
113  s.pack("Determinant::linsol", linsol_);
114  }
115 
117  s.unpack("Determinant::linsol", linsol_);
118  }
119 
120 } // namespace casadi
Helper class for C code generation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string workel(casadi_int n) const
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
Definition: determinant.cpp:86
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
Definition: determinant.cpp:77
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:91
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: determinant.cpp:39
Determinant(const MX &x, const Linsol &linsol)
Constructor.
Definition: determinant.cpp:32
size_t codegen_sz_w() const override
Length of w the generated code needs (eval uses Linsol memory)
Definition: determinant.cpp:73
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: determinant.cpp:43
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: determinant.cpp:65
std::string dim(bool with_nz=false) const
Get string representation of dimensions.
bool is_square() const
Check if the matrix expression is square.
static Matrix< Scalar > zeros(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries zero.
virtual void generate_det(CodeGenerator &g, const std::string &A, const std::string &d) const
Generate C code that computes the determinant into scalar d.
virtual size_t sz_w_fact() const
Linear solver.
Definition: linsol.hpp:55
void nfact(const DM &A) const
Numeric factorization of the linear system.
Definition: linsol.cpp:130
double det(const DM &A) const
Matrix determinant.
Definition: linsol.cpp:195
void sfact(const DM &A) const
Symbolic factorization of the linear system, e.g. selecting pivots.
Definition: linsol.cpp:105
Node class for MX objects.
Definition: mx_node.hpp:51
const Sparsity & sparsity() const
Get the sparsity.
Definition: mx_node.hpp:410
casadi_int nnz(casadi_int i=0) const
Definition: mx_node.hpp:427
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
Definition: mx_node.hpp:392
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
Definition: mx_node.cpp:530
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
Definition: mx_node.cpp:224
void set_dep(const MX &dep)
Set unary dependency.
Definition: mx_node.cpp:228
MX - Matrix expression.
Definition: mx.hpp:92
MX T() const
Transpose the matrix.
Definition: mx.cpp:1095
Sparse matrix class. SX and DM are specializations.
Definition: matrix_decl.hpp:99
Scalar * ptr()
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
static Sparsity dense(casadi_int nrow, casadi_int ncol=1)
Create a dense rectangular sparsity pattern *.
Definition: sparsity.cpp:1028
The casadi namespace.
Definition: archiver.cpp:28
T dot(const std::vector< T > &a, const std::vector< T > &b)