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.Sat, 25 Mar 2017 01:45:53 +0100Create a band matrix with repeating and alternating valueshttps://ask.sagemath.org/question/37065/create-a-band-matrix-with-repeating-and-alternating-values/I have 7 values: a, b, c, d, e, f and g
I would like to construct an m by n matrix in this way:
[ a b c d 0 0 0 0 . . . .]
[ b e f g 0 0 0 0 . . . .]
[ c f a b c d 0 0 . . . .]
[ d g b e f g 0 0 . . . .]
[ 0 0 c f a b c d 0 0 . .]
[ 0 0 d g b e f g 0 0 . .]
[ . . 0 0 c f a b c d . .]
[ . . 0 0 d g b e f g . .]
And so forth...
Therefore, the desired matrix is symmetrical. Values **a** and **e** alternate on the main diagonal; values **b** and **f** alternate on the *1st upper diagonal*; values **c** and **g** alternate on the *2nd upper diagonal*; values **d** and **0** alternate on the *3rd upper diagonal*. I would like to be able to specify the matrix size with m by n parameters (even though the resulting matrix is non-square).
What would be an efficient way to construct this matrix?Fri, 24 Mar 2017 20:09:22 +0100https://ask.sagemath.org/question/37065/create-a-band-matrix-with-repeating-and-alternating-values/Answer by ndomes for <p>I have 7 values: a, b, c, d, e, f and g</p>
<p>I would like to construct an m by n matrix in this way:</p>
<pre><code>[ a b c d 0 0 0 0 . . . .]
[ b e f g 0 0 0 0 . . . .]
[ c f a b c d 0 0 . . . .]
[ d g b e f g 0 0 . . . .]
[ 0 0 c f a b c d 0 0 . .]
[ 0 0 d g b e f g 0 0 . .]
[ . . 0 0 c f a b c d . .]
[ . . 0 0 d g b e f g . .]
</code></pre>
<p>And so forth...</p>
<p>Therefore, the desired matrix is symmetrical. Values <strong>a</strong> and <strong>e</strong> alternate on the main diagonal; values <strong>b</strong> and <strong>f</strong> alternate on the <em>1st upper diagonal</em>; values <strong>c</strong> and <strong>g</strong> alternate on the <em>2nd upper diagonal</em>; values <strong>d</strong> and <strong>0</strong> alternate on the <em>3rd upper diagonal</em>. I would like to be able to specify the matrix size with m by n parameters (even though the resulting matrix is non-square).</p>
<p>What would be an efficient way to construct this matrix?</p>
https://ask.sagemath.org/question/37065/create-a-band-matrix-with-repeating-and-alternating-values/?answer=37070#post-id-37070
var('a b c d e f g')
A = matrix(2,2,[a,b,b,e])
C = matrix(2,2,[c,d,f,g])
B = block_matrix(2,2, [A,C,C.transpose(),A])
M = matrix(SR,10,10)
for k in range(4):
M.set_block(2*k,2*k,B)
MSat, 25 Mar 2017 00:24:44 +0100https://ask.sagemath.org/question/37065/create-a-band-matrix-with-repeating-and-alternating-values/?answer=37070#post-id-37070Answer by danny for <p>I have 7 values: a, b, c, d, e, f and g</p>
<p>I would like to construct an m by n matrix in this way:</p>
<pre><code>[ a b c d 0 0 0 0 . . . .]
[ b e f g 0 0 0 0 . . . .]
[ c f a b c d 0 0 . . . .]
[ d g b e f g 0 0 . . . .]
[ 0 0 c f a b c d 0 0 . .]
[ 0 0 d g b e f g 0 0 . .]
[ . . 0 0 c f a b c d . .]
[ . . 0 0 d g b e f g . .]
</code></pre>
<p>And so forth...</p>
<p>Therefore, the desired matrix is symmetrical. Values <strong>a</strong> and <strong>e</strong> alternate on the main diagonal; values <strong>b</strong> and <strong>f</strong> alternate on the <em>1st upper diagonal</em>; values <strong>c</strong> and <strong>g</strong> alternate on the <em>2nd upper diagonal</em>; values <strong>d</strong> and <strong>0</strong> alternate on the <em>3rd upper diagonal</em>. I would like to be able to specify the matrix size with m by n parameters (even though the resulting matrix is non-square).</p>
<p>What would be an efficient way to construct this matrix?</p>
https://ask.sagemath.org/question/37065/create-a-band-matrix-with-repeating-and-alternating-values/?answer=37075#post-id-37075![image description](/upfiles/14904027382145047.png)Sat, 25 Mar 2017 01:45:53 +0100https://ask.sagemath.org/question/37065/create-a-band-matrix-with-repeating-and-alternating-values/?answer=37075#post-id-37075