Posts

Showing posts from October, 2017

Random Walks to solve Diophantine Equations

Image
Random walks (check my older post here ) and Diophantine equations are two simple mathematical beasts. After a college seminar, I tried putting them together to make something neat, and came up with this: just pick a Diophantine equation, simulate a random walk, and try to see if the random walk went over any solutions! In this report I briefly go over how I came up with this, and show the code I wrote/some results. The Matlab code is all in here (I also have some Python code about random walks in general here , check my post !). In the image above, the blue line represents a path taken by the random walk and in orange/red the nodes tell us how far we are from a solution (the smaller the node, the closer we are). Note that this notion of proximity isn't given by actually computing the distance to a known solution, but just by looking at how similar the two sides of the equation are. The image below shows the evolution of three independent random walks trying to find an example ...

Two-dimensional random walk simulations

Image
Think of a drunk man that continuously tumbles left and right, back and forth, with no final destination. That can be thought of as a random walk on the plane... Now imagine that the drunk man has a shorter leg and tumbles more to one of the sides: that is a biased random walk. Now imagine the drunk man can teleport to a nearby location. That is (kind of) a Lèvy flight ! All those are quite interesting to observe in motion and I implemented them in Python. (the code can be found here ). From those, only the tilted walks and the one with the trail are such that the particle's movement wraps around the borders (leaving the green area makes the particle appear in the opposite side, just like the snake from the game Snake). The image above is a screenshot of the execution of the random walk that leaves a coloured trail. I also implemented an animation where the screen gets progressively filled with circles of different colours, as if being splattered with paint. Not sure why... But it...

Egyptian multiplication with Haskell

Very recently a saw a Youtube video from a friend, MathGurl , in which she explained the ancient Egyptian multiplication method. At the same time, and for no particular reason, I remembered Haskell, so I decided to implement the method. It was not a big feat of programming, but I did enjoy relearning the basics of Haskell I once knew. You can find the file with the implementation here. (The implementation only works for non-negative integers) The method is quite simple and works because of the binary expansion of a number. Basically, if you want to calculate a * b , either b is even or odd. If b is even, just cut b in half and duplicate a to compute (2a)(b/2) . If b is odd, then ab = a + (2a)*((b-1)/2) . Another way of thinking about this is by writing b in the form b = 2^(k_1) + 2^(k_2) + ... + 2^(k_n) and then having ab = a( 2^(k_1) + 2^(k_2) + ... + 2^(k_n)) = a2^(k_1) + a2^(k_2) + ... + a2^(k_n) . My formulation is just the recursive way of writing it. If I get the courage t...

Fractals and the Filled Julia set

Image
The Filled Julia set is another fractal that can be built with the complex numbers, just like the Mandelbrot set. To build a Filled Julia set, we pick a complex number c and then repeatedly apply f(z) = z^2 + c to every point of the plane. If that iterative application of f produces a sequence that goes to infinity, the point does not belong to the Filled Julia set. If it does not go to infinity, then the point belongs to the Filled Julia set and is coloured black. Using Python and pygame I developed a small script that allows the user to click a point on the complex plane and create its Filled Julia set. The code, which is very simple, can be found here . A Windows executable can be downloaded from here . Pressing the left arrow shows the previous Filled Julia sets you created, while the right arrow takes you to the more recent ones. Pressing the spacebar erases the current fractal and shows the initial black axis. [Pt] O 'Filled Julia set' é outro fractal que pode ser def...

On recursive functions and Kleene's approach

In Computation Theory it is of interest to study properties about functions, and what functions satisfy said properties. We then consider the set of all functions that satisfy those properties. One of those sets is the set of recursive functions R as defined by Kleene. To define R, Kleene gives a series of different primitive functions that are known to be in R, and then defines some operations that preserve functions in R. For the purposes of what I will be sharing next, I will just enumerate said primitive functions and constructions, so that the reader is aware of what will be used (notice that what comes below is almost identical to what you can see here ). The primitive functions are: The constant functions of arity 0, one for each natural number; The zero function of arity 1, that always returns 0; The successor function of arity 1, that sends x to x+1 ; The projection{a,b} of arity a, that returns the b-th argument unchanged. For example, projection{3,2}(a,b,c) = b . After tha...

