I have lots of definitions of (usually numeric) quantities that depend on parameters; they're mostly matrices (in the actual code, there are many more definitions):
model1A = {{1, E^(I phi)}, {E^(-I phi), t}};
model1B = {b*t, 2};
model2A = {{1, delta E^(I phi)}, {delta E^(-I phi), t}};
model2B = {b*t, delta};
They are truly parameter in a semantic sense: Sometimes they're used generically (unevaluated), sometimes with concrete values, and often I consider certain conditions on these parameter, such as 0<phi<Pi/2
. Furthermore, as typical for parameters and as opposed to function arguments, they're identified by their names rather than by their 'slot position' and they appear in some quantities but not in others (as illustrated above).
This makes obvious why it's not sensible to implement them as function arguments. For the given reasons, it wouldn't make much sense to write model1B[b_,t_] = {b*t, 2}
.
Instead, I usually use replacement rules if I need to specify certain values of the parameters, and I can still define a function (using Set, not SetDelayed) whenever I deliberately want to consider the parameter dependence of a quantity, as in
normplot[b_] = Norm[model1B];
Plot[normplot[b], {b, 1, 5}]
But this becomes a problem when I want to improve my code:
- I want to organise my code a bit and not use symbol names for everything. In the example above, I have two models, model 1 and model 2, and it would be better to use something like an association to store the matrices, such that I can just write
models["model 1"]["B"]
. - Included in this is the question of how to treat the left-hand side symbols, that is, whether to use symbols or something else for A and B (above, I just appended "A" and "B" to "model1" and "model2".)
- Most importantly, I want to avoid relying on undefined global symbols
phi
,t
,b
,delta
for the parameters.
How can I avoid the use of undefined global symbols for the quantities and parameters? I'm looking for a solution for how to store and implement
- "model 1" and "model 2"
- "A" and "B"
- "phi", "t", "b" and "delta".
There have been several other questions and answers on parts of this topic, but they usually only cover one part of the issue, and many are rather cumbersome (creating a separate package, using Options, etc.). I know that there are several constructs available to solve different parts of this problem — Downvalues, Associations, optional arguments, Contexts, packages (too much here!) and probably more. I think here it's important to consider the situation as a whole: how to best implement parameters on the right-hand side will very much depend on which of these constructs are most useful.