4
$\begingroup$

Given a function f[x , y].
The definition of f is not self-describing. (f was defined with objects other than f.)
Then I want to construct a function g[x] such that
f[x,y] and g[x][y] are the same, and the definition of g does not contain f.

For example, given

In[1]  f[x_, y_] := x^2 + y^3

I've tried

In[2] g[x_]:=f[x,#]&

Checked an example

In[3]  f[3,2] == 17 == g[3][2]
Out[3] True

Looks okay so far but

In[4]  Definition[g]
Out[4] g[x_] := f[x, #1] &

The definition of g contains f. (fail)
What I want is a function h such that

In[4] Definition[h]
Out[4] x^2+#1^3&

I've tried Evaluate/Activate/FullSimplify, to g, but not worked.

Can you construct a function let's say, ToOperator, such that ToOperator[f] wil produce h ?

(ToOperator produces a warning message if the definition of f is self-describing. The generalization of ToOperator will be of course, multiple variable function as input + user choose some of those variables)

$\endgroup$
0

1 Answer 1

7
$\begingroup$

What you want is called "currying".

Not involving new operators you may try:

Clear[f, g]
f[x_, y_] := x^2 + y^3
g[x_] = Evaluate[f[x, #]] &;

f[3, 2] == 17 == g[3][2]
?? g

enter image description here

However, MMA has a special operator CurryApplied for this:

Clear[f, g]
f[x_, y_] := x^2 + y^3
g = CurryApplied[f, 2];
f[3, 2] == 17 == g[3][2]
?? g

enter image description here

You may want to also look at OperatorApplied.

$\endgroup$
1
  • $\begingroup$ To quote the OP (re CurryApplied solution): "The definition of g contains f. (fail)". $\endgroup$
    – Michael E2
    Commented Mar 22, 2021 at 14:10

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.