Skip to main content

replacement - Best practice of passing a large number of parameters to functions


I have a number of functions that all take a large number of parameters. I am wondering what is the best practice of passing these parameters to those functions. I could, of course, simply specify the parameters outside the functions, as in (note that, in the actual example, there are far more parameters)



mu=1;
sigma=1;
lb=0;
ub=10;
f[x_] := PDF[LogNormalDistribution[mu, sigma],x]

However, I would prefer to explicitly pass the parameters to the functions. In Python, I would use a dictionary. In Mathematica, one possibility would be a replacement rule.


par={mu->1,sigma->1,lb->0,ub->10};
f[x_,par_] := PDF[LogNormalDistribution[mu, sigma],x]/.par


However, this can cause warnings if a function only takes numerical arguments, e.g.


Plot[f[x,par],{x,lb,ub}]/.par


Plot::plln: Limiting value lb in {x,lb,ub} is not a machine-sized real number. >>



The plotting actually, works, though.


Also, passing parameters using replacement rules seems to be inefficient, since - if possible - evaluations are done symbolically, and only then are values substituted for variables.




Comments