writeMatrix¶
Prints a rectangular matrix (or vector) stored in contiguous memory locations.
Required Arguments¶
- char
title(Input) - The matrix title. Use
\nwithin a title to create a new line. Long titles are automatically wrapped. - float
a[[]](Input) - Array of size
nra×ncacontaining 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 characterffor the entire matrix. (For the conversion characterf, the matrix must be of type float, double, complex). Alternatively,writeFormat= “%10.3e%10.3e%10.3f%10.3f%10.3f” specifies the conversion characterefor columns 1 and 2 and the conversion characterffor columns 3, 4, and 5. (For complex matrices, two conversion specifications are required for each column of the matrix so the conversion charactereis used in column 1. The conversion characterfis used in column 2 and the real part of column 3.) If the end ofwriteFormatis encountered and if some columns of the matrix remain, format control continues with the first conversion specification inwriteFormat.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,nis the field width and d is the number of significant digits generally printed. Valid values fornare3, 4, …, 40. Valid values for d are1, 2, …, n-2. IfwriteFormatspecifies one conversion specification ending with w, all elements of a are examined to determine one conversion specification for printing.
If
writeFormatspecifies more than one conversion specification, separate conversion specifications are generated for each conversion specification ending with w. SetwriteFormat ="10.4w"if you want a single conversion specification selected automatically with field width 10 and with four significant digits.- Characters not associated with a conversion specification are not
allowed. For example, in the format
noRowLabels, or
rowNumber, or
rowNumberZero, or
rowLabels, char[](Input)If
rowLabelsis specified,rowLabelsis a vector of lengthnracontaining the character strings comprising the row labels. Here,nrais the number of rows in the printed matrix. Use\nwithin a label to create a new line. Long labels are automatically wrapped. If no row labels are desired, use thenoRowLabelsoptional argument. If the numbers 1, 2, …,nraare desired, use therowNumberoptional argument. If the numbers 1, 2, …,nra− 1 are desired, use therowNumberZerooptional argument. If none of these optional arguments is used, the numbers 1, 2, 3, …,nraare used for the row labels by default whenevernra> 1.If
nra= 1, the default is no row labels.
noColLabels, or
colNumber, or
colNumberZero, or
colLabels, char[](Input)If
colLabelsis specified,colLabelsis a vector of lengthnca+ 1 containing the character strings comprising the column headings. The heading for the row labels iscolLabels[0], andcolLabels[i],i= 1, …,nca, is the heading for thei-th column. Use\nwithin a label to create a new line. Long labels are automatically wrapped. If no column labels are desired, use thenoColLabelsoptional argument. If the numbers 1, 2, …,nca, are desired, use thecolNumberoptional argument. If the numbers 0, 1, …,nca− 1 are desired, use thecolNumberZerooptional argument. If none of these optional arguments is used, the numbers 1, 2, 3, …,ncaare used for the column labels by default whenevernca> 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