[Python-ideas] [RFC] draft PEP: Dedicated infix operators for matrix multiplication and matrix power

Alexander Belopolsky alexander.belopolsky at gmail.com
Sat Mar 15 21:50:28 CET 2014


On Sat, Mar 15, 2014 at 11:11 AM, Steven D'Aprano <steve at pearwood.info>wrote:

> The only question is whether it is more common to write:
>
>     Matrix @ Matrix @ Column_Vector
>
> or
>
>     Row_Vector @ Matrix @ Matrix
>
>
> I'll leave it to those who do matrix maths to decide which they use more
> often, but personally I've never come across the second case except in
> schoolbook exercises.
>

Abstractly, 1-dimensional arrays are neither columns nor rows, but Python's
horizontal notation makes them more row-like than column-like.  In
2-dimensional case, [[1,2]] is a row-vector and [[1],[2]] is a
column-vector.  Which one is more "natural"?

When you have a matrix

A = [[1, 2],
     [3, 4]]

A[1] is [3, 4], which is a row.  To get a column, [2, 4], one has to write
A[:,1] in numpy.

When it comes to matrix - vector multiplication,

[1, 2] @ [[1, 2],
          [3, 4]] -> [7, 10]

has a text-book appearance, while

[[1, 2],
 [3, 4]] @ [1, 2] -> [5, 11]

has to be mentally cast into

([[1, 2],
  [3, 4]] @ [[1],
             [2]])[0] -> [5, 11]

While it is more common in math literature to see Mat @ vec than vec @ Mat,
I don't think anyone who has completed an introductory linear algebra
course would have trouble understanding what [1, 2, 3] @ Mat means.  On the
other hand, novice programmers may find it puzzling why Mat @ [Mat1, Mat2]
is the same as [Mat @ Mat1, Mat @ Mat2], but [Mat @ [vec1, vec2]] is not
[Mat @ vec1, Mat @ vec2].
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140315/67daccb1/attachment.html>


More information about the Python-ideas mailing list