Class SimpleBase<T extends SimpleBase<T>>

java.lang.Object
org.ejml.simple.SimpleBase<T>
All Implemented Interfaces:
Serializable, ConstMatrix<T>
Direct Known Subclasses:
SimpleMatrix

public abstract class SimpleBase<T extends SimpleBase<T>> extends Object implements ConstMatrix<T>, Serializable
Parent of SimpleMatrix implements all the standard matrix operations and uses generics to allow the returned matrix type to be changed. This class should be extended instead of SimpleMatrix.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
    protected Matrix
    Internal matrix which this is a wrapper around.
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
     
    protected
    SimpleBase(int numRows, int numCols)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Size of internal array elements.
    cols(int begin, int end)
    Extracts the specified columns from the matrix.
    combine(int insertRow, int insertCol, ConstMatrix<?> _B)
    Creates a new matrix that is a combination of this matrix and matrix B.
    concatColumns(ConstMatrix<?>... matrices)
    Concatenates all the matrices together along their columns.
    concatRows(ConstMatrix<?>... matrices)
    Concatenates all the matrices together along their columns.
    double
    The condition p = 2 number of a matrix is used to measure the sensitivity of the linear system Ax=b.
    Returns the complex conjugate of this matrix.
    void
    Switches from a real to complex matrix
    void
    Switches from a sparse to dense matrix
    void
    Switches from a dense to sparse matrix
    Creates and returns a matrix which is identical to this one.
    protected T
    createComplexMatrix(int numRows, int numCols)
    Creates a complex matrix with the same floating type as 'this'
    Creates a matrix that is the same type and shape
    protected abstract T
    createMatrix(int numRows, int numCols, MatrixType type)
    Used internally for creating new instances of SimpleMatrix.
    protected T
    createRealMatrix(int numRows, int numCols)
    Creates a real matrix with the same floating type as 'this'
    double
    Computes the determinant of the matrix.
    Computes the determinant of a complex matrix.
    If a vector then a square matrix is returned if a matrix then a vector of diagonal ements is returned
    divide(double val)
    Returns the result of dividing each element by 'val': bi,j = ai,j/val
    double
    Computes the dot product (a.k.a.
    eig()
    Returns the Eigen Value Decomposition (EVD) of this matrix.
    Returns a matrix which is the result of an element by element division of 'this' and 'b': ci,j = ai,j/bi,j
    Returns a matrix which is the result of an element by element exp of 'this' ci,j = Math.exp(ai,j)
    Returns a matrix which is the result of an element by element exp of 'this' ci,j = Math.log(ai,j)
    double
    Returns the maximum real value of all the elements in this matrix.
    double
    Returns the maximum absolute value of all the elements in this matrix.
    double
    Returns the minimum real value of all the elements in this matrix.
    double
    Returns the minimum absolute value of all the elements in this matrix.
    Returns a matrix which is the result of an element by element multiplication of 'this' and 'b': ci,j = ai,j*bi,j
    Applies a user defined complex-valued function to a real or complex-valued matrix.
    Applies a user defined real-valued function to a real-valued matrix.
    elementPower(double b)
    Returns a matrix which is the result of an element by element power of 'this' and 'b': ci,j = ai,j ^ b
    Returns a matrix which is the result of an element by element power of 'this' and 'b': ci,j = ai,j ^ bi,j
    double
    Computes the sum of all the elements in the matrix.
    Computes the sum of all the elements in the matrix.
    void
    equation(String equation, Object... variables)
    Allows you to perform an equation in-place on this matrix by specifying the right hand side.
    extractMatrix(int y0, int y1, int x0, int x1)
    Creates a new SimpleMatrix which is a submatrix of this matrix.
    extractVector(boolean extractRow, int element)
    Extracts a row or column from this matrix.
    void
    fill(double val)
    Sets all the elements in this matrix equal to the specified value.

    aij = val
    void
    fillComplex(double real, double imaginary)
    In-place fills the matrix with a complex value.
    double
    get(int index)
    Returns the value of the matrix at the specified index of the 1D row major array.
    double
    get(int row, int col)
    Returns the value of the specified matrix element.
    void
    get(int row, int col, Complex_F64 output)
    Used to get the complex value of a matrix element.
     
    getColumn(int col)
    Returns the specified column in 'this' matrix as a column vector.
     
     
     
     
    double
    getImaginary(int row, int col)
    Returns the imaginary component of the element.
    int
    getIndex(int row, int col)
    Returns the index in the matrix's array.
    <InnerType extends Matrix>
    InnerType
    Returns a reference to the matrix that it uses internally.
    int
    Returns the number of columns in this matrix.
    int
    Returns the number of rows in this matrix.
    double
    getReal(int row, int col)
    Returns the real component of the element.
    getRow(int row)
    Returns the specified row in 'this' matrix as a row vector.
    Returns the type of matrix it is wrapping.
     
    boolean
    Checks to see if any of the elements in this matrix are either NaN or infinite.
    Returns a matrix that contains the imaginary valued portion of a complex matrix.
    void
    insertIntoThis(int insertRow, int insertCol, T B)
    Copy matrix B into this matrix at location (insertRow, insertCol).
    Returns the inverse of this matrix.

    b = a-1
    void
    invoke(Method m, Object... inputs)
     
    boolean
    isIdentical(ConstMatrix<?> _a, double tol)
    Checks to see if matrix 'a' is the same as this matrix within the specified tolerance.
    boolean
    isInBounds(int row, int col)
    Returns true of the specified matrix element is valid element inside this matrix.
    boolean
    Returns true if this matrix is a vector.
    iterator(boolean rowMajor, int minRow, int minCol, int maxRow, int maxCol)
    Creates a new iterator for traversing through a submatrix inside this matrix.
    Computes the Kronecker product between this matrix and the provided B matrix:

    C = kron(A,B)
    loadCSV(String fileName)
    Loads a new matrix from a CSV file.
    protected static SimpleOperations
     
    Returns a real matrix that has the complex magnitude of each element in the matrix.
    minus(double b)
    Returns the result of matrix-double subtraction:

    c = a - b

    where c is the returned matrix, a is this matrix, and b is the passed in double.
    Returns the result of matrix subtraction:

    c = a - b

    where c is the returned matrix, a is this matrix, and b is the passed in matrix.
    minusComplex(double real, double imag)
    Subtracts a complex scalar from each element in the matrix.
    Returns a matrix which is the result of matrix multiplication:

    c = a * b

    where c is the returned matrix, a is this matrix, and b is the passed in matrix.
    Returns a new matrix whose elements are the negative of 'this' matrix's elements.

    bij = -aij
    double
    Computes the Frobenius normal of the matrix:

    normF = Sqrt{ ∑i=1:mj=1:n { aij2} }
    int
    Deprecated.
    Inconsistent API.
    int
    Deprecated.
    Inconsistent API.
    plus(double b)
    Returns the result of scalar addition:

    c = a + b

    where c is the returned matrix, a is this matrix, and b is the passed in double.
    plus(double beta, ConstMatrix<?> _B)
    Performs a matrix addition and scale operation.

    c = a + β*b

    where c is the returned matrix, a is this matrix, and b is the passed in matrix.
    Returns the result of matrix addition:

    c = a + b

    where c is the returned matrix, a is this matrix, and b is the passed in matrix.
    plusComplex(double real, double imag)
    Adds a complex scalar from each element in the matrix.
    void
    Prints the matrix to standard out.
    void
    print(String format)
    Prints the matrix to standard out given a PrintStream.printf(java.lang.String, java.lang.Object...) style floating point format, e.g.
    void
    Prints the number of rows and column in this matrix.
    Computes the Moore-Penrose pseudo-inverse
    Returns a matrix that contains the real valued portion of a complex matrix.
    void
    reshape(int numRows, int numCols)
    Reshapes the matrix to the specified number of rows and columns.
    rows(int begin, int end)
    Extracts the specified rows from the matrix.
    void
    Saves this matrix to a file in a CSV format.
    void
    Saves this matrix to a file in a matrix market format.
    scale(double val)
    Returns the result of scaling each element by 'val':
    bi,j = val*ai,j
    scaleComplex(double real, double imag)
    Scales/multiplies each element in the matrix by the complex number.
    void
    set(int index, double value)
    Assigns an element a value based on its index in the internal array.
    void
    set(int row, int col, double value)
    Assigns the element in the Matrix to the specified value.
    void
    set(int row, int col, double real, double imaginary)
    Used to set the complex value of a matrix element.
    void
    set(int row, int col, Complex_F64 value)
    Used to set the complex value of a matrix element.
    void
    setColumn(int column, int startRow, double... values)
    Assigns consecutive elements inside a column to the provided array.

    A(offset:(offset + values.length),column) = values
    void
    setColumn(int column, ConstMatrix<?> src)
    Copies the vector into the specified column.
    protected void
     
    void
    setRow(int row, int startColumn, double... values)
    Assigns consecutive elements inside a row to the provided array.

    A(row,offset:(offset + values.length)) = values
    void
    setRow(int row, ConstMatrix<?> src)
    Copies the vector into the specified row.
    void
    setTo(T a)
    Sets the elements in this matrix to be equal to the elements in the passed in matrix.
    Solves for X in the following equation:

    x = a-1b

    where 'a' is this matrix and 'b' is an n by p matrix.
    svd()
    Computes a full Singular Value Decomposition (SVD) of this matrix with the eigenvalues ordered from largest to smallest.
    svd(boolean compact)
    Computes the SVD in either compact format or full format.
    double[][]
    Returns 2D array of doubles using the get(int, int) method.
    Converts the array into a string format for display purposes.
    double
    Computes the trace of the matrix.
    Computes the trace of a complex matrix.
    Returns the transpose of this matrix.
    aT
    Returns a matrix that is the conjugate transpose.
    protected abstract T
     
    void
    Sets all the elements in the matrix equal to zero.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.ejml.simple.ConstMatrix

    getImag, getNumElements, imag
  • Field Details

  • Constructor Details

    • SimpleBase

      protected SimpleBase(int numRows, int numCols)
    • SimpleBase

      protected SimpleBase()
  • Method Details

    • createMatrix

      protected abstract T createMatrix(int numRows, int numCols, MatrixType type)
      Used internally for creating new instances of SimpleMatrix. If SimpleMatrix is extended by another class this function should be overridden so that the returned matrices are of the correct type.
      Parameters:
      numRows - number of rows in the new matrix.
      numCols - number of columns in the new matrix.
      type - Type of matrix it should create
      Returns:
      A new matrix.
    • createRealMatrix

      protected T createRealMatrix(int numRows, int numCols)
      Creates a real matrix with the same floating type as 'this'
    • createComplexMatrix

      protected T createComplexMatrix(int numRows, int numCols)
      Creates a complex matrix with the same floating type as 'this'
    • wrapMatrix

      protected abstract T wrapMatrix(Matrix m)
    • getMatrix

      public <InnerType extends Matrix> InnerType getMatrix()

      Returns a reference to the matrix that it uses internally. This is useful when an operation is needed that is not provided by this class.

      Returns:
      Reference to the internal DMatrixRMaj.
    • getDDRM

      public DMatrixRMaj getDDRM()
    • getFDRM

      public FMatrixRMaj getFDRM()
    • getZDRM

      public ZMatrixRMaj getZDRM()
    • getCDRM

      public CMatrixRMaj getCDRM()
    • getDSCC

      public DMatrixSparseCSC getDSCC()
    • getFSCC

      public FMatrixSparseCSC getFSCC()
    • lookupOps

      protected static SimpleOperations lookupOps(MatrixType type)
    • transpose

      public T transpose()

      Returns the transpose of this matrix.
      aT

      Specified by:
      transpose in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      A matrix that is n by m.
      See Also:
    • transposeConjugate

      public T transposeConjugate()
      Returns a matrix that is the conjugate transpose. If real then this is the same as calling ConstMatrix.transpose().
      Specified by:
      transposeConjugate in interface ConstMatrix<T extends SimpleBase<T>>
    • mult

      public T mult(ConstMatrix<?> _B)

      Returns a matrix which is the result of matrix multiplication:

      c = a * b

      where c is the returned matrix, a is this matrix, and b is the passed in matrix.

      Specified by:
      mult in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      _B - A matrix that is n by p. Not modified.
      Returns:
      The results of this operation.
      See Also:
    • kron

      public T kron(ConstMatrix<?> _B)

      Computes the Kronecker product between this matrix and the provided B matrix:

      C = kron(A,B)

      Specified by:
      kron in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      _B - The right matrix in the operation. Not modified.
      Returns:
      Kronecker product between this matrix and B.
      See Also:
    • plus

      public T plus(ConstMatrix<?> _B)

      Returns the result of matrix addition:

      c = a + b

      where c is the returned matrix, a is this matrix, and b is the passed in matrix.

      Specified by:
      plus in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      _B - m by n matrix. Not modified.
      Returns:
      The results of this operation.
    • minus

      public T minus(ConstMatrix<?> _B)

      Returns the result of matrix subtraction:

      c = a - b

      where c is the returned matrix, a is this matrix, and b is the passed in matrix.

      Specified by:
      minus in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      _B - m by n matrix. Not modified.
      Returns:
      The results of this operation.
      See Also:
    • minus

      public T minus(double b)

      Returns the result of matrix-double subtraction:

      c = a - b

      where c is the returned matrix, a is this matrix, and b is the passed in double.

      NOTE: If the matrix is complex then 'b' will be treated like a complex number with imaginary = 0.

      Specified by:
      minus in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      b - Value subtracted from each element
      Returns:
      The results of this operation.
      See Also:
    • minusComplex

      public T minusComplex(double real, double imag)
      Subtracts a complex scalar from each element in the matrix. If the matrix is real, then it will return a complex matrix unless the imaginary component of the scalar is zero.
      Specified by:
      minusComplex in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      real - Real component of scalar value
      imag - Imaginary component of scalar value
      Returns:
      The results of this operation.
    • plus

      public T plus(double b)

      Returns the result of scalar addition:

      c = a + b

      where c is the returned matrix, a is this matrix, and b is the passed in double.

      NOTE: If the matrix is complex then 'b' will be treated like a complex number with imaginary = 0.

      Specified by:
      plus in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      b - Value added to each element
      Returns:
      A matrix that contains the results.
      See Also:
    • plusComplex

      public T plusComplex(double real, double imag)
      Adds a complex scalar from each element in the matrix. If the matrix is real, then it will return a complex matrix unless the imaginary component of the scalar is zero.
      Specified by:
      plusComplex in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      real - Real component of scalar value
      imag - Imaginary component of scalar value
      Returns:
      The results of this operation.
    • plus

      public T plus(double beta, ConstMatrix<?> _B)

      Performs a matrix addition and scale operation.

      c = a + β*b

      where c is the returned matrix, a is this matrix, and b is the passed in matrix.

      NOTE: If the matrix is complex then 'b' will be treated like a complex number with imaginary = 0.

      Specified by:
      plus in interface ConstMatrix<T extends SimpleBase<T>>
      _B - m by n matrix. Not modified.
      Returns:
      A matrix that contains the results.
      See Also:
    • dot

      public double dot(ConstMatrix<?> _v)
      Computes the dot product (a.k.a. inner product) between this vector and vector 'v'.
      Specified by:
      dot in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      _v - The second vector in the dot product. Not modified.
      Returns:
      dot product
    • isVector

      public boolean isVector()
      Returns true if this matrix is a vector. A vector is defined as a matrix that has either one row or column.
      Specified by:
      isVector in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      Returns true for vectors and false otherwise.
    • scale

      public T scale(double val)

      Returns the result of scaling each element by 'val':
      bi,j = val*ai,j

      Specified by:
      scale in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      val - The multiplication factor. If matrix is complex then the imaginary component is zero.
      Returns:
      The scaled matrix.
      See Also:
    • scaleComplex

      public T scaleComplex(double real, double imag)
      Scales/multiplies each element in the matrix by the complex number. If the matrix is real, then it will return a complex matrix unless the imaginary component of the scalar is zero.
      Specified by:
      scaleComplex in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      real - Real component of scalar value
      imag - Imaginary component of scalar value
      Returns:
      Scaled matrix
    • divide

      public T divide(double val)

      Returns the result of dividing each element by 'val': bi,j = ai,j/val

      Specified by:
      divide in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      val - Divisor. If matrix is complex then the imaginary component is zero.
      Returns:
      Matrix with its elements divided by the specified value.
      See Also:
    • invert

      public T invert()

      Returns the inverse of this matrix.

      b = a-1

      If the matrix could not be inverted then SingularMatrixException is thrown. Even if no exception is thrown the matrix could still be singular or nearly singular.

      Specified by:
      invert in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      The inverse of this matrix.
      See Also:
    • pseudoInverse

      public T pseudoInverse()

      Computes the Moore-Penrose pseudo-inverse

      Specified by:
      pseudoInverse in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      inverse computed using the pseudo inverse.
    • solve

      public T solve(ConstMatrix<?> _B)

      Solves for X in the following equation:

      x = a-1b

      where 'a' is this matrix and 'b' is an n by p matrix.

      If the system could not be solved then SingularMatrixException is thrown. Even if no exception is thrown 'a' could still be singular or nearly singular.

      Specified by:
      solve in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      _B - n by p matrix. Not modified.
      Returns:
      The solution for 'x' that is n by p.
      See Also:
    • setTo

      public void setTo(T a)
      Sets the elements in this matrix to be equal to the elements in the passed in matrix. Both matrix must have the same dimension.
      Parameters:
      a - The matrix whose value this matrix is being set to.
    • fill

      public void fill(double val)

      Sets all the elements in this matrix equal to the specified value.

      aij = val

      Parameters:
      val - The value each element is set to.
      See Also:
    • fillComplex

      public void fillComplex(double real, double imaginary)
      In-place fills the matrix with a complex value. If the matrix is real valued, then it will become a complex matrix.
    • zero

      public void zero()
      Sets all the elements in the matrix equal to zero.
      See Also:
    • normF

      public double normF()

      Computes the Frobenius normal of the matrix:

      normF = Sqrt{ ∑i=1:mj=1:n { aij2} }

      Specified by:
      normF in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      The matrix's Frobenius normal.
      See Also:
    • conditionP2

      public double conditionP2()

      The condition p = 2 number of a matrix is used to measure the sensitivity of the linear system Ax=b. A value near one indicates that it is a well conditioned matrix.

      Specified by:
      conditionP2 in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      The condition number.
      See Also:
    • determinant

      public double determinant()
      Computes the determinant of the matrix.
      Specified by:
      determinant in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      The determinant.
      See Also:
    • determinantComplex

      public Complex_F64 determinantComplex()
      Computes the determinant of a complex matrix. If the matrix is real then the imaginary component is always zero.
      Specified by:
      determinantComplex in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      The determinant.
      See Also:
    • trace

      public double trace()

      Computes the trace of the matrix.

      Specified by:
      trace in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      The trace of the matrix.
      See Also:
    • traceComplex

      public Complex_F64 traceComplex()

      Computes the trace of a complex matrix. If the matrix is real then the imaginary component is always zero.

      Specified by:
      traceComplex in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      The trace of the matrix.
      See Also:
    • reshape

      public void reshape(int numRows, int numCols)

      Reshapes the matrix to the specified number of rows and columns. If the total number of elements is ≤ number of elements it had before the data is saved. Otherwise a new internal array is declared and the old data lost.

      This is equivalent to calling A.getMatrix().reshape(numRows,numCols,false).

      Parameters:
      numRows - The new number of rows in the matrix.
      numCols - The new number of columns in the matrix.
      See Also:
    • set

      public void set(int row, int col, double value)
      Assigns the element in the Matrix to the specified value. Performs a bounds check to make sure the requested element is part of the matrix.
      Parameters:
      row - The row of the element.
      col - The column of the element.
      value - The element's new value.
    • set

      public void set(int index, double value)
      Assigns an element a value based on its index in the internal array.
      Parameters:
      index - The matrix element that is being assigned a value.
      value - The element's new value.
    • set

      public void set(int row, int col, double real, double imaginary)
      Used to set the complex value of a matrix element.
      Parameters:
      row - The row of the element.
      col - The column of the element.
      real - Real component of assigned value
      imaginary - Imaginary component of assigned value
    • set

      public void set(int row, int col, Complex_F64 value)
      Used to set the complex value of a matrix element.
      Parameters:
      row - The row of the element.
      col - The column of the element.
      value - The value that the element is being assigned to
    • setRow

      public void setRow(int row, int startColumn, double... values)

      Assigns consecutive elements inside a row to the provided array.

      A(row,offset:(offset + values.length)) = values

      Parameters:
      row - The row that the array is to be written to.
      startColumn - The initial column that the array is written to.
      values - Values which are to be written to the row in a matrix.
    • setRow

      public void setRow(int row, ConstMatrix<?> src)

      Copies the vector into the specified row. The 'src' vector can be a row or column vector as long as it has the correct length.

      Parameters:
      row - Row in 'this'
      src - Vector which is to be copied into the row
    • setColumn

      public void setColumn(int column, int startRow, double... values)

      Assigns consecutive elements inside a column to the provided array.

      A(offset:(offset + values.length),column) = values

      Parameters:
      column - The column that the array is to be written to.
      startRow - The initial column that the array is written to.
      values - Values which are to be written to the row in a matrix.
    • setColumn

      public void setColumn(int column, ConstMatrix<?> src)

      Copies the vector into the specified column. The 'src' vector can be a row or column vector as long as it has the correct length.

      Parameters:
      column - Column in 'this'
      src - Vector which is to be copied into the column
    • get

      public double get(int row, int col)
      Returns the value of the specified matrix element. Performs a bounds check to make sure the requested element is part of the matrix.

      NOTE: Complex matrices will throw an exception

      Specified by:
      get in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      row - The row of the element.
      col - The column of the element.
      Returns:
      The value of the element.
    • get

      public double get(int index)
      Returns the value of the matrix at the specified index of the 1D row major array.
      Specified by:
      get in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      index - The element's index whose value is to be returned
      Returns:
      The value of the specified element.
      See Also:
    • get

      public void get(int row, int col, Complex_F64 output)
      Used to get the complex value of a matrix element.
      Specified by:
      get in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      row - The row of the element.
      col - The column of the element.
      output - Storage for the value
    • getReal

      public double getReal(int row, int col)
      Returns the real component of the element. If a real matrix this is the same as calling ConstMatrix.get(int, int).
      Specified by:
      getReal in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      row - The row of the element.
      col - The column of the element.
    • getImaginary

      public double getImaginary(int row, int col)
      Returns the imaginary component of the element. If a real matrix this will always be zero.
      Specified by:
      getImaginary in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      row - The row of the element.
      col - The column of the element.
    • getIndex

      public int getIndex(int row, int col)
      Returns the index in the matrix's array.
      Specified by:
      getIndex in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      row - The row number.
      col - The column number.
      Returns:
      The index of the specified element.
      See Also:
    • iterator

      public DMatrixIterator iterator(boolean rowMajor, int minRow, int minCol, int maxRow, int maxCol)
      Creates a new iterator for traversing through a submatrix inside this matrix. It can be traversed by row or by column. Range of elements is inclusive, e.g. minRow = 0 and maxRow = 1 will include rows 0 and 1. The iteration starts at (minRow,minCol) and ends at (maxRow,maxCol)
      Specified by:
      iterator in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      rowMajor - true means it will traverse through the submatrix by row first, false by columns.
      minRow - first row it will start at.
      minCol - first column it will start at.
      maxRow - last row it will stop at.
      maxCol - last column it will stop at.
      Returns:
      A new MatrixIterator
    • copy

      public T copy()
      Creates and returns a matrix which is identical to this one.
      Specified by:
      copy in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      A new identical matrix.
    • numRows

      @Deprecated public int numRows()
      Deprecated.
      Inconsistent API. Use getNumRows() instead.
      Returns the number of rows in this matrix.
      Returns:
      number of rows.
    • numCols

      @Deprecated public int numCols()
      Deprecated.
      Inconsistent API. Use getNumCols() instead.
      Returns the number of columns in this matrix.
      Returns:
      number of columns.
    • getNumRows

      public int getNumRows()
      Returns the number of rows in this matrix.
      Specified by:
      getNumRows in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      number of rows.
    • getNumCols

      public int getNumCols()
      Returns the number of columns in this matrix.
      Specified by:
      getNumCols in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      number of columns.
    • print

      public void print()
      Prints the matrix to standard out.
      Specified by:
      print in interface ConstMatrix<T extends SimpleBase<T>>
    • print

      public void print(String format)

      Prints the matrix to standard out given a PrintStream.printf(java.lang.String, java.lang.Object...) style floating point format, e.g. print("%f").

      Specified by:
      print in interface ConstMatrix<T extends SimpleBase<T>>
    • toArray2

      public double[][] toArray2()
      Returns 2D array of doubles using the get(int, int) method.
      Specified by:
      toArray2 in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      2D array of doubles.
    • toString

      public String toString()

      Converts the array into a string format for display purposes. The conversion is done using MatrixIO.print(java.io.PrintStream, DMatrix).

      Overrides:
      toString in class Object
      Returns:
      String representation of the matrix.
    • extractMatrix

      public T extractMatrix(int y0, int y1, int x0, int x1)

      Creates a new SimpleMatrix which is a submatrix of this matrix.

      si-y0 , j-x0 = oij for all y0 ≤ i < y1 and x0 ≤ j < x1

      where 'sij' is an element in the submatrix and 'oij' is an element in the original matrix.

      If any of the inputs are set to SimpleMatrix.END then it will be set to the last row or column in the matrix.

      Specified by:
      extractMatrix in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      y0 - Start row.
      y1 - Stop row + 1.
      x0 - Start column.
      x1 - Stop column + 1.
      Returns:
      The submatrix.
    • extractVector

      public T extractVector(boolean extractRow, int element)

      Extracts a row or column from this matrix. The returned vector will either be a row or column vector depending on the input type.

      Specified by:
      extractVector in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      extractRow - If true a row will be extracted.
      element - The row or column the vector is contained in.
      Returns:
      Extracted vector.
      See Also:
    • getRow

      public T getRow(int row)
      Returns the specified row in 'this' matrix as a row vector.
      Specified by:
      getRow in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      row - Row in the matrix
      Returns:
      Extracted vector
      See Also:
    • getColumn

      public T getColumn(int col)
      Returns the specified column in 'this' matrix as a column vector.
      Specified by:
      getColumn in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      col - Column in the matrix
      Returns:
      Extracted vector
      See Also:
    • diag

      public T diag()

      If a vector then a square matrix is returned if a matrix then a vector of diagonal ements is returned

      Specified by:
      diag in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      Diagonal elements inside a vector or a square matrix with the same diagonal elements.
      See Also:
    • isIdentical

      public boolean isIdentical(ConstMatrix<?> _a, double tol)
      Checks to see if matrix 'a' is the same as this matrix within the specified tolerance.
      Specified by:
      isIdentical in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      _a - The matrix it is being compared against.
      tol - How similar they must be to be equals.
      Returns:
      If they are equal within tolerance of each other.
    • hasUncountable

      public boolean hasUncountable()
      Checks to see if any of the elements in this matrix are either NaN or infinite.
      Specified by:
      hasUncountable in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      True of an element is NaN or infinite. False otherwise.
    • svd

      public SimpleSVD<T> svd()
      Computes a full Singular Value Decomposition (SVD) of this matrix with the eigenvalues ordered from largest to smallest.
      Returns:
      SVD
    • svd

      public SimpleSVD<T> svd(boolean compact)
      Computes the SVD in either compact format or full format.
      Returns:
      SVD of this matrix.
    • eig

      public SimpleEVD<T> eig()
      Returns the Eigen Value Decomposition (EVD) of this matrix.
    • insertIntoThis

      public void insertIntoThis(int insertRow, int insertCol, T B)
      Copy matrix B into this matrix at location (insertRow, insertCol).
      Parameters:
      insertRow - First row the matrix is to be inserted into.
      insertCol - First column the matrix is to be inserted into.
      B - The matrix that is being inserted.
    • combine

      public T combine(int insertRow, int insertCol, ConstMatrix<?> _B)

      Creates a new matrix that is a combination of this matrix and matrix B. B is written into A at the specified location if needed the size of A is increased by growing it. A is grown by padding the new area with zeros.

      While useful when adding data to a matrix which will be solved for it is also much less efficient than predeclaring a matrix and inserting data into it.

      If insertRow or insertCol is set to SimpleMatrix.END then it will be combined at the last row or column respectively.

      Specified by:
      combine in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      insertRow - Row where matrix B is written in to.
      insertCol - Column where matrix B is written in to.
      _B - The matrix that is written into A.
      Returns:
      A new combined matrix.
    • elementMax

      public double elementMax()
      Returns the maximum real value of all the elements in this matrix.
      Specified by:
      elementMax in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      Largest real value of any element.
    • elementMin

      public double elementMin()
      Returns the minimum real value of all the elements in this matrix.
      Specified by:
      elementMin in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      Smallest real value of any element.
    • elementMaxAbs

      public double elementMaxAbs()
      Returns the maximum absolute value of all the elements in this matrix. This is equivalent to the infinite p-norm of the matrix.
      Specified by:
      elementMaxAbs in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      Largest absolute value of any element.
    • elementMinAbs

      public double elementMinAbs()
      Returns the minimum absolute value of all the elements in this matrix.
      Specified by:
      elementMinAbs in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      Smallest absolute value of any element.
    • elementSum

      public double elementSum()
      Computes the sum of all the elements in the matrix. Only works on real matrices.
      Specified by:
      elementSum in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      Sum of all the elements.
    • elementSumComplex

      public Complex_F64 elementSumComplex()
      Computes the sum of all the elements in the matrix. Works with both real and complex matrices.
      Specified by:
      elementSumComplex in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      Sum of all the elements.
    • elementMult

      public T elementMult(ConstMatrix<?> _B)

      Returns a matrix which is the result of an element by element multiplication of 'this' and 'b': ci,j = ai,j*bi,j

      Specified by:
      elementMult in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      _B - A simple matrix.
      Returns:
      The element by element multiplication of 'this' and 'b'.
    • elementDiv

      public T elementDiv(ConstMatrix<?> _B)

      Returns a matrix which is the result of an element by element division of 'this' and 'b': ci,j = ai,j/bi,j

      Specified by:
      elementDiv in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      _B - A simple matrix.
      Returns:
      The element by element division of 'this' and 'b'.
    • elementPower

      public T elementPower(ConstMatrix<?> _B)

      Returns a matrix which is the result of an element by element power of 'this' and 'b': ci,j = ai,j ^ bi,j

      Specified by:
      elementPower in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      _B - A simple matrix.
      Returns:
      The element by element power of 'this' and 'b'.
    • elementPower

      public T elementPower(double b)

      Returns a matrix which is the result of an element by element power of 'this' and 'b': ci,j = ai,j ^ b

      Specified by:
      elementPower in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      b - Scalar
      Returns:
      The element by element power of 'this' and 'b'.
    • elementExp

      public T elementExp()

      Returns a matrix which is the result of an element by element exp of 'this' ci,j = Math.exp(ai,j)

      Specified by:
      elementExp in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      The element by element power of 'this' and 'b'.
    • elementLog

      public T elementLog()

      Returns a matrix which is the result of an element by element exp of 'this' ci,j = Math.log(ai,j)

      Specified by:
      elementLog in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      The element by element power of 'this' and 'b'.
    • elementOp

      public T elementOp(SimpleOperations.ElementOpReal op)

      Applies a user defined real-valued function to a real-valued matrix.

      ci,j = op(i, j, ai,j)

      If the matrix is sparse then this is only applied to non-zero elements

      Specified by:
      elementOp in interface ConstMatrix<T extends SimpleBase<T>>
    • elementOp

      public T elementOp(SimpleOperations.ElementOpComplex op)

      Applies a user defined complex-valued function to a real or complex-valued matrix.

      ci,j = op(i, j, ai,j)

      If the matrix is sparse then this is only applied to non-zero elements

      Specified by:
      elementOp in interface ConstMatrix<T extends SimpleBase<T>>
    • negative

      public T negative()

      Returns a new matrix whose elements are the negative of 'this' matrix's elements.

      bij = -aij

      Specified by:
      negative in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      A matrix that is the negative of the original.
    • conjugate

      public T conjugate()

      Returns the complex conjugate of this matrix.

      Specified by:
      conjugate in interface ConstMatrix<T extends SimpleBase<T>>
    • magnitude

      public T magnitude()

      Returns a real matrix that has the complex magnitude of each element in the matrix. For a real matrix this is the abs()

      Specified by:
      magnitude in interface ConstMatrix<T extends SimpleBase<T>>
    • equation

      public void equation(String equation, Object... variables)

      Allows you to perform an equation in-place on this matrix by specifying the right hand side. For information on how to define an equation see Equation. The variable sequence alternates between variable and it's label String. This matrix is by default labeled as 'A', but is a string is the first object in 'variables' then it will take on that value. The variable passed in can be any data type supported by Equation can be passed in. This includes matrices and scalars.

      Examples:
       perform("A = A + B",matrix,"B");     // Matrix addition
       perform("A + B",matrix,"B");         // Matrix addition with implicit 'A = '
       perform("A(5,:) = B",matrix,"B");    // Insert a row defined by B into A
       perform("[A;A]");                    // stack A twice with implicit 'A = '
       perform("Q = B + 2","Q",matrix,"B"); // Specify the name of 'this' as Q
      
       
      Parameters:
      equation - String representing the symbol equation
      variables - List of variable names and variables
    • saveToFileCSV

      public void saveToFileCSV(String fileName) throws IOException

      Saves this matrix to a file in a CSV format. For the file format see MatrixIO.

      Specified by:
      saveToFileCSV in interface ConstMatrix<T extends SimpleBase<T>>
      Throws:
      IOException
    • saveToMatrixMarket

      public void saveToMatrixMarket(String fileName) throws IOException

      Saves this matrix to a file in a matrix market format. For the file format see MatrixIO.

      Specified by:
      saveToMatrixMarket in interface ConstMatrix<T extends SimpleBase<T>>
      Throws:
      IOException
    • loadCSV

      public T loadCSV(String fileName) throws IOException

      Loads a new matrix from a CSV file. For the file format see MatrixIO. The returned matrix will be the same matrix type as 'this'.

      Parameters:
      fileName - File which is to be loaded.
      Returns:
      The matrix.
      Throws:
      IOException
      See Also:
    • isInBounds

      public boolean isInBounds(int row, int col)
      Returns true of the specified matrix element is valid element inside this matrix.
      Specified by:
      isInBounds in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      row - Row index.
      col - Column index.
      Returns:
      true if it is a valid element in the matrix.
    • printDimensions

      public void printDimensions()
      Prints the number of rows and column in this matrix.
    • bits

      public int bits()
      Size of internal array elements. 32 or 64 bits
      Specified by:
      bits in interface ConstMatrix<T extends SimpleBase<T>>
    • concatColumns

      public T concatColumns(ConstMatrix<?>... matrices)

      Concatenates all the matrices together along their columns. If the rows do not match the upper elements are set to zero.

      A = [ this, m[0] , ... , m[n-1] ]
      Specified by:
      concatColumns in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      matrices - Set of matrices
      Returns:
      Resulting matrix
    • concatRows

      public T concatRows(ConstMatrix<?>... matrices)

      Concatenates all the matrices together along their columns. If the rows do not match the upper elements are set to zero.

      A = [ this; m[0] ; ... ; m[n-1] ]
      Specified by:
      concatRows in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      matrices - Set of matrices
      Returns:
      Resulting matrix
    • rows

      public T rows(int begin, int end)
      Extracts the specified rows from the matrix.
      Specified by:
      rows in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      begin - First row (inclusive).
      end - Last row (exclusive).
      Returns:
      Submatrix that contains the specified rows.
    • cols

      public T cols(int begin, int end)
      Extracts the specified columns from the matrix.
      Specified by:
      cols in interface ConstMatrix<T extends SimpleBase<T>>
      Parameters:
      begin - First column (inclusive).
      end - Last column (exclusive).
      Returns:
      Submatrix that contains the specified columns.
    • getType

      public MatrixType getType()
      Returns the type of matrix it is wrapping.
      Specified by:
      getType in interface ConstMatrix<T extends SimpleBase<T>>
    • real

      public T real()
      Returns a matrix that contains the real valued portion of a complex matrix. For a real valued matrix this will return a copy.
      Specified by:
      real in interface ConstMatrix<T extends SimpleBase<T>>
    • imaginary

      public T imaginary()
      Returns a matrix that contains the imaginary valued portion of a complex matrix. For a real valued matrix this will return a matrix full of zeros.
      Specified by:
      imaginary in interface ConstMatrix<T extends SimpleBase<T>>
    • createLike

      public T createLike()
      Creates a matrix that is the same type and shape
      Specified by:
      createLike in interface ConstMatrix<T extends SimpleBase<T>>
      Returns:
      New matrix
    • setMatrix

      protected void setMatrix(Matrix mat)
    • invoke

      public void invoke(Method m, Object... inputs)
    • convertToSparse

      public void convertToSparse()
      Switches from a dense to sparse matrix
    • convertToDense

      public void convertToDense()
      Switches from a sparse to dense matrix
    • convertToComplex

      public void convertToComplex()
      Switches from a real to complex matrix