gpt4 book ai didi

java Mandelbrot集发散边界

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:54 25 4
gpt4 key购买 nike

我的程序不断收到 bigNum 变量的死亡红线。我正在尝试测试它是否在边界内。

public class ComplexNumber {

private final MyDouble real; // To be initialized in constructors
private final MyDouble imag; // To be initialized in constructors
// constructor initializing

public ComplexNumber(MyDouble realIn, MyDouble imagIn) {

this.real = realIn;
this.imag = imagIn;

}

public ComplexNumber(MyDouble realnumber) {
this.real = realnumber;
this.imag = new MyDouble(0);

}

public MyDouble getReal() {
return real;
}

public MyDouble getImag() {
return imag;
}

// copy constructor
public ComplexNumber(ComplexNumber c) {
this(c.getReal(), c.getImag());
}

// addition of complex numbers
public ComplexNumber add(ComplexNumber a) {

// this.real.add(a.getReal());

return new ComplexNumber(this.real.add(a.getReal()), this.imag.add(a
.getImag()));

}// subtraction of complex numbers

public ComplexNumber subtract(ComplexNumber s) {

return new ComplexNumber(this.real.subtract(s.getReal()), this.imag
.subtract(s.getImag()));

}

// Multiplication of complex numbers
public ComplexNumber multiply(ComplexNumber m) {
MyDouble first = (this.real.multiply(m.getReal()));
MyDouble outside = (m.getReal().multiply(this.imag));
MyDouble inside = (m.getImag().multiply(this.real));
MyDouble last = (this.imag.multiply(m.getImag()));

return (new ComplexNumber(first.subtract(last), (outside.add(inside))));

}

// dividing complex numbers
// double check again but should work
public ComplexNumber divide(ComplexNumber x) {

// MyDouble topReal1 = this.real.multiply(d.getReal());
// MyDouble topReal2 = this.imag.multiply(d.getImag());
// MyDouble topImag1 = this.imag.multiply(d.getReal());
// MyDouble topImag2 = d.getReal().multiply(this.imag);
// MyDouble bottomReal = this.real.multiply(this.real);
// MyDouble bottomImag = d.getImag().multiply(d.getImag());
// MyDouble realSet = topReal1.add(topReal2);
// MyDouble imagSet = topImag1.subtract(topImag2);
// MyDouble top = realSet.add(imagSet);
// MyDouble bottom = bottomReal.add(bottomImag);
//
// return new ComplexNumber(realSet.divide(bottom), imagSet.divide(bottom));


MyDouble demon = x.real.multiply(x.real).add(x.imag.multiply(x.imag));
MyDouble r = real.multiply(x.real).add(imag.multiply(x.imag));
MyDouble i = imag.multiply(x.real).subtract(real.multiply(x.imag));
return new ComplexNumber(r.divide(demon),i.divide(demon));


}

// equals method
public boolean equals(ComplexNumber n) {

return this.real.equals(n.getReal()) && this.imag.equals(n.getImag());

}

// compare to method
//public int compareTo(MyDouble x) {
// int imagNum = x.compareTo(this.getImag());
// int realNum = x.compareTo(this.getReal());
// return realNum + imagNum;

//if ( this.norm(x.equals(x.))

//}

// string to string method
public String toString() {
// thinking how to get negative values since if else didnt work out too
// well
return getReal()+"+"+getImag()+"i";

}

// annoying square root function does not work and api is not as useful
// complex norm method static
public static MyDouble norm(ComplexNumber n) {
MyDouble imag = (n.imag.multiply(n.getImag()));
MyDouble poly = imag.add((n.real.multiply(n.getReal())));

return n.real.multiply(n.real).add(n.imag.multiply(n.imag)).sqrt();

}

// parse method to clear the spaces between the numbers and characters
public static ComplexNumber parse(String s) {
s.replace(" ", "");
String realstring;
String imagstring;
if (s.indexOf('+') != -1) {
realstring = s.substring(0, s.indexOf("+"));
imagstring = s.substring(s.lastIndexOf("+") + 1, s.indexOf("i"));
} else {
realstring = s.substring(0, s.lastIndexOf("-"));
imagstring = s.substring(s.lastIndexOf("-") - 1, s.indexOf("i"));
}
return new ComplexNumber(new MyDouble(Double.parseDouble(realstring)),
new MyDouble(Double.parseDouble(imagstring)));

}
}

最佳答案

如果将光标悬停在代码中的红线或左边距中的红色圆圈上,您应该会看到一条信息更丰富的错误消息。

不过,我可以猜测一下:

我不知道您的 ComplexNumber 类,但我怀疑 multiplyadd 方法会产生 ComplexNumber 结果,而您想要一个 double。我会以不同的方式编写你的测试,如下所示:

double bigNum = aa.getReal() * aa.getReal() + aa.getImag() * aa.getImag();
return (bigNum > Controller.DIVERGENCE_BOUNDARY);

关于java Mandelbrot集发散边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5573123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com