运算公式
(这将是百度上关于复数运算较为全面的一篇)
加法
-
(a+bi)+(c+di)=(a+c)+(b+d)i
减法
-
(a+bi)+(c+di)=(a-c)+(b-d)i
乘法
-
(a+bi)(c+di)=(ac-bd)+(bc+ad)i
除法
-
(a+bi)/(c+di)=(ac+bd)/(c ^ 2 + d ^ 2) +((bc-ad)/(c ^ 2 + d ^ 2)) i
exp
log
对于其它对数计算可使用换低公式:
sin & cos
幂
结果检验
代码
(注意参数可能为空指针,由于使用浮点数作为基础数据类型,对于复数为零的判断要设置一个阈值,这里使用了10倍的Double最小值)
package complex;
import java.util.Objects;
public class Complex {
private double re; //real
private double im; //imaginary
static final Complex I=new Complex(0,1);
static final Complex NI=new Complex(0,-1);
public Complex(double real, double imag) {
re = real;
im = imag;
}
public static Complex add(Complex a,Complex b) {
Objects.requireNonNull(a);
Objects.requireNonNull(b);
return new Complex(a.re + b.re,a.im + b.im);
}
public static Complex sub(Complex a,Complex b) {
Objects.requireNonNull(a);
Objects.requireNonNull(b);
return new Complex(a.re - b.re,a.im - b.im);
}
public static Complex mul(Complex a,Complex b) {
Objects.requireNonNull(b);
Objects.requireNonNull(b);
double real = a.re * b.re - a.im * b.im;
double imag = a.re * b.im + a.im * b.re;
return new Complex(real,imag);
}
public static Complex div(Complex a,Complex b) {
Objects.requireNonNull(b);
Objects.requireNonNull(b);
if(b.isZero()) {
throw new ArithmeticException("/ by zero");
}
double den=b.re * b.re + b.im * b.im;
double real = a.re * b.re + a.im * b.im;
double imag = a.im * b.re - a.re * b.im;
return new Complex(real/den, imag/den);
}
public static Complex log(Complex com) {
double real = com.re * com.re + com.im * com.im;
double imag = Math.atan(com.im * com.re );
return new Complex(Math.log(real)/2,imag);
}
public static Complex exp(Complex com) {
double real = Math.cos(com.im);
double imag = Math.sin(com.im);
double expx= Math.exp(com.re);
return new Complex(expx*real,expx*imag);
}
public static Complex sin(Complex com) {
final Complex cf=new Complex(0,2);//coefficient
Complex e1=exp(mul(I,com));
Complex e2=exp(mul(NI,com));
return div(sub(e1,e2),cf);
}
public static Complex cos(Complex com) {
final Complex cf=new Complex(0,2);//coefficient
Complex e1=exp(mul(I,com));
Complex e2=exp(mul(NI,com));
return div(add(e1,e2),cf);
}
public static Complex pow(Complex a,Complex b) {;
return Complex.exp( Complex.mul(b, Complex.log(a)));
}
public boolean isZero() {
if(Math.abs(re)<10*Double.MIN_VALUE
&&Math.abs(im)<10*Double.MIN_VALUE) {
return true;
}
return false;
}
public Complex conjugate() {
return new Complex(re,-im);
}
public double length() {
return Math.sqrt(re * re + im * im);
}
public String toString() {
if(im<0)
return "( "+re+im+"i )";
return "( "+re+"+"+im+"i )";
}
}
测试数据
public static void main(String[] args) {
Complex a=new Complex(1,2);
Complex b=new Complex(0,1);
System.out.println("add "+a+" + " +b +" = "+add(a,b));
System.out.println("sub "+a+" - " +b +" = "+sub(a,b));
System.out.println("mul "+a+" * " +b +" = "+mul(a,b));
System.out.println("div "+a+" / " +b +" = "+div(a,b));
Complex lg=new Complex(1,1);
System.out.println("log "+lg+" = "+log(lg));
System.out.println("value :"+0.5*Math.log(2)+" "+Math.PI/4+"i");
Complex ex=new Complex(2,1);
System.out.println("exp "+ex+" = "+exp(ex));
System.out.println("value :"+(Math.exp(2)*Math.cos(1))+" "+(Math.exp(2)*Math.sin(1))+"i");
Complex sinc=new Complex(0,1);
System.out.println("sin "+sinc+" = "+sin(sinc));
System.out.println("value :"+0+" "+(Math.exp(1)-Math.exp(-1))/2+"i");
Complex cosc=new Complex(0,1);
System.out.println("cos "+sinc+" = "+cos(cosc));
System.out.println("value :"+0+" "+(Math.exp(1)+Math.exp(-1))/2+"i");
Complex p1=new Complex(1,1);
Complex p2=new Complex(0,1);
System.out.println("pow "+p1+","+p2 +" = "+pow(p1,p2));
System.out.println("value :"+0.428829006+" "+0.154871752+"i");
}
add ( 1.0+2.0i ) + ( 0.0+1.0i ) = ( 1.0+3.0i )
sub ( 1.0+2.0i ) - ( 0.0+1.0i ) = ( 1.0+1.0i )
mul ( 1.0+2.0i ) * ( 0.0+1.0i ) = ( -2.0+1.0i )
div ( 1.0+2.0i ) / ( 0.0+1.0i ) = ( 2.0-1.0i )
log ( 1.0+1.0i ) = ( 0.34657359027997264+0.7853981633974483i )
value :0.34657359027997264 0.7853981633974483i
exp ( 2.0+1.0i ) = ( 3.992324048441272+6.217676312367968i )
value :3.992324048441272 6.217676312367968i
sin ( 0.0+1.0i ) = ( 0.0+1.1752011936438014i )
value :0 1.1752011936438014i
cos ( 0.0+1.0i ) = ( 0.0-1.5430806348152437i )
value :0 1.5430806348152437i
pow ( 1.0+1.0i ),( 0.0+1.0i ) = ( 0.4288290062943678+0.15487175246424675i )
value :0.428829006 0.154871752i