Posts

Showing posts with the label computing

AI from scratch

Image
My first neural net Like everybody else I've been playing a lot with ChatGPT recently and I'm gobsmacked by how good it is!  This has led me to start researching how exactly these things work.  In particular I wanted to understand how neural nets - a core component of the technology - are trained. Running a neural net is simple.  The neural net consists of layers of nodes connected by weights and biases.  The first layer is an input layer and setting the activation levels of each node in that layer causes the next layer to adopt values determined by the weights and biases.  The second layer determines the activation levels in the next layer and so on until the final layer, which is interpreted as the output. 

Creating Cayley Graphs from Group Presentations

Image
Definitions: A Cayley graph is a directed graph showing how to get from any element of a group to any other using only a finite number of "generator" elements.  For example, in the group $\mathbb{Z}_2\times\mathbb{Z}_2$ we could use the generators $a = (1,0)$ and $b = (0,1)$ and we'd end up with a graph that looks like a square with arrows going round the edges. A Group presentation defines a group by specifying generators and relations between them.  For example the presentation $\langle a,b \vert a^2=1,b^2=1,aba^{-1}b^{-1}=1\rangle$ specifies a group.  In fact the group it specifies is isomorphic to  $\mathbb{Z}_2\times\mathbb{Z}_2$ and we can prove this using the facts that $a$ and $b$ commute and both have order 2.  In this case it's easy to see what group we have but in general it's a bit more difficult.  A useful first step would be to be able to draw the Cayley graph automatically. Question: Is it possible to automatically generate a Cayley graph from a g

mpool

Image
An overhead free aligned memory allocator (Well, almost overhead free)      I designed this memory allocator for a Linux device driver I wrote 20 years ago .  It has the property that if you ask for 64 bytes you get a 64 byte aligned block; if you ask for 128, you get a 128 byte aligned block; and so on.  The other nice thing about it is that it automatically defragments, which is to say that if, for example, a 128 byte aligned 128 byte block is free then it can be allocated as a 128 byte block, even if it one or both halves have previously been allocated as 64 byte blocks and later freed. The key to the design is the data structure.  Each free block has a 64 byte header consisting of 16 pointers (I wrote this for a 32-bit architecture, although it could easily be ported).  For this reason 64 bytes is the smallest block size that can be allocated.  The pointers are all either NULL or point to another free block of the same size, forming a 16-ary tree of equal-sized free blocks.  At th

Lord Greyscale

Image
  Posting a pretty picture of some greyscale in which the fractal nature of it is clearly visible.  Black rectangles represent zeros and white rectangles represent ones. Read it as an array of columns going from left to right.  In each column exactly one bit changes relative to the previous, making it an ideal scheme for a head to determine its position. An $n$-bit greyscale sequence has $2^n$ members, and the last member always differs from the first in just 1 bit position.  This makes greyscale ideal for determining position around a cylinder. Here's the code: #!/usr/bin/env python3 def __greyscale_fwd(nbits, preset_bits): if nbits == 1: yield 0 + preset_bits yield 1 + preset_bits else : for x in __greyscale_fwd(nbits-1, preset_bits): yield x for x in __greyscale_rev(nbits-1, preset_bits | (1 << (nbits-1))): yield x def __greyscale_rev(nbits, preset_bits): if nbits == 1: yield 1 + preset_bits yi

Many Worlds Quantum Tic Tac Toe

Image
MWQT$^3$ I've just discovered Quantum Tic Tac Toe .  This is a brilliant game designed to give players an intuition for Quantum Mechanics without requiring them to learn any complicated mathematics. Superposition Instead of playing in just one square each player gets to play in two squares at once.   Here player X has played in the 1st & 2nd squares on their first move, the 3rd & 6th squares on their 2nd move, and the 8th & 9th squares on their 3rd move.  Similarly player O has played on two squares each move, and the marks are subscripted to indicate when in the game play they were laid down. Entanglement Ultimately each pair of marks will be replaced with a single mark and the board will look like ordinary Tic Tac Toe - which is how we are able to determine a winner.  But the final location of, say, $O_2$ may need to depend on the final location of, say, $X_1$ if we are to avoid ending up with squares with multiple marks in them.  In this case the moves $X_1$ and $O_2

Finite State Machine Wizard

Image
    Here's my free to use, no attribute required FSM Wizard .  This one generates C++ code.  I'm not a fan of C++ but it happens to be the language I'm using at the moment.  However, it can easily be ported to other languages if desired. The FSM Wizard works like this 1. Write a CSV file with format initialState,Event,NewState,ActionFn,OptionalComment .  The code includes an example called MyFsm.csv RedGateLow,TrainPassed,RedGateLow,CheckLine, RedGateLow,LineClear,RedGateLow,RaiseGate, RedGateLow,GateRaised,Green,SetGreen, Green,TrainDetected,Yellow,StartTimerYellowRed Yellow,TimerExpired,RedGateHigh,StartTimerRedGate RedGateHigh,TimerExpired,RedGateHigh,LowerGate RedGateHigh,GateLowered,RedGateLow,  2. Run the wizard: ./fsmGen.py MyFsm.csv   3. View the output PNG file and optionally edit the autogenerated C++ files

