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.Wed, 31 Aug 2011 11:24:49 +0200extracting the coefficients of a linear combinationhttps://ask.sagemath.org/question/8297/extracting-the-coefficients-of-a-linear-combination/Let me just say that I'm quite new to sage (although I'm making a progress!)
I wish to input a number field (in my case real quadratic field), a basis $w_1,w_2$ over $
\mathbb Q$ and an element $v$ of this number field and get the coefficient of $v$ as a linear combination of $w_1$ and $w_2$. I.e., if $v=aw_1+bw_2$ I wish to get (a,b).
I tried doing this with just solving equations in matrices but I didn't find a way to make it solve the equations over the base field $
\mathbb Q$.
Thanks a lot for helping!
Menny
Sun, 28 Aug 2011 10:23:10 +0200https://ask.sagemath.org/question/8297/extracting-the-coefficients-of-a-linear-combination/Answer by Volker Braun for <p>Let me just say that I'm quite new to sage (although I'm making a progress!)</p>
<p>I wish to input a number field (in my case real quadratic field), a basis $w_1,w_2$ over $
\mathbb Q$ and an element $v$ of this number field and get the coefficient of $v$ as a linear combination of $w_1$ and $w_2$. I.e., if $v=aw_1+bw_2$ I wish to get (a,b).</p>
<p>I tried doing this with just solving equations in matrices but I didn't find a way to make it solve the equations over the base field $
\mathbb Q$.</p>
<p>Thanks a lot for helping!
Menny</p>
https://ask.sagemath.org/question/8297/extracting-the-coefficients-of-a-linear-combination/?answer=12612#post-id-12612I take it you understand how to get the coefficients in the standard basis, e.g.:
sage: var('x')
x
sage: K.<a> = NumberField(x^2+1)
sage: 3+5*a+a^2
5*a + 2
sage: (3+5*a+a^2).vector()
(2, 5)
Then going to your preferred basis is just a linear algebra problem. For example, if your preferred QQ-basis is (1,1) and (2,0) then you could do:
sage: Q2 = (QQ^2).span_of_basis([(1,1), (2,0)]); Q2
Vector space of degree 2 and dimension 2 over Rational Field
User basis matrix:
[1 1]
[2 0]
sage: Q2.coordinates([2,5])
[5, -3/2]
Check that this is correct:
sage: 5*vector([1,1]) + (-3/2)*vector([2,0])
(2, 5)
Wed, 31 Aug 2011 10:23:17 +0200https://ask.sagemath.org/question/8297/extracting-the-coefficients-of-a-linear-combination/?answer=12612#post-id-12612Comment by Menny for <p>I take it you understand how to get the coefficients in the standard basis, e.g.:</p>
<pre><code>sage: var('x')
x
sage: K.<a> = NumberField(x^2+1)
sage: 3+5*a+a^2
5*a + 2
sage: (3+5*a+a^2).vector()
(2, 5)
</code></pre>
<p>Then going to your preferred basis is just a linear algebra problem. For example, if your preferred QQ-basis is (1,1) and (2,0) then you could do:</p>
<pre><code>sage: Q2 = (QQ^2).span_of_basis([(1,1), (2,0)]); Q2
Vector space of degree 2 and dimension 2 over Rational Field
User basis matrix:
[1 1]
[2 0]
sage: Q2.coordinates([2,5])
[5, -3/2]
</code></pre>
<p>Check that this is correct:</p>
<pre><code>sage: 5*vector([1,1]) + (-3/2)*vector([2,0])
(2, 5)
</code></pre>
https://ask.sagemath.org/question/8297/extracting-the-coefficients-of-a-linear-combination/?comment=21291#post-id-21291thanks a lot! this is an act of pure giving... MennyWed, 31 Aug 2011 11:24:49 +0200https://ask.sagemath.org/question/8297/extracting-the-coefficients-of-a-linear-combination/?comment=21291#post-id-21291