Jump to: Site Navigation


        Matrix macros for the PG language



These macros are fairly old. The most useful is display_matrix and its variants.

Frequently it will be most useful to use the MathObjects Matrix (defined in and Vector types which have more capabilities and more error checking than the subroutines in this file. These macros have no object orientation and work with vectors and matrices stored as perl anonymous arrays.

There are also Matrix objects defined in and but in almost all cases the MathObjects Matrix types are preferable.


               \{ 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.


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} ) )


                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.


                Usage:   ra_flatten_matrix($A)
                        returns:  [a11, a12,a21,a22]

                        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)


        # This subroutine is probably obsolete and not generally useful.  
        # It was patterned after the APL
        # constructs for multiplying matrices. It might come in handy 
        # for non-standard multiplication of
        # of matrices (e.g. mod 2) for indice matrices.


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

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


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 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.


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) );

zero_check (deprecated -- use MathObjects matrices and vectors)

        # this subroutine zero_check is not very well designed below -- if it is used much it should receive
        # more work -- particularly for checking relative tolerance.  More work needs to be done if this is
        # actually used.

vec_dot() (deprecated -- use MathObjects vectors and matrices)

sub vec_dot{ my $vec1 = shift; my $vec2 = shift; warn "vectors must have the same length" unless @$vec1 == @$vec2; # the vectors must have the same length. my @vec1=@$vec1; my @vec2=@$vec2; my $sum = 0;

        while(@vec1) {
                $sum += shift(@vec1)*shift(@vec2);

proj_vect (deprecated -- use MathObjects vectors and matrices)

vec_cmp (deprecated -- use MathObjects vectors and matrices)

Site Navigation