This section is devoted to a very important practical tectniques how to extract submatrices and how to build matrices from smaller matrices.

Operations on Vectors

In Maxima, vectors can be represented in a couple of ways, but the most common representation is using square brackets [] to denote a list, which can be used as a vector. For example:
  • Using square brackets:
    is the standard way to represent a vector in Maxima. This is the preferred method for representing vectors because it is directly compatible with Maxima’s vector and matrix operations.
  • Using parentheses:
    represents a sequence in Maxima, not a vector. While sequences can sometimes be used similarly to vectors, they are not the same and may behave differently in certain contexts.
  • Column vectors: To create a column vector, you would typically use a matrix with a single column. For example, matrix([1], [2], [3], [4], [5], [6]) creates a column matrix, which can be treated as a column vector:
Maxima allows a user to make linear combinations.
We can define vectors with entries as functions:
Dot-product is defined as usual:
However, Hadamard product is defined with "*"
We check whether vectors are equal:
The magnitude of a vector u is related to the dot product by way of:
We can define a function for evaluation of the absolute value of a vector

To create vectors with specific types of entries (like even or odd), you would manually specify the entries that meet your criteria. For example, [2, 4, 6, 8] for even entries, and [1, 3, 5, 7] for odd entries. There’s no built-in function to automatically generate such vectors, but you can use list comprehension or loops to generate them programmatically:

To create a vector where each entry is the third consecutive integer starting from 1 (i.e., [1, 3, 6, 9, ...]), you can use a formula within a list comprehension. For example, you can use the formula n·(n+1)/2 for the n-th term of the sequence and generate a list based on this:

To create a vector with random entries in Maxima, you can use the random function within a list comprehension or a loop. For example,
would create a vector of 6 entries where each entry is a random integer between 0 and 9.

============================= vectors with complex entries

build a new vector (either row or column) from two vectors, say

[1,2] and [3,4]

make one vector: [1, 2, 3, 4]

Similar extract pne entry or subvector from [1, 2, 3, 4, 5]

Partition Matrices

There are many matrix functions available when you start Maxima and these functions can be used without loading in any additional packages. Here is the list (but several of these – see below – need separate packages loaded) from the Maxima html Help Manual (In the Help Manual index, type: matrixp, and then click on the category: Matrices, to see this list.)

In Maxima, matrices can be created using the matrix function. The syntax is straightforward: you provide the rows of the matrix as arguments to the function, with each row being a list of elements. Maxima also supports symbolic elements, so you can create matrices with variables or expressions. For example, this creates a 3 × 3 matrix A with the numbers 1 to 9 and a random matrix B:

The following code creates a 3 × 2 random matrix C and a 4 × 3 random matrix D:
Maxima allows you to perform all the basic matrix operations, including addition, subtraction, multiplication, and scalar multiplication. For instance:
Multiplying matrices can be done using the . operator, provided the matrices have compatible dimensions:
Multiplying a matrix by a scalar (a single number) is done using the "*" operator:
The Hadamard multiplication of two matrices is performed with the same command:

============================= matrices with complex entries

The determinant of a square matrix can be evaluated as follows

Transposition could be achived as
The inverse of a square matrix (if it exists)

Extracting Blocks from Matrices

Extracting submatrices is a powerful technique for focusing on specific parts of a matrix that are relevant to a particular problem. This can be useful in a variety of contexts, such as solving smaller parts of a larger system of equations, performing operations on specific sections of data, or simplifying complex calculations.
It returns a new matrix composed of the matrix M with rows ⟨i_1⟩, … , ⟨i_m⟩ deleted, and columns ⟨j_1⟩, … , ⟨j_n⟩ deleted.
Slicing, on the other hand, is a related concept that involves selecting continuous parts of a matrix, such as a range of rows or columns. Slicing can be thought of as a more specific form of creating submatrices, where the selection is based on sequential indices. This is particularly useful in data analysis and processing tasks, where one might need to extract consecutive rows or columns for further analysis or visualization.

For example, first and second row of the above matrix A:

Also
With second row skipped:
Extracting columns:
In practical terms, both submatrices and slicing are achieved through indexing operations. In a software tool like Maxima, one might use specific functions or syntax to specify the indices of the rows and columns to be extracted. These operations are essential for working with large matrices, allowing users to easily manipulate and analyze sections of data.

Row and Column Operations: You can perform operations on specific rows or columns, such as row reduction or applying a function to every element in a row or column. Row and column operations are transformations that can be applied to the rows or columns of a matrix. These operations are the backbone of many algorithms in linear algebra, including those used for solving systems of equations, determining matrix rank, and performing matrix decompositions.

Common row and column operations include:

  • Scaling: Multiplying a row or column by a non-zero scalar. This operation can change the scale of a system of equations without affecting its solutions:
  • Replacement: Adding to one row or column the scalar multiple of another. This is used extensively in the process of row reduction or Gaussian elimination:
  • >b>Interchanging: Swapping two rows or columns. This operation is often used to simplify calculations or to bring a matrix into a more desirable form, such as when performing pivoting in Gaussian elimination:
In Maxima, there is no direct access to matrix columns. To achieve a similar effect for columns, you need to transpose the matrix, perform the desired operations on the rows, and then transpose the matrix back once you’re done.
Also all these operations might be performed manually through a series of commands or by utilizing specific functions designed for linear algebraic manipulations. For example, one could use loops or apply functions to perform operations across an entire row or column, or use built-in functions to automate common tasks like row reduction.
Example 1: Let us consider the 3-by-4 matrix Insert a column at position 2:
v = Range[21, 23]; Insert[m // Transpose, v, 2] // Transpose // MatrixForm
Out[4]= \( \displaystyle \quad \begin{pmatrix} 1& 21&2& 3& 4 \\ 5& 22&6& 7& 8 \\ 9& 23&10& 11& 12 \end{pmatrix} \)
Extract row 3:
m[[3]]
Out[5]= {9, 10, 11, 12}
Extract column 2
m[[All, 2]] // MatrixForm
Out[6]= \( \displaystyle \quad \begin{pmatrix} 2 \\ 6 \\ 10 \end{pmatrix} \)
Insert a row at position 2:
v = Range[30, 33]; Insert[m, v, 2] // MatrixForm
Out[8]= \( \displaystyle \quad \begin{pmatrix} 1&2&3&4 \\ 30&31&32&33 \\ 5&6&7&8 \\ 9&10&11&12 \end{pmatrix} \)
Adding two rows or columns. First, we add column 3 = column 3 + column 1:
m2 = m; m2[[All, 3]] += m2[[All, 1]];
m2 // MatrixForm
Out[8]= \( \displaystyle \quad \begin{pmatrix} 1&2&4&4 \\ 5&6&12&8 \\ 9&10&20&12 \end{pmatrix} \)
Now row 2 = row 2 + row 3:
m2 = m;
m2[[2]] += m2[[3]];
m2 // MatrixForm
Out[8]= \( \displaystyle \quad \begin{pmatrix} 1&2&3&4 \\ 14&16&18&20 \\ 9&10&11&12 \end{pmatrix} \)
MatrixPlot[{{1, 2, 3, 4}, {14, 16, 18, 20}, {9, 10, 11, 12}}]
Swapping rows or columns. Swap row 1 and row 3:
m2 = m; m2[[{1, 3}]] = m2[[{3, 1}]];
m2 // MatrixForm
Out[11]= \( \displaystyle \quad \begin{pmatrix} 9&10&11&12 \\ 5&6&7&8 \\ 1&2&3&4 \end{pmatrix} \)
Swap column 1 and 3:
m2[[All, {1, 3}]] = m2[[All, {3, 1}]];
m2 // MatrixForm
Out[13]= \( \displaystyle \quad \begin{pmatrix} 11&10&9&12 \\ 7&6&5&8 \\ 3&2&1&4 \end{pmatrix} \)
Multiply row 2 with 3:
m*{1, 3, 1} // MatrixForm
Out[14]= \( \displaystyle \quad \begin{pmatrix} 1&2&3&4 \\ 15&18&21&24 \\ 9&10&11&12 \end{pmatrix} \)
Multiply column 1 with 4:
((m // Transpose)*{4, 1, 1, 1}) // Transpose // MatrixForm
Out[15]= \( \displaystyle \quad \begin{pmatrix} 4&2&3&4 \\ 20&6&7&8 \\ 36&10&11&12 \end{pmatrix} \)
Replace a block of a matrix:
mat = RandomInteger[10, {5, 5}];
mat // MatrixForm
Out[2]= \( \displaystyle \quad \begin{pmatrix} 3&8&10&7&1 \\ 9&5&10&7&1 \\ 3&6&0&4&4 \\ 10&3&2&3&6 \\ 7&8&9&3&8 \end{pmatrix} \)
Update the 3-by-4 submatrix by using the short form of Span (;;) to specify the relevant span of rows and columns:
mat[[1 ;; 3, 1 ;; 4]] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
mat // MatrixForm
Out[4]= \( \displaystyle \quad \begin{pmatrix} 0&0&0&0&1 \\ 0&0&0&0&1 \\ 0&0&0&0&4 \\ 10&3&2&3&6 \\ 7&8&9&3&8 \end{pmatrix} \)
To pick out a submatrix, you can use Span (;;). First, define a 4×5 matrix:
mat = RandomInteger[10, {4, 5}];
mat // MatrixForm
Out[6]= \( \displaystyle \quad \begin{pmatrix} 3&1&8&5&0 \\ 0&0&8&6&8 \\ 8&0&0&10&0 \\ 4&4&8&9&8 \end{pmatrix} \)
Extract the top-left 3×4 submatrix by using Span (;;) to specify the relevant span of rows and columns:
mat[[1 ;; 3, 1 ;; 4]] // MatrixForm
Out[8]= \( \displaystyle \quad \begin{pmatrix} 3&1&8&5 \\ 0&0&8&6 \\ 8&0&0&10 \end{pmatrix} \)
You can get the same block from a matrix with another command:
Take[mat, {1, 3}, {1, 4}]
Another option is cut off some rows or/and columns. If one wants to delete, say the second row, type:
Drop[mat, {2}]
or
Delete[mat, 2]
If one wants to delete the first two rows, type:
Drop[mat, 2]
Now suppose you want to remove certain rows and columns from a matrix:
remove[a_?MatrixQ, row_?VectorQ, col_?VectorQ] := Module[{nr, nc, krow, kcol}, {nr, nc} = Dimensions[a]; krow = Complement[Range[1, nr], row]; kcol = Complement[Range[1, nc], col]; a[[krow, kcol]] ]
As an example. consider a 4×5 matrix:
b = RandomInteger[{0, 10}, {4, 5}];
MatrixForm[b]
Out[4]= \( \displaystyle \quad \begin{pmatrix} 3&3&1&10&9 \\ 9&9&3&4&3 \\ 8&10&3&9&0 \\ 8&1&1&6&0 \end{pmatrix} \)
Here we attempt to remove rows 2,5 and column 4. Note there is no row 5, so the command disregards it.
remove[b, {2, 5}, {4}] // MatrixForm
Out[5]= \( \displaystyle \quad \begin{pmatrix} 3&3&1&9 \\ 8&10&3&0 \\ 8&1&1&0 \end{pmatrix} \)

Extract all elements except the outermost rows and columns (negative indices count from the end):
mat // MatrixForm
mat[[2 ;; -2, 2 ;; -1]]
Out[9]= \( \displaystyle \quad \begin{pmatrix} 3&1&8&5&0 \\ 0&0&8&6&8 \\ 8&0&0&10&0 \\ 4&4&8&9&8 \end{pmatrix} \)
Out[10]= {{0, 8, 6, 8}, {0, 0, 10, 0}}
Extract diagonal elements:
Diagonal[mat]
Out[11]= {3, 0, 0, 9}
Diagonal[mat, 2]
Out[12]= {8, 6, 0}
To find sum of diagonal elements (which is called trace), enter:
Total[Diagonal[mat]]
Out[13]= 12
   ■
End of Example 1

Building Matrices from Blocks

Example 2: Let us consider the 3-by-4 matrix    ■
End of Example 2

 


  1. Axier, S., Linear Algebra Done Right. Undergraduate Texts in Mathematics (3rd ed.). Springer. 2015, ISBN 978-3-319-11079-0.
  2. Beezer, R.A., A First Course in Linear Algebra, 2017.
  3. Woollett, E., Maxima by Example: Ch.5. Matrix Solution Method. 2016.