Assigning solve result to function



Dmitry Vinokurov wrote:
> julien cubizolles wrote:
>   
>> Le samedi 28 novembre 2009 ? 14:55 +0500, Dmitry Vinokurov a ?crit :
>>   
>>     
>>> Hello,
>>>
>>> I've got an equation, for example 'x_1 + x_2 = 0' (of course real case
>>> is not so simple, but it's enough for demonstration) and I need to get
>>> function F(x_2) from it.
>>>
>>> I tried to use solve for it 'solve(x_1 + x_2 = 0, x_1)' and get 'x_1 = -
>>> x_2'. Now I couldn't find, how to extract right part of solve result
>>> into function. Just 'F(x_2) := solve(x_1 + x_2 = 0, x_1)' of course
>>> doesn't work, it results to 'x_1 = -x_2' instead of wanted '- x_2'.
>>>     
>>>       
>> You can use : 
>>
>> F(x_2):=sublis(solve(x_1+x_2=0,x_1),x_1);
>>
>> In some more complex situations, the := function definition won't work
>> and you will need to use define, as in : 
>>
>> define(F(x_2),sublis(solve(x_1+x_2=0,x_1),x_1));
>>
>> I'm not sure exactly why but I just recently found out how to do it.
>>
>> Julien.
>>
>>   
>>     
> To Jul

It is possible that this is the right solution, but probably very 
inefficient, unless you want to call solve every time you use F.
You might prefer something like this:

solve(...., x_1);

F(x_1):=''rhs(%);

or use sublis ...   That is two single quotes ' '       which does an 
immediate substitution in the function definition.

RJF