复数与复变函数基本运算(加,减,乘,除,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)<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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值