Message Boards Message Boards

0
|
5472 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Can you extract variables from a list of equations?

Posted 9 years ago

Given a list of equations (or inequalities) I wish to find all the variables in said list. Example:

{x >= 0, y >= 0, z >= 0, 2 x >= y, 2 z >= y, 2 x + y <= 4, 2 z + y <= 4};
-or-
{x1 >= 0, x2 >= 0, x3 >= 0, 2 x1 >= x2, 2 x3 >= x2, 2 x1 + x2 <= 4, 2 x3 + x2 <= 4}

This will generate:

{x,y,z}
-or
{x1,x2,x3}

Respectively. Ive tried to use Variables, however, Variables is only for expressions. Is this even possible?

POSTED BY: Conor Nelson
2 Replies

Try this:

list = {x1 >= 0, x2 >= 0, x3 >= 0, 2 x1 >= x2, 2 x3 >= x2, 2 x1 + x2 <= 4, 2 x3 + x2 <= 4} ;
Variables[Part[list,All,1]]
Variables[Apply[List,list,1]]

I.M.

POSTED BY: Ivan Morozov

This will return all symbols at level -1 that are not heads and not numeric:

vars[expr_] := Union@Cases[expr, s_Symbol /; Not@NumericQ[s], {-1}]

vars[{x >= 0, y >= 0, z >= 0, 2 x >= y, 2 z >= y, 2 x + y <= 4, 2 z + y <= 4}]
(* {x,y,z} *)

Why not heads and not numeric? So vars[Sin[x] + Pi] will give only x but not Sin or Pi.

Be cautious when using this solution. I'm sure it wouldn't be hard to find cases when it returns symbols it shouldn't. It also doesn't handle non-symbol variables such as x[1] in x[1]^2+1, or subscripted ones such as $x_1$. Variables does handle these. But variables only handles polynomials, e.g. Variables[Sin[x]] --> {Sin[x]}.

POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract