How to do algebra on summations of variable expressions?
As far as I know, there is no easy, general way to handle this kind of algebra with Sum
expressions.
What follows is an attempt to use replacement rules to handle a wider range of cases than chris's example. I don't consider it to be the canonical answer that is required, but perhaps someone might be able to use it as a starting point.
I use Inactive
on Sum
to stop Mathematica from attempting to evaluate the sums at every stage. I've also used Indexed
in place of Part
. So getting started:
sum = Inactive[Sum];
SetOptions[Integrate, GenerateConditions -> False];
gau[x_, m_, var_] := (E^(-(x - m)^2/(2 var)))/Sqrt[2 Pi*var]
f[x_] := sum[gau[x, Indexed[m, i], Indexed[v, i]], {i, 1, n}]
ga[x_] := sum[Indexed[a, m]*Sin[m*x], {m, 0, Infinity}]
gb[x_] := sum[Indexed[b, m]*Sin[m*x], {m, 0, Infinity}]
The first thing to note is that by using Indexed
we avoid getting those part specification errors. Also it displays more nicely:
Activate@f[x]
Now some rules...
reindex = s : sum[_, {i_, __}] :> (s /. i -> Unique@i);
unpower = s_sum^p_Integer :> Times @@ Table[s /. reindex, {p}];
expand = sum[e1_, i1__] sum[e2_, i2__] :> sum[e1 e2, i1, i2];
distribute = Integrate[sum[e_, s__], i__] :> sum[Integrate[e, i], s];
niceindex[expr_] := expr /. (Thread[# -> Take[{i, j, k, l}, Length[#]]] &@
DeleteDuplicates[Cases[expr, s_Symbol /; SymbolName[s]~StringMatchQ~"*$*" :> s, -1]]);
doitall[expr_] := expr /. unpower /. reindex //. expand /. distribute // niceindex;
Description of the rules:
reindex
simply replaces the summation index by a unique symbol. This will allow us to expand powers and products of sums without getting colliding symbols.unpower
expands integer powers of sums into explicit products.expand
expands a product of sums into a sum of products.distribute
distributesIntegrate
overSum
, i.e. it converts the integral of a sum into a sum of integrals.niceindex
replaces the unique symbols generated byreindex
with plain i,j,k,l. As written this assumes that there are no more than four summation indices required in the final result, and that the symbols i,j,k,l are not in use. These are both dangerous assumptions! Ideally you should not use this function (or write a better version), but personally I find summation indices likei$123456
hard to read.doitall
applies all the rules in an attempt to transform the expression.
So here are a couple of examples. For this one we only need distribute
:
Integrate[f[x], {x, -∞, ∞}] /. distribute
Having done the integration we can now Activate
the sum to get the desired result (I've also applied the assumption that $v_i$ is positive)
Simplify[%, Indexed[v, i] > 0] // Activate
n
For the next two I've used doitall
to apply the full set of transformations:
Integrate[f[x]^2, {x, -∞, ∞}] // doitall
Integrate[ga[x] gb[x], {x, 0, 2 Pi}] // doitall
Yes we can !
MapAt[Integrate[#,{x,-Infinity,Infinity}]&,f[x],1]//PowerExpand
(* n *)
tt = f[x]^2 /. Power[Sum[a__, b__], 2] :>
sum[a (a /. i -> j) // Release, b, b /. {i -> j}]
MapAt[Integrate[#, {x, -Infinity, Infinity}] &, tt, 1] /.
sum -> Sum // PowerExpand