第二小组

复数类实现 - kb

一共三个文件:
         ComplexException.java   :  Complex类所应的异常
         Complex.java   : Complex类实现
         TestComplex.java   : 测试Complex类

/**
 * File: ComplexException.java
 * author: kb @ 2005-7-28
 
*/


package kb.complex;

public class ComplexException extends Exception {
    
public ComplexException( Exception e ) 
{
        super( e );
    }

    
    
public ComplexException() {
        super();
    }

    
    
public String toString() {
        
return "ComplexException: Maybe init string's form uncorrect."
;
    }

}



/**
 * File: Complex.java
 * author: kb @ 2005-7-28
 
*/


package kb.complex;

import java.util.StringTokenizer;

public class Complex {

    
private double
 real;
    
private double
 imag;
    
    
private static final String delimiters = " (),\t"
;
    
    
/**
     * -*- constructors -*-
     
*/

    
public Complex() {}
    
    
public Complex( double r ) { real = r; }
    
    
public Complex( Complex c ) { real = c.real; imag = c.imag; }
    
    
public Complex( double r, double i ) { real = r; imag = i; }
    
    
public Complex( String c ) throws ComplexException {
        
        
if( c == null || c.length() == 0
 )
        
{
            real 
= imag = 0.0
;
            
return
;
        }

        
        StringTokenizer t 
= new StringTokenizer( c, delimiters );
        String[] data 
= new
 String[t.countTokens()];
        
forint i = 0; i < data.length; i++ ) 
{
            data[ i ] 
=
 t.nextToken();
        }

        
        
switch( data.length )
        
{
        
case 1:        //form: ",imag" "real," "real" and "(,imag)" "(real,)" "(real)"

            double tmp;
            tmp 
= Double.parseDouble( data[0
] );
            
            String str 
= c.split(",")[0
];
            
if( str.indexOf( data[0] ) >= 0 ) 
{
                real 
=
 tmp;
            }
 else {
                imag 
=
 tmp;
            }

            
            
break;
        
case 2:        //form: "real,imag" and "(real,imag)"

            real = Double.parseDouble( data[0] );
            imag 
= Double.parseDouble( data[1
] );
            
break
;
        
default
:
            
throw new
 ComplexException();
        }

        
    }

    
    
/**
     * -*- accessor definition -*- 
     
*/

    
public double getReal() return real; }
    
public double getImag() return imag; }
    
    
public void setReal( double r ) { real = r; }
    
public void setImag( double i ) { imag = i; }
    
    
public String toString() {
        
return new String( "(" + real + "," + imag + ")"
 );
    }

    
    
/**
     * -*- 实例运算方法,对自身作运算。-*-
     * 还应该加入复数与double类型数据的实例运算方法,但
     * 考虑到代码长度,便省了.
     
*/

    
public Complex add( Complex c ) {
        real 
+=
 c.real;
        imag 
+=
 c.imag;
        
return this
;
    }

    
    
public Complex minus( Complex c ) {
        real 
-=
 c.real;
        imag 
-=
 c.imag;
        
return this
;
    }

    
    
public Complex multiply( Complex c ) {
        
double tmp =
 real;
        real 
= real * c.real - imag *
 c.imag;
        imag 
= tmp * c.imag + imag *
 c.real;
        
return this
;
    }

    
    
public Complex divide( Complex c ) {
        
this.multiply( new Complex( c.real, -
 c.imag ) );
        
        
double tmp = c.real * c.real + c.imag *
 c.imag;
        real 
/=
 tmp;
        imag 
/=
 tmp;
        
return this
;
    }


    
/**
     * -*- 类运算方法,提供两个复数的运算。-*-
     * 还应该加入复数与double类型数据的静态运算方法,但
     * 考虑到代码长度,便省了.
     
*/

    
public static Complex add( Complex c1, Complex c2 ) {
        
return new Complex( c1.real + c2.real, c1.imag +
 c2.imag );
    }

    
    
public static Complex minus( Complex c1, Complex c2 ) {
        
return new Complex( c1.real - c2.real, c1.imag -
 c2.imag );
    }

    
    
public static Complex multiply( Complex c1, Complex c2 ) {
        
double real = c1.real * c2.real - c1.imag *
 c2.imag;
        
double imag = c1.real * c2.imag + c1.imag *
 c2.real;
        
return new
 Complex( real, imag );
    }

    
    
public static Complex divide( Complex c1, Complex c2 ) {
        Complex c 
= new Complex( c2.real, -
 c2.imag );
        
double tmp = c2.real * c2.real + c2.imag *
 c2.imag;
        
        c.multiply( c1 );
        
        c.real 
/=
 tmp;
        c.imag 
/=
 tmp;
        
return
 c;
    }

    
    
