예제 #1
0
파일: test_pycode.py 프로젝트: msgoff/sympy
def test_NumPyPrinter():
    from sympy import (
        Lambda,
        ZeroMatrix,
        OneMatrix,
        FunctionMatrix,
        HadamardProduct,
        KroneckerProduct,
        Adjoint,
        DiagonalOf,
        DiagMatrix,
        DiagonalMatrix,
    )
    from sympy.abc import a, b

    p = NumPyPrinter()
    assert p.doprint(sign(x)) == "numpy.sign(x)"
    A = MatrixSymbol("A", 2, 2)
    B = MatrixSymbol("B", 2, 2)
    C = MatrixSymbol("C", 1, 5)
    D = MatrixSymbol("D", 3, 4)
    assert p.doprint(A**(-1)) == "numpy.linalg.inv(A)"
    assert p.doprint(A**5) == "numpy.linalg.matrix_power(A, 5)"
    assert p.doprint(Identity(3)) == "numpy.eye(3)"

    u = MatrixSymbol("x", 2, 1)
    v = MatrixSymbol("y", 2, 1)
    assert p.doprint(MatrixSolve(A, u)) == "numpy.linalg.solve(A, x)"
    assert p.doprint(MatrixSolve(A, u) + v) == "numpy.linalg.solve(A, x) + y"

    assert p.doprint(ZeroMatrix(2, 3)) == "numpy.zeros((2, 3))"
    assert p.doprint(OneMatrix(2, 3)) == "numpy.ones((2, 3))"
    assert (p.doprint(FunctionMatrix(4, 5, Lambda(
        (a, b), a + b))) == "numpy.fromfunction(lambda a, b: a + b, (4, 5))")
    assert p.doprint(HadamardProduct(A, B)) == "numpy.multiply(A, B)"
    assert p.doprint(KroneckerProduct(A, B)) == "numpy.kron(A, B)"
    assert p.doprint(Adjoint(A)) == "numpy.conjugate(numpy.transpose(A))"
    assert p.doprint(DiagonalOf(A)) == "numpy.reshape(numpy.diag(A), (-1, 1))"
    assert p.doprint(DiagMatrix(C)) == "numpy.diagflat(C)"
    assert p.doprint(DiagonalMatrix(D)) == "numpy.multiply(D, numpy.eye(3, 4))"

    # Workaround for numpy negative integer power errors
    assert p.doprint(x**-1) == "x**(-1.0)"
    assert p.doprint(x**-2) == "x**(-2.0)"

    assert p.doprint(S.Exp1) == "numpy.e"
    assert p.doprint(S.Pi) == "numpy.pi"
    assert p.doprint(S.EulerGamma) == "numpy.euler_gamma"
    assert p.doprint(S.NaN) == "numpy.nan"
    assert p.doprint(S.Infinity) == "numpy.PINF"
    assert p.doprint(S.NegativeInfinity) == "numpy.NINF"
예제 #2
0
def test_diagonal():
    assert ask(Q.diagonal(X + Z.T + Identity(2)), Q.diagonal(X) &
               Q.diagonal(Z)) is True
    assert ask(Q.diagonal(ZeroMatrix(3, 3)))
    assert ask(Q.lower_triangular(X) & Q.upper_triangular(X), Q.diagonal(X))
    assert ask(Q.diagonal(X), Q.lower_triangular(X) & Q.upper_triangular(X))
    assert ask(Q.symmetric(X), Q.diagonal(X))
    assert ask(Q.triangular(X), Q.diagonal(X))
    assert ask(Q.diagonal(C0x0))
    assert ask(Q.diagonal(A1x1))
    assert ask(Q.diagonal(A1x1 + B1x1))
    assert ask(Q.diagonal(A1x1*B1x1))
    assert ask(Q.diagonal(V1.T*V2))
    assert ask(Q.diagonal(V1.T*(X + Z)*V1))
    assert ask(Q.diagonal(MatrixSlice(Y, (0, 1), (1, 2)))) is True
    assert ask(Q.diagonal(V1.T*(V1 + V2))) is True
    assert ask(Q.diagonal(X**3), Q.diagonal(X))
    assert ask(Q.diagonal(Identity(3)))
    assert ask(Q.diagonal(DiagMatrix(V1)))
    assert ask(Q.diagonal(DiagonalMatrix(X)))