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 Tue, 10 May 2011 20:28:47 GMT

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

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

Maybe this code works ... Yes it looks ugly ...

I want to extend FieldElement and wnt it use in vector and matrix.
 
so the problem is getting the right "backing class" for the interface.
For the matrix is a third generics needed....

Maybe someone has a better solution here is my "draft".

public class App 
{
    public static interface FieldElement<T> {

        T add(T a);
    }

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

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

        V createVector(T[] data);

    }

    public static interface ComplexFieldElement<T extends ComplexFieldElement<T>>
extends FieldElement<T> {

        double getReal();

        T conjugate();
    }

    public static class Complex implements ComplexFieldElement<Complex> {

        @Override
        public double getReal() {
            return 1;
        }

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

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

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

        V conjugate();

        double[] getReal();
    }

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

        protected T[] data;

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

    public static class ArrayFieldVector<T extends FieldElement<T>> extends AbstractArrayFieldVector<T,
FieldVector> {

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

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

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

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

    }

    public static interface ComplexVector extends GenericComplexFieldVector<Complex, ComplexVector>
{

    }

    public static class ArrayComplexVector extends AbstractArrayComplexVector<Complex,
ComplexVector> implements ComplexVector {

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


    }

public static void main(String [] args) {
        ArrayComplexVector v = new ArrayComplexVector();
        ComplexVector v1 = v.multiply(new ArrayComplexVector());  // FiledVector type survives
...
        FieldVector<Complex, ?> v2 = v1.multiply(v);
        FieldVector<Complex, FieldVector> v3 = v2.multiply(v2);
    }
}


> 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