2016-10-13 33 views
0

我是java新手,不确定如何处理java中的复数。我正在为我的项目编写代码。我用欧拉的身份exp(i theeta)= cos(theeta)+ i Sin(theeta)找到exp(i * 2 * pi * f)。我必须将这个结果复数与数组“d”中的另一个数相乘。这是我做了什么如何在java中使用复数?

Complex Data[][] = new Complex[20][20]; 
for (int j = 0; j < d.size(); j++){ 
    for (int k = 0; k<20; k++){ 
     for (int l = 0; l<20; l++){ 
      double re = Math.cos(2 * Math.PI * f); 
      double im = Math.sin(2 * Math.PI * f); 
      Complex p = new Complex(re, im); 
      Data[k][l] = ((d.get(j) * p.getReal()), (d.get(j) * p.getImaginary()));  
     } 
    } 
} 

我,但是,说“赋值的左边必须是一个变量”表达Data[k][l] = ((d.get(j) * p.getReal()), (d.get(j) * p.getImaginary()));得到一个错误。 请帮我解决这个问题。谢谢

+2

k] [l]',那么你通常需要一些'Data [k] [l] = new Complex(...)'的形式。你目前似乎正在试图将两个逗号分隔值赋给一个变量,这将永远不会工作。 – khelwood

+0

感谢khelwood的回复。我已纠正它。 – user01

回答

1

不幸的是,它不像C++中的复制构造函数或重载赋值运算符。

你必须显式调用构造函数的复杂,就像

Data[k][l] = new Complex(realValue, imaginaryVal); 

当然,你需要复杂的使用方法,以两个数相乘,因为没有任何其他的想法Java中的运算符重载。

所以,也许如果你想Complex`的`一个实例分配给`数据[中Complex类可能有一些你可能能够转而使用运营商的方法,像

class Complex { 
    public static Complex mul(Complex c0, Complex c1) { 
    double r0=c.getRe(), r1=c1.getRe(); 
    double i0=c.getIm(), i1=c1.getIm(); 
    return new Complex(r0*r1-i0*i1, r0*i1+r1*i0); 
    } 

    public static Complex mulStore(Complex res, Complex c0, Complex c1) { 
    double r0=c.getRe(), r1=c1.getRe(); 
    double i0=c.getIm(), i1=c1.getIm(); 
    if(res==null) { 
     res=new Complex(); 
    } 
    res.setRe(r0*r1-i0*i1); 
    res.setIm(r0*i1+r1*i0); 
    return res; 
    } 

    // equiv with this *= rhs; 
    public void mulAssign(Complex rhs) { 
    // perform the "this * rhs" multiplication and 
    // store the result in this. 
    Complex.mulStore(this, rhs, this); 
    } 

}