xreplace vs subs

137 views
Skip to first unread message

Jack Kemp

unread,
Nov 17, 2015, 10:50:38 PM11/17/15
to sympy
On profiling my code I found it spent a lot of time in subs, particularly the subs call at the end of linsolve where it substitutes in your given free variables for the dummy parameters from gauss_jordan_solve. If I replace subs with xreplace I get a 10x speedup in the substitution process (250x without fastcache!).

Are there any possible pitfalls in doing this? My understanding is the advantage of subs over xreplace is that it is mathematically aware, so (x**4).subs(x**2, y) will yield y**2 for example, while the same would not work for xreplace. But if one is always replacing just a free symbol, such as replacing the dummy symbol in linsolve, surely the two are equivalent? If so, should linsolve just use xreplace?

On the subject, why does the matrix class not have an xreplace method which applies xreplace to its elements, like it has for subs?

Jason Moore

unread,
Nov 17, 2015, 11:17:26 PM11/17/15
to sy...@googlegroups.com
subs is slower and replace/xreplace should be used in places that need exact matches of sub-expressions or nodes.

There could be pitfalls if the code relies on the features of subs.

Replacing the dummy symbols in linsolve sounds like a job for xreplace, not subs.

I'm not sure why the matrix class doesn't have an xreplace method. My guess is we'd accept a pull request that adds one.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, 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/da51f89a-b686-459d-a590-37049e42335d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jack Kemp

unread,
Nov 17, 2015, 11:25:36 PM11/17/15
to sympy
OK, thank you. I'll put together a couple of PRs when I have a moment.

Jack
Reply all
Reply to author
Forward
0 new messages