How to factorize fraction by a given symbol?

I have the following fraction:

import sympy as sp
a = sp.Symbol("a")
b = sp.Symbol("b")

a/(a+b)

And would like to print it as 1/(1+b/a)

I saw sympy had a factor function but I couldn't obtain the expected behaviour.

I thought I could maybe do something like:

sp.factor((a/(a+b)), a)

Upvotes: 0

Views: 169

Answers (2)

smichr
smichr

Reputation: 19077

I would call this "distributing the numerator in the denominator":

>>> a/(a + b)
>>> 1/expand(1/_)
1/(1 + b/a)

Upvotes: 1

Pegasus
Pegasus

Reputation: 21

You can use expand and collect

import sympy as sp


a = sp.Symbol("a")
b = sp.Symbol("b")


expanded = sp.expand(a/(a+b))

collected = sp.collect(expanded, a)

print(collected)

Upvotes: 0

Related Questions