diff --git a/symengine/lib/symengine_wrapper.pyx b/symengine/lib/symengine_wrapper.pyx index 4c5f0f82..b38eb080 100644 --- a/symengine/lib/symengine_wrapper.pyx +++ b/symengine/lib/symengine_wrapper.pyx @@ -3895,7 +3895,7 @@ cdef class DenseMatrixBase(MatrixBase): l.append(c2py(A.get(i, j))._sympy_()) s.append(l) import sympy - return sympy.Matrix(s) + return sympy.ImmutableMatrix(s) def _sage_(self): s = [] @@ -3906,7 +3906,7 @@ cdef class DenseMatrixBase(MatrixBase): l.append(c2py(A.get(i, j))._sage_()) s.append(l) import sage.all as sage - return sage.Matrix(s) + return sage.Matrix(s, immutable=True) def dump_real(self, double[::1] out): cdef size_t ri, ci, nr, nc @@ -4046,6 +4046,12 @@ cdef class ImmutableDenseMatrix(DenseMatrixBase): def __setitem__(self, key, value): raise TypeError("Cannot set values of {}".format(self.__class__)) + def _applyfunc(self, f): + res = DenseMatrix(self) + res._applyfunc(f) + return ImmutableDenseMatrix(res) + + ImmutableMatrix = ImmutableDenseMatrix diff --git a/symengine/tests/test_matrices.py b/symengine/tests/test_matrices.py index 55ebd046..de997529 100644 --- a/symengine/tests/test_matrices.py +++ b/symengine/tests/test_matrices.py @@ -3,13 +3,23 @@ Rational, function_symbol, I, NonSquareMatrixError, ShapeError, zeros, ones, eye, ImmutableMatrix) from symengine.test_utilities import raises +import unittest try: import numpy as np - HAVE_NUMPY = True + have_numpy = True except ImportError: - HAVE_NUMPY = False + have_numpy = False + +try: + import sympy + from sympy.core.cache import clear_cache + import atexit + atexit.register(clear_cache) + have_sympy = True +except ImportError: + have_sympy = False def test_init(): @@ -520,10 +530,8 @@ def test_reshape(): assert C != A -# @pytest.mark.skipif(not HAVE_NUMPY, reason='requires numpy') +@unittest.skipIf(not have_numpy, 'requires numpy') def test_dump_real(): - if not HAVE_NUMPY: # nosetests work-around - return ref = [1, 2, 3, 4] A = DenseMatrix(2, 2, ref) out = np.empty(4) @@ -531,10 +539,9 @@ def test_dump_real(): assert np.allclose(out, ref) -# @pytest.mark.skipif(not HAVE_NUMPY, reason='requires numpy') + +@unittest.skipIf(not have_numpy, 'requires numpy') def test_dump_complex(): - if not HAVE_NUMPY: # nosetests work-around - return ref = [1j, 2j, 3j, 4j] A = DenseMatrix(2, 2, ref) out = np.empty(4, dtype=np.complex128) @@ -741,3 +748,8 @@ def test_repr_latex(): latex_string = testmat._repr_latex_() assert isinstance(latex_string, str) init_printing(False) + +@unittest.skipIf(not have_sympy, "SymPy not installed") +def test_simplify(): + A = ImmutableMatrix([1]) + assert type(A.simplify()) == type(A)