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, 26 Aug 2013 04:45:48 -0500vector division?https://ask.sagemath.org/question/10451/vector-division/given a vector in sage
sage: a,b=var('a,b')
sage: sv=vector(SR,[1,a,b^2])
then why is this a meaningful operation
sage: sv/sv
1
To my (utterly humble) understanding, I always thought, that if vector multiplication '*' is defined via the scalar product - as it seems to be in sage
sage: sv*sv
a^2 + b^4 + 1
then, division cannot be defined meaningfully?
[Just as a sideremark along that line: I have no problem with numpy's element wise array-arithmetic
In [1]: v=array([1.,2.,3.]) # vector
In [2]: e=array([1.,1.,1.]) # unity is a vector
In [3]: e*v == v # multiplication by unity
Out[4]: array([ True, True, True], dtype=bool)
In [5]: vi=v**(-1) # inverse is a vector
In [6]: e/v == vi # unity/vector == inverse
Out[7]: array([ True, True, True], dtype=bool)
In [8]: e == v*vi # vector * inverse == unity
Out[9]: array([ True, True, True], dtype=bool)
In my layman's world, this type of division makes perfect sense. (Moreover I'm curious why sage seems to not adopt this pythonic way of array arithmetic)]Sat, 17 Aug 2013 22:18:06 -0500https://ask.sagemath.org/question/10451/vector-division/Comment by Luca for <p>given a vector in sage</p>
<pre><code>sage: a,b=var('a,b')
sage: sv=vector(SR,[1,a,b^2])
</code></pre>
<p>then why is this a meaningful operation</p>
<pre><code>sage: sv/sv
1
</code></pre>
<p>To my (utterly humble) understanding, I always thought, that if vector multiplication '*' is defined via the scalar product - as it seems to be in sage</p>
<pre><code>sage: sv*sv
a^2 + b^4 + 1
</code></pre>
<p>then, division cannot be defined meaningfully?</p>
<p>[Just as a sideremark along that line: I have no problem with numpy's element wise array-arithmetic</p>
<pre><code>In [1]: v=array([1.,2.,3.]) # vector
In [2]: e=array([1.,1.,1.]) # unity is a vector
In [3]: e*v == v # multiplication by unity
Out[4]: array([ True, True, True], dtype=bool)
In [5]: vi=v**(-1) # inverse is a vector
In [6]: e/v == vi # unity/vector == inverse
Out[7]: array([ True, True, True], dtype=bool)
In [8]: e == v*vi # vector * inverse == unity
Out[9]: array([ True, True, True], dtype=bool)
</code></pre>
<p>In my layman's world, this type of division makes perfect sense. (Moreover I'm curious why sage seems to not adopt this pythonic way of array arithmetic)]</p>
https://ask.sagemath.org/question/10451/vector-division/?comment=17147#post-id-17147Note that `*` is not only inner product, but also multiplication by a scalar. Indeed `2*sv` returns the expected answer. I'd say that Sage strives to give an interface that is closer to the mathematical usage, rather than the "pythonic" way. Sometimes this can be surprising, as in `sv/sv`.Sun, 18 Aug 2013 01:35:15 -0500https://ask.sagemath.org/question/10451/vector-division/?comment=17147#post-id-17147Answer by Volker Braun for <p>given a vector in sage</p>
<pre><code>sage: a,b=var('a,b')
sage: sv=vector(SR,[1,a,b^2])
</code></pre>
<p>then why is this a meaningful operation</p>
<pre><code>sage: sv/sv
1
</code></pre>
<p>To my (utterly humble) understanding, I always thought, that if vector multiplication '*' is defined via the scalar product - as it seems to be in sage</p>
<pre><code>sage: sv*sv
a^2 + b^4 + 1
</code></pre>
<p>then, division cannot be defined meaningfully?</p>
<p>[Just as a sideremark along that line: I have no problem with numpy's element wise array-arithmetic</p>
<pre><code>In [1]: v=array([1.,2.,3.]) # vector
In [2]: e=array([1.,1.,1.]) # unity is a vector
In [3]: e*v == v # multiplication by unity
Out[4]: array([ True, True, True], dtype=bool)
In [5]: vi=v**(-1) # inverse is a vector
In [6]: e/v == vi # unity/vector == inverse
Out[7]: array([ True, True, True], dtype=bool)
In [8]: e == v*vi # vector * inverse == unity
Out[9]: array([ True, True, True], dtype=bool)
</code></pre>
<p>In my layman's world, this type of division makes perfect sense. (Moreover I'm curious why sage seems to not adopt this pythonic way of array arithmetic)]</p>
https://ask.sagemath.org/question/10451/vector-division/?answer=15359#post-id-15359Division of two vectors is only allowed if they are a scalar multiple of each other, and the result is the ratio of their length. If not, you get an error:
sage: vector(QQ, [1,2,3]) / vector(QQ, [2,4,6])
1/2
sage: vector(QQ, [1,2,3]) / vector(QQ, [1,1,1])
Traceback (most recent call last)
...
ArithmeticError: vector is not in free module
The ratio is in the same base ring as the vectors, so if you have vectors over `QQ` then the ratio is rational, if you have vectors over `SR` then the ratio is again an element of the symbolic ring.Sun, 18 Aug 2013 00:15:09 -0500https://ask.sagemath.org/question/10451/vector-division/?answer=15359#post-id-15359Comment by Mark for <p>Division of two vectors is only allowed if they are a scalar multiple of each other, and the result is the ratio of their length. If not, you get an error:</p>
<pre><code>sage: vector(QQ, [1,2,3]) / vector(QQ, [2,4,6])
1/2
sage: vector(QQ, [1,2,3]) / vector(QQ, [1,1,1])
Traceback (most recent call last)
...
ArithmeticError: vector is not in free module
</code></pre>
<p>The ratio is in the same base ring as the vectors, so if you have vectors over <code>QQ</code> then the ratio is rational, if you have vectors over <code>SR</code> then the ratio is again an element of the symbolic ring.</p>
https://ask.sagemath.org/question/10451/vector-division/?comment=17148#post-id-17148Ok., this makes (some) sense. Still, would you mind educating me on if this is just syntactic candy, or rather something absolutely standard that I should find in my high school linear algebra textbooks.Sun, 18 Aug 2013 01:09:01 -0500https://ask.sagemath.org/question/10451/vector-division/?comment=17148#post-id-17148Answer by ndomes for <p>given a vector in sage</p>
<pre><code>sage: a,b=var('a,b')
sage: sv=vector(SR,[1,a,b^2])
</code></pre>
<p>then why is this a meaningful operation</p>
<pre><code>sage: sv/sv
1
</code></pre>
<p>To my (utterly humble) understanding, I always thought, that if vector multiplication '*' is defined via the scalar product - as it seems to be in sage</p>
<pre><code>sage: sv*sv
a^2 + b^4 + 1
</code></pre>
<p>then, division cannot be defined meaningfully?</p>
<p>[Just as a sideremark along that line: I have no problem with numpy's element wise array-arithmetic</p>
<pre><code>In [1]: v=array([1.,2.,3.]) # vector
In [2]: e=array([1.,1.,1.]) # unity is a vector
In [3]: e*v == v # multiplication by unity
Out[4]: array([ True, True, True], dtype=bool)
In [5]: vi=v**(-1) # inverse is a vector
In [6]: e/v == vi # unity/vector == inverse
Out[7]: array([ True, True, True], dtype=bool)
In [8]: e == v*vi # vector * inverse == unity
Out[9]: array([ True, True, True], dtype=bool)
</code></pre>
<p>In my layman's world, this type of division makes perfect sense. (Moreover I'm curious why sage seems to not adopt this pythonic way of array arithmetic)]</p>
https://ask.sagemath.org/question/10451/vector-division/?answer=15358#post-id-15358It is funny.
1/sv
gives the expected answer:
...
TypeError: unsupported operand parent(s) for '/': 'Integer Ring' and
'Vector space of dimension 3 over Symbolic Ring'
The vector sv has a division operator,
which makes sense if interpreted as scalar multiplication:
sv.__div__(2)
(1/2, 1/2*a, 1/2*b^2)
The expression sv/2 is element of a vector space:
(sv/2).parent()
Vector space of dimension 3 over Symbolic Ring
As far nothing to complain.
I don't know why, but the expression sv/sv is astonishingly an element of the Symbolic Ring:
(sv/sv).parent()
Symbolic Ring
Sat, 17 Aug 2013 23:35:58 -0500https://ask.sagemath.org/question/10451/vector-division/?answer=15358#post-id-15358Comment by vdelecroix for <p>It is funny.</p>
<pre><code>1/sv
</code></pre>
<p>gives the expected answer:</p>
<pre><code>...
TypeError: unsupported operand parent(s) for '/': 'Integer Ring' and
'Vector space of dimension 3 over Symbolic Ring'
</code></pre>
<p>The vector sv has a division operator,
which makes sense if interpreted as scalar multiplication:</p>
<pre><code>sv.__div__(2)
(1/2, 1/2*a, 1/2*b^2)
</code></pre>
<p>The expression sv/2 is element of a vector space:</p>
<pre><code>(sv/2).parent()
Vector space of dimension 3 over Symbolic Ring
</code></pre>
<p>As far nothing to complain.</p>
<p>I don't know why, but the expression sv/sv is astonishingly an element of the Symbolic Ring:</p>
<pre><code>(sv/sv).parent()
Symbolic Ring
</code></pre>
https://ask.sagemath.org/question/10451/vector-division/?comment=17125#post-id-17125this is because your vector sv belongs to the symbolic ring (try sv.parent()). It is coherent that the answer belongs to the ring or field of your module or vector space.Mon, 26 Aug 2013 04:45:48 -0500https://ask.sagemath.org/question/10451/vector-division/?comment=17125#post-id-17125