diff --git a/symengine/lib/symengine_wrapper.in.pyx b/symengine/lib/symengine_wrapper.in.pyx index 5548e9a4..e25be87e 100644 --- a/symengine/lib/symengine_wrapper.in.pyx +++ b/symengine/lib/symengine_wrapper.in.pyx @@ -4073,6 +4073,10 @@ cdef class DenseMatrixBase(MatrixBase): def diff(self, *args): return _diff(self, *args) + def evalf(self, unsigned long prec=53, real=None): + """Evaluate each entry using the scalar evalf precision and domain.""" + return self.applyfunc(lambda x: x.evalf(prec, real)) + #TODO: implement this in C++ def subs(self, *args): cdef _DictBasic D = get_dict(*args) diff --git a/symengine/tests/test_matrices.py b/symengine/tests/test_matrices.py index 1c98f0a1..fd58df69 100644 --- a/symengine/tests/test_matrices.py +++ b/symengine/tests/test_matrices.py @@ -1,4 +1,4 @@ -from symengine import symbols, init_printing +from symengine import symbols, init_printing, RealDouble from symengine.lib.symengine_wrapper import (DenseMatrix, Symbol, Integer, Rational, function_symbol, I, NonSquareMatrixError, ShapeError, zeros, ones, eye, ImmutableMatrix) @@ -672,6 +672,20 @@ def test_diff(): assert result == DenseMatrix(1, 2, [2*x, 1]) +def test_evalf(): + x = Symbol("x") + for matrix_type in (DenseMatrix, ImmutableMatrix): + matrix = matrix_type([[x**2 / 4, 3*x]]) + exact = matrix.diff(x).subs(x, 3) + + result = exact.evalf(real=True) + + assert type(result) is matrix_type + assert result.shape == (1, 2) + assert list(result) == [RealDouble(1.5), RealDouble(3)] + assert exact == matrix_type([[Rational(3, 2), Integer(3)]]) + + def test_immutablematrix(): A = ImmutableMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) assert A.shape == (3, 3)