Hi all, I'm working on a FEM framework that takes in weak forms of PDEs and discretizes them automatically. SymPy comes in very handy in many aspects.
Related to simplification of expressions, unless I'm mistaken, it seems there is no recursive version of collect() in standard SymPy? It doesn't have the "deep" flag, unlike some of the other simplifier functions. Consider the following artificial example: a*b + a*b*c + a*b*c*d When we collect() it, we get a * (b + b*c + b*c*d) In some use cases, it would be desirable to get a * b * c * (1 + d) This would lead to a larger reduction in operation count, which is especially useful e.g. in FEM, where the atoms are later replaced by large NumPy arrays. To cover this, I wrote a function to do recursive collection, by walking the expression tree and applying the existing collect() at each level. Additionally, considering the use case, my code provides a rudimentary analyzer function, which tries to automatically create a suitable list of symbols for collect, in order to produce a reasonable reduction in operation count. This is done by default if no syms are provided, but it can also be overridden with a manually provided symbol list. The automatic list is created by sorting the atoms of expr in decreasing order of number of occurrences. Ties are broken by preferring the symbol whose topmost occurrence is nearer the top of the expression tree. (This is useful for input which is not necessarily in expanded form.) So, I'm writing to ask, are you guys interested in adding this functionality to standard SymPy? The code is available at https://yousource.it.jyu.fi/jjrandom2/freya/blobs/master/symutil.py in functions recursive_collect() analyze() count_atoms() It's ~290 lines including comments and docstrings; the actual code is something like ~110 lines, of which ~90 lines belong to the automatic construction of the symbol list. The recursive collection itself is pretty much trivial. Of course, I'm not suggesting the code for inclusion as-is, but as a basis which could be worked into something reasonable, if there is an interest. I can provide a patch, but I thought to ask for opinions first :) So, what do you guys think? Useful? Not useful? Other, please specify? ------------------------------------------------- Juha Jeronen, Ph.D. University of Jyväskylä Department of Mathematical Information Technology ------------------------------------------------- -- You received this message because you are subscribed to the Google Groups "sympy" group. To view this discussion on the web visit https://groups.google.com/d/msg/sympy/-/ZQ3nSEyfdGgJ. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
