lsqr.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_LSQR_HPP
27 #define CASADI_LSQR_HPP
28 
29 #include "casadi/core/linsol_internal.hpp"
30 #include <casadi/solvers/casadi_linsol_lsqr_export.h>
31 
43 
44 namespace casadi {
45 
47  struct LsqrMemory : public LinsolMemory {
48  // Work vectors
49  std::vector<const double*> arg;
50  std::vector<double> w;
51 
52  std::vector<double> A;
53  };
54 
62  class Lsqr : public LinsolInternal {
63  public:
64  // Constructor
65  Lsqr(const std::string& name, const Sparsity& sp);
66 
67  // Destructor
68  ~Lsqr() override;
69 
70  // Get name of the plugin
71  const char* plugin_name() const override { return "lsqr";}
72 
73  // Name of the class
74  std::string class_name() const override { return "Lsqr";}
75 
77  static LinsolInternal* creator(const std::string& name, const Sparsity& sp) {
78  return new Lsqr(name, sp);
79  }
80 
82  void* alloc_mem() const override { return new LsqrMemory();}
83 
85  int init_mem(void* mem) const override;
86 
88  void free_mem(void *mem) const override { delete static_cast<LsqrMemory*>(mem);}
89 
90  // Factorize the linear system
91  int nfact(void* mem, const double* A) const override;
92 
93  // Solve the linear system
94  int solve(void* mem, const double* A, double* x, casadi_int nrhs, bool tr) const override;
95 
97  void generate(CodeGenerator& g, const std::string& A, const std::string& x,
98  casadi_int nrhs, bool tr) const override;
99 
101  static const std::string meta_doc;
102 
104  static ProtoFunction* deserialize(DeserializingStream& s) { return new Lsqr(s); }
105 
106  protected:
108  explicit Lsqr(DeserializingStream& s) : LinsolInternal(s) {}
109  };
110 
111 } // namespace casadi
112 
114 #endif // CASADI_LSQR_HPP
The casadi namespace.