Faster alternate to expr.as_coeff_exponent()

32 views
Skip to first unread message

Nathan Goldbaum

unread,
Oct 4, 2015, 6:32:42 PM10/4/15
to sympy
Hi all,

During a performance pass on some code that makes heavy use of sympy symbol objects, I noticed expr.as_coeff_exponent() showing up in my profiles. I'm using this function to multiplicatively factor a Symbol object representing physical dimensions. Given a symbol x/y, I'm looking for a way to compute that this is equivalent to x**1 * y**-1

Here x and y are members of a "base" set of Symbols I know all other symbols must be composed through various multiplicative factors.

I'm curious if there is a more performant way to implement this function, given that I know I'm passing it instances of `Mul`.

Thanks for your help!

-Nathan

Nathan Goldbaum

unread,
Oct 4, 2015, 10:02:30 PM10/4/15
to sy...@googlegroups.com
I'm not sure this is necessarily the best way to do it, but I found that combining as_ordered_factors() and as_base_exp() seems to do the trick.

If anyone is curious, here is a pull request that implements this optimization (among other things):


--
You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/hKtATeALRYU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/f85cb699-747f-41f4-a386-64a39fde2d0b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages