1
$\begingroup$

I have a huge output from Resolve (quantifier elimination) that I must parse to extract the coefficients as numbers. Take for instance something that looks like this:

(c | x | y | z) \[Element] Reals && (c <= 1 && 
    c > -1 && -x - y + z == 0 && -2 c + 2 x >= -2)

As you can see, it outputs the basis and domain, followed by the expressions for the region the solution defines. (In this case, for instance, c <= 1 would be what I call an "expression", as well as -x - y + z == 0.)

I now must extract the coefficients from each expression of this result. A reasonable output for the example above would be to build the list

{{1, 0, 0, 0, "<=", 1}, {1, 0, 0, 0, ">", -1},
{0, -1, -1, 1, "==", 0}, {-2, 2, 0, 0, ">=", -2}}

but anything reasonable (in the sense it'd allow me to get the extract coefficients of each expression as numbers, while also storing the relation type) would do.

The closest hint I found in the documentation was the Coefficient function, but I couldn't adapt it to work in this case. Is it possible to do so? If not, are there other built-in options?

Also, I've seen a few related questions (such as this) that do something similar using pattern matching, but for a single expression. Would this approach be a extensible to my case? Do you have any hints on how should I proceed to apply it to my situation?

I'm quite new to Wolfram Language so I appreciate any help whatsoever.

$\endgroup$
3
  • 1
    $\begingroup$ For clarity, please provide the output that you expect for this case. $\endgroup$ – Bob Hanlon Oct 8 '20 at 14:37
  • 1
    $\begingroup$ Your question is not too clear. To you mean to set all numeric coefficients to zero? Easy: expy/. x_/;NumericQ[x] ->0. Of course this may result in new coefficients != zero in this case use "//." instead of "/." $\endgroup$ – Daniel Huber Oct 8 '20 at 15:06
  • $\begingroup$ @DanielHuber My question wasn't clear enough. Following the suggestion above, I updated it with a desired output. Your answer helped me anyway. Do you have any tips on how would I use this type of pattern matching to, instead of substitute the values, extract them? More than that: how would I extract the ones in each expression, while settings the missing ones (i.e.: present in the basis but not in a given expression) to zero? $\endgroup$ – ppln Oct 9 '20 at 0:37
2
$\begingroup$

You may extract the different comparisons using patterns by e.g.:

ex = (c | x | y | z) \[Element] 
    Reals && (c <= 1 && c > -1 && -x - y + z == 0 && -2 c + 2 x >= -2);

comp=Cases[ex, (Less | Greater | LessEqual | GreaterEqual | Equal)[__]  ]

(*{c <= 1, c > -1, -x - y + z == 0, -2 c + 2 x >= -2}*)

The coefficients of the variables you can get e.g. by:

coef = CoefficientRules[comp[[All, 1]], {c, x, y, z}]

(*{{{1, 0, 0, 0} -> 1}, {{1, 0, 0, 0} -> 
   1}, {{0, 1, 0, 0} -> -1, {0, 0, 1, 0} -> -1, {0, 0, 0, 1} -> 
   1}, {{1, 0, 0, 0} -> -2, {0, 1, 0, 0} -> 2}}*)

Or in another form:

coef /. Rule[x1_, x2_] :> {Pick[vars, x1 /. 1 -> True][[1]], x2}

(*{{{c, 1}}, {{c, 1}}, {{x, -1}, {y, -1}, {z, 1}}, {{c, -2}, {x, 2}}}*)

The right sides of the comparisons:

rhs=comp[[All, 2]]

(*{1, -1, 0, -2}*)
$\endgroup$

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.