1
$\begingroup$

I have a function that accepts a single argument that is described by the following grammar:

term := (Less | Greater | Equal | LessEqual | GreaterEqual)[Symbol,Number] 
argument := term | Or[term,term,...]

The expressions are generated from operations like:

Reduce[x^2 -x -6x == 0, x, Reals] // FullForm
(* Or[Equal[x,-2],Equal[x,3]] *)

I want to use a pattern in the function definition to ensure that the argument is of the correct form and I want to convert the argument into the following form for use within the function:

List[term,...]

I am currently bogged down in pattern purgatory. I have tried all of the following and am confused by the results.

(* Test data *)
s = Or[Equal[x,-2],GreaterEqual[x,3]]

This pattern matched the test case. So far so good.

(* case 1 *)
s /. Or[(Less | Greater | Equal | LessEqual | GreaterEqual)
    [x_Symbol, (_Integer | _Rational | _Real)] ..] :> {2}
(* {2} V {2} *)

Let's see if we can capture the head in a variable. Yes, we can. I've got this figured out.

(* case 2 *)
s /. f_Symbol[(Less | Greater | Equal | LessEqual | GreaterEqual)
    [x_Symbol, (_Integer | _Rational | _Real)] ..] :> {f}
(* {Or} *)

Let's limit the pattern to a specific head instead of any symbol. This didn't match. I'm not clear on why.

(* case 3 *)
s /. f_Or[(Less | Greater | Equal | LessEqual | GreaterEqual)
     [x_Symbol, (_Integer | _Rational | _Real)] ..] :> {f}
(* No Match *)

Maybe a different syntax will work. And it does, but I expected {Or} as a result.

(* case 4 *)
s /. f : (Or)[(Less | Greater | Equal | LessEqual | GreaterEqual)
     [x_Symbol, (_Integer | _Rational | _Real)] ..] :> {f}
(* {x == -2} V {x >= 3} *)

Edit: Some appropriately placed parens make a difference. I understand this result, but I still don't understand the previous one.

(* case 5)
s /. (f : Or)[(Less | Greater | Equal | LessEqual | GreaterEqual)
    [x_Symbol, (_Integer | _Rational | _Real)] ..] :> {f}
(* {Or} *)

End Edit

I am missing something important about how patterns/rules work. I would like to correct that gap in knowledge. And, secondarily, I'd like to get my function to only accept an appropriate argument and be able to convert the argument into the desired format.

$\endgroup$
9
  • 1
    $\begingroup$ f_Or won't match Or[f] because it is already evaluated and becomes f $\endgroup$
    – rm -rf
    Feb 26, 2013 at 21:48
  • 1
    $\begingroup$ f_Or matches something with head Or, not exactly Or. So, _Or[stuff] matches Or[something][stuff] $\endgroup$
    – Rojo
    Feb 26, 2013 at 22:08
  • $\begingroup$ @Rojo Does that same logic apply to f_Symbol? Because the pattern with f_Symbol[...] matches while the one with f_Or[...] does not. I assumed that f_Or was simply a more specific version of f_Symbol. Is that not the case? $\endgroup$
    – RandomBits
    Feb 26, 2013 at 22:24
  • $\begingroup$ @rm-rf Why would f_Or ever match Or[f]? I am not following that part of your comment. I do understand why you need to use HoldPattern for Or[f]. $\endgroup$
    – RandomBits
    Feb 26, 2013 at 22:28
  • 1
    $\begingroup$ It is not the case. Symbol is a symbol, and Or is another symbol. One isn't a subset of the other. All symbols have the "invisible head" Symbol. With that in mind, Or matches _Symbol. f_Or would be "equivalent" to f:Or[___] which would be a subset of f:_Symbol[___] $\endgroup$
    – Rojo
    Feb 26, 2013 at 22:28

2 Answers 2

2
$\begingroup$

Why not just this?

f[expr_] := Flatten[{expr /. Or -> List}]

f /@ {Reduce[x == 4, x, Reals], Reduce[x^2 == 4, x, Reals], 
      Reduce[x^2 > 4, x, Reals], Reduce[x^2 >= 4, x, Reals], 
      Reduce[x^2 < 4, x, Reals]}
$\endgroup$
1
$\begingroup$

In the comments to my question above, @Rojo et. al. helped me figure out what I was missing with using patterns.

Based on that help I was able to figure out the answer to the second part of my question. Here is the code that I am using:

Remove[ftermQ];
Remove[f];
ftermQ[expr_] := Module[
  {
    termQ = MatchQ[#, (Less | Greater | Equal | LessEqual | GreaterEqual)] &,
    numberQ = MatchQ[#, (_Integer | _Rational | _Real)] &
   },
   MatchQ[expr, _?termQ[_Symbol, _?numberQ]] || 
    MatchQ[expr, Inequality[_, Less, _Symbol, Less, _]] ||
    MatchQ[expr, Inequality[_, LessEqual, _Symbol, LessEqual, _]] 
]

f[term_?ftermQ | HoldPattern[Or[term : (_?ftermQ) ..]]] := {term}

f /@ {Reduce[x == 4, x, Reals],
      Reduce[x^2 == 4, x, Reals],
      Reduce[x^2 > 4, x, Reals],
      Reduce[x^2 >= 4, x, Reals],
      Reduce[x^2 < 4, x, Reals]}
(**
    {{x==4},
     {x==-2,x==2},
     {x<-2,x>2},
     {x<=-2,x>=2},
     {-2<x<2}}
**)

At this point the only obvious issue is the pattern::patv messages produced because I use term_ as both a single and multiple element.

$\endgroup$

Your Answer

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

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