# /home/jaubrey/webwork//pg_TRUNK/macros/PGmatrixmacros.pl

### From WeBWorK

# NAME

Matrix macros for the PG language

# SYNPOSIS

# DESCRIPTION

Almost all of the macros in the file are very rough at best. The most useful is display_matrix. Many of the other macros work with vectors and matrices stored as anonymous arrays.

Frequently it may be more useful to use the Matrix objects defined RealMatrix.pm and Matrix.pm and the constructs listed there.

#### display_matrix

Usage \{ display_matrix( [ [1, '\(\sin x\)'], [ans_rule(5), 6] ]) \} \{ display_matrix($A, align=>'crvl') \} \[ \{ display_matrix_mm($A) \} \] \[ \{ display_matrix_mm([ [1, 3], [4, 6] ]) \} \]

display_matrix produces a matrix for display purposes. It checks whether it is producing LaTeX output, or if it is displaying on a web page in one of the various modes. The input can either be of type Matrix, Value::Matrix (mathobject) or a reference to an array.

Entries can be numbers, Fraction objects, bits of math mode, or answer boxes. An entire row can be replaced by the string 'hline' to produce a horizontal line in the matrix.

display_matrix_mm functions similarly, except that it should be inside math mode. display_matrix_mm cannot contain answer boxes in its entries. Entries to display_matrix_mm should assume that they are already in math mode.

Both functions take an optional alignment string, similar to ones in LaTeX tabulars and arrays. Here c for centered columns, l for left flushed columns, and r for right flushed columns.

The alignment string can also specify vertical rules to be placed in the matrix. Here s or | denote a solid line, d is a dashed line, and v requests the default vertical line. This can be set on a system-wide or course-wide basis via the variable $defaultDisplayMatrixStyle, and it can default to solid, dashed, or no vertical line (n for none).

The matrix has left and right delimiters also specified by $defaultDisplayMatrixStyle. They can be parentheses, square brackets, braces, vertical bars, or none. The default can be overridden in an individual problem with optional arguments such as left=>"|", or right=>"]".

You can specify an optional argument of 'top_labels'=> ['a', 'b', 'c']. These are placed above the columns of the matrix (as is typical for linear programming tableau, for example). The entries will be typeset in math mode.

Top labels require a bit of care. For image modes, they look better with display_matrix_mm where it is all one big image, but they work with display_matrix. With tth, you pretty much have to use display_matrix since tth can't handle the TeX tricks used to get the column headers up there if it gets the whole matrix at once.

#### side_labels

Produces an array that can be used to add labels outside a matrix. useful for presenting tableaus. Entries are set in mathmode

side_labels( @array );

\( \{lp_display_mm([$matrix3->value], top_labels=>[qw(x_1 x_2 x_3 x_4 obj b)] ) \} \{side_labels( qw(\text{cash} \text{hours} \text{profits} ) ) \} \)

#### mbox

Usage \{ mbox(thing1, thing2, thing3) \} \{ mbox([thing1, thing2, thing3], valign=>'top') \}

mbox takes a list of constructs, such as strings, or outputs of display_matrix, and puts them together on a line. Without mbox, the output of display_matrix would always start a new line.

The inputs can be just listed, or given as a reference to an array. With the latter, optional arguments can be given.

Optional arguments are allowbreaks=>'yes' to allow line breaks in TeX output; and valign which sets vertical alignment on web page output.

#### ra_flatten_matrix

Usage: ra_flatten_matrix($A)

where $A is a matrix object The output is a reference to an array. The matrix is placed in the array by iterating over columns on the inside loop, then over the rows. (e.g right to left and then down, as one reads text)

#### create2d_matrix

This can be a useful method for quickly entering small matrices by hand. --MEG

create2d_matrix("1 2 4, 5 6 8"); produces the anonymous array [[1,2,4],[5,6,8] ]

Matrix(create2d_matrix($string));

#### check_matrix_from_ans_box_cmp

An answer checker factory built on create2d_matrix. This still needs work. It is not feature complete, particularly with regard to error messages for incorrect input. --MEG

$matrix = Matrix("[[1,4],[2,3]"); ANS( check_matrix_from_ans_box($matrix) );

## convert_to_array_ref {

$output_matrix = convert_to_array_ref($input_matrix)

Converts a MathObject matrix (ref($input_matrix eq 'Value::Matrix') or a MatrixReal1 matrix (ref($input_matrix eq 'Matrix')to a reference to an array (e.g [[4,6],[3,2]]). This adaptor allows all of the Linear Programming subroutines to be used with MathObject arrays.

$mathobject_matrix->value outputs an array (usually an array of array references) so placing it inside
square bracket produces and array reference (of array references) which is what `lp_display_mm()`

is
seeking.