Post Reply 
Matrices Built from Shifted Elements
03-06-2019, 01:26 PM
Post: #1
Matrices Built from Shifted Elements
The programs LSM (left-shift matrix) and RSM (right-shift matrx) create a n x n matrix based on the elements of a given list. Each row has each of the elements rotated one element.

For LSM, each row has the elements shifted to the left one element.

For RSM, each row has the elements shifted to the right one element.

HP Prime Program: LSM
Code:

EXPORT LSM(L0)
BEGIN
// EWS 2019-03-02
// left shift matrix
LOCAL L1,N,M0,K;
N:=SIZE(L0);
L1:=L0;
FOR K FROM 1 TO N-1 DO
L1:=CONCAT(tail(L1),head(L1));
L0:=CONCAT(L0,L1);
END;
M0:=list2mat(L0,N);
RETURN M0;
END;

HP Prime Program: RSM
Code:

EXPORT RSM(L0)
BEGIN
// EWS 2019-03-03
// right shift matrix
LOCAL L1,N,M0,K;
N:=SIZE(L0);
L1:=L0;
FOR K FROM 1 TO N-1 DO
L1:=REVERSE(CONCAT(
tail(REVERSE(L1)),
head(REVERSE(L1))
));
L0:=CONCAT(L0,L1);
END;
M0:=list2mat(L0,N);
RETURN M0;
END;

Note: The program RSM creates a circulant matrix.

Example:

list = {1, 7, 8, -2, 0}

LSM({1, 7, 8, -2, 0} returns:

[ [ 1, 7, 8, -2, 0 ]
[ 7, 8, -2, 0, 1 ]
[ 8, -2, 0, 1, 7 ]
[ -2, 0, 1, 7, 8 ]
[ 0, 1, 7, 8, -2 ] ]

RSM({1,7,8,-2,0}) returns:

[ [ 1, 7, 8, -2, 0 ]
[ 0, 1, 7, 8, -2 ]
[ -2, 0, 1, 7, 8 ]
[ 8, -2, 0, 1, 7 ]
[ 7, 8, -2, 0, 1 ] ]

Blog post: https://edspi31415.blogspot.com/2019/03/...ifted.html
Visit this user's website Find all posts by this user
Quote this message in a reply
03-06-2019, 04:32 PM
Post: #2
RE: Matrices Built from Shifted Elements
Interesting topic, another way to do it would be to use MAKEMAT like this:

Code:
EXPORT LSM(L0)
BEGIN
 LOCAL N:= SIZE(L0);
 MAKEMAT(L0((J+I-1) MOD N),N,N);   // left shift matrix
END;

EXPORT RSM(L0)
BEGIN
 LOCAL N:= SIZE(L0);
 MAKEMAT(L0((J-I+1) MOD N),N,N);   // right shift matrix
END;
Find all posts by this user
Quote this message in a reply
03-06-2019, 05:41 PM
Post: #3
RE: Matrices Built from Shifted Elements
Interesting, thanks for posting. As an aside, your left-shifted matrix is a Hankel matrix, useful in linear algebra and integer transforms.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: