How to create a function directly from the output of Solve

前端 未结 5 1832
天命终不由人
天命终不由人 2020-12-31 14:34

If I evaluate Solve[f[x,y]==0,x], I get a bunch of solutions like:

{{x -> something g[y]}, {x -> something else}}, etc.

Now I

5条回答
  •  一生所求
    2020-12-31 14:36

    Here's a simple solution that could be cleaned up

    In[1]:= solns = Solve[x^2+a x+b==0, x]
    Out[1]= {{x -> 1/2 (-a-Sqrt[a^2-4 b])}, {x -> 1/2 (-a+Sqrt[a^2-4 b])}}
    
    In[2]:= Table[Symbol["g"<>ToString[i]][a_,b_] := Evaluate[x/.solns[[i]]],
                  {i,Length[solns]}];
    
    In[3]:= DownValues/@{g1,g2}
    Out[3]= {{HoldPattern[g1[a_,b_]]:>1/2 (-a-Sqrt[a^2-4 b])},
             {HoldPattern[g2[a_,b_]]:>1/2 (-a+Sqrt[a^2-4 b])}}
    

提交回复
热议问题