Class GenericMatrixFactory<E>

  • Type Parameters:
    E - Base type of elements for the matrices or vectors
    All Implemented Interfaces:
    MatrixFactoryI, JepComponent, java.io.Serializable
    Direct Known Subclasses:
    ComplexMatrixFactory, ObjectMatrixFactory, RationalMatrixFactory

    public class GenericMatrixFactory<E>
    extends java.lang.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:
    Serialized Form
    • Field Detail

      • ZERO

        protected final E ZERO
      • ONE

        protected final E ONE
    • Constructor Detail

      • GenericMatrixFactory

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

        public GenericMatrixFactory​(E zero,
                                    E one)
    • Method Detail

      • elementValue

        public E elementValue​(java.lang.Object o)
                       throws EvaluationException
        Convert the element o to type E. A typical implementation will just use
         public abstract E elementValue(Object o) {
             if(o instance of 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.
        Throws:
        EvaluationException
      • 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>
      • zeroEement

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

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

        protected java.lang.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 java.lang.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​(java.lang.Object[][] data)
      • newVectorUnchecked

        public GenericVector<E> newVectorUnchecked​(java.lang.Object[] data)
      • init

        public void init​(Jep jep)
        Description copied from interface: JepComponent
        Initialize the component. This methods 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