
I understand Mathematica can't assign the results of a Solve to the unknowns because there may be more than 1 solution. How can I assign the 4 values of following result to variables?
Solve[y^2 == 13 x + 17 && y == 193 x + 29, ]
24.1k 7 7 gold badges 58 58 silver badges 103 103 bronze badges
asked Jun 11, 2012 at 13:13
6,978 5 5 gold badges 42 42 silver badges 64 64 bronze badges
$\begingroup$ Could you elaborate on what you are trying to achieve? In principle you can use Replace (or Part ) to assign the values to variables. $\endgroup$
Commented Jun 11, 2012 at 13:36$\begingroup$ @sebhofer - I want to assign the first x value to a variable X1, then the y value to Y1, the other two to X2 and Y2. I just don't seem to get the hang of references yet. $\endgroup$
Commented Jun 11, 2012 at 13:40You can do this :
s = Solve[y^2 == 13 x + 17 && y == 193 x + 29, ]; xx = s[[All, 1, 2]]; yy = s[[All, 2, 2]];
Now you can access solutions, this way xx[[1]] , yy[[2]] .
If you prefer to collect solutions in Array , there is another way :
X = Array[ x, ]; Y = Array[ y, ]; x[k_] /; MemberQ[ Range[ Length @ s], k] := s[[k, 1, 2]] y[k_] /; MemberQ[ Range[ Length @ s], k] := s[[k, 2, 2]]
now X is equivalent to s[[All, 1, 2]] , while Y to s[[All, 2, 2]] , e.g. :
X[[1]] == x[1] Y == s[[All, 2, 2]]
True True
You do not have to use or even to define X and Y arrays, e.g.
We've used Condition i.e. /; to assure definitions of x[i], y[i] only for i in an appropriate range determined by Length @ s , i.e. number of solutions.
answered Jun 11, 2012 at 13:37 57.7k 13 13 gold badges 158 158 silver badges 246 246 bronze badges$\begingroup$ I think the OP is a complete beginner, and all he's looking for is ReplaceAll . This might be a bit too advanced for someone new to Mma. $\endgroup$
Commented Jun 11, 2012 at 13:57$\begingroup$ @Szabolcs Literally there is the assignment tag, so he is rather looking for Set or SetDelayed applications. $\endgroup$
Commented Jun 11, 2012 at 16:07$\begingroup$ I think I'll use your first solution for now, until I get the hang of ReplaceAll. @Szabolcs: Yes, Mma virgin. Thanks, all. $\endgroup$
Commented Jun 12, 2012 at 7:09$\begingroup$ @anhnha That's all information you can get, unless you've defined s in a different way, however one can use FullForm[s] . $\endgroup$
Commented Sep 21, 2017 at 5:06$\begingroup$ @anhnha Read the doccumentation pages of Part then you'll understand what such a notation as [[i,j,k]] mean. $\endgroup$
Commented Sep 21, 2017 at 6:55 $\begingroup$Usually you don't want to actually assign values to x and y , and you would use replacement rules instead:
sols = Solve[y^2 == 13 x + 17 && y == 193 x + 29, ]; /. sols[[1]]
or for the second solution:
/. sols[[2]]
If you really want to assign values to x and y globally, you could use:
Set @@@ sols[[1]]
but you must clear x and y before using another set:
Clear[x, y] Set @@@ sols[[2]]
If you want to assign values to x and y within a Block you could do something like this:
Hold @@ /. Rule -> Set /. _[vars_] :> Block[vars, Sin[x] + Sqrt[y] // N ]
This uses what I am calling the injector pattern to get the values into Block in the right syntax without it prematurely evaluating.
answered Jun 11, 2012 at 13:39 273k 34 34 gold badges 593 593 silver badges 1.4k 1.4k bronze badges $\begingroup$ +1.Something related to your last comment and the injector pattern. $\endgroup$ Commented Jun 11, 2012 at 14:05$\begingroup$ I agree that often you just want to use rules, but in my case I'm trying to set a global variable before going on to solve for other things (where Solve will work better if it has this global value instead of trying to remain general for all possible values). I tried the equivalent of Set[sols[[1]]] , and when that didn't work. Of course the "help" file is no help because it insists on explaining the symbol = and not the function Set , so I ended up here. What is Set @@@ sols[[1]] doing? $\endgroup$
Commented Feb 21, 2016 at 22:39$\begingroup$ @Travis Assuming you want this in my own words please reference (46248). @@@ is shorthand for Apply at levelspec . So foo @@@ val1, y -> val2> becomes
$\begingroup$ @Travis Okay. You need to become familiar with the long form of all these "shorthand" operators. You can see the long form by wrapping code in HoldForm[FullForm[ code ]] -- for example HoldForm[FullForm[
$\begingroup$ @Travis the canonical Q&A here is (15567), and I suggest you also look at the links in my answer to (29750). If these do not answer all your questions please let me know. $\endgroup$
Commented Mar 8, 2016 at 12:45 $\begingroup$Update: Version 10 built-in function Values does value extraction conveniently for rules appearing in lists of arbitrary lengths and depths:
, > = Values[Solve[y^2 == 13 x + 17 && y == 193 x + 29, ]] (* , > *)
lst=1,b->2>,3>,4>>,5,6,7>>>>; Values[lst] (* ,,>,>>> *)
Original post:
, > = Solve[y^2 == 13 x + 17 && y == 193 x + 29, ][[All, All, -1]] (* <<(-11181 - Sqrt[2242057])/74498, 1/386 (13 - Sqrt[2242057])>, <(-11181 + Sqrt[2242057])/74498, 1/386 (13 + Sqrt[2242057])>> *) (* <(-11181- Sqrt[2242057]) / 74498, 1 / 386 (13 + Sqrt[2242057])>*)
answered Sep 19, 2014 at 20:55
399k 18 18 gold badges 485 485 silver badges 919 919 bronze badges
$\begingroup$ I'm sorry you've deleted your previous answer nevertheless suppresing duplicates we reduce increasing overall entropy of this site, +1. $\endgroup$
Commented Sep 19, 2014 at 21:07$\begingroup$ @artes, thank you for the upvote. I agree with your concern over excessive duplicates. The other Q/A is indeed a special case of this one. However, because of its special structure, some tricks that work there do not work here, e.g Last@@@Solve[. ] . $\endgroup$
Commented Sep 19, 2014 at 21:16 $\begingroup$If you really wish to assign solutions to variables, you can do something like this:
In[1]:= ClearAll[Subscript] sols=Solve[y^2==13x+17&&y==193x+29,]; i=0; sols/.:>Set@@@(/.var:x|y->Subscript[var,++i]); Subscript//Definition
Out[5]=
Subscript[x,1]=(-11181-Sqrt[2242057])/74498
Subscript[x,2]=(-11181+Sqrt[2242057])/74498
Subscript[y,1]=1/386(13-Sqrt[2242057])
Subscript[y,2]=1/386 (13+Sqrt[2242057])
Then you can use the solutions for demonstration purposes:

Here's a painless solution:
s = Solve[y^2 == 13 x + 17 && y == 193 x + 29, ];
Assign the four results of the solution s above to a variable each, in sequence
= s // Values // Flatten;
answered Mar 6, 2018 at 4:41
Vixillator Vixillator
974 6 6 silver badges 10 10 bronze badges
$\begingroup$
Ah, they finally implemented it in version 10, then! Here's a procedure I've been using since version 5, it might provide similar features in versions prior to the introduction of Value. (I'm not sure, but maybe I posted it on the MathGroup. so forgive me if this is not news)
I had called it "ToValues". I gave it two options:
Options[ToValues] = < Flattening ->Automatic, IndexedFunction -> False>;
The help message is hopefully self-explicating:
ToValues::usage = "ToValues[li] suppresses the Rule wrapper in every part of list li.\n ToValues[li,F] \ applies the function F to every rhs of Rule, turning var->value into \ F[value]. If the function F has a parametrized head, then it is possible to \ pass the lhs of Rule to it by setting the option IndexedFunction->True. It will \ turn var->value into F[var][value].\n When the option Flattening is set to \ Automatic, ToValues flattens li to yield a simplified structure (the \ flattening is tuned to get the simplest list of values for the solution of a \ system of several equation in several variables). With Flattening set to None \ the original structure is left intact.";
The code is really short.
ToValues[li_, opts___Rule] := Module[ , fl = Flattening /. /. Options[ToValues]; sols = First[Dimensions[li]]; vars = Last[Dimensions[li]]; newli = li /. Rule[_, v_] -> v; If[fl == Automatic && vars == 1, newli = Flatten[newli]]; If[fl == Automatic && sols == 1, First[newli], newli] ] ToValues[li_, fun_, opts___Rule] := Module[ , mi = IndexedFunction /. /. Options[ToValues]; fl = Flattening /. /. Options[ToValues]; If[mi == True, newli = li /. (x_ -> v_) -> foo[x][v], newli = li /. (_ -> v_) -> foo[v] ]; sols = First[Dimensions[li]]; vars = Last[Dimensions[li]]; If[fl == Automatic && vars == 1, newli = Flatten[newli]]; If[fl == Automatic && sols == 1, First[newli], newli] //. foo -> fun ]
sols = 1>, 2>, 3>>;
Application of ToValues to lists of rules produces a list of values
ToValues[sols] // InputForm
Of course assignment is immediate, here
= ToValues[sols]
By default, ToValues returns the simplest list of values possible, suppressing nested list. If you want to preserve the original nesting, this is what the Flattening option does:
ToValues[sols, Flattening -> None] // InputForm
We can specify a function to be applied to the returned values.
ToValues[sols, F] // InputForm
We can also use function that make use of the right hand sides of the rules. Just define them as functions with a parametrized Head and set IndexedFunction->True to instruct ToValues to make use of that. In this case we want to return lists in the form :
F[var_][value_] := ToValues[sols, F, IndexedFunction -> True] // InputForm
ToValues[sols, F, IndexedFunction -> True, Flattening -> None] // InputForm
Real world applications
ToValues is normally used to extract solutions from the lists returned by Solve, NSolve, DSolve, etc. In its default form it can be applied in this way:
Solve[] // ToValues
This gives a list of the complex solutions
Solve[x^5 == 1] // ToValues
This uses the optional function to compute the real and imaginary part of each solution
coords = ToValues[Solve[x^5 == 1, x], &] // N; ListPlot[coords, AspectRatio -> Automatic, Frame -> True, PlotStyle -> PointSize[.018]];
And this pushes the function to create graphics objects (Points) based on those values inside ToValues:
pts = ToValues[Solve[x^9 == 1, x], Point[] &]; Show[Graphics[], AspectRatio -> 1, Frame -> True, Axes -> True];
Someone who has patience enough might want to add the plots.