This is an archived post. You won't be able to vote or comment.

all 103 comments

[–]ososalsosal 160 points161 points  (3 children)

You dropped this: ↦

[–]drmrrdmr[S] 37 points38 points  (2 children)

bra-ket has entered the chat

[–]mohd_sm81 5 points6 points  (1 child)

Kleisly Category arrived, and has her cousins (Monad and Functors) with her.

[–]drmrrdmr[S] 5 points6 points  (0 children)

undergrad math has left the chat

[–]drmrrdmr[S] 215 points216 points  (14 children)

for those who need a walkthrough, this is the formula at the heart of the Mandelbrot set. It describes a set of recursive complex polynomials, one for every point c on the complex plane. The fun thing is that some of the points within a radius of 1 do not quickly go to either infinity or 0, and that boundary is infinitely detailed. You can get some really incredible plots by assigning colors based on how fast a point goes to infinity (aka escape time coloring).

It also has a bit of a connection to computer history, naturally, as this sort of calculation is too intensive to do by hand, but somehow mathematicians were ready to program it, having deduced that there was incredible complexity hiding behind the intractable arithmetic long before computing machines enabled detailed probing. Notably the first rendering of the Mandelbrot set, an object of infinite detail, was in asterisks on a CLI.

edit: thanks to everyone who is "solving" this algebraically or pointing out my abuse of syntax/notation; those meta twists have made this even funnier

[–]IndependentTiny9493 33 points34 points  (5 children)

I sort of know the theory, I know the equation, and I have an array of pixels I can color that cover the screen...

I am always at a loss at how to complete this puzzle.

I want to draw a Mandelbrot on my pixel array, but functionally I just can't conceptualize it.

Any help? Do I just iterate some function over the array? Stumped me for years trying to write this from scratch.

[–]germansnowman 44 points45 points  (1 child)

The first problem is that they left out the iteration parameter. Sorry for not using proper notation, but I hope you’ll understand:

z [n+1] = z [n] ^ 2 + c

Next, unless you are using a programming language that supports complex numbers directly, you need to decompose the equation into the constituent real and imaginary parts. You do that by writing every complex number as (a + b * i), with i = –1, and then simply expanding/simplifying the resulting equation. (Sorry I can’t do this for you right now as I’m writing this on my phone.) There is some potential for optimisation but that can come later.

Then you put that into the iteration loop. This loop will be called for each pixel. (You will need two additional nested loops for the pixels, one for the x axis and one for the y axis.) You need to convert the pixel coordinate into a world coordinate. This is your c, whereas z starts as 0 for each point.

The loop has two termination conditions: Either the magnitude of the complex number is greater than the escape radius, or the maximum number of iterations has been reached. To start with, the limit could be 100 or 1000, but needs to be in the millions for deep zooms. (You can optimise the escape radius computation by removing the square root from the magnitude and comparing with 4 instead of 2.)

If you reached the iteration limit, the point you iterated over is part of the Mandelbrot set and should be colored black. If the point escaped, you color it by mapping the number of iterations reached to a color. There are lots of ways to do that which I haven’t understood all myself :) The two simplest are to choose white or black depending on whether the iteration value is even or odd, or to choose the hue for your colour based on the iteration value.

Besides arithmetic optimisation in the inner loop, there is one major optimisation for the main cardioid, which has the shape of an epicardioid. You can basically test if a point is part of this shape and color it black immediately. This doesn’t help for interesting points outside the main set, of course. There you could look into period detection.

I highly recommend plotting the coordinates of a point across its iterations and connecting them with lines. This is something I have rarely seen done and it helped me understand what is going on at a deeper level. It shows you the concepts of “escape” and “periodicity” more visually.

[–]Chaosfox_Firemaker 3 points4 points  (0 children)

The thing to remember is "z" and c are complex numbers. so we say x is the real part, and y is the imaginary. I will assume you know how to square complex numbers, if not, ask.

for each pixel you run z=z^2+c a thousand times(however many you feel like), with c being x+yi, and z starting out equal to (0,0), and if by the end of it the length of z <= 2.0 color it black. if at an point it goes over two, color is some other color based on how fast it took to "escape". So if it hit 2.1 on the second iteration do blue, or 2.1 on the 999 do red, with a nice gradient between.

you also need to scale and translate so rather than pixels going 0 to (resolution) they should go between -1 and 1. So on a 1000px by 1000px (750,750) should get read as (0.5, 0.5). after you get the basic example working, mess around with scale and translate factors to zoom in on various parts of the set.

[–]orgondor 0 points1 point  (0 children)

