Testing equivalence of mathematical expressions in Python

I have got two strings in Python,

A m * B s / (A m + C m)

and

C m * B s / (C m + A m)

that are both equivalent functions of the unordered set (A, C) and the unordered set (B). m and s indicate units that can be swapped among the same but not with another unit.

So far, I'm doing permutations of A, B, and C and testing them using eval and SymPy's == operator. This has multiple drawbacks:

  • for more complicated expressions, I have to generate a large number of permutations (in my case 8 nested for loops)
  • I need to define A, B, C as symbols, which is not optimal when I don't know which parameters I will have (so I have to generate all of them -> terribly inefficient and messing up my variable namespace)

Is there a pythonian way to test for this kind of equivalence? It should work an arbitrary expressions.

8
задан D Adams 26 October 2015 в 23:06
поделиться