writeMatrix

Prints a rectangular matrix (or vector) stored in contiguous memory locations.

Synopsis

writeMatrix (title, a)

Required Arguments

char title (Input)
Matrix title. Use \n within a title to create a new line. Long titles are automatically wrapped.
float a[[]] (Input)
Array of size nra × nca containing the matrix to be printed.

Optional Arguments

transpose, (Input)
Print \(a^T\).

printAll,

or

printLower,

or

printUpper,

or

printLowerNoDiag,

or

printUpperNoDiag
Exactly one of these optional arguments can be specified to indicate that either a triangular part of the matrix or the entire matrix is to be printed. If omitted, the entire matrix is printed.
Keyword Action
printAll Entire matrix is printed (the default).
printLower Lower triangle of the matrix is printed, including the diagonal.
printUpper Upper triangle of the matrix is printed, including the diagonal.
printLowerNoDiag Lower triangle of the matrix is printed, without the diagonal.
printUpperNoDiag Upper triangle of the matrix is printed, without the diagonal.

Default: printAll.

writeFormat, char (Input)

Character string containing a list of C conversion specifications (formats) to be used when printing the matrix. Any list of C conversion specifications suitable for the data type can be given. For example, writeFormat = "%10.3f" specifies the conversion character f for the entire matrix. For the conversion character f, the matrix must be of type float or double. Alternatively,``writeFormat`` = "%10.3e%10.3e%10.3f%10.3f%10.3f" specifies the conversion character e for columns 1 and 2 and the conversion character f for columns 3, 4, and 5. If the end of writeFormat is encountered and if some columns of the matrix remain, format control continues with the first conversion specification in writeFormat.

Aside from restarting the format from the beginning, other exceptions to the usual C formatting rules are as follows:

Characters not associated with a conversion specification are not allowed. For example, in the format writeFormat = "1%d2%d", the characters 1 and 2 are not allowed and result in an error.

A conversion character d can be used for floating-point values (matrices of type float or double). The integer part of the floating-point value is printed.

For printing numbers whose magnitudes are unknown, the conversion character g is useful; however, the decimal points will generally not be aligned when printing a column of numbers. The w (or W) conversion character is a special conversion character used by this function to select a conversion specification so that the decimal points will be aligned. The conversion specification ending with w is specified as "%n.dw". Here, n is the field width and d is the number of significant digits generally printed. Valid values for n are 3, 4, …, 40. Valid values for d are 1, 2, …, n − 2. If writeFormat specifies one conversion specification ending with w, all elements of a are examined to determine one conversion specification for printing. If writeFormat specifies more than one conversion specification, separate conversion specifications are generated for each conversion specification ending with w. Set writeFormat = "10.4w" for a single conversion specification selected automatically with field width 10 and with four significant digits.

noRowLabels, (Input)

Indicates that no row labels are used.

Default: If none of these optional arguments is used, the numbers 1, 2, 3, …, nra are used for the row labels whenever nra > 1. If nra = 1, the default is no row labels.

or

rowNumber, (Input)

Indicates the numbers 1, 2, 3, …, nra are used for the row labels.

Default: If none of these optional arguments is used, the numbers 1, 2, 3, …, nra are used for the row labels whenever nra > 1. If nra = 1, the default is no row labels.

or

rowNumberZero, (Input)

Indicates the numbers 1, 2, 3, …, nra-1 are used for the row labels.

Default: If none of these optional arguments is used, the numbers 1, 2, 3, …, nra are used for the row labels whenever nra > 1. If nra = 1, the default is no row labels.

or

rowLabels, char[] (Input)

Indicates rowLabels is a vector of length nra containing the character strings comprising the row labels. Here, nra is the number of rows in the printed matrix. Use \n within a label to create a new line. Long labels are automatically wrapped.

Default: If none of these optional arguments is used, the numbers 1, 2, 3, …, nra are used for the row labels whenever nra > 1. If nra = 1, the default is no row labels.

noColLabels, (Input)

Indicates that no column labels are used.

Default: If none of these optional arguments is used, the numbers 1, 2, 3, …, nca are used for the column labels whenever nca > 1. If nca = 1, the default is no column labels.

or

colNumber, (Input)

Indicates the numbers 1, 2, 3, …, nca are used for the column labels.

Default: If none of these optional arguments is used, the numbers 1, 2, 3, …, nca are used for the column labels whenever nca > 1. If nca = 1, the default is no column labels.

or

colNumberZero, (Input)

Indicates the numbers 1, 2, 3, …, nca-1 are used for the column labels.

Default: If none of these optional arguments is used, the numbers 1, 2, 3, …, nca are used for the column labels whenever nca > 1. If nca = 1, the default is no column labels.

or

colLabels, char[] (Input)

Indicates colLabels is a vector of length nca + 1 containing the character strings comprising the column headings. The heading for the column labels is colLabels [0]; colLabels [i], i = 1, …, nca, is the heading for the i-th column. Use \n within a label to create a new line. Long labels are automatically wrapped.

Default: If none of these optional arguments is used, the numbers 1, 2, 3, …, nca are used for the column labels whenever nca > 1. If nca = 1, the default is no column labels.

Description

Function writeMatrix prints a real rectangular matrix (stored in a) with optional row and column labels (specified by rowLabels and colLabels, respectively, regardless of whether a or \(a^T\) is printed). An optional format, writeFormat, can be used to specify a conversion specification for each column of the matrix.

In addition, the write matrix functions can restrict printing to the elements of the upper or lower triangles of a matrix by using the printUpper, printLower, printUpperNoDiag, and printLowerNoDiag options. Generally, these options are used with symmetric matrices, but this is not required. Vectors can be printed by specifying a row or column dimension of 1.

Output is written to the file specified by the function outputFile (Chapter 15, Utilities). The default output file is standard output (corresponding to the file pointer stdout). A page width of 78 characters is used. Page width and page length can be reset by invoking function page.

Horizontal centering, the method for printing large matrices, paging, the method for printing NaN (Not a Number), and whether or not a title is printed on each page can be selected by invoking function writeOptions.

Examples

Example 1

This example is representative of the most common situation in which no optional arguments are given.

from numpy import *
from pyimsl.stat.writeMatrix import writeMatrix

nra = 3
nca = 4
a = zeros((nra, nca))
for i in range(0, nra):
    for j in range(0, nca):
        a[i, j] = (i + 1 + (j + 1) * 0.1)

writeMatrix("matrix", a)

Output

 
                       matrix
             1            2            3            4
1          1.1          1.2          1.3          1.4
2          2.1          2.2          2.3          2.4
3          3.1          3.2          3.3          3.4

Example 2

In this example, some of the optional arguments available in the writeMatrix functions are demonstrated.

from numpy import *
from pyimsl.stat.writeMatrix import writeMatrix

nra = 3
nca = 4
a = zeros((nra, nca))
fmt = "%10.6W"
rlabel = ["row 1", "row 2", "row 3"]
clabel = ["", "col 1", "col 2", "col 3", "col 4"]
for i in range(0, nra):
    for j in range(0, nca):
        a[i, j] = (i + 1 + (j + 1) * 0.1)

writeMatrix("matrix", a, writeFormat=fmt, rowLabels=rlabel,
            colLabels=clabel, printUpperNoDiag=True)

Output

 
                       matrix
            col 2       col 3       col 4            
row 1         1.2         1.3         1.4            
row 2                     2.3         2.4            
row 3                                 3.4

Example 3

In this example, a row vector of length four is printed.

from numpy import *
from pyimsl.stat.writeMatrix import writeMatrix

nra = 1
nca = 4
a = zeros((nra, nca))
clabel = ["", "col 1", "col 2", "col 3", "col 4"]
for i in range(0, nca):
    a[0, i] = i + 1

writeMatrix("matrix\na", a, colLabels=clabel)

Output

 
                      matrix
                         a
      col 1        col 2        col 3        col 4
          1            2            3            4