Bug in Multiplication between MatrixSymbol and Matrix

23 views
Skip to first unread message

Duy Nguyen Truong

unread,
Apr 22, 2015, 11:46:08 PM4/22/15
to sy...@googlegroups.com

A matrix A and a MatrixSymbol B are defined as follows:

>>> from sympy import *
>>> q = MatrixSymbol('q', 4, 1)
>>> A = Matrix([[q[0,0], 1, 2], [q[1,0], 2, 3], [q[2,0], 3, 4]])
>>> B = MatrixSymbol('B', 3, 3)

The behavior is different for A * B and B * A. The operation A * B succeeds:

>>> A * B
Matrix([
[q[0, 0], 1, 2],
[q[1, 0], 2, 3],
[q[2, 0], 3, 4]])*B

However, B * A crashes:

>>> B * A
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/matrices/expressions/matexpr.py", line 21, in __sympifyit_wrapper
    return func(a, b)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/core/decorators.py", line 118, in binary_op_wrapper
    return func(self, other)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/matrices/expressions/matexpr.py", line 101, in __mul__
    return MatMul(self, other).doit()
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/matrices/expressions/matmul.py", line 112, in doit
    return canonicalize(MatMul(*args))
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/strategies/core.py", line 11, in exhaustive_rl
    new, old = rule(expr), expr
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/strategies/core.py", line 95, in switch_rl
    return rl(expr)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/strategies/core.py", line 85, in do_one_rl
    result = rl(expr)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/matrices/expressions/matmul.py", line 183, in xxinv
    if X.is_square and Y.is_square and X == Y.inverse():
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/matrices/expressions/matexpr.py", line 194, in inverse
    return self._eval_inverse()
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/matrices/dense.py", line 307, in _eval_inverse
    rv = M.inverse_GE(iszerofunc=iszerofunc)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/matrices/matrices.py", line 2621, in inverse_GE
    red = big.rref(iszerofunc=iszerofunc, simplify=True)[0]
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/matrices/matrices.py", line 2679, in rref
    r[pivot, i] = simpfunc(r[pivot, i])
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/simplify/simplify.py", line 3700, in simplify
    expr2 = shorter(together(expr, deep=True), together(expr1, deep=True))
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/simplify/simplify.py", line 3694, in shorter
    return min(choices, key=measure)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy-0.7.6_git-py2.7.egg/sympy/core/function.py", line 2290, in count_ops
    if a.is_Rational:
AttributeError: 'str' object has no attribute 'is_Rational'

The cause of the crash is related to issue with count_ops ( https://groups.google.com/forum/#!topic/sympy/Edq9jnzwqhI )

Can we make the behavior of MatrixSymbol * Matrix same as Matrix * MatrixSymbol ? (Since multiplication is supposed to be commutative)

Reply all
Reply to author
Forward
0 new messages