# Strange behavior when integrating vector valued piecewise function

The following straightforward integral should clearly equal $$\{0,0\}$$ for any $$R\in\mathbb R$$:

Integrate[Piecewise[{{{1,1},x < 1}},{0,0}]Piecewise[{{x,1<x<R}},0],{x,0,2}]


Strangely, Mathematica outputs a piecewise expression $$\begin{cases}\{0,0\},&R\leq1\\\{\{0,0\},\{0,0\}\},&\text{true}.\end{cases}$$ Somehow it gets the wrong type of object when $$R>1$$.

This error persists if the assumption $$R\in\mathbb R$$ is added, or if you fiddle with the other integration bounds, or if either one of the Piecewise functions is replaced by an equivalent If. Funnily, if both are replaced by If then it works. However this minimal example was obtained from a more complicated expression where even with two Ifs it breaks.

If anyone knows the cause of this bug I would be interested. I suppose integrating lists is not such a natural thing for Mathematica...

• If you do 2 separate integrations, one for R<=1 and one for R>1 you get the following !Mathematica graphics on V 13.3.1 Jan 8 at 21:04

I think that the problem is that the assumption 0*x==0 is baked into Mathematica at quite a low level. Consequently, I think you have expressions that are sometimes evaluated as a vector and sometimes a scalar.

If you ensure that your expressions always evaluate as two element vectors, you may avoid the problem. For example

Integrate[
Piecewise[{{{1, 1}, x < 1}}, {0, 0}]
Piecewise[{{{x, x}, 1 < x < R}}, {0, 0}], {x, 0, 2}]
(* {0, 0} *)


As an example of the odd things that can happen when summing things that might be scalars or vectors, I offer the following:

((0 + {0, 0}) + c) /. c -> {0, 0}
(* {{0, 0}, {0, 0}} *)


Try

Integrate[Piecewise[{{{1, 1}, x < 1}}, {0, 0}] x Boole[1 < x < R] , {x, 0,2}]
(* {0,0} *)