Skip to content

Commit

Permalink
Newton Mode implemented, compiles and runs, output is OK.
Browse files Browse the repository at this point in the history
Colors are a hack
  • Loading branch information
tamchow committed Nov 24, 2015
1 parent 00a8a6e commit 7f2948e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 31 deletions.
58 changes: 39 additions & 19 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 30 additions & 10 deletions src/in/tamchow/fractal/FractalGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ public void mandelbrotGenerate(int start_x, int end_x, int start_y, int end_y, i
Stack<Complex> last = new Stack<>();
FunctionEvaluator fe = new FunctionEvaluator(Complex.ZERO.toString(), variableCode, consts);
long ctr = 0;
outer:
for (int i = start_y; i < end_y; i++) {
for (int j = start_x; j < end_x; j++) {
Complex z = new Complex(Complex.ZERO);
Expand All @@ -350,7 +351,7 @@ public void mandelbrotGenerate(int start_x, int end_x, int start_y, int end_y, i
System.out.println(ctr+" iterations of "+maxiter);
c++;
if(ctr>maxiter){
break;
break outer;
}
ctr++;
}
Expand Down Expand Up @@ -378,6 +379,7 @@ public void newtonGenerate(int start_x, int end_x, int start_y, int end_y, int i
FunctionEvaluator fe = new FunctionEvaluator(Complex.ZERO.toString(), variableCode, consts);
degree = fe.getDegree(polynomial);
long ctr = 0;
outer:
for (int i = start_y; i < end_y; i++) {
for (int j = start_x; j < end_x; j++) {
Complex z = argand_map[i][j];
Expand All @@ -387,18 +389,25 @@ public void newtonGenerate(int start_x, int end_x, int start_y, int end_y, int i
int c = 0x1;
fe.setZ_value(z.toString());
last.push(z);
while (c <= iterations && z.modulus() < 0.0001/*(!MathUtils.approxEquals(z,Complex.ZERO,0.00000001))*/) {
while (c <= iterations) {
Complex ztmp = ComplexOperations.subtract(z, ComplexOperations.multiply(constant, ComplexOperations.divide(fe.evaluate(function), fe.evaluate(polynomial.derivative().toString()))));
if (z.equals(Complex.ZERO)) {
c = iterations;
break;
}
if (ComplexOperations.distance_squared(z, ztmp) < 0.0001) {
break;
}
z = new Complex(ztmp);
fe.setZ_value(z.toString());
System.out.println(ctr + " iterations of " + maxiter);
c++;
if (ctr > maxiter) {
break;
break outer;
}
ctr++;
}
double escape_radius = ComplexOperations.divide(ComplexOperations.principallog(argand_map[i][j]), ComplexOperations.principallog(z)).modulus();
double root_reached = ComplexOperations.divide(ComplexOperations.principallog(argand_map[i][j]), ComplexOperations.principallog(z)).modulus();
Complex[] pass = new Complex[3];
for (int k = 0; k < last.size() && k < pass.length; k++) {
pass[k] = last.pop();
Expand All @@ -408,8 +417,9 @@ public void newtonGenerate(int start_x, int end_x, int start_y, int end_y, int i
pass[m] = new Complex(Complex.ZERO);
}
}
pass[0] = new Complex(argand_map[i][j]);
escapedata[i][j] = c - 1;
argand.setPixel(i, j, getColor(c, pass, escape_radius, iterations));
argand.setPixel(i, j, getColor(c, pass, root_reached, iterations));
last.clear();
}
}
Expand All @@ -423,6 +433,7 @@ public void newtonGenerate(int start_x, int end_x, int start_y, int end_y, int i
FunctionEvaluator fe = new FunctionEvaluator(Complex.ZERO.toString(), variableCode, consts);
degree = polynomial.getDegree();
long ctr = 0;
outer:
for (int i = start_y; i < end_y; i++) {
for (int j = start_x; j < end_x; j++) {
Complex z = argand_map[i][j];
Expand All @@ -432,18 +443,25 @@ public void newtonGenerate(int start_x, int end_x, int start_y, int end_y, int i
int c = 0x1;
fe.setZ_value(z.toString());
last.push(z);
while (c <= iterations && z.modulus() < 0.0001/*(!MathUtils.approxEquals(z,Complex.ZERO,0.00000001))*/) {
while (c <= iterations) {
Complex ztmp = ComplexOperations.subtract(z, ComplexOperations.divide(fe.evaluate(function), fe.evaluate(polynomial.derivative().toString())));
if (z.equals(Complex.ZERO)) {
c = iterations;
break;
}
if (ComplexOperations.distance_squared(z, ztmp) < 0.0001) {
break;
}
z = new Complex(ztmp);
fe.setZ_value(z.toString());
System.out.println(ctr + " iterations of " + maxiter);
c++;
if (ctr > maxiter) {
break;
break outer;
}
ctr++;
}
double escape_radius = ComplexOperations.divide(ComplexOperations.principallog(argand_map[i][j]), ComplexOperations.principallog(z)).modulus();
double root_reached = ComplexOperations.divide(ComplexOperations.principallog(argand_map[i][j]), ComplexOperations.principallog(z)).modulus();
Complex[] pass = new Complex[3];
for (int k = 0; k < last.size() && k < pass.length; k++) {
pass[k] = last.pop();
Expand All @@ -453,8 +471,9 @@ public void newtonGenerate(int start_x, int end_x, int start_y, int end_y, int i
pass[m] = new Complex(Complex.ZERO);
}
}
pass[0] = new Complex(argand_map[i][j]);
escapedata[i][j] = c - 1;
argand.setPixel(i, j, getColor(c, pass, escape_radius, iterations));
argand.setPixel(i, j, getColor(c, pass, root_reached, iterations));
last.clear();
}
}
Expand All @@ -464,6 +483,7 @@ public void juliaGenerate(int start_x, int end_x, int start_y, int end_y, int it
Stack<Complex> last = new Stack<>();
FunctionEvaluator fe = new FunctionEvaluator(Complex.ZERO.toString(), variableCode, consts);
long ctr = 0;
outer:
for (int i = start_y; i < end_y; i++) {
for (int j = start_x; j < end_x; j++) {
Complex z = argand_map[i][j];
Expand All @@ -485,7 +505,7 @@ public void juliaGenerate(int start_x, int end_x, int start_y, int end_y, int it
System.out.println(ctr+" iterations of "+maxiter);
c++;
if(ctr>maxiter){
break;
break outer;
}
ctr++;
}
Expand Down
4 changes: 2 additions & 2 deletions src/in/tamchow/fractal/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public static void main(String[] args) {
String func = "( z ^ 3 ) + ( d * z ) + c", variableCode = "z";
String poly = "{1,z,3};+;{d,z,1};+;{c,z,0}";
String[][] consts = {{"c", "-0.8,+0.156i"}, {"d", "-0.7198,+0.911i"}};
int resx = 401, resy = 401, zoom = 10, zoompow = 0, baseprec = 150, colmode = ColorMode.COLOR_DIVIDE, numcol = 32, coldens = 256, fracmode = FractalGenerator.MODE_JULIA, iter = 128;
int resx = 401, resy = 401, zoom = 10, zoompow = 0, baseprec = 150, colmode = ColorMode.COLOR_MULTIPLY_2, numcol = 32, coldens = 256, fracmode = FractalGenerator.MODE_NEWTON, iter = 128;
double bound = 2.0, escrad = 2.0;
Complex constant = null;
//func=poly;
func = poly;
boolean fromFile = false;
FractalConfig fccfg = new FractalConfig(0, 0, 0);
if (args.length > 1) {
Expand Down
4 changes: 4 additions & 0 deletions src/in/tamchow/fractal/math/complex/ComplexOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ public static Complex principallog(Complex z) {
double r = z.modulus();
return new Complex(Math.log(r), z.arg());
}

public static double distance_squared(Complex z1, Complex z2) {
return Math.sqrt((Math.pow(z1.real() - z2.real(), 2) + Math.pow(z1.imaginary() - z2.imaginary(), 2)));
}
}

0 comments on commit 7f2948e

Please sign in to comment.