[Next][Prev] [Right] [Left] [Up] [Index] [Root]

Accessing or Modifying Entries

The following functions and procedures enable the user to access or set individual entries of sparse matrices.

A[i] : MtrxSprs, RngIntElt -> ModTupRngElt
Given a sparse matrix A over a ring R having m rows and n columns, and an integer i such that 1 <= i <= m, return the i-th row of A, as a dense vector of length n (lying in R^n).
A[i, j] : MtrxSprs, RngIntElt, RngIntElt -> RngElt
Given a sparse matrix A over a ring R having m rows and n columns, integers i and j such that 1 <= i <= m and 1 <= j <= n, return the (i, j)-th entry of A, as an element of the ring R.
A[i, j] := x : MtrxSprs, RngIntElt, RngIntElt, RngElt ->
Given a sparse matrix A over a ring R having m rows and n columns, integers i and j such that 1 <= i <= m and 1 <= j <= n, and a ring element x coercible into R, modify the (i, j)-th entry of A to be x. Here i and j must be within the ranges given by the current dimensions of A; see SetEntry below for a procedure to automatically extend A if necessary.
SetEntry(~A, i, j, x) : MtrxSprs, RngIntElt, RngIntElt, RngElt ->
(Procedure.) Given a sparse matrix A over a ring R, integers i, j >= 1, and a ring element x coercible into R, modify the (i, j)-th entry of A to be x. The entry specified by i and j is allowed to be beyond the current dimensions of A; if so, A is automatically extended to have at least i rows and j columns.

This procedure will be commonly used in situations where the final size of the matrix is not known as an algorithm proceeds (e.g., in index-calculus methods). One can create the 0 x 0 sparse matrix over Z, say, and then call SetEntry to build up the matrix dynamically. See the example H43E3 below, which uses this technique.

Note that extending the dimensions of A with a very large i or j will not in itself consume much memory, but if A then becomes dense or is passed to some algorithm, then the memory needed may of course be proportional to the dimensions of A.


Example SMat_Indexing (H43E2)

This example demonstrates simple ways of accessing the entries of sparse matrices.

> A := SparseMatrix(2, 3, [<1,2,3>, <2,3,-1>]);
> A;
Sparse matrix with 2 rows and 3 columns over Integer Ring
> Matrix(A);
[ 0  3  0]
[ 0  0 -1]
> A[1];
(0 3 0)
> A[1, 3]:=5;
> A[1];
(0 3 5)
We next extend A using the procedure SetEntry.

> SetEntry(~A, 1, 5, -7);
> A;
Sparse matrix with 2 rows and 5 columns over Integer Ring
> Matrix(A);
[ 0  3  5  0 -7]
[ 0  0 -1  0  0]
A common situation is to start with the empty 0 x 0 matrix over Z and then to extend it dynamically.

> A := SparseMatrix(); 
> A;
Sparse matrix with 0 rows and 0 columns over Integer Ring
> SetEntry(~A, 1, 4, -2); 
> A;
Sparse matrix with 1 row and 4 columns over Integer Ring
> SetEntry(~A, 2, 3, 8);
> A;
Sparse matrix with 2 rows and 4 columns over Integer Ring
> Matrix(A);
[ 0  0  0 -2]
[ 0  0  8  0]
> SetEntry(~A, 200, 319, 1);
> SetEntry(~A, 200, 3876, 1);
> A;
Sparse matrix with 200 rows and 3876 columns over Integer Ring
> Nrows(A);
200
> Ncols(A);
3876
> NNZEntries(A);
4
> Density(A);
0.000005159958720330237358101135190
> Support(A, 200);
[ 319, 3876 ]


 [Next][Prev] [Right] [Left] [Up] [Index] [Root]