Edit: My original question implicated D
in the problem, but it seems unrelated, so I've removed that part of the question.
I am writing a function that uses functions involving subscripts and an open-ended Sum
. It's unreasonably slow. Here's a minimal example (the real situation is much worse):
Sum[
-2 Exp[-(xx - Subscript[x, k])^2/(V + Subscript[Vx, k]^2)] Subscript[n, k] (xx - Subscript[x, k])/(V + Subscript[Vx, k]^2)
, {k, nsp}] // AbsoluteTiming
Sum[
-2 Exp[-(xx - subscript[x, k])^2/(V + subscript[Vx, k]^2)] subscript[n, k] (xx - subscript[x, k])/(V + subscript[Vx, k]^2)
, {k, nsp}] // AbsoluteTiming
Well, good thing I'm not using Superscript
, because that's 10X worse:
Sum[
-2 Exp[-(xx - Superscript[x, k])^2/(V + Superscript[Vx, k]^2)] Superscript[n, k] (xx - Superscript[x, k])/(V + Superscript[Vx, k]^2)
, {k, nsp}] // AbsoluteTiming
So, is there some way to tell Sum
not to bother trying anything fancy? And what's with the 100-fold range of timing of otherwise identical code?
N.B.: these timings change on rerunning the same code due to some kind of caching
x_[j] -> Subscript[x, j]]
. $\endgroup$Format[x[j_]] := Subscript[x, j]
and similar, then the indexed variables will display as subscripts in the output. $\endgroup$Block
to temporarily redefineSum
andSubscript
with your own versions. These need only implement minimal functionality $\endgroup$D[Inactive[Sum][...], ..]
? $\endgroup$