Package com.singularsys.extensions.matrix.genericmat
Generic implementations for constructing vectors and matrices where all the elements are of type <E>. These classes are used as the basic for most of the types of matrices provided.
To use the concrete implementations of the
GenericMatrix
,
GenericVector
,
GenericMatrixFactory
,
and GenericMatrixField
Should be provided.
See com.singularsys.extensions.matrix.complexmat
package for a typical implementation.
For example vectors and matrices where all the elements are Complex
numbers.
First the types ComplexMatrix
and ComplexVector
are implemented.
public class ComplexMatrix extends GenericMatrix<Complex> { protected ComplexMatrix(Object[][] 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 at "+row+", "+col+" should be Complex. It was "+val.toString()); } } public class ComplexVector extends GenericVector<Complex> { public ComplexVector(Object[] 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 at "+index+" should be Complex. It was "+val.toString()); } }
The Factory
public class ComplexMatrixFactory extends GenericMatrixFactory<Complex> { private static final long serialVersionUID = 340L; // Call superclass construction with values for 0 and 1. public ComplexMatrixFactory() { super(new Complex(0.0,0.0),new Complex(1.0,0.0)); } // Attempt to convert an item to type for this class @Override public Complex elementValue(Object o) { if(o instanceof Complex) return (Complex) o; return null; } // If possible convert to an integer @Override public Integer intValue(Object o) throws EvaluationException { if(o instanceof Complex) { Complex c = (Complex) o; if(c.im() != 0.0) return null; Double d = c.re(); int i = d.intValue(); if( i == d) return i; } return null; } // build a matrix given a set of data known to be of the correct type @Override public ComplexMatrix newMatrixUnchecked(Object[][] data) { return new ComplexMatrix(data); } // build a vector known to be of the correct type @Override public ComplexVector newVectorUnchecked(Object[] data) { return new ComplexVector(data); } @Override public ComplexMatrix cast(MatrixI m) { return (ComplexMatrix) m; } @Override public ComplexVector cast(VectorI v) { return (ComplexVector) v; } }
And finally the MatrixField
public final class ComplexMatrixField extends GenericMatrixField<Complex> { private static final long serialVersionUID = 330L; public ComplexMatrixField( ComplexMatrixFactory mf) { super(mf); } @Override protected Complex addEle(Complex l, Complex r) throws EvaluationException { return l.add(r); } .... }
Using a GenericField<E>
If the operations on the base field are provided by a sub class of
GenericField
then the
and
GenericMatrixFactory
and
GenericFieldMatrixField
can be used.
The RationalMatrixFactory
and
The RationalMatrixTest
are an example implementation.
Note that the Vector and Matrix classes are (non static) inner classes of the
Matrix Factory.
- Since:
- Jep 3.5 / Extensions 2.0
-
Class Summary Class Description GenericFieldMatrixField<E> A MatrixField where the elements of the matrices and vectors are of type E and the evaluation is carried out using aGenericPowerField
of type E.GenericMatrix<E> Generic implementation of a matrix when all elements are of the base type E.GenericMatrixFactory<E> An abstract matrix factory for vectors and matrices over a specific type such as Object or Complex.GenericMatrixField<E> A Generic matrix field providing default implementations for all operations.GenericVector<E> Generic implementation of a matrix when all elements are of the base type E.