0
$\begingroup$

Suppose I have a function f[x, coeff]

coeff[n_] := Table[RandomVariate[NormalDistribution[]], {n + 1}];
f[x_, coeff_] := Sum[coeff[[i + 1]] x^i, {i, 0, Length[coeff] - 1}]

and I want to create another function f1[x] by substituting the concrete second argument.

Update: At first I tried to define it as a simple function:

f1[x_] := f[x, coeff1]

But when executing ?f1 I'd like to see the definition of f1 as a concrete polynomial rather than a definition in terms of another function. In other words I would like f[x, coeff1] to be evaluated for coeff1 but not for x.

The closest I could get to that goal was:

coeff1 = coeff[1]
f1[x_] := Evaluate[f[Hold[x], coeff1]]

This results in f1 containing the Hold over x:

f1[x_] := 1.23788 - 0.790537 Hold[x]

Suppose also that a have x defined somewhere:

x = 1

I tried some combinations of Function, ReleaseHold and Unevaluated. For example:

f1 = Function[x, Evaluate[f[Hold[x], coeff1]]]

f1[x_] := ReleaseHold[Evaluate[f[Hold[x], coeff1]]]

f1[x_] := Evaluate[f[Unevaluated[x], coeff1]]]

... and some others. But I either get a function with Hold[x], or a function which was already evaluated for x (a constant in this case). What I want to get is when executing ?f1 I want to see:

f1[x_] := 1.23788 - 0.790537 x

rather than

f1[x_] := 1.23788 - 0.790537 Hold[x]

Could anyone please explain how to force the WL's evaluation algorithm to do this?

$\endgroup$
9
  • $\begingroup$ Try Clear[f1]; f1[x_] := f[x, coeff[1]] for the first part of your problem. For the second part, if $x$ is defined, it will be substituted in: you can't reasonably avoid that. You seem to be trying very hard to work against normal practices, which suggests that there is another underlying problem that you are trying to solve in this forcing way. Perhaps you should explain why you need this behavior instead: there might be a better, more natural way to achieve what you ultimately want. $\endgroup$
    – MarcoB
    Nov 3, 2021 at 12:31
  • $\begingroup$ @MarcoB Thank you. I have updated my question. $\endgroup$
    – Max
    Nov 3, 2021 at 12:46
  • $\begingroup$ "...For the second part, if x is defined, it will be substituted in: you can't reasonably avoid that. ..." My idea was that if it is possible to define a function g[x_]:=x + 1 without x being evaluated, then it is likely possible with my problem too. $\endgroup$
    – Max
    Nov 3, 2021 at 12:49
  • $\begingroup$ to "recover" a polynomial expression from the function f1, the simplest way would be to just evaluate f1[x] , where x is unassigned. $\endgroup$ Nov 3, 2021 at 12:58
  • $\begingroup$ If I understand correctly, what you want is: f1[x_] = f[x, coeff[1]] This will evaluate at once. However if you say: f1[x_] := f[x, coeff[1]] the evaluation is delayed to when you call the function. $\endgroup$ Nov 3, 2021 at 12:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.