Class Matrix<E>

Type Parameters:
E - is the type of the elements of a Matrix.
All Implemented Interfaces:
Filterable<E>, Groupable<E>, IContainer<E>, IFilteringContainerViewProvider<E>, IIntervallContainerViewProvider<E>, IMappingContainerViewProvider<E>, Mappable<E>, AggregationRequestable<E>, ArrayMappable<E>, CountRequestable<E>, IndexRequestable<E>, IterableWithCopyableIterator<E>, SingleSearchable<E>, StoringRequestable<E>, StringMappable, IMatrix<E>, Clearable, EmptinessRequestable, MaterializationRequestable, Iterable<E>

public final class Matrix<E> extends AbstractExtendedContainer<E> implements IMatrix<E>
A Matrix is a AbstractContainer that stores its elements in rows and columns. A Matrix is clearable.
Author:
Silvan Wyss
  • Method Details

    • createEmpty

      public static <T> Matrix<T> createEmpty()
      Type Parameters:
      T - is the type of the elements of the created Matrix.
      Returns:
      a new empty Matrix.
    • fromMatrix

      public static <T> Matrix<T> fromMatrix(IMatrix<T> matrix)
      Type Parameters:
      T - is the type of the elements of the given matrix.
      Parameters:
      matrix -
      Returns:
      a new Matrix with the size and elements of the given matrix.
    • addColumn

      public Matrix<E> addColumn(E... elements)
      Adds a new column to the current Matrix with the given elements. The complexity of this implementation is O(m + n) if: -The current Matrix contains m elements. -n elements are given.
      Parameters:
      elements -
      Returns:
      the current Matrix.
      Throws:
      RuntimeException - if the given elements is null.
      RuntimeException - if one of the given elements is null.
      UnequalArgumentException - if the current Matrix is not empty or not as many elements are given as the number of rows of the current Matrix.
    • addColumn

      public Matrix<E> addColumn(Iterable<E> elements)
      Adds a new column to the current Matrix with the given elements. The complexity of this implementation is O(m + n) if: -The current Matrix contains m elements. -n elements are given.
      Parameters:
      elements -
      Returns:
      the current Matrix.
      Throws:
      RuntimeException - if the given elements is null.
      RuntimeException - if one of the given elements is null.
      UnequalArgumentException - if the current Matrix is not empty and if not as many elements are given as the number of rows of the current Matrix.
    • addRow

      public Matrix<E> addRow(E... elements)
      Adds a new row to the current Matrix with the given elements. The complexity of this implementation is O(m + n) if: -The current Matrix contains m rows. -n elements are given.
      Parameters:
      elements -
      Returns:
      the current Matrix.
      Throws:
      RuntimeException - if the given elements is null.
      RuntimeException - if one of the given elements is null.
      UnequalArgumentException - the current Matrix is not empty and if not as many elements are given as the number of columns of the current Matrix.
    • addRow

      public Matrix<E> addRow(Iterable<E> elements)
      Adds a new row to the current Matrix with the given elements. The complexity of this implementation is O(m + n) if: -The current Matrix contains m rows. -n elements are given.
      Parameters:
      elements -
      Returns:
      the current Matrix.
      Throws:
      RuntimeException - if the given elements is null.
      RuntimeException - if one of the given elements is null.
      UnequalArgumentException - the current Matrix is not empty and if not as many elements are given as the number of columns of the current Matrix.
    • clear

      public void clear()
      Removes all elements of the current Matrix. The complexity of this implementation is O(1).
      Specified by:
      clear in interface Clearable
    • getColumn

      public IMatrixColumn<E> getColumn(int oneBasedColumnIndex)
      Parameters:
      oneBasedColumnIndex -
      Returns:
      the column of the current Matrix at the given oneBasedColumnIndex.
      Throws:
      RuntimeException - if the given oneBasedColumnIndex is not positive or bigger than the number of the columns of the current Matrix.
    • getColumnCount

      public int getColumnCount()
      The time complexity of this implementation is O(1).
      Specified by:
      getColumnCount in interface IMatrix<E>
      Returns:
      the number of columns of the current Matrix.
    • getColumnIndexOf

      public int getColumnIndexOf(int index)
      Parameters:
      index -
      Returns:
      the index of the column of the element of the current Matrix at the given index.
      Throws:
      RuntimeException - if the given index is not positive.
      RuntimeException - if the given index is bigger than the number of elements of the current Matrix.
    • getColumns

      public IContainer<IMatrixColumn<E>> getColumns()
      Specified by:
      getColumns in interface IMatrix<E>
      Returns:
      the columns of the current Matrix.
    • getCopy

      public Matrix<E> getCopy()
      The time complexity of this implementation is O(m * n) if: -This matrix contains m rows. -This matrix contains n columns.
      Returns:
      a new Matrix with the elements of the current Matrix.
    • getCount

      public int getCount()
      The time complexity of this implementation is O(1).
      Specified by:
      getCount in interface CountRequestable<E>
      Returns:
      the number of elements of the current Matrix.
    • getIndexOf

      public int getIndexOf(int rowIndex, int columnIndex)
      Parameters:
      rowIndex -
      columnIndex -
      Returns:
      the index of the element of the current Matrix at the given row index and column index.
      Throws:
      RuntimeException - if the given row index is not positive.
      RuntimeException - if the given row index is bigger than the number of rows of the current Matrix.
      RuntimeException - if the given column index is not positive.
      RuntimeException - if the given column index is bigger than the number of columns of the current Matrix.
    • getStoredAtOneBasedIndex

      public E getStoredAtOneBasedIndex(int oneBasedIndex)
      Specified by:
      getStoredAtOneBasedIndex in interface SingleSearchable<E>
      Parameters:
      oneBasedIndex -
      Returns:
      the element of the current Matrix at the given index .
      Throws:
      RuntimeException - if the given index is not positive.
      RuntimeException - if the given index is bigger than the number of elements of the current Matrix.
    • getStoredAtOneBasedRowIndexAndColumnIndex

      public E getStoredAtOneBasedRowIndexAndColumnIndex(int oneBasedRowIndex, int oneBasedColumnIndex)
      The time complexity of this implementation is O(1).
      Specified by:
      getStoredAtOneBasedRowIndexAndColumnIndex in interface IMatrix<E>
      Parameters:
      oneBasedRowIndex -
      oneBasedColumnIndex -
      Returns:
      the element of the current Matrix at the given row index and column index.
      Throws:
      RuntimeException - if the given row index is not positive.
      RuntimeException - if the given row index is bigger than the number of rows of the current Matrix.
      RuntimeException - if the given column index is not positive.
      RuntimeException - if the given column index is bigger than the number of columns of the current Matrix.
    • getRow

      public IMatrixRow<E> getRow(int oneBasedRowIndex)
      Parameters:
      oneBasedRowIndex -
      Returns:
      the row of the current Matrix at the given oneBasedRowIndex.
      Throws:
      RuntimeException - if the given oneBasedRowIndex is not positive or bigger than the number of rows of the current Matrix.
    • getRowIndexOf

      public int getRowIndexOf(int index)
      Parameters:
      index -
      Returns:
      the index of the row of the element of the current Matrix at the given index.
      Throws:
      RuntimeException - if the given index is not positive.
      RuntimeException - if the given index is bigger than the number of elements of the current Matrix.
    • getRows

      public IContainer<IMatrixRow<E>> getRows()
      Specified by:
      getRows in interface IMatrix<E>
      Returns:
      the rows of the current Matrix.
    • getRowCount

      public int getRowCount()
      The time complexity of this implementation is O(1).
      Specified by:
      getRowCount in interface IMatrix<E>
      Returns:
      the number of rows of the current Matrix.
    • isMaterialized

      public boolean isMaterialized()
      Specified by:
      isMaterialized in interface MaterializationRequestable
      Returns:
      true if the current MaterializationRequestable is materialized, false otherwise.
    • iterator

      public CopyableIterator<E> iterator()
      The time complexity of this implementation is O(1).
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface IterableWithCopyableIterator<E>
      Returns:
      a new iterator for the current Matrix.
    • setAt

      public void setAt(int index, E element)
      Sets the given element to the current Matrix at the given index. The complexity of this implementation is O(1).
      Parameters:
      index -
      element -
      Throws:
      RuntimeException - if the given index is not positive.
      RuntimeException - if the given index is bigger than the number of elements of the current Matrix.
      RuntimeException - if the given element is null.
    • setAtOneBasedRowIndexAndColumnIndex

      public void setAtOneBasedRowIndexAndColumnIndex(int oneBasedRowIndex, int oneBasedColumnIndex, E element)
      Sets the given element to the current Matrix to the row with the given row index and the column with the given column index. The time complexity of this implementation is O(1).
      Specified by:
      setAtOneBasedRowIndexAndColumnIndex in interface IMatrix<E>
      Parameters:
      oneBasedRowIndex -
      oneBasedColumnIndex -
      element -
      Throws:
      RuntimeException - if the given row index is not positive.
      RuntimeException - if the given row index is bigger than the number of rows of the current Matrix.
      RuntimeException - if the given column index is not positive.
      RuntimeException - if the given column index is bigger than the number of columns of the current Matrix.
      RuntimeException - if the given element is null.
    • toMatrix

      public <O> Matrix<O> toMatrix(Function<E,O> transformer)
      The time complexity of this implementation is O(n) if: -This matrix contains n elements. -The given transformer has a complexity of O(1).
      Type Parameters:
      O - is the type of the elements the given transformer returns.
      Parameters:
      transformer -
      Returns:
      a new matrix with the elements the given transformer transforms of the elements of the current Matrix.
    • toLeftRotatedMatrix

      public Matrix<E> toLeftRotatedMatrix()
      The time complexity of this implementation is O(n) if the current Matrix contains n elements.
      Returns:
      a new left rotated Matrix of the current Matrix.
    • toRightRotatedMatrix

      public Matrix<E> toRightRotatedMatrix()
      The time complexity of this implementation is O(n) if the current Matrix contains n elements.
      Returns:
      a new right rotated Matrix of the current Matrix.
    • toString

      public String toString()
      The time complexity of this implementation is O(n) if: -The current Matrix contains n elements. -The toString method of the elements of the current Matrix has a complexity of O(1).
      Overrides:
      toString in class Object
      Returns:
      a String representation of the current Matrix.