multiply numerator and denominator of rational expression

83 views
Skip to first unread message

Scott Calabrese Barton

unread,
Nov 10, 2016, 2:38:59 PM11/10/16
to sympy
Hi, I wonder if there is an easy way to multiply the numerator and denominator of a rational expression by the same factor. I'd like to do this to group terms so that I can make a substitution.

I tried making a function to do this:

def tb(exp,factor):
    if exp.func==Mul and exp.args[-1].func==Pow \
        and exp.args[-1].args[-1]==-1:
        e=exp*factor
        denom=e.args[-1].args[0] * factor
        e.args[-1].args[0] = denom
        
    return e



Unfortunately it doesn't work because of immutable tuples.


Aaron Meurer

unread,
Nov 10, 2016, 3:54:30 PM11/10/16
to sy...@googlegroups.com
SymPy expressions are immutable. You have to create a new expression.
I would recommend something like

def mul_and_div_factor(expr, factor):
n, d = expr.as_numer_denom()
return (n*factor)/(d*factor)

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/773995c5-ed93-42cd-a480-2e59bb9f4974%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Scott Calabrese Barton

unread,
Nov 17, 2016, 8:17:33 AM11/17/16
to sympy
Aaron, thanks, that's definitely the right idea, but doesn't work, probably because of simplification. Here's an example:



What I really want is a k3/k1 term in the denominator that can be substituted. Any way to achieve this?

Scott Calabrese Barton

unread,
Nov 17, 2016, 8:23:02 AM11/17/16
to sympy
Unfortunately that image will not render.  Here's a link to it:  https://goo.gl/J1wn0P

Aaron Meurer

unread,
Nov 17, 2016, 2:30:50 PM11/17/16
to sy...@googlegroups.com
Ah, I forgot that if factor is not a number, it won't distribute
automatically. You'll want to do something like

n, d = expr.as_numer_denom()
expand(n*factor)/expand(d*factor)

The way it is now, factor just immediately cancels.

And by the way, if your goal is to write a rational function in
reduced terms, you can use cancel() or factor() (the latter will also
factor the numerator and denominator).

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/2d2bbab6-ed5c-4a9a-957f-51986b4f40e8%40googlegroups.com.

Scott Calabrese Barton

unread,
Nov 17, 2016, 8:27:08 PM11/17/16
to sympy
Thanks, that worked perfectly. My final function is:

def ndf(expr, factor): 
    n, d = expr.as_numer_denom() 
    return expand(n*factor)/expand(d*factor)
Reply all
Reply to author
Forward
0 new messages