symbolic_qr.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_SYMBOLIC_QR_HPP
27 #define CASADI_SYMBOLIC_QR_HPP
28 
29 #include "casadi/core/linsol_internal.hpp"
30 #include <casadi/solvers/casadi_linsol_symbolicqr_export.h>
31 
43 
44 namespace casadi {
45  typedef SX* SXPtr;
46  typedef std::vector<SXPtr> SXPtrV;
47 
49  struct SymbolicQrMemory : public LinsolMemory {
50  // Work vectors
51  std::vector<const double*> arg;
52  std::vector<double*> res;
53  std::vector<casadi_int> iw;
54  std::vector<double> w;
55 
56  // Allocate memory for a function
57  void alloc(const Function& f);
58 
59  // Storage for QR factorization
60  std::vector<double> q, r;
61  };
62 
70  class SymbolicQr : public LinsolInternal {
71  public:
72  // Constructor
73  SymbolicQr(const std::string& name, const Sparsity& sp);
74 
75  // Destructor
76  ~SymbolicQr() override;
77 
78  // Get name of the plugin
79  const char* plugin_name() const override { return "symbolicqr";}
80 
81  // Name of the class
82  std::string class_name() const override { return "SymbolicQr";}
83 
85  static LinsolInternal* creator(const std::string& name, const Sparsity& sp) {
86  return new SymbolicQr(name, sp);
87  }
88 
92  // Keep the inherited numeric det(void*, const double*) visible: the static
93  // overload below would otherwise hide it (-Werror=overloaded-virtual on MinGW).
94  using LinsolInternal::det;
95  static SX det(const SX& A, const Dict& opts);
96 
98 
99  static const Options options_;
100  const Options& get_options() const override { return options_;}
102 
103  // Initialize
104  void init(const Dict& opts) override;
105 
107  void* alloc_mem() const override { return new SymbolicQrMemory();}
108 
110  int init_mem(void* mem) const override;
111 
113  void free_mem(void *mem) const override { delete static_cast<SymbolicQrMemory*>(mem);}
114 
115  // Factorize the linear system
116  int nfact(void* mem, const double* A) const override;
117 
118  // Solve the linear system
119  int solve(void* mem, const double* A, double* x, casadi_int nrhs, bool tr) const override;
120 
122  void linsol_eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w, void* mem,
123  bool tr, casadi_int nrhs) const override;
124 
126  static const std::string meta_doc;
127 
128  // Functions for factorization and (optionally transposed) solve
129  Function factorize_, solve_, solveT_;
130 
131  // Generated function options
132  Dict fopts_;
133 
135  void serialize_body(SerializingStream &s) const override;
136 
138  static ProtoFunction* deserialize(DeserializingStream& s) { return new SymbolicQr(s); }
139 
140  protected:
142  explicit SymbolicQr(DeserializingStream& s);
143  };
144 
145 } // namespace casadi
146 
148 #endif // CASADI_SYMBOLIC_QR_HPP
The casadi namespace.
Definition: archiver.hpp:32
Matrix< SXElem > SX
Definition: sx_fwd.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.