如何编写ArrayFieldVector <complex>(apache commons)</complex>的子类

时间:2014-02-16 08:52:39

标签: java arrays generics

ApacheCommons中有 ArrayFieldVector 类:

    public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<T>, Serializable

现在我需要使用复杂的载体。

    ArrayFieldVector<Complex> c = new ArrayFieldVector<>(100, new Complex(1,0));

问题是 ArrayFieldVector 类中没有pow方法。所以我决定写自己的课程:

  public class ComplexVector extends ArrayFieldVector<Complex>{
    Complex[] data=super.getDataRef();

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

    public ComplexVector pow(double p){
       Complex[] out=new Complex[data.length];
       for(int i=0;i<data.length;i++){
           out[i]=data[i].pow(p);
                                     }
       return new ComplexVector(out);
  }
}

现在我可以 ComplexVector 但不能将 ComplexVector 添加到 ArrayFieldVector ,因为没有 ComplexVector 论点。其中一个解决方案是用 ComplexVector 参数重写所有这些方法(add,multiply,ebeAdd,...)。也许有其他方法可以解决这个问题?

1 个答案:

答案 0 :(得分:0)

你最好使用这样的东西:

public final class ArrayFieldVectorUtils {

    private ArrayFieldVectorUtils(){
        // avoid instantiation
    }

    public static ArrayFieldVector<Complex> pow(final ArrayFieldVector<Complex> a, int pow){
       Complex[] out = new Complex[a.length];
       for(int i=0;i<a.length;i++){
           out[i]=a[i].pow(pow);
                                     }
       return new ArrayFieldVector<Complex>(out);
    }
}

用法:ArrayFieldVectorUtils.pow(someArrayFieldVector, 3)