Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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,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))
mv7=vecordinv_matrix*mv5
mv8=vecordinv_matrix*mv4
mv9=vecordinv_matrix*mv3
mva=vecordinv_matrix*mv2
mvb=vecordinv_matrix*mv1
A=matrix([mv1,mv2,mv3,mv4,mv5,mv6,mv7,mv8,mv9,mva,mvb])

(note that the quantities are actually numerical evaluations of functions which depend on aa and nn), I was able to generalize only the vecordinv_matrix which invertes the order of the elements of a vector:

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

Is there a way to perform construction also for the matrix A? Thanks in advance.

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,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))
mv7=vecordinv_matrix*mv5
mv8=vecordinv_matrix*mv4
mv9=vecordinv_matrix*mv3
mva=vecordinv_matrix*mv2
mvb=vecordinv_matrix*mv1
A=matrix([mv1,mv2,mv3,mv4,mv5,mv6,mv7,mv8,mv9,mva,mvb])

(note that the quantities are actually numerical evaluations of functions which depend on aa and nn), I was able to generalize only the vecordinv_matrix which invertes the order of the elements of a vector:

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

Is there a way to perform construction also for the matrix A? Thanks in advance.