Status: Accepted
Owner: ----
Labels: Type-Defect Priority-Medium
New issue 3121 by [email protected]: warning MatMul is a pseudo-Mul
http://code.google.com/p/sympy/issues/detail?id=3121
Not sure what to call it exactly, but whatever has been done to define
special behaviors for Matrix elements is going to cause some problems. Here
is one symptom:
>>> from sympy import MatrixSymbol, symbols
>>> n, m, l = symbols('n m l')
>>> A = MatrixSymbol('A', n, m)
>>> B = MatrixSymbol('B', m, l)
>>> C = MatrixSymbol('C', n, l)
>>> (2*A*B).as_independent(B)
(1, 2*A*B)
That should have been `(2*A, B)` but the as_independent routine does checks
like `if self.func is Add`; although self.is_Add is True (because that
attribute is defined for MatMul) the func is not a Mul so the routine's
path for Muls is not followed.
>>> type(2*A*B)
<class 'sympy.matrices.expressions.matmul.MatMul'>
>>> (2*A*B).is_Mul
True
>>> (2*A*B).func is Mul
False
>>>
So here is a case where we have a non-trivial difference between
isinstance(foo, Mul) and foo.is_Mul. Any code for expr through which this
pseudo-Mul passes will fail any test that uses the func attribute instead
of the is_Mul attribute. I'm not sure what the best thing to do, here, is.
The first thought is that perhaps Mul.flatten and Add.flatten could sift
terms and send them off to _eval_flatten routines:
d = sift(sequence, lambda x: x.func)
new_sequence = []
for f, terms in d.items():
new_sequence.append(f._eval_flatten(terms))
make_it_canonical(new_sequence)
return new_sequence
That way, for example, a collection of I's would be passed off for
processing to ImaginaryUnit and the gauntlet of little tests that it would
have had to go through in the flatten routine would be avoided (or at least
handled behind the scenes by Python's machinery for finding the right
_eval_flatten routine).
/c
--
You received this message because you are subscribed to the Google Groups
"sympy-issues" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy-issues?hl=en.