print / compose matrix

Prev Next

Function Names

print matrix, echo matrix, compose matrix

Library

Utility Library

Description

The function print matrix takes a combination of strings resp. scalar values and matrices embedded in sets. The output consist of mulitple rows of text where the matrices and text elements are properly aligned. You can print out 1 or more matrices with text (e.g. variables, mathematical operators, scalrs) inbetween.
The function echo matrix adds an additional 'new line' after printing out the matrix. The function compose matrix does not print out, but provides the resulting text lines in the return value.

Call as: procedure or function

Restrictions

Under normal conditions, the 'Utility Library' is loaded automatically, so no 'include(...)' call is needed.

Parameter count

Min 4

Parameters

No.TypeDescription
1.
input
string Number format

This number format is used to output numerals. See Numeral to String Formatting Parameters. Keep blank for generic output of numbers. The number format will not be used for other types, e.g. dates and strings. The matrix will appear wider if numbers with many digits are printed out.
Examples:

  • # - Number without decimal digits, zero is shown as blank
  • 0 - Numbers without decimal digits
  • 0.00 - Numbers with 2 decimal digits
  • #,##0 - Numbers with 1000 separators

2.
input
numeral Element length

This is the minimum width of every matrix element. The field with the longest contents will widen the columns so no contents need to be truncated..

3.
input
string Separator line

Typically, use a vertical line, or for nicer formatting add one space before and behind the vertical line. Other symbols such as spaces are also useful.

4,...
input
valid types Contents

Numerals, strings, date, Booleans, etc. are treated as text and scalers which can be used to create formulas consisting of both matrices and formula elements, for example constants, variables and operators.
Paramter sets are treated as matrices:

  • { 1, 2, 3 } translates to a vertical matrix.
  • {{ 1, 2, 3 } tranlates to a horziontal matrix.
  • { {1}, {2}, {3} } translates to a vertical matrix.
  • { {1,2}, {3,4}, {5,6} } translates to a matrix containing 3 rows and 2 columns.
  • { A, pi, 3 } Combination of text and numbers is allowed.

Return value

TypeDescription
set Matrix

Every element in the set contains one line of text.

Examples

       matrix 1[] = { { 1,20,300 }, {4, 50, 600}, {700, 8, 9} };
       matrix 2[] = { 10, 20, 30, 40 };
       matrix 3[] = horizontal( { 5, 10, 15, 20 } );
       matrix 4[] = vertical( {100,200,300,400} );

       echo matrix( "0.00", 6, " | ", "A = ",
               matrix 1[], "+", matrix 2[], ", ", matrix 3[], " and ", matrix 4[] );

       o[] = compose matrix( "0.00", 6, " | ", "A = ",
               matrix 1[], "+", matrix 2[], ", ", matrix 3[], " and ", matrix 4[] );

       echo("Alternative way to print the matrix:", new line, join( o[], new line ));

Output

                                  |  10.00 |                                             | 100.00 |
     |   1.00   20.00  300.00 |   |  20.00 |                                             | 200.00 |
A =  |   4.00   50.00  600.00 | + |  30.00 | ,  |   5.00   10.00   15.00   20.00 |  and  | 300.00 |
     | 700.00    8.00    9.00 |   |  40.00 |                                             | 400.00 |

Alternative way to print the matrix:
                                  |  10.00 |                                             | 100.00 |
     |   1.00   20.00  300.00 |   |  20.00 |                                             | 200.00 |
A =  |   4.00   50.00  600.00 | + |  30.00 | ,  |   5.00   10.00   15.00   20.00 |  and  | 300.00 |
     | 700.00    8.00    9.00 |   |  40.00 |                                             | 400.00 |
Try it yourself: Open LIB_Function_print_matrix.b4p in B4P_Examples.zip. Decompress before use.