def test_CCodePrinter(): x = Symbol("x") y = Symbol("y") myprinter = CCodePrinter() assert myprinter.doprint(1+x, "bork") == "bork = 1 + x;" assert myprinter.doprint(1*x) == "x" assert myprinter.doprint(MutableDenseMatrix(1, 2, [x, y]), "larry") == "larry[0] = x;\nlarry[1] = y;" raises(TypeError, lambda: myprinter.doprint(sin(x), Integer)) raises(RuntimeError, lambda: myprinter.doprint(MutableDenseMatrix(1, 2, [x, y])))
def test_ccode(): x = Symbol("x") y = Symbol("y") assert ccode(x) == "x" assert ccode(x**3) == "pow(x, 3)" assert ccode(x**(y**3)) == "pow(x, pow(y, 3))" assert ccode(x**-1.0) == "pow(x, -1.0)" assert ccode(Max(x, x*x)) == "max(x, pow(x, 2))" assert ccode(sin(x)) == "sin(x)" assert ccode(Integer(67)) == "67" assert ccode(Integer(-1)) == "-1"
def test_det(): A = DenseMatrix(2, 2, [1, 2, 3, 4]) assert A.det() == -2 a = Symbol("a") b = Symbol("b") c = Symbol("c") d = Symbol("d") A = DenseMatrix(2, 2, [a, b, c, d]) assert A.det() == a * d - b * c A = DenseMatrix(3, 2, [1, 2, 3, 4, 5, 6]) raises(NonSquareMatrixError, lambda: A.det())
def test_add_matrix(): A = DenseMatrix(2, 2, [1, 2, 3, 4]) B = DenseMatrix(2, 2, [1, 0, 0, 1]) assert A.add_matrix(B) == DenseMatrix(2, 2, [2, 2, 3, 5]) a = Symbol("a") b = Symbol("b") c = Symbol("c") d = Symbol("d") A = DenseMatrix(2, 2, [a + b, a - b, a, b]) B = DenseMatrix(2, 2, [a - b, a + b, -a, b]) assert A.add_matrix(B) == DenseMatrix(2, 2, [2 * a, 2 * a, 0, 2 * b]) assert A + B == DenseMatrix(2, 2, [2 * a, 2 * a, 0, 2 * b])
def test_is_real_matrix(): A = DenseMatrix(2, 2, [1, 2, 3, I]) assert not A.is_real_matrix B = DenseMatrix(1, 1, [Symbol('x')]) assert B.is_real_matrix is None C = DenseMatrix(3, 3, [0, 0, 0, 0, 0, 0, 0, 0, 0]) assert C.is_real_matrix
def test_set(): i7 = Integer(7) y = Symbol("y") g = function_symbol("g", y) c = 2*I + 3 A = DenseMatrix(2, 2, [Integer(5), Symbol("x"), function_symbol("f", Symbol("x")), 1 + I]) A.set(0, 0, i7) assert A.get(0, 0) == i7 A.set(0, 1, y) assert A.get(0, 1) == y A.set(1, 0, g) assert A.get(1, 0) == g A.set(1, 1, c) assert A.get(1, 1) == c
def test_is_symmetric(): A = DenseMatrix(2, 2, [1, 3, 2, I]) assert not A.is_symmetric B = DenseMatrix(1, 1, [Symbol('x')]) assert B.is_symmetric C = DenseMatrix(3, 3, [0, 0, 0, 0, 0, 0, 0, 2, 0]) assert not C.is_symmetric
def test_is_hermitian(): A = DenseMatrix(2, 2, [1, 3, 2, I]) assert not A.is_hermitian B = DenseMatrix(1, 1, [Symbol('x')]) assert B.is_hermitian is None C = DenseMatrix(3, 3, [0, I, 0, 0, 0, 0, 0, 2, 0]) assert not C.is_hermitian
def test_is_diagonal(): A = DenseMatrix(2, 2, [1, 0, 0, I]) assert A.is_diagonal B = DenseMatrix(1, 1, [Symbol('x')]) assert B.is_diagonal C = DenseMatrix(3, 3, [0, 0, 0, 0, 0, 0, 0, 2, 0]) assert not C.is_diagonal
def test_series_expansion(): x = Symbol('x') ex = series(sin(1 + x), x, n=10) assert ex.coeff(x, 7) == -cos(1) / 5040 x = Symbol('x') ex = series(1 / (1 - x), x, n=10) assert ex.coeff(x, 9) == 1 ex = series(sin(x) * cos(x), x, n=10) assert ex.coeff(x, 8) == 0 assert ex.coeff(x, 9) == Integer(2) / Integer(2835) ex = series(E**x, x, n=10) assert ex.coeff(x, 9) == Integer(1) / Integer(362880) ex1 = series(1 / sqrt(4 - x), x, n=50) ex2 = series((4 - x)**(Integer(-1) / Integer(2)), x, n=50) assert ex1.coeff(x, 49) == ex2.coeff(x, 49)
def test_ConditionSet(): x = Symbol("x") i1 = Interval(-oo, oo) f1 = FiniteSet(0, 1, 2, 4) cond1 = Ge(x**2, 9) assert ConditionSet(x, And(Eq(0, 1), i1.contains(x))) == EmptySet() assert ConditionSet(x, And(Gt(1, 0), i1.contains(x))) == i1 assert ConditionSet(x, And(cond1, f1.contains(x))) == FiniteSet(4)
def test_add_matrix(): A = DenseMatrix(2, 2, [1, 2, 3, 4]) B = DenseMatrix(2, 2, [1, 0, 0, 1]) assert A.add_matrix(B) == DenseMatrix(2, 2, [2, 2, 3, 5]) a = Symbol("a") b = Symbol("b") c = Symbol("c") d = Symbol("d") A = DenseMatrix(2, 2, [a + b, a - b, a, b]) B = DenseMatrix(2, 2, [a - b, a + b, -a, b]) assert A.add_matrix(B) == DenseMatrix(2, 2, [2 * a, 2 * a, 0, 2 * b]) assert A + B == DenseMatrix(2, 2, [2 * a, 2 * a, 0, 2 * b]) C = DenseMatrix(1, 2, [a, b]) raises(ShapeError, lambda: A + C)
def test_add_scalar(): A = DenseMatrix(2, 2, [1, 2, 3, 4]) a = Symbol("a") assert A.add_scalar(a) == DenseMatrix(2, 2, [1 + a, 2 + a, 3 + a, 4 + a]) i5 = Integer(5) assert A.add_scalar(i5) == DenseMatrix(2, 2, [6, 7, 8, 9]) assert A + 5 == DenseMatrix(2, 2, [6, 7, 8, 9]) assert 5 + A == DenseMatrix(2, 2, [6, 7, 8, 9])
def test_sub(): A = DenseMatrix(2, 2, [1, 2, 3, 4]) B = DenseMatrix(2, 2, [0, -1, -2, -3]) a = Symbol("a") assert A - 5 == DenseMatrix(2, 2, [-4, -3, -2, -1]) assert a - A == DenseMatrix(2, 2, [a - 1, a - 2, a - 3, a - 4]) assert A - B == DenseMatrix(2, 2, [1, 3, 5, 7]) C = DenseMatrix(2, 1, [1, 2]) raises(ShapeError, lambda: A - C)
def test_mul_scalar(): A = DenseMatrix(2, 2, [1, 2, 3, 4]) a = Symbol("a") assert A.mul_scalar(a) == DenseMatrix(2, 2, [a, 2 * a, 3 * a, 4 * a]) i5 = Integer(5) assert A.mul_scalar(i5) == DenseMatrix(2, 2, [5, 10, 15, 20]) assert A * 5 == DenseMatrix(2, 2, [5, 10, 15, 20]) assert 5 * A == DenseMatrix(2, 2, [5, 10, 15, 20])
def test_sub(): A = DenseMatrix(2, 2, [1, 2, 3, 4]) B = DenseMatrix(2, 2, [0, -1, -2, -3]) a = Symbol("a") assert A - B == DenseMatrix(2, 2, [1, 3, 5, 7]) C = DenseMatrix(2, 1, [1, 2]) raises(ShapeError, lambda: A - C) raises(TypeError, lambda: A - 5) raises(TypeError, lambda: 5 - A)
def test_add_scalar(): A = DenseMatrix(2, 2, [1, 2, 3, 4]) a = Symbol("a") assert A.add_scalar(a) == DenseMatrix(2, 2, [1 + a, 2 + a, 3 + a, 4 + a]) i5 = Integer(5) assert A.add_scalar(i5) == DenseMatrix(2, 2, [6, 7, 8, 9]) raises(TypeError, lambda: A + 5) raises(TypeError, lambda: 5 + A)
def test_FiniteSet(): x = Symbol("x") A = FiniteSet(1, 2, 3) B = FiniteSet(3, 4, 5) AorB = Union(A, B) AandB = A.intersection(B) assert AandB == FiniteSet(3) assert FiniteSet(EmptySet()) != EmptySet() assert FiniteSet(FiniteSet(1, 2, 3)) != FiniteSet(1, 2, 3)
def test_mul_matrix(): A = DenseMatrix(2, 2, [1, 2, 3, 4]) B = DenseMatrix(2, 2, [1, 0, 0, 1]) assert A.mul_matrix(B) == A a = Symbol("a") b = Symbol("b") c = Symbol("c") d = Symbol("d") A = DenseMatrix(2, 2, [a, b, c, d]) B = DenseMatrix(2, 2, [1, 0, 1, 0]) assert A.mul_matrix(B) == DenseMatrix(2, 2, [a + b, 0, c + d, 0]) assert A * B == DenseMatrix(2, 2, [a + b, 0, c + d, 0]) A = DenseMatrix(2, 3, [1, 2, 3, 2, 3, 4]) B = DenseMatrix(3, 2, [3, 4, 4, 5, 5, 6]) assert A.mul_matrix(B) == DenseMatrix(2, 2, [26, 32, 38, 47])
def test_immutablematrix(): A = ImmutableMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) assert A.shape == (3, 3) assert A[1, 2] == 6 assert A[2, 2] == 9 assert A[1, :] == ImmutableMatrix([[4, 5, 6]]) assert A[:2, :2] == ImmutableMatrix([[1, 2], [4, 5]]) with raises(TypeError): A[2, 2] = 5 X = DenseMatrix([[1, 2], [3, 4]]) assert X.as_immutable() == ImmutableMatrix([[1, 2], [3, 4]]) assert X.det() == -2 X = ImmutableMatrix(eye(3)) assert isinstance(X + A, ImmutableMatrix) assert isinstance(X * A, ImmutableMatrix) assert isinstance(X * 2, ImmutableMatrix) assert isinstance(2 * X, ImmutableMatrix) X = ImmutableMatrix([[1, 2], [3, 4]]) Y = ImmutableMatrix([[1], [0]]) assert type(X.LUsolve(Y)) == ImmutableMatrix x = Symbol("x") X = ImmutableMatrix([[1, 2], [3, 4]]) Y = ImmutableMatrix([[1, 2], [x, 4]]) assert Y.subs(x, 3) == X assert Y.xreplace(x, 3) == X X = ImmutableMatrix([[1, 2], [3, 4]]) Y = ImmutableMatrix([[5], [6]]) Z = X.row_join(Y) assert isinstance(Z, ImmutableMatrix) assert Z == ImmutableMatrix([[1, 2, 5], [3, 4, 6]]) X = ImmutableMatrix([[1, 2], [3, 4]]) Y = ImmutableMatrix([[5, 6]]) Z = X.col_join(Y) assert isinstance(Z, ImmutableMatrix) assert Z == ImmutableMatrix([[1, 2], [3, 4], [5, 6]]) # Operations of one immutable and one mutable matrix should give immutable result X = ImmutableMatrix([1]) Y = DenseMatrix([1]) assert type(X + Y) == ImmutableMatrix assert type(Y + X) == ImmutableMatrix assert type(X * Y) == ImmutableMatrix assert type(Y * X) == ImmutableMatrix
def test_mul_matrix(): A = DenseMatrix(2, 2, [1, 2, 3, 4]) B = DenseMatrix(2, 2, [1, 0, 0, 1]) assert A.mul_matrix(B) == A a = Symbol("a") b = Symbol("b") c = Symbol("c") d = Symbol("d") A = DenseMatrix(2, 2, [a, b, c, d]) B = DenseMatrix(2, 2, [1, 0, 1, 0]) assert A.mul_matrix(B) == DenseMatrix(2, 2, [a + b, 0, c + d, 0]) assert A * B == DenseMatrix(2, 2, [a + b, 0, c + d, 0]) C = DenseMatrix(2, 3, [1, 2, 3, 2, 3, 4]) D = DenseMatrix(3, 2, [3, 4, 4, 5, 5, 6]) assert C.mul_matrix(D) == DenseMatrix(2, 2, [26, 32, 38, 47]) raises(ShapeError, lambda: A * D)
def test_solve(): x = Symbol("x") reals = Interval(-oo, oo) assert solve(1, x, reals) == EmptySet() assert solve(0, x, reals) == reals assert solve(x + 3, x, reals) == FiniteSet(-3) assert solve(x + 3, x, Interval(0, oo)) == EmptySet() assert solve(x, x, reals) == FiniteSet(0) assert solve(x**2 + 1, x) == FiniteSet(-I, I) assert solve(x**2 - 2*x + 1, x) == FiniteSet(1) assert solve(Eq(x**3 + 3*x**2 + 3*x, -1), x, reals) == FiniteSet(-1) assert solve(x**3 - x, x) == FiniteSet(0, 1, -1)
def test_get(): A = DenseMatrix([[1, 2], [3, 4]]) assert A.get(0, 0) == 1 assert A.get(0, 1) == 2 assert A.get(1, 1) == 4 a = Symbol("a") b = Symbol("b") c = Symbol("c") d = Symbol("d") A = DenseMatrix(2, 2, [a, b, c, d]) assert A.get(0, 0) == a assert A.get(1, 0) == c assert A.get(1, 1) == d assert A.get(-1, 0) == c assert A.get(-1, -1) == d raises(IndexError, lambda: A.get(2, 0)) raises(IndexError, lambda: A.get(0, 2)) raises(IndexError, lambda: A.get(-3, 0))
from symengine.utilities import raises from symengine.lib.symengine_wrapper import (true, false, Eq, Ne, Ge, Gt, Le, Lt, Symbol, I, And, Or, Not, Nand, Nor, Xor, Xnor, Piecewise, Contains, Interval, FiniteSet, oo, log) x = Symbol("x") y = Symbol("y") z = Symbol("z") def test_relationals(): assert Eq(0) == true assert Eq(1) == false assert Eq(x, x) == true assert Eq(0, 0) == true assert Eq(1, 0) == false assert Ne(0, 0) == false assert Ne(1, 0) == true assert Lt(0, 1) == true assert Lt(1, 0) == false assert Le(0, 1) == true assert Le(1, 0) == false assert Le(0, 0) == true assert Gt(1, 0) == true assert Gt(0, 1) == false assert Ge(1, 0) == true assert Ge(0, 1) == false assert Ge(1, 1) == true assert Eq(I, 2) == false
def test_is_negative_definite(): A = DenseMatrix(2, 2, [-2, -1, -1, -2]) assert A.is_negative_definite C = DenseMatrix(3, 3, [Symbol('x'), -2, 0, 0, -4, 0, 0, 0, -4]) assert C.is_negative_definite is None
def test_is_strongly_diagonally_dominant(): A = DenseMatrix(2, 2, [2, 1, 1, 2]) assert A.is_strongly_diagonally_dominant C = DenseMatrix(3, 3, [Symbol('x'), 2, 0, 0, 4, 0, 0, 0, 4]) assert C.is_strongly_diagonally_dominant is None
def test_ImageSet(): x = Symbol("x") i1 = Interval(0, 1) assert ImageSet(x, x**2, EmptySet()) == EmptySet() assert ImageSet(x, 1, i1) == FiniteSet(1) assert ImageSet(x, x, i1) == i1
def test_is_positive_definite(): A = DenseMatrix(2, 2, [2, 1, 1, 2]) assert A.is_positive_definite C = DenseMatrix(3, 3, [Symbol('x'), 2, 0, 0, 4, 0, 0, 0, 4]) assert C.is_positive_definite is None
def test_atoms(): a = Symbol("a") b = Symbol("b") X = DenseMatrix([[a, 2], [b, 4]]) assert X.atoms(Symbol) == set([a, b])
def test_UniversalSet(): U = UniversalSet() x = Symbol("x") assert U.union(Interval(2, 4)) == U assert U.intersection(Interval(2, 4)) == Interval(2, 4) assert U.contains(0) == true