Previous Up Next

5.50.1  Defining sparse matrices

A matrix is sparse if most of its elements are 0. To define a sparse matrix, it is enough to define the non-zero elements, which can be done with a table. The matrix command can then turn the table into a matrix.
Input:

A := table((0,0)=1, (1,1)=2, (2,2)=3, (3,3) = 4, (4,4) = 5)

or:

purge(A)
A[0..4,0..4]:=[1,2,3,4,5]

Output:

table((0,0) = 1, (1,1) = 2, (2,2) = 3, (3,3) = 4, (4,4) = 5)

This table can be converted to a matrix with either the convert command or the matrix command.
Input:

a := convert(A,array)

or:

a := matrix(A)

Output:

[[1,0,0,0,0],[0,2,0,0,0],[0,0,3,0,0],[0,0,0,4,0],[0,0,0,0,5]]

Previous Up Next