tags:

views:

43

answers:

2

I'm generating a list of replacement rules like this

ops = {LessEqual, GreaterEqual};
ineqRules = Table[HoldPattern[Inequality[a_, op1, c_, _, e_]] -> a == c, {op1, ops}]

Above doesn't work because "op1" is hidden from Table by HoldPattern, how do I fix it?

This is a follow-up to previous question

+1  A: 

I am sure there should be a better way, but this seems to work:

ops = {LessEqual, GreaterEqual};
ineqRules[op_] := HoldPattern[Inequality[a_, op, c_, _, e_]] -> a == c;
ineq = Table[ineqRules[op], {op, ops}];
Inequality[1, LessEqual, x, Less, 2] /. ineq

Out: 1 == x

HTH

Edit

Be carefull with this:

Inequality[e1, GreaterEqual, e2, Equal, e3] /. ineq
Out> e1 == e2

But

Inequality[1, GreaterEqual, e2, Equal, 2] /. ineq
Out> False

I guess some Hold[] beast is needed to get out of that if needed ... let us know

belisarius
This is because Inequality[1, GreaterEqual, x, Less, 2] evaluates to And[GreaterEqual[1,x],Less[x,2]], the same problem occurs in my answer...
Simon
@Simon yes, see my edit
belisarius
Thanks, it works. This is actually as a part of trying to do http://stackoverflow.com/questions/3815496/plotting-linear-inequalities-in-mathematica but it turns out that simply replacing >= with == doesn't give me vertices of the region
Yaroslav Bulatov
+2  A: 

How about

ops = {LessEqual, GreaterEqual};    
ineqRules = (HoldPattern[Inequality[a_, #, c_, _, e_]] :> a == c) & /@ ops

Edit: To fix the problem noted in belisarius's answer, try:

ineqRules=Flatten[{HoldPattern[Inequality[a_,#,c_,___]]:>a==c,HoldPattern[#[a_,c_]&&___]:>a==c}&/@ops]

This obviously depends on you having a simple structure to begin with, i.e. no other &&'s.

Simon
@Simon Sometimes I hate reading clever code in Mathematica :) +1
belisarius