commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arne Plöse (JIRA) <j...@apache.org>
Subject [jira] [Commented] (MATH-571) make FieldVector generic
Date Wed, 11 May 2011 10:16:47 GMT

    [ https://issues.apache.org/jira/browse/MATH-571?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13031627#comment-13031627
] 

Arne Plöse commented on MATH-571:
---------------------------------

Here is my latest, if it looks promising, I will give it a try to make the big step and change
the sources, test ...
If you thing this is not the way things should go, let me know before I waste my time on it.

/*
 * To each fieldElement interface belangs a fieldvector and fieldMatrix interface.
 * if I create a new fieldElement I need a new fieldvector and fieldMatrix.
 * The Abstract* classes implement these iterfaces and are open
 * the classes without the Abstract prefix are final and hide the vector and maticx interfaces...
 * so each new fieldElement results in 3 new interfaces and 4 new classes :-)
 */
public class App 
{
    public static interface FieldElement<T> {

        T add(T a);

 //not really needed, but just to male sure one can mutiply a sclar to a vector ... vector
times scalar is implemented in vector
       <V extends FieldVector> V mapMultiply(V v);
    }

    public static interface FieldVector<T extends FieldElement<T>, M extends FieldMatrix,
V extends FieldVector> {

        V ebeMultiply(FieldVector<T, M, V> v);

        V mapMultiply(T v);

        M multiply(FieldVector<T, M, V> v);

        V createVector(T[] data);

        V createVector(FieldVector<T, ?, ?> v);

        M createMatrix(T[][] data);

        M createMatrix(FieldMatrix<T, ?, ?> v);

        T procuct();
    }

    public static interface FieldMatrix<T extends FieldElement<T>, M extends FieldMatrix,
V extends FieldVector> {

        M multiply(FieldVector<T, M, V> v);

        M createMatrix(T[][] data);

        M createMatrix(FieldMatrix<T, ?, ?> v);

        V createVector(T[] data);

        V createVector(FieldVector<T, ?, ?> v);

        V getRow(int i);
    }

    // generic interface that one can implement a BigReal ... CoplexClass if needed
    public static interface ComplexFieldElement<T extends ComplexFieldElement<T>>
extends FieldElement<T> {

        T conjugate();


    }

    public static interface GenericComplexFieldVector<T extends ComplexFieldElement<T>,
M extends GenericComplexFieldMatrix, V extends GenericComplexFieldVector> extends FieldVector<T,
M, V> {

        V conjugate();

    }

    public static interface GenericComplexFieldMatrix<T extends ComplexFieldElement<T>,
M extends GenericComplexFieldMatrix, V extends GenericComplexFieldVector> extends FieldMatrix<T,
M, V> {

        M conjugate();

    }

    public abstract static class AbstractArrayFieldVector<T extends FieldElement<T>,
M extends FieldMatrix, V extends FieldVector> implements FieldVector<T, M, V> {

        protected T[] data;

     @Override
        public V ebeMultiply(FieldVector<T, M, V> v) {
            return createVector(data);
        }

     @Override
        public V mapMultiply(T v) {
            return createVector(data);
        }

     @Override
        public M multiply(FieldVector<T, M, V> v) {
            return createMatrix((T[][])null);
        }
     @Override
        public T procuct() {
            return null;
        }

    }

    public abstract static class AbstractArrayFieldMatrix<T extends FieldElement<T>,
M extends FieldMatrix, V extends FieldVector> implements FieldMatrix<T, M, V> {

        protected T[][] data;

        @Override
        public M multiply(FieldVector<T, M, V> v) {
            return createMatrix(data);
        }

        @Override
        public V getRow(int i) {
            return createVector((T[])null);
        }
    }

    // get rid of the vector and matrix
    public final static class ArrayFieldVector<T extends FieldElement<T>> extends
AbstractArrayFieldVector<T, FieldMatrix, FieldVector> {

        @Override
        public ArrayFieldVector createVector(T[] data) {
            ArrayFieldVector result = new ArrayFieldVector();
            result.data = data;
            return result;
        }

        @Override
        public FieldMatrix createMatrix(T[][] data) {
            ArrayFieldMatrix result = new ArrayFieldMatrix();
            result.data = data;
            return result;
        }

        @Override
        public FieldVector createVector(FieldVector<T, ?, ?> v) {
            ArrayFieldVector result = new ArrayFieldVector();
            return result;
        }

        @Override
        public FieldMatrix createMatrix(FieldMatrix<T, ?, ?> v) {
            ArrayFieldMatrix result = new ArrayFieldMatrix();
            return result;
        }

    }

