Programmer Guide > Using Subscripts with Arrays > Matrices
  

Matrices
If A is an m by n array, the command
PRINT, A
yields:
The fact that the array is printed this way may disturb those who are used to the linear algebra convention for listing a matrix. For this reason PV‑WAVE is equipped with a set of input/output routines that subscribe to the linear algebra convention. RMF and PMF read and write files according to the linear algebra convention, and RM and PM are the interactive versions of RMF and PMF. For instance, the command:
PM, A 
yields:
 
note
Regardless of how the array is read in or printed out, memory storage order is unaffected. Thus, the distinction between arrays and matrices in PV‑WAVE is completely superficial.
Reading and Printing Matrices Interactively
Matrices can be entered interactively using the RM procedure and printed to the screen using PM (see the PV‑WAVE Reference). In this example, a matrix is interactively entered and printed along with its inverse.
; Enter 3 by 3 matrix A.
RM, a, 3, 3
; User is prompted to enter the rows of the matrix.
row 0: 3 1 2
row 1: 4 5 1
row 2: 7 3 9
; Print the matrix.
PM, a
; PV-WAVE prints the following:
; 3.00000      1.00000      2.00000
; 4.00000      5.00000      1.00000
; 7.00000      3.00000      9.00000
; Print the inverse of A.
PM, INVERT(a)
; PV-WAVE prints the following:
;  0.823530   -0.0588235    -0.176471
; -0.568628    0.254902      0.0980392
; -0.450980   -0.0392157     0.215686
The matrix multiplication operator is “#”. For instance
; Enter 4 by 2 matrix P.
RM, p, 4, 2
row 0: 2 4
row 1: 1 3
row 2: 5 6
row 3: 0 7
; Enter 2 by 3 matrix Q.
RM, q, 2, 3
row 0: 1 3 5
row 1: 2 4 6
; Print the matrix product of P and Q.
PM, p # q
; PV-WAVE prints the following:
; 10.0000      22.0000      34.0000
; 7.00000      15.0000      23.0000
; 17.0000      39.0000      61.0000
; 14.0000      28.0000      42.0000
Matrices also can be entered elementwise, starting with the (0, 0) subscript. As is standard in mathematics, the first subscript refers to the row and the second to the column. For example:
; Allocate w to be a 3 by 3 float array.
w = FLTARR(3, 3)
; Assign values to w and print as a matrix.
w(0, 0) = 1
w(0, 1) = 2
w(0, 2) = 3
w(1, 0) = 4
w(1, 1) = 5
w(1, 2) = 6
w(2, 0) = 7
w(2, 1) = 8
w(2, 2) = 9
PM, w
; PV-WAVE prints the following:
; 1.00000      2.00000      3.00000
; 4.00000      5.00000      6.00000
; 7.00000      8.00000      9.00000
; Print w. It is the transpose of the previous statement.
PRINT, w
; PV-WAVE prints the following:
; 1.00000      4.00000      7.00000
; 2.00000      5.00000      8.00000
; 3.00000      6.00000      9.00000
In a matrix, the elements are stored columnwise; i.e., the elements of the   0th column are first, followed by the elements of the 1st row, and so forth. Continuing the above example, the elements in the 0 column (1, 4, 7) come first, followed by those in the 1st column (2, 5, 8), etc.
FOR k = 0, 8 DO PRINT, k, w(k)
; PV-WAVE prints the following:
; 0      1.00000
; 1      4.00000
; 2      7.00000
; 3      2.00000
; 4      5.00000
; 5      8.00000
; 6      3.00000
; 7      6.00000
; 8      9.00000
Reading a Matrix From a File
In this example, the RMF procedure is used to read a matrix contained in an external file. The file cov.dat contains the following data:
1.0    0.523 0.395 0.471 0.346 0.426 0.576 0.434 0.639
0.523 1.0   0.479 0.506 0.418 0.462 0.547 0.283 0.645
0.395 0.479 1.0   0.355 0.27  0.254 0.452 0.219 0.504
0.471 0.506 0.355 1.0   0.691 0.791 0.443 0.285 0.505
0.346 0.418 0.27  0.691 1.0   0.679 0.383 0.149 0.409
0.426 0.462 0.254 0.791 0.679 1.0   0.372 0.314 0.472
0.576 0.547 0.452 0.443 0.383 0.372 1.0   0.385 0.68
0.434 0.283 0.219 0.285 0.149 0.314 0.385 1.0   0.47
0.639 0.645 0.504 0.505 0.409 0.472 0.68  0.47  1.0
After reading the matrix, principal components are computed for a nine-variable covariance matrix. (This example uses the PV-WAVE IMSL Statistics PRINC_COMP function.)
OPENR, unit, 'cov.dat', /Get_Lun
RMF, unit, covariances, 9, 9
CLOSE, unit
values = PRINC_COMP(covariances)
PM, values, Title = 'Eigenvalues:'
; PV-WAVE prints the following:
; Eigenvalues:
;      4.67692
;      1.26397
;      0.844450
;      0.555027
;      0.447076
;      0.429125
;      0.310241
;      0.277006
;      0.196197
Printing a Matrix to a File
This example retrieves a statistical data set using the PV-WAVE IMSL Statistics function STATDATA, then outputs the matrix to the file stat.dat.
; Get the data from STATDATA and print the 13 by 5 matrix.
stats = STATDATA(5)
PM, stats
; PV-WAVE prints the following:
; 7.00000 26.0000 6.00000 60.0000 78.5000
; 1.00000 29.0000 15.0000 52.0000 74.3000
; 11.0000 56.0000 8.00000 20.0000 104.300
; 11.0000 31.0000 8.00000 47.0000 87.6000
; 7.00000 52.0000 6.00000 33.0000 95.9000
; 11.0000 55.0000 9.00000 22.0000 109.200
; 3.00000 71.0000 17.0000 6.00000 102.700
; 1.00000 31.0000 22.0000 44.0000 72.5000
; 2.00000 54.0000 18.0000 22.0000 93.1000
; 21.0000 47.0000 4.00000 26.0000 115.900
; 1.00000 40.0000 23.0000 34.0000 83.8000
; 11.0000 66.0000 9.00000 12.0000 113.300
; 10.0000 68.0000 8.00000 12.0000 109.400
; Print the 13 by 5 matrix to a file.
OPENW, unit, 'stat.dat', /Get_Lun
; Use PMF to output the matrix, then close the file.
PMF, unit, stats
CLOSE, unit
Subarrays
Using subscript ranges, it is possible to extract submatrices. For instance, the 0th and 2nd row of matrix w are extracted by using the following statements:
; Print W as a matrix.
PM, w
; PV-WAVE prints the following:
; 1.00000      2.00000      3.00000
; 4.00000      5.00000      6.00000
; 7.00000      8.00000      9.00000
PM, w([0, 2], *)
; PV-WAVE prints the following:
; 1.00000      2.00000      3.00000
; 7.00000      8.00000      9.00000
Matrix Expressions
Complicated matrix expressions are possible. Using the matrices defined above, the following statements compute the inverse of a:
; Print the matrix.
PM, a
; PV-WAVE prints the following:
; 3.00000      1.00000      2.00000
; 4.00000      5.00000      1.00000
; 7.00000      3.00000      9.00000
; AA–1 should be identity. Error due to round off.
PM, a # INVERT(a)
; PV-WAVE prints the following:
; 1.00000           0.00000       0.00000
; 1.19209e-07       1.00000      -1.19209e-07
; 9.53674e-07      -2.98023e-08   1.00000
In the following code segment, (3.5A + W) (QTQ) is computed:
PM, q
; PV-WAVE prints the following:
; 1.00000      3.00000      5.00000
; 2.00000      4.00000      6.00000
; Compute and print (3.5A + W)(QTQ).
PM, (3.5 * a + w) # (TRANSPOSE(q) # q)
; PV-WAVE prints the following:
; 288.000      654.000      1020.00
; 499.000      1131.00      1763.00
; 1049.50      2388.50      3727.50

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.