Construct a matrix row by row with some rules on elements

I want to construct a matrix in which the first rows, along with the last ones, are peculiar, being the last ones the inverted forms of the first ones, while the central rows have the same form, just with the nonzero part shifted right by proceeding from one row to the next one. In Matlab or Octave I would have written something like:

# begin Matlab equivalent code for matrix
# A=zeros(nn+1)
# A(1,1:4)=[m11 m12 m13 m14]
# A(2,1:5)=[m12 m22 m23 m24 m25]
# A(3,1:6)=[m13 m23 m33 m34 m35 m36]
# A(4,1:7)=[m14 m24 m34 m33 m34 m35 m36]
# A(5,2:8)=[m25 m35 m34 m33 m34 m35 m36]
# for i=6:nn-4
# A(i,i-3:i+3)=[m36 m35 m34 m33 m34 m35 m36]
# end
# A(nn-3,nn-6:nn)=A(5,8:-1:2)
# A(nn-2,nn-5:nn+1)=A(4,7:-1:1)
# A(nn-1,nn-4:nn+1)=A(3,6:-1:1)
# A(nn,nn-3:nn+1)=A(2,5:-1:1)
# A(nn+1,nn-2:nn+1)=A(1,4:-1:1)
# end Matlab equivalent code for matrix

Although I was able to construct it also in Sage with a fixed nn=10:

aa=10
nn=10
vecordinv_matrix=matrix([[0,0,0,0,0,0,0,0,0,0,1],[0,0,0,0,0,0,0,0,0,1,0],[0,0,0,0,0,0,0,0,1,0,0], \
[0,0,0,0,0,0,0,1,0,0,0],[0,0,0,0,0,0,1,0,0,0,0],[0,0,0,0,0,1,0,0,0,0,0], \
[0,0,0,0,1,0,0,0,0,0,0],[0,0,0,1,0,0,0,0,0,0,0],[0,0,1,0,0,0,0,0,0,0,0], \
[0,1,0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0,0,0]])
mv1=vector((N(m11_tot(aa,nn)),N(m12_tot(aa,nn)),N(m13_tot(aa,nn)),N(m14_tot(aa,nn)), \
0,0,0,0,0,0,0))
mv2=vector((N(m12_tot(aa,nn)),N(m22_tot(aa,nn)),N(m23_tot(aa,nn)),N(m24_tot(aa,nn)), \
N(m25_tot(aa,nn)),0,0,0,0,0,0))
mv3=vector((N(m13_tot(aa,nn)),N(m23_tot(aa,nn)),N(m33_tot(aa,nn)),N(m34_tot(aa,nn)), \
N(m35_tot(aa,nn)),N(m36_tot(aa,nn)),0,0,0,0,0))
mv4=vector((N(m14_tot(aa,nn)),N(m24_tot(aa,nn)),N(m34_tot(aa,nn)),N(m33_tot(aa,nn)), \
N(m34_tot(aa,nn)),N(m35_tot(aa,nn)),N(m36_tot(aa,nn)),0,0,0,0))
mv5=vector((0,N(m25_tot(aa,nn)),N(m35_tot(aa,nn)),N(m34_tot(aa,nn)),N(m33_tot(aa,nn)), \
N(m34_tot(aa,nn)),N(m35_tot(aa,nn)),N(m36_tot(aa,nn)),0,0,0))
mv6=vector((0,0,N(m36_tot(aa,nn)),N(m35_tot(aa ...
edit retag close merge delete

Sort by ยป oldest newest most voted

The same syntax is almost valid in Sage

sage: A=matrix(4)
sage: A[0,:] = vector([0,1,2,3])
sage: A[1,-2:] = vector([13,-3])
sage: A
[ 0  1  2  3]
[ 0  0 13 -3]
[ 0  0  0  0]
[ 0  0  0  0]

A big difference is that in Sage, row/column indices start at 0. I find that using the vector is a bit painful, but I do not know if we can get around.

Vincent

more

It seems to work, thanks. Code like

m=matrix.identity(nn+1)
nn1_half=(nn+1)/2
for j in range(nn1_half) :
m=m*elementary_matrix(nn+1,row1=j,row2=nn-j)
vecordinv_matrix=m # a (nn+1)-sized matrix with 1 on the anti-diagonal and 0 as other entries
#
A=matrix(CC,nn+1)
A[0,0:4]=vector((N(m11_tot(aa,nn)),N(m12_tot(aa,nn)),N(m13_tot(aa,nn)),N(m14_tot(aa,nn))))
A[1,0:5]=vector((N(m12_tot(aa,nn)),N(m22_tot(aa,nn)),N(m23_tot(aa,nn)),N(m24_tot(aa,nn)), \
N(m25_tot(aa,nn))))
A[2,0:6]=vector((N(m13_tot(aa,nn)),N(m23_tot(aa,nn)),N(m33_tot(aa,nn)),N(m34_tot(aa,nn)), \
N(m35_tot(aa,nn)),N(m36_tot(aa,nn))))
A[3,0:7]=vector((N(m14_tot(aa,nn)),N(m24_tot(aa,nn)),N(m34_tot(aa,nn)),N(m33_tot(aa,nn)), \
N(m34_tot(aa,nn)),N(m35_tot(aa,nn)),N(m36_tot(aa,nn))))
A[4,1:8]=vector((N(m25_tot(aa,nn)),N(m35_tot(aa,nn)),N(m34_tot(aa,nn)),N(m33_tot(aa,nn)), \
N(m34_tot(aa,nn)),N(m35_tot(aa,nn)),N(m36_tot(aa,nn))))
for j in [5..nn-5] :
A[j,j-3:j+4]=vector((N(m36_tot(aa,nn)),N(m35_tot(aa,nn)),N(m34_tot(aa,nn)),N(m33_tot(aa,nn)), \
N(m34_tot(aa,nn)),N(m35_tot(aa,nn)),N(m36_tot(aa,nn))))
A_aux=matrix(CC,nn+1,5)
for j in range(5) :
A_aux[:,j]=vecordinv_matrix*A[j,:].transpose()
A[nn-j,:]=A_aux[:,j].transpose()
M=nn*A

correctly outputs the desired results. Differently from Matlab/Octave, a:b means from a to b-1 and not from a to b (the right extremum is excluded). Also, matrix has to be declared with the right type of entries (complex, CC, in this case), otherwise they are assumed to be integers, and in the case they are not, an error message is displayed (something that has the meaning of attempted coercition of non integral number in an integer).

more

You are right, the syntax a:b is different. But I actually find it much more useful for concatenation as (a:b) + (b:c) = (a:c) whenever a < b < c.

( 2014-10-20 18:56:04 +0200 )edit