On Tue, Nov 3, 2015 at 5:35 PM, Mateusz Paprocki <[email protected]> wrote:
> Hi,
>
> On 3 November 2015 at 21:47, Hugh <[email protected]> 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

Perhaps collect() should gain some kind of "auto" flag, which would
automatically collect things that appear in more than one term (using
some kind of greedy approach).

As for more general factorization, I'm not sure if it's valuable in
general, especially since the concept of a partial factorization is
not really well-defined (at least in terms of uniqueness). Probably
separating different variables like x**2 + 2*x + y**2 + 2*y + 2 -> (x
+ 1)**2 + (y + 1)**2 would be about the furthest you'd want to go (and
even in that example, you can see the real issue comes in how to
correctly "split" constants).

Aaron Meurer

>
>> 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].
>> 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/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/CAGBZUCarw_f-UbLkcCuOjxWPPM3_W7kWTVaVG4zPAobh6obzUA%40mail.gmail.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/CAKgW%3D6JBOF6c5%3DO%3DXEOAxwKRfWn51judh9Gn7DDEDWn%2BYwsStQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to