# How to Speed Up Piecewise Symbolic Integration

In Mathematica, I am attempting to integrate over 2-D $$\phi$$ "hat functions" for a FEM scheme. I suppose I am only observing "interior points" in the domain $$\left( 0 , 2 \right) \times \left( 0 , 10 \right)$$. I formulated the function and tested that it does exactly what I want. I then setup the integrals, but the issue is I can by hand solve these integrals faster than the time Mathematica is taking. Below is the code I'm using (substituted greek letters for easier reading):

ClearAll["Global*"];
phi[i_, j_, h_, tau_, x_, t_] = Piecewise[{
{1 - (x - h*i)/h - (t - tau*j)/tau,
And[x >= h*i, t >= tau*j, t - tau*(j + 1) <= -(tau/h)*(x - h*i)]},
{1 - (x - h*i)/h,
And[x <= h*(i + 1), t <= tau*j, t - tau*j >= -(tau/h)*(x - h*i)]},
{1 + (t - tau*j)/tau,
And[x >= h*i, t >= tau*(j - 1), t - tau*j <= -(tau/h)*(x - h*i)]},
{1 + (x - h*i)/h + (t - tau*j)/tau,
And[x <= h*i, t <= tau*j, t - tau*j >= -(tau/h)*(x - h*(i - 1))]},
{1 + (x - h*i)/h,
And[x >= h*(i - 1), t >= tau*j,
t - tau*(j + 1) <= -(tau/h)*(x - h*(i - 1))]},
{1 - (t - tau*j)/tau,
And[x <= h*i, t <= tau*(j + 1),
t - tau*(j + 1) >= -(tau/h)*(x - h*(i - 1))]}
(*0 ELSE*)
}];
phix[i_, j_, h_, tau_, x_, t_] = D[phi[i, j, h, tau, x, t], {x, 1}];

Integrate[
Integrate[
phix[i, j, h, tau, x, t]*phix[i, j, h, tau, x, t], {x, 0, 2},
Assumptions ->
i \[Element] Integers && 1 < i < 2/h && j \[Element] Integers &&
1 < j < 10/tau && h \[Element] Reals && 0 < h &&
tau \[Element] Reals && 0 < tau], {t, 0, 10},
Assumptions ->
i \[Element] Integers && 1 < i < 2/h && j \[Element] Integers &&
1 < j < 10/tau && h \[Element] Reals && 0 < h &&
tau \[Element] Reals && 0 < tau]


For some background, $$i$$ and $$j$$ are "interior indices" and so for some $$m$$ and $$n$$ we have $$h=\frac{2}{m}$$ and $$\tau = \frac{10}{n}$$ in which I require $$1 and $$1. $$h$$ and $$\tau$$ are positive reals that go down to $$0$$.

If I require an abstract answer, one in terms of symbolic $$h$$ and $$\tau$$, how can I speed up the integral's evaluation without having to separate the integral into individual regions?

Right as I clicked "Review Question", I found a trick that sped things up significantly, and will leave as an answer. Further optimization is still requested.

By realizing $$\phi _{i , j} \times \phi _{i , j}$$ is only non-zero within an "almost square" domain locally in terms of $$i$$ and $$j$$ (including derivatives), we can shrink the domain of integration - using Mathematica code {x, (i-1)*h, (i+1)*h} and {t, (j-1)*tau, (j+1)*tau} as the new bounds are equivalent to integrating over the entire domain. I can only assume this somehow significantly reduces operations. This yields the much faster code:

ClearAll["Global*"];
phi[i_, j_, h_, tau_, x_, t_] = Piecewise[{
{1 - (x - h*i)/h - (t - tau*j)/tau,
And[x >= h*i, t >= tau*j, t - tau*(j + 1) <= -(tau/h)*(x - h*i)]},
{1 - (x - h*i)/h,
And[x <= h*(i + 1), t <= tau*j, t - tau*j >= -(tau/h)*(x - h*i)]},
{1 + (t - tau*j)/tau,
And[x >= h*i, t >= tau*(j - 1), t - tau*j <= -(tau/h)*(x - h*i)]},
{1 + (x - h*i)/h + (t - tau*j)/tau,
And[x <= h*i, t <= tau*j, t - tau*j >= -(tau/h)*(x - h*(i - 1))]},
{1 + (x - h*i)/h,
And[x >= h*(i - 1), t >= tau*j,
t - tau*(j + 1) <= -(tau/h)*(x - h*(i - 1))]},
{1 - (t - tau*j)/tau,
And[x <= h*i, t <= tau*(j + 1),
t - tau*(j + 1) >= -(tau/h)*(x - h*(i - 1))]}
(*0 ELSE*)
}];
phix[i_, j_, h_, tau_, x_, t_] = D[phi[i, j, h, tau, x, t], {x, 1}];

Integrate[
Integrate[
phix[i, j, h, tau, x, t]*
phix[i, j, h, tau, x, t], {x, (i - 1)*h, (i + 1)*h},
Assumptions ->
i \[Element] Integers && 1 < i < 2/h && j \[Element] Integers &&
1 < j < 10/tau && h \[Element] Reals && 0 < h &&
tau \[Element] Reals && 0 < tau], {t, (j - 1)*tau, (j + 1)*tau},
Assumptions ->
i \[Element] Integers && 1 < i < 2/h && j \[Element] Integers &&
1 < j < 10/tau && h \[Element] Reals && 0 < h &&
tau \[Element] Reals && 0 < tau]


This new code requires the assumptions to prove the bounds of integration are real.

Expanding the code to all the other "interpolant coefficients", or integrating $$\phi _{i , j} \times \phi _{\alpha , \beta}$$ for $$\left( i , j \right) \neq \left( \alpha , \beta \right)$$, the same bounds can be used by the support of these $$\phi$$ functions, and so only a modification to the integrand is required.