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 CASADI_LINSOL_SYMBOLICQR_EXPORT 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 CASADI_LINSOL_SYMBOLICQR_EXPORT 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
125 
127  void serialize_body(SerializingStream &s) const override;
128 
131 
132  protected:
134  explicit SymbolicQr(DeserializingStream& s);
135  };
136 
137 } // namespace casadi
138 
140 #endif // CASADI_SYMBOLIC_QR_HPP
Helper class for Serialization.
Function object.
Definition: function.hpp:60
Sparse matrix class. SX and DM are specializations.
Definition: matrix_decl.hpp:99
Base class for FunctionInternal and LinsolInternal.
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
Helper class for Serialization.
General sparsity class.
Definition: sparsity.hpp:106
'symbolicqr' plugin for Linsol
Definition: symbolic_qr.hpp:70
void * alloc_mem() const override
Create memory block.
Definition: symbolic_qr.hpp:99
static const Options options_
Options.
Definition: symbolic_qr.hpp:91
void free_mem(void *mem) const override
Free memory block.
static LinsolInternal * creator(const std::string &name, const Sparsity &sp)
Create a new Linsol.
Definition: symbolic_qr.hpp:85
std::string class_name() const override
Readable name of the internal class.
Definition: symbolic_qr.hpp:82
static const std::string meta_doc
A documentation string.
const char * plugin_name() const override
Definition: symbolic_qr.hpp:79
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
const Options & get_options() const override
Options.
Definition: symbolic_qr.hpp:92
The casadi namespace.
Definition: archiver.cpp:28
std::vector< SXPtr > SXPtrV
Definition: symbolic_qr.hpp:46
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
SX * SXPtr
Definition: symbolic_qr.hpp:45
Options metadata for a class.
Definition: options.hpp:40
Memory for SymbolicQR
Definition: symbolic_qr.hpp:49
std::vector< casadi_int > iw
Definition: symbolic_qr.hpp:53
std::vector< double > q
Definition: symbolic_qr.hpp:60
std::vector< double * > res
Definition: symbolic_qr.hpp:52
std::vector< const double * > arg
Definition: symbolic_qr.hpp:51
std::vector< double > w
Definition: symbolic_qr.hpp:54