Package dsa
Class SparseMatrix
java.lang.Object
dsa.SparseMatrix
A data type to represent an
m x n
sparse matrix.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiondouble
get
(int i, int j) Returns the entry in this matrix at rowi
and columnj
.static void
Unit tests the data type.int
nCols()
Returns the number of columns in this matrix.int
nRows()
Returns the number of rows in this matrix.plus
(SparseMatrix other) Returns the sum of this matrix andother
.void
put
(int i, int j, double value) Sets the entry at rowi
and columnj
in this matrix tovalue
.int
size()
Returns the number of nonzero entries in this matrix.Returns the product of this matrix and the vectorx
.toString()
Returns a string representation of this matrix.
-
Constructor Details
-
SparseMatrix
public SparseMatrix(int m, int n) Constructs anm x n
dimensional zero matrix.- Parameters:
m
- number of rows in the matrix.n
- number of columns in the matrix.
-
-
Method Details
-
nRows
public int nRows()Returns the number of rows in this matrix.- Returns:
- the number of rows in this matrix.
-
nCols
public int nCols()Returns the number of columns in this matrix.- Returns:
- the number of columns in this matrix.
-
size
public int size()Returns the number of nonzero entries in this matrix.- Returns:
- the number of nonzero entries in this matrix.
-
put
public void put(int i, int j, double value) Sets the entry at rowi
and columnj
in this matrix tovalue
.- Parameters:
i
- row index.j
- column index.value
- the value.
-
get
public double get(int i, int j) Returns the entry in this matrix at rowi
and columnj
.- Parameters:
i
- row index.j
- column index.- Returns:
- the entry in this matrix at row
i
and columnj
.
-
plus
Returns the sum of this matrix andother
.- Parameters:
other
- the other matrix.- Returns:
- the sum of this matrix and
other
.
-
times
Returns the product of this matrix and the vectorx
.- Parameters:
x
- the vector.- Returns:
- the product of this matrix and the vector
x
.
-
toString
Returns a string representation of this matrix. -
main
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-