6
$\begingroup$

I want to define a functional E by using the property E[ E[f[x]] ] := E[f[x]] for some arbitrary function f[x]. Meaning, that I want Mathematica to evaluate every expression E[ E[f[x]] ] to E[f[x]] for all functions f[x]. How do I make this in Mathematica? I tried

E[ E[f_[x]] ] := E[f[x]]

but this gives the following error "$IterationLimit::itlim: Iteration limit of 4096 exceeded." and as output Hold[E[f[x]]]. Thanks!

$\endgroup$
2
  • 4
    $\begingroup$ Hello. First, do not use the letter E because it has built-in meaning, type ?E to see this. So let me use e instead (you can replace this by any other name that does not correspond to a symbol with built-in meaning). Then perhaps what you want is e[e[expr_]]:=e[expr]. Try typing e[e[f[x]]] now. You can also use something like Format[e]=Style["E",Blue] to change the way your e is printed in the notebook output. $\endgroup$
    – user293787
    Jul 26 at 8:34
  • $\begingroup$ Please edit your question so that ALL code is formatted properly. I changed some of it so that you can see how it works. $\endgroup$
    – user293787
    Jul 26 at 8:39

1 Answer 1

12
$\begingroup$

The evaluation loop for f[f[x]]will first evaluate f[x]and then feed to output to f . Therefroe you must prevent the first evaluation. You can do this by giving f the attribute HoldFirst. Only then you may define f[f[x]]:

ClearAll[f]
SetAttributes[f, HoldFirst]
f[f[x_]] = f[x];
f[x_] = x^2;
f[f[3]]

(* 9 *)
$\endgroup$
0

Your Answer

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

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