Class GenericMatrixFactory<E>

java.lang.Object
com.singularsys.extensions.matrix.genericmat.GenericMatrixFactory<E>
Type Parameters:
E - Base type of elements for the matrices or vectors
All Implemented Interfaces:
MatrixFactoryI, JepComponent, Serializable
Direct Known Subclasses:
ComplexMatrixFactory, ObjectMatrixFactory, RationalMatrixFactory

public class GenericMatrixFactory<E> extends Object implements MatrixFactoryI
An abstract matrix factory for vectors and matrices over a specific type such as Object or Complex.

A typical implementation for Complex numbers would be

 public class ComplexMatrixFactory extends 
    GenericMatrixFactory<ComplexMatrix,ComplexVector,Complex> {
    private static final long serialVersionUID = 340L;

    public ComplexMatrixFactory() {
        super(new Complex(0.0,0.0),new Complex(1.0,0.0));
    }

    @Override
    public Complex[][] buildDataArray(int rows, int cols) {
        return new Complex[rows][cols];
    }

    @Override
    public Complex[] buildDataArray(int len) {
        return new Complex[len];
    }

    @Override
    public Complex elementValue(Object o) {
        if(o instanceof Complex)
            return (Complex) o;
        return null;
    }

    @Override
    public ComplexMatrix newMatrixG(Complex[][] data) {
        return new ComplexMatrix(data);
    }

    @Override
    public ComplexVector newVectorG(Complex[] data) {
        return new ComplexVector(data);
    }

    @Override
    public ComplexMatrix cast(MatrixI m) {
        return (ComplexMatrix) m;
    }

    @Override
    public ComplexVector cast(VectorI v) {
        return (ComplexVector) v;
    }

    public class ComplexMatrix extends GenericMatrix<Complex> {

        protected ComplexMatrix(Complex[][] data) {
            super(data);
        }

        @Override
        public void setEle(int row, int col, Object val) throws EvaluationException {
            if(val instanceof Complex)
                this.setEleG(row, col, (Complex) val);
            else
                throw new EvaluationException("Complex matrix setEle: element should be either Complex or Matrix, it was "+val.toString());
        }

    }

    public class ComplexVector extends GenericVector<Complex> {

        public ComplexVector(Complex[] data) {
            super(data);
        }

        @Override
        public void setEle(int index, Object val) throws EvaluationException {
            if(val instanceof Complex)
                this.setEleG(index, (Complex) val);
            else
                throw new EvaluationException("Complex vector setEle: element should be either Complex or Matrix, it was "+val.toString());
        }
    }
}
 
See Also:
  • Field Details

    • ZERO

      protected final E ZERO
    • ONE

      protected final E ONE
  • Constructor Details

    • GenericMatrixFactory

      public GenericMatrixFactory(GenericField<E> f)
      Parameters:
      f - base field to uses
    • GenericMatrixFactory

      public GenericMatrixFactory(E zero, E one)
  • Method Details

    • elementValue

      public E elementValue(Object o)
      Convert the element o to type E. A typical implementation will just use
       public abstract E elementValue(Object o) {
           if(o instanceof E)
               return (E) o;
           return null;
       }
      Specified by:
      elementValue in interface MatrixFactoryI
      Parameters:
      o - value to convert
      Returns:
      the value cast to type E or null if the type cannot be converted.
    • cast

      protected GenericMatrix<E> cast(MatrixI m)
      A typical implementation will just use return (GenericMatrix<E>) m;
      Parameters:
      m - the matrix to cast
      Returns:
      a matrix of type GenericMatrix<E> = GenericMatrix<E>
    • cast

      protected GenericVector<E> cast(VectorI v)
      A typical implementation will just use return (V) v;
      Parameters:
      v - the vector to cast
      Returns:
      a vector of type V = GenericVector<E>
    • zeroElement

      protected E zeroElement()
      The element representing 0.
    • getONE

      public E getONE()
      The element representing 1.
    • buildDataArray

      protected Object[][] buildDataArray(int rows, int cols)
      Build a data array. A typical concrete implementation will just use return new E[rows][cols].
      Parameters:
      rows - number of rows
      cols - number of cols
      Returns:
      the array.
    • buildDataArray

      protected Object[] buildDataArray(int len)
      Build a data array. A typical concrete implementation will just use return new E[rows][len].
      Parameters:
      len - number of elements
      Returns:
      the array.
    • newMatrixUnchecked

      public GenericMatrix<E> newMatrixUnchecked(Object[][] data)
    • newMatrix

      public GenericMatrix<E> newMatrix(Object[][] data) throws EvaluationException
      Subclasses do not need to implement this method.
      Specified by:
      newMatrix in interface MatrixFactoryI
      Parameters:
      data -
      Returns:
      a new MatrixI the precise type depends on the implementing class
      Throws:
      EvaluationException
    • newVectorUnchecked

      public GenericVector<E> newVectorUnchecked(Object[] data)
    • newVector

      public GenericVector<E> newVector(Object... data) throws EvaluationException
      Subclasses do not need to implement this method.
      Specified by:
      newVector in interface MatrixFactoryI
      Parameters:
      data -
      Returns:
      a new VectorI the precise type depends on the implementing class
      Throws:
      EvaluationException
    • newVector

      public VectorI newVector(Collection<?> set) throws EvaluationException
      Description copied from interface: MatrixFactoryI
      Create a new VectorI from a collection. Default implementation simple calls newVector(set.toArray()). Implementing classes can override this method to provide more efficient implementations.
      Specified by:
      newVector in interface MatrixFactoryI
      Parameters:
      set - any collection including Lists.
      Returns:
      a new vector
      Throws:
      EvaluationException
    • identity

      public GenericMatrix<E> identity(int size) throws EvaluationException
      Description copied from interface: MatrixFactoryI
      Create a square identity matrix
      Specified by:
      identity in interface MatrixFactoryI
      Parameters:
      size - size
      Returns:
      a size by size identity matrix
      Throws:
      EvaluationException
    • identity

      public GenericMatrix<E> identity(int rows, int cols)
      Description copied from interface: MatrixFactoryI
      Create an identity matrix.
      Specified by:
      identity in interface MatrixFactoryI
      Parameters:
      rows -
      cols -
      Returns:
      a rows by cols identity matrix
    • zeroMat

      public GenericMatrix<E> zeroMat(int size) throws EvaluationException
      Throws:
      EvaluationException
    • zeroMat

      public GenericMatrix<E> zeroMat(int rows, int cols)
      Description copied from interface: MatrixFactoryI
      Create an zero matrix.
      Specified by:
      zeroMat in interface MatrixFactoryI
      Parameters:
      rows -
      cols -
      Returns:
      a rows by cols matrix
    • zeroMat

      public GenericMatrix<E> zeroMat(Dimensions dimensions) throws EvaluationException
      Description copied from interface: MatrixFactoryI
      Create a matrix with zero elements
      Specified by:
      zeroMat in interface MatrixFactoryI
      Parameters:
      dimensions - dimensions specifying the size of a matrix.
      Returns:
      a matrix filled with zeros
      Throws:
      EvaluationException - if the dimensions are not of order 2
    • zeroVec

      public GenericVector<E> zeroVec(int size)
      Description copied from interface: MatrixFactoryI
      Create a vector with zeros
      Specified by:
      zeroVec in interface MatrixFactoryI
      Parameters:
      size -
      Returns:
      a vector of zeros
    • init

      public void init(Jep jep)
      Description copied from interface: JepComponent
      Initialize the component. This method is called whenever a component is added to Jep. Hence, it allows components to keep track of the other components they may rely on.
      Specified by:
      init in interface JepComponent
      Parameters:
      jep - the current Jep instance
    • getLightWeightInstance

      public JepComponent getLightWeightInstance()
      Returns this.
      Specified by:
      getLightWeightInstance in interface JepComponent
      Returns:
      either a new instance, null or 'this'.
    • duplicateGV

      public GenericVector<E> duplicateGV(GenericVector<E> l)
    • duplicateGM

      public GenericMatrix<E> duplicateGM(GenericMatrix<E> gm)
    • duplicate

      public GenericMatrix<E> duplicate(MatrixI mat) throws EvaluationException
      Description copied from interface: MatrixFactoryI
      Return a copy of the matrix. Default implementation is a little inefficient creating a temporary data array.
      Specified by:
      duplicate in interface MatrixFactoryI
      Parameters:
      mat - matrix to copy
      Returns:
      a copy
      Throws:
      EvaluationException
    • duplicate

      public GenericVector<E> duplicate(VectorI mat) throws EvaluationException
      Description copied from interface: MatrixFactoryI
      Return a copy of the vector. Default implementation is a little inefficient.
      Specified by:
      duplicate in interface MatrixFactoryI
      Parameters:
      mat - vector to copy
      Returns:
      a copy
      Throws:
      EvaluationException