## Saturday, October 29, 2016

### HP Prime and HP 49G/50G: Tridiagonal Matrices

HP Prime and HP 49G/50G:  Tridiagonal Matrices

The program TRIDIAG will build a tridiagonal matrix of the form:

 D1 R1 0 0 … 0 0 0 L1 D2 R2 0 … 0 0 0 0 L2 D3 R3 … 0 0 0 … … … … … … ... … 0 0 0 0 … L(n-2) D(n-1) R(n-1) 0 0 0 0 … 0 L(n-1) D(n)

Where

D represents the list/vector of diagonal elements, and D has the length n,
L represents the list/vector of elements to the left of the diagonal, and L has the length n-1, and
R represents the list/vector of elements to the right of the diagonal, and R has the length of n-1.

HP Prime Program tridiag

Input:   tridiag(ll, ld, lr) where the arguments are lists.

Program:
EXPORT tridiag(ll, ld, lr)
BEGIN
LOCAL m,s,j,t;
ssize(ld);
mdiag(ld);
FOR j FROM 1 to s-1 DO
tj+1;
m(t,j)ll(j);
m(j,t)lr(j);
END;
RETURN m;
END;

HP 49G/50g Program TRIDIAG

This is the first program I made on a HP 49G, but it should work on the 50g just fine.

Input:
3:  vector of left elements
2:  vector of diagonals
1:  vector of right elements

Program:
→ L D R
D SIZE OBJ→
DROP DUP D
SWAP DIAG→ →
S M
M 1 S 1 –
FOR J
J 1 + J 2 →LIST
L J GET PUT
J J 1 + 2 →LIST
R J GET PUT
NEXT ‘J’ PURGE

Example:
Left elements:  4, -3, 8
Diagonals: 1, 2, 3, 4
Right elements: -2, 5, 1

There are 4 diagonal elements, therefore the matrix will be 4 x 4.

Inputs:

HP Prime:  tridiag({4,-3,8},{1,2,3,4},{-2,5,1})

HP 49G/50g (RPN Mode):
3:  [4, -3, 8]
2:  [1, 2, 3, 4]
1:  [-2, 5, 1]

Result:

 1 -2 0 0 4 2 5 0 0 -3 3 1 0 0 8 4

I hope you like my new style for matrices.

Happy Halloween!

This blog is property of Edward Shore, 2016.