MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: here's one driving me mad!


Glenn Roberts wrote

>i have used the FindRoot function and it returns an output:
>{sd -> 10} say
>how do i get the output to return it without the sd -> bit ?

The most obvious answer is : apply the rule

    sd /. {sd->10}
        10

You can also suppress the sd-> bit with a rule Rule[_, v_] -> v

    {sd->10} /. Rule[_,v_]->v
        {10}

A more general approach could be the following procedure that uses
Dimensions to understand how many solutions and how many variables are
passed to it:

    toValues[li_] := Module[
        {newli, vars, sols},
            sols = First[Dimensions[li]]; vars = Last[Dimensions[li]];
            newli = li /. Rule[_, v_] -> v;
            If[vars == 1, newli = Flatten[newli]];
            If[sols == 1, First[newli], newli]
    ]

This procedure gives
    - the value if there is only one solution in one variable
        {{x->10}}//toValues
            10
        {x->10}//toValues
            10
    (as an aside: the latter works because First and Last give
    the same single value (1) to sols and vars)
    - a list of values if there are multiple solution in one variable
        {{x->2},{x->-2}}//toValues
            {2,-2}
    or one solution in multiple variables
        {{x->1,y->2}}//toValues
            {1,2}
    - and a list of lists when there are multiple solutions in multiple
    variables
        {{x->1,y->2},{x->0,y->-1}}//toValues
            {{1,2},{0,-1}}

Help yourself!

cheers,
Peltio
invalid address in reply-to. Crafty demunging required to mail me.




  • Prev by Date: Re: here's one driving me mad!
  • Next by Date: RE: How to use Rules from Solve and FindRoot
  • Previous by thread: Re: here's one driving me mad!
  • Next by thread: change of a coefficient in a polynomial