Wednesday, March 6, 2019

HP Prime: Matrices Built from Shifted Elements

HP Prime:  Matrices Built from Shifted Elements

Introduction

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.

The illustration below shows how to programs work.



HP Prime Program: LSM

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

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 ] ]

Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.