Skip to main content

symbolic - How to do algebra on summations of variable expressions?


Is there any easy, general way to handle algebra on variable Sums? For examples, Gaussian Mixtures or Fourier Series:


gau[x_, m_, var_] := (E^(-(x - m)^2/(2 var)))/Sqrt[2 Pi*var];
f[x_] := Sum[gau[x, m[[i]], v[[i]]], {i, 1, n}];
ga[x_] := Sum[a[[m]]*Sin[m*x], {m, 0, Infinity}] // Inactive;
gb[x_] := Sum[b[[m]]*Sin[m*x], {m, 0, Infinity}] // Inactive;

f[x]=ni=1e(xm[[i]])22v[[i]]2πv[[i]]


It's easy on paper to do things like integrate this expression:


(ni=1e(xm[[i]])22v[[i]]2πv[[i]])dx=ni=11=n



...or to integrate f[x]^2 analytically:


(ni=1e(xm[[i]])22v[[i]]2πv[[i]])2dx=12πni=11v[[i]]+ni=2(i1j=1e(m[[i]]m[[j]])22(v[[i]]+v[[j]])2πv[[i]]+v[[j]])


(Sorry, no code for these results; I did them by hand and wrote them in TeX)


In an Answer below, @chris posted code to get MMa to handle just these two specific instances.


MapAt[Integrate[#, {x, -Infinity, Infinity}] &, f[x],    1] // PowerExpand

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


But we would have to write new pattern/substitution code if the index was other than "i" or if we wanted to Integrate any higher powers of f or to do other operations on the Sum.


Other examples that would require new code:


exp1 = Integrate[ga[x]*gb[x],{x,0,2Pi}];

equals (derived by hand because MMa pooped out)


Pi*Sum[a[[m]]*b[[m]], {m, 0, Infinity}]

exp2 = Integrate[ga[x]^3,{x,0,2Pi}];

equals (also derived by hand)



(Pi/2)*Sum[Sum[a[[m]]*a[[n]]*(a[[m + n]] + a[[m - n]]), {n, 1, m - 1}], {m, 2,Infinity}]

Is there any way to handle algebraic Sums so that we don't have to anticipate and code around every conceivable math function we might do on them?


(Sorry for posting TeXForm, but no one understood what I was asking for the first time I tried to post this question.)



Answer



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]

enter image description here



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 distributes Integrate over Sum, i.e. it converts the integral of a sum into a sum of integrals.




  • niceindex replaces the unique symbols generated by reindex 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 like i$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

enter image description here


Having done the integration we can now Activate the sum to get the desired result (I've also applied the assumption that vi 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

enter image description here


Integrate[ga[x] gb[x], {x, 0, 2 Pi}] // doitall


enter image description here


Comments

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]