设有复数a=(3,5);b=(4,8);3,4为复数的实部,5,8为复数的虚部.
编程求复数a,b,的加减乘除运算???
[CODE]
/**
*写一个关于复数运算的类
*在java中sqrt(-1)的值为NaN,我们做如下约定:用数组[a,b]代表一个复数,其中
*a代表复数的实部,b代表复数的虚部。
*Complex类实现了复数的基本运算。
*/
import java.util.*;
public final class Complex{
public static double complexNorm(double[] a){
return Math.sqrt(a[0]*a[0]+a[1]*a[1]);
}
public static double[] complexAdd(double[] a,double[] b){
return new double[]{a[0]+b[0],a[1]+b[1]};
}
public static double[] complexSub(double[] a,double[] b){
return new double[]{a[0]-b[0],a[1]-b[1]};
}
public static double[] complexMuti(double[] a,double[] b){
return new double[]{a[0]*b[0]-a[1]*b[1],a[0]*b[1]+a[1]*b[0]};
}
public static double[] complexDvi(double[] a,double[] b){
double c=complexNorm(b)*complexNorm(b);
double[] d={b[0],-b[1]};
double[] e=complexMuti(a,d);
return new double[]{e[0]/c,e[1]/c};
}
public static void main(String[] args){
double[] a={1,2};
double[] b={3,4};
System.out.println(Arrays.toString(complexAdd(a,b)));
System.out.println(Arrays.toString(complexSub(a,b)));
System.out.println(Arrays.toString(complexMuti(a,b)));
System.out.println(Arrays.toString(complexDvi(a,b)));
}
}