3
$\begingroup$

The output of the equation

Solve[f[x] - x^2 == 0, f[x]]

is:

{{f[x] -> x^2}}

I would like the output to be

{f -> Function[x, x^2]}

In this way I can evaluate, for example,

f'[x] /. Out[2] 

with the desired result,

2x

Can this be done? Of course I have many equations of this form, and far longer... Thanks, Max

$\endgroup$
4
  • 4
    $\begingroup$ Solve[f[x] - x^2 == 0, f[x]][[1]] /. Rule[func_, expr_] :> Rule[Head[func], (Function @@ {List @@ func, expr})] $\endgroup$
    – Bob Hanlon
    Commented May 7, 2023 at 17:36
  • $\begingroup$ That was quick. Thank you Bob. $\endgroup$
    – MaxB
    Commented May 7, 2023 at 17:40
  • 7
    $\begingroup$ There's an internal function that changes solutions to functions for DSolve: DSolve`DSolveToPureFunction[{{f[x] -> x^2}}] $\endgroup$
    – Michael E2
    Commented May 7, 2023 at 17:48
  • $\begingroup$ Thanks Michael. Very useful command too. Max $\endgroup$
    – MaxB
    Commented May 7, 2023 at 18:27

1 Answer 1

3
$\begingroup$

Use the resource function SolutionRulesToFunctions:

First@ResourceFunction["SolutionRulesToFunctions"][Solve[f[x] - x^2 == 0, f[x]]]

(*{f -> Function[{x}, x^2]}*)
$\endgroup$
2
  • 1
    $\begingroup$ I did not know about the ResourceFunction site! Thanks, this is exactly what I was looking for. $\endgroup$
    – MaxB
    Commented May 8, 2023 at 22:56
  • $\begingroup$ I've added a hyperlink. Please see the update. :-) $\endgroup$ Commented May 9, 2023 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.