简体   繁体   English

如何编写ArrayFieldVector的子类 <Complex> (Apache Commons)

[英]How to write subclass of ArrayFieldVector<Complex> (apache commons)

There are ArrayFieldVector class in ApacheCommons: ApacheCommons中有ArrayFieldVector类:

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

Now I need to work with complex vectors. 现在,我需要处理复杂的向量。

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

The problem is that there aren't pow method in ArrayFieldVector class. 问题是ArrayFieldVector类中没有pow方法。 So I decided to write my own class: 因此,我决定编写自己的课程:

  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);
  }
}

Now I can pow ComplexVector but can not add ComplexVector to ArrayFieldVector because there are no add method with ComplexVector argument. 现在,我可以启动ComplexVector,但是不能将ComplexVector添加到ArrayFieldVector,因为没有带有ComplexVector参数的add方法。 One of the solution is to rewrite all this metods (add, multiply, ebeAdd,...) with ComplexVector argument. 解决方案之一是用ComplexVector参数重写所有这些方法(加,乘,ebeAdd,...)。 Maybe there are other way to solve this problem? 也许还有其他方法可以解决这个问题?

You'd better use something like this: 您最好使用这样的东西:

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);
    }
}

Usage: ArrayFieldVectorUtils.pow(someArrayFieldVector, 3) 用法: ArrayFieldVectorUtils.pow(someArrayFieldVector, 3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM