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?