ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 17 Feb 2014 08:40:08 -0600Why cant I multiply two vectors together on sage cell?https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/Hello all. Sage noob here. My friend recently told me about sage and convinced me to give it a whirl. (My background is MATLAB).
I made an account, and went to sage cell for a quick run. I typed this in:
v = vector([3, 4]);
vC = v.column();
vC*v
I would like to simply get the outer product of those two vectors. However it errors out.
I would like to think that I am boneheading something here, because this is a legitimate operation. I have multiple tutorial/documentation tabs open but nothing really elucidating why/how this doesnt work.
Would appreciate any insight.
Mon, 17 Feb 2014 05:11:18 -0600https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/Answer by slelievre for <p>Hello all. Sage noob here. My friend recently told me about sage and convinced me to give it a whirl. (My background is MATLAB). </p>
<p>I made an account, and went to sage cell for a quick run. I typed this in:</p>
<pre><code>v = vector([3, 4]);
vC = v.column();
vC*v
</code></pre>
<p>I would like to simply get the outer product of those two vectors. However it errors out. </p>
<p>I would like to think that I am boneheading something here, because this is a legitimate operation. I have multiple tutorial/documentation tabs open but nothing really elucidating why/how this doesnt work. </p>
<p>Would appreciate any insight. </p>
https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/?answer=16062#post-id-16062How about doing this:
sage: v = vector([3, 4])
sage: vc = v.column()
sage: vr = v.row()
sage: vr * vc
[25]
sage: vc * vr
[ 9 12]
[12 16]
Mon, 17 Feb 2014 07:39:36 -0600https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/?answer=16062#post-id-16062Comment by Gravitus for <p>How about doing this:</p>
<pre><code>sage: v = vector([3, 4])
sage: vc = v.column()
sage: vr = v.row()
sage: vr * vc
[25]
sage: vc * vr
[ 9 12]
[12 16]
</code></pre>
https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/?comment=16244#post-id-16244That makes sense slelievre, but I am still confused as to why I can use "v" by itself. Why can I do v*A if A was a 2x2 matrix without having to tell it that v is a row vector? Mon, 17 Feb 2014 08:33:55 -0600https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/?comment=16244#post-id-16244Answer by tmonteil for <p>Hello all. Sage noob here. My friend recently told me about sage and convinced me to give it a whirl. (My background is MATLAB). </p>
<p>I made an account, and went to sage cell for a quick run. I typed this in:</p>
<pre><code>v = vector([3, 4]);
vC = v.column();
vC*v
</code></pre>
<p>I would like to simply get the outer product of those two vectors. However it errors out. </p>
<p>I would like to think that I am boneheading something here, because this is a legitimate operation. I have multiple tutorial/documentation tabs open but nothing really elucidating why/how this doesnt work. </p>
<p>Would appreciate any insight. </p>
https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/?answer=16057#post-id-16057When multiplied to the right, a vector behaves like a column matrix, when multiplied to the left, a vector behaves like a row matrix. So your product is like multipliying a $2\times 1$ by another $2\times 1$ matrix, which is invalid.
So, if you want v to be understood as a row matrix, you have to convert it as follows:
sage: Matrix(v)
[3 4]
Then, your product becomes valid:
sage: vC * Matrix(v)
[ 9 12]
[12 16]
Note that you could work with matrices from the beginning:
sage: v = Matrix([[3, 4]]) ; v
[3 4]
sage: vC = v.transpose() ; vC
[3]
[4]
sage: vC * v
[ 9 12]
[12 16]
Mon, 17 Feb 2014 05:44:54 -0600https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/?answer=16057#post-id-16057Comment by Gravitus for <p>When multiplied to the right, a vector behaves like a column matrix, when multiplied to the left, a vector behaves like a row matrix. So your product is like multipliying a $2\times 1$ by another $2\times 1$ matrix, which is invalid.</p>
<p>So, if you want v to be understood as a row matrix, you have to convert it as follows:</p>
<pre><code>sage: Matrix(v)
[3 4]
</code></pre>
<p>Then, your product becomes valid:</p>
<pre><code>sage: vC * Matrix(v)
[ 9 12]
[12 16]
</code></pre>
<p>Note that you could work with matrices from the beginning:</p>
<pre><code>sage: v = Matrix([[3, 4]]) ; v
[3 4]
sage: vC = v.transpose() ; vC
[3]
[4]
sage: vC * v
[ 9 12]
[12 16]
</code></pre>
https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/?comment=16243#post-id-16243OK, I guess that makes sense... thanks!Mon, 17 Feb 2014 08:40:08 -0600https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/?comment=16243#post-id-16243Comment by tmonteil for <p>When multiplied to the right, a vector behaves like a column matrix, when multiplied to the left, a vector behaves like a row matrix. So your product is like multipliying a $2\times 1$ by another $2\times 1$ matrix, which is invalid.</p>
<p>So, if you want v to be understood as a row matrix, you have to convert it as follows:</p>
<pre><code>sage: Matrix(v)
[3 4]
</code></pre>
<p>Then, your product becomes valid:</p>
<pre><code>sage: vC * Matrix(v)
[ 9 12]
[12 16]
</code></pre>
<p>Note that you could work with matrices from the beginning:</p>
<pre><code>sage: v = Matrix([[3, 4]]) ; v
[3 4]
sage: vC = v.transpose() ; vC
[3]
[4]
sage: vC * v
[ 9 12]
[12 16]
</code></pre>
https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/?comment=16245#post-id-16245I updated the beginning of my answer to explain this. You can check that if `vC` is a `2*2` matrix, then both `v*vC` and `vC*v` make senseMon, 17 Feb 2014 07:43:46 -0600https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/?comment=16245#post-id-16245Comment by Gravitus for <p>When multiplied to the right, a vector behaves like a column matrix, when multiplied to the left, a vector behaves like a row matrix. So your product is like multipliying a $2\times 1$ by another $2\times 1$ matrix, which is invalid.</p>
<p>So, if you want v to be understood as a row matrix, you have to convert it as follows:</p>
<pre><code>sage: Matrix(v)
[3 4]
</code></pre>
<p>Then, your product becomes valid:</p>
<pre><code>sage: vC * Matrix(v)
[ 9 12]
[12 16]
</code></pre>
<p>Note that you could work with matrices from the beginning:</p>
<pre><code>sage: v = Matrix([[3, 4]]) ; v
[3 4]
sage: vC = v.transpose() ; vC
[3]
[4]
sage: vC * v
[ 9 12]
[12 16]
</code></pre>
https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/?comment=16247#post-id-16247Thanks tmonteil, however I am more confused now. If I try v*vC it works just find, but vC*v doesnt work - why the discrepancy? That is, why does it complain about the 'type' of data structure in one case, but does not complain about it in the other?
Another thing: I tried to multiply v with a matrix A. It seems that it 'knows' how to interpret v regardless of which side I put it around A. Yet it does not know how to interpret v with A is substituted for vC?
Not complaining, just trying to understand how it thinks. Thanks.Mon, 17 Feb 2014 06:09:33 -0600https://ask.sagemath.org/question/11049/why-cant-i-multiply-two-vectors-together-on-sage-cell/?comment=16247#post-id-16247