/*
     * 复数虽没有大小比较算法,但比较复数模的大小却是经常的。
     * 本方法实现的就是比较两个复数模的大小。
     
*/

    
public int compareTo( Complex c ) {
        
double a = Math.pow( real, 2 ) + Math.pow( imag, 2
 );
        
double b = Math.pow( c.real, 2 ) + Math.pow( c.imag, 2
 );
        
return ( a > b ? 1 : ( a < b ? -1 : 0
 ) );
    }


}



/**
 * File: TestComplex.java
 * author: kb @ 2005-7-28
 
*/


package kb.complex;

public class TestComplex {

    
/**
     * 测试不是很细致,所以可能仍会留有Bugs
     
*/

    
public static void main(String[] args) throws Exception {
        Complex c1;
        Complex c2;
        
        String d1 
= "(1,2)"
;
        String d2 
= "1,2"
;
        
        String dd1 
= " (   1 , 2 ) "
;
        String dd2 
= " 1 , 2 "
;
        
        String ddd1 
= "(2,)";    //"(,2)" "(2)" : also ok

        String ddd2 = "2,";        //",2" "2" : also ok
        
        String d 
= "";            //null also ok

        
        c1 
= new
 Complex( d );
        System.
out.println( "c1 by d: " +
 c1 );
        
        c1 
= new
 Complex( d1 );
        System.
out.println( "c1 by d1: " +
 c1 );
        
        c2 
= new
 Complex( d2 );
        System.
out.println( "c2 by d2: " +
 c2 );

        System.
out.println( "c1 / c2(Complex.divide(c1, c2)): " +
 Complex.divide(c1, c2) );
        System.
out.println( "c1 /= c2(c1.divide(c2)): " +
 c1.divide(c2) );
        
        c1 
= new
 Complex( dd1 );
        System.
out.println( "c1 by dd1: " +
 c1 );
        
        c2 
= new
 Complex( dd2 );
        System.
out.println( "c2 by dd2: " +
 c2 );

        System.
out.println( "c1 * c2(Complex.multiply(c1, c2)): " +
 Complex.multiply(c1, c2) );
        System.
out.println( "c1 *= c2(c1.multiply(c2)): " +
 c1.multiply(c2) );
        
        c1 
= new
 Complex( ddd1 );
        System.
out.println( "c1 by ddd1: " +
 c1 );
        
        c2 
= new
 Complex( ddd2 );
        System.
out.println( "c2 by ddd2: " +
 c2 );
        
        System.
out.println( "c1 compareTo c2: " +
 c1.compareTo(c2) );
        
        System.
out.println( "c1 += c2(c1.add(c2)): " +
 c1.add(c2) );
        System.
out.println( "c1 + c2(Complex.minus(c1,c2)): " +
 Complex.minus(c1,c2) );

    }


}



输出结果如下:
c1 by d: (0.0,0.0)
c1 by d1: (
1.0,2.0)
c2 by d2: (
1.0,2.0)
c1 
/ c2(Complex.divide(c1, c2)): (1.0,0.0)
c1 
/= c2(c1.divide(c2)): (1.0,0.0)
c1 by dd1: (
1.0,2.0)
c2 by dd2: (
1.0,2.0)
c1 
* c2(Complex.multiply(c1, c2)): (-3.0,4.0)
c1 
*= c2(c1.multiply(c2)): (-3.0,4.0)
c1 by ddd1: (
2.0,0.0)
c2 by ddd2: (
2.0,0.0)
c1 compareTo c2: 
0
c1 
+= c2(c1.add(c2)): (4.0,0.0)
c1 
+ c2(Complex.minus(c1,c2)): (2.0,0.0)

posted on 2005-07-28 22:44 第二小组 阅读(2031) 评论(6)  编辑  收藏 所属分类: 小组交流

Feedback

# re: 复数类实现 - kb 2005-07-29 00:08 Flair@ZJU

:p, so many hidden bugs... anyway, it's nice to have a try.
Here is a good reference: org.apache.commons.math.complex.Complex  回复  更多评论   

# re: 复数类实现 - kb 2005-07-29 00:51 第二小组

.......so many hidden bugs......?
Really?
看来我的代码还是很烂啊.
可不可以指点一下?让我也长长记性.  回复  更多评论   

# re: 复数类实现 - kb 2005-07-29 01:06 第二小组

刚才看了一下org.apache.commons.math.complex.Complex ,
确实感觉出差距来了,但不知道它内部是如何实现的,那样的话会更好的理解.其实很多的复数算法我都不知道是怎么算的,比如pow,sqrt,tan,etc......  回复  更多评论   

# re: 复数类实现 - kb 2005-07-29 01:54 Flair@ZJU

U cannot see the src code..?
Just download it from http://jakarta.apache.org/site/downloads/downloads_commons-math.cgi  回复  更多评论   

# 复数类实现 2006-10-19 17:19 坚持到底[匿名]

/*复数类的实现:湖南农业大学信息科学技术学院计算机科学与技术八班(坚持到底) 2006-10-19 */
/*复数加法公式:(a+bi)+(c+di)=(a+c)+(b+d)i*/
/*复数减法公式:(a+bi)-(c+di)=(a-c)+(b-d)i*/
/*复数乘法公式:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i*/
/*复数除法公式:(a+bi)/(c+di)=[(ac+bd)/(c*c+d*d)]+[(bc-ad)/(c*c+d*d)]i*/
public class ComplexNumber {
//属性部分定义
//定义复数部分实部
double m_dRealpart;
//定义复数部分虚部
double m_dImaginpart;
//定义一个中间变量
double R_dRealpart;
//定义一个除法实部
double Realpart;


//方法部分定义
//构造函数实现部分
public ComplexNumber (double r,double I){
this.m_dRealpart=r;
this.m_dImaginpart=I;
}

//获取复数实部值
double getRealpart(){
return this.m_dRealpart;
}

//获取复数虚部值
double getImaginpart(){
return this.m_dImaginpart;
}

//设置复数实部值
void setRealpart(double temp){
this.m_dRealpart=temp;
}

//设置复数虚部值
void setImaginpart(double temp){
this.m_dImaginpart=temp;
}

//两个复数对象加法运算
ComplexNumber complexAdd(ComplexNumber c){
this.m_dRealpart=this.m_dRealpart+c.m_dRealpart;
this.m_dImaginpart=this.m_dImaginpart+c.m_dImaginpart;
return this;
}

//两个复数对象的减法运算
ComplexNumber complexMinus(ComplexNumber c){
this.m_dRealpart=this.m_dRealpart-c.m_dRealpart;
this.m_dImaginpart=this.m_dImaginpart-c.m_dImaginpart;
return this;
}


//两个复数对象的乘法运算
ComplexNumber complexChen(ComplexNumber c){
R_dRealpart=this.m_dRealpart;
this.m_dRealpart=this.m_dRealpart*c.m_dRealpart-this.m_dImaginpart*c.m_dImaginpart;
this.m_dImaginpart=R_dRealpart*c.m_dImaginpart+c.m_dRealpart*this.m_dImaginpart;
return this;
}

//两个复数对象的除法运算
ComplexNumber complexChu(ComplexNumber c){
Realpart=this.m_dRealpart;
this.m_dRealpart=(Realpart*c.m_dRealpart+this.m_dImaginpart*c.m_dImaginpart)/(c.m_dRealpart*c.m_dRealpart+c.m_dImaginpart*c.m_dImaginpart);
this.m_dImaginpart=(this.m_dImaginpart*c.m_dRealpart-Realpart*c.m_dImaginpart)/(c.m_dRealpart*c.m_dRealpart+c.m_dImaginpart*c.m_dImaginpart);
return this;
}


//构造一个a+bi的字符串形式
public String toString(){
String strTemp="";
strTemp=this.m_dRealpart+"+"+
this.m_dImaginpart+"i";
return strTemp;
}


public static void main(String[] args) {
//输出两个复数相加结果
ComplexNumber complex1 = new ComplexNumber(10.0,10.0);
System.out.println("m_dRealPart:"+complex1.m_dRealpart);
ComplexNumber complex2 = new ComplexNumber(3.0,5.0);
System.out.println("\n\ncomplex1:"+complex1.toString());
System.out.println("complex2:"+complex2.toString());
complex1.complexAdd(complex2);
System.out.println("complex1+complex2=" + complex1.toString());

//输出两个复数相减结果
ComplexNumber complex3 = new ComplexNumber(10.0,10.0);
System.out.println("\n\ncomplex3:"+complex3.toString());
System.out.println("complex2:"+complex2.toString());
complex3.complexMinus(complex2);
System.out.println("complex3-complex2="+complex3.toString());

//输出两个复数相乘结果
ComplexNumber complex4 = new ComplexNumber(-1.0,2.0);
System.out.println("\n\ncomplex4:"+complex4.toString());
ComplexNumber complex5 = new ComplexNumber(2.0,-3.0);
System.out.println("complex5:"+complex5.toString());
complex4.complexChen(complex5);
System.out.println("complex4*complex5="+complex4.toString());


//输出两个复数相除结果
ComplexNumber complex6 = new ComplexNumber(1.0,2.0);
System.out.println("\n\ncomplex6:"+complex6.toString());
ComplexNumber complex7 = new ComplexNumber(2.0,3.0);
System.out.println("complex7:"+complex7.toString());
complex6.complexChu(complex7);
System.out.println("complex6/complex7="+complex6.toString());

}

}
  回复  更多评论   

# 复数类实现 2006-10-19 17:22 坚持到底[匿名]

//file: ComplexNumber.java
/*复数类的实现:湖南农业大学信息科学技术学院计算机科学与技术八班(坚持到底) 2006-10-19 */
/*复数加法公式:(a+bi)+(c+di)=(a+c)+(b+d)i*/
/*复数减法公式:(a+bi)-(c+di)=(a-c)+(b-d)i*/
/*复数乘法公式:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i*/
/*复数除法公式:(a+bi)/(c+di)=[(ac+bd)/(c*c+d*d)]+[(bc-ad)/(c*c+d*d)]i*/
public class ComplexNumber {
//属性部分定义
//定义复数部分实部
double m_dRealpart;
//定义复数部分虚部
double m_dImaginpart;
//定义一个中间变量
double R_dRealpart;
//定义一个除法实部
double Realpart;


//方法部分定义
//构造函数实现部分
public ComplexNumber (double r,double I){
this.m_dRealpart=r;
this.m_dImaginpart=I;
}

//获取复数实部值
double getRealpart(){
return this.m_dRealpart;
}

//获取复数虚部值
double getImaginpart(){
return this.m_dImaginpart;
}

//设置复数实部值
void setRealpart(double temp){
this.m_dRealpart=temp;
}

//设置复数虚部值
void setImaginpart(double temp){
this.m_dImaginpart=temp;
}

//两个复数对象加法运算
ComplexNumber complexAdd(ComplexNumber c){
this.m_dRealpart=this.m_dRealpart+c.m_dRealpart;
this.m_dImaginpart=this.m_dImaginpart+c.m_dImaginpart;
return this;
}

//两个复数对象的减法运算
ComplexNumber complexMinus(ComplexNumber c){
this.m_dRealpart=this.m_dRealpart-c.m_dRealpart;
this.m_dImaginpart=this.m_dImaginpart-c.m_dImaginpart;
return this;
}


//两个复数对象的乘法运算
ComplexNumber complexChen(ComplexNumber c){
R_dRealpart=this.m_dRealpart;
this.m_dRealpart=this.m_dRealpart*c.m_dRealpart-this.m_dImaginpart*c.m_dImaginpart;
this.m_dImaginpart=R_dRealpart*c.m_dImaginpart+c.m_dRealpart*this.m_dImaginpart;
return this;
}

//两个复数对象的除法运算
ComplexNumber complexChu(ComplexNumber c){
Realpart=this.m_dRealpart;
this.m_dRealpart=(Realpart*c.m_dRealpart+this.m_dImaginpart*c.m_dImaginpart)/(c.m_dRealpart*c.m_dRealpart+c.m_dImaginpart*c.m_dImaginpart);
this.m_dImaginpart=(this.m_dImaginpart*c.m_dRealpart-Realpart*c.m_dImaginpart)/(c.m_dRealpart*c.m_dRealpart+c.m_dImaginpart*c.m_dImaginpart);
return this;
}


//构造一个a+bi的字符串形式
public String toString(){
String strTemp="";
strTemp=this.m_dRealpart+"+"+
this.m_dImaginpart+"i";
return strTemp;
}


public static void main(String[] args) {
//输出两个复数相加结果
ComplexNumber complex1 = new ComplexNumber(10.0,10.0);
System.out.println("m_dRealPart:"+complex1.m_dRealpart);
ComplexNumber complex2 = new ComplexNumber(3.0,5.0);
System.out.println("\n\ncomplex1:"+complex1.toString());
System.out.println("complex2:"+complex2.toString());
complex1.complexAdd(complex2);
System.out.println("complex1+complex2=" + complex1.toString());

//输出两个复数相减结果
ComplexNumber complex3 = new ComplexNumber(10.0,10.0);
System.out.println("\n\ncomplex3:"+complex3.toString());
System.out.println("complex2:"+complex2.toString());
complex3.complexMinus(complex2);
System.out.println("complex3-complex2="+complex3.toString());

//输出两个复数相乘结果
ComplexNumber complex4 = new ComplexNumber(-1.0,2.0);
System.out.println("\n\ncomplex4:"+complex4.toString());
ComplexNumber complex5 = new ComplexNumber(2.0,-3.0);
System.out.println("complex5:"+complex5.toString());
complex4.complexChen(complex5);
System.out.println("complex4*complex5="+complex4.toString());


//输出两个复数相除结果
ComplexNumber complex6 = new ComplexNumber(1.0,2.0);
System.out.println("\n\ncomplex6:"+complex6.toString());
ComplexNumber complex7 = new ComplexNumber(2.0,3.0);
System.out.println("complex7:"+complex7.toString());
complex6.complexChu(complex7);
System.out.println("complex6/complex7="+complex6.toString());

}

}
  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航: