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 
90 
91  static const Options options_;
92  const Options& get_options() const override { return options_;}
94 
95  // Initialize
96  void init(const Dict& opts) override;
97 
99  void* alloc_mem() const override { return new SymbolicQrMemory();}
100 
102  int init_mem(void* mem) const override;
103 
105  void free_mem(void *mem) const override { delete static_cast<SymbolicQrMemory*>(mem);}
106 
107  // Factorize the linear system
108  int nfact(void* mem, const double* A) const override;
109 
110  // Solve the linear system
111  int solve(void* mem, const double* A, double* x, casadi_int nrhs, bool tr) const override;
112 
114  void linsol_eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w, void* mem,
115  bool tr, casadi_int nrhs) const override;
116 
118  static const std::string meta_doc;
119 
120  // Functions for factorization and (optionally transposed) solve
121  Function factorize_, solve_, solveT_;
122 
123  // Generated function options
124  Dict fopts_;
125 
127  void serialize_body(SerializingStream &s) const override;
128 
130  static ProtoFunction* deserialize(DeserializingStream& s) { return new SymbolicQr(s); }
131 
132  protected:
134  explicit SymbolicQr(DeserializingStream& s);
135  };
136 
137 } // namespace casadi
138 
140 #endif // CASADI_SYMBOLIC_QR_HPP
The casadi namespace.
Matrix< SXElem > SX
Definition: sx_fwd.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.