Re: [sympy] substitute fractions in denominator

104 views
Skip to first unread message

Chris Smith

unread,
Jul 4, 2012, 9:28:05 AM7/4/12
to sy...@googlegroups.com
Thanks for the error report. This can be filed as an issue on our
issue tracker: http://code.google.com/p/sympy/issues

A workaround is to use fraction() to break up the original fraction
since this is not recursive like as_numer_denom. fraction will give
the literal numerator and denominator of an expression; numer and
denom are methods that can also target a given part of the fraction:

>>> fraction(f)
(x + y, a/b + y)
>>> numer(f).subs(a/b,z)/denom(f).subs(a/b,z)
(x + y)/(y + z)

/Chris

matthjes

unread,
Jul 4, 2012, 9:28:28 AM7/4/12
to sy...@googlegroups.com
Ok, I've found the answer myself:

One solution is to use a helper function, something like this:

from sympy.simplify import fraction
def symsubst(Y, expr, subs):
    num_denom = fraction(Y)
    return num_denom[0].subs(exp, subs)/num_denom[1].subs(expr, subs)
   

Am Mittwoch, 4. Juli 2012 14:33:20 UTC+2 schrieb matthjes:
I've run into the following problem:

I have some function, let's say, e.g.

f = (x + y)/(y + a/b),

and I'd like to substitute "a/b" with "k". I would use "f.subs(a/b, k)", however, this does not work as f seems to be internally represented as:

f = b(x + y)/(by + a),

so sympy seems not to be able to find "a/b". You can check this by looking at the nominator and denominator of the original f using as_numer_denom(), its (b(x + y), by + a). Is there a way to "force" the first / original representation, i.e., with the fraction in the denominator intact?

krastano...@gmail.com

unread,
Jul 4, 2012, 9:31:01 AM7/4/12
to sy...@googlegroups.com
> and I'd like to substitute "a/b" with "k". I would use "f.subs(a/b, k)",

I would use `f.subs(a, k*b)`. It seems more consistent with the idea
of substituting a *variable* with something else.

matthjes

unread,
Jul 4, 2012, 9:36:33 AM7/4/12
to sy...@googlegroups.com

That's also possible, however, the docs say that "subs" should substitute an expression, not just a variable, thats why I've tried the substitution above.

But it's working now using "fraction", thanks for your help!

Aaron Meurer

unread,
Jul 4, 2012, 1:45:44 PM7/4/12
to sy...@googlegroups.com
On Jul 4, 2012, at 7:36 AM, matthjes <matt...@gmail.com> wrote:

Am Mittwoch, 4. Juli 2012 15:31:01 UTC+2 schrieb Stefan Krastanov:
> and I'd like to substitute "a/b" with "k". I would use "f.subs(a/b, k)",

I would use `f.subs(a, k*b)`. It seems more consistent with the idea
of substituting a *variable* with something else.

That's also possible, however, the docs say that "subs" should substitute an expression, not just a variable, thats why I've tried the substitution above.

It does, and it does it's best to use heuristics to find that expression, but it can only go so far, and if the expression itself is not there, it can fail. Substituting a Symbol will always work.

Aaron Meurer


But it's working now using "fraction", thanks for your help!

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sympy/-/K2bSo2GW1LMJ.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
Reply all
Reply to author
Forward
0 new messages