Loading [MathJax]/jax/output/HTML-CSS/jax.js
Ask Your Question
1

common factor in symbolic expressions

asked 3 years ago

cav_rt gravatar image

updated 3 years ago

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).

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
3

answered 3 years ago

Max Alekseyev gravatar image

updated 3 years ago

This can be done by calling:

gcd( [x^4*cos(x), x^3*cos(x), x^2*cos(x)*sin(x)] )
Preview: (hide)
link

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).

cav_rt gravatar imagecav_rt ( 3 years ago )
1

You can use:

 gcd(map(numerator, mylist)) / gcd(map(denominator, mylist))
Max Alekseyev gravatar imageMax Alekseyev ( 3 years ago )
1

answered 3 years ago

cav_rt gravatar image

updated 3 years ago

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)

Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 3 years ago

Seen: 395 times

Last updated: Dec 04 '21