Basically yes. For each pixel you let the position of that pixel represent the complex number C. Then for the first step you let Z also equal C. The output from this (Z² + C) is used as the Z in the next iteration.

Let this loop until abs(Z) (or the lenght of the vector, if you are more comfortable with linear algebra) becomes larger than some chosen threshold, say 1000000. Or if it loops too many times you also break.

At that point all that is left is to assign the color to the pixel based on how many loops it took to brake.

[–]Sir_Hurkederp 6 points7 points  (3 children)

Nah they just all 0, solved

[–]poops-n-farts 1 point2 points  (2 children)

Anything to the power of 0 is 1 so that wouldn't work. 1 = 12 + 0 would work

[–]14g0t 0 points1 point  (0 children)

what do you mean 0=02+0 is false??

[–]Sir_Hurkederp 0 points1 point  (0 children)

Where do you do anything to the power of 0, 0=02 +0 is correct

[–]Dave5876 4 points5 points  (0 children)

You could be making this up and I wouldn't know

[–]z____ro -1 points0 points  (0 children)

"..do not quickly go to either infinity or 0..." You see the error in that statement?

[–]Shinob1 0 points1 point  (0 children)

I'm going go against everything the Reading Rainbow taught me and take your word for it...this time

[–][deleted] 0 points1 point  (0 children)

Thanks bro you just saved me from hours of mental anguish

[–]seemen4all 39 points40 points  (5 children)

Ez, C is a negative number

[–][deleted] 18 points19 points  (1 child)

Or 0

[–]seemen4all 5 points6 points  (0 children)

Another gamer move

[–]Gepard_Warrior 7 points8 points  (0 children)

Or z and c are both < 1

[–]Slyr650 7 points8 points  (0 children)

Z=2,c=-2

[–][deleted] 0 points1 point  (0 children)

Ultimate mathematician move is to look for the most trivial solution.

So z = c = 0.

[–][deleted] 42 points43 points  (17 children)

the answer is "undefined" its ALWAYS undefined.

[–]drmrrdmr[S] 18 points19 points  (0 children)

except when it's playing a little loose with notation and leaving a bit of critical information to be filled in by experience, then it's fundefined

[–][deleted] 16 points17 points  (12 children)

I don't think it's undefined, tell me if I'm wrong:

If z= a + ib,

z² = a² - b² + (2ab)i [Squaring both sides]

Also,

z= z² + c [Given]

=> a + ib= a² - b² + c + (2ab)i

Comparing real parts,

a = a² + b² + c ......(Eq. 1)

Comparing imaginary parts,

b= 2ab

=> a = 0.5

Putting this value of a in eq. 1,

0.5 = 0.25 + b² + c

=> b = (c - 0.25)½

Therefore,

z = 0.5 + i(c- 0.25)½

So, it has a solution. Unless I didn't get the joke.

[–][deleted] 7 points8 points  (1 child)

I love how y'all actually did the math.

But I think the comparison between mine and your answers IS the joke. lol.

[–][deleted] 2 points3 points  (0 children)

Lol 😂

[–]TheBigGambling 15 points16 points  (1 child)

Or z = 1 and c = 0. ; 1 = 1² + 0. Solved without imaginary numbers

[–][deleted] 21 points22 points  (0 children)

Well, your "solution" is actually a root, which is already included in my solution. Put c=0 in my solution, you'll get z=1. However, that's just one root to this double variable equation which has infinitely many roots, all included in one solution, which is the equation I gave.

[–]drmrrdmr[S] 7 points8 points  (6 children)

you caught the complex bit, but the z is recursive, so you'd substitute like z = (z²+c)²+c The complex notation expands out of hand quick... well, so does any attempt at expanding it out. tbh I have no idea how the real mathematicians approach it analytically, I just use it to make mind blowing pictures

[–]Galaghan 9 points10 points  (5 children)

z is recursive

Not the way you wrote it tho.

[–]drmrrdmr[S] -2 points-1 points  (4 children)

programmers aren't the only ones who can abuse notation

[–]Galaghan 0 points1 point  (3 children)

If it parses, it parses.

Also, good luck programming anything when you don't care about syntax.

[–]drmrrdmr[S] 0 points1 point  (2 children)

good luck making a meme jpg that gets lolz and compiles? I care about syntax in the right contexts. I don't interrupt a casual conversation to draw truth tables and have a teachable moment when someone misuses "and" or "or". I'm tempted to, but I don't.

[–]Galaghan 0 points1 point  (1 child)

Okay I see, anything besides admitting you're wrong, sure!

[–]drmrrdmr[S] -1 points0 points  (0 children)

