Usually the doit() method applies such rules. Aaron Meurer
On Nov 22, 2013, at 9:07 AM, Matthew Rocklin <[email protected]> wrote: When used with explicitly defined Matrix objects, MatrixExpressions were intended to be lazy by default. The idea here is that if you wanted immediate evaluation you would have used standard arithmetic operators which would have pointed you to Matrix.__add__ etc. and forced immediate evaluation. Obviously there are cases where this assumption breaks down. It's also not clear that it's the right decision. The old issues on matrix exprs have discussion on this topic. The standard solution is to call Matrix on the result as you mention. I don't understand your final comment though about this not being a general solution. By my reckoning Matrix Expressions are known to be matrices. If you wanted to fix it then yes, you could add a new rule something like the following: def collapse_explicit_matrices(matadd): explicit = [arg for arg in matadd.args if isinstance(arg, MatrixBase)] if not explicit: return matadd exprs = [arg for arg in matadd.args if not isinstance(arg, MatrixBase)] return MatAdd(sum(explicit), *exprs) and then add this to the list `rules`. You might want to do something similar in MatMul if we decide this is the correct path to take. On Thu, Nov 21, 2013 at 11:36 PM, Mark Dewing <[email protected]> wrote: > I would like to write an expression using Matrix symbols, and then > evaluate it by substituting concrete Matrix values. However, the > resulting Matrix expression will not simplify. > > # set up general expression > n = Symbol('n') > r1 = MatrixSymbol('r1',n,1) > r2 = MatrixSymbol('r2',n,1) > d = r2 - r1 > > # concrete vectors to substitute > c1 = Matrix([1.1, 2.3, 4.4]) > c2 = Matrix([0.1, 2.1, 4.3]) > > # evaluate > d = d.subs(n,3) > r1 = r1.subs(n,3) > r2= r2.subs(n,3) > > d2 = d.subs({r1:c1, r2:c2}) > print d2.doit() > > The result is a matrix add: > > Matrix([ > [-1.1], > [-2.3], > [-4.4]]) + Matrix([ > [0.1], > [2.1], > [4.3]]) > > > I suspect the 'canonicalize' rules in matadd.py need to be enhanced to fix > this? > > > (If the final result is put in a Matrix it gives the expected result: > "print Matrix(d2)". This is not a general solution - it only works if the > expression is known to be a Matrix.) > > > > > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sympy. > For more options, visit https://groups.google.com/groups/opt_out. > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sympy. For more options, visit https://groups.google.com/groups/opt_out. -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sympy. For more options, visit https://groups.google.com/groups/opt_out.
