Discussion:
Simple question
(too old to reply)
George Kamin
2004-05-07 08:43:54 UTC
Permalink
Hi All,
This is simple question, but I do not know how to go about searching for it
in this user groups data base. I also could not find a reference to the
solution in the "Mathematica Book". How does one assign the result of
Solve[.] or FindRoot[.] in the form {x1-> 3.14,x2->0.763} to the two
variables y1,y2 respectively?? Thanks for any assistance or specific
references in the M book. George Kamin
Denis Mângia
2004-05-08 05:30:35 UTC
Permalink
Post by George Kamin
Hi All,
This is simple question, but I do not know how to go about searching for
it
Post by George Kamin
in this user groups data base. I also could not find a reference to the
solution in the "Mathematica Book". How does one assign the result of
Solve[.] or FindRoot[.] in the form {x1-> 3.14,x2->0.763} to the two
variables y1,y2 respectively?? Thanks for any assistance or specific
references in the M book. George Kamin
You can do this, for example:

{a, b} = {x, y} /. {x -> 2, y -> 3}

After this command, a = 2 and b = 3

Denis Maciel
Murray Eisenberg
2004-05-08 05:37:40 UTC
Permalink
Just do a bit of generalization from the examples given in the
Mathematica Book, section 1.5.7 "Solving equation", about getting values
from the rules provided by Solve with a single variable:


{y1, y2} = {x1, x2} /. {x1 -> 3.14, x2 -> 0.763}
{3.14, 0.763}
Post by George Kamin
Hi All,
This is simple question, but I do not know how to go about searching for it
in this user groups data base. I also could not find a reference to the
solution in the "Mathematica Book". How does one assign the result of
Solve[.] or FindRoot[.] in the form {x1-> 3.14,x2->0.763} to the two
variables y1,y2 respectively?? Thanks for any assistance or specific
references in the M book. George Kamin
--
Murray Eisenberg ***@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305
Jean-Michel Collard
2004-05-08 05:38:40 UTC
Permalink
Post by George Kamin
Hi All,
This is simple question, but I do not know how to go about searching for it
in this user groups data base. I also could not find a reference to the
solution in the "Mathematica Book". How does one assign the result of
Solve[.] or FindRoot[.] in the form {x1-> 3.14,x2->0.763} to the two
variables y1,y2 respectively?? Thanks for any assistance or specific
references in the M book. George Kamin
Read the * Manual i.e the mathematica book :-)

In[1]:=
l={x1->3.14,x2->0.763};



In[8]:=
y1=Last[First[l]]

Out[8]=
3.14

In[9]:=
y1

Out[9]=
3.14

In[10]:=
y2=Last[Last[l]]

Out[10]=
0.763

In[11]:=
y2

Out[11]=
0.763

There's a shorter way,guess :)
Bill Rowe
2004-05-08 05:44:46 UTC
Permalink
Post by George Kamin
This is simple question, but I do not know how to go about
searching for it in this user groups data base. I also could not
find a reference to the solution in the "Mathematica Book". How
does one assign the result of Solve[.] or FindRoot[.] in the form
{x1-> 3.14,x2->0.763} to the two variables y1,y2 respectively??
Thanks for any assistance or specific references in the M book.
Use ReplaceAll i.e.

{y1,y2} = {x1,x2}/.{x1-> 3.14,x2->0.763}

For more detail look up ReplaceAll in either the Mathematica Book or Help Browser
--
To reply via email subtract one hundred and four
Peltio
2004-05-08 05:45:46 UTC
Permalink
Post by George Kamin
This is simple question, but I do not know how to go about searching for it
in this user groups data base. I also could not find a reference to the
solution in the "Mathematica Book". How does one assign the result of
Solve[.] or FindRoot[.] in the form {x1-> 3.14,x2->0.763} to the two
variables y1,y2 respectively??
Let's call your solution sol:
sol = {x1-> 3.14,x2->0.763};
I guess that
{y1,y2} = {x1,x2} /. sol
would do what you want.

I wrote a simple function, named ToValues, to avoid replacements of this
kind. They can sometimes be tedious because you have to reproduce the
structure of the result twice. With ToValues you can still assign the values
to your variables, such as in

{y1,y2} = ToValues[sol]

but the procedure can be used as a postfix operator without having to build
a pure function (as in the case of the replacement)

{y1,y2} = sol // ToValues

Moreover, it can understand the structure of the solution of systems of
equations in several variables and arrange the values avoid
unnecessary nesting of parenthesis. ("unnecessary" for the naive use
I had in mind at the time I wrote the function).
For example, suppose you want to solve the equation x^5==1:

sols= Solve[x^5 == 1] // ToValues
{1, -(-1)^(1/5), (-1)^(2/5), -(-1)^(3/5), (-1)^(4/5)}

Its functionality is essentialy of cosmetic nature: the code is just a
little bit cleaner.

ToValues can also perform some actions, if instructed to do so.
It allows you to specify a function that can do things on the values
extracted. In this case we extract the real and imaginary parts and
enclose them into a list structure to use to plot the points in the complex
plane.

cmplxToXY[z_]:={Re[z], Im[z]}
pts = ToValues[ Solve[x^5 == 1, x], cmplxToXY ] // N;
ListPlot[pts, AspectRatio -> Automatic];

The functions can also be indexed, but that means going too far away
from what you asked.
The code for ToValues is the following:

(**** Code begins ****)
ToValues::usage = "ToValues[li] suppresses the Rule wrapper in
every part of the 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
to it the lhs of Rule 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 in order to simplify its structure (the flattening is tuned to get
the simplest list of values for the solution of a system of several
equations in several variables).
With Flattening set to None the original structure is left
intact.";

Options[ToValues] = {Flattening -> Automatic, IndexedFunction -> False};

ToValues[li_, opts___Rule] := Module[
{newli, vars, sols, fl},
fl = Flattening /. {opts} /. Options[ToValues];
sols = First[Dimensions[li]]; vars = Last[Dimensions[li]];
newli = li /. (_ -> v_) -> v;
If[fl == Automatic && vars == 1, newli = Flatten[newli]];
If[fl == Automatic && sols == 1, First[newli], newli]
]

ToValues[li_, fun_, opts___Rule] := Module[
{newli, vars, sols, foo, fl, mi},
mi = IndexedFunction /. {opts} /. Options[ToValues];
fl = Flattening /. {opts} /. 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
]

(**** Code ends****)

cheers,
Peltio
Paul Abbott
2004-05-11 09:39:57 UTC
Permalink
Post by George Kamin
This is simple question, but I do not know how to go about searching for it
in this user groups data base. I also could not find a reference to the
solution in the "Mathematica Book". How does one assign the result of
Solve[.] or FindRoot[.] in the form {x1-> 3.14,x2->0.763} to the two
variables y1,y2 respectively??
Others have answered your question -- but I should ask you _why_ you
want to assign the values (returned as rules) to variables? The idea of
returning results as a list of replacement rules is to avoid explicit
variable assignments.

If you have

vals = {x1-> 3.14,x2->0.763};

then you can substitute these values into any expression involving x1
and/or x2. For example

x1^2 - x2 /. vals

or

ContourPlot[Evaluate[(x-x1)^2+(y-x2)^2 /. vals], {x,-5,5}, {y,-1,1}];

Sometimes variable assignment is desirable or required but, usually, the
above approach is preferable.

Cheers,
Paul
--
Paul Abbott Phone: +61 8 9380 2734
School of Physics, M013 Fax: +61 8 9380 1014
The University of Western Australia (CRICOS Provider No 00126G)
35 Stirling Highway
Crawley WA 6009 mailto:***@physics.uwa.edu.au
AUSTRALIA http://physics.uwa.edu.au/~paul
Loading...