# Piecewise continuation of a discrete function on an area

I have a discrete function (finite set of values) defined on an area $\Omega$ in $R^2$. Each value of the function is defined on a subarea $\Omega_i$(triangle in my case). These triangles account for the whole area $\Omega=\bigcup \Omega_i$. I need create a new piecewise function which would be constant on each triangle, but defined on the $\Omega$. How one can do it?

I can define a piecewise function for distinct triangles. Then I have to union all of them into the single function. So, turning an array of piecewise functions into the single piecewise function would solve my problem.

For example:

n = 4;
tr1 = Triangle[{{0, 0}, {0.25, 0}, {0.125, 0.25}}];
tr2 = Triangle[{{0.25, 0}, {0.375, 0.25}, {0.125, 0.25}}];
tr3 = Triangle[{{0.25, 0}, {0.5, 0}, {0.375, 0.25}}];
tr4 = Triangle[{{0.125, 0.25}, {0.375, 0.25}, {0.25, 0.5}}];
tr = {tr1, tr2, tr3, tr4};
val = {1, 2, 3, 4};
f[x_, y_] =
Table[Piecewise[ {{val[[i]], {x, y} \[Element]
tr[[i]]} , {0, {x, y} ! \[Element] tr[[i]]}} ], {i, 1, n}];


I need the sole function, defined on $\bigcup tr_i$, which should be the sum of that four functions defined on $tr_i$:

g[x_, y_] =
Piecewise[ { {val[], {x, y} \[Element]
tr[]}, {val[], {x, y} \[Element]
tr[]}, {val[], {x, y} \[Element]
tr[]}, {val[], {x, y} \[Element] tr[]}  }]


In my problem $n$ is much bigger. How can I wrap it into a loop? Moreover, is it an effective way to create such a function? The command Plot3D[g[x, y], {x, 0, 0.25}, {y, 0, 0.25}] for example, works for too long.

• So what is the question? How to union them? How about adding them together? Or define all the different triangles in one piecewise? – Ruud3.1415 Jan 29 '18 at 15:15
• I've updated the question. I just need define a new function as a sum of other functions. – Artem Zefirov Jan 29 '18 at 16:02
• The question is how to define them into the one piecewise function – Artem Zefirov Jan 29 '18 at 16:20
• how about defining f like this:f[x_, y_] = Piecewise[Table[{val[[i]], {x, y} \[Element] tr[[i]]}, {i, 1, n}]]; – Ruud3.1415 Jan 29 '18 at 17:08

If we define the table within the piecewise (inferring 0 unless otherwise specified) we get the following:

n = 4;
tr1 = Triangle[{{0, 0}, {0.25, 0}, {0.125, 0.25}}];
tr2 = Triangle[{{0.25, 0}, {0.375, 0.25}, {0.125, 0.25}}];
tr3 = Triangle[{{0.25, 0}, {0.5, 0}, {0.375, 0.25}}];
tr4 = Triangle[{{0.125, 0.25}, {0.375, 0.25}, {0.25, 0.5}}];
tr = {tr1, tr2, tr3, tr4};
val = {1, 2, 3, 4};
f[x_, y_] =
Piecewise[Table[{val[[i]], {x, y} \[Element] tr[[i]]}, {i, 1, n}]];
DensityPlot[f[x, y], {x, 0, 0.25}, {y, 0, 0.25}, PlotRange -> All,
PlotPoints -> 20, MaxRecursion -> 5]


to get: or to lower computing time:

DensityPlot[f[x, y], {x, 0, 0.25}, {y, 0, 0.25}, PlotRange -> All,
PlotPoints -> 2, MaxRecursion -> 1] • Thanks. That's what I need. Is there any way to speed up the plotting? Or it is a price I should pay for working with such a function? – Artem Zefirov Jan 29 '18 at 17:33
• Could you advice how to get red of that gaps among regions? ContourPlot[f[x, y], {x, y} [Element] = RegionUnion[tr1, tr2, tr3, tr4], PlotLegends -> Automatic, ColorFunction -> "TemperatureMap"] – Artem Zefirov Jan 29 '18 at 17:44
• Would 'DensityPlot' work in your case? It would be a lot faster – Ruud3.1415 Jan 29 '18 at 17:45
• I've tried it. A bit faster. Gaps are what really worry me. I think, that performance will increase when I get red of them. – Artem Zefirov Jan 29 '18 at 17:50
• If your only interested in a graph I would look into Graphics and Triangle,plotting would not be the way to go for large n – Ruud3.1415 Jan 29 '18 at 17:52