Skip to content

Commit

Permalink
Python unit test and API doc of SquareMatrix.inverse() and SymmetricM…
Browse files Browse the repository at this point in the history
…atrix.inverse()
  • Loading branch information
mbaudin47 committed Jul 8, 2023
1 parent 228b856 commit be1cc0e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
21 changes: 20 additions & 1 deletion python/src/SquareMatrix_doc.i.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Get or set terms
[[ 1 ]
[ 1 ]]

Create an openturns matrix from a **square** numpy 2d-array (or matrix, or
Create a matrix from a **square** Numpy 2d-array (or matrix, or
2d-list)...

>>> import numpy as np
Expand Down Expand Up @@ -299,3 +299,22 @@ Examples
>>> M = ot.SquareMatrix([[1.0, 2.0], [3.0, 4.0]])
>>> M.computeTrace()
5.0"

// ---------------------------------------------------------------------

%feature("docstring") OT::SquareMatrix::inverse
"Compute the inverse of the matrix.

Returns
-------
inverseMatrix : :class:`~openturns.SquareMatrix`
The inverse of the matrix.

Examples
--------
>>> import openturns as ot
>>> M = ot.SquareMatrix([[1.0, 2.0, 3.0], [3.0, 2.0, 1.0], [2.0, 1.0, 3.0]])
>>> print(12.0 * M.inverse())
[[ -5 3 4 ]
[ 7 3 -8 ]
[ 1 -3 4 ]]"
21 changes: 20 additions & 1 deletion python/src/SymmetricMatrix_doc.i.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Get or set terms
[[ 1 ]
[ 2 ]]

Create an openturns matrix from a **symmetric** numpy 2d-array (or matrix, or
Create a matrix from a **symmetric** Numpy 2d-array (or matrix, or
2d-list)...

>>> import numpy as np
Expand Down Expand Up @@ -153,3 +153,22 @@ Examples

%feature("docstring") OT::SymmetricMatrix::checkSymmetry
"Check if the internal representation is really symmetric."

// ---------------------------------------------------------------------

%feature("docstring") OT::SymmetricMatrix::inverse
"Compute the inverse of the matrix.

Returns
-------
inverseMatrix : :class:`~openturns.SymmetricMatrix`
The inverse of the matrix.

Examples
--------
>>> import openturns as ot
>>> M = ot.SymmetricMatrix([[4.0, 2.0, 1.0], [2.0, 5.0, 3.0], [1.0, 3.0, 6.0]])
>>> print(67.0 * M.inverse())
[[ 21 -9 1 ]
[ -9 23 -10 ]
[ 1 -10 16 ]]"
12 changes: 12 additions & 0 deletions python/test/t_SquareMatrix_std.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#! /usr/bin/env python

import openturns as ot
from openturns.testing import assert_almost_equal

ot.TESTPREAMBLE()

Expand Down Expand Up @@ -126,3 +127,14 @@
print("squareMatrix0 is empty = ", squareMatrix0.isEmpty())
print("squareMatrix1 is empty = ", squareMatrix1.isEmpty())
print("squareMatrix5 is empty = ", squareMatrix5.isEmpty())

# Check inverse()
squareMatrix6 = ot.SquareMatrix(
[[1.0, 2.0, 3.0], [3.0, 2.0, 1.0], [2.0, 1.0, 3.0]]
)
squareMatrix7 = squareMatrix6.inverse()
inverseReference = ot.SquareMatrix(
[[-5.0, 3.0, 4.0], [7.0, 3.0, -8.0], [1.0, -3.0, 4.0]]
)
inverseReference /= 12.0
assert_almost_equal(squareMatrix7, inverseReference)
8 changes: 8 additions & 0 deletions python/test/t_SymmetricMatrix_std.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#! /usr/bin/env python

import openturns as ot
from openturns.testing import assert_almost_equal

ot.TESTPREAMBLE()

Expand Down Expand Up @@ -127,3 +128,10 @@
print("symmetricMatrix0 is empty = ", symmetricMatrix0.isEmpty())
print("symmetricMatrix1 is empty = ", symmetricMatrix1.isEmpty())
print("symmetricMatrix5 is empty = ", symmetricMatrix5.isEmpty())

# Check inverse()
symmetricMatrix6 = ot.SymmetricMatrix([[4.0, 2.0, 1.0], [2.0, 5.0, 3.0], [1.0, 3.0, 6.0]])
symmetricMatrix7 = symmetricMatrix6.inverse()
inverseReference = ot.SymmetricMatrix([[21.0, -9.0, 1.0], [-9.0, 23.0, -10.0], [1.0, -10.0, 16.0]])
inverseReference /= 67.0
assert_almost_equal(symmetricMatrix7, inverseReference)

0 comments on commit be1cc0e

Please sign in to comment.