    // get rid of the vector and matrix
    public final static class ArrayFieldMatrix<T extends FieldElement<T>> extends
AbstractArrayFieldMatrix<T, FieldMatrix, FieldVector> {

        @Override
        public FieldVector createVector(T[] data) {
            ArrayFieldVector result = new ArrayFieldVector();
            result.data = data;
            return result;
        }

        @Override
        public FieldMatrix createMatrix(T[][] data) {
            ArrayFieldMatrix result = new ArrayFieldMatrix();
            result.data = data;
            return result;
        }

        @Override
        public FieldMatrix createMatrix(FieldMatrix<T, ?, ?> v) {
            ArrayFieldMatrix result = new ArrayFieldMatrix();
            return result;
        }

        @Override
        public FieldVector createVector(FieldVector<T, ?, ?> v) {
            ArrayFieldVector result = new ArrayFieldVector();
            return result;
        }

    }

    public abstract static class AbstractArrayComplexVector<T extends ComplexFieldElement<T>,
M extends  GenericComplexFieldMatrix, V extends GenericComplexFieldVector> extends AbstractArrayFieldVector<T,
M, V> implements GenericComplexFieldVector<T, M, V>{

        @Override
        public V conjugate() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

   }

    public abstract static class AbstractArrayComplexMatrix<T extends ComplexFieldElement<T>,
M extends  GenericComplexFieldMatrix, V extends GenericComplexFieldVector> extends AbstractArrayFieldMatrix<T,
M, V> implements GenericComplexFieldMatrix<T, M, V> {

        @Override
        public M conjugate() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

    }

    // should better be named ComplexDouble ...
        public static class Complex implements ComplexFieldElement<Complex> {

        public double getReal() {
            return 1;
        }

        @Override
        public Complex add(Complex a) {
            return new Complex();
        }

        @Override
        public Complex conjugate() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public <V extends FieldVector> V mapMultiply(V v) {
            return (V)v.mapMultiply(this); // Call vectors mapMultiply
        }
    }


    // should better be named ComplexDoubleVector ...
    public static interface ComplexVector extends GenericComplexFieldVector<Complex, ComplexMatrix,
ComplexVector> {

        double[] getReal();

    }

    // should better be named ComplexDoubleMatrix ...
    public static interface ComplexMatrix extends GenericComplexFieldMatrix<Complex, ComplexMatrix,
ComplexVector> {

        double[][] getReal();
    }

    // get rid of the fieldElement, vector and matrix
    public final static class ArrayComplexVector extends AbstractArrayComplexVector<Complex,
ComplexMatrix, ComplexVector> implements ComplexVector {

        @Override
        public ComplexVector createVector(Complex[] data) {
            ArrayComplexVector result = new ArrayComplexVector();
            result.data = data;
            return result;
        }

        @Override
        public ComplexMatrix createMatrix(Complex[][] data) {
            ArrayComplexMatrix result = new ArrayComplexMatrix();
            return result;
        }

        @Override
        public double[] getReal() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public ComplexVector createVector(FieldVector<Complex, ?, ?> v) {
            ArrayComplexVector result = new ArrayComplexVector();
            return result;
        }

        @Override
        public ComplexMatrix createMatrix(FieldMatrix<Complex, ?, ?> v) {
            ArrayComplexMatrix result = new ArrayComplexMatrix();
            return result;
        }

    }

    // get rid of the fieldElement, vector and matrix  ... its just a matric of Copmplex
    public final static class ArrayComplexMatrix extends AbstractArrayComplexMatrix<Complex,
ComplexMatrix, ComplexVector> implements ComplexMatrix {

        @Override
        public ComplexVector createVector(Complex[] data) {
            ArrayComplexVector result = new ArrayComplexVector();
            result.data = data;
            return result;
        }

        @Override
        public ComplexMatrix createMatrix(Complex[][] data) {
            ArrayComplexMatrix result = new ArrayComplexMatrix();
            return result;
        }

        @Override
        public double[][] getReal() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public ComplexMatrix createMatrix(FieldMatrix<Complex, ?, ?> v) {
            ArrayComplexMatrix result = new ArrayComplexMatrix();
            return result;
        }

        @Override
        public ComplexVector createVector(FieldVector<Complex, ?, ?> v) {
            ArrayComplexVector result = new ArrayComplexVector();
            return result;
        }

    }

    public static class SomeFancyStuff<T extends FieldElement<T>, M extends FieldMatrix,
V extends FieldVector> {
        private V v;
        private M m;

        public SomeFancyStuff(M m) {
            this.m = m;
            v = (V)m.getRow(0);
        }

        V getV() {
            return v;
        }

    }

    public static class SomeFancyStuffComplex<T extends ComplexFieldElement<T>, M
extends ComplexMatrix, V extends ComplexVector> {
        private V v;
        private M m;

        public SomeFancyStuffComplex(M m) {
            this.m = m;
            v = (V)m.getRow(0);
        }

        V getV() {
            return v;
        }


    }

public static void main(String [] args) {
        ComplexVector v = new ArrayComplexVector();
        ComplexMatrix m1 = v.multiply(new ArrayComplexVector());  // FiledVector type survives
...
        FieldVector<Complex, ?, ?> v2 = m1.getRow(0);
        ComplexVector v3 = m1.getRow(1);
        SomeFancyStuff<Complex, FieldMatrix, FieldVector> sf1 = new SomeFancyStuff<Complex,
FieldMatrix, FieldVector>(new ArrayFieldMatrix<Complex>());
        SomeFancyStuffComplex sf2 = new SomeFancyStuffComplex(new ArrayComplexMatrix());
        SomeFancyStuff<Complex, FieldMatrix, FieldVector> sf3 = new SomeFancyStuff<Complex,
FieldMatrix, FieldVector>(new ArrayComplexMatrix());
        v2= sf1.getV();
        v3 = sf2.getV();
        v2 = sf3.getV();
        Complex c = new Complex();
        ComplexVector v4 = c.mapMultiply(v);
        FieldVector<Complex, FieldMatrix, FieldVector> vc = c.mapMultiply(new ArrayFieldVector<Complex>());
        Complex c1 = vc.procuct();
    }
}

> make FieldVector generic
> ------------------------
>
>                 Key: MATH-571
>                 URL: https://issues.apache.org/jira/browse/MATH-571
>             Project: Commons Math
>          Issue Type: Improvement
>    Affects Versions: 3.0
>            Reporter: Arne Plöse
>            Priority: Minor
>
> make FieldVector generic, so one can extend i.e. ArrayVieldVector<Complex> to ArrayComplexVector
an introduce new methoids (getReal())...
> if one has an equation complexvector.copy the original type ArrayComplexVector is lost
thus access to getReal() is not possible.
> solution:
> public class InheritationTest {
>     public static interface FieldVector<T extends FieldElement<T>, R extends
FieldVector> {
>         R copy();
>     }
>     public abstract static class ArrayFieldVectorExtendable<T extends FieldElement<T>,
R extends FieldVector> implements FieldVector<T, R>, Serializable {
>         protected T[] data;
>         @Override
>         public R copy() {
>             return createVector(data);
>         }
>         abstract protected R createVector(T[] data);
>     }
>     public static class ArrayFieldVector<T extends FieldElement<T>> extends
ArrayFieldVectorExtendable<T, ArrayFieldVector> {
>         @Override
>         protected ArrayFieldVector<T> createVector(T[] data) {
>             ArrayFieldVector<T> result = new ArrayFieldVector<T>();
>             result.data = data;
>             return result;
>         }
>     }
>     public static class ArrayComplexVector extends ArrayFieldVectorExtendable<Complex,
ArrayComplexVector> {
>         @Override
>         protected ArrayComplexVector createVector(Complex[] data) {
>             ArrayComplexVector result = new ArrayComplexVector();
>             result.data = data;
>             return result;
>         }
>         public double[] getReal() {
>             return null;
>         }
>         public double[] getImaginary() {
>             return null;
>         }
>     }
>     public void test() {
>         ArrayComplexVector v = new ArrayComplexVector();
>         ArrayComplexVector v1 = v.copy();  // FiledVector type survives ...
>     }
> }

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message