       Re: Extracting functions from Solve

• To: mathgroup at smc.vnet.net
• Subject: [mg7057] Re: Extracting functions from Solve
• From: Dick Zacher <dick at loc3.tandem.com>
• Date: Sat, 3 May 1997 22:04:49 -0400 (EDT)
• Organization: Tandem Computers
• Sender: owner-wri-mathgroup at wolfram.com

```mark christopher haase wrote:
>
> How may I extract and define as functions the solutions derived from
> Solve?
>
> E.g., Solve[x^2+y^2==1,y], I would like to define two functions f1 and
> f2 that are the solutions to this.
>

You could do it this way:

(* Solve the equations *)

In:=
solution = Solve[x^2 + y^2 == 1, y]

Out=
{{y -> -Sqrt[1 - x^2]}, {y -> Sqrt[1 - x^2]}}

(* Define functions based on the two solutions *)

In:=
f1 = Function[ x, Evaluate[ y /. solution[] ] ]

Out=
Function[x, -Sqrt[1 - x^2]]

In:=
f2 = Function[ x, Evaluate[ y /. solution[] ] ]

Out=
Function[x, Sqrt[1 - x^2]]

(* Now test the functions *)

In:=
f1[z]

Out=
-Sqrt[1 - z^2]

In:=
f2[z]

Out=
Sqrt[1 - z^2]

Or, if you were feeling playful, or were expecting to have to do this
repeatedly, you could define a function to make the functions for you.
An example:

In:=
makeAFunction[soln_,yy_,xx_,fun_]:=
(fun=Map[Function[xx, Evaluate[yy /.#]]&,soln])

In:=
SetAttributes[makeAFunction,HoldRest]

In:=
makeAFunction[solution,y,x,g]

Out=
{Function[x, -Sqrt[1 - x^2]], Function[x, Sqrt[1 - x^2]]}

In:=
g[][z]

Out=
-Sqrt[1 - z^2]

In:=
g[][z]

Out=
Sqrt[1 - z^2]

--
-----------------------------
Dick Zacher
Performance Engineering Dept., Tandem Computers
zacher_dick at tandem.com     phone: 408-285-5746     fax:   408-285-7079

```

• Prev by Date: Re: Help ! complex permutations
• Next by Date: Re: (Q:) ReplacePart + SubMatrix replacement
• Previous by thread: Re: Extracting functions from Solve
• Next by thread: How to return the position of a multidimensional list ..