As a very minimal example, consider a sum of fractions such as:
$A=\frac{a}{s_{123}bc}+\frac{d}{e}$
In practice, I have many hundreds of thousands of these fractions generated from a recursion relation. My desired result comes around from the prescription in my theory, where I am required to multiply through by $s_{123}$ and then take $s_{123}\to 0$. Upon replicating in MMA, I use a code similar to:
A*s[1,2,3]//Expand
%/.{s[1,2,3]->0}
This Expand
is necessary since by simply multiplying A
by the relevant factor, MMA will first interpret it as $s_{123} \big (\frac{a}{s_{123}bc}+\frac{d}{e}\big)$. I think this is what is slowing my code down substantially, and crashes MMA due to a lack of memory.
To get around this, I have had a few ideas;
- Use something like
DeleteCase
as a simple method (or something perhaps similar to Filter out all terms not involving a given variable which might be a bit overkill for my needs) to delete all terms not involving $s_{123}$, and then set the remaining factors of it in the leftover terms to 1. - Instead of using
Expand
, define a new function which brings common factors inside to each fraction.
Which of these, alongside any other methods, would you suggest? How could I go about implementing them? I think 2 would be easier to do, but at this point, I am needing efficiency in my code on such a large scale.
List
of terms is better (e.g. effectivelyList@@A
) than working with a sum of terms. PresumablyExpand
is comparing a large number of terms, looking for common subexpressions. $\endgroup$Cancel[]
would work better here thanExpand[]
. $\endgroup$Cancel
actually crashes my code in this case. It simply runs out of memory on my laptop, but nonetheless;Expand
was my first thought, and did the trick at the time when efficiency wasn't as important. $\endgroup$