# Barnsley Fern in Python 3

## Posted on November 2, 2017 by Sol

In this article, I will show you how to render the Barnsley Fern in Python 3. The Barnsley Fern is a fractal that can be generated using four simple affine transformations of the form:

where the coefficients of the transform are:

 w a b c d e f p 1 0 0 0 0.16 0 0 0.01 2 0.85 0.04 -0.04 0.85 0 1.6 0.85 3 0.2 -0.26 0.23 0.22 0 1.6 0.07 4 -0.15 0.28 0.26 0.24 0 0.44 0.07

In the above table p represents the probability factor for a transform. For example, the second transform will be used 85% of times, third transform 7% and so on.

From a practical point of view, the Barnsley Fern is generated starting with an initial point ${x_{1}}$, $(0, 0)$, and iteratively calculating the next point ${x_{i}}$ using one of the above ${w_{i}}$ transforms.

At each step, we generate a random number r from the interval [0, 1) and, interpreting this number as a probability, we pick the corresponding transform. For example, if r is in the interval $[0, 0.01]$ we pick ${w_{1}}$, if r is in the interval $(0.01 + 0.85, 0.01 + 0.85 + 0.07]$ we pick ${w_{3}}$ and so on. Note, we define the intervals using the cumulative sum of the probability factors.

This is the result, after 1000000 iterations:

# PPM image from scratch in Python 3

## Posted on October 25, 2017 by Sol

Someone asked me if it is possible to write image files in pure Python, without any libraries. The answer is, of course, you can implement any image format in pure Python, as long as you have a clear description of the image format and you know how to write binary files.

In this article, I will show you how to write binary PPM images in pure Python. Linux and recent versions of macOS have native support for PPM files, so you can open a PPM image using the default OS image viewer. For Windows, you can use a program like IrfanView. The PPM file format can store uncompressed RGB images in binary or human readable, ASCII format. As a side note, the PPM format is not an efficient format to store images, however it is relatively easy to implement and use, from a programming point of view.

# Building GCC 7 on Chrome OS

## Posted on October 5, 2017 by Sol

This is a short introduction on how to build GCC 7 from sources on your Chromebook or Chrome OS device. I’ve tested the procedure on an Intel based Chromebook with 4GB of RAM. In principle, same approach should work on an ARM device. I assume that you’ve enabled the Developer Mode on your Chrome OS device.

In order to be able to build GCC 7, you will need a C++ compiler. You can install an older compiler toolchain from Chromebrew, for more details read my previous article or, if you prefer the short version, open a shell tab and write the next commands:

1 shell
2 wget -q -O - https://raw.github.com/skycocker/chromebrew/master/install.sh | bash
3 crew install buildessential
4 crew install autconf
5 crew install automake


At this point, you should have GCC 4.9 installed on your machine. We will use the older 4.9 to compile GCC 7.

# Chrome OS native development

## Posted on September 13, 2017 by Sol

Today is the third day of my Two weeks programming on a Chromebook challenge. For the first two days I’ve played with enabling the Developer Mode on my Chromebook and installing Crouton and GCC 7 for C++17 development. Running Chrome OS and Crouton side by side is the easiest path for a complete Unix like development experience.

I think it could be interesting and more challenging for me personally, to investigate if I could do native Chrome OS development. By native I mean using only applications that run directly on Chrome OS or can be compiled to run on Chrome OS. Using a separate OS is not an option!

# C++ local development on a Chromebook or Chrome OS device

## Posted on September 11, 2017 by Sol

In this article, from my Two weeks programming on a Chromebook challenge, I will show you how to install locally a modern C++ development environment on your Chromebook.

Please make sure that you have Developer Mode enabled. If you want to be able to use a modern C++, like the one included with GCC 7, I suggest you to install Crouton using a CLI only install or using the LXDE desktop environment. My suggestion is to use the LXDE approach, unless you are Linux veteran and prefer to do your development from command line only.

# My two weeks programming on a Chromebook challenge

## Posted on September 11, 2017 by Sol

Introduction:

A few days ago, I bought a Chromebook for testing purposes. I was curious to see if it is possible to use it as a general development machine. My challenge is to use only the Chromebook and my iPhone for the next two weeks. I’m allowed to use local or external servers as long as I access these from my Chromebook.

In its default configuration, a Chromebook is not a developer machine. Chromebook was designed to be used as a cloud connected device, that can be used occasionally offline. The machine has a strong emphasis on security and ease of use for Internet related tasks: browsing, writing documents, email and so on.

# Building Python 3.6 from source on Ubuntu and Debian Linux

## Posted on June 30, 2017 by Sol

This is a short article about building Python 3.6 from source on Ubuntu 16.04 or Debian 9 Linux. At the time of this writing both Ubuntu LTS and Debian stable come with Python 3.5. Python 3.6 comes with a production ready asyncio module, new formatted string literals and many more improvements vs the old 3.5 version. You can read more about what’s new in Python 3.6 here. The procedure described in this tutorial also works with Windows Subsystem for Linux, WSL, aka Bash on Ubuntu on Windows.

As a side note, you should be able to use the same procedure on other Debian based systems, like Raspbian on Raspberry Pi, just replace apt with apt-get and you are good to go.

# Compiling GCC 7 on macOS

## Posted on May 21, 2017 by Sol

In this tutorial, I will show you how to compile from source and install the current stable version of GCC with Graphite loop optimizations on your macOS computer. The instructions from this tutorial were tested with Xcode 8 and Sierra (macOS 10.12).

Clang, the default compiler for macOS, supports only C, C++, Objective-C and Objective-C++. If you are interested in a modern Fortran compiler, e.g. you will need gfortran that comes with GCC. Another reason to have the latest stable version of GCC on your macOS is that it provides you with an alternative C and C++ compiler. Testing your code with two different compilers is always a good idea.

# Emacs 25 on Windows Subsystem for Linux

## Posted on May 18, 2017 by Sol

Recently, a reader of this blog asked me about using Emacs under Windows Subsystem for Linux. According to the WSL documentation, Emacs, Vim and other Linux applications that run from console should work just fine. Turns out that the situation is a bit more complex. At the time of this writing, Emacs works fine for normal text editing. But, when you try to install a new package directly from Emacs, the program is stopped and sent to background and doesn’t seem to be able to cleanly recover when you use fg. From a practical point of view, this makes Emacs, under WSL, useless if you need to install any package that is not built in.

Fortunately, there is a workaround for the above bug and I will show you in this post how to avoid the problem. As far as I know, Emacs under WSL seems to work without a problem only if you install a desktop environment like Xfce 4. The idea is to use the GUI version of Emacs when you need to install a package. For normal editing, you can use Emacs from the WSL console.

# Building GCC 7 on Windows Subsystem for Linux

## Posted on May 4, 2017 by Sol

In this article, I will show you how to compile from sources GCC 7.1 on WSL, Windows Subsystem for Linux. The default version of GCC, at the time of this writing, is 5.4 which is pretty old. GCC 7.1 has complete support for C++11, C++14 and experimental support for the current C++17 draft.

GCC 7.1 has *C++14 support enabled by default. If you want to try the new C++17 support use the -std=c++1z flag, example:

1 g++-7.1 -std=c++1z test.cpp -o test


## Posted on May 2, 2017 by Sol

I assume that you have the latest Xcode installed on your Mac, and that you know how to code small iOS applications in Swift 3.

I also assume that you have access to a server, ideally you have your own server with a domain name and a SSL certificate installed. If this is not the case, you can use a local Apache or NGINX web server. Personally, I use the free version of MAMPP which allows me to easily switch between Apache and NGINX if necessary. A good alternative to MAMPP is XAMPP. As a side note, macOS comes with an old version of Apache and PHP 5.3 already installed.

# Getting started with Swift 3 on Windows Subsystem for Linux

## Posted on April 19, 2017 by Sol

Recently, I’ve played a bit with Swift 3 on WSL (Windows Subsystem for Linux). In this article, I will show you how to install the latest stable version of Swift under WSL. Combining the open source Swift compiler with a text editor like Visual Studio Code is quite a pleasant coding experience, as you can see in the next screenshot:

# Using the Windows Subsystem for Linux with Xfce 4

## Posted on April 16, 2017 by Sol

In my previous article, I’ve shown you how to install WSL, the Windows System for Linux, on a fresh Windows 10 Creators Update. Officially, at the time of this writing, WSL supports only command-line applications. However, you can install graphical Linux applications and run these under WSL if you have an X server like VcXsrv or Xming, on your Windows machine.

Assuming that you have a working WSL installation, start Bash on Ubuntu on Windows, or open a Command Prompt and type bash in it. First step is to install a window manager. From my experience, Xfce works reasonably well with WSL:

1 sudo apt install xfce4


# Install WSL - Windows Subsystem for Linux

## Posted on April 15, 2017 by Sol

With the recent release of Windows 10 Creators Update, I’ve decided that I want to test the Windows Subsystem for Linux, WSL, aka Bash on Ubuntu on Windows. WSL is a way to run unmodified Ubuntu binaries on Windows natively, using the Windows kernel instead of the Linux kernel. The advantage of WSL is that you can run Linux command-line applications without the extra weight of a virtual machine. At this time, WSL on Windows 10 Creators Update uses binaries from Ubuntu 16.04.2.

If you want to run WSL on your Windows 10 installation, make sure that you have a 64 bits machine with a 64 bits Windows. If this is the case, check if your system is updated. Ideally, you will have at least the Creators Update which is version 1703. You can check the above by going in Settings → System → About:

# Transferring data between Matlab and Armadillo

## Posted on April 12, 2017 by Sol

In this article, I will show you how to move data between Matlab and Armadillo, specifically how to move matrices between the two. If you need an introduction in how to install Armadillo on your system, check my previous article.

Easiest approach is to transfer data between Matlab and Armadillo using text files. For example, the next Matlab code will generate some data and save it in a text file that can be loaded from Armadillo:

1 A = rand(4,5);
2
3 % Save the matrix A to "matlab_data.txt" the content of the file is overwritten
4 dlmwrite('matlab_data.txt', A, 'delimiter', '\t', 'precision', 16);


1 arma::Mat<double> A;