Thanks Mateusz for sharing the code. I thought it was a clever solution to solve my problem.
On Wednesday, 4 November 2015 07:35:45 UTC+8, Mateusz Paprocki wrote: > > Hi, > > On 3 November 2015 at 21:47, Hugh <[email protected] <javascript:>> wrote: > > import sympy > > sympy.init_session() > > > > > > x11, x12, x13, x21, x22, x23, x31, x32, x33 = symbols('x_1:4(1:4)') > > > > A = Matrix(3,3,symbols('x_1:4(1:4)')) > > expr1 = A.det() > > > > expr2 = x11*(x22*x33 - x23*x32) - x12*(x21*x33 -x23*x31) + x13*(x21*x32 > - > > x22*x31) > > > > # How to get expr2 from expr1? > > In [1]: from sympy import * > > In [2]: var('x_1:4(1:4)') > Out[2]: (x_11, x_12, x_13, x_21, x_22, x_23, x_31, x_32, x_33) > > In [3]: A = Matrix(3, 3, _) > > In [4]: expr1 = A.det() > > In [5]: expr1 > Out[5]: x_11*x_22*x_33 - x_11*x_23*x_32 - x_12*x_21*x_33 + > x_12*x_23*x_31 + x_13*x_21*x_32 - x_13*x_22*x_31 > > In [6]: from sympy.utilities.iterables import multiset_partitions > > In [7]: min([ sum([ factor(sum(f)) for f in l ]) for l in > multiset_partitions(expr1.args) ], key=lambda e: e.count_ops()) > Out[7]: x_11*(x_22*x_33 - x_23*x_32) - x_12*(x_21*x_33 - x_23*x_31) + > x_13*(x_21*x_32 - x_22*x_31) > > > I would like to use sympy to rewrite expressions just like how people > would > > commonly do when writing proofs or doing homework. What are the > > documentation that I must read so that I can be proficient at this? > > > > I've read the tutorial and some of the modules in the module reference > but > > feel that I have just barely touched the surface of sympy's > capabilities. > > For example, in the above code snippet, I don't know how to manipulate > expr1 > > to get expr2. I thought expr1.factor() would work but it didn't. > > At this point SymPy doesn't have any built-in function that would do > out of the box what's requested here. factor() can't help (at least > directly), because it gives a complete factorization (it works over > entire expression). What you are looking for would be called, e.g., > factorsum() (as in Maxima). Such functionality can be implemented in > SymPy as shown in _7, just it's very inefficient due to a large number > of partitions. > > Mateusz > > > Please advise. > > > > -- > > 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] <javascript:>. > > To post to this group, send email to [email protected] > <javascript:>. > > Visit this group at http://groups.google.com/group/sympy. > > To view this discussion on the web visit > > > https://groups.google.com/d/msgid/sympy/9cbb1995-3987-44f1-8c72-6de11e6a4013%40googlegroups.com. > > > > For more options, visit https://groups.google.com/d/optout. > -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/82ed152a-f27e-4d38-84b2-d62780d9ddb0%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