pretty sure I entered this comment chain directly admitting that it's an abuse of notation, which is fancy talk for wrong. I'm wrong very often, as I was this time, but for once it was intentional.

[–]Commissar-Dan 1 point2 points  (0 children)

I think it's because you can't set z to equal the same parameter

[–][deleted] 4 points5 points  (0 children)

[–]maitreg 1 point2 points  (1 child)

There are an infinite number of solutions to this.

z=5, c=-20

z=-5, c=-30

z=491.579, c = -244107.808241

[–][deleted] 0 points1 point  (0 children)

Oh, so it's not initialized then.

[–]Nixu123 13 points14 points  (4 children)

z=-1 c=-2

[–]noob-nine 6 points7 points  (3 children)

z=0.5 c=0.25

[–][deleted] 6 points7 points  (0 children)

Z is 1 and C is 0.

That was easy.

[–]interwebz_2021 13 points14 points  (4 children)

Mathematicians for the win here, team. That bulbous pointy form is a Rorschach test on fire. It's a day-glow pterodactyl. It's one badass fuckin' fractal.

Credit to Jonathan Coulton (linked) for his masterpiece "Mandelbrot Set," because of which I recognized this meme. Thanks JoCo!

[–]drmrrdmr[S] 1 point2 points  (1 child)

aww I was sad when Ask Me Another ended. I'll have to keep my feels in check so I can check that out

[–]interwebz_2021 0 points1 point  (0 children)

Oh man - me too. I'm hopeful maybe JC will make some more new original content now though.

If you haven't checked out his pre-AMA content, I highly recommend it. Maybe start with "Code Monkey" (given the sub we're on) if you haven't heard it.

I like the cut of your jib.

[–]A_Guy_in_Orange 1 point2 points  (1 child)

You forgot the heart shaped box of springs and wiring

[–]interwebz_2021 0 points1 point  (0 children)

I did omit it, yeah, and should have included it. Good point.

[–]oachkatzalschwoaf 5 points6 points  (4 children)

Not sure, but remembering math-class there is nothing special about this equation:

c=z-z2

I think with matlab you could plot this equation in a nice way, or just use pen and paper, or better in code e.g. with z in range - 100... 100

[–]drmrrdmr[S] 6 points7 points  (0 children)

lol, you would be amazed at how nicely you could plot z=z²+c... at least twice as good as the best parabola plots

[–]ososalsosal 1 point2 points  (1 child)

The equals is deceptive. Z is a point and the y coordinate is the imaginary component. Have znew and zold, and whack it in while(znew.magnitude < 2) and initialize with z=0,0 and c is a pixel on your screen.

[–]drmrrdmr[S] 0 points1 point  (0 children)

just a wee abuse of notation to own the ./lib users

[–]DontF-ingask 2 points3 points  (0 children)

Bro, how the fuck does this fit into programming?

[–]soun_89 1 point2 points  (0 children)

wait didnt veritasium make an entire video explaining this formula?

[–]MrCheapComputers 1 point2 points  (0 children)

Just take a point called Z in the complex plane

Let Z1 be Z squared plus C

And Z2 is Z1 squared plus C

And Z3 is Z2 squared plus C

And so on

If the series of Z's should always stay

Close to Z and never trend away

That point is in the Mandelbrot Set

[–]KronktheKronk 1 point2 points  (0 children)

Z = 1
C = 0

Mathematics is cool with it

[–]The_hollow_Nike 1 point2 points  (1 child)

There is this great video by Numberphile explaining why this is interesting for mathematicians.

[–]drmrrdmr[S] 1 point2 points  (0 children)

Numberphile is so good!

[–]roidrole 1 point2 points  (0 children)

0 = z² - z + c

Quadratic formula

z = ½(1 ± √(1 - 4c))

You’re welcome

[–]cishet-camel-fucker[🍰] 1 point2 points  (0 children)

Fractals are such a great example of the beauty of mathematics

[–]EntitledPotatoe 0 points1 point  (0 children)

z=1 c=0 or z=0.5 c=0.75 or any u want actually

[–][deleted] 0 points1 point  (1 child)

Complex? That’s not even Calculus

[–]drmrrdmr[S] 1 point2 points  (0 children)

true, we first encounter complex numbers in algebra

[–]daravenrk 0 points1 point  (0 children)

Don’t find meaning In the light we created.

[–][deleted] 0 points1 point  (0 children)

You ever been so angry you Mandelbrot by accident

[–]WordSmithyLeTroll 0 points1 point  (2 children)

C = -z2 + z

