diff --git a/python/src/SquareMatrix_doc.i.in b/python/src/SquareMatrix_doc.i.in index d2f2908d2c2..972dd638114 100644 --- a/python/src/SquareMatrix_doc.i.in +++ b/python/src/SquareMatrix_doc.i.in @@ -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 @@ -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 ]]" diff --git a/python/src/SymmetricMatrix_doc.i.in b/python/src/SymmetricMatrix_doc.i.in index 7840b440128..4a58d508e20 100644 --- a/python/src/SymmetricMatrix_doc.i.in +++ b/python/src/SymmetricMatrix_doc.i.in @@ -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 @@ -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 ]]" diff --git a/python/test/t_SquareMatrix_std.py b/python/test/t_SquareMatrix_std.py index ee599decdf1..8a9651071d9 100755 --- a/python/test/t_SquareMatrix_std.py +++ b/python/test/t_SquareMatrix_std.py @@ -1,6 +1,7 @@ #! /usr/bin/env python import openturns as ot +from openturns.testing import assert_almost_equal ot.TESTPREAMBLE() @@ -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) diff --git a/python/test/t_SymmetricMatrix_std.py b/python/test/t_SymmetricMatrix_std.py index 2956ce56bdc..638a3b77067 100755 --- a/python/test/t_SymmetricMatrix_std.py +++ b/python/test/t_SymmetricMatrix_std.py @@ -1,6 +1,7 @@ #! /usr/bin/env python import openturns as ot +from openturns.testing import assert_almost_equal ot.TESTPREAMBLE() @@ -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)