ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 15 Aug 2013 03:09:59 -0500append a variable to a vectorhttp://ask.sagemath.org/question/10436/append-a-variable-to-a-vector/I would like to define a new vector in Sage $\vec{u} = [O, P, \sigma]$ using a previously defined vecotor $\vec{x} = [O, P]$ and a new variable $\sigma$.
My code looks like:
> var(O, P');
>
> var('sigma');
>
> x = vector([O,P]);
>
> u = vector([x, sigma]);
Which gives an error:
> TypeError: unable to find a common ring for all elements
Apparently this is because vector() requires the variables to be of the same ring. In my case
> x.parent()
>
> Vector space of dimension 2 over Symbolic Ring
and
> sigma.parent()
> Symbolic Ring
There is any way how to connect this two objects ($\vec{x}$, $\sigma$) together to create one vector?Wed, 14 Aug 2013 23:23:40 -0500http://ask.sagemath.org/question/10436/append-a-variable-to-a-vector/Answer by Tomas for <p>I would like to define a new vector in Sage $\vec{u} = [O, P, \sigma]$ using a previously defined vecotor $\vec{x} = [O, P]$ and a new variable $\sigma$.</p>
<p>My code looks like:</p>
<blockquote>
<p>var(O, P');</p>
<p>var('sigma');</p>
<p>x = vector([O,P]);</p>
<p>u = vector([x, sigma]);</p>
</blockquote>
<p>Which gives an error:</p>
<blockquote>
<p>TypeError: unable to find a common ring for all elements</p>
</blockquote>
<p>Apparently this is because vector() requires the variables to be of the same ring. In my case </p>
<blockquote>
<p>x.parent()</p>
<p>Vector space of dimension 2 over Symbolic Ring</p>
</blockquote>
<p>and</p>
<blockquote>
<p>sigma.parent()
Symbolic Ring</p>
</blockquote>
<p>There is any way how to connect this two objects ($\vec{x}$, $\sigma$) together to create one vector?</p>
http://ask.sagemath.org/question/10436/append-a-variable-to-a-vector/?answer=15348#post-id-15348So an solution which work is to convert the vector to a list, append the new variable and convert to a vector again. The code for that would be as follows:
> u = x.list()
>
> u.append(sigma)
>
> u = vector(u)
Do you see any potential problems with this solution? Why there is no method append in vector? Could I do better?Thu, 15 Aug 2013 01:36:41 -0500http://ask.sagemath.org/question/10436/append-a-variable-to-a-vector/?answer=15348#post-id-15348Comment by kcrisman for <p>So an solution which work is to convert the vector to a list, append the new variable and convert to a vector again. The code for that would be as follows:</p>
<blockquote>
<p>u = x.list()</p>
<p>u.append(sigma)</p>
<p>u = vector(u)</p>
</blockquote>
<p>Do you see any potential problems with this solution? Why there is no method append in vector? Could I do better?</p>
http://ask.sagemath.org/question/10436/append-a-variable-to-a-vector/?comment=17160#post-id-17160The short answer to your question about vector is that a vector in Sage is not the same as a "vector" in other languages. Python lists correspond to that notion. A vector in Sage is a *mathematical* object, and there is no one way to make a (say) 3-d vector a 4-d vector. One could indeed add a dimension, but where? It would be (mathematically) just as natural to do it 'in front' as 'in back'. Your solution is actually a good one, and has the advantage that if your original vectors live in two different spaces (say, over QQ and RR) then your new vector will still make sense.Thu, 15 Aug 2013 03:09:59 -0500http://ask.sagemath.org/question/10436/append-a-variable-to-a-vector/?comment=17160#post-id-17160