4
$\begingroup$

This is something that I couldn't explain.

functions = {ConditionalExpression[Abs[1 - 2 x], x >= 1 || x <= 0], 
   ConditionalExpression[1 + x, x >= 0]};

My first function has two separate branches.

Plot[ConditionalExpression[Abs[1 - 2 x], x >= 1 || x <= 0], {x, -5, 
  5}, GridLines -> Automatic]

enter image description here

Now assume that I want to combine all lines into one Line head like this and plot it with ListLinePlot.
As you can see in the image, it has only one branch.

ListLinePlot @@ 
 Cases[Plot[functions[[1]], {x, -5, 5}][[1]], {_Directive, l__Line} :>
     If[Length@{l} == 1, l, Line[{l}[[All, 1]]]], All][[1]]

enter image description here

However, if I modify a bit and use {l} in the If function (not correct though) then it works for the first function and the image shows two branches.

ListLinePlot @@ 
 Cases[Plot[functions[[1]], {x, -5, 5}][[1]], {_Directive, l__Line} :>
     If[Length@{l} == 1, {l}, Line[{l}[[All, 1]]]], All][[1]]

enter image description here

Why does it behave like this?
Did I miss something or a bug? I'm using version 12.2.0.0 (windows)

$\endgroup$

1 Answer 1

6
$\begingroup$

A simpler example that replicates what you are seeing:

input1 = {Directive[blah], Line[foo], Line[bar]}

Cases[input1, 
 {_Directive, l__Line} :> If[Length@{l} == 1, l, Line[{l}[[All, 1]]]], All][[1]]
Line[bar]
Cases[input1,
  {_Directive, l__Line} :> If[Length@{l} == 1, {l}, Line[{l}[[All, 1]]]], All][[1]]
Line[{foo, bar}]

The reason we are getting Line[bar] in the first case above is: When we use l in the second argument of If we are actually using a four-argument form of If where

If[condition,t,f,u] gives u if condition evaluates to neither True nor False.

Wrap If[...] with HoldForm to see what expression is processed on the rhs of RuleDelayed in the first case above:

Cases[input1, {_Directive, l__Line} :> 
  HoldForm[If[Length@{l} == 1, l, Line[{l}[[All, 1]]]]], All]

enter image description here

As an other illustration try a modified input that triggers an error message:

input2 = {Directive[blah], Line[foo], Line[bar], Line[buz]}; 

Cases[input2, 
  {_Directive, l__Line} :> If[Length@{l} == 1, l, Line[{l}[[All, 1]]]], All][[1]]

enter image description here

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks, how can I make If behave as 3-arguments? How about HoldForm[l]? It works here but I'm not sure if that is the way to go. Aslo just curious why does it look like a dot in your Out[] instead of a number? $\endgroup$
    – hana
    Mar 24, 2022 at 18:06
  • 1
    $\begingroup$ You can use If[Length@{l} == 1,First@{l}, Line[{l}[[All, 1]]]] instead of If[Length@{l} == 1, l, Line[{l}[[All, 1]]]]. Re Out[ ], it happens when I use Edit >> Copy As >> Bitmap ( don't know why.) $\endgroup$
    – kglr
    Mar 24, 2022 at 18:20
  • $\begingroup$ Nice, I have never used copy Bitmap like that. $\endgroup$
    – hana
    Mar 24, 2022 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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