Re: Simple question

• To: mathgroup at smc.vnet.net
• Subject: [mg48049] Re: Simple question
• From: "Peltio" <peltio at twilight.zone>
• Date: Sat, 8 May 2004 01:24:08 -0400 (EDT)
• References: <c7fi8a\$oda\$1@smc.vnet.net>
• Reply-to: "Peltio" <peltioNOSP at Mdespammed.com.invalid>
• Sender: owner-wri-mathgroup at wolfram.com

```"George Kamin" wrote:

>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??

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
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

```

• Prev by Date: Re: FindRoot cannot find obvious solution
• Next by Date: packges for boolean stuff...
• Previous by thread: Re: Simple question
• Next by thread: Re: Simple question