复数与复变函数基本运算(加,减,乘,除,exp,log,sin,cos,幂运算) | 您所在的位置:网站首页 › 复变函数虚部要加i › 复数与复变函数基本运算(加,减,乘,除,exp,log,sin,cos,幂运算) |
运算公式
(这将是百度上关于复数运算较为全面的一篇) 加法 (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) return new Complex(re,-im); } public double length() { return Math.sqrt(re * re + im * im); } public String toString() { if(im |
CopyRight 2018-2019 实验室设备网 版权所有 |