Re: Inequalities into Standard Form
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Inequalities into Standard Form
- From: Edmund Greaves <egreaves>
- Date: Mon, 27 Jun 94 16:32:50 CDT
>
> Some time ago, I wrote asking how to put
> linear inequalities into standar form:
> with all variable terms on the lhs, and
> the constant term on the rhs. I wrote that
> I could make such a function myself, but
> hoped for something built-in or ready-made.
> Eventually, I asked WRI tech support, and
> am glad I did. Edmund Greaves, of WRI,
> immediately started in on the problem, and
> has sent me a far neater, and watertight,
> function than anything I would have made.
> Here is the function supplied by Ed Greaves
> of Wolfram Research:
> (I have to hand type everything, so any
> mistakes are mine.)
>
> convertIneq[ x_[lhs_,rhs_] ] :=
> Module{temp,const,result},
> temp = Expand[ lhs - rhs ]; (*all terms to rhs*)
> const = If[Head$ at {(J[temp] === Plus,
> Select[temp, NumberQ],
> 0];
> result = x[temp-const, -const];
> (* If a negative rhs is okay, done *)
> (* If need rhs non-negative... *)
> If[const>0,
> result = Map[ Minus, result];
> result = result /. {Less -> Greater,
> LessEqual->GreaterEqual,
> Greater->Less,
> GreaterEqual->LessEqual}];
> result
> ]
>
> I hope this is of help to any one with similar problems.
>
>
Ronald,
I'm glad you found my solution useful. There was a slight typo in what
you've given here so I thought I'd repost what I wrote:
convertIneq[ x_[lhs_, rhs_] ] :=
Module[{temp, const, result},
temp = Expand[lhs - rhs]; (* move all terms to LHS *)
const = If[Head[temp] === Plus,
Select[temp, NumberQ],
0];
result = x[temp - const, -const];
If[const > 0, (* Is b negative on RHS? *)
result = Map[Minus, result];
result = result /. {Less -> Greater,
LessEqual -> GreaterEqual,
Greater -> Less,
GreaterEqual -> LessEqual}];
result
]
Here are some examples of how you would use this function:
In[2]:= convertIneq[2 (x - 1) == 1]
Out[2]= 2 x == 3
In[3]:= convertIneq[-4 + 2 x + 3 y >= 1 - 4 x + 2 y]
Out[3]= 6 x + y >= 5
In[4]:= ineq2 = x + y - 3 z - 1 <= 2 x - 5
Out[4]= -1 + x + y - 3 z <= -5 + 2 x
In[5]:= convertIneq[ineq2]
Out[5]= x - y + 3 z >= 4
The main use for this function is to convert inequalities to a form
which could be used in a linear programming function.
---------------------------------------------------------------------------
Edmund Greaves egreaves at wri.com
{My views may or may not be the views of Wolfram Research}
---------------------------------------------------------------------------