Just recently I came across ‘Kaprekar’s Constant‘ and maybe Mr Kaprekar had too much spare time… but still, it is quite interesting. The idea is to take a 4 digit number where the digits are not all the same then make the largest and smallest numbers possible with these digits, subtract the smaller from the larger then rinse and repeat with the result of the subtraction. e.g start with 4123 4321 – 1234 = 3087 8730 – 0378 = 8352 8532 – 2358 = 6174 7641 – 1467 = 6174 … repeats… and in fact all 4 digits ‘converge’ to Read More

# Dual Numbers and Differentiation. Part 2.

At the end of the previous post I had intended this posting to be an exploration of a recursive definition of Dual that will give an infinite (lazy) list of derivatives. However, there’s still a lot to play with using our simple data Dual a = Dual a a Let’s try a simple function of two variables… and at we have Now we can evaluate at using dual numbers with a subscript of x or y to ‘remember’ where it came from…i.e we want but really are the ‘same thing’. Notice that the coefficients of are the same as the Read More

# Dual Numbers and Differentiation. Part 1.

Overview Just recently I came across the interesting and, at first viewing, the rather abstract idea of dual numbers. I suppose they are no more or less abstract than other numbers… anyway the idea is similar to that of complex numbers where we have Dual numbers are quite similar, we have the dual number d as So now lets take this idea of a dual number and explore how adding, multiplying and dividing them might be defined. Addition and subtraction are simple – we just add the corresponding components – in much the same way that Read More

# Haskell, Vectors and Simple Mechanics – part 4.

I think this post will wrap up the series on Vectors and Simple Mechanics and we’ll look at Simple Harmonic Motion (SHM) and compare the numerical solutions to SHM using the naive step function from the previous post – aka the ‘Euler‘ step and the more accurate ‘Euler-Cromer‘ method. Here’s the Euler Step from last time.

1 2 3 4 5 |
step :: Accnf -> Float -> State -> State step f dt st@(t, r, v) = (t', r', v') where t' = t + dt r' = r ^+^ v ^* dt v' = v ^+^ f st ^* dt |

a very simple change to the above yields the ‘Euler-Cromer‘ step where the ‘new velocity‘ rather than the ‘old‘ is used to determine the ‘new‘ position.

1 2 3 4 5 |
ecStep :: Accnf -> Float -> State -> State ecStep f dt st@(t, r, v) = (t', r', v') where t' = t + dt r' = r ^+^ v' ^* dt v' = v ^+^ f st ^* dt |

These two functions have the same signature, Accnf -> Float -> State -> State which allows us to generalise a solution based Read More

# Haskell, Vectors and Simple Mechanics – part 3.

In this post we’ll continue the previous one about vectors and take a look at calculating the path of a projectile and rendering that path to the screen. Imagine a single particle in three dimensional space, we can characterise a state for it as its position and velocity at some instant in time. type State = (Time, Displacement, Velocity) From basic mechanics and Newtons laws we know that if no forces act on it then not a lot happens really! Time will increase and depending on your point of view, not much else will change. However, if some forces are acting then things become more Read More

# Haskell, Vectors and Simple Mechanics – part 2.

Here we will continue the previous ideas about vectors and take a look at basic rendering of Vectors – for which we will use the Haskell Gloss package at https://hackage.haskell.org/package/gloss Gloss claims that “Gloss hides the pain of drawing simple vector graphics behind a nice data type and a few display functions. Gloss uses OpenGL under the hood, but you won’t need to worry about any of that. Get something cool on the screen in under 10 minutes.” and to be fair I found it very easy to use but not without problems when installing. However I believe these problems Read More

# Haskell, Vectors and Simple Mechanics – part 1.

I like vectors! A long time ago my maths teacher introduced me to them and I just like the way three numbers (typically three) can express the notion of a position in space and combining these under different operations can produce other interesting properties. Just recently I found this paper Learn Physics by Programming in Haskell which gives a very interesting Haskell oriented discussion on vectors and their use in mechanical problems. Reading it has inspired me to create my own implementation of vectors, bore you all senseless with it and see where it goes. What I’d like to do Read More

# Text Classification 1.

What we’re trying to do is classify a piece of text, a review – it could be a review of film, a holiday, a car… anything. And we’ll claim that a review can either be positive e.g. ‘The bicycle was excellent and went very quickly uphill‘ . Or a review can be negative. e.g. ‘I was unhappy with the screwdriver, it was really poor and always turned the wrong way‘. The task is to take an arbitrary piece of text – a review – and decide which class it falls into – positive or negative. There are a number of Read More

# Haskell Parsers Part 3.

This is the third part of parsing in Haskell. The previous posts are available at Haskell Parsers Part 1 and Haskell Parsers Part 2. In part one we defined a parser as a type and derived a very simple parser, a parser of char. Part two extended the ideas and created monadic, functorial and applicative instances of the parser type. This allowed us to combine parsers monadically – using ‘do’ or in an applicative style – e.g. using ‘<*>’ etc. Which to use is often a stylistic choice. In this post we’ll look at creating a few extra functions to Read More

# Java Dockerfile.

Docker – what’s not to like?

Here’s a docker file that installs Java 8 over an Ubuntu base.

It also installs Git and Maven. It takes a little while to build but does provide a full JDK with git and maven.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Ubuntu with Java 8 installed. # Build image with: docker build -f Dockerfile_Java8 --rm=true -t=java8 # Run with: docker run -it java8 FROM ubuntu MAINTAINER Mike # Using && prevents intermediate containers being generated RUN apt-get update && \ apt-get upgrade -y && \ apt-get install -y software-properties-common && \ add-apt-repository ppa:webupd8team/java -y && \ apt-get update && \ echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections && \ apt-get install -y oracle-java8-installer && \ apt-get clean RUN apt-get install -y git && apt-get install -y maven ENV JAVA_HOME /usr/lib/jvm/java-8-oracle # Default command CMD ["bash"] |

To help remove unwanted images and stop any and all running containers here are two bash scripts.

1 2 3 4 5 |
#!/bin/bash # # removes all images docker rm -f $(docker ps -a -q) docker rmi $(docker images -q) |

1 2 3 4 |
#!/bin/bash # # force stop any running containers docker rm -f $(docker ps -a -q) |