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, 19 Dec 2016 13:12:33 -0600building matrices/vectors recursivelyhttps://ask.sagemath.org/question/35944/building-matricesvectors-recursively/Suppose that a is a vector, s is a scalar and I am trying to build (say) b=[a; a*s] (a vector twice as long as a).
How do I accomplish this with sage? the default behavior will be for b to be a vector with 2 entries, each of which is a vector. But what I wanted is a vector that is effectively twice as long as a.
Wed, 07 Dec 2016 17:57:05 -0600https://ask.sagemath.org/question/35944/building-matricesvectors-recursively/Answer by tmonteil for <p>Suppose that a is a vector, s is a scalar and I am trying to build (say) b=[a; a*s] (a vector twice as long as a).</p>
<p>How do I accomplish this with sage? the default behavior will be for b to be a vector with 2 entries, each of which is a vector. But what I wanted is a vector that is effectively twice as long as a.</p>
https://ask.sagemath.org/question/35944/building-matricesvectors-recursively/?answer=35946#post-id-35946If `a` is a vector:
sage: a = vector(QQ, [1,2,3,4]) ; a
(1, 2, 3, 4)
The scalar product leads to:
sage: 5*a
(5, 10, 15, 20)
And the sum is:
sage: a+a
(2, 4, 6, 8)
For lists; the sum works as you expect (catenation):
sage: [1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]
You can easily transform a vector into a list:
sage: list(a)
[1, 2, 3, 4]
To transform a list into a vector, it is safer to provide the base ring:
sage: vector(QQ,[1,2,3,4])
(1, 2, 3, 4)
sage: vector(RDF,[1,2,3,4])
(1.0, 2.0, 3.0, 4.0)
sage: vector(ZZ,[1,2,3,4]).parent()
Ambient free module of rank 4 over the principal ideal domain Integer Ring
sage: vector(QQ,[1,2,3,4]).parent()
Vector space of dimension 4 over Rational Field
So, to sum up, you can transform your vectors into litst, sum them and make them vectors again, by keeping the base_ring information:
sage: vector(a.base_ring(),list(a)+list(5*a))
(1, 2, 3, 4, 5, 10, 15, 20)
**EDIT** For matrices, you can use `block_matrix`:
sage: A = matrix(QQ, 2, 2, [3,9,6,10]) ; A
[ 3 9]
[ 6 10]
sage: block_matrix([[A, -A], [10*A, 100*A]], subdivide=False)
[ 3 9 -3 -9]
[ 6 10 -6 -10]
[ 30 90 300 900]
[ 60 100 600 1000]
See `block_matrix?` for more details.Thu, 08 Dec 2016 01:49:11 -0600https://ask.sagemath.org/question/35944/building-matricesvectors-recursively/?answer=35946#post-id-35946Comment by slelievre for <p>If <code>a</code> is a vector:</p>
<pre><code>sage: a = vector(QQ, [1,2,3,4]) ; a
(1, 2, 3, 4)
</code></pre>
<p>The scalar product leads to:</p>
<pre><code>sage: 5*a
(5, 10, 15, 20)
</code></pre>
<p>And the sum is:</p>
<pre><code>sage: a+a
(2, 4, 6, 8)
</code></pre>
<p>For lists; the sum works as you expect (catenation):</p>
<pre><code>sage: [1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]
</code></pre>
<p>You can easily transform a vector into a list:</p>
<pre><code>sage: list(a)
[1, 2, 3, 4]
</code></pre>
<p>To transform a list into a vector, it is safer to provide the base ring:</p>
<pre><code>sage: vector(QQ,[1,2,3,4])
(1, 2, 3, 4)
sage: vector(RDF,[1,2,3,4])
(1.0, 2.0, 3.0, 4.0)
sage: vector(ZZ,[1,2,3,4]).parent()
Ambient free module of rank 4 over the principal ideal domain Integer Ring
sage: vector(QQ,[1,2,3,4]).parent()
Vector space of dimension 4 over Rational Field
</code></pre>
<p>So, to sum up, you can transform your vectors into litst, sum them and make them vectors again, by keeping the base_ring information:</p>
<pre><code>sage: vector(a.base_ring(),list(a)+list(5*a))
(1, 2, 3, 4, 5, 10, 15, 20)
</code></pre>
<p><strong>EDIT</strong> For matrices, you can use <code>block_matrix</code>:</p>
<pre><code>sage: A = matrix(QQ, 2, 2, [3,9,6,10]) ; A
[ 3 9]
[ 6 10]
sage: block_matrix([[A, -A], [10*A, 100*A]], subdivide=False)
[ 3 9 -3 -9]
[ 6 10 -6 -10]
[ 30 90 300 900]
[ 60 100 600 1000]
</code></pre>
<p>See <code>block_matrix?</code> for more details.</p>
https://ask.sagemath.org/question/35944/building-matricesvectors-recursively/?comment=36050#post-id-36050@luis lastras, if this answers your question, please click the check mark next to the answer, so that the question is marked as answered.Mon, 19 Dec 2016 13:12:33 -0600https://ask.sagemath.org/question/35944/building-matricesvectors-recursively/?comment=36050#post-id-36050Comment by tmonteil for <p>If <code>a</code> is a vector:</p>
<pre><code>sage: a = vector(QQ, [1,2,3,4]) ; a
(1, 2, 3, 4)
</code></pre>
<p>The scalar product leads to:</p>
<pre><code>sage: 5*a
(5, 10, 15, 20)
</code></pre>
<p>And the sum is:</p>
<pre><code>sage: a+a
(2, 4, 6, 8)
</code></pre>
<p>For lists; the sum works as you expect (catenation):</p>
<pre><code>sage: [1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]
</code></pre>
<p>You can easily transform a vector into a list:</p>
<pre><code>sage: list(a)
[1, 2, 3, 4]
</code></pre>
<p>To transform a list into a vector, it is safer to provide the base ring:</p>
<pre><code>sage: vector(QQ,[1,2,3,4])
(1, 2, 3, 4)
sage: vector(RDF,[1,2,3,4])
(1.0, 2.0, 3.0, 4.0)
sage: vector(ZZ,[1,2,3,4]).parent()
Ambient free module of rank 4 over the principal ideal domain Integer Ring
sage: vector(QQ,[1,2,3,4]).parent()
Vector space of dimension 4 over Rational Field
</code></pre>
<p>So, to sum up, you can transform your vectors into litst, sum them and make them vectors again, by keeping the base_ring information:</p>
<pre><code>sage: vector(a.base_ring(),list(a)+list(5*a))
(1, 2, 3, 4, 5, 10, 15, 20)
</code></pre>
<p><strong>EDIT</strong> For matrices, you can use <code>block_matrix</code>:</p>
<pre><code>sage: A = matrix(QQ, 2, 2, [3,9,6,10]) ; A
[ 3 9]
[ 6 10]
sage: block_matrix([[A, -A], [10*A, 100*A]], subdivide=False)
[ 3 9 -3 -9]
[ 6 10 -6 -10]
[ 30 90 300 900]
[ 60 100 600 1000]
</code></pre>
<p>See <code>block_matrix?</code> for more details.</p>
https://ask.sagemath.org/question/35944/building-matricesvectors-recursively/?comment=35957#post-id-35957I edited my answer.Thu, 08 Dec 2016 08:19:05 -0600https://ask.sagemath.org/question/35944/building-matricesvectors-recursively/?comment=35957#post-id-35957Comment by luis lastras for <p>If <code>a</code> is a vector:</p>
<pre><code>sage: a = vector(QQ, [1,2,3,4]) ; a
(1, 2, 3, 4)
</code></pre>
<p>The scalar product leads to:</p>
<pre><code>sage: 5*a
(5, 10, 15, 20)
</code></pre>
<p>And the sum is:</p>
<pre><code>sage: a+a
(2, 4, 6, 8)
</code></pre>
<p>For lists; the sum works as you expect (catenation):</p>
<pre><code>sage: [1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]
</code></pre>
<p>You can easily transform a vector into a list:</p>
<pre><code>sage: list(a)
[1, 2, 3, 4]
</code></pre>
<p>To transform a list into a vector, it is safer to provide the base ring:</p>
<pre><code>sage: vector(QQ,[1,2,3,4])
(1, 2, 3, 4)
sage: vector(RDF,[1,2,3,4])
(1.0, 2.0, 3.0, 4.0)
sage: vector(ZZ,[1,2,3,4]).parent()
Ambient free module of rank 4 over the principal ideal domain Integer Ring
sage: vector(QQ,[1,2,3,4]).parent()
Vector space of dimension 4 over Rational Field
</code></pre>
<p>So, to sum up, you can transform your vectors into litst, sum them and make them vectors again, by keeping the base_ring information:</p>
<pre><code>sage: vector(a.base_ring(),list(a)+list(5*a))
(1, 2, 3, 4, 5, 10, 15, 20)
</code></pre>
<p><strong>EDIT</strong> For matrices, you can use <code>block_matrix</code>:</p>
<pre><code>sage: A = matrix(QQ, 2, 2, [3,9,6,10]) ; A
[ 3 9]
[ 6 10]
sage: block_matrix([[A, -A], [10*A, 100*A]], subdivide=False)
[ 3 9 -3 -9]
[ 6 10 -6 -10]
[ 30 90 300 900]
[ 60 100 600 1000]
</code></pre>
<p>See <code>block_matrix?</code> for more details.</p>
https://ask.sagemath.org/question/35944/building-matricesvectors-recursively/?comment=35955#post-id-35955Thank you!! The other (implied) question is for matrices - for example, how to I accomplish composing a 4x4 matrix using 4 individual 2x2 matrices?Thu, 08 Dec 2016 06:55:18 -0600https://ask.sagemath.org/question/35944/building-matricesvectors-recursively/?comment=35955#post-id-35955