Ask Your Question

# common factor in symbolic expressions

Is there any way of extracting a common factor of all terms in a list of symbolic expressions?

For example, in the following list

[x^4*cos(x), x^3*cos(x), x^2*cos(x)*sin(x)]


such function should return x^2*cos(x). And for

 [1/x^4*cos(x), 1/x^3*cos(x), 1/x^2*cos(x)*sin(x)]


it should return 1/x^2*cos(x).

edit retag close merge delete

## 2 Answers

Sort by ยป oldest newest most voted

This can be done by calling:

gcd( [x^4*cos(x), x^3*cos(x), x^2*cos(x)*sin(x)] )

more

## Comments

My initial example wasn't that good. For

[1/x^4*cos(x), 1/x^3*cos(x), 1/x^2*cos(x)*sin(x)]


it should return 1/x^2*cos(x).

( 2021-12-04 04:41:31 +0200 )edit
1

You can use:

 gcd(map(numerator, mylist)) / gcd(map(denominator, mylist))

( 2021-12-04 13:48:45 +0200 )edit

Using the @Max Alekseyev suggestion I was able to write a simple python function that solves the problem.

def common_factor(expr_list):
numerators, denominators = zip(*[expr.numerator_denominator() for expr in expr_list])
return gcd(numerators)/gcd(denominators) 
more

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2021-12-04 01:35:10 +0200

Seen: 68 times

Last updated: Dec 04 '21