I have many delayed-defined functions of many variables, and all the functions share a term containing some of these variables. Thus, I would like to just call the term x and for each function to immediately read x as that term. What would be the easiest/best/cleanest way to do this?
As a very simplified example, the code
x = a1 + a2;
f[a1_, a2_, a3_] := x + a3
f[b1, b2, b3]
gives the output
a1 + a2 + b3
but the output I want is
b1 + b2 + b3
Given my problem, the following concerns and conditions are understandable.
I want to avoid defining
xexplicitly as a function (i.e.x[...]) because it depends on many variables, it would be called many times, andx[...]would end up being almost as large as the term it is supposed to be a shortcut for.I want to avoid calling
Module[]orBlock[]or using replacement rules in each function because I would have to do this for many functions and they are complicated enough as it is. This is why I would like the definition forxto be global.
It seems like there should be a straightforward way for Mathematica to just immediately read a symbol x in each delayed-defined function as a term containing some of the functions' arguments.
f[a1_, a2_, a3_] = x + a3? – Carl Woll Oct 25 '18 at 16:17globalEnvironment[x->a1+a2, f[a1_,a2_, a3_] = x+a3, g[a1_, a2_, a3_] = x^2-a3, ...]work, or do you want to modify howSetDelayedworks (i.e., modify a built-in symbol) so that you can just usef[a1_,a2_, a3_] = x+a3; g[a1_, a2_, a3_] = x^2-a3? – Carl Woll Oct 25 '18 at 16:50globalEnvironment, except I wouldn't want to define a large number of big functions inside one function. I don't need to modifySetDelayedoverall, except maybe for how it works just the symbolx. – Just Some Old Man Oct 25 '18 at 20:09BlockI believe it is the correct solution here. I consider this question a duplicate of (69590), which I answered withblockSet; please try that before rejecting the idea. – Mr.Wizard Oct 27 '18 at 17:55