I'm defining a function in Mathematica as follows:
f[x_] := x+x-x //FullSimplify; In this setup, every time f is called, FullSimplify is executed, which can be inefficient. Ideally, I want to precompute the simplification so that the function is defined directly as:
f[x_] := x; I prefer using SetDelayed ":=" over Set "=" to prevent interference from any existing values assigned to x. Using Evaluate within SetDelayed also leads to interference. I think using the Hold function is an option, but I'm seeking a more straightforward approach in Mathematica. There has to be a better way?
Setdoesn't do what you want? $\endgroup$x = 0; f[x_] = x; f[1]This will give me 0 as an output $\endgroup$NameSpace. Avoid this by using replacementRuledefinition instead. That is,rules = {x -> 3}rather thanx=3. You'll note that two of the answers use scoping constructBlock, which requires you to know which symbols are undefined anyway $\endgroup$