Create a matrix
01-26-2019, 11:19 AM
Post: #1
 DrD Senior Member Posts: 1,133 Joined: Feb 2014
Create a matrix
Specifically, I would like to create a matrix, using a 3 element specified pattern, (tridiagonal), with a specified number of rows and columns. The center value of the pattern should be on the main diagonal of the matrix:

Some kind of variation on this (non-working) theme:
makemat([-1,2,-1], 5,5); // Uses the pattern -1,2,-1 in a 5x5 matrix, with 2 on the main diagonal:

[[2,-1,0,0,0],
[-1,2,-1,0,0],
[0,-1,2,-1,0],
[0,0,-1,2,-1],
[0,0,0,-1,2]]

I (think) this can be done using the matlab eye() command, for example. Any ideas how to create a custom matrix in similar fashion, on the prime?

-Dale-
01-26-2019, 12:00 PM (This post was last modified: 01-26-2019 12:37 PM by JMB.)
Post: #2
 JMB Member Posts: 98 Joined: Jan 2016
RE: Create a matrix
Try:
MAKEMAT(IFTE(I=J,2,IFTE(J=I-1 OR J=I+1,-1,0)),5,5)
Or:
MAKEMAT(IFTE(I=J,2,IFTE(ABS(I-J)=1,-1,0)),5,5)

Josep Mollera. HP PRIME, HW: C, SW: 2.1.14730 (2023 04 13).
01-26-2019, 03:18 PM
Post: #3
 DrD Senior Member Posts: 1,133 Joined: Feb 2014
RE: Create a matrix

Something interesting happens when I copy from your post, into the [CAS] (emulator):
MAKEMAT(IFTE(I=J,2,IFTE(J=I-1 OR J=I+1,-1,0)),5,5) :

CAS changed changed all of the IFTE()'s to when()'s:
MAKEMAT(when(I=J,2,when(J=I-1 OR J=I+1,-1,0)),5,5)

Then I recalled that result to the command line, it changed all of the when()'s to:
MAKEMAT(((I = J)? 2 : (((J = (I-1)) OR (J = (I+1)))? -1 : 0)),5,5)

I've never seen that last format before, on the prime. Maybe an XCAS shorthand?
01-26-2019, 05:49 PM (This post was last modified: 01-26-2019 05:50 PM by JMB.)
Post: #4
 JMB Member Posts: 98 Joined: Jan 2016
RE: Create a matrix
The programming language C has this same ternary operator

Cond ? a : b

If Cond is true returns a, otherwise returns b.

https://en.wikipedia.org/wiki/%3F:

Josep Mollera. HP PRIME, HW: C, SW: 2.1.14730 (2023 04 13).
 « Next Oldest | Next Newest »