Package dsa
Class SparseMatrix
- java.lang.Object
-
- dsa.SparseMatrix
-
public class SparseMatrix extends Object
A data type to represent anm x n
sparse matrix.
-
-
Constructor Summary
Constructors Constructor Description SparseMatrix(int m, int n)
Constructs anm x n
dimensional zero matrix.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description double
get(int i, int j)
Returns the entry in this matrix at rowi
and columnj
.static void
main(String[] args)
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.SparseMatrix
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.SparseVector
times(SparseVector x)
Returns the product of this matrix and the vectorx
.String
toString()
Returns a string representation of this matrix.
-
-
-
Method Detail
-
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
public SparseMatrix plus(SparseMatrix other)
Returns the sum of this matrix andother
.- Parameters:
other
- the other matrix.- Returns:
- the sum of this matrix and
other
.
-
times
public SparseVector times(SparseVector x)
Returns the product of this matrix and the vectorx
.- Parameters:
x
- the vector.- Returns:
- the product of this matrix and the vector
x
.
-
toString
public String toString()
Returns a string representation of this matrix.
-
main
public static void main(String[] args)
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-
-