# Splitting Piecewise with vectors to components

I have a piecewise function defined with vectors, e.g.

ab = Piecewise[{
{{a1, a2, a3}, 0 < r < 1},
{{b1, b2, b3}, 1 < r < 2}
}, {c1, c2, c3}]


The result of which is

{a1,a2,a3}  0<r<1
{b1,b2,b3}  1<r<2
{c1,c2,c3}  True


I'm looking for a way to address only one element of the vectors - e.g. I would like to call "ab[[1,;;]]" and get

a1  0<r<1
b1  1<r<2
c1  True


Or "ab[[2,;;]]" and get

a2  0<r<1
b2  1<r<2
c2  True


Could someone help me with the syntax?

Thanks!

ab = Piecewise[{{{a1, a2, a3}, 0 < r < 1}, {{b1, b2, b3},
1 < r < 2}}, {c1, c2, c3}];

abPart[n_Integer?Positive] := (ab /. {x_, y_, z_} :> {x, y, z}[[n]])

abPart[1]


You can create a list of piecewise expressions from ab:

splitPW = Piecewise @@@ Thread[{Thread[Thread /@ #[[1]]], #[[2]]}]&;

TeXForm /@ splitPW[ab]


${\begin{cases} \text{a1} & 0<r<1 \\ \text{b1} & 1<r<2 \\ \text{c1} & \text{True} \end{cases},\begin{cases} \text{a2} & 0<r<1 \\ \text{b2} & 1<r<2 \\ \text{c2} & \text{True} \end{cases},\begin{cases} \text{a3} & 0<r<1 \\ \text{b3} & 1<r<2 \\ \text{c3} & \text{True} \end{cases}}$

and use Part to get the desired part:

splitPW[ab][[2]] // TeXForm


$\begin{cases} \text{a2} & 0<r<1 \\ \text{b2} & 1<r<2 \\ \text{c2} & \text{True} \end{cases}$

Alternatively,

pwPart = Module[{fpw = InternalFromPiecewise[#], n = #2},
InternalToPiecewise @@ {fpw[[1]], fpw[[2, All, n]]}] &;

pwPart[ab, 1]  // TeXForm


$\begin{cases} \text{a1} & 0<r<1 \\ \text{b1} & 1<r<2 \\ \text{c1} & \text{True} \end{cases}$

Note:

InternalFromPiecewise[ab]


{{0 < r < 1, 1 < r < 2, True},
{{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}}}

InternalFromPiecewise[ab][[2]]


{{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}}

Looking at the FullForm of ab we see

Piecewise[
List[
List[List[a1,a2,a3],Less[0,r,1]],List[List[b1,b2,b3],Less[1,r,2]]
],
List[c1,c2,c3]
]


where the indentation is mine. We see that the default case is the second argument to Piecewise, with all other cases is in a List in the first argument. One way to preserve this structure is to use

piecePart[n_][vec_] :=
ReplacePart[
vec, {{1, j_, 1} :> vec[[1, j, 1, n]], 2 :> vec[[2, n]]}]


Example:

piecePart[2][ab]

a2  0 < r < 1
b2  0 1 < r < 2
c2  True

ab = Piecewise[{{{a1, a2, a3}, 0 < r < 1}, {{b1, b2, b3}, 1 < r < 2}}, {c1, c2, c3}];

pwPart[n_] := MapAt[#[[n]] &, {{1, All, 1}, {2}}]

ab // pwPart[1]
ab // pwPart[2]
ab // pwPart[3]