1

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

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

        }

    }
4

1 回答 1

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 问题有太多需要解决的问题)。请查看复杂数学的基础课程或关于复数的维基百科文章

于 2013-01-30T03:21:12.100 回答