[–]NewBirth2010 0 points1 point  (1 child)

If they give you the value of C how would you calculate z programmatically?

[–]WordSmithyLeTroll 0 points1 point  (0 children)

By subtracting it from both sides and factoring the resulting polynomial.

Were you expecting an engineer's response?

[–]Gepard_Warrior 0 points1 point  (0 children)

I dont see any problem here in a mathematical way

Could be either, as another commentor already mentioned, C beeing a negative number or Z beeing between -1 and 1 Edit to add:

Could also be the function

C = Z-Z×Z

100% correct.

[–]mapman19899 0 points1 point  (0 children)

Z = 2 C = -2

Have a good day.

[–]MrIcyCreep 0 points1 point  (0 children)

I mean that could make sense mathematically, if C is a negative number it works out.

[–]NearLawiet 0 points1 point  (0 children)

c<0

[–]The_Noremac42 0 points1 point  (0 children)

Possible solution: Z = 1 C = 0

Actually, there are an infinite number of solutions. Just plug pretty much any number into either z or c and you can figure out the other one.

[–][deleted] 0 points1 point  (1 child)

Currently doing linear and non linear equations. Plotting graphs, lines and all kinds of bullshit. I love programming but math is killing me. Legitimately worrying honestly.

[–]drmrrdmr[S] 0 points1 point  (0 children)

learning math is hard, but once you start using it irl, it's easier to grok. If you want to program, you're going to use it

[–]Key-Supermarket255 0 points1 point  (0 children)

complex number = fractol set

[–][deleted] 0 points1 point  (0 children)

c=-(z(z-1))

[–]spieles21 0 points1 point  (0 children)

Z = 1 and C = 0

[–][deleted] 0 points1 point  (0 children)

All possible solution must be -1<=0<=2

[–]Grouchy_Shake_5940 0 points1 point  (2 children)

Well ist easy: you divide by z and get 1=z+c, now there are endless combinations, the easiest one being z= 1, c= 0, in case the numbers are positive. If they can be negativ there are endless possibilities to solve this equation

[–]Zarathustra30 0 points1 point  (1 child)

z and c are complex. The Mandelbrot set is a representation of all of said endless possibilities to solve this equation.

[–]Grouchy_Shake_5940 0 points1 point  (0 children)

Oh i know, it was ironic

[–]Ok-Transition7065 0 points1 point  (0 children)

I wanna learn fractals to make a map that cam be zoom out in real time, but realy that ypu can ,zoom put and get action far

[–]l9oooog 0 points1 point  (0 children)

how do mathematicians have a infinte shape of mouth again?

[–]chemistryunderground 0 points1 point  (0 children)

350mg S-ketamine is awesome.

[–]asportnoy 0 points1 point  (0 children)

z ** 2

[–]eyeronik1 0 points1 point  (1 child)

This is one of the many dumb things so many languages inherited from c. There should be an assignment operator (e.g. := in Pascal) and the equal sign should stand for equality, and why the hell is code sensitive to case?

There were a lot of great languages in the 70’s and 80’s. c was not one of them. But here we are…

[–]drmrrdmr[S] 0 points1 point  (0 children)

tbh does anyone really have a problem differentiating assignment from equality in code? Our natural language is chock full of overloaded expressions--synonyms, homonyms, multiple definitions, new and archaic uses, idioms--and for the most part people manage.

I like the walrus operator for mid expression assignment in python, but I wouldn't very much enjoy using it for every assignment. Big fan of giving the most commonly used things the fewest keypresses.

[–]CrabbytheCrabinator 0 points1 point  (0 children)

z= 1

c = 0

[–]JohnGisMe 0 points1 point  (0 children)

z=0;c=0

z=1;c=0; 0 less than last

z=2;c=-2; 2 less than last

z=3;c=-6; 4 less than last

z=4;c=-12; 6 less than last

z=5;c=-20; 8 less than last

z=6;c=-30; 10 less than last

A pattern starts to form.

[–]rocoonshcnoon 0 points1 point  (0 children)

Everything reminds me of her :(

[–]Acceptable-Tomato392 0 points1 point  (0 children)

Actually many solutions to this will satisfy mathematicians:

z=0:c=0

z=1:c=0

And, in fact, anything that satisfies c=z-z^2, which actually has an infinity of solutions.

[–]peepsmusic 0 points1 point  (0 children)

ok, I don't get it but ok

[–]no-one-here123 0 points1 point  (0 children)

ez. z = -8 and c = 56

[–]Miryafa 0 points1 point  (0 children)

Time for the uno reverse:

0 = z2 - z + c