Here's my basic setup:
Say I have a list of specific symbols and a function noSymbol
, which accepts a mathematica expression and checks whether any symbol from my list is contained in it, for example:
symbols = {basisElement};
noSymbol[x_] := And @@ ( Length[Position[x, #]] === 0 & /@ symbols );
An examle use-case could be that basisElement
is a function enumerating basis vectors of some n
-dimensional algebra, so that I could define the multiplication of the algebra as
mult[basisElement[i_], basisElement[j_]] := Sum[strucConst[i, j, k] * basisElement[k], {k, 1, n}];
with structConst
the structure constants of the algebra.
Of course in this situation it is natural to define linear functions, and what I would often find myself doing is
Clear[f];
f[0] := 0;
f[v_ + w_] := f[v] + f[w];
f[a_ * v_] := a * f[v] /; noSymbol[a];
f[v_ * a_] := a * f[v] /; noSymbol[a];
(* actual function definition *)
at the beginning of every definition of a linear function.
This adds a lot of repetition to my code, so I wanted to ask if using a "linearizing function" like
linearize[f_] := (
f[0] := 0;
f[v_ + w_] := f[v] + f[w];
f[a_ * v_] := a * f[v] /; noSymbol[a];
f[v_ * a_] := a * f[v] /; noSymbol[a];
);
Clear[f];
linearize[f];
(* actual function definition *)
is considered good practices and makes sense, or if it could lead to any serious issues.
EDIT:
Additionally, if this is good practice, how can I extend my linearize
function so that it can "multilinearize"? For example, my multiplication from above would have to be linear in each argument.