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

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你好!实现两个复数、乘、除运算是可以的。你可以使用复数的实部和虚部进行计算。下面是一个示例代码,用于演示这些运算: ```python class ComplexNumber: def __init__(self, real, imag): self.real = real self.imag = imag def add(self, other): real_part = self.real + other.real imag_part = self.imag + other.imag return ComplexNumber(real_part, imag_part) def subtract(self, other): real_part = self.real - other.real imag_part = self.imag - other.imag return ComplexNumber(real_part, imag_part) def multiply(self, other): real_part = (self.real * other.real) - (self.imag * other.imag) imag_part = (self.real * other.imag) + (self.imag * other.real) return ComplexNumber(real_part, imag_part) def divide(self, other): denominator = (other.real ** 2) + (other.imag ** 2) real_part = ((self.real * other.real) + (self.imag * other.imag)) / denominator imag_part = ((self.imag * other.real) - (self.real * other.imag)) / denominator return ComplexNumber(real_part, imag_part) def __str__(self): return f"{self.real} + {self.imag}i" # 示例用法 c1 = ComplexNumber(2, 3) c2 = ComplexNumber(4, 5) print(c1.add(c2)) # 运算 print(c1.subtract(c2)) # 运算 print(c1.multiply(c2)) # 乘法运算 print(c1.divide(c2)) # 除法运算 ``` 这段代码定义了一个名为`ComplexNumber`的类,其中包含了、乘、除运算的方法。你可以创建两个复数对象并调用对应的方法来进行运算。输出结果将会显示运算的结果。 请注意,这只是一个简单的示例代码,你可以根据自己的需求进行修改和扩展。希望对你有所帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值