Home > OS >  How to factorize fraction by a given symbol?
How to factorize fraction by a given symbol?

Time:12-09

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)

CodePudding user response:

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

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

CodePudding user response:

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)
  • Related