def test_canonicalize(): X = MatrixSymbol('X', 2, 2) Y = MatrixSymbol('Y', 2, 2) expr = HadamardProduct(X, check=False) assert isinstance(expr, HadamardProduct) expr2 = expr.doit() # unpack is called assert isinstance(expr2, MatrixSymbol) Z = ZeroMatrix(2, 2) U = OneMatrix(2, 2) assert HadamardProduct(Z, X).doit() == Z assert HadamardProduct(U, X, X, U).doit() == HadamardPower(X, 2) assert HadamardProduct(X, U, Y).doit() == HadamardProduct(X, Y) assert HadamardProduct(X, Z, U, Y).doit() == Z
def test_mixed_indexing(): X = MatrixSymbol('X', 2, 2) Y = MatrixSymbol('Y', 2, 2) Z = MatrixSymbol('Z', 2, 2) assert (X*HadamardProduct(Y, Z))[0, 0] == \ X[0, 0]*Y[0, 0]*Z[0, 0] + X[0, 1]*Y[1, 0]*Z[1, 0]
def test_mixed_indexing(): X = MatrixSymbol("X", 2, 2) Y = MatrixSymbol("Y", 2, 2) Z = MatrixSymbol("Z", 2, 2) assert (X * HadamardProduct( Y, Z))[0, 0] == X[0, 0] * Y[0, 0] * Z[0, 0] + X[0, 1] * Y[1, 0] * Z[1, 0]
def test_hadamard_product_with_explicit_mat(): A = MatrixSymbol("A", 3, 3).as_explicit() B = MatrixSymbol("B", 3, 3).as_explicit() X = MatrixSymbol("X", 3, 3) expr = hadamard_product(A, B) ret = Matrix([i * j for i, j in zip(A, B)]).reshape(3, 3) assert expr == ret expr = hadamard_product(A, X, B) assert expr == HadamardProduct(ret, X)
def test_tensorflow_matrices(): if not tf: skip("TensorFlow not installed") expr = M assert tensorflow_code(expr) == "M" _compare_tensorflow_matrix((M, ), expr) expr = M + N assert tensorflow_code(expr) == "tensorflow.math.add(M, N)" _compare_tensorflow_matrix((M, N), expr) expr = M * N assert tensorflow_code(expr) == "tensorflow.linalg.matmul(M, N)" _compare_tensorflow_matrix((M, N), expr) expr = HadamardProduct(M, N) assert tensorflow_code(expr) == "tensorflow.math.multiply(M, N)" _compare_tensorflow_matrix((M, N), expr) expr = M * N * P * Q assert tensorflow_code(expr) == \ "tensorflow.linalg.matmul(" \ "tensorflow.linalg.matmul(" \ "tensorflow.linalg.matmul(M, N), P), Q)" _compare_tensorflow_matrix((M, N, P, Q), expr) expr = M**3 assert tensorflow_code(expr) == \ "tensorflow.linalg.matmul(tensorflow.linalg.matmul(M, M), M)" _compare_tensorflow_matrix((M, ), expr) expr = Trace(M) assert tensorflow_code(expr) == "tensorflow.linalg.trace(M)" _compare_tensorflow_matrix((M, ), expr) expr = Determinant(M) assert tensorflow_code(expr) == "tensorflow.linalg.det(M)" _compare_tensorflow_matrix_scalar((M, ), expr) expr = Inverse(M) assert tensorflow_code(expr) == "tensorflow.linalg.inv(M)" _compare_tensorflow_matrix_inverse((M, ), expr, use_float=True) expr = M.T assert tensorflow_code(expr, tensorflow_version='1.14') == \ "tensorflow.linalg.matrix_transpose(M)" assert tensorflow_code(expr, tensorflow_version='1.13') == \ "tensorflow.matrix_transpose(M)" _compare_tensorflow_matrix((M, ), expr)
def test_HadamardProduct(): assert HadamardProduct(A, B, A).shape == A.shape raises(ShapeError, lambda: HadamardProduct(A, B.T)) raises(TypeError, lambda: HadamardProduct(A, n)) raises(TypeError, lambda: HadamardProduct(A, 1)) assert HadamardProduct(A, 2*B, -A)[1, 1] == \ -2 * A[1, 1] * B[1, 1] * A[1, 1] mix = HadamardProduct(Z*A, B)*C assert mix.shape == (n, k) assert set(HadamardProduct(A, B, A).T.args) == set((A.T, A.T, B.T))
def test_HadamardProduct(): n, m, k = symbols('n,m,k') Z = MatrixSymbol('Z', n, n) A = MatrixSymbol('A', n, m) B = MatrixSymbol('B', n, m) C = MatrixSymbol('C', m, k) assert HadamardProduct(A, B, A).shape == A.shape raises(ShapeError, lambda: HadamardProduct(A, B.T)) raises(TypeError, lambda: HadamardProduct(A, n)) raises(TypeError, lambda: HadamardProduct(A, 1)) assert HadamardProduct(A, 2 * B, -A)[1, 1] == -2 * A[1, 1]**2 * B[1, 1] mix = HadamardProduct(Z * A, B) * C assert mix.shape == (n, k) assert HadamardProduct(A, B, A).T == HadamardProduct(A.T, B.T, A.T)
def test_hadamard(): m, n, p = symbols('m, n, p', integer=True) A = MatrixSymbol('A', m, n) B = MatrixSymbol('B', m, n) C = MatrixSymbol('C', m, p) X = MatrixSymbol('X', m, m) I = Identity(m) with raises(TypeError): hadamard_product() assert hadamard_product(A) == A assert isinstance(hadamard_product(A, B), HadamardProduct) assert hadamard_product(A, B).doit() == hadamard_product(A, B) with raises(ShapeError): hadamard_product(A, C) hadamard_product(A, I) assert hadamard_product(X, I) == X assert isinstance(hadamard_product(X, I), MatrixSymbol) a = MatrixSymbol("a", k, 1) expr = MatAdd(ZeroMatrix(k, 1), OneMatrix(k, 1)) expr = HadamardProduct(expr, a) assert expr.doit() == a
def test_canonicalize(): X = MatrixSymbol('X', 2, 2) expr = HadamardProduct(X, check=False) assert isinstance(expr, HadamardProduct) expr2 = expr.doit() # unpack is called assert isinstance(expr2, MatrixSymbol)
def test_HadamardProduct_isnt_commutative(): assert HadamardProduct(A, B) != HadamardProduct(B, A)
def test_canonicalize(): X = MatrixSymbol('X', 2, 2) expr = HadamardProduct(X, evaluate=False, check=False) assert isinstance(expr, HadamardProduct) expr2 = expr.canonicalize() # unpack is called assert isinstance(expr2, MatrixSymbol)