2
$\begingroup$

I have a function (Piecewise) that returns a list, e.g.:

F[x_] := Piecewise[{{{x^2, x}, x < 0}, {{x, x^2}, x > 0}}, {0, 0}];

I would like to add another list (e.g. {2,3}) to the output of that function, i.e., I would like to obtain:

enter image description here

If I simply do:

{2, 3} + F[x] 

I obtain:

{2 + F[x], 3 + F[x]} 

,i.e.:

enter image description here

which is not what I want. Naturally, I could include the list I want to add ({2,3}) in every branch of the definition of the Piecewise function, e.g.:

Piecewise[{{{2, 3} + {x^2, x}, x < 0}, {{2, 3} + {x, x^2}, x > 0}, {2,3}}]

But this is unsatisfactory, because my actual problem is much more convoluted that this MWE, with many different cases in the piecewise function, and I also want to use the result of the function as a vector in matrix multiplications and so on.

I have tried things like:

MapThread[Plus,{{2, 3}, F[x]}] 

Add[x_List, y_List] := x + y;
Add[{2, 3}, F[x]]

Assuming[VectorQ[F[_]], {2, 3} + F[x]]

without any success. Is there any simple way of doing this? Thanks a lot

$\endgroup$
1
  • $\begingroup$ From the help of Piecewise: "If any of the preceding Subscript[cond, i] do not literally yield False, the Piecewise function is returned in symbolic form. " $\endgroup$ Commented Feb 23 at 7:49

1 Answer 1

3
$\begingroup$

As to your failed trial, I'm afraid you've severely misunderstood the usage of MapThread, _List and Assuming. Please notice Assuming only changes the global variable $Assumptions so it only has effect on those functions with Assumptions option; MapThread and _List simply don't do mathematical transforms, they won't handle Piecewise in the clever way you're imagining. (In my view, this post that you've also participated explains the topic well. )

As to your problem, the simplest solution I can think out is, transforming Piecewise to combination of UnitStep so the F[x] will be an explicit list. This can be easily done with Simplify`PWToUnitStep:

F[x_] = 
 Piecewise[{{{x^2, x}, x < 0}, {{x, x^2}, x > 0}}, {0, 0}] // Simplify`PWToUnitStep
(* {x (1 - UnitStep[-x]) + x^2 (1 - UnitStep[x]), 
 x^2 (1 - UnitStep[-x]) + x (1 - UnitStep[x])} *)

trial = F[x] + {2, 3};

Plot[trial, {x, -4, 4}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you so much for solving my problem and for explaining the answer so well. I really appreciate it! Simplify`PWToUnitStep is not documented, right? $\endgroup$
    – Luis
    Commented Feb 23 at 17:57
  • 1
    $\begingroup$ @Luis Yeah, it's an undocumented function that looks strange at first glance but actually quite useful when dealing with various issues related to Piecewise. Just search in this site and you'll find more interesting examples :) . $\endgroup$
    – xzczd
    Commented Feb 24 at 2:26

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