格点和复数

时间:2013-01-30 03:11:28

标签: java

我对复数代码的乘法方法很难。这条线似乎给了我最大的麻烦:

public IntegerComplex multiply(IntegerComplex a){
    return new IntegerComplex((this.c*a.c - this.d*a.d)+(this.c*a.c + this.d*a.d));
}

我无法理解我的生活;现在我还没有编过复杂的数字

    public class LatticePoint {

        private int x;
        private int y;
        protected double distance;


        public LatticePoint()
        {
            x = 0;
            y = 0;
        }

        public LatticePoint(int x, int y){
            setX(x);
            setY(y);
        }

        public void setX(int x){
            this.x = x;
        }

        public int getX()
        {   
            return this.x;
        }

        public void setY(int y){
            this.y = y;     
        }

        public int getY()
        {
            return this.y;
        }

        public double distance(LatticePoint p){
            LatticePoint q = new LatticePoint(p.x - this.y, p.y - this.x);
            return q.distance();

        }

        public double distance()
        {
            return Math.sqrt((x*x)+(y*y));
        }

        public LatticePoint add(LatticePoint p){
            return new LatticePoint(this.x + p.x, this.y + p.y);
        }

        //returns this - p
        public LatticePoint sub(LatticePoint p){
            return new LatticePoint(this.x - p.x, this.y - p.y); 
        }


        public static void main(String[] args) {
            // TODO Auto-generated method stub

        }

            }


    public class IntegerComplex extends LatticePoint {

       private int c;
       private int d;


       public IntegerComplex()
        {
            super();
        }
        public IntegerComplex(int c, int d)
        {
            super.setX(c);
            super.setY(d);

        }

        public int getC(){
            return this.c;
        }
        public int getD(){
            return this.d;
        }

        public IntegerComplex add(IntegerComplex a){
    super.add(a);
            return a;
        }

        public double distance(IntegerComplex a){
            super.distance(a);
            return a.distance();
        }


        public IntegerComplex multiply(IntegerComplex a){
            return new IntegerComplex((this.c*a.c - this.d*a.d)+(this.c*a.c +this.d*a.d));
        }



        public static void main(String[] args) {
            // TODO Auto-generated method stub

        }

    }

1 个答案:

答案 0 :(得分:0)

public IntegerComplex multiply(IntegerComplex a){
    return new IntegerComplex((this.c*a.c - this.d*a.d)+(this.c*a.c + this.d*a.d));
}

没有IntegerComplex构造函数将一个int作为参数,因此无法编译。

编辑:

如果你想实现公式(ac - bd) + (bc + ad)*i,那么方法应该这样写:

public IntegerComplex multiply(IntegerComplex a){
    return new IntegerComplex(this.c*a.c - this.d*a.d, this.d*a.c + this.c*a.d);
}

但是请注意,你的数学似乎是假的(并且有太多的东西要解决stackoverflow问题)。请查看有关复杂数学的基础课程或the wikipedia article on complex numbers