Exemplo n.º 1
0
def test_Matrix_array():
    class matarray:
        def __array__(self):
            from numpy import array
            return array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

    matarr = matarray()
    assert Matrix(matarr) == Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Exemplo n.º 2
0
def test_Matrix_sum():
    M = Matrix([[1, 2, 3], [x, y, x], [2 * y, -50, z * x]])
    m = numpy.array([[2, 3, 4], [x, 5, 6], [x, y, z**2]])
    assert M + m == Matrix([[3, 5, 7], [2 * x, y + 5, x + 6],
                            [2 * y + x, y - 50, z * x + z**2]])
    assert m + M == Matrix([[3, 5, 7], [2 * x, y + 5, x + 6],
                            [2 * y + x, y - 50, z * x + z**2]])
    assert M + m == M.add(m)
Exemplo n.º 3
0
def test_funcmatrix():
    X = FunctionMatrix(3, 3, Lambda((i, j), i - j))
    assert X[1, 1] == 0
    assert X[1, 2] == -1
    assert X.shape == (3, 3)
    assert X.rows == X.cols == 3
    assert Matrix(X) == Matrix(3, 3, lambda i, j: i - j)
    assert isinstance(X * X + X, MatrixExpr)
Exemplo n.º 4
0
def test_matrix_to_vector():
    m = Matrix([[1], [2], [3]])
    assert matrix_to_vector(m, C) == C.i + 2 * C.j + 3 * C.k
    m = Matrix([[0], [0], [0]])
    assert matrix_to_vector(m, N) == matrix_to_vector(m, C) == \
           Vector.zero
    m = Matrix([[q1], [q2], [q3]])
    assert matrix_to_vector(m, N) == q1 * N.i + q2 * N.j + q3 * N.k
Exemplo n.º 5
0
def test_sympyissue_11678():
    p = Matrix([[1./2, 1./4, 1./4],
                [1./2, 0, 1./2],
                [1./4, 0, 3./4]])
    e = (p**x).applyfunc(lambda i: limit(i, x, oo))
    assert e == Matrix([[Float('0.36363636363636359', prec=15),
                         Float('0.090909090909090898', prec=15),
                         Float('0.54545454545454541', prec=15)]]*3)
Exemplo n.º 6
0
def test_orienters():
    A = CoordSysCartesian('A')
    axis_orienter = AxisOrienter(a, A.k)
    B = body_orienter = BodyOrienter(a, b, c, '123')
    assert (B.angle1, B.angle2, B.angle3) == (a, b, c)
    assert B.rot_order == '123'
    B = BodyOrienter(a, b, c, Symbol('123'))
    assert B.rot_order == '123'
    space_orienter = SpaceOrienter(a, b, c, '123')
    Q = q_orienter = QuaternionOrienter(q1, q2, q3, q4)
    assert (Q.q0, Q.q1, Q.q2, Q.q3) == (q1, q2, q3, q4)
    assert axis_orienter.rotation_matrix(A) == Matrix([[+cos(a),
                                                        sin(a), 0],
                                                       [-sin(a),
                                                        cos(a), 0], [0, 0, 1]])
    assert body_orienter.rotation_matrix() == Matrix(
        [[
            cos(b) * cos(c),
            sin(a) * sin(b) * cos(c) + sin(c) * cos(a),
            sin(a) * sin(c) - sin(b) * cos(a) * cos(c)
        ],
         [
             -sin(c) * cos(b), -sin(a) * sin(b) * sin(c) + cos(a) * cos(c),
             sin(a) * cos(c) + sin(b) * sin(c) * cos(a)
         ], [sin(b), -sin(a) * cos(b),
             cos(a) * cos(b)]])
    assert space_orienter.rotation_matrix() == Matrix(
        [[cos(b) * cos(c), sin(c) * cos(b), -sin(b)],
         [
             sin(a) * sin(b) * cos(c) - sin(c) * cos(a),
             sin(a) * sin(b) * sin(c) + cos(a) * cos(c),
             sin(a) * cos(b)
         ],
         [
             sin(a) * sin(c) + sin(b) * cos(a) * cos(c),
             -sin(a) * cos(c) + sin(b) * sin(c) * cos(a),
             cos(a) * cos(b)
         ]])
    assert q_orienter.rotation_matrix() == Matrix(
        [[
            q1**2 + q2**2 - q3**2 - q4**2, 2 * q1 * q4 + 2 * q2 * q3,
            -2 * q1 * q3 + 2 * q2 * q4
        ],
         [
             -2 * q1 * q4 + 2 * q2 * q3, q1**2 - q2**2 + q3**2 - q4**2,
             2 * q1 * q2 + 2 * q3 * q4
         ],
         [
             2 * q1 * q3 + 2 * q2 * q4, -2 * q1 * q2 + 2 * q3 * q4,
             q1**2 - q2**2 - q3**2 + q4**2
         ]])

    B = CoordSysCartesian('T')
    pytest.raises(ValueError, lambda: A.orient_new_axis('A', a, B.i))

    pytest.raises(TypeError, lambda: BodyOrienter(a, b, c, '12'))
    pytest.raises(TypeError, lambda: BodyOrienter(a, b, c, '111'))
Exemplo n.º 7
0
def test_matrix():
    A = Matrix([[x, y], [y * x, z**2]])
    assert lambdarepr(A) == "MutableDenseMatrix([[x, y], [x*y, z**2]])"

    # Test printing a Matrix that has an element that is printed differently
    # with the LambdaPrinter than in the StrPrinter.
    p = Piecewise((x, True), evaluate=False)
    A = Matrix([p])
    assert lambdarepr(A) == "MutableDenseMatrix([[((x) if (True) else None)]])"
Exemplo n.º 8
0
def test_necklaces():
    def count(n, k, f):
        return len(list(necklaces(n, k, f)))

    m = []
    for i in range(1, 8):
        m.append((i, count(i, 2, 0), count(i, 2, 1), count(i, 3, 1)))
    assert Matrix(m) == Matrix([[1, 2, 2, 3], [2, 3, 3, 6], [3, 4, 4, 10],
                                [4, 6, 6, 21], [5, 8, 8, 39], [6, 14, 13, 92],
                                [7, 20, 18, 198]])
Exemplo n.º 9
0
def test_numpy_matmul():
    xmat = Matrix([[x, y], [z, 1+z]])
    ymat = Matrix([[x**2], [Abs(x)]])
    mat_func = lambdify((x, y, z), xmat*ymat, modules="numpy")
    numpy.testing.assert_array_equal(mat_func(0.5, 3, 4), numpy.array([[1.625], [3.5]]))
    numpy.testing.assert_array_equal(mat_func(-0.5, 3, 4), numpy.array([[1.375], [3.5]]))
    # Multiple matrices chained together in multiplication
    f = lambdify((x, y, z), xmat*xmat*xmat, modules="numpy")
    numpy.testing.assert_array_equal(f(0.5, 3, 4), numpy.array([[72.125, 119.25],
                                                                [159, 251]]))
Exemplo n.º 10
0
def test_constant_system():
    A = Matrix([[-(x + 3) / (x - 1), (x + 1) / (x - 1), 1],
                [-x - 3, x + 1, x - 1], [2 * (x + 3) / (x - 1), 0, 0]])
    u = Matrix([(x + 1) / (x - 1), x + 1, 0])
    DE = DifferentialExtension(extension={'D': [Poly(1, x)]})
    assert constant_system(A, u, DE) == \
        (Matrix([[1, 0, 0],
                 [0, 1, 0],
                 [0, 0, 0],
                 [0, 0, 1]]), Matrix([0, 1, 0, 0]))
Exemplo n.º 11
0
def test_transpose():
    assert transpose(A * B) == Transpose(B) * Transpose(A)
    assert transpose(2 * A * B) == 2 * Transpose(B) * Transpose(A)
    assert transpose(2 * I * C) == 2 * I * Transpose(C)

    M = Matrix(2, 2, [1, 2 + I, 3, 4]).as_immutable()
    MT = Matrix(2, 2, [1, 3, 2 + I, 4])
    assert transpose(M) == MT
    assert transpose(2 * M) == 2 * MT
    assert transpose(MatMul(2, M)) == MatMul(2, MT).doit()
Exemplo n.º 12
0
def test_adjoint():
    assert adjoint(A * B) == Adjoint(B) * Adjoint(A)
    assert adjoint(2 * A * B) == 2 * Adjoint(B) * Adjoint(A)
    assert adjoint(2 * I * C) == -2 * I * Adjoint(C)

    M = Matrix(2, 2, [1, 2 + I, 3, 4]).as_immutable()
    MA = Matrix(2, 2, [1, 3, 2 - I, 4])
    assert adjoint(M) == MA
    assert adjoint(2 * M) == 2 * MA
    assert adjoint(MatMul(2, M)) == MatMul(2, MA).doit()
Exemplo n.º 13
0
def test_matrix2numpy_conversion():
    a = Matrix([[1, 2, sin(x)], [x**2, x, Rational(1, 2)]])
    b = numpy.array([[1, 2, sin(x)], [x**2, x, Rational(1, 2)]])
    assert (matrix2numpy(a) == b).all()
    assert matrix2numpy(a).dtype == numpy.dtype('object')

    c = matrix2numpy(Matrix([[1, 2], [10, 20]]), dtype='int8')
    d = matrix2numpy(Matrix([[1, 2], [10, 20]]), dtype='float64')
    assert c.dtype == numpy.dtype('int8')
    assert d.dtype == numpy.dtype('float64')
Exemplo n.º 14
0
def test_atan2_expansion():
    assert cancel(atan2(x**2, x + 1).diff(x) - atan(x**2/(x + 1)).diff(x)) == 0
    assert cancel(atan(y/x).series(y, 0, 5) - atan2(y, x).series(y, 0, 5)
                  + atan2(0, x) - atan(0)) == O(y**5)
    assert cancel(atan(y/x).series(x, 1, 4) - atan2(y, x).series(x, 1, 4)
                  + atan2(y, 1) - atan(y)) == O((x - 1)**4, (x, 1))
    assert cancel(atan((y + x)/x).series(x, 1, 3) - atan2(y + x, x).series(x, 1, 3)
                  + atan2(1 + y, 1) - atan(1 + y)) == O((x - 1)**3, (x, 1))
    assert Matrix([atan2(y, x)]).jacobian([y, x]) == \
        Matrix([[x/(y**2 + x**2), -y/(y**2 + x**2)]])
Exemplo n.º 15
0
def _get_valued_base_test_variables():
    minkowski = Matrix((
        (1, 0, 0, 0),
        (0, -1, 0, 0),
        (0, 0, -1, 0),
        (0, 0, 0, -1),
    ))
    Lorentz = TensorIndexType('Lorentz', dim=4)
    Lorentz.data = minkowski

    i0, i1, i2, i3, i4 = tensor_indices('i0:5', Lorentz)

    E, px, py, pz = symbols('E px py pz')
    A = tensorhead('A', [Lorentz], [[1]])
    A.data = [E, px, py, pz]
    B = tensorhead('B', [Lorentz], [[1]], 'Gcomm')
    B.data = range(4)
    AB = tensorhead('AB', [Lorentz] * 2, [[1]]*2)
    AB.data = minkowski

    ba_matrix = Matrix((
        (1, 2, 3, 4),
        (5, 6, 7, 8),
        (9, 0, -1, -2),
        (-3, -4, -5, -6),
    ))

    BA = tensorhead('BA', [Lorentz] * 2, [[1]]*2)
    BA.data = ba_matrix

    # Let's test the diagonal metric, with inverted Minkowski metric:
    LorentzD = TensorIndexType('LorentzD')
    LorentzD.data = [-1, 1, 1, 1]
    mu0, mu1, mu2 = tensor_indices('mu0:3', LorentzD)
    C = tensorhead('C', [LorentzD], [[1]])
    C.data = [E, px, py, pz]

    # ### non-diagonal metric ###
    ndm_matrix = (
        (1, 1, 0,),
        (1, 0, 1),
        (0, 1, 0,),
    )
    ndm = TensorIndexType('ndm')
    ndm.data = ndm_matrix
    n0, n1, n2 = tensor_indices('n0:3', ndm)
    NA = tensorhead('NA', [ndm], [[1]])
    NA.data = range(10, 13)
    NB = tensorhead('NB', [ndm]*2, [[1]]*2)
    NB.data = [[i+j for j in range(10, 13)] for i in range(10, 13)]
    NC = tensorhead('NC', [ndm]*3, [[1]]*3)
    NC.data = [[[i+j+k for k in range(4, 7)] for j in range(1, 4)] for i in range(2, 5)]

    return (A, B, AB, BA, C, Lorentz, E, px, py, pz, LorentzD, mu0, mu1, mu2, ndm, n0, n1,
            n2, NA, NB, NC, minkowski, ba_matrix, ndm_matrix, i0, i1, i2, i3, i4)
Exemplo n.º 16
0
def test_simplify_expr():
    A = Symbol('A')
    f = Function('f')

    assert all(simplify(tmp) == tmp for tmp in [I, E, oo, x, -x, -oo, -E, -I])

    e = 1 / x + 1 / y
    assert e != (x + y) / (x * y)
    assert simplify(e) == (x + y) / (x * y)

    e = A**2 * s**4 / (4 * pi * k * m**3)
    assert simplify(e) == e

    e = (4 + 4 * x - 2 * (2 + 2 * x)) / (2 + 2 * x)
    assert simplify(e) == 0

    e = (-4 * x * y**2 - 2 * y**3 - 2 * x**2 * y) / (x + y)**2
    assert simplify(e) == -2 * y

    e = -x - y - (x + y)**(-1) * y**2 + (x + y)**(-1) * x**2
    assert simplify(e) == -2 * y

    e = (x + x * y) / x
    assert simplify(e) == 1 + y

    e = (f(x) + y * f(x)) / f(x)
    assert simplify(e) == 1 + y

    e = (2 * (1 / n - cos(n * pi) / n)) / pi
    assert simplify(e) == (-cos(pi * n) + 1) / (pi * n) * 2

    e = integrate(1 / (x**3 + 1), x).diff(x)
    assert simplify(e) == 1 / (x**3 + 1)

    e = integrate(x / (x**2 + 3 * x + 1), x).diff(x)
    assert simplify(e) == x / (x**2 + 3 * x + 1)

    f = Symbol('f')
    A = Matrix([[2 * k - m * w**2, -k], [-k, k - m * w**2]]).inv()
    assert simplify((A*Matrix([0, f]))[1]) == \
        -f*(2*k - m*w**2)/(k**2 - (k - m*w**2)*(2*k - m*w**2))

    f = -x + y / (z + t) + z * x / (z + t) + z * a / (z + t) + t * x / (z + t)
    assert simplify(f) == (y + a * z) / (z + t)

    A, B = symbols('A,B', commutative=False)

    assert simplify(A * B - B * A) == A * B - B * A
    assert simplify(A / (1 + y / x)) == x * A / (x + y)
    assert simplify(A * (1 / x + 1 / y)) == A / x + A / y  # (x + y)*A/(x*y)

    assert simplify(log(2) + log(3)) == log(6)
    assert simplify(log(2 * x) - log(2)) == log(x)

    assert simplify(hyper([], [], x)) == exp(x)
Exemplo n.º 17
0
def test_solve_linear_system():
    assert solve_linear_system(Matrix([[0, 0]]), x) == {}

    M = Matrix([[0, 0, n * (n + 1), (n + 1)**2, 0],
                [n + 1, n + 1, -2 * n - 1, -(n + 1), 0], [-1, 0, 1, 0, 0]])

    assert solve_linear_system(M, x, y, z, t) == {
        x: -t - t / n,
        z: -t - t / n,
        y: 0
    }
Exemplo n.º 18
0
def test_matrix():
    A = Matrix([[x, x*y], [sin(z) + 4, x**z]])
    sol = Matrix([[1, 2], [sin(3) + 4, 1]])
    f = lambdify((x, y, z), A, modules="diofant")
    assert f(1, 2, 3) == sol
    f = lambdify((x, y, z), (A, [A]), modules="diofant")
    assert f(1, 2, 3) == (sol, [sol])
    J = Matrix((x, x + y)).jacobian((x, y))
    v = Matrix((x, y))
    sol = Matrix([[1, 0], [1, 1]])
    assert lambdify(v, J, modules='diofant')(1, 2) == sol
    assert lambdify(v.T, J, modules='diofant')(1, 2) == sol
Exemplo n.º 19
0
def test_Matrix_str():
    M = Matrix([[x**+1, 1], [y, x + y]])
    assert str(M) == "Matrix([\n[x,     1],\n[y, x + y]])"
    assert sstr(M) == "Matrix([\n[x,     1],\n[y, x + y]])"
    M = Matrix([[1]])
    assert str(M) == sstr(M) == "Matrix([[1]])"
    M = Matrix([[1, 2]])
    assert str(M) == sstr(M) == "Matrix([[1, 2]])"
    M = Matrix()
    assert str(M) == sstr(M) == "Matrix(0, 0, [])"
    M = Matrix(0, 1, lambda i, j: 0)
    assert str(M) == sstr(M) == "Matrix(0, 1, [])"
Exemplo n.º 20
0
def test_from_ndarray():
    # See sympy/sympy#7465
    assert Matrix(numpy.array([1, 2, 3])) == Matrix([1, 2, 3])
    assert Matrix(numpy.array([[1, 2, 3]])) == Matrix([[1, 2, 3]])
    assert Matrix(numpy.array([[1, 2, 3], [4, 5, 6]])) == \
        Matrix([[1, 2, 3], [4, 5, 6]])
    assert Matrix(numpy.array([x, y, z])) == Matrix([x, y, z])
    pytest.raises(NotImplementedError, lambda: Matrix(numpy.array([[
        [1, 2], [3, 4]], [[5, 6], [7, 8]]])))
Exemplo n.º 21
0
def test_Matrix2():
    a = numpy.array([[2, 4], [5, 1]])
    assert Matrix(a) == Matrix([[2, 4], [5, 1]])
    assert Matrix(a) != Matrix([[2, 4], [5, 2]])
    a = numpy.array([[sin(2), 4], [5, 1]])
    assert Matrix(a) == Matrix([[sin(2), 4], [5, 1]])
    assert Matrix(a) != Matrix([[sin(0), 4], [5, 1]])
Exemplo n.º 22
0
def test_diff_integrate():
    M = Matrix([x, 1]).as_immutable()
    assert M.integrate(x) == Matrix([x**2 / 2, x])
    assert M.diff(x) == Matrix([1, 0])
    assert M.limit(x, 1) == Matrix([1, 1])

    assert zeros(2).as_immutable().integrate(x) == zeros(2)
Exemplo n.º 23
0
def test_Matrix4():
    a = matrix([[2, 4], [5, 1]])
    assert Matrix(a) == Matrix([[2, 4], [5, 1]])
    assert Matrix(a) != Matrix([[2, 4], [5, 2]])
    a = matrix([[sin(2), 4], [5, 1]])
    assert Matrix(a) == Matrix([[sin(2), 4], [5, 1]])
    assert Matrix(a) != Matrix([[sin(0), 4], [5, 1]])
Exemplo n.º 24
0
def test_rotation_matrix():
    N = CoordSysCartesian('N')
    A = N.orient_new_axis('A', q1, N.k)
    B = A.orient_new_axis('B', q2, A.i)
    C = B.orient_new_axis('C', q3, B.j)
    D = N.orient_new_axis('D', q4, N.j)
    E = N.orient_new_space('E', q1, q2, q3, '123')
    F = N.orient_new_quaternion('F', q1, q2, q3, q4)
    G = N.orient_new_body('G', q1, q2, q3, '123')
    assert N.rotation_matrix(C) == Matrix([
        [- sin(q1) * sin(q2) * sin(q3) + cos(q1) * cos(q3), - sin(q1) *
         cos(q2), sin(q1) * sin(q2) * cos(q3) + sin(q3) * cos(q1)],
        [sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1),
         cos(q1) * cos(q2), sin(q1) * sin(q3) - sin(q2) * cos(q1) *
         cos(q3)], [- sin(q3) * cos(q2), sin(q2), cos(q2) * cos(q3)]])
    test_mat = D.rotation_matrix(C) - Matrix(
        [[cos(q1) * cos(q3) * cos(q4) - sin(q3) * (- sin(q4) * cos(q2) +
                                                   sin(q1) * sin(q2) * cos(q4)), - sin(q2) * sin(q4) - sin(q1) *
          cos(q2) * cos(q4), sin(q3) * cos(q1) * cos(q4) + cos(q3) *
          (- sin(q4) * cos(q2) + sin(q1) * sin(q2) * cos(q4))],
         [sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), cos(q1) *
          cos(q2), sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3)],
         [sin(q4) * cos(q1) * cos(q3) - sin(q3) * (cos(q2) * cos(q4) +
                                                   sin(q1) * sin(q2) *
                                                   sin(q4)), sin(q2) *
          cos(q4) - sin(q1) * sin(q4) * cos(q2), sin(q3) *
          sin(q4) * cos(q1) + cos(q3) * (cos(q2) * cos(q4) +
                                         sin(q1) * sin(q2) * sin(q4))]])
    assert test_mat.expand() == zeros(3, 3)
    assert E.rotation_matrix(N) == Matrix(
        [[cos(q2)*cos(q3), sin(q3)*cos(q2), -sin(q2)],
         [sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1),
          sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3), sin(q1)*cos(q2)],
         [sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3), -
          sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1), cos(q1)*cos(q2)]])
    assert F.rotation_matrix(N) == Matrix([[
        q1**2 + q2**2 - q3**2 - q4**2,
        2*q1*q4 + 2*q2*q3, -2*q1*q3 + 2*q2*q4], [ -2*q1*q4 + 2*q2*q3,
                                                  q1**2 - q2**2 + q3**2 - q4**2, 2*q1*q2 + 2*q3*q4],
        [2*q1*q3 + 2*q2*q4,
         -2*q1*q2 + 2*q3*q4,
         q1**2 - q2**2 - q3**2 + q4**2]])
    assert G.rotation_matrix(N) == Matrix([[
        cos(q2)*cos(q3),  sin(q1)*sin(q2)*cos(q3) + sin(q3)*cos(q1),
        sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3)], [
            -sin(q3)*cos(q2), -sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3),
            sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1)], [
                sin(q2), -sin(q1)*cos(q2), cos(q1)*cos(q2)]])

    pytest.raises(TypeError, lambda: G.rotation_matrix(a))
Exemplo n.º 25
0
def test_prde_linear_constraints():
    DE = DifferentialExtension(extension={'D': [Poly(1, x)]})
    G = [(Poly(2*x**3 + 3*x + 1, x), Poly(x**2 - 1, x)), (Poly(1, x), Poly(x - 1, x)),
        (Poly(1, x), Poly(x + 1, x))]
    assert prde_linear_constraints(Poly(1, x), Poly(0, x), G, DE) == \
        ((Poly(2*x, x), Poly(0, x), Poly(0, x)), Matrix([[1, 1, -1], [5, 1, 1]]))
    G = [(Poly(t, t), Poly(1, t)), (Poly(t**2, t), Poly(1, t)), (Poly(t**3, t), Poly(1, t))]
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]})
    assert prde_linear_constraints(Poly(t + 1, t), Poly(t**2, t), G, DE) == \
        ((Poly(t, t), Poly(t**2, t), Poly(t**3, t)), Matrix())
    G = [(Poly(2*x, t), Poly(t, t)), (Poly(-x, t), Poly(t, t))]
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(1/x, t)]})
    prde_linear_constraints(Poly(1, t), Poly(0, t), G, DE) == \
        ((Poly(0, t), Poly(0, t)), Matrix([[2*x, -x]]))
Exemplo n.º 26
0
def test_converting_functions():
    arr_list = [1, 2, 3, 4]
    arr_matrix = Matrix(((1, 2), (3, 4)))

    # list
    arr_ndim_array = MutableDenseNDimArray(arr_list, (2, 2))
    assert isinstance(arr_ndim_array, MutableDenseNDimArray)
    assert arr_matrix.tolist() == arr_ndim_array.tolist()

    # Matrix
    arr_ndim_array = MutableDenseNDimArray(arr_matrix)
    assert isinstance(arr_ndim_array, MutableDenseNDimArray)
    assert arr_matrix.tolist() == arr_ndim_array.tolist()
    assert arr_matrix.shape == arr_ndim_array.shape
Exemplo n.º 27
0
def test_Matrices():
    assert octave_code(Matrix(1, 1, [10])) == '10'
    A = Matrix([[1, sin(x / 2), abs(x)], [0, 1, pi], [0, exp(1), ceiling(x)]])
    expected = ('[1 sin(x/2)  abs(x);\n'
                '0        1      pi;\n'
                '0   exp(1) ceil(x)]')
    assert octave_code(A) == expected
    # row and columns
    assert octave_code(A[:, 0]) == '[1; 0; 0]'
    assert octave_code(A[0, :]) == '[1 sin(x/2) abs(x)]'
    # empty matrices
    assert octave_code(Matrix(0, 0, [])) == '[]'
    assert octave_code(Matrix(0, 3, [])) == 'zeros(0, 3)'
    # annoying to read but correct
    assert octave_code(Matrix([[x, x - y, -y]])) == '[x x - y -y]'
Exemplo n.º 28
0
def test_BlockMatrix():
    A = MatrixSymbol('A', n, m)
    B = MatrixSymbol('B', n, k)
    C = MatrixSymbol('C', l, m)
    D = MatrixSymbol('D', l, k)
    M = MatrixSymbol('M', m + k, p)
    N = MatrixSymbol('N', l + n, k + m)
    X = BlockMatrix(Matrix([[A, B], [C, D]]))

    assert X.__class__(*X.args) == X

    # block_collapse does nothing on normal inputs
    E = MatrixSymbol('E', n, m)
    assert block_collapse(A + 2 * E) == A + 2 * E
    F = MatrixSymbol('F', m, m)
    assert block_collapse(E.T * A * F) == E.T * A * F

    assert X.shape == (l + n, k + m)
    assert X.blockshape == (2, 2)
    assert transpose(X) == BlockMatrix(Matrix([[A.T, C.T], [B.T, D.T]]))
    assert transpose(X).shape == X.shape[::-1]

    # Test that BlockMatrices and MatrixSymbols can still mix
    assert (X * M).is_MatMul
    assert X._blockmul(M).is_MatMul
    assert (X * M).shape == (n + l, p)
    assert (X + N).is_MatAdd
    assert X._blockadd(N).is_MatAdd
    assert (X + N).shape == X.shape

    E = MatrixSymbol('E', m, 1)
    F = MatrixSymbol('F', k, 1)

    Y = BlockMatrix(Matrix([[E], [F]]))

    assert (X * Y).shape == (l + n, 1)
    assert block_collapse(X * Y).blocks[0, 0] == A * E + B * F
    assert block_collapse(X * Y).blocks[1, 0] == C * E + D * F

    # block_collapse passes down into container objects, transposes, and inverse
    assert block_collapse(transpose(X * Y)) == transpose(block_collapse(X * Y))
    assert block_collapse(Tuple(X * Y, 2 * X)) == (block_collapse(X * Y),
                                                   block_collapse(2 * X))

    # Make sure that MatrixSymbols will enter 1x1 BlockMatrix if it simplifies
    Ab = BlockMatrix([[A]])
    Z = MatrixSymbol('Z', *A.shape)
    assert block_collapse(Ab + Z) == A + Z
