Cantera  3.2.0
Loading...
Searching...
No Matches
DenseMatrix.h
Go to the documentation of this file.
1/**
2 * @file DenseMatrix.h
3 * Headers for the DenseMatrix object, which deals with dense rectangular matrices and
4 * description of the numerics groupings of objects
5 * (see @ref numerics and @link Cantera::DenseMatrix DenseMatrix @endlink) .
6 */
7
8// This file is part of Cantera. See License.txt in the top-level directory or
9// at https://cantera.org/license.txt for license and copyright information.
10
11#ifndef CT_DENSEMATRIX_H
12#define CT_DENSEMATRIX_H
13
16#include "cantera/base/Array.h"
17
18namespace Cantera
19{
20/**
21 * @defgroup numerics Numerical Utilities
22 *
23 * @details %Cantera contains some capabilities for solving nonlinear equations and
24 * integrating both ODE and DAE equation systems in time.
25 */
26
27/**
28 * @defgroup matrices Matrix Handling
29 * Classes and methods implementing matrix operations.
30 * @ingroup numerics
31 */
32
33//! A class for full (non-sparse) matrices with Fortran-compatible data storage,
34//! which adds matrix operations to class Array2D.
35/*!
36 * The dense matrix class adds matrix operations onto the Array2D class. These
37 * matrix operations are carried out by the appropriate BLAS and LAPACK routines
38 *
39 * @deprecated After %Cantera 3.2, all errors will be handled by throwing exceptions.
40 * The #m_useReturnErrorCode and #m_printLevel member variables will be removed,
41 * and the `solve` and `invert` methods will be changed to return `void`.
42 *
43 * Error handling from BLAS and LAPACK are handled via the following
44 * formulation. Depending on a variable, a singular matrix or other terminal
45 * error condition from LAPACK is handled by either throwing an exception or
46 * by returning the error code condition to the calling routine.
47 *
48 * The int variable, m_useReturnErrorCode, determines which method is used. The
49 * default value of zero means that an exception is thrown. A value of 1 means
50 * that a return code is used.
51 *
52 * Reporting of these LAPACK error conditions is handled by the class variable
53 * m_printLevel. The default is for no reporting. If m_printLevel is nonzero,
54 * the error condition is reported to Cantera's log file.
55 *
56 * @ingroup matrices
57 */
58class DenseMatrix : public Array2D
59{
60public:
61 //! Default Constructor
62 DenseMatrix() = default;
63
64 //! Constructor.
65 /*!
66 * Create an @c n by @c m matrix, and initialize all elements to @c v.
67 *
68 * @param n New number of rows
69 * @param m New number of columns
70 * @param v Default fill value. defaults to zero.
71 */
72 DenseMatrix(size_t n, size_t m, double v = 0.0);
73
74 DenseMatrix(const DenseMatrix& y);
75 DenseMatrix& operator=(const DenseMatrix& y);
76
77 //! Resize the matrix
78 /*!
79 * Resize the matrix to n rows by m cols.
80 *
81 * @param n New number of rows
82 * @param m New number of columns
83 * @param v Default fill value. defaults to zero.
84 */
85 void resize(size_t n, size_t m, double v=0.0) override;
86
87 virtual double* const* colPts();
88
89 //! Return a const vector of const pointers to the columns
90 /*!
91 * Note, the Jacobian can not be altered by this routine, and therefore the
92 * member function is const.
93 *
94 * @returns a vector of pointers to the top of the columns of the matrices.
95 */
96 const double* const* const_colPts() const;
97
98 virtual void mult(const double* b, double* prod) const;
99
100 //! Multiply A*B and write result to @c prod.
101 /*!
102 * Take this matrix to be of size NxM.
103 * @param[in] b DenseMatrix B of size MxP
104 * @param[out] prod DenseMatrix prod size NxP
105 */
106 virtual void mult(const DenseMatrix& b, DenseMatrix& prod) const;
107
108 //! Left-multiply the matrix by transpose(b), and write the result to prod.
109 /*!
110 * @param b left multiply by this vector. The length must be equal to n
111 * the number of rows in the matrix.
112 * @param prod Resulting vector. This is of length m, the number of columns
113 * in the matrix
114 */
115 virtual void leftMult(const double* const b, double* const prod) const;
116
117 //! Return a changeable value of the pivot vector
118 /*!
119 * @returns a reference to the pivot vector as a vector<int>
120 */
121 vector<int>& ipiv();
122
123 //! Return a changeable value of the pivot vector
124 /*!
125 * @returns a reference to the pivot vector as a vector<int>
126 */
127 const vector<int>& ipiv() const {
128 return m_ipiv;
129 }
130
131protected:
132 //! Vector of pivots. Length is equal to the max of m and n.
133 vector<int> m_ipiv;
134
135 //! Vector of column pointers
136 vector<double*> m_colPts;
137
138public:
139 //! Error Handling Flag
140 /*!
141 * The default is to set this to 0. In this case, if a factorization is
142 * requested and can't be achieved, a CESingularMatrix exception is
143 * triggered. No return code is used, because an exception is thrown. If
144 * this is set to 1, then an exception is not thrown. Routines return with
145 * an error code, that is up to the calling routine to handle correctly.
146 * Negative return codes always throw an exception.
147 *
148 * @deprecated To be removed after %Cantera 3.2. Errors always throw exceptions.
149 */
151
152 //! Print Level
153 /*!
154 * Printing is done to the log file using the routine writelogf().
155 *
156 * Level of printing that is carried out. Only error conditions are printed
157 * out, if this value is nonzero.
158 * @deprecated To be removed after %Cantera 3.2. Errors always throw exceptions.
159 */
161};
162
163
164//! Solve Ax = b. Array b is overwritten on exit with x.
165/*!
166 * The solve function uses the LAPACK routine dgetrf to invert the m xy n matrix.
167 *
168 * The factorization has the form
169 *
170 * A = P * L * U
171 *
172 * where P is a permutation matrix, L is lower triangular with unit diagonal
173 * elements (lower trapezoidal if m > n), and U is upper triangular (upper
174 * trapezoidal if m < n).
175 *
176 * The system is then solved using the LAPACK routine dgetrs
177 *
178 * @param A Dense matrix to be factored
179 * @param b RHS(s) to be solved.
180 * @param nrhs Number of right hand sides to solve
181 * @param ldb Leading dimension of b, if nrhs > 1
182 * @deprecated After %Cantera 3.2, the return type will be `void`.
183 */
184int solve(DenseMatrix& A, double* b, size_t nrhs=1, size_t ldb=0);
185
186//! Solve Ax = b for multiple right-hand-side vectors.
187/*!
188 * @param A Dense matrix to be factored
189 * @param b Dense matrix of RHS's. Each column is a RHS
190 * @deprecated After %Cantera 3.2, the return type will be `void`.
191 */
192int solve(DenseMatrix& A, DenseMatrix& b);
193
194//! Multiply @c A*b and return the result in @c prod. Uses BLAS routine DGEMV.
195/*!
196 * @f[
197 * prod_i = sum^N_{j = 1}{A_{ij} b_j}
198 * @f]
199 *
200 * @param[in] A Dense Matrix A with M rows and N columns
201 * @param[in] b vector b with length N
202 * @param[out] prod vector prod length = M
203 */
204void multiply(const DenseMatrix& A, const double* const b, double* const prod);
205
206//! Multiply @c A*b and add it to the result in @c prod. Uses BLAS routine DGEMV.
207/*!
208 * @f[
209 * prod_i += sum^N_{j = 1}{A_{ij} b_j}
210 * @f]
211 *
212 * @param[in] A Dense Matrix A with M rows and N columns
213 * @param[in] b vector b with length N
214 * @param[out] prod vector prod length = M
215 */
216void increment(const DenseMatrix& A, const double* const b, double* const prod);
217
218//! invert A. A is overwritten with A^-1.
219/*!
220 * @param A Invert the matrix A and store it back in place
221 * @param nn Size of A. This defaults to -1, which means that the number of
222 * rows is used as the default size of n
223 * @deprecated After %Cantera 3.2, the return type will be `void`.
224 */
225int invert(DenseMatrix& A, size_t nn=npos);
226
227}
228
229#endif
Header file for class Cantera::Array2D.
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition Array.h:32
A class for full (non-sparse) matrices with Fortran-compatible data storage, which adds matrix operat...
Definition DenseMatrix.h:59
virtual void leftMult(const double *const b, double *const prod) const
Left-multiply the matrix by transpose(b), and write the result to prod.
int m_printLevel
Print Level.
int m_useReturnErrorCode
Error Handling Flag.
const vector< int > & ipiv() const
Return a changeable value of the pivot vector.
void resize(size_t n, size_t m, double v=0.0) override
Resize the matrix.
vector< int > & ipiv()
Return a changeable value of the pivot vector.
DenseMatrix()=default
Default Constructor.
const double *const * const_colPts() const
Return a const vector of const pointers to the columns.
vector< double * > m_colPts
Vector of column pointers.
vector< int > m_ipiv
Vector of pivots. Length is equal to the max of m and n.
This file contains definitions of constants, types and terms that are used in internal routines and a...
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
void increment(const DenseMatrix &A, const double *b, double *prod)
Multiply A*b and add it to the result in prod. Uses BLAS routine DGEMV.
const size_t npos
index returned by functions to indicate "no position"
Definition ct_defs.h:180
void multiply(const DenseMatrix &A, const double *const b, double *const prod)
Multiply A*b and return the result in prod. Uses BLAS routine DGEMV.
int invert(DenseMatrix &A, size_t nn)
invert A. A is overwritten with A^-1.
int solve(DenseMatrix &A, double *b, size_t nrhs, size_t ldb)
Solve Ax = b. Array b is overwritten on exit with x.