Ask Your Question
1

common factor in symbolic expressions

asked 2021-12-04 01:35:10 +0100

cav_rt gravatar image

updated 2021-12-04 05:52:11 +0100

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2021-12-04 02:33:12 +0100

Max Alekseyev gravatar image

updated 2021-12-04 02:33:33 +0100

This can be done by calling:

gcd( [x^4*cos(x), x^3*cos(x), x^2*cos(x)*sin(x)] )
edit flag offensive delete link 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).

cav_rt gravatar imagecav_rt ( 2021-12-04 04:41:31 +0100 )edit
1

You can use:

 gcd(map(numerator, mylist)) / gcd(map(denominator, mylist))
Max Alekseyev gravatar imageMax Alekseyev ( 2021-12-04 13:48:45 +0100 )edit
1

answered 2021-12-04 06:10:04 +0100

cav_rt gravatar image

updated 2021-12-04 07:47:21 +0100

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)

edit flag offensive delete link more

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: 2021-12-04 01:35:10 +0100

Seen: 324 times

Last updated: Dec 04 '21