1
$\begingroup$

I would like to override the behavior of Dot, so that Dot[u,v] will return a scalar instead of a 1x1 matrix in the case that the result is 1x1. The behavior I want is given by CircleDot defined below.

CircleDot[u_, v_] := u.v /. {{{a_}} -> a};

I have tried experimenting with SetOptions and I have studied here. Any help you could provide would be most welcome.

As an example, consider

{{1,2,3}}.Transpose[{{1,2,3}}]

The result is {{14}}. Whereas

CircleDot[{{1,2,3}},Transpose[{{1,2,3}]]

Gives 14

I want to work with row and column vectors i.e. 1xn and nx1 matrices.

$\endgroup$
6
  • 2
    $\begingroup$ I'm confused. If I do Dot[{1}, {2}], MMA returns 2. $\endgroup$ – march Aug 25 '15 at 15:59
  • $\begingroup$ Unprotect[Dot] redefine Dot Protect[Dot]? Please provide an example of the vectors. $\endgroup$ – rhermans Aug 25 '15 at 15:59
  • 1
    $\begingroup$ I have to think a simple example would help clarify this question. $\endgroup$ – chuy Aug 25 '15 at 16:02
  • 3
    $\begingroup$ John, if I write your example as Dot[{1, 2, 3}, {1, 2, 3}], it works fine, and the result is already 14. "Because of the way the Wolfram Language uses lists to represent vectors and matrices, you never have to distinguish between "row" and "column" vectors." (link to docs). Wouldn't that be acceptable in your case? $\endgroup$ – MarcoB Aug 25 '15 at 16:35
  • $\begingroup$ I understand that Mathematica treats lists a vectors. I want to work with standard notation using row and column vectors for in-class examples. $\endgroup$ – John McGee Aug 25 '15 at 16:41
5
$\begingroup$

If you want to do the Unprotect thing, take care to restrict your mod to the specific case:

Unprotect[Dot];
Dot[a_List, b_List] /; 
    Dimensions[a][[1]] == 1 &&
       Dimensions[a] == Reverse@Dimensions[b] :=
         Total@Thread@Times[First@a, First/@b]
Protect[Dot];
x = {{1, 2, 3}};
y = {{4}, {5}, {6}};
Row[ { MatrixForm[x] MatrixForm[y], " = " , x.y}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks, this approach will work for me. $\endgroup$ – John McGee Aug 28 '15 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.