# Tutorial example matrix multiplication

Hi, I'm looking at the tutorial linear algebra:

We have

sage: A = Matrix([[1,2,3],[3,2,1],[1,1,1]])
sage: w = vector([1,1,-4])
sage: w*A
(0, 0, 0)
sage: A*w
(-9, 1, -2)


How does the multiplication work for A*w ?

We have a 3x3 x 1x3 ... and we get a vector 3x 1 ?

But this is not conformable... so what does A*w mean in sage ?

Merci à l'avance

edit retag close merge delete

For A*w $w$ is considered as a column-vector; for w*A it is considered as a row-vector.

( 2024-01-10 17:54:25 +0200 )edit

Sort by » oldest newest most voted

Thanks Max,

So the answer is: A*w is well defined because the vector w assumes the column form to complete the operation: 3x3 x 3x1 --> 3x1

In other words, it seems that when we define an object of type vector, it can assume column or row dimensions depending on the operation ?

(P.S. - that may explain why the vector object does not support the transpose operator ?)

more

Another way to understand @Max Alekseyev 's answer :

A vector is a vector is a vector, id est a rank 1 object (object having one dimension, its length). The so called "row" and "column" vectors are a delusion (or hallucination) imposed upon american undergraduates. Rank 2 objects are matrices, which have two dimensions.

(P.S. - that may explain why the vector object does not support the transpose operator ?)

Indeed : "transpose" means "swap dimensions", which is meaningful for rank 2 objects and meaningless for rank 1 objects...

Conformability : V is conformable at the left of M if its length is equal to, the number of columns of M ; it is conformable at the right of M if its length is equal to, the number of rows of M. Illustration :

sage: V=vector(var("v", n=3)) ; V
(v0, v1, v2)
sage: W=vector(var("w", n=2)) ; W
(w0, w1)
sage: M=matrix(var("m", n=6), ncols=2, nrows=3) ; M
[m0 m1]
[m2 m3]
[m4 m5]
sage: V*M # Conformant
(m0*v0 + m2*v1 + m4*v2, m1*v0 + m3*v1 + m5*v2)
sage: M*W # Also conformant
(m0*w0 + m1*w1, m2*w0 + m3*w1, m4*w0 + m5*w1)
sage: M*V # Non confomant : this raise a TypeError
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[27], line 1
----> 1 M*V

File /usr/local/sage-10/src/sage/structure/element.pyx:4099, in sage.structure.element.Matrix.__mul__()
4097
4098         if BOTH_ARE_ELEMENT(cl):
-> 4099             return coercion_model.bin_op(left, right, mul)
4100
4101         cdef long value

File /usr/local/sage-10/src/sage/structure/coerce.pyx:1278, in sage.structure.coerce.CoercionModel.bin_op()
1276     # We should really include the underlying error.
1277     # This causes so much headache.
-> 1278     raise bin_op_exception(op, x, y)
1279
1280 cpdef canonical_coercion(self, x, y) noexcept:

TypeError: unsupported operand parent(s) for *: 'Full MatrixSpace of 3 by 2 dense matrices over Symbolic Ring' and 'Vector space of dimension 3 over Symbolic Ring'
sage: W*M # Also nonconformant ; ditto...
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[28], line 1
----> 1 W*M

File /usr/local/sage-10/src/sage/structure/element.pyx:3685, in sage.structure.element.Vector.__mul__()
3683     if have_same_parent(left, right):
3684         return (<Vector>left)._dot_product_(<Vector>right)
-> 3685     return coercion_model.bin_op(left, right, mul)
3686
3687 cpdef _dot_product_(Vector left, Vector right) noexcept:

File /usr/local/sage-10/src/sage/structure/coerce.pyx:1278, in sage.structure.coerce.CoercionModel.bin_op()
1276     # We should really include the underlying error.
1277     # This causes so much headache.
-> 1278     raise bin_op_exception(op, x, y)
1279
1280 cpdef canonical_coercion(self, x, y) noexcept:

TypeError: unsupported operand parent(s) for *: 'Vector space of dimension 2 over Symbolic Ring' and 'Full MatrixSpace of 3 by 2 dense matrices over Symbolic Ring'


Note :I never encountered the distinction during my initial education (in France, at a time where vectors were informally introduced in 9th grade (to allow their use in physics starting in 10t grade), and formally studied (along with elementary matrix algebra) in 11th grade). I met this silly distinction when reading american textbooks...

HTH,

more

Merci Emmanuel pour vos précisons! C'est bien expliqué! Pat

( 2024-01-11 03:20:57 +0200 )edit