complex.java

来自「很好的播放软件可以用FFT算法计算波形 有点像media9 不过完整程序有两个部」· Java 代码 · 共 190 行

JAVA
190
字号
//复数类



public class Complex
{
	//定义实部,虚部,模,辐角
	private double real,img,mod,angle;
	
	public Complex()
	{
		real=0.0;
		img=0.0;
		setMod();
		setAngle();
	}
	
	public Complex(double real,double img)
	{
		this.real=real;
		this.img=img;
		setMod();
		setAngle();
	}
	
	//设置复数的模
	private void setMod()
	{
		mod=Math.sqrt(real*real+img*img);
	}
	
	//设置复数的辐角
	private void setAngle()
	{
		//当实部不为零时,辐角为虚部/实部的反正切值
		//当实部为零时,虚部大于零,辐角为Pi/2;虚部小于零,辐角为-Pi/2
		if(real!=0.0)
		{
			angle=Math.atan(img/real);
		}
		else 
		{
			if(img>0)
			{
				angle=Math.PI/2;
			}
			else
			{
				angle=-Math.PI/2;
			}
		}
	}
	
	//设置实部
	public void setReal(double real)
	{
		this.real=real;
	}
	//设置虚部
	public void setImage(double img)
	{
		this.img=img;
	}
	//获得实部
	public double getReal()
	{
		return real;
	}
	//获得虚部
	public double getImage()
	{
		return img;
	}
	//获得模
	public double getModel()
	{
		setMod();
		return mod;
	}	
	//获得辐角
	public double getAngle()
	{
		setAngle();
		return angle;
	}
	//取共轭
	public static Complex getConjugate(Complex c)
	{
		c.img=-c.img;
		return c;
	}	
	//复数加法,返回值是一个复数类
	public Complex add(Complex c)
	{
		Complex result=new Complex();
		result.real=this.real+c.real;
		result.img=this.img+c.img;
		return result;
	}
	//复数加法,允许不生成实例直接用于计算
	public static Complex add(Complex c1,Complex c2)
	{
		Complex result=new Complex();
		result.real=c1.real+c2.real;
		result.img=c1.img+c2.img;
		return result;
	}
	//复数乘法
	public Complex multiply(Complex c)
	{
		Complex result=new Complex();
		double real,img;
		real=this.real*c.real - this.img*c.img;
		img=this.real*c.img + this.img*c.real;
		result.real=real;
		result.img=img;
		return result;
	}
	//复数乘法,允许不生成实例直接用于计算
	public static Complex multiply(Complex c1,Complex c2)
	{
		Complex result=new Complex();
		result.real=c1.real*c2.real-c1.img*c2.img;
		result.img=c1.real*c2.img+c1.img*c2.real;
		return result;
	}
	//复数除法
	public Complex divide(Complex c)
	{
		Complex result=new Complex();
		double real,img;
		//m2为除数模的平方
		double m2=c.getModel()*c.getModel();
		real=(this.real*c.real+this.img*c.img)/m2;
		img=(this.img*c.real-this.real*c.img)/m2;
		result.real=real;
		result.img=img;
		return result;
	}
	//复数除法,允许不生成实例直接用于计算
	public static Complex divide(Complex c1,Complex c2)
	{
		Complex result=new Complex();
		//m2为除数c2模的平方
		double m2=c2.getModel()*c2.getModel();
		result.real=(c1.real*c2.real+c1.img*c2.img)/m2;
		result.img=(c1.img*c2.real-c1.real*c2.img)/m2;
		return result;
	}
	//输出复数的字符串形式
	public String toString()
	{
		double real=this.real;
		double img=this.img;
		String str="";
		if(img>=0)
			str=real+"+j"+img;
		else
			str=real+"-j"+Math.abs(img);
		return str;
	}
	
	public static void main(String args[])	
	{
		Complex c1=new Complex(1,1);
		Complex c2=new Complex(2,1);
		Complex result=new Complex();
		
		result=c1.add(c2);
		System.out.println("c1+c2="+result);
		result=Complex.add(c1,c2);
		System.out.println("c1+c2="+result);
		
		result=c1.multiply(c2);
		System.out.println("c1*c2="+result);
		result=Complex.multiply(c1,c2);
		System.out.println("c1*c2="+result);
		
		result=c1.divide(c2);
		System.out.println("c1/c2="+result);
		result=Complex.divide(c1,c2);
		System.out.println("c1/c2="+result);
		
		double mod=c1.getModel();
		System.out.println(mod);
		double angle=c1.getAngle();
		
		System.out.println(angle);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?