Gormanian calendar shell utility

Image
CAL++(1)                    BSD General Commands Manual                   CAL++(1) NAME      cal++ — displays The Gormanian Calendar SYNOPSIS     cal++ -h     cal++ [-y] [[month] year] DESCRIPTION The cal++ utility displays a simple Gormanian calendar. The options are as follows: -h       help -y      [DEPRECATED] Make Gormanian year match Gregorian year. This is deprecated as it causes Dave Gorman's birthday to inconveniently have different representations in the two calendars. However, the option is included for compatibility with some online calculators. A single parameter specifies the year (1–9999) to be displayed; note the year must be fully specified: “cal++ 89” will not display a calendar for 1989. Two parameters denote the month and year; the month is either a number between 1 and 14 - where 14 represents the Intermission.  The current date is highlighed if it appears in the output. A year starts on March 1. SEE ALSO cal(1) HISTORY The Gormanian calendar

How To Make ASCII Diagrams Beautifuller

Image
I've discovered an excellent tool in asciiflow.com .  The website makes it really easy to create ASCII box diagrams like this They put these things in fruit machines you know! This is ideal for source code banners, which I think should contain helpful documentation - but most programmers think it's a good place for the COPYRIGHT information and nothing else. But wait! we can make it beautifuller... and easier to read... by replacing some of the ASCII characters with ones available in UTF-8: There!  Isn't that better?  (Although some purists may object to non-ASCII characters in your code base.) SOURCE CODE: #!/usr/bin/python2 # coding: utf-8 # + gets converted in different ways depending on it's 4 neighbours # # . N . { nsew(N,S,E,W) has bit 3 set if N in "+|<>" # W + E { nsew(N,S,E,W) has bit 2 set if S in "+|<>" # . S . { nsew(N,S,E,W) has bit 1 set if W in "+-^v" # { nsew(N,S,E,W) has bit 0 set if E in

A Patent Protection Racket

Image
From: Learn To Speak Mafia About 15 years ago I was working for a small firm making telecoms equipment , and developing from scratch a Voice over IP box.  It was a very simple device to convert SIP internet calls to local analogue POTS lines (Plain Old Telephony Service) and there were just two of us working on the project: Mike the hardware engineer, and me, for the software. The box had a microprocessor running Linux (including a massively hacked version of Linphone for the SIP stack), and it had a DSP to support the codecs (short for Codify/Decodify).  It was quite a fun project (*) . A word about codecs and SIP:  SIP stands for Session Initiation Protocol (**) and is an internet standard allowing two internet peers to start a phone call.  One of the main tasks SIP has to perform is to co-ordinate on which codec to use.  One peer may support GSM, G.729, and G.711, and another may support G.726, GSM, and Speex - in which case the two peers would have to agree to use their on

The Debugger's Theorem

Image
This reminder has been on a wall in my house for ~15 years As a programmer for 22 years I've fixed thousands of bugs, and created many times more.  Very often it appeared that the problem I was trying to fix had multiple independent causes.  However I have found - almost invariably - that if you dig long enough you'll find a single cause for all the problems you are seeing.  In fact the moment you hit on the right theory is often really obvious because it suddenly explains a whole bunch of other things that have been going wrong!  But, I wondered, can this observation be proven mathematically?  It turns out it can! The Debugger's Theorem If a system that usually works currently isn't working, then it is more likely than not that there's just one thing causing all the observed problems. Proof Let $P_0$ be the probability that the system has no problems, $P_1$ be the probability that one independent problem has occurred, $P_{2+}$ the probability that two

Sunflowers and traffic lights

Image
It occurred to me the other day while looking at a set of LED traffic lights that they could be improved quite easily.  The layout of the LEDs in all the ones I've seen seems to favour some radial directions over others and/or result in a higher concentration of bulbs at some radii than at others. My solution involves placing LEDs a increasing distances from the centre and as each LED is placed rotating by the golden angle .  This ensures that no radial direction is preferred over any other.  In addition to this, setting $r_n = \sqrt{n}$ ensures that each bulb covers a roughly similar area. By "area covered" I am referring to the portion of space which is closer to that bulb than to any other.  This is the Voronoi cell generated by a point belonging to a set of points.  The picture above shows the Voronoi cells generated by a set of LEDs placed in the manner described, and the code below shows how I obtained it.  I think it would make a pretty stain glass

Time AND date sundial

Image
Photo taken by me at 18:00 BST May 20, 2018 I was out and about in Cambridge and I saw a modern sundial on the side of a building in Tennis Court road .  It occurred to me then that I should be able to build a sundial which tells you both the time - in GMT - and the date.  (Although you do need to know whether it is before or after the summer solstice!).  In fact all you need is to know one out of compass bearing, time, date and you should be able to work out the other two! To run it you need to do the following copy the text into sundial.py and chmod +x sundial.py install pre-requisite packages: sudo apt install python-numpy python-matplotlib run it:  ./sundial.py The result is a printout like the one shown.  If you want to adapt the picture for your locale just edit the parameters passed to plot_fixed_lat_long() . When I printed out my first sundial I was surprised to discover that the trajectory of the shadow is straight on the equinoxes.  After a day of pondering