I would like to elaborate on my previous question Function of arguments given in non-fixed order and a relevant related one Best practice of passing a large number of parameters to functions
I find that neither Options
nor Association
are perfect for what I need to do here, I am hoping to find some hint to a better solution here.
I have to handle a large set of data that are essentially the results of the evaluation of a very time-consuming function on some points and that now I want to store. What I want to achieve is a quick way to recall the results that I have stored and a quick way to store them when I evaluate the function.
Ideally I would like to achieve a point where I can do
MyStorage[lot of parameters]=LengthyFunction[lot of parameters]
so that I can recall the results as
MyStorage[lot of parameters]
If the list of parameters is small this is not a problem, you just use the two lines above. But when the list of parameters gets long it comes the trouble, because you need to put lot of parameters
in the same order as the function is defined. This also comes with the additional difficulty that MyStorage[2,3,5]
completely hides to you the meaning of the entries in the arguments sequence.
So I thought it was best to use something like MyStorage[{x->2,y->3,z->5}]
, which makes very explicit at what point you are calling the function. This, however, is not a solution, because the order of the arguments still matters. In fact defining MyStorage[{x -> 2, y -> 3, z -> 5}] = {a, d, b}
then you can recall the stored value only by MyStorage[{x -> 2, y -> 3, z -> 5}]
and not by MyStorage[{y -> 3, z -> 5, x -> 2}]
Using associations does not help either, because one runs in the exact same issue of ordering. So I am left with Options
as the sole Orderless
way to go. Unfortunately Options
cannot be used directly in this example, unless I have missed something. The shortest way to use Options
that I can think of is to use a "Put" and a "Get" function, that take Options as inputs and then do the job using an ordinary "ordered" function for storage of the type MyStorage[2,3,5]
.
MyStoragePut[opt : OptionsPattern[], value_] := Module[{point},
point = {x, y, z} /. opt;
MyStorage[Sequence[point]] = value;
]
at this point you can fill in the storage without recalling the order of the variables. Indeed MyStoragePut[{y -> 3, z -> 99, x -> 2}, {2, 3, 34}]
followed by MyStoragePut[{x -> 2, y -> 3, z -> 99}, {2, 23, 34}]
would replace if the value at {x -> 2, y -> 3, z -> 99}
.
To retrieve the values I define the "Get" method
MyStorageGet[opt : OptionsPattern[]] := Module[{point},
point = {x, y, z} /. opt;
MyStorage[Sequence[point]]
]
which also works regardless of the order in which the values are specified: MyStorageGet[{x -> 2, y -> 3, z -> 99}]
and MyStorageGet[{x -> 2, z -> 99, y -> 3}]
give the same result stored in the symbol MyStorage[2,3,99]
.
I wanted to ask if this is a dumb way of reinventing a wheel that is already (better) implemented in Mathematica or there are anyways better strategies to achieve this orderless data storage and retrieval.
Thanks for your inputs, Roberto
Association
I should say that they are useful to take a long list of Options, and change the value of one of the options and obtain back a list of options. Say by doingChangeOptionValue[mykey_, Val_, BaseParameters_] := Module[{myass = (Association[Sequence[BaseParameters]])}, myass[mykey] = Val; Normal[myass] ]
$\endgroup$