4
$\begingroup$

It would be convenient to have a function VarsfromLogical which out of {a+b>c,c+d==a} outputs {a,b,c,d}

Incidentally, I use this for detecting the variables for FindInstance[expr,vars], and it might have been convenient for beginners if Mathematica would have offered also the syntax FindInstance[expr], because the vars are uniquely determined by the expr , and so there is no need to be explicit about them. Anyway, this may be achieved via the answers to the similar question Extracting variables from an expression or by some other instructive answers like Union[Cases[list, _Symbol, ∞]], Variables[Level[list, {-1}]] and p = Position[list, _Symbol, Heads -> False]; Extract[p] @ list Remains the question of why these lovely solutions are necessary at all in a FindInstance

$\endgroup$
3
  • 1
    $\begingroup$ This seems the same question: mathematica.stackexchange.com/questions/21257/… $\endgroup$
    – Goofy
    Feb 10 at 19:45
  • 1
    $\begingroup$ For instance Reduce`FreeVariables[{a + b > 0, c + d == a}] works (from an answer to the linked question). $\endgroup$
    – Goofy
    Feb 10 at 19:47
  • $\begingroup$ Indeed, one of the answers DeleteDuplicates@Cases[list, _Symbol, Infinity] does the job, together with all the other variants mentioned. Incidentally, I use this for detecting the variables for FindInstance[expr,vars], and it seems convenient for beginners if Mathematica would have offered also the syntax FindInstance[expr], because the vars are uniquely determined by the expr $\endgroup$
    – florin
    Feb 10 at 22:26

4 Answers 4

4
$\begingroup$
Union[Cases[list, _Symbol, ∞]]
$\endgroup$
3
  • $\begingroup$ This works also without Union, just Cases[list, _Symbol, [Infinity]], and also Cases[list, _Symbol, 3] $\endgroup$
    – florin
    Feb 10 at 19:05
  • 1
    $\begingroup$ @florin What does Cases[{a + b > 0, c + d == a}, _Symbol, Infinity] return for you? Also Union@Cases[{a + b > 0, c + d == a}, _Symbol, {-1}] might be better than using the level spec 3 (if you want to avoid Infinity). $\endgroup$
    – Goofy
    Feb 10 at 19:42
  • $\begingroup$ Both Cases[{a + b > 0, c + d == a}, _Symbol, Infinity] and Cases[{a + b > 0, c + d == a}, _Symbol, {-1}] output {a, b, c, d, a} on my 13.3 Oops, the a appears twice if I don't use also Union, or Variables, so these are necessary $\endgroup$
    – florin
    Feb 10 at 22:17
5
$\begingroup$
list = {a + b > 0, c + d == 0};

p = Position[list, _Symbol, Heads -> False]

{{1, 1, 1}, {1, 1, 2}, {2, 1, 1}, {2, 1, 2}}

Extract[p] @ list

{a, b, c, d}

$\endgroup$
3
  • 1
    $\begingroup$ Beautiful! 4 answers showing the beauty of Mathematica:) $\endgroup$
    – florin
    Feb 10 at 18:04
  • 1
    $\begingroup$ Mikado's and my solution also work with deeply nested lists like list = {{a + b > 0, c + d == {{g + h}}}}. You don't have to worry about level-specifications. $\endgroup$
    – eldo
    Feb 10 at 18:10
  • $\begingroup$ Thanks, I see! I think I have maybe digested all the solutions, except that I would have written yours in a simpler syntax, as Extract[list, p] ; I still don't understand the point in having the alternative syntax using the prefix operator @ $\endgroup$
    – florin
    Feb 10 at 19:23
2
$\begingroup$
Variables@list[[All, 1]]

{a, b, c, d}


For a more general case:

list = {a + b > 0, c + d == g + h}
v1 = Variables@list[[All, 1]]
v2 = Variables@list[[All, 2]]
Union[v1, v2]

{a, b, c, d, g, h}

$\endgroup$
2
  • 1
    $\begingroup$ Variables[Level[list, {-1}]] is very robust $\endgroup$
    – Bob Hanlon
    Feb 10 at 18:28
  • $\begingroup$ As an aside, Level[list, {-1}] works also. Another issue is to understand the command Level, which seems rather sophisticated, and in particular the option -1. Thanks a lot anyway, I'm using this in a FindInstance for a list of many conditions, some nested, and it works! $\endgroup$
    – florin
    Feb 10 at 19:40
2
$\begingroup$
{a + b > 0, c + d == 0};
Variables[List @@@ %]

{a, b, c, d}
$\endgroup$

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