I am trying to define a function that takes in a Matrix and when its dimensions are not provided as input, compute these dimensions in the optional parameter d
This does not work but gives you the idea (The options parameter need be constants):
Options[DimM] = {d -> Dimensions[A]};
DimM[A_?MatrixQ, OptionsPattern[]] := OptionValue@d;
Indeed the simple way is to input an impossible value and in the function def put an if condition as in
Options[DimM] = {d -> 0};
DimM[A_?MatrixQ, OptionsPattern[]] :=If[OptionValue@d==0,Dimensions[A],OptionValue@d]
How can I accomplish this most efficiently?
For your original formulation, @WReach gave a fine answer. However, it may make sense to reconsider your design a bit: note that you have a (dependent on input arguments) value for
d
in any case. Optional arguments are designed exactly for that - to be optional. In your case, a default argument seems more appropriate. You can set it up withAutomatic
, similarly to what @WReach suggested:To use this in more than one place in your code, you will however need to introduce an auxiliary variable or constant (using
With
orModule
), to store this value. As an alternative, you can also use the following code:which has the advantage that you can use the same original parameter
d
everywhere in the body of your function. What happens here is rather non-trivial:Module
is used to generate a unique symbol, which is then given as a default ford
and used to dynamically compute the dimensions. Note thatBlock
localizes not the symboldims
, but the unique symbol likedims$77542
produced byModule
. This combination ofModule
andBlock
makes this technique completely safe. Examples of use:I think this combination of
Module
andBlock
is an interesting technique which may find other uses. Essentially, it is a version of dynamic scoping made safe by lexical scoping (or, more precisely, its imitation in Mathematica) - since one of the main dangers of dynamic scoping is inadvertent collisions of dynamically localized symbols with the same name.On an unrelated matter - it is best to not start your variables and functions with a capital letter, since they may collide with the system symbols.
This is not really an improvement over your "simple way", but for many built-in Mathematica functions the symbol
Automatic
is used as the "impossible value". For example: