Skip to content

Commit dcb654a

Browse files
committed
Update DenseMatrix docs
* Clarify that the _decomposition_type is cleared when the matrix is resized and/or zeroed. * Add warnings to the lu_solve() and cholesky_solve() APIs about not modifying the matrix contents via operator(i,j) calls in between calls to lu_solve(), as this will produce wrong results.
1 parent eac4e63 commit dcb654a

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

Diff for: include/numerics/dense_matrix.h

+29-2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ class DenseMatrix : public DenseMatrixBase<T>
9393
DenseMatrix & operator= (DenseMatrix &&) = default;
9494
virtual ~DenseMatrix() = default;
9595

96+
/**
97+
* Sets all elements of the matrix to 0 and resets any decomposition
98+
* flag which may have been previously set. This allows e.g. a new
99+
* LU decomposition to be computed while reusing the same storage.
100+
*/
96101
virtual void zero() override;
97102

98103
/**
@@ -234,8 +239,11 @@ class DenseMatrix : public DenseMatrixBase<T>
234239
void swap(DenseMatrix<T> & other_matrix);
235240

236241
/**
237-
* Resize the matrix. Will never free memory, but may
238-
* allocate more. Sets all elements to 0.
242+
* Resizes the matrix to the specified size and calls zero(). Will
243+
* never free memory, but may allocate more. Note: when the matrix
244+
* is zero()'d, any decomposition (LU, Cholesky, etc.) is also
245+
* cleared, forcing a new decomposition to be computed the next time
246+
* e.g. lu_solve() is called.
239247
*/
240248
void resize(const unsigned int new_m,
241249
const unsigned int new_n);
@@ -389,6 +397,15 @@ class DenseMatrix : public DenseMatrixBase<T>
389397
* Solve the system Ax=b given the input vector b. Partial pivoting
390398
* is performed by default in order to keep the algorithm stable to
391399
* the effects of round-off error.
400+
*
401+
* Important note: once you call lu_solve(), you must _not_ modify
402+
* the entries of the matrix via calls to operator(i,j) and call
403+
* lu_solve() again without first calling either zero() or resize(),
404+
* otherwise the code will skip computing the decomposition of the
405+
* matrix and go directly to the back substitution step. This is
406+
* done on purpose for efficiency, so that the same LU decomposition
407+
* can be used with multiple right-hand sides, but it does also make
408+
* it possible to "shoot yourself in the foot", so be careful!
392409
*/
393410
void lu_solve (const DenseVector<T> & b,
394411
DenseVector<T> & x);
@@ -401,6 +418,16 @@ class DenseMatrix : public DenseMatrixBase<T>
401418
* One nice property of Cholesky decompositions is that they do not require
402419
* pivoting for stability.
403420
*
421+
* Important note: once you call cholesky_solve(), you must _not_
422+
* modify the entries of the matrix via calls to operator(i,j) and
423+
* call cholesky_solve() again without first calling either zero()
424+
* or resize(), otherwise the code will skip computing the
425+
* decomposition of the matrix and go directly to the back
426+
* substitution step. This is done on purpose for efficiency, so
427+
* that the same decomposition can be used with multiple right-hand
428+
* sides, but it does also make it possible to "shoot yourself in
429+
* the foot", so be careful!
430+
*
404431
* \note This method may also be used when A is real-valued and x
405432
* and b are complex-valued.
406433
*/

0 commit comments

Comments
 (0)