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 with Automatic
, similarly to what @WReach suggested:
dimMAuto[a_?MatrixQ, d_: Automatic] :=
If[d === Automatic, Dimensions[a], d];
To use this in more than one place in your code, you will however need to introduce an auxiliary variable or constant (using With
or Module
), to store this value. As an alternative, you can also use the following code:
Module[{dims},
dimM[a_?MatrixQ, d_: dims] :=
Block[{dims = Dimensions[a]},
d]
]
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 for d
and used to dynamically compute the dimensions. Note that Block
localizes not the symbol dims
, but the unique symbol like dims$77542
produced by Module
. This combination of Module
and Block
makes this technique completely safe. Examples of use:
In[1466]:= dimM[IdentityMatrix[3],{1,1}]
Out[1466]= {1,1}
In[1467]:= dimM[IdentityMatrix[3]]
Out[1467]= {3,3}
I think this combination of Module
and Block
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.