Class DenseSparseMatrix

java.lang.Object
org.tribuo.math.la.DenseSparseMatrix
All Implemented Interfaces:
Serializable, Iterable<MatrixTuple>, Matrix, Tensor

public class DenseSparseMatrix extends Object implements Matrix
A matrix which is dense in the first dimension and sparse in the second.

Backed by an array of SparseVector.

See Also:
  • Constructor Details

    • DenseSparseMatrix

      public DenseSparseMatrix(List<SparseVector> values)
      Constructs a DenseSparseMatrix out of the supplied sparse vector list.

      Throws IllegalArgumentException if the list forms a ragged matrix.

      Does not copy the vectors, it directly wraps them.

      Parameters:
      values - The SparseVectors.
    • DenseSparseMatrix

      public DenseSparseMatrix(DenseSparseMatrix other)
      Creates a new DenseSparseMatrix by deep copying the supplied DenseSparseMatrix.
      Parameters:
      other - The matrix to copy.
    • DenseSparseMatrix

      public DenseSparseMatrix(int dim1, int dim2)
      Creates a DenseSparseMatrix with no values or indices.

      Used as a placeholder in the gradient system.

      Parameters:
      dim1 - The first dimension.
      dim2 - The second dimension.
  • Method Details

    • createFromSparseVectors

      public static DenseSparseMatrix createFromSparseVectors(SparseVector[] values)
      Defensively copies the values.
      Parameters:
      values - The sparse vectors to use.
      Returns:
      A DenseSparseMatrix containing the supplied vectors.
    • getShape

      public int[] getShape()
      Description copied from interface: Tensor
      Returns an int array specifying the shape of this Tensor.
      Specified by:
      getShape in interface Tensor
      Returns:
      An int array.
    • reshape

      public Tensor reshape(int[] newShape)
      Description copied from interface: Tensor
      Reshapes the Tensor to the supplied shape. Throws IllegalArgumentException if the shape isn't compatible.
      Specified by:
      reshape in interface Tensor
      Parameters:
      newShape - The desired shape.
      Returns:
      A Tensor of the desired shape.
    • copy

      public DenseSparseMatrix copy()
      Description copied from interface: Matrix
      Copies the matrix.
      Specified by:
      copy in interface Matrix
      Specified by:
      copy in interface Tensor
      Returns:
      A copy of the matrix.
    • get

      public double get(int i, int j)
      Description copied from interface: Matrix
      Gets an element from this Matrix.
      Specified by:
      get in interface Matrix
      Parameters:
      i - The index for the first dimension.
      j - The index for the second dimension.
      Returns:
      The value at matrix[i][j].
    • set

      public void set(int i, int j, double value)
      Description copied from interface: Matrix
      Sets an element at the supplied location.
      Specified by:
      set in interface Matrix
      Parameters:
      i - The index for the first dimension.
      j - The index for the second dimension.
      value - The value to be stored at matrix[i][j].
    • getDimension1Size

      public int getDimension1Size()
      Description copied from interface: Matrix
      The size of the first dimension.
      Specified by:
      getDimension1Size in interface Matrix
      Returns:
      The size of the first dimension.
    • getDimension2Size

      public int getDimension2Size()
      Description copied from interface: Matrix
      The size of the second dimension.
      Specified by:
      getDimension2Size in interface Matrix
      Returns:
      The size of the second dimension.
    • leftMultiply

      public DenseVector leftMultiply(SGDVector input)
      Description copied from interface: Matrix
      Multiplies this Matrix by a SGDVector returning a vector of the appropriate size.

      The input must have dimension equal to Matrix.getDimension2Size().

      Specified by:
      leftMultiply in interface Matrix
      Parameters:
      input - The input vector.
      Returns:
      A new SGDVector of size Matrix.getDimension1Size().
    • rightMultiply

      public DenseVector rightMultiply(SGDVector input)
      rightMultiply is very inefficient on DenseSparseMatrix due to the storage format.
      Specified by:
      rightMultiply in interface Matrix
      Parameters:
      input - The input vector.
      Returns:
      A*input.
    • add

      public void add(int i, int j, double value)
      Description copied from interface: Matrix
      Adds the argument value to the value at the supplied index.
      Specified by:
      add in interface Matrix
      Parameters:
      i - The index for the first dimension.
      j - The index for the second dimension.
      value - The value to add.
    • intersectAndAddInPlace

      public void intersectAndAddInPlace(Tensor other, DoubleUnaryOperator f)
      Only implemented for DenseMatrix.
      Specified by:
      intersectAndAddInPlace in interface Tensor
      Parameters:
      other - The other Tensor.
      f - A function to apply.
    • hadamardProductInPlace

      public void hadamardProductInPlace(Tensor other, DoubleUnaryOperator f)
      Only implemented for DenseMatrix.
      Specified by:
      hadamardProductInPlace in interface Tensor
      Parameters:
      other - The other Tensor.
      f - A function to apply.
    • foreachInPlace

      public void foreachInPlace(DoubleUnaryOperator f)
      Description copied from interface: Tensor
      Applies a DoubleUnaryOperator elementwise to this Tensor.
      Specified by:
      foreachInPlace in interface Tensor
      Parameters:
      f - The function to apply.
    • numActiveElements

      public int numActiveElements(int row)
      Description copied from interface: Matrix
      The number of non-zero elements in that row.

      An element could be active and zero, if it was active on construction.

      Specified by:
      numActiveElements in interface Matrix
      Parameters:
      row - The index of the row.
      Returns:
      The number of non-zero elements.
    • getRow

      public SparseVector getRow(int i)
      Description copied from interface: Matrix
      An SGDVector view of the row.

      This refers to the same values as the matrix, so updating this vector will update the matrix.

      Specified by:
      getRow in interface Matrix
      Parameters:
      i - The index of the row to extract.
      Returns:
      An SGDVector.
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • twoNorm

      public double twoNorm()
      Description copied from interface: Tensor
      Calculates the euclidean norm for this vector.
      Specified by:
      twoNorm in interface Tensor
      Returns:
      The euclidean norm.
    • matrixMultiply

      public DenseMatrix matrixMultiply(Matrix other)
      Description copied from interface: Matrix
      Multiplies this Matrix by another Matrix returning a matrix of the appropriate size.

      The input must have dimension 1 equal to Matrix.getDimension2Size().

      Specified by:
      matrixMultiply in interface Matrix
      Parameters:
      other - The input matrix.
      Returns:
      A new Matrix of size Matrix.getDimension1Size(), input.getDimension2Size().
    • matrixMultiply

      public DenseMatrix matrixMultiply(Matrix other, boolean transposeThis, boolean transposeOther)
      Description copied from interface: Matrix
      Multiplies this Matrix by another Matrix returning a matrix of the appropriate size.

      Must obey the rules of matrix multiplication after the transposes are applied.

      Specified by:
      matrixMultiply in interface Matrix
      Parameters:
      other - The input matrix.
      transposeThis - Implicitly transposes this matrix just for the multiplication.
      transposeOther - Implicitly transposes other just for the multiplication.
      Returns:
      A new Matrix.
    • rowSum

      public DenseVector rowSum()
      Description copied from interface: Matrix
      Generates a DenseVector representing the sum of each row.
      Specified by:
      rowSum in interface Matrix
      Returns:
      A new DenseVector of size Matrix.getDimension1Size().
    • rowScaleInPlace

      public void rowScaleInPlace(DenseVector scalingCoefficients)
      Description copied from interface: Matrix
      Scales each row by the appropriate value in the DenseVector.
      Specified by:
      rowScaleInPlace in interface Matrix
      Parameters:
      scalingCoefficients - A DenseVector with size Matrix.getDimension1Size().
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • iterator

      public MatrixIterator iterator()
      Specified by:
      iterator in interface Iterable<MatrixTuple>