Skip to content

Matrices and vectors in STACK

This page documents the use of matrices in STACK. There is a topics page for setting linear algebra STACK questions.


Note that in Maxima, the operator . represents noncommutative multiplication and scalar product. The star A*B gives element-wise multiplication.

The following functions are part of Maxima, but are very useful for us.


Where m[i]: m[i] + k * m[j].


Where m[i]: k * m[i]. And a function to compute reduced row echelon form


Assigning individual elements

To assign values to individual elements, use the simple syntax such as the following.


Note also Maxima's setelmx function:

setelmx (<x>, <i>, <j>, <M>)

Assigns <x> to the (<i>, <j>)'th element of the matrix <M>, and returns the altered matrix. <M> [<i>, <j>]: <x> has the same effect, but returns <x> instead of <M>.

Showing working

It is quite common to want to show part of a matrix calculation "un-evaluated". For example, the following is typical.

This is achieved, by having a question in which simplification is off, and we define the question variables as follows.


Notice the use of zip_with which is not a core Maxima function, but is defined by STACK. The above equation is then generated by the CASText

\[ {@A@}+{@B@}={@C@}={@D@}.\]

A similar procedure is needed for showing working when multiplying matrices. Here we need to loop over the matrices, for square matrices we use the following.

C:zeromatrix (first(matrix_size(A)), second(matrix_size(A)));
S:for a:1 thru first(matrix_size(A)) do for b:1 thru second(matrix_size(A)) do C[ev(a,simp),ev(b,simp)]:apply("+",zip_with("*",A[ev(a,simp)],BT[ev(b,simp)]));

Notice we need to simplify the arguments before we take indices of expressions. This is one problem with simp:false.

For non-square matrices we can use this.

C:zeromatrix (first(matrix_size(A)), second(matrix_size(B)));
S:for a:1 thru first(matrix_size(A)) do for b:1 thru second(matrix_size(B)) do C[ev(a,simp),ev(b,simp)]:apply("+",zip_with("*",A[ev(a,simp)],BT[ev(b,simp)]));

Now it makes no sense to include the point wise multiplication of elements as a possible wrong answer.

There must be a more elegant way to do this!

Display of matrices

You can set the type of parentheses used to surround matrices in a number of ways. Firstly, the admin user should set the site default in the qtype_stack options page.

For an individual question, the teacher can set the variable


in any of the usual places, e.g. in the question variables.

To set the display of an individual matrix, m say, in castext you can use

{@(lmxchar:"|", m)@}

Since lmxchar is a global setting in Maxima, you will have to set it back when you next display a matrix. Not ideal, but there we are.

Note, STACK only displays matrices with matching parentheses. If you want something like then you will have to display the matrix without parentheses and sort out the mismatching parentheses in the CASText at the level of display.

For example, if we have the question variable f:matrix([4*x+4, x<1],[-x^2-4*x-8, x>=1]; and the castext \[ f(x) := \left\{ {@(lmxchar:"", f)@} \right. \] STACK generates Notice the use of LaTeX \left\{ to automatically size the parentheses and \right. to represent a matching, but invisible closing parentesis.


If you are trying to use the vector notation such as you will probably want to redefine to be an abstract symbol, not a complex number. More information on this is given under Numbers.

Another way to do this is to create matrices as follows:


Now we can use the dot product to create the vector. The STACK function texboldatoms wraps all atomic variable names in the ephemeral function stackvector, which is typeset in bold.


If you turn the option "Multiplication sign" to none, this should display as Notice the use of the function ordergreat. ordergreat can only be used once at the beginning of the question.

The vector stackvector(a) and the atom a are different, and are not considered algebraically equivalent. While students may type in stackvector(a) as an answer, they are likely to type in a. The teacher can either (1) add in stackvector ephemeral forms to the student's answer in the feedback variables using texboldatoms or 92) remove all stackvector forms from the teacher's answer by using the destackvector(ex) function on their answer. In the future we may have an option in the input to apply texboldatoms to student's expressions.

The display of the ephemeral form of stackvector is controlled by the function stackvectortex, e.g. you can display vectors differently using the following examples.

stackvectortex(ex):= block(sconcat("{\\bf \\vec{", tex1(first(args(ex))), "}}"));
stackvectortex(ex):= block(sconcat("{\\bf \\underline{", tex1(first(args(ex))), "}}"));

which should go in the question variables.

Any texput commands in the question variables now become "context variables" and will be available to the inputs. So, if in the context of your question you would like a variable such as x to be considered as a vector and displayed as a vector you can add one of the following to the question variables.

texput(x, "\\mathbf{\\vec{x}}");
texput(x, "\\mathbf{\\underline{x}}");

Whenever x is then displayed in any part of the question, including the student's input validation or feedback generated by the answer tests, we will have the updated tex for this atom. E.g.

texput(x, "\\mathbf{\\vec{x}}");
texput(y, "\\mathbf{\\vec{y}}");

and a student types in a*x+b*y then the tex output will be .

Vector cross product

The wedge product operator is denoted by the tilde ~. This is the itensor package. This package is not normally loaded by STACK, and in any case the package takes lists and not matrices. For convenience, the following function has been added which requires 3*1 matrices.


Another advantage of this function is the ability to return an un-simplified version with simp:false.

Student input

Students do find typing in matrices very tedious. Some teachers have asked for a convenient notation such as


for column vectors and


For row vectors. This is not a core part of STACK currently, but in individual questions you can convert such notation easily into mainstream Maxima using code such as the following.

vec_convert(sa) := if op(sa)=c then transpose(matrix(args(sa))) elseif op(sa)=v then matrix(args(sa));

Once converted into Matrices, the student's answer will be evaluated by PRTs as matrices. Of course, this will not be reflected in the valuation. If there is sufficient demand for this contact the developers.