# Integration of a piecewise function

Define

f[x_, i_] := Piecewise[{{1/n,
0 < x < (i - 1)/n}, {(i/n - x), (i - 1)/n < x < i/n} , {0,
i/n < x}} ];


I would like to compute

Integrate[f[x,i]*f[y,i]*f[x,j]*f[y,j],{x,0,1},{y,0,1}],


assuming that $n$, $i$ and $j$ are natural numbers and $1\leq j\leq i\leq n$. So far, I have try to use PiecewiseExpand[f[x,i]*f[y,i]*f[x,j]*f[y,j]] and split the integral in different ways. However, it takes forever the computation of the double integral. Do you have any advice to proceed?

• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– user9660
Commented Nov 2, 2015 at 16:43
• Your problem is that you define a function of just $x$ and $i$ that somehow depends upon $n$. Commented Nov 2, 2015 at 18:49
• @DavidG.Stork shouldn't it just treat the n as constant and go about the integration as in Integrate[Piecewise[{{ x, 0 <= x < a}, {a, x >= a}}],x]? Commented Nov 2, 2015 at 19:21
• Which I now notice returns an incorrect result. Commented Nov 2, 2015 at 19:22
• @DavidG.Stork I tried defining instead f[x_, i_,n_] := Piecewise[{{1/n, 0 < x < (i - 1)/n}, {(i/n - x), (i - 1)/n < x < i/n} , {0, i/n < x}} ]; but it did not shown any improvement. Commented Nov 3, 2015 at 16:17

res[j_, j_, n_] := (3 j - 2)^2 /(9 n^6)  /; j <= n
res[j_, i_, n_] := (2 j - 1)^2 /(4 n^6)  /; j <= i <= n


rr[j1_, i1_, n1_] := Block[{j = j1, i = i1, n = n1},
Integrate[f[x, i]*f[y, i]*f[x, j]*f[y, j], {x, 0, 1}, {y, 0, 1}]]

list = Sort /@ RandomInteger[{1, 10^2}, {100, 3}];
rr @@@ list == res @@@ list
(* True *)


But please don't ask me how I came to know it :)

ArrayPlot[ ] for n == 4

• Thank you! This actually solves the question as I formulated it. The true is that I also would like to compute Integrate[f[w,i]*f[x,i]*f[x,j]*f[y,j]*f[y,k]*f[w,k],{w,0,1},{x,0,1},{y,0,1}], and so on. But I am afraid I should do some math on paper and latter ask mathematica. Commented Nov 3, 2015 at 16:25