reducing equation to minimize variables

50 views
Skip to first unread message

Timothy Wright

unread,
May 18, 2016, 5:55:14 PM5/18/16
to sympy
Hi

I am using factor() to reduce the equation '8 + 8/(1+p)'

I get 8*(p + 3)/(p + 2) because factor() must return a product.  But I want a reduction that minimizes the number of each variable in the equation.  In this case, my input referenced p once, running factor gives me a product that has two references to p.

I need to generalize this to very long equations with 5 independent variables.  I am replacing the use of mathematica with sympy, mathematica seems to be able to reduce the equation in this way

I have looked at cancel(), simplify(), apart(), expand(), all don't do what I want.

After reviewing the email history, I don't think this can be done out of the box.  But there could be a way to write my own reducer to do this?

Is this correct?  I want to check if I am on the right path.

Thanks

Tim

Aaron Meurer

unread,
May 18, 2016, 7:15:31 PM5/18/16
to sy...@googlegroups.com
For 8 + 8/(1+p), apart() will give this form.

In general, it likely depends on the expression which function will do
this. You can use the count method to count how many times a variable
appears in an expression

In [65]: (8 + 8/(1+x)).count(x)
Out[65]: 1

In [66]: (8*(x + 3)/(x + 2)).count(x)
Out[66]: 2

You could use the measure option to simplify to tell it to minimize this:

In [67]: simplify(8 + 8/(1+x), measure=lambda i: i.count(x))
Out[67]:
8
8 + ─────
x + 1

You could also write your own function that tries various functions
and returns the result with the fewest occurrences of the variables.

Aaron Meurer
> --
> 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 sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/43f8ff85-9b85-43ad-942b-90722d86a506%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Denis Akhiyarov

unread,
May 19, 2016, 12:50:08 AM5/19/16
to sympy
Wow, this is incredible and quite low-level! I was looking for this some time ago and did not find any documentation either, but had no time to post the question.

Timothy Wright

unread,
May 19, 2016, 6:53:25 AM5/19/16
to sympy
Thanks Aaron, this is what I was looking for.

Tim
Reply all
Reply to author
Forward
0 new messages