ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 11 Jan 2024 03:20:57 +0100Tutorial example matrix multiplicationhttps://ask.sagemath.org/question/75426/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'avanceWed, 10 Jan 2024 17:33:37 +0100https://ask.sagemath.org/question/75426/tutorial-example-matrix-multiplication/Comment by Max Alekseyev for <p>Hi,
I'm looking at the tutorial linear algebra:</p>
<p>We have </p>
<pre><code>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)
</code></pre>
<p>How does the multiplication work for A*w ? </p>
<p>We have a 3x3 x 1x3 ... and we get a vector 3x 1 ?</p>
<p>But this is not conformable... so what does A*w mean in sage ?</p>
<p>Merci à l'avance</p>
https://ask.sagemath.org/question/75426/tutorial-example-matrix-multiplication/?comment=75427#post-id-75427For `A*w` $w$ is considered as a column-vector; for `w*A` it is considered as a row-vector.Wed, 10 Jan 2024 17:54:25 +0100https://ask.sagemath.org/question/75426/tutorial-example-matrix-multiplication/?comment=75427#post-id-75427Answer by Emmanuel Charpentier for <p>Hi,
I'm looking at the tutorial linear algebra:</p>
<p>We have </p>
<pre><code>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)
</code></pre>
<p>How does the multiplication work for A*w ? </p>
<p>We have a 3x3 x 1x3 ... and we get a vector 3x 1 ?</p>
<p>But this is not conformable... so what does A*w mean in sage ?</p>
<p>Merci à l'avance</p>
https://ask.sagemath.org/question/75426/tutorial-example-matrix-multiplication/?answer=75431#post-id-75431Another 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,Wed, 10 Jan 2024 23:20:23 +0100https://ask.sagemath.org/question/75426/tutorial-example-matrix-multiplication/?answer=75431#post-id-75431Comment by PatB for <p>Another way to understand <a href="/users/26682/max-alekseyev/">@Max Alekseyev</a> 's answer : </p>
<p>A vector is a vector is a vector, <em>id est</em> a rank 1 object (object having <em>one</em> 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 <em>two</em> dimensions.</p>
<blockquote>
<p>(P.S. - that may explain why the vector object does not support the transpose operator ?)</p>
</blockquote>
<p>Indeed : "transpose" means "swap dimensions", which is meaningful for rank 2 objects and meaningless for rank 1 objects...</p>
<p><strong>Conformability :</strong> V is conformable <em>at the left</em> of M if its length is equal to, the number of <em>columns</em> of M ; it is conformable <em>at the right</em> of M if its length is equal to, the number of <em>rows</em> of M. Illustration :</p>
<pre><code>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'
</code></pre>
<p><strong>Note :</strong>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...</p>
<p>HTH,</p>
https://ask.sagemath.org/question/75426/tutorial-example-matrix-multiplication/?comment=75434#post-id-75434**Merci Emmanuel pour vos précisons!
C'est bien expliqué!**
PatThu, 11 Jan 2024 03:20:57 +0100https://ask.sagemath.org/question/75426/tutorial-example-matrix-multiplication/?comment=75434#post-id-75434Answer by PatB for <p>Hi,
I'm looking at the tutorial linear algebra:</p>
<p>We have </p>
<pre><code>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)
</code></pre>
<p>How does the multiplication work for A*w ? </p>
<p>We have a 3x3 x 1x3 ... and we get a vector 3x 1 ?</p>
<p>But this is not conformable... so what does A*w mean in sage ?</p>
<p>Merci à l'avance</p>
https://ask.sagemath.org/question/75426/tutorial-example-matrix-multiplication/?answer=75428#post-id-75428Thanks 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 ?)Wed, 10 Jan 2024 18:40:38 +0100https://ask.sagemath.org/question/75426/tutorial-example-matrix-multiplication/?answer=75428#post-id-75428