3
$\begingroup$

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!

$\endgroup$
0
$\begingroup$
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]

enter image description here

$\endgroup$
1
$\begingroup$

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 = Internal`FromPiecewise[#], n = #2}, 
    Internal`ToPiecewise @@ {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:

Internal`FromPiecewise[ab]

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

Internal`FromPiecewise[ab][[2]]

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

$\endgroup$
0
$\begingroup$

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
$\endgroup$
0
$\begingroup$
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]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.