5
$\begingroup$

I need to use the numerical values returned by Minimize in further computations. Consider,

Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, {x, y}]
{-1, {x -> 0, y -> 1}}

How can I assign the numerical values in this result to variables a, b, c such that a = -1, b = 0 and c = 1?

$\endgroup$

closed as off-topic by Oleksandr R., ilian, MarcoB, bbgodfrey, Sjoerd C. de Vries Aug 10 '15 at 22:01

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Oleksandr R., ilian, MarcoB, bbgodfrey
If this question can be reworded to fit the rules in the help center, please edit the question.

4
$\begingroup$

If I understand you correctly:

{a, b, c} = Extract[{{1}, {2, 1, 2}, {2, 2, 2}}]@
             {-1, {x -> 0, y -> 1}}

After this, a == -1 && b == 0 && c == 1.

$\endgroup$
2
$\begingroup$

In V10, here are a few ways to use Values:

{a = #, {b, c} = Values[#2]} & @@ 
 Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, {x, y}]
(*  {-1, {0, 1}}  *)

{a, {b, c}} = Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, {x, y}] /. 
  sol : {__Rule} :> Values[sol]
(*  {-1, {0, 1}}  *)

With[{minsol = Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, {x, y}]},
 a = First@ minsol;
 {b, c} = Values@ Last@ minsol;
 ]

The last way, being more verbose, might also be considered more expressive.

$\endgroup$
1
$\begingroup$

I believe the most clear way is something like

{a, {b, c}} = {#[[1]], {x, y} /. #[[2]]} &@
              Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, {x, y}]
$\endgroup$
1
$\begingroup$

I think that the following three methods are clear and sufficiently verbose (and work in all MMa versions at least starting from version 5):

res = Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, {x, y}];

Clear[a, b, c] 
{a, {b, c}} = res /. Rule[_, v_] :> v
{-1, {0, 1}}
Clear[a, b, c] 
{a, {b, c}} = res /. r_Rule :> Last[r]
{-1, {0, 1}}
Clear[a, b, c] 
{a, {b, c}} = res /. Rule -> CompoundExpression
{-1, {0, 1}}

Accidentally I found that undocumented two-argument form of Last works in version 10.2 (but does not work in version 8.0.4):

Clear[a, b, c] 
{a, {b, c}} = res /. Rule -> Last
{-1, {0, 1}}

Another alternative (this syntax form of MapAt was introduced after version 8):

Clear[a, b, c] 
{a, {b, c}} = MapAt[Last, res, {2, All}]
{-1, {0, 1}}

And completely different approach (works in all MMa versions):

Clear[a, b, c]
a = First@Replace[res, {x -> b, y -> c, Rule -> Set}, {-1}, Heads -> True];
{a, b, c}
{-1, 0, 1}
$\endgroup$
1
$\begingroup$

I think I'll go with this one.

 {a, b, c} = 
   With[{r = Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, {x, y}]},
     Extract[r, Position[r, _?NumericQ]]]
$\endgroup$
  • $\begingroup$ Cases[r, _?NumberQ, {-1}] is a bit shorter. Another way: Level[r, {-1}][[;; ;; 2]]. $\endgroup$ – Alexey Popkov Aug 10 '15 at 21:02
  • $\begingroup$ @AlexeyPopkov. I thought someone had already posted the Cases case. $\endgroup$ – m_goldberg Aug 10 '15 at 21:15

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