February 15th, 2014

Generate A Mandelbrot Set In H2O

RSS icon RSS Category: Uncategorized [EN]

Roses are red,
Violets are ~ Blue,
H2O is sweet,
And fractals are too!

In this post, we’ll dive into how to make some nice mathematical Valentine’s day grams using H2O!
(NB: No paper or scissors required! It does require an input dataset corresponding to the image dimensions you desire.
We generated a 1500×1500 pixel grid of (x,y)-coordinates with some Python code.)
While generating a Mandelbrot set and its image is not a big data task, H2O does enable you to produce a fairly high quality Mandelbrot image in just a few seconds.
As you may know, the Mandelbrot set (a subset of the complex numbers) is a set whose elements form the basis of a convergent sequence of complex numbers. The sequence is given by this recurrence relation

$$z_n = z_{n-1}^P + c$$

Where c is a “candidate” complex number. (Typically you’ll see $$P = 2$$ — that’s what we’ll do too).
We set the the size of the sequence to the number of iterations we want, and measure convergence by looking at the modulus of $$z_n$$ at each iteration. If the modulus exceeds some threshold, we say the sequence diverges and “c” is then not in the Mandelbrot set, otherwise it converges!
The point “c” is then colored according to the number of iterations accomplished. To produce really stellar images, it’s better to have fine-grained color gradients in your palette and then alpha compost them. The palette we used to generate the Valentine’s day grams in this post had ~200 colors. Although there is still some “banding”, the gradients look pretty smooth on my screen (Retina 15″ MBP).
OK, let’s get to work!

First Glance

At first glance, there are a few important inputs:

Image dimensions        - Height and Width of image in pixels. (We generated data by hand)<br />
Max Iterations          - Iterations of the recurrence relation.<br />
Convergence  Threshold  - What constitutes membership in the Mandelbrot set.<br />

Here are a few more that we may want later when we’re exploring our final images:

Zoom    - Zoom in on the Mandelbrot set at the (offsetX,offsetY) position.
offsetX - Zoom focus x-coordinate.
offsetY - Zoom focus y-coordinate.

The goal is to add a Mandelbrot UI for setting the input parameters and upon submitting produces the Mandelbrot image.
First let’s start off with a skeleton implementation and fill it in a little bit at a time:

* The main class that holds all of the Mandelbrot-related functionality.
*  Any UI page must subclass Request2 and implement Response.serve().
*  This is demonstrated below.=
public class Mandelbrot extends Request2 {
   * Declare any public API arguments here.
   * Here's where our inputs will go: Height, Width, Threshold,
   * Max Iterations, Zoom, OffsetX, OffsetY
    @Override protected Response serve() {
    // This is where we'll make the call to generate the Mandelbrot
    // set & produce the image.
  *  A class that represents our fractal.
  *  This is the centerpiece of our implementation.
  *  To do distributed & parallel work, we will have this
  *  class extend MRTask2 and override the map() method.
  *  More on this below....
  protected static class FractalSet extends MRTask2<FractalSet> {
        private double[] z_squared(double a, double b) {
        //return the square of the complex number z = a + ib
       private double modulus(double a, double b) {
       //return the modulus of a complex number z = a + ib
      public int iterate(double a, double b) {
      // take the candidate complex number c = a + ib
      // and iterate with the recurrence relation zn = z_{n-1}^2 + c
      // return the color
      private int palette(int iters) {
      //choose color from palette based on the number of iterations
      //Color returned is number of iterations modulo palette size
      private int linerp(int color1, int color2, double alpha) {
      //linear interpolate (AKA alpha compost) two colors.
       @Override public void map(Chunk[] cs, NewChunk[] ncs) {
        * Break the input into chunks and compute the
        * the iteration number per row (i.e. per pixel)
        * Produce a new frame with x, y, and decimal color.
  public class MandelbrotView extends JFrame {
     * Here we set the up details of the JFrame.
     * We use Java's BufferedImage to make the final image
     * This part is single threaded...

Starting with the FractalSet class, we can knock down the z_squared and modulus methods pretty easily:
The square of a complex number can be computed using the FOIL method, and remembering that the imaginary unit follows the same squaring rules as real numbers:

$$z^2 = (a + bi) * (a + bi) = (a^2 - b^2) + i*2*a*b$$

The modulus is a fancy name for length, or magnitude, or distance with respect to the origin. So:

$$|z| = \sqrt(a*a + b*b)$$

Our methods are written as follows:

private double[] z_squared(double a, double b) {
  double newa = a*a - b*b;
  double newb = 2*a*b;
  return new double[]{newa, newb};
private double modulus(double a, double b) {
  return Math.sqrt(a*a + b*b);

The palette is also pretty simple to implement. We chose colors suitable for the day, and there 193 of them. Again we chose colors according to the number of iterations taken modulo the number of colors in our palette:

private int palette(int it) {
      int[] color_palette = new int[]{7473449,7539499,7605805,7737391,7803697,7935284,8001590,8133176,8199482,8331325,8397375,8529217
      return color_palette[it % color_palette.length];

In order to compost two colors together, we’ll use a linear interpolation function that takes two colors, strips out their rgb components, blends the components and then combines the r, g, b colors into a single int value.

private int linerp(int color1, int color2, double alpha) {
      int r1 = (color1 >> 16) & 0xFF;
      int g1 = (color1 >> 8) & 0xFF;
      int b1 = color1 & 0xFF;
      int r2 = (color2 >> 16) & 0xFF;
      int g2 = (color2 >> 8) & 0xFF;
      int b2 = color2 & 0xFF;</p>
      int rgb = (int)Math.round(r1*alpha + r2*(1 - alpha));
      rgb = (rgb << 8) + (int)Math.round(g1*alpha + g2*(1 - alpha));
      rgb = (rgb << 8) + (int)Math.round(b1*alpha + b2*(1 - alpha));
      return rgb;

Let’s tackle the iterate()function now.
The idea here is to generate a new complex number while it does not exceed some threshold and the number of iterations is below some threshold. Additionally, though, we want to produce a color from this process, and attempt to smooth the color transitions caused by different numbers of iterations. Therefore, if the number of iterations reached is not maximal, then we can color using the following pieces of math:
$$\nu = (\log(z_n) / \log(T) ) / \log(T)$$
where $$T$$ is the threshold. By subtract this from the iteration count and then flooring it, we can retrieve a color from our palette. Retrieve the neighboring color from the palette and then compost them. Here’s what that code looks like:

public int iterate(double a, double b) {
      int n = 0;
      double it;
      double ca = (a - _density/2) / _zoom;
      double cb = (b - _density/2) / _zoom;
      a = b = 0;
      int iter = _maxIters;
      while((modulus(a, b) < _threshold) && (iter > 0)) {
        double[] z2 = zsquared(a,b);
        a = z2[0] + ca;
        b = z2[1] + cb;
      if ( iter > 0) {
        double zn = modulus(a, b);
        double nu = ( Math.log(zn) / Math.log(_threshold) ) / Math.log(_threshold);
        it = iter + 1 - nu;
      } else it = iter;
      int _it = (int)Math.floor(it);
      int color1 = palette(_it);
      int color2 = palette(_it + 1);
      return linerp(color1, color2, it % 1);

The last piece of this is the map. It takes a chunk array, which is a chunk of rows, and produces some new chunks. The coder doesn’t need to worry about how the chunks are managed, H2O takes care of all that.
The chunk array is expected to have length 2, since we are working with 2 columns of input (x-dim, y-dim). The output dimension is 3: x, y, color. Again, color is the output from the iterate() function above. Here’s what the map looks like:

@Override public void map(Chunk[] cs, NewChunk[] ncs) {
  assert cs.length == 2;
    for(int row = 0; row < cs[0]._len; row++) {
      double x = cs[0].at0(row);
      double y = cs[1].at0(row);
      int color = iterate(x,y);

@Override Response serve()

Let’s work on the serve() call as our last part (the JFrame part is standard Java, I’ll just hand that out at the end. No secret sauce there.)
There’s a standard “header” to each Request2 subclass that contains various UI knobs and declarations.
Here’s what a starting header might look like (OffsetX and OffsetY are left to the reader!):

public class Mandelbrot extends Request2 {
  static final int API_WEAVER = 1; // This file has auto-gen'd doc & json fields
  static public DocGen.FieldDoc[] DOC_FIELDS; // Initialized from Auto-Gen code.
  @API(help = "Destination key", required = false, filter = Default.class)
  protected final Key destination_key = Key.make("Mandelbrot");
  @API(help = "Data frame", required = true, filter = Default.class)
  public Frame source;
  @API(help = "Threshold to stop iterating.", required= true, filter = Default.class)
  public int threshold = 2;
  @API(help = "Max Iterations", required = true, lmin = 1, filter = Default.class)
  public int max_iters = 10000;
  @API(help = "Zoom in on Mandelbrot drawing (default is no zoom)", required = false, lmin = 1, filter = Default.class)
  public int zoom = 1;

The serve call will instantiate a new FractalSet object (constructor provided in full code) and that will invoke the computation with the function doAll(int, Vec[]), which takes an integer (the number of output fields) and a Vec array. A Vec is an internal H2O data format representing a single column vector. The output frame is then retrieved and passed on to a MandelbrotView object (not shown yet) that creates a new BufferedImage object, loads in the pixels and colors, and finally paints it. Here’s the serve() call code:

@Override protected Response serve() {
  try {
    FractalSet F = new FractalSet(max_iters, zoom, threshold, source.anyVec().length());
    Vec[] vecs = source.vecs();
    F.doAll(3, vecs);
    Frame fr = F.outputFrame(destination_key, new String[]{"x", "y", "decimal_color"}, null);
    UKV.put(destination_key, fr);
    new MandelbrotView(fr).setVisible(true);
  } catch( Throwable t ) {
    return Response.error(t);
  return Inspect2.redirect(this, destination_key.toString()); //MandelbrotView.redirect(this, destination_key);

And last but not least, the MandlebrotView class:

public class MandelbrotView extends JFrame {
  private final int width = 1500;
  private final int height = 1500;
  private BufferedImage I;
  private Frame _fr;
  public MandelbrotView(Frame fr) {
    super("Mandelbrot Set");
    _fr = fr;
    setBounds(100, 100, width, height);
    I = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
    Vec[] vecs = fr.vecs();
    for (int row = 0; row < vecs[0].length(); row++) {
      I.setRGB((int)vecs[0].at8(row), (int)vecs[1].at8(row), (int)vecs[2].at8(row));
  @Override public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    RenderingHints rh = new RenderingHints(
    RenderingHints rh2 = new RenderingHints(
    RenderingHints rh3 = new RenderingHints(
    RenderingHints rh4 = new RenderingHints(
            RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g2.drawImage(I, 0, 0, this);

Here’s a few more images generated by this code:
Happy Valentine’s Day!

Leave a Reply

H2O Wave joins Hacktoberfest

It’s that time of the year again. A great initiative by DigitalOcean called Hacktoberfest that aims to bring

September 29, 2022 - by Martin Turoci
Three Keys to Ethical Artificial Intelligence in Your Organization

There’s certainly been no shortage of examples of AI gone bad over the past few

September 23, 2022 - by H2O.ai Team
Using GraphQL, HTTPX, and asyncio in H2O Wave

Today, I would like to cover the most basic use case for H2O Wave, which is

September 21, 2022 - by Martin Turoci
머신러닝 자동화 솔루션 H2O Driveless AI를 이용한 뇌에서의 성차 예측

Predicting Gender Differences in the Brain Using Machine Learning Automation Solution H2O Driverless AI 아동기 뇌인지

August 29, 2022 - by H2O.ai Team
Make with H2O.ai Recap: Validation Scheme Best Practices

Data Scientist and Kaggle Grandmaster, Dmitry Gordeev, presented at the Make with H2O.ai session on

August 23, 2022 - by Blair Averett
Integrating VSCode editor into H2O Wave

Let’s have a look at how to provide our users with a truly amazing experience

August 18, 2022 - by Martin Turoci

Start Your Free Trial