tmaric
tmaric

Reputation: 5477

Reformulate a fractional expression in sympy using a ratio of symbols

from sympy import *
a,b,c = symbols("a b c")
f = (a*b + c) / (a*b)

How to express f as a function of the ratio "b/c"?

This is what I want to achieve

f = (a*(b/c) + 1) / (a*(b/c))

I tried factor(f, 1/c), factor(f, c) in the attempt to divide with c later, dividing by c, but it doesn't work.

Upvotes: 1

Views: 142

Answers (1)

smichr
smichr

Reputation: 19077

I generally use symbol-trickery to get an expression to look like this since you are asking for the expression to be in a very non-standard form for SymPy:

>>> factor(f.subs(b,u*c)).subs(u, Symbol('b/c'))
(a*b/c + 1)/(a*b/c)

If you don't create a 'b/c' symbol (i.e. subs(u, b/c)), SymPy will give you this:

c*(a*b/c + 1)/(a*b)

Upvotes: 3

Related Questions