writeMatrix

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

Synopsis

writeMatrix (title, a)
writeMatrixComplex (title, a)

Required Arguments

char title (Input)
The 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
Print \(\text{a}^T\).

printAll, or

printLower, or

printUpper, or

printLowerNoDiag, or

printUpperNoDiag
Exactly one of these optional arguments can be specified in order 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 The entire matrix is printed (the default).
printLower The lower triangle of the matrix is printed, including the diagonal.
printUpper The upper triangle of the matrix is printed, including the diagonal.
printLowerNoDiag The lower triangle of the matrix is printed, without the diagonal.
printUpperNoDiag The upper triangle of the matrix is printed, without the diagonal.
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 may 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, double, complex). 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. (For complex matrices, two conversion specifications are required for each column of the matrix so the conversion character e is used in column 1. The conversion character f is used in column 2 and the real part of column 3.) 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, double, complex). 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" if you want a single conversion specification selected automatically with field width 10 and with four significant digits.

noRowLabels, or

rowNumber, or

rowNumberZero, or

rowLabels, char[] (Input)

If rowLabels is specified, 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. If no row labels are desired, use the noRowLabels optional argument. If the numbers 1, 2, …, nra are desired, use the rowNumber optional argument. If the numbers 1, 2, …, nra − 1 are desired, use the rowNumberZero optional argument. If none of these optional arguments is used, the numbers 1, 2, 3, …, nra are used for the row labels by default whenever nra > 1.

If nra = 1, the default is no row labels.

noColLabels, or

colNumber, or

colNumberZero, or

colLabels, char[] (Input)

If colLabels is specified, colLabels is a vector of length nca + 1 containing the character strings comprising the column headings. The heading for the row labels is colLabels[0], and 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. If no column labels are desired, use the noColLabels optional argument. If the numbers 1, 2, …, nca, are desired, use the colNumber optional argument. If the numbers 0, 1, …, nca − 1 are desired, use the colNumberZero optional argument. If none of these optional arguments is used, the numbers 1, 2, 3, …, nca are used for the column labels by default whenever nca > 1.

If nca = 1, the default is no column labels.

returnString (Output)
A None-terminated string containing the matrix to be printed. Lines are new-line separated and the last line does not have a trailing new-line character.
writeToConsole
This matrix is printed to a console window. If a console has not been allocated, a default console (80 × 24, white on black, no scrollbars) is created.

Description

The 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, may be used to specify a conversion specification for each column of the matrix.

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.math.writeMatrixComplex import writeMatrixComplex

nra = 3
nca = 4
a = zeros((nra, nca), dtype='complex')
for i in range(0, nra):
    for j in range(0, nca):
        re = (i + 1 + (j + 1) * 0.1)
        im = -re + 100
        a[i, j] = complex(re, im)

writeMatrixComplex("matrix\na", a)

Output

 
                        matrix
                           a
                           1                          2
1  (        1.1,       98.9)  (        1.2,       98.8)
2  (        2.1,       97.9)  (        2.2,       97.8)
3  (        3.1,       96.9)  (        3.2,       96.8)
 
                           3                          4
1  (        1.3,       98.7)  (        1.4,       98.6)
2  (        2.3,       97.7)  (        2.4,       97.6)
3  (        3.3,       96.7)  (        3.4,       96.6)

Example 2

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

from numpy import *
from pyimsl.math.writeMatrix import writeMatrix

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

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

Output

 
                       matrix
                          a
            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.math.writeMatrix import writeMatrix

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

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

Output

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