I am trying to slightly modify the Dot
function by treating it like a usual function, but soon I notice something weird about it.
Suppose there is a set set = {λ}
, and I want to force Dot[a, λ]
(having two arguments) to just print out {a, λ}
.
set = {λ}; Unprotect[Dot]; Clear[Dot] Dot[x_, λ_] := Module[{}, Print[{x, λ}]] /; MemberQ[set, λ] Unprotect[Dot]; (* test *) Dot[a, λ] Dot[a, b, λ]
The two tests print out
{a,λ}
and
{b, λ} {a.b, λ}
respectively.
However, the same code for a different function Dott
behave completly differently,
set = {λ}; Dott[x_, λ_] := Module[{}, Print[{x, λ}]] /; MemberQ[set, λ] (* tests *) Dott[a, λ] Dott[a, a, λ]
where the two tests print out {a,λ}
and an unevaluated expression Dott[a, a, λ]
respectively, and as expected.
I wonder what is going on under the hood (it appears that Dot
is trying to exhaust all possible "two-factor product" given any number of dotted factors), and how to properly temper with the Dot
product? (my ultimate goal is to adapt it for some non-commutative algebra).