Welcome to Fractal Forums

Fractal Software => Help & Support => Topic started by: fractalwizz on November 24, 2008, 08:42:57 PM




Title: Java Mandelbrot segment
Post by: fractalwizz on November 24, 2008, 08:42:57 PM
This is the code I was given to generate the mandelbrot.
How would I edit it to produce other exponents of the set?
Code:
private boolean escapesToInfinity(double a, double b) {
        double x = 0;
        double y = 0;
        int iterations = 0;
        do {
            double xnew = x * x - y * y + a;
            double ynew = 2 * x * y + b;
            x = xnew;
            y = ynew;
            iterations++;
            if (iterations == MAX_ITERATIONS) {
                return false;
            }
        } while (x <= 2 && y <= 2);
        return true;
    }



Title: Re: Java Mandelbrot segment
Post by: cKleinhuis on November 25, 2008, 01:48:07 AM
if you want integer powers, the
 double xnew = x * x - y * y + a;
 double ynew = 2 * x * y + b;
without the +a , +b parts, is the complex square, if you repeat this, you get z^4, z^6 and so on

 double xnew = x * x - y * y ;
 double ynew = 2 * x * y ;

... and add a nd b afterwards

 double xnew = xnew+a ;
 double ynew = ynew + b ;

 O0


Title: Re: Java Mandelbrot segment
Post by: fractalwizz on November 25, 2008, 03:31:11 AM
So, how would that look like in the end code wise?


Title: Re: Java Mandelbrot segment
Post by: cKleinhuis on November 25, 2008, 10:16:00 AM
try this: (far from perfect )
Code:

// power=1 -> z^2
// power=2 -> z^4 ...
private boolean escapesToInfinity(double a, double b, int power) {
        double x = 0;
        double y = 0;
        int iterations = 0;
        do {

           for(int i =0;i<power;i++)
            {
              double xnew = x * x - y * y ;
              double ynew = 2 * x * y ;
              x = xnew;
              y = ynew;
            }
            // Finally add the seed
            x=x+a;
            y=y+b;

            // Continue with iteration
            iterations++;
            if (iterations == MAX_ITERATIONS) {
                return false;
            }
        } while (x <= 2 && y <= 2);
        return true;
    }



Title: Re: Java Mandelbrot segment
Post by: Duncan C on December 20, 2008, 04:53:26 AM
This is the code I was given to generate the mandelbrot. <snip>
How would I edit it to produce other exponents of the set?


For Z3, here's what I get: (somebody check me, I'm tired)
Code:
private boolean escapesToInfinity(double a, double b) {
        double x = 0;
        double y = 0;
        int iterations = 0;
        do {
            double xnew = x*x*x - y*y*x - 2*x*y*y + a;
            double ynew = 2*x*x*y + x*x*y - y*y*y + b;
            x = xnew;
            y = ynew;
            iterations++;
            if (iterations == MAX_ITERATIONS) {
                return false;
            }
        } while (x <= 2 && y <= 2);
        return true;
    }

You have to figure the odd powers by hand, working out the algebra with complex numbers. Z can also be written as the complex number (x+bi). What I did to find Z3 was to write out (x+yi) (x+yi) (x+yi). First I multiplied (x+yi)(x+yi), and got the Z2 result. I then multiplied my Z2 (in long form) by another (x+yi)

Every time you calculate i*i, the i turns to -1.

So,
   Z2 becomes
   (x+yi)(x+yi) becomes
   x2 + 2xyi - y2

I then multiply THAT by (x+yi):

  (x2 + 2xyi - y2) (x+yi) becomes

  x3 + 2x2yi - y2x + x2yi - 2xy2 - y3i

Any term in the above without an i in it gets added to xnew (the real part of the new Z value), and any term with an i in it gets added to ynew (the imaginary part of the new z value.)

That means

   xnew = x3 - y2x - 2xy2
   ynew = 2x2y + x2y - y3

Finally, we add in our c, which this code represents as a+bi, so we get

   xnew = x3 - y2x - 2xy2 + a
   ynew = 2x2y + x2y - y3 + b

Most programming languages don't have a clean notation for integer powers that's also efficient, so you just write them as repeated multiplications. Thus, the final form becomes what I put in the code block above:


   xnew = x*x*x - y*y*x - 2xy*y + a
   ynew = 2x*x*y + x*x*y - y*y*y + b


I hope that makes sense.


Regards,

Duncan


Title: Re: Java Mandelbrot segment
Post by: fractalwizz on December 20, 2008, 02:08:49 PM
It works. Thanks.


Title: Re: Java Mandelbrot segment
Post by: Duncan C on December 25, 2008, 02:54:06 AM
It works. Thanks.

Which one did you try? Trifox's even power code, or my Z3?

I'd have to figure out Z5 for you, but I don't have time right now.


Duncan C


Title: Re: Java Mandelbrot segment
Post by: fractalwizz on December 25, 2008, 05:15:00 PM
I have tried both and they work


Title: Re: Java Mandelbrot segment
Post by: cKleinhuis on December 26, 2008, 08:24:43 PM
:D

but now you shouldf think about implementing rational powers, e.g. z^2.5
or even complex powers :D
this is done via the polar view of complex numbers....

 i have to search
out the formula, and can post it here fort you ...


Title: Re: Java Mandelbrot segment
Post by: HPDZ on December 29, 2008, 06:58:52 PM
Here's how to raise a complex number to the power of another complex number. It's a fair amount of calculation:

First let us write

Z = a + bi
Y = c + di

We want Z to the power of Y

ZY = (a2+b2)Y/2e(iY Arg(Z))

where

Arg(Z) = the argument (polar angle) of Z = arctan(b/a)

To expand this all out to its real and imaginary parts explicitly:

First calculate u = c arg(Z) + (d ln(a2+b2))/2

Then ZY = (a2+b2)c/2 e(-d arg(Z)) [cos(u) + i sin(u)]

If you set d=0 you get the formula for raising a complex number Z to an arbitrary real-valued exponent c:

Zc = (a2+b2)c/2 {cos[c Arg(Z)] + i sin[c Arg(Z)]}


Title: Re: Java Mandelbrot segment
Post by: cKleinhuis on December 29, 2008, 08:01:24 PM
thank you, good explanation!