I need to define a function let's say f[x_, y_] := x^2 + y^2
such that I need to fix one variable (let's take y = a
) and to evaluate the expression which would now be in terms of only the other variable and the fixed variable. So, for input like f[x1, y1]
, the output must be x1^2 + a^2
. For input f[x2, y2]
, the corresponding output must be x2^2 + a^2
.
$\begingroup$
$\endgroup$
5
1 Answer
$\begingroup$
$\endgroup$
You could define y
to be an optional argument which will get the default value a
when no value for y
is supplied.
f[x_, y_: a] := x^2 + y^2
{f[u, v], f[u]}
{u^2 + v^2, a^2 + u^2}
fya[x_] := f[x, a]
$\endgroup$SubValues
:fya[a_][x_]:=f[x,a]
(so that then one always usesfya[a]
as a "function"), or create a closure at run-time:makeFYA[a_]:= Function[x,f[x,a]]
, and thenfya = makeFYA[a]
. The advantage here is that we have a better control. $\endgroup$