Package dsa

Class SparseMatrix


  • public class SparseMatrix
    extends Object
    A data type to represent an m x n sparse matrix.
    • Constructor Detail

      • SparseMatrix

        public SparseMatrix​(int m,
                            int n)
        Constructs an m x n dimensional zero matrix.
        Parameters:
        m - number of rows in the matrix.
        n - number of columns in the 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 row i and column j in this matrix to value.
        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 row i and column j.
        Parameters:
        i - row index.
        j - column index.
        Returns:
        the entry in this matrix at row i and column j.
      • plus

        public SparseMatrix plus​(SparseMatrix other)
        Returns the sum of this matrix and other.
        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 vector x.
        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.
        Overrides:
        toString in class Object
        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.