def test_matrix_derivatives_of_traces(): ## First order: # Cookbook example 99: expr = Trace(X) assert expr.diff(X) == Identity(k) # Cookbook example 100: expr = Trace(X * A) assert expr.diff(X) == A.T # Cookbook example 101: expr = Trace(A * X * B) assert expr.diff(X) == A.T * B.T # Cookbook example 102: expr = Trace(A * X.T * B) assert expr.diff(X) == B * A # Cookbook example 103: expr = Trace(X.T * A) assert expr.diff(X) == A # Cookbook example 104: expr = Trace(A * X.T) assert expr.diff(X) == A # Cookbook example 105: # TODO: TensorProduct is not supported #expr = Trace(TensorProduct(A, X)) #assert expr.diff(X) == Trace(A)*Identity(k) ## Second order: # Cookbook example 106: expr = Trace(X**2) assert expr.diff(X) == 2 * X.T # Cookbook example 107: expr = Trace(X**2 * B) assert expr.diff(X) == (X * B + B * X).T expr = Trace(MatMul(X, X, B)) assert expr.diff(X) == (X * B + B * X).T # Cookbook example 108: expr = Trace(X.T * B * X) assert expr.diff(X) == B * X + B.T * X # Cookbook example 109: expr = Trace(B * X * X.T) assert expr.diff(X) == B * X + B.T * X # Cookbook example 110: expr = Trace(X * X.T * B) assert expr.diff(X) == B * X + B.T * X # Cookbook example 111: expr = Trace(X * B * X.T) assert expr.diff(X) == X * B.T + X * B # Cookbook example 112: expr = Trace(B * X.T * X) assert expr.diff(X) == X * B.T + X * B # Cookbook example 113: expr = Trace(X.T * X * B) assert expr.diff(X) == X * B.T + X * B # Cookbook example 114: expr = Trace(A * X * B * X) assert expr.diff(X) == A.T * X.T * B.T + B.T * X.T * A.T # Cookbook example 115: expr = Trace(X.T * X) assert expr.diff(X) == 2 * X expr = Trace(X * X.T) assert expr.diff(X) == 2 * X # Cookbook example 116: expr = Trace(B.T * X.T * C * X * B) assert expr.diff(X) == C.T * X * B * B.T + C * X * B * B.T # Cookbook example 117: expr = Trace(X.T * B * X * C) assert expr.diff(X) == B * X * C + B.T * X * C.T # Cookbook example 118: expr = Trace(A * X * B * X.T * C) assert expr.diff(X) == A.T * C.T * X * B.T + C * A * X * B # Cookbook example 119: expr = Trace((A * X * B + C) * (A * X * B + C).T) assert expr.diff(X) == 2 * A.T * (A * X * B + C) * B.T # Cookbook example 120: # TODO: no support for TensorProduct. # expr = Trace(TensorProduct(X, X)) # expr = Trace(X)*Trace(X) # expr.diff(X) == 2*Trace(X)*Identity(k) # Higher Order # Cookbook example 121: expr = Trace(X**k) #assert expr.diff(X) == k*(X**(k-1)).T # Cookbook example 122: expr = Trace(A * X**k) #assert expr.diff(X) == # Needs indices # Cookbook example 123: expr = Trace(B.T * X.T * C * X * X.T * C * X * B) assert expr.diff( X ) == C * X * X.T * C * X * B * B.T + C.T * X * B * B.T * X.T * C.T * X + C * X * B * B.T * X.T * C * X + C.T * X * X.T * C.T * X * B * B.T # Other # Cookbook example 124: expr = Trace(A * X**(-1) * B) assert expr.diff(X) == -Inverse(X).T * A.T * B.T * Inverse(X).T # Cookbook example 125: expr = Trace(Inverse(X.T * C * X) * A) # Warning: result in the cookbook is equivalent if B and C are symmetric: assert expr.diff(X) == -X.inv().T * A.T * X.inv() * C.inv().T * X.inv( ).T - X.inv().T * A * X.inv() * C.inv() * X.inv().T # Cookbook example 126: expr = Trace((X.T * C * X).inv() * (X.T * B * X)) assert expr.diff(X) == -2 * C * X * (X.T * C * X).inv() * X.T * B * X * ( X.T * C * X).inv() + 2 * B * X * (X.T * C * X).inv() # Cookbook example 127: expr = Trace((A + X.T * C * X).inv() * (X.T * B * X)) # Warning: result in the cookbook is equivalent if B and C are symmetric: assert expr.diff(X) == B * X * Inverse(A + X.T * C * X) - C * X * Inverse( A + X.T * C * X) * X.T * B * X * Inverse(A + X.T * C * X) - C.T * X * Inverse( A.T + (C * X).T * X) * X.T * B.T * X * Inverse( A.T + (C * X).T * X) + B.T * X * Inverse(A.T + (C * X).T * X)
def test_matrix_expression_from_index_summation(): from sympy.abc import a, b, c, d A = MatrixSymbol("A", k, k) B = MatrixSymbol("B", k, k) C = MatrixSymbol("C", k, k) w1 = MatrixSymbol("w1", k, 1) i0, i1, i2, i3, i4 = symbols("i0:5", cls=Dummy) expr = Sum(W[a, b] * X[b, c] * Z[c, d], (b, 0, l - 1), (c, 0, m - 1)) assert MatrixExpr.from_index_summation(expr, a) == W * X * Z expr = Sum(W.T[b, a] * X[b, c] * Z[c, d], (b, 0, l - 1), (c, 0, m - 1)) assert MatrixExpr.from_index_summation(expr, a) == W * X * Z expr = Sum(A[b, a] * B[b, c] * C[c, d], (b, 0, k - 1), (c, 0, k - 1)) assert MatrixSymbol.from_index_summation(expr, a) == A.T * B * C expr = Sum(A[b, a] * B[c, b] * C[c, d], (b, 0, k - 1), (c, 0, k - 1)) assert MatrixSymbol.from_index_summation(expr, a) == A.T * B.T * C expr = Sum(C[c, d] * A[b, a] * B[c, b], (b, 0, k - 1), (c, 0, k - 1)) assert MatrixSymbol.from_index_summation(expr, a) == A.T * B.T * C expr = Sum(A[a, b] + B[a, b], (a, 0, k - 1), (b, 0, k - 1)) assert MatrixExpr.from_index_summation(expr, a) == A + B expr = Sum((A[a, b] + B[a, b]) * C[b, c], (b, 0, k - 1)) assert MatrixExpr.from_index_summation(expr, a) == (A + B) * C expr = Sum((A[a, b] + B[b, a]) * C[b, c], (b, 0, k - 1)) assert MatrixExpr.from_index_summation(expr, a) == (A + B.T) * C expr = Sum(A[a, b] * A[b, c] * A[c, d], (b, 0, k - 1), (c, 0, k - 1)) assert MatrixExpr.from_index_summation(expr, a) == MatMul(A, A, A) expr = Sum(A[a, b] * A[b, c] * B[c, d], (b, 0, k - 1), (c, 0, k - 1)) assert MatrixExpr.from_index_summation(expr, a) == MatMul(A, A, B) # Parse the trace of a matrix: expr = Sum(A[a, a], (a, 0, k - 1)) assert MatrixExpr.from_index_summation(expr, None) == trace(A) expr = Sum(A[a, a] * B[b, c] * C[c, d], (a, 0, k - 1), (c, 0, k - 1)) assert MatrixExpr.from_index_summation(expr, b) == trace(A) * B * C # Check wrong sum ranges (should raise an exception): ## Case 1: 0 to m instead of 0 to m-1 expr = Sum(W[a, b] * X[b, c] * Z[c, d], (b, 0, l - 1), (c, 0, m)) raises(ValueError, lambda: MatrixExpr.from_index_summation(expr, a)) ## Case 2: 1 to m-1 instead of 0 to m-1 expr = Sum(W[a, b] * X[b, c] * Z[c, d], (b, 0, l - 1), (c, 1, m - 1)) raises(ValueError, lambda: MatrixExpr.from_index_summation(expr, a)) # Parse nested sums: expr = Sum(A[a, b] * Sum(B[b, c] * C[c, d], (c, 0, k - 1)), (b, 0, k - 1)) assert MatrixExpr.from_index_summation(expr, a) == A * B * C # Test Kronecker delta: expr = Sum(A[a, b] * KroneckerDelta(b, c) * B[c, d], (b, 0, k - 1), (c, 0, k - 1)) assert MatrixExpr.from_index_summation(expr, a) == A * B expr = Sum( KroneckerDelta(i1, m) * KroneckerDelta(i2, n) * A[i, i1] * A[j, i2], (i1, 0, k - 1), (i2, 0, k - 1)) assert MatrixExpr.from_index_summation(expr, m) == A.T * A[j, n] # Test numbered indices: expr = Sum(A[i1, i2] * w1[i2, 0], (i2, 0, k - 1)) assert MatrixExpr.from_index_summation(expr, i1) == A * w1 expr = Sum(A[i1, i2] * B[i2, 0], (i2, 0, k - 1)) assert MatrixExpr.from_index_summation(expr, i1) == MatrixElement(A * B, i1, 0)
def test_matrix_derivatives_of_traces(): ## First order: # Cookbook example 99: expr = Trace(X) assert expr.diff(X) == Identity(k) # Cookbook example 100: expr = Trace(X * A) assert expr.diff(X) == A.T # Cookbook example 101: expr = Trace(A * X * B) assert expr.diff(X) == A.T * B.T # Cookbook example 102: expr = Trace(A * X.T * B) assert expr.diff(X) == B * A # Cookbook example 103: expr = Trace(X.T * A) assert expr.diff(X) == A # Cookbook example 104: expr = Trace(A * X.T) assert expr.diff(X) == A # Cookbook example 105: # TODO: TensorProduct is not supported #expr = Trace(TensorProduct(A, X)) #assert expr.diff(X) == Trace(A)*Identity(k) ## Second order: # Cookbook example 106: expr = Trace(X**2) assert expr.diff(X) == 2 * X.T # Cookbook example 107: expr = Trace(X**2 * B) # TODO: wrong result #assert expr.diff(X) == (X*B + B*X).T expr = Trace(MatMul(X, X, B)) assert expr.diff(X) == (X * B + B * X).T # Cookbook example 108: expr = Trace(X.T * B * X) assert expr.diff(X) == B * X + B.T * X # Cookbook example 109: expr = Trace(B * X * X.T) assert expr.diff(X) == B * X + B.T * X # Cookbook example 110: expr = Trace(X * X.T * B) assert expr.diff(X) == B * X + B.T * X # Cookbook example 111: expr = Trace(X * B * X.T) assert expr.diff(X) == X * B.T + X * B # Cookbook example 112: expr = Trace(B * X.T * X) assert expr.diff(X) == X * B.T + X * B # Cookbook example 113: expr = Trace(X.T * X * B) assert expr.diff(X) == X * B.T + X * B # Cookbook example 114: expr = Trace(A * X * B * X) assert expr.diff(X) == A.T * X.T * B.T + B.T * X.T * A.T # Cookbook example 115: expr = Trace(X.T * X) assert expr.diff(X) == 2 * X expr = Trace(X * X.T) assert expr.diff(X) == 2 * X # Cookbook example 116: expr = Trace(B.T * X.T * C * X * B) assert expr.diff(X) == C.T * X * B * B.T + C * X * B * B.T # Cookbook example 117: expr = Trace(X.T * B * X * C) assert expr.diff(X) == B * X * C + B.T * X * C.T # Cookbook example 118: expr = Trace(A * X * B * X.T * C) assert expr.diff(X) == A.T * C.T * X * B.T + C * A * X * B # Cookbook example 119: expr = Trace((A * X * B + C) * (A * X * B + C).T) assert expr.diff(X) == 2 * A.T * (A * X * B + C) * B.T
class TestAllGood(object): # These latex strings should parse to the corresponding SymPy expression GOOD_PAIRS = [ ("0", 0), ("1", 1), ("-3.14", -3.14), ("5-3", _Add(5, -3)), ("(-7.13)(1.5)", _Mul(Rational('-7.13'), Rational('1.5'))), ("\\left(-7.13\\right)\\left(1.5\\right)", _Mul(Rational('-7.13'), Rational('1.5'))), ("x", x), ("2x", 2 * x), ("x^2", x**2), ("x^{3 + 1}", x**_Add(3, 1)), ("x^{\\left\\{3 + 1\\right\\}}", x**_Add(3, 1)), ("-3y + 2x", _Add(_Mul(2, x), Mul(-1, 3, y, evaluate=False))), ("-c", -c), ("a \\cdot b", a * b), ("a / b", a / b), ("a \\div b", a / b), ("a + b", a + b), ("a + b - a", Add(a, b, _Mul(-1, a), evaluate=False)), ("a^2 + b^2 = c^2", Eq(a**2 + b**2, c**2)), ("a^2 + b^2 != 2c^2", Ne(a**2 + b**2, 2 * c**2)), ("a\\mod b", Mod(a, b)), ("\\sin \\theta", sin(theta)), ("\\sin(\\theta)", sin(theta)), ("\\sin\\left(\\theta\\right)", sin(theta)), ("\\sin^{-1} a", asin(a)), ("\\sin a \\cos b", _Mul(sin(a), cos(b))), ("\\sin \\cos \\theta", sin(cos(theta))), ("\\sin(\\cos \\theta)", sin(cos(theta))), ("\\arcsin(a)", asin(a)), ("\\arccos(a)", acos(a)), ("\\arctan(a)", atan(a)), ("\\sinh(a)", sinh(a)), ("\\cosh(a)", cosh(a)), ("\\tanh(a)", tanh(a)), ("\\sinh^{-1}(a)", asinh(a)), ("\\cosh^{-1}(a)", acosh(a)), ("\\tanh^{-1}(a)", atanh(a)), ("\\arcsinh(a)", asinh(a)), ("\\arccosh(a)", acosh(a)), ("\\arctanh(a)", atanh(a)), ("\\arsinh(a)", asinh(a)), ("\\arcosh(a)", acosh(a)), ("\\artanh(a)", atanh(a)), ("\\operatorname{arcsinh}(a)", asinh(a)), ("\\operatorname{arccosh}(a)", acosh(a)), ("\\operatorname{arctanh}(a)", atanh(a)), ("\\operatorname{arsinh}(a)", asinh(a)), ("\\operatorname{arcosh}(a)", acosh(a)), ("\\operatorname{artanh}(a)", atanh(a)), ("\\cos^2(x)", cos(x)**2), ("\\cos(x)^2", cos(x)**2), ("\\frac{a}{b}", a / b), ("\\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))), ("\\frac{7}{3}", _Mul(7, _Pow(3, -1))), ("(\\csc x)(\\sec y)", csc(x) * sec(y)), ("\\lim_{x \\to 3} a", Limit(a, x, 3)), ("\\lim_{x \\rightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\Rightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\Longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\to 3^{+}} a", Limit(a, x, 3, dir='+')), ("\\lim_{x \\to 3^{-}} a", Limit(a, x, 3, dir='-')), ("\\infty", oo), ("\\infty\\%", oo), ("\\$\\infty", oo), ("-\\infty", -oo), ("-\\infty\\%", -oo), ("-\\$\\infty", -oo), ("\\lim_{x \\to \\infty} \\frac{1}{x}", Limit(_Mul(1, _Pow(x, -1)), x, oo)), ("\\frac{d}{dx} x", Derivative(x, x)), ("\\frac{d}{dt} x", Derivative(x, t)), # ("f(x)", f(x)), # ("f(x, y)", f(x, y)), # ("f(x, y, z)", f(x, y, z)), # ("\\frac{d f(x)}{dx}", Derivative(f(x), x)), # ("\\frac{d\\theta(x)}{dx}", Derivative(theta(x), x)), ("|x|", _Abs(x)), ("\\left|x\\right|", _Abs(x)), ("||x||", _Abs(Abs(x))), ("|x||y|", _Abs(x) * _Abs(y)), ("||x||y||", _Abs(_Abs(x) * _Abs(y))), ("\\pi^{|xy|}", pi**_Abs(x * y)), ("\\frac{\\pi}{3}", _Mul(pi, _Pow(3, -1))), ("\\sin{\\frac{\\pi}{2}}", sin(_Mul(pi, _Pow(2, -1)), evaluate=False)), ("a+bI", a + I * b), ("e^{I\\pi}", -1), ("\\int x dx", Integral(x, x)), ("\\int x d\\theta", Integral(x, theta)), ("\\int (x^2 - y)dx", Integral(x**2 - y, x)), ("\\int x + a dx", Integral(_Add(x, a), x)), ("\\int da", Integral(1, a)), ("\\int_0^7 dx", Integral(1, (x, 0, 7))), ("\\int_a^b x dx", Integral(x, (x, a, b))), ("\\int^b_a x dx", Integral(x, (x, a, b))), ("\\int_{a}^b x dx", Integral(x, (x, a, b))), ("\\int^{b}_a x dx", Integral(x, (x, a, b))), ("\\int_{a}^{b} x dx", Integral(x, (x, a, b))), ("\\int_{ }^{}x dx", Integral(x, x)), ("\\int^{ }_{ }x dx", Integral(x, x)), ("\\int^{b}_{a} x dx", Integral(x, (x, a, b))), # ("\\int_{f(a)}^{f(b)} f(z) dz", Integral(f(z), (z, f(a), f(b)))), ("\\int (x+a)", Integral(_Add(x, a), x)), ("\\int a + b + c dx", Integral(Add(a, b, c, evaluate=False), x)), ("\\int \\frac{dz}{z}", Integral(Pow(z, -1), z)), ("\\int \\frac{3 dz}{z}", Integral(3 * Pow(z, -1), z)), ("\\int \\frac{1}{x} dx", Integral(Pow(x, -1), x)), ("\\int \\frac{1}{a} + \\frac{1}{b} dx", Integral(_Add(_Pow(a, -1), Pow(b, -1)), x)), ("\\int \\frac{3 \\cdot d\\theta}{\\theta}", Integral(3 * _Pow(theta, -1), theta)), ("\\int \\frac{1}{x} + 1 dx", Integral(_Add(_Pow(x, -1), 1), x)), ("x_0", Symbol('x_{0}', real=True)), ("x_{1}", Symbol('x_{1}', real=True)), ("x_a", Symbol('x_{a}', real=True)), ("x_{b}", Symbol('x_{b}', real=True)), ("h_\\theta", Symbol('h_{theta}', real=True)), ("h_{\\theta}", Symbol('h_{theta}', real=True)), # ("h_{\\theta}(x_0, x_1)", Symbol('h_{theta}', real=True)(Symbol('x_{0}', real=True), Symbol('x_{1}', real=True))), ("x!", _factorial(x)), ("100!", _factorial(100)), ("\\theta!", _factorial(theta)), ("(x + 1)!", _factorial(_Add(x, 1))), ("\\left(x + 1\\right)!", _factorial(_Add(x, 1))), ("(x!)!", _factorial(_factorial(x))), ("x!!!", _factorial(_factorial(_factorial(x)))), ("5!7!", _Mul(_factorial(5), _factorial(7))), ("\\sqrt{x}", sqrt(x)), ("\\sqrt{x + b}", sqrt(_Add(x, b))), ("\\sqrt[3]{\\sin x}", root(sin(x), 3)), ("\\sqrt[y]{\\sin x}", root(sin(x), y)), ("\\sqrt[\\theta]{\\sin x}", root(sin(x), theta)), ("x < y", StrictLessThan(x, y)), ("x \\leq y", LessThan(x, y)), ("x > y", StrictGreaterThan(x, y)), ("x \\geq y", GreaterThan(x, y)), ("\\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))), ("\\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum^3_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))), ("\\sum_{n = 0}^{\\infty} \\frac{1}{n!}", Sum(_Pow(_factorial(n), -1), (n, 0, oo))), ("\\prod_{a = b}^{c} x", Product(x, (a, b, c))), ("\\prod_{a = b}^c x", Product(x, (a, b, c))), ("\\prod^{c}_{a = b} x", Product(x, (a, b, c))), ("\\prod^c_{a = b} x", Product(x, (a, b, c))), ("\\ln x", _log(x, E)), ("\\ln xy", _log(x * y, E)), ("\\log x", _log(x, 10)), ("\\log xy", _log(x * y, 10)), # ("\\log_2 x", _log(x, 2)), ("\\log_{2} x", _log(x, 2)), # ("\\log_a x", _log(x, a)), ("\\log_{a} x", _log(x, a)), ("\\log_{11} x", _log(x, 11)), ("\\log_{a^2} x", _log(x, _Pow(a, 2))), ("[x]", x), ("[a + b]", _Add(a, b)), ("\\frac{d}{dx} [ \\tan x ]", Derivative(tan(x), x)), ("2\\overline{x}", 2 * Symbol('xbar', real=True)), ("2\\overline{x}_n", 2 * Symbol('xbar_{n}', real=True)), ("\\frac{x}{\\overline{x}_n}", x / Symbol('xbar_{n}', real=True)), ("\\frac{\\sin(x)}{\\overline{x}_n}", sin(Symbol('x', real=True)) / Symbol('xbar_{n}', real=True)), ("2\\bar{x}", 2 * Symbol('xbar', real=True)), ("2\\bar{x}_n", 2 * Symbol('xbar_{n}', real=True)), ("\\sin\\left(\\theta\\right) \\cdot4", sin(theta) * 4), ("\\ln\\left(\\theta\\right)", _log(theta, E)), ("\\ln\\left(x-\\theta\\right)", _log(x - theta, E)), ("\\ln\\left(\\left(x-\\theta\\right)\\right)", _log(x - theta, E)), ("\\ln\\left(\\left[x-\\theta\\right]\\right)", _log(x - theta, E)), ("\\ln\\left(\\left\\{x-\\theta\\right\\}\\right)", _log(x - theta, E)), ("\\ln\\left(\\left|x-\\theta\\right|\\right)", _log(_Abs(x - theta), E)), ("\\frac{1}{2}xy(x+y)", Mul(_Pow(2, -1), x, y, (x + y), evaluate=False)), ("\\frac{1}{2}\\theta(x+y)", Mul(_Pow(2, -1), theta, (x + y), evaluate=False)), ("1-f(x)", 1 - f * x), ("\\begin{matrix}1&2\\\\3&4\\end{matrix}", Matrix([[1, 2], [3, 4]])), ("\\begin{matrix}x&x^2\\\\\\sqrt{x}&x\\end{matrix}", Matrix([[x, x**2], [_Pow(x, S.Half), x]])), ("\\begin{matrix}\\sqrt{x}\\\\\\sin(\\theta)\\end{matrix}", Matrix([_Pow(x, S.Half), sin(theta)])), ("\\begin{pmatrix}1&2\\\\3&4\\end{pmatrix}", Matrix([[1, 2], [3, 4]])), ("\\begin{bmatrix}1&2\\\\3&4\\end{bmatrix}", Matrix([[1, 2], [3, 4]])), # scientific notation ("2.5\\times 10^2", 250), ("1,500\\times 10^{-1}", 150), # e notation ("2.5E2", 250), ("1,500E-1", 150), # multiplication without cmd ("2x2y", Mul(2, x, 2, y, evaluate=False)), ("2x2", Mul(2, x, 2, evaluate=False)), ("x2", x * 2), # lin alg processing ("\\theta\\begin{matrix}1&2\\\\3&4\\end{matrix}", MatMul(theta, Matrix([[1, 2], [3, 4]]), evaluate=False)), ("\\theta\\begin{matrix}1\\\\3\\end{matrix} - \\begin{matrix}-1\\\\2\\end{matrix}", MatAdd(MatMul(theta, Matrix([[1], [3]]), evaluate=False), MatMul(-1, Matrix([[-1], [2]]), evaluate=False), evaluate=False)), ("\\theta\\begin{matrix}1&0\\\\0&1\\end{matrix}*\\begin{matrix}3\\\\-2\\end{matrix}", MatMul(theta, Matrix([[1, 0], [0, 1]]), Matrix([3, -2]), evaluate=False)), ("\\frac{1}{9}\\theta\\begin{matrix}1&2\\\\3&4\\end{matrix}", MatMul(Pow(9, -1, evaluate=False), theta, Matrix([[1, 2], [3, 4]]), evaluate=False)), ("\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix};\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}\\right\\}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix},\\begin{pmatrix}1\\\\1\\\\1\\end{pmatrix}\\right\\}", [Matrix([1, 2, 3]), Matrix([4, 3, 1]), Matrix([1, 1, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}\\right\\}", Matrix([1, 2, 3])), ("\\left{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}\\right}", Matrix([1, 2, 3])), ("{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}}", Matrix([1, 2, 3])), # us dollars ("\\$1,000.00", 1000), ("\\$543.21", 543.21), ("\\$0.009", 0.009), # percentages ("100\\%", 1), ("1.5\\%", 0.015), ("0.05\\%", 0.0005), # empty set ("\\emptyset", S.EmptySet) ] def test_good_pair(self, s, eq): assert_equal(s, eq)
from __future__ import division, print_function import sympy from sympy import (init_printing, Matrix, MatMul, integrate, symbols) init_printing() dt, phi, u, x, y, z = symbols('dt \Phi_s, \mu, x,y,z') F_k = Matrix([[1, 0, dt, 0, 0], [0, 1, 0, dt, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]) Q_c = Matrix([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]]) * phi Q = integrate(F_k * Q_c * F_k.T, (dt, 0, dt)) # factor phi out of the matrix to make it more readable Q = Q / phi MatMul(Q, phi) print(Q) print(Q * Q.T)
def as_coeff_mmul(self): return 1, MatMul(self)
from sympy import (Symbol, MatrixSymbol, ZeroMatrix, Add, Mul, MatAdd, MatMul, Determinant, Inverse, Trace, Transpose) from .symbols import d, Kron, SymmetricMatrixSymbol from .simplifications import simplify_matdiff MATRIX_DIFF_RULES = { # e =expression, s = a list of symbols respsect to which # we want to differentiate Symbol: lambda e, s: d(e) if (e in s) else 0, MatrixSymbol: lambda e, s: d(e) if (e in s) else ZeroMatrix(*e.shape), SymmetricMatrixSymbol: lambda e, s: d(e) if (e in s) else ZeroMatrix(*e.shape), Add: lambda e, s: Add(*[_matDiff_apply(arg, s) for arg in e.args]), Mul: lambda e, s: _matDiff_apply(e.args[0], s) if len(e.args)==1 else Mul(_matDiff_apply(e.args[0],s),Mul(*e.args[1:])) + Mul(e.args[0], _matDiff_apply(Mul(*e.args[1:]),s)), MatAdd: lambda e, s: MatAdd(*[_matDiff_apply(arg, s) for arg in e.args]), MatMul: lambda e, s: _matDiff_apply(e.args[0], s) if len(e.args)==1 else MatMul(_matDiff_apply(e.args[0],s),MatMul(*e.args[1:])) + MatMul(e.args[0], _matDiff_apply(MatMul(*e.args[1:]),s)), Kron: lambda e, s: _matDiff_apply(e.args[0],s) if len(e.args)==1 else Kron(_matDiff_apply(e.args[0],s),Kron(*e.args[1:])) + Kron(e.args[0],_matDiff_apply(Kron(*e.args[1:]),s)), Determinant: lambda e, s: MatMul(Determinant(e.args[0]), Trace(e.args[0].I*_matDiff_apply(e.args[0], s))), # inverse always has 1 arg, so we index Inverse: lambda e, s: -Inverse(e.args[0]) * _matDiff_apply(e.args[0], s) * Inverse(e.args[0]), # trace always has 1 arg Trace: lambda e, s: Trace(_matDiff_apply(e.args[0], s)), # transpose also always has 1 arg, index Transpose: lambda e, s: Transpose(_matDiff_apply(e.args[0], s)) } def _matDiff_apply(expr, syms): if expr.__class__ in list(MATRIX_DIFF_RULES.keys()): return MATRIX_DIFF_RULES[expr.__class__](expr, syms) elif expr.is_constant():
def _eval_inverse(self): try: return MatMul(*[arg.inverse() for arg in self.args[::-1]]).doit() except ShapeError: from sympy.matrices.expressions.inverse import Inverse return Inverse(self)
def expand(expr, factor=1, depth=0): return sympyExpand(expr) if depth == 0: return flattenAdd(expand(expr, depth=depth + 1)) if not isinstance(expr, MatMul) and not isinstance(expr, MatAdd): return sympyExpand(factor * expr, depth=depth + 1) else: if isinstance(expr, MatAdd): ret = [] for arg in expr.args: tmp = expand(arg, factor=factor, depth=depth + 1) if not isZero(tmp): ret.append(tmp) return MatAdd(*ret) scalar = factor mat = [] for el in expr.args: if not (hasattr(el, 'is_Matrix') and el.is_Matrix): scalar *= el else: mat.append(el) scalar = [ el for el in flatten(sympyExpand(scalar).as_coeff_add()) if el != 0 ] if len(scalar) == 0: return 0 if len(scalar) > 1: ret = [] for s in scalar: tmp = expand(MatMul(*mat), factor=s, depth=depth + 1) if not isZero(tmp): ret.append(tmp) return MatAdd(*ret) add = [] toDistribute = False for el in mat: el = expand(el, depth=depth + 1) if isinstance(el, MatAdd): add.append(el.args) toDistribute = True else: add.append([el]) if toDistribute == False: ret = scalar[0] for el in mat: if not isinstance(el, MatMul): ret *= el else: for ell in el.args: ret *= ell return ret add = itertools.product(*add) ret = [] for el in add: if len(el) == 1: tmp = expand(el[0], factor=scalar[0], depth=depth + 1) else: tmp = expand(MatMul(*el), factor=scalar[0], depth=depth + 1) if tmp == 0: continue ret.append(tmp) return MatAdd(*ret)
def as_coeff_mmul(self): coeff, matrices = self.as_coeff_matrices() return coeff, MatMul(*matrices)
def _eval_adjoint(self): return MatMul(*[adjoint(arg) for arg in self.args[::-1]]).doit()
def time_MatMul_doit(self): MatMul(*args).doit()
def time_MatMul(self): MatMul(*args)
import numpy as np import sympy from sympy import (init_printing, Matrix, MatMul, integrate, symbols) init_printing(use_latex='mathjax') var = symbols('\sigma^2_v') dt = symbols('\Delta{t}') # dim==4 (third order, constant jerk) ''' F_k = Matrix([ [1,dt,dt**2/2,dt**3/6], [0,1, dt,dt**2/2], [0,0,1,dt], [0,0,0,1] ]) v = Matrix([ [dt**3/6], [dt**2/2], [dt], [1] ]) Q = v * var * v.T Q = Q / var MatMul(Q,var) ''' # dim==3 (second order, constant acceleration) ''' F_k = Matrix([
def __matmul__(self, other): return MatMul(self, other).doit()
def comm(A, B): """ Returns the commutator of A,B: [A,B]=A.B-B.A. Assumes 'A','B' are sympy matrices.""" # TODO: extend to check type, and work for numpy matrices/ndarrays return (MatMul(A, B) - MatMul(B, A))
def __rmatmul__(self, other): return MatMul(other, self).doit()
def build_derivs(hamiltonian, decay=None, showeqs=False, lambdifyhelp=False, _hbar=None, get_eqs=False): """ return derivatives for RHS of von Neumann eq given a hamiltonian Args: 'hamiltonian': a sympy Matrix of dimension N x N representing the full system Hamiltonian 'lambdifyhelp': bool. if True, shows the docstring for the function generated by sympy Lambdify for the derivatives function, which is wrapped by the return function. 'decay': (optional, sympy Matrix) dimension N x N representing the "Louivillian" or decay operator '_hbar': (optional, int) set to 1 to use hbar=1 Returns: 'derivs': a function with call signature derivs(t, y), where t is time and y = [r00, r01, .. r11, r12, ..] is a list of unraveled density matrix elements of type complex """ assert hamiltonian.shape[0] == hamiltonian.shape[1], ( "Hamiltonian" + " must be square, " + f"not of shape " + str(hamiltonian.shape)) if _hbar == 1: h = 1 else: h = hbar dims = hamiltonian.shape[0] # build a symbolic density matrix r = MatrixSymbol('r', dims, dims).as_mutable() for i in range(dims): for j in range(dims): r[i, j] = symbols(f'r{i}{j}') if i > j: r[i, j] = np.conj(r[j, i]) # calculate [r, H]: rhs = -1j * comm(r, hamiltonian) / h # decay term if supplied: if decay != None: rhs -= MatMul(decay, r) / h # prune off non-redundant elements pruned_rhs = [] expected_elems = "" for i in range(dims): for j in range(dims): if i <= j: expected_elems += f'r{i}{j} ' pruned_rhs.append(rhs[i, j]) # sort arguments in the order of pruned_rhs args = list(rhs.free_symbols) # check if all the expected elems are in args, if not then add the elem expected_elems = symbols(expected_elems) for elem in expected_elems: if elem not in args: args.append(elem) args.sort(key=lambda x: x.__repr__()) rhs = pruned_rhs if showeqs is True: print("Equations: \n") for var, eq in zip(args, rhs): print('D[' + str(var) + '] = ' + str(eq) + '\n') f = lambdify(args, rhs) if lambdifyhelp: print(help(f)) # TODO make below general; maybe include the name of an additional args as # a kwarg and then check by name if it is in args # check whether t is in the args if 't' in [a.__repr__() for a in args]: def derivs(t, y): """ the RHS of the von Neumann equation for a given hamiltonian this function is a lambdifygenerated function which returns the derivative side of D[rho] = -i*[r, H]/hbar where H is the hamiltonian specified when build_derivs was called Args: 't': (float) the current time in the simulation 'y': (list-like) the unraveled non-redunant elements of the density matrix, of type complex. i.e. for H 3 x 3, y = [r00, r01, r02, r11, r12, r33] Returns: 'D[y]': (list-like) the element-wise derivative of y given by the generated equations. """ return f(*y, t) else: def derivs(t, y): """ the RHS of the von Neumann equation for a given hamiltonian this function is a lambdifygenerated function which returns the derivative side of D[rho] = -i*[r, H]/hbar where H is the hamiltonian specified when build_derivs was called Args: 't': (float) the current time in the simulation 'y': (list-like) the unraveled non-redunant elements of the density matrix, of type complex. i.e. for H 3 x 3, y = [r00, r01, r02, r11, r12, r33] Returns: 'D[y]': (list-like) the element-wise derivative of y given by the generated equations. """ return f(*y) if not get_eqs: return derivs else: return derivs, rhs
def __neg__(self): return MatMul(S.NegativeOne, self).doit()
def show_answer(self): init_printing() return Math( "$$" + latex( MatMul( self.A, self.x ), mat_str = "matrix" ) + "=" + latex( self.answer, mat_str = "matrix" ) + "$$")