Creating a programming language from scratch!

Image
Python is the programming language I am more comfortable with, by far; at an early stage of my learning I heard that Python was built on top of C. Whatever that meant. Then I started thinking of it in this way: programming languages are used to build programs. Someone used C to create another program, that can be used to create even more programs! That seemed reasonable. At some point in time I got interested in compilers and interpreters and all those weird things, and I came across this wonderful blog post. I immediately started following the "Let's Build a Simple Interpreter" series to write my own Pascal interpreter with Python, which is the aim of that series. At that time there were only 5 or 6 posts, and I got to the point where I had done everything he told us to do and couldn't wait for the other posts, as I noticed the frequency with which the author posted in that series was rather low. At that point, my project's aim diverged of that of the blog and I...

Minesweeper remake

Image
Minesweeper has to be one of the most well-known games ever. You start with a fully covered grid and your aim is to flag all mines that are hidden. Upon pressing a cell, if it didn't have a mine, you will be told how many mines there are adjacent to that cell you uncovered. For this game, cells that share only a corner are also considered adjacent: any cell that is not on the border of the frame has 8 adjacent cells; one for each side and one for each corner. The controls are similar to those of the original game. Left-click to uncover a cell and right-click to (un)flag it. You win if you flag all the mines and uncover all the other cells. You lose if you uncover a mine. The code was written in Python3 and made use of pygame. The source code and the artwork can be found here ; a windows executable can be found here . [Pt] O minesweeper é um jogo muito icónico dos dias de hoje, com instruções fáceis, mas por vezes não tão fácil de jogar quanto isso! O código fonte e os gráficos nece...

Pigeon (pooping) simulator

Image
This project was the first decent game I wrote. Inspired by flappy bird and by my crazy English teacher, who jokingly talked about a pigeon simulator game, I created the Pigeon (pooping) Simulator. The game controls are pretty simple. In the first screen you choose the difficulty, which relates to how fast the pigeon flies. Use your keyboard + and - buttons to adjust it. Inside the game, use the spacebar to poop. At any time, pressing Q exits the game. Your goal is to hit the people below you. Beware though, as you can't poop constantly. After 60s of flying and pooping, another screen shows up with some stats. The code was written in Python3 also using pygame. The amazing artwork was also created by me, using Paint. Both the code and the images can be found in this folder. A windows executable can be found inside this folder as well. Remember that to run the executable, the folder needs to be left as-is. [Pt] Este foi o meu primeiro jogo decente. Inspirado pelo flappy bird e por...

Fractals and the Mandelbrot set

Image
I have always liked the concept of fractal. They are very beautiful, they have a notion of infinity embedded in it, and they make no sense (seriously though, self-similarity ?). How couldn't they be loved? Despite being fond of fractals, I had never understood them because I didn't know how to mathematically define one. I knew how to draw some, for example the snowflake or the Sierpinski triangle , but drawing and mathematical definitions aren't quite the same thing. Enlightenment struck after watching this video from Numberphile about the Mandelbrot set, the fractal I presented above. After all, a fractal like this was not but a simple formula and a check for a bound on a sequence! I recommend all of you to watch that video. Simplifying it a lot, for each number you can create a sequence. After you do that, you check if the numbers in that sequence explode or remain small. After learning how to create the Mandelbrot set I put that knowledge to practice, making use of my ...

A gradient descent algorithm to optimally distribute points in a sphere

Image
In this first post I want to share with you guys a piece of code I wrote to "solve" a problem where geometry meets optimization. I say "solve" because I didn't actually do anything that fantastic regarding the actual problem I address, but rather developed a small tool to help visualize the geometrical part of the problem. Even so, I do believe that for the smaller cases my tool can solve the problem. The problem is along the lines of: define an energy function whose value depends on the positions of points in a sphere, and now try to minimize/maximize it (depending on a parameter). That is it. I used my coding skills to write an algorithm that solves this when the number of points is small, and that lets me see the creation of the solution: I create a random possible distribution of points and then let them adjust themselves to their desired positions, hopefully reaching the desired minimum/maximum. In here you can find the report I wrote for this, in English....