0

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?

Max
  • 291
  • 1
  • 9
  • 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. – MarcoB Nov 03 '21 at 12:31
  • @MarcoB Thank you. I have updated my question. – Max Nov 03 '21 at 12:46
  • "...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. – Max Nov 03 '21 at 12:49
  • to "recover" a polynomial expression from the function f1, the simplest way would be to just evaluate f1[x] , where x is unassigned. – user3257842 Nov 03 '21 at 12:58
  • 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. – Daniel Huber Nov 03 '21 at 12:59
  • And to evaluate an expression in x such as exp = 0.655591 + 0.23043 x, you can use replacement rules such as exp/.{x->7} – user3257842 Nov 03 '21 at 12:59
  • @Daniel Huber Thank you. Unfortunately f1[x_] = f[x, coeff[1]] doesn't work, because x is defined and it gets substituted. What I want is f1[x_] to be defined as a polynomial. I want f[x, coeff[1]] to be evaluated for coeff, but not for x – Max Nov 03 '21 at 13:05
  • Can't you choose an undefined variable instead of x? If no, you could try: Clear[f1]; With[{x = Unique[]}, f1[x, 2] = f[x, coeff[1]]] – Daniel Huber Nov 03 '21 at 13:16
  • I can choose another name. I just want to understand how evaluation works in this case. – Max Nov 03 '21 at 14:32

0 Answers0