Cantera  3.3.0a1
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 * @ingroup matrices
40 */
41class DenseMatrix : public Array2D
42{
43public:
44 //! Default Constructor
45 DenseMatrix() = default;
46
47 //! Constructor.
48 /*!
49 * Create an @c n by @c m matrix, and initialize all elements to @c v.
50 *
51 * @param n New number of rows
52 * @param m New number of columns
53 * @param v Default fill value. defaults to zero.
54 */
55 DenseMatrix(size_t n, size_t m, double v = 0.0);
56
57 DenseMatrix(const DenseMatrix& y);
58 DenseMatrix& operator=(const DenseMatrix& y);
59
60 //! Resize the matrix
61 /*!
62 * Resize the matrix to n rows by m cols.
63 *
64 * @param n New number of rows
65 * @param m New number of columns
66 * @param v Default fill value. defaults to zero.
67 */
68 void resize(size_t n, size_t m, double v=0.0) override;
69
70 virtual double* const* colPts();
71
72 //! Return a const vector of const pointers to the columns
73 /*!
74 * Note, the Jacobian can not be altered by this routine, and therefore the
75 * member function is const.
76 *
77 * @returns a vector of pointers to the top of the columns of the matrices.
78 */
79 const double* const* const_colPts() const;
80
81 virtual void mult(const double* b, double* prod) const;
82
83 //! Multiply A*B and write result to @c prod.
84 /*!
85 * Take this matrix to be of size NxM.
86 * @param[in] b DenseMatrix B of size MxP
87 * @param[out] prod DenseMatrix prod size NxP
88 */
89 virtual void mult(const DenseMatrix& b, DenseMatrix& prod) const;
90
91 //! Left-multiply the matrix by transpose(b), and write the result to prod.
92 /*!
93 * @param b left multiply by this vector. The length must be equal to n
94 * the number of rows in the matrix.
95 * @param prod Resulting vector. This is of length m, the number of columns
96 * in the matrix
97 */
98 virtual void leftMult(const double* const b, double* const prod) const;
99
100 //! Return a changeable value of the pivot vector
101 /*!
102 * @returns a reference to the pivot vector as a vector<int>
103 */
104 vector<int>& ipiv();
105
106 //! Return a changeable value of the pivot vector
107 /*!
108 * @returns a reference to the pivot vector as a vector<int>
109 */
110 const vector<int>& ipiv() const {
111 return m_ipiv;
112 }
113
114protected:
115 //! Vector of pivots. Length is equal to the max of m and n.
116 vector<int> m_ipiv;
117
118 //! Vector of column pointers
119 vector<double*> m_colPts;
120};
121
122
123//! Solve Ax = b. Array b is overwritten on exit with x.
124/*!
125 * The solve function uses the LAPACK routine dgetrf to invert the m xy n matrix.
126 *
127 * The factorization has the form
128 *
129 * A = P * L * U
130 *
131 * where P is a permutation matrix, L is lower triangular with unit diagonal
132 * elements (lower trapezoidal if m > n), and U is upper triangular (upper
133 * trapezoidal if m < n).
134 *
135 * The system is then solved using the LAPACK routine dgetrs
136 *
137 * @param A Dense matrix to be factored
138 * @param b RHS(s) to be solved.
139 * @param nrhs Number of right hand sides to solve
140 * @param ldb Leading dimension of b, if nrhs > 1
141 */
142void solve(DenseMatrix& A, double* b, size_t nrhs=1, size_t ldb=0);
143
144//! Solve Ax = b for multiple right-hand-side vectors.
145/*!
146 * @param A Dense matrix to be factored
147 * @param b Dense matrix of RHS's. Each column is a RHS
148 */
149void solve(DenseMatrix& A, DenseMatrix& b);
150
151//! Multiply @c A*b and return the result in @c prod. Uses BLAS routine DGEMV.
152/*!
153 * @f[
154 * prod_i = sum^N_{j = 1}{A_{ij} b_j}
155 * @f]
156 *
157 * @param[in] A Dense Matrix A with M rows and N columns
158 * @param[in] b vector b with length N
159 * @param[out] prod vector prod length = M
160 */
161void multiply(const DenseMatrix& A, const double* const b, double* const prod);
162
163//! Multiply @c A*b and add it to the result in @c prod. Uses BLAS routine DGEMV.
164/*!
165 * @f[
166 * prod_i += sum^N_{j = 1}{A_{ij} b_j}
167 * @f]
168 *
169 * @param[in] A Dense Matrix A with M rows and N columns
170 * @param[in] b vector b with length N
171 * @param[out] prod vector prod length = M
172 */
173void increment(const DenseMatrix& A, const double* const b, double* const prod);
174
175//! invert A. A is overwritten with A^-1.
176/*!
177 * @param A Invert the matrix A and store it back in place
178 * @param nn Size of A. This defaults to -1, which means that the number of
179 * rows is used as the default size of n
180 */
181void invert(DenseMatrix& A, size_t nn=npos);
182
183}
184
185#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:42
virtual void leftMult(const double *const b, double *const prod) const
Left-multiply the matrix by transpose(b), and write the result to prod.
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.
void solve(DenseMatrix &A, double *b, size_t nrhs, size_t ldb)
Solve Ax = b. Array b is overwritten on exit with x.
void invert(DenseMatrix &A, size_t nn)
invert A. A is overwritten with A^-1.