4
$\begingroup$

Here is some pattern code:

parse2[a_ < b_ < c_] := (Print["Matched!"];)
iterAnd[x_ && y_] := y;

Print["Good example"]
ccr = a < b < c
parse2[ccr]

Print["Bad example"]
ccr2 = iterAnd[BooleanConvert[Reduce[a < b < c, {b}, Reals]]]
parse2[ccr2]

And here is the result:

Good example
a < b < c
Matched!

Bad example
a < b < c
parse2[a < b < c]

The source expressions look similar, but pattern match results vary. What is the reason?

$\endgroup$
  • 1
    $\begingroup$ I find your question hard to follow. Can you create a simple but complete example that shows the problem? Show the input, actual output and expected output. Your example is neither simple nor complete at the moment. $\endgroup$ – Szabolcs Aug 18 '16 at 9:33
  • 1
    $\begingroup$ closely related: Converting inequalities to intervals $\endgroup$ – Kuba Aug 18 '16 at 11:47
5
$\begingroup$

This is confusing indeed. Let me repeat what you are showing here:

In[25]:= ccr2    
Out[25]= a < b < c

In[26]:= parse2[ccr2]    
Out[26]= parse2[a < b < c]

In[27]:= parse2[a < b < c]

During evaluation of In[27]:= Matched!

Out[26] and In[27] looks exactly the same, yet one evaluates the other doesn't. Are they really the same? The first thing to check in such cases is their FullForm.

In[30]:= FullForm[ccr2]
Out[30]//FullForm= Inequality[a,Less,b,Less,c]

In[31]:= FullForm[a<b<c]
Out[31]//FullForm= Less[a,b,c]

As you can see, they are not the same, even though they are formatted in the same way in StandardForm. This explains why there is a match in one case but there isn't a match in another.

I'll update the post if I can come up with a workaround that seems better than the obvious (i.e. manual transformation of Inequality expressions).


Possible workarounds:

The undocumented function Reduce`InequalityExpand seems to convert inequalities such as a < b < c to the form a < b && b < c. This may be useful for your purposes.

The documented function LogicalExpand also carries out this transformation. But it also does other things which may or may not be desirable depending on your specific problem.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you for this function, let it be the weapon of a last chance. :) For my purpose it would be better not to expand this. The final goal is to extract integration limits from the system of inequalities. So I take a big expression, reduce it to an disjunctive form, which carries this way all limits cyphered in inequalities. So I want just to parse this result. $\endgroup$ – Игорь Моисеев Aug 18 '16 at 11:53
  • $\begingroup$ @ИгорьМоисеев You could also try matching both on Less[a_, b_, c_] and Inequality[a_, Less, b_, Less, c_]. But then we'd have to think very carefully if there are any other possible forms that represent the same thing ... $\endgroup$ – Szabolcs Aug 18 '16 at 11:57
  • $\begingroup$ Thank you very much, thought about this too. :) $\endgroup$ – Игорь Моисеев Aug 18 '16 at 11:59

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.