Exemplo n.º 29
0
def test_Matrix_mul():
    M = Matrix([[1, 2, 3], [x, y, x]])
    m = numpy.array([[2, 4], [x, 6], [x, z**2]])
    assert M * m == Matrix([
        [2 + 5 * x, 16 + 3 * z**2],
        [2 * x + x * y + x**2, 4 * x + 6 * y + x * z**2],
    ])

    assert m * M == Matrix([
        [2 + 4 * x, 4 + 4 * y, 6 + 4 * x],
        [7 * x, 2 * x + 6 * y, 9 * x],
        [x + x * z**2, 2 * x + y * z**2, 3 * x + x * z**2],
    ])
    a = numpy.array([2])
    assert a[0] * M == 2 * M
    assert M * a[0] == 2 * M
Exemplo n.º 30
0
def test_m_matrix_output_autoname_2():
    e1 = (x + y)
    e2 = Matrix([[2 * x, 2 * y, 2 * z]])
    e3 = Matrix([[x], [y], [z]])
    e4 = Matrix([[x, y], [z, 16]])
    name_expr = ('test', (e1, e2, e3, e4))
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    source = result[1]
    expected = ('function [out1, out2, out3, out4] = test(x, y, z)\n'
                '  out1 = x + y;\n'
                '  out2 = [2*x 2*y 2*z];\n'
                '  out3 = [x; y; z];\n'
                '  out4 = [x  y;\n'
                '  z 16];\n'
                'end\n')
    assert source == expected
Exemplo n.º 31
0
def test_sympyissue_2827_trigsimp_methods():
    def measure1(expr):
        return len(str(expr))

    def measure2(expr):
        return -count_ops(expr)  # Return the most complicated result

    expr = (x + 1)/(x + sin(x)**2 + cos(x)**2)
    ans = Matrix([1])
    M = Matrix([expr])
    assert trigsimp(M, method='fu', measure=measure1) == ans
    assert trigsimp(M, method='fu', measure=measure2) != ans
    # all methods should work with Basic expressions even if they
    # aren't Expr
    M = Matrix.eye(1)
    assert all(trigsimp(M, method=m) == M for m in
               'fu matching groebner old'.split())
    # watch for E in exptrigsimp, not only exp()
    eq = 1/sqrt(E) + E
    assert exptrigsimp(eq) == eq
Exemplo n.º 32
0
def test_as_immutable():
    X = Matrix([[1, 2], [3, 4]])
    assert sympify(X) == X.as_immutable() == ImmutableMatrix([[1, 2], [3, 4]])
    X = SparseMatrix(5, 5, {})
    assert sympify(X) == X.as_immutable() == ImmutableSparseMatrix(
        [[0 for i in range(5)] for i in range(5)])
Exemplo n.º 33
0
def test_sympyissue_11413():
    V = Matrix([[x], [y], [z]])
    U = V.normalized()
    r = sqrt(Abs(x)**2 + Abs(y)**2 + Abs(z)**2)
    assert U == Matrix([[x/r], [y/r], [z/r]])
    assert U.norm().simplify() == 1