Cantera  4.0.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 void mult(span<const double> b, span<double> prod) const;
71
72 //! Multiply A*B and write result to @c prod.
73 /*!
74 * Take this matrix to be of size NxM.
75 * @param[in] b DenseMatrix B of size MxP
76 * @param[out] prod DenseMatrix prod size NxP
77 */
78 virtual void mult(const DenseMatrix& b, DenseMatrix& prod) const;
79
80 //! Left-multiply the matrix by transpose(b), and write the result to prod.
81 /*!
82 * @param b left multiply by this vector. The length must be equal to n
83 * the number of rows in the matrix.
84 * @param prod Resulting vector. This is of length m, the number of columns
85 * in the matrix
86 */
87 virtual void leftMult(span<const double> b, span<double> prod) const;
88
89 //! Return a changeable value of the pivot vector
90 /*!
91 * @returns a reference to the pivot vector as a vector<int>
92 */
93 vector<int>& ipiv();
94
95 //! Return a changeable value of the pivot vector
96 /*!
97 * @returns a reference to the pivot vector as a vector<int>
98 */
99 const vector<int>& ipiv() const {
100 return m_ipiv;
101 }
102
103protected:
104 //! Vector of pivots. Length is equal to the max of m and n.
105 vector<int> m_ipiv;
106};
107
108
109//! Solve Ax = b. Array b is overwritten on exit with x.
110/*!
111 * The solve function uses the LAPACK routine dgetrf to invert the m xy n matrix.
112 *
113 * The factorization has the form
114 *
115 * A = P * L * U
116 *
117 * where P is a permutation matrix, L is lower triangular with unit diagonal
118 * elements (lower trapezoidal if m > n), and U is upper triangular (upper
119 * trapezoidal if m < n).
120 *
121 * The system is then solved using the LAPACK routine dgetrs
122 *
123 * @param A Dense matrix to be factored
124 * @param b RHS(s) to be solved.
125 * @param nrhs Number of right hand sides to solve
126 * @param ldb Leading dimension of b, if nrhs > 1
127 */
128void solve(DenseMatrix& A, span<double> b, size_t nrhs=1, size_t ldb=0);
129
130//! Solve Ax = b for multiple right-hand-side vectors.
131/*!
132 * @param A Dense matrix to be factored
133 * @param b Dense matrix of RHS's. Each column is a RHS
134 */
135void solve(DenseMatrix& A, DenseMatrix& b);
136
137//! Multiply @c A*b and return the result in @c prod. Uses BLAS routine DGEMV.
138/*!
139 * @f[
140 * prod_i = sum^N_{j = 1}{A_{ij} b_j}
141 * @f]
142 *
143 * @param[in] A Dense Matrix A with M rows and N columns
144 * @param[in] b vector b with length N
145 * @param[out] prod vector prod length = M
146 */
147void multiply(const DenseMatrix& A, span<const double> b, span<double> prod);
148
149//! Multiply @c A*b and add it to the result in @c prod. Uses BLAS routine DGEMV.
150/*!
151 * @f[
152 * prod_i += sum^N_{j = 1}{A_{ij} b_j}
153 * @f]
154 *
155 * @param[in] A Dense Matrix A with M rows and N columns
156 * @param[in] b vector b with length N
157 * @param[out] prod vector prod length = M
158 */
159void increment(const DenseMatrix& A, span<const double> b, span<double> prod);
160
161//! invert A. A is overwritten with A^-1.
162/*!
163 * @param A Invert the matrix A and store it back in place
164 * @param nn Size of A. This defaults to -1, which means that the number of
165 * rows is used as the default size of n
166 */
167void invert(DenseMatrix& A, size_t nn=npos);
168
169}
170
171#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
const vector< int > & ipiv() const
Return a changeable value of the pivot vector.
Definition DenseMatrix.h:99
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.
virtual void leftMult(span< const double > b, span< double > prod) const
Left-multiply the matrix by transpose(b), and write the result to prod.
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
const size_t npos
index returned by functions to indicate "no position"
Definition ct_defs.h:183
void increment(const DenseMatrix &A, span< const double > b, span< double > prod)
Multiply A*b and add it to the result in prod. Uses BLAS routine DGEMV.
void solve(DenseMatrix &A, span< double > b, size_t nrhs, size_t ldb)
Solve Ax = b. Array b is overwritten on exit with x.
void multiply(const DenseMatrix &A, span< const double > b, span< double > prod)
Multiply A*b and return the result in prod. Uses BLAS routine DGEMV.
void invert(DenseMatrix &A, size_t nn)
invert A. A is overwritten with A^-1.