lapack_lu.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_LAPACK_LU_HPP
27 #define CASADI_LAPACK_LU_HPP
28 
29 #include "casadi/core/linsol_internal.hpp"
30 #include <casadi/interfaces/lapack/casadi_linsol_lapacklu_export.h>
31 
32 extern "C" {
34  void dgetrf_(int *m, int *n, double *a, int *lda, int *ipiv, int *info);
35 
37  void dgetrs_(char* trans, int *n, int *nrhs, double *a,
38  int *lda, int *ipiv, double *b, int *ldb, int *info);
39 
41  void dgeequ_(int *m, int *n, double *a, int *lda, double *r, double *c,
42  double *colcnd, double *rowcnd, double *amax, int *info);
43 
45  void dlaqge_(int *m, int *n, double *a, int *lda, double *r, double *c,
46  double *colcnd, double *rowcnd, double *amax, char *equed);
47 }
48 
49 namespace casadi {
50 
62  struct LapackLuMemory : public LinsolMemory {
63  // Matrix
64  std::vector<double> mat;
65 
67  std::vector<int> ipiv;
68 
70  std::vector<double> r, c;
71 
73  char equed;
74  };
75 
82  class LapackLu : public LinsolInternal {
83  public:
84  // Create a linear solver given a sparsity pattern and a number of right hand sides
85  LapackLu(const std::string& name, const Sparsity& sp);
86 
88  static LinsolInternal* creator(const std::string& name, const Sparsity& sp) {
89  return new LapackLu(name, sp);
90  }
91 
93  ~LapackLu() override;
94 
96 
97  static const Options options_;
98  const Options& get_options() const override { return options_;}
100 
102  void init(const Dict& opts) override;
103 
105  void* alloc_mem() const override { return new LapackLuMemory();}
106 
108  int init_mem(void* mem) const override;
109 
111  void free_mem(void *mem) const override { delete static_cast<LapackLuMemory*>(mem);}
112 
113  // Factorize the linear system
114  int nfact(void* mem, const double* A) const override;
115 
116  // Solve the linear system
117  int solve(void* mem, const double* A, double* x, casadi_int nrhs, bool tr) const override;
118 
120  static const std::string meta_doc;
121 
123  void serialize_body(SerializingStream &s) const override;
124 
126  static ProtoFunction* deserialize(DeserializingStream& s) { return new LapackLu(s); }
127 
128  protected:
130  explicit LapackLu(DeserializingStream& s);
131 
133  bool equilibriate_;
134 
136  bool allow_equilibration_failure_;
137 
138  // Get name of the plugin
139  const char* plugin_name() const override { return "lapacklu";}
140 
141  // Get name of the class
142  std::string class_name() const override { return "LapackLu";}
143  };
144 
146 
147 } // namespace casadi
148 
149 #endif // CASADI_LAPACK_LU_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.