All posts by Hannah Fry

Counting Crowds

Screen Shot 2013-07-30 at 13.55.23

This month has seen some dramatic developments in Egypt, with widespread protests, Morsi deposed from power – and more recently – outbreaks of violence across the country.

In the early days of the events, a nationwide protest was organised on the 30th of June to call for Morsi’s resignation. Huge numbers of people turned to the streets, with reports that up to 30 million people were involved – leading to the media claiming this was the largest protest in history.

Motivated by this headline, BBC Radio 4′s More or Less looked to investigate these numbers, and find out a bit more about how big crowd numbers are calculated. I was interviewed for a section on the show which you can find on iPlayer: http://www.bbc.co.uk/iplayer/episode/p01c2m3k/More_or_Less_Egypt_Biggest_Protest_in_History/  or download the podcast from the More or Less website:  http://www.bbc.co.uk/podcasts/series/moreorless

In addition, we filmed a short explanation of how to count crowds for the BBC website: : http://www.bbc.co.uk/news/world-23314873 and Ruth Alexander wrote a BBC magazine piece to accompany the work: http://www.bbc.co.uk/news/magazine-23312656

How to install Networkx on a mac

1) Download the link “networkx-1.6.zip” from here: http://networkx.lanl.gov/download/networkx/

2) Open your downloads folder and click on the file you just downloaded. Hopefully it should create a second, uncompressed file (also in your downloads folder) called “networkx-1.6″
3) Press Command + Space to open spotlight, and search for Terminal. Click on the top result and a white window with some black text should appear.
4) You now need to navigate your terminal to the downloads folder. It might be that all you need to do is type
“cd Downloads” BUT it depends on how your system is set up.
Essentially this terminal is a text version of the folders in finder.
-To see what folders are in your current location you type “ls”.
-To move into a new folder within your current directory (say a folder called “Hannah”) you type “cd Hannah”.
-To go up a folder you type “cd ..”
Move around until you find the downloads folder.
5) From within the downloads folder type “cd networkx-1.6″ into the terminal window
6) Also in the terminal window, type “sudo python setup.py install”
7) Type in your password (don’t worry that nothing appears on the screen)
8) Lots of text should come up. Have a glance through and see if an error comes up. If it does, send me a screen shot, if it doesn’t, go on to step 9.
9) Open up python and type in “import networkx” If no red text pops up then hurrah! You’ve installed everything correctly.
This might be helpful too:
http://networkx.github.io/documentation/latest/tutorial/tutorial.html

 

The return of the Tuesday Teaser

TuesdayTeaserI’ve missed puzzles. I’ve been too busy recently ‘working’ and ‘being a grown up’ to play with any. But this week CASA’s @frogo  has lined me up with a couple of absolute stonkers and they’ve re-ignited my desire to bring back the Tuesday Teasers. I know, right? Brilliant news. <Whoo! Yeah!>

And so.. without further ado. Here’s one from @frogo:

There was once an evil wizard with a weird and slightly unsavoury fetish for jewel encrusted goblins. Let’s call him Harry.

Harry liked to kidnap goblins and lock them up in his underground dungeon. As he did so, he magically implanted a precious gem in each goblin’s forehead – choosing either a ruby or an emerald each time, but refusing to tell the goblin which gem he had implanted. All they knew was that at least one of their fellow prisoners had a ruby, and at least one had an emerald. 

One day, Harry’s friend Germione – who was generally quite a decent soul – made him feel all guilty about being mean to the poor goblins, and persuaded him to give them a chance at freedom.

The next morning before breakfast – and every morning thereafter –  Harry would line up the goblins and ask all those with the rubys in their forehead to step forward – if exactly all of the goblins with a ruby stepped forward, while every goblin with an emerald remained in line, all the goblins would be freed and Harry would promise to never bother them again.

If they wanted, the goblins could all choose to remain in their line and not participate in Harry’s twisted little game & come to no harm.  But, if the incorrect number of rubied goblins stepped forward on any given morning, Harry would massacre every last goblin in a bloody, sickening and frenzied attack. And that Harry has a pretty nasty reputation.

Complete silence is required in the dungeon and the goblins are not allowed to talk to each other nor discuss strategies nor communicate in any way, there are no mirrors and each goblin has no way of knowing which gem they have in their heads without using bad-ass logic to work it out.

So.. how did the goblins win their freedom?

Deprivation, youth services and the London riots

Screen shot 2013-02-21 at 16.58.37

This video came about as a splinter project of some work I’ve been doing at UCL, trying to understand the 2011 London riots from a mathematical perspective.

While we were looking into the data behind the events, it was hard not to notice the striking links between the areas of the city that most rioters came from and the deprivation of those communities. This link was not the focus of our academic project, but I felt it was important not to just leave it as a one-line comment in a journal article. So I tried to investigate further by speaking to charities, youth workers and young people across the capital.

Once the riots had died down, many were very quick to demonise the groups of young people involved, but after taking the time to speak to young people from the poorest areas of the city I heard stories that shocked, appalled and surprised me – even as a Londoner myself. Perhaps the riots were not the result of innate criminal instincts lying dormant in certain groups of society, but rather a manifestation of deeper underlying problems in our cities.

I thought the voices of these young people and the often desperate situations they face deserved to be heard, and so obtained some funding through UCL’s Focus on the Positive to make this film. It turned out to be harder than I thought to get young people to open up on film but I hope that, regardless, this short piece might offer some insight into another way of looking at things.

I should point out that the links to deprivation and government cuts discussed here are not the subject or focus of the Scientific Reports paper ‘A mathematical model of the London Riots and their policing‘. The work described in this video has not been peer reviewed and does not represent the views of UCL or the other authors.

Who says complex networks aren’t funny?

Ok. Well maybe they aren’t. But I recently gave it a good bash at a stand-up comedy routine for UCL’s brightclub.

The night is a sort of academic variety night, where researchers from across the UK are given five minute slots to entertain a 600 seater theatre with a light-hearted look at their work. And here is my attempt – officially: looking at immunisation strategies on scale-free networks; unofficially: mucking about onstage with some good old fashioned ginger jokes.

WARNING. Contains some naughty words. And a hideous powerpoint fail at the start. Action begins from around 0:50.

TEDxUCL


UCL recently hosted their first ever TEDx event, and to my amazement (probably after seeing some seriously dodgy maths themed stand-up I did) they invited me to give a talk.

So if you have a spare ten minutes and fancy watching me ramble on about snooker balls, the solar system, leopards and how they all relate to complexity science then you’ve come to the right place.

Alternatively, use this link.. Or find out more about the event here.

EDIT: I’m totally mind bogglingly  amazed and thrilled-thrilled -thrilled that the lovely chaps at TED decided to promote my video as their talk of the day on the 4th August 2012.

Here is the link to the TED page: http://www.ted.com/talks/hannah_fry_is_life_really_that_complex.html

and to my bio on their site: http://www.ted.com/speakers/hannah_fry.html

Focus on the positive

focus

On Monday, UCL public engagement played host to a new type of event, funded by EPSRC and titled Focus On The Positive.

The idea is beautifully simple – six academics pitch a solution to a real world problem to an audience, who then vote for whichever cause they deem the most worthy. The winner then has £1000 to spend towards funding their solution.

While working on the London riots model with Toby Davies (which you can read more about in this article from Wired magazine) we realised how striking the links were between rioter involvement and deprivation and that the kids involved tended to come from the London boroughs worst hit by recent government cuts.

We thought the focus on the positive event would be the ideal opportunity to raise awareness about these links and the issues faced by young people in London from deprived backgrounds  - in particular given the recent cuts in funding to essential youth services. We decided our best chance at a lasting legacy from the funding would be to make a short film, and pitched our idea to the audience at the event on Monday.

I am incredibly honoured to say that our project won the funding on the night, and we are now in the processes of pre-production on our film, which I will be sure to post here when it’s ready.

In the meantime, the next Focus on the positive event will be held in Bloomsbury theatre on the 19th of June. You can find out more about the night here, buy tickets here, or contact Hilary Jackson more information

Tuesday Teaser 20th March

This week, a politics question:

Since WW2, how many British Prime Ministers first got the job without winning a general election?

As ever, feel free to gloat and post answers and guesses in the comments section. But only after the snake, mind. We don’t want to ruin it for everyone else.

Tuesday Teaser 13th March

Sorry for the lack of Tuesday Teaser last week, it’s because I was too busy living it up here. Back in full swing now, I promise. And to show you that I mean it, this week I’m pulling out one of my absolute favourite questions: 

There is only one country in the world whose name ends in the letter ‘k’. What is it? 

Please feel free to post answers and gloating remarks in the comments section below. And as ever, so as to keep up this delightful atmosphere of fun, I’ve included the silly snake to entertain you while you’re scrolling.

 

And the correct answer is…. Denmark.

The UK doesn’t count, you douches.

Tuesday Teaser 28th Feb

I promised I would try and avoid general knowledge teasers, but I just really like this question – so I hope that you’ll forgive me, just this once.

There are currently 7 Communist countries in the world. Name them.

And for a bonus point.. Which of these 7 are also democracies? 

As ever, feel free to gloat/post your answers after the snake..

 

 

Time for the answers, which you can read more about here.

The current communist one-party states are:

The Republic of Cuba (since 1948)

The Peoples Republic of China (since 1949)

The Socialist Republic of Vietnam (Communist since 1954, one-party state since 1976)

Democratic People’s Republic of Korea (since 1948)

Lao People’s Democratic Republic (since 1975)

And the democratic states with communist government:

Cyprus, (Progressive Party of Working People since February 2008),

Nepal (Unified Communist Party of Nepal (Maoist) since April 2008).

 

 

Tuesday Teaser 21st Feb

This week a word puzzle:

Which plural noun in the English language becomes singular when you add an s?

As ever, please feel free to gloat in the comments section below.. Answers after the snake

 

 

Bit of a controversial one this one. So far I have found 4 nouns which fit the above description. They are:

Princes/Princess

Bras/Brass

Millionaires/Millionaress

Billionaire/Billionairess

Any others?

 

Tuesday Teaser 14th Feb

This week’s teaser is about the London tube network:

Which is the only London Underground station whose name does not include any of the letters in the word ‘Mackerel’?

With this question, you either get it or you don’t, so it’s a bit tricky to allow solutions to be posted. Feel free to gloat in the comments section though if you know the answer.

UPDATE: Answer is now posted in the answers section below. Well done to F. Dans for getting it right.

Tuesday Teaser 7th Feb

This week’s teaser is a geography one..

Which state of the USA is the most:

i) Northerly

ii) Easterly

iii) Westerly

iv) Southerly. 

Feel free to post your solutions at the bottom of the page.  I’ve now included a lovely drawing of a little snake to amuse you while you’re scrolling.

UPDATE: The answers are now up, and can be found after the snake.

Time for the answers. First up, F. Dans was right that Attu is the most westerly island of Alaska, but it actually lies seven degrees west of the 180° longitude line, placing it in the Eastern Hemisphere. Hence, it is another member of the Aleutian Islands: Semisopochnoi, which can claim to be the most Easterly point of the US, being 14 minutes West of the 180° longitude line.

You can read more about it here and here on good old Wikipedia.

Regardless, that leaves Alaska as the most Northerly, Easterly, and Westerly state, with Hawaii rounding off the answers as the most Southerly.

Well done to Owen Boswarva for being the first one to post the right answer. You win my unbounded respect.

Welcome to Tuesday Teasers


For the past 8 years or so, I’ve been a member of a pub quiz team with some good friends of mine from my undergraduate days.

We’ve  had differing degrees of success (inversely correlated with the strength of our arch-rivals More Cool Mafia, as it goes) but in that time, I’ve come across some absolutely stonking good quiz questions – the type that most people know the answer to, but are still really tricky to get.

I thought it might be fun to post some of these questions here in a new regular feature (well, we’ll see how long it lasts) called Tuesday Teasers.

And so, to kick us off:

There are 10 parts of the human body, whose names in common usage are spelt with three letters each. What are they?

Slang words – like ‘bum’, ‘tum’ etc –  and medical terms don’t count. Don’t get all smart on me now.

UPDATE: The answers are now posted  at the bottom of the page. So you don’t get bored while you’re scrolling, I’ve included a picture of a lovely little snake.

And the answers are:

Arm, Leg, Toe, Eye, Ear, Lip, Hip, Rib, Gum and Jaw

Well done if you got it right!

Basic python syntax

A friend of mine has just started using Python, and asked me for a quick run down of the basic syntax. Given that, wherever possible, I try to live my life according to the above xkcd comic, I figured it would be worth my while typing out a short tutorial for her. Of course, there are thousands of similar (and probably far better) scripts available on the internet. But this seemed as good a place as any to use as a storage facility.

#Python has a few differerent types of built in objects.

#Integers
itg = 3

#Floats
flt = 3.

#If you divide an integer by another, you will get the floor of the answer
print 'Dividing integers'
print itg/2

#But you can change the object type:
print 'Converting to floats'
print float(itg)/2

#You can also convert to integers
print int(flt)

#And strings
print str(flt)

#Actually, when printing, there is an easier way to do this using the percentage symbol
print 'Using the percentage symbol %f' %flt

#Here %f means - find the float
#Likewise %s means - find the string
print 'Another example %s' %'Here'

#this is a good way to do print statements as you can use more than one simultaneously
print '.2d means to two significant figures like this %.2d or include a float %f' %(flt, flt)

#You can pull out bits of strings using the index labels, which start from zero.
name = 'Minnie'

#Then the command:
print name[0]
#Should print 'M'

#Working backwards, the labels start with -1:
print name[-1]
#Will print 'e'

#This labelling system works for lists too - another important python object
a = [] #An empty list
b = [1, 4, 'hello']#A list with three items.

#Then:
print b[-1]
#Should print 'hello'

#You can add items on to the end of lists like this:
b.append('new item')

#See?
print b

#Which is especially usefull when you're creating lists using loops
for n in name: #Will loop through all the letters in name
    a.append(n)#And add them to the list a as individual items

print a
#See?

#Another thing to note is that no end statements are used in python. Everything is based on indentation

#There are a few other things you can do with lists, because they are objects. Type a. and then press tab in the shell window to
#see the possible commands. they include a.pop, a.index and so on. 

#You can also put for loops inside lists like this - which is the same as above, just written on one line
a2 = [n for n in name]

#Lists are also super useful because they can contain objects themselves.

#To explain this, we need to use an example of a class
class Girl():
    def __init__(self, name, age):
        self.name  = name
        self.age = age

#The init function, within a class, initialises the instance of the class. For example
g = Girl('Minnie', 30)

#Makes a girl object with name minnie and age 30. Because of the self tag, you can now type
print g.name

#to get Minnie or
print g.age

#To get 30.

#Back to lists, you don't even need to give a handle to each instance of the girl class. Instead, you could make a list, 'girls'
girls = []
girls.append(g)
girls.append(Girl('Hannah', 27))

#Now the first item in the girls list takes the place of your g handle above:
print girls[0].name

#Same with the last item
print girls[-1].name

#Infact typing girls[1]. and then tab in the shell will give you the options which we defined in Girl: age and name.

#Geddit?

#There's one thing you need to be careful of with lists/python in general which is when setting things equal to each other.
list1 = [1]
list2 = list1

#Now, list2 isn't *actually* equal to list1, it just points to the same address on the computer.
print list2

#Now if you change list1:
list1[0] = 5

#List2 will automatically update itself
print list2

#You should be careful of this when coding as it can catch you out (it certainly has me in the past). Actually though,
#Although it initally seems stupid, it's actually a really useful (and quick) feature.

#Say you're working out a function f which depends on itself at the previous time step
#f(t+1) = f(f(t))

#You can code it like this:
#fnew = f**2 + 3 (or whatever the dependence is)
#Then when you're done, you can swap the pointers over
#fnew, f = f, fnew

#The other important built in object in python are dictionaries
D = {} #An empty dictionary

#You could use this instead of the girls object above
girlsD = {'Hannah':27, 'Minnie':30}

#Or to add a new item:
girlsD['Nenna'] = 26

#These are especailly useful when doing stuff with networks, but you should bear in mind that dictionaries DO NOT preserve order (lists do).

#Again type girlsD. and then tab in the shell to see the options available to you.

#That pretty much brings us to the end of what you can do without importing any other packages, so we'll move on to the most useful python
#package (as far as mathematical programming is concerned) Scipy.

#It's custom to import it with a shortcut handle
import scipy as sp

#Then anything that's in scipy can be called using the sp handle
sp.exp(5.)

#Actually, you can call the handle anything you like
import scipy as anything
anything.exp(5.)

#Or, not use one
import scipy
scipy.exp(5.)

#Or import everything, so you don't need to use a handle at all
from scipy import *
exp(5.)

#This is not that efficient though tbh, so probs best avoided.

#Now scipy has lots of cool stuff that you can see online, but the most important feature is the arrays. With lists you see, you can't add them
#Or do any arithmetic with them
a = [1,2,3]
b = [3,2,1]

#Now, if you typed a+b, it would produce an error.

#If a and b were arrays though:
a = sp.array([1,2,3])
b = sp.array([3,2,1])

#Now a+b would work.
print a+b

#and a*b gives element-wise multiplication
print a*b

#Geddit?

#a and b are 1d arrays, but you can also make vectors and matrices with sp.array:
row= sp.array([[1,2,3]])

#row is a 1x3 array
print sp.shape(row)

#likewise
col = sp.array([[1],[2],[3]])

#Is a 3x1
print sp.shape(col)

#Now, you could do matrix multiplication with these bad boys:
sp.dot(row, col)

#Which isn't the nicest notation, but it'll do.

#There are two other sp things that are quite useful:
xvals = sp.linspace(0, 100, 200)

#Auto generates an array from 0 to 100 with 200 points

#The other:
yvals = sp.arange(0, 10, 0.05)

#Lets you define your stepsize instead.

#Now, lets say we wanted to plot these, we'd need the plotting library
import pylab as plt

#Guess what the command is to plot something?
plt.plot(xvals, yvals)

#And if you want to save the figure?
plt.savefig('test')

#Phwoar. Got to love python.

PhD Thesis

I submitted my PhD thesis in January 2011, and was awarded the degree after my viva in March.

You can see the thesis in in full by clicking on the title page below: and if I do say so myself, it’s a delightful romp through the two-dimensional Navier-Stokes equations. I don’t want to give away the ending, but it should certainly please all you high Reynolds number two-phase flow fans out there.

 

Getting started with Python

This guide should give you a good overview of the packages you’ll need to download and install to start scientific programming using Python.

First, Python itself.  Python 2.7 is the latest version, and it works with the majority of toolkits you’ll need.

So, here it is:

http://python.org/download/releases/2.7.2/

You’ll also need the numerical extensions to python, NumPy (which lets you use arrays and matrices) and SciPy (which has lots of useful functions for integration, linear algebra, Fourier analysis and so on).

Luckily there are ready-made installers of both of these for both mac and windows:

Those for NumPy can be found here at
(For direct download of the 2.7 compatible version, you can use this link for mac or this one for windows)
.
And SciPy is  here (Or you can use the direct downloads of the 2.6 compatible version  for a mac or for windows)
.
If you want to do any plotting or visualisations you’ll need Matplotlib too. It’s very similar to matlab in its syntax and an overall delight to use. Find it here (Or directly download the 2.6 compatible version for windows and mac ).
.
NetworkX is another handy plugin for dealing with everything to do with networks. The easiest possible way to set it up is with another piece of software called easy install (documentation of which can be found here). I have a feeling (don’t quote me) that this comes pre-loaded on the mac,  but for windows, you can access the download page here or just download it directly.
.
Once that’s done, open up a terminal window (start>run>”cmd” for windows) and use the command ”easy_install networkx”. ‘Tis as simple as that.
.
Since my work is in a global dynamics project, I often have to produce plots overlaid on maps. For that matplotlib has a handy little toolbox called Basemap. The sourceforge page includes a lovely windows installer, unfortunately though, there’s no such thing for mac users. I have managed to set up basemap on my mac, but cannot for the life of me remember how. I know it took a long time to work it out, and I know I used this guide, which was about as useful as a chocolate teapot, but other than that you’re on your own I’m afraid.

1 + 1 = 2

A couple of days ago I saw a post on twitter about the proof that 1 + 1 = 2 by Alfred North Whitehead and Bertrand Russell. It appears in their 1910, three volume work, Principia Mathematica.
Despite the obvious importance of their work, I can definitely see the funny side. (Especially that it took them until page 379 to get this far, and they haven’t even defined arithmetic addition yet). But it does raise what I think is an interesting question of when to accept things as obvious, and when some things that should be obvious turn out to be quite counter intuitive.

One brilliant example was given in a talk by UCL’s own Jack Grahl in July. Consider the infinite sum:

 S = 1 - 1 + 1 - 1 + 1 - 1 + \cdots

and ask the question, what does S add up to? Obvious right?

First rewrite S, so that all the +1‘s and -1‘s are grouped together in pairs:
 S = (1 - 1) + (1 - 1) + (1 - 1) + \cdots
Each of these pairs cancel to zero:
 S = (0) + (0) + (0) + \cdots
So, the answer is
 S = 0

Done!. Although, not quite, because the choice of pairing could be done in a different way, leaving a +1 at the front:
 S = 1 + (-1 + 1) +(-1 + 1) + (-1 + 1) + \cdots
The terms in the brackets still cancel, so that:
 S = 1 + (0) + (0) + \cdots
leaving
 S = 1
eh??!?

And you can do this another way, and get a third answer. First writing S out twice:
 S = 1 - 1 + 1 - 1 + 1 - 1 + \cdots
 S = 0 + 1 - 1 + 1 - 1 + 1 - 1 + \cdots
(the second version is just the same, with a zero in the front to act as a kind of spacer).
Adding these two together. Inside each brackets is the top term, plus the bottom term:
 (S+S) = (1+0)+(- 1+1) + (1 - 1) + (-1+1) + \cdots
but again alot of this cancels, leaving:
 2 S = 1 + 0 + 0 + 0 + \cdots
Which can be solved to show
 S = \frac{1}{2}

So one seemingly simple problem has three mathematically legitimate results.

Counter-intuitively perhaps, given that it is a fraction made from the sum of an infinite series of integers, it is the third result of S = \frac{1}{2} which is generally accepted as ‘correct’. However, this answer seems positively sane when compared to the similar problem of determining the sum of the infinite series:
 1 + 2 + 3 + 4 + 5 + \cdots
Each term gets successively larger, so it should be obvious that if you continue adding infinitely many terms, you end up with an infinitely large sum, right? Wrong.

In a letter which the then unknown and relatively uneducated Ramanujan wrote to Hardy in 1913, he demonstrated his proof that the sum of the above series was.. (wait for it) -\frac{1}{12} .

As it turns out, this answer is absolutely correct, and can be verified by the Riemann Zeta function. Anecdote has it that that was when Hardy knew that Ramanujan was the real thing – if he was just barking mad and trying to pull a fast one, he would surely have come up with something much more sensible.

Mixing it up.


 

 

 

 

 

 

 

 

Following on from the storming success of my last puzzle post (I don’t like to brag, but I believe it had over 31 views..) I thought I’d pop up another. This one is potentially a little more involved – it took me a couple of days and some some fairly heavy maths before I was sure of the answer. However, knowing the solution, there is a simple logical argument involving only two physical concepts which I’m sure many people much cleverer than me could spot straight away.

The puzzle is as follows:

A vase is part filled with water, and part with oil, as in the first picture. The two are then mixed together, as in the second.

What happens to the pressure at the bottom of the vase if it is:
a) Cylindrical
b) Shaped as in the picture above.

And for the less fluid-dynamically minded, the two things you need to know to solve the problem are:

1) Pressure is force per unit area. In this case, pressure at a given point can be considered analogous to the weight of the fluid above that point.
2) Oil is lighter than water.

PS. All viscosity, intertia, vorticity, surface tension, compressibility, imiscibility, blah blah blah effects are ignored. Don’t get all smart on me now.

A little puzzle: the solution

I ruddy love this puzzle. (Find the original post here). Have had great fun tormenting friends and colleagues with it over the last few days.

To begin with, we – like Jones – are looking for four distinct numbers which add up to 18.

1,2,3,4
1,2,3,5
1,2,3,6…

and so on.

But Jones has an extra bit of information to us, in that he knows the door number of Prof White’s house.

The key thing to notice however, is that this still isn’t enough for Jones to determine a unique solution to the problem. Therefore, Prof White’s house number must be one which you can factorise into four numbers, which add up to less than 18, in more than one way.

This knocks off almost every possible house number apart from 60, for which there are two possible solutions which fit the bill:

1,2,5,6
1,3,4,5

And 120, for which there are three:

1,4,5,6
1,3,5,8
2,3,4,5

Now, Jones asks how many children are in the Black (smallest) family, and based on Prof. White’s answer he knows the solution to the problem.

In that case, Prof. White must have answered “2″, else Jones would still not have enough information (regardless of whether the house number was 60 or 120).  So the answer must be 2,3,4,5!

Phwoar. Don’t ya just love a good bit of maths?

A little puzzle

Recently I’ve been getting into the habit of reading maths and logic puzzle books on my tube journey home. I get some slightly weird looks from my fellow commuters when they see me working out matchstick configurations on the back of Tesco receipts, but overall I find it passes the time wonderfully well.

This one I found last night in Martin Gardners book “My best mathematical and logic puzzles”. It took me a little while to solve, but I particularly liked it because (to me at least) it initially seems as though there simply isn’t enough information in the question.

Anyway, here it is. I’ll post the solution in a few days.

“I hear some children playing in the backyard” said Jones, a graduate student in mathematics. “Are they all yours?”

“Heavens no,” exclaimed Professor White, the eminent number theorist. “My children are playing with friends from three other families in the neighborhood, although our family happens to be the largest. The Browns have a smaller number of children, the Greens have a still smaller number, and the Blacks the smallest of all.”

“How many children are there altogether?” asked Jones.

“Let me put it this way,” said White. “There are fewer than 18 children, and the product of the numbers in the four families happens to be my house number which you saw when you arrived.”

Jones took a notebook and a pencil from his pocket and started scribbling. A moment later he looked up and said, “I need more information. Is there more than one child in the Black family?”

As soon as White replied, Jones smiled and correctly stated the number of children in each family.

Knowing the house number and whether the Blacks had more than one child, Jones found the problem trivial. It is a remarkable fact, however, that the number of children in each family can be determined solely on the basis of the information given above..

 

PS. Solution is now up.

Getting started: the basic bits of kit

This guide should give you a good overview of the packages you’ll need to download and install to start scientific programming using Python.

First, Python itself.  There are later versions than the 2.6 which I use but:

a) they can have some compatibility issues when installing other toolkits

b) unless you really know what you’re doing, 2.6 is capable of basically everything you’ll ever want to do.

So, here it is:

http://www.python.org/download/releases/2.6.6/

You’ll also need the numerical extensions to python, NumPy (which lets you use arrays and matrices) and SciPy (which has lots of useful functions for integration, linear algebra, Fourier analysis and so on).

Luckily there are ready-made installers of both of these for both mac and windows:

Those for NumPy can be found here at
(For direct download of the 2.6 compatible version, you can use this link for mac or this one for windows)
.
And SciPy is here (Or you can use the direct downloads of the 2.6 compatible version  for a mac or for windows)
.
If you want to do any plotting or visualisations you’ll need Matplotlib too. It’s very similar to matlab in its syntax and an overall delight to use. Find it here (Or directly download the 2.6 compatible version for windows and mac ).
.
NetworkX is another handy plugin for dealing with everything to do with networks. The easiest possible way to set it up is with another piece of software called easy install (documentation of which can be found here). I have a feeling (don’t quote me) that this comes pre-loaded on the mac,  but for windows, you can access the download page here or just download it directly.
.
Once that’s done, open up a terminal window (start>run>”cmd” for windows) and use the command ”easy_install networkx”. ‘Tis as simple as that.
.
Since my work is in a global dynamics project, I often have to produce plots overlaid on maps. For that matplotlib has a handy little toolbox called Basemap. The sourceforge page includes a lovely windows installer, unfortunately though, there’s no such thing for mac users. I have managed to set up basemap on my mac, but cannot for the life of me remember how. I know it took a long time to work it out, and I know I used this guide, which was about as useful as a chocolate teapot, but other than that you’re on your own I’m afraid.

Just another Python blog.

I’m Hannah Fry, I’ve been lecturing at UCL for 5 years, trained as a mathematician and now working as a fully fledged academic on a global dynamics and complexity project.

Now that I’ve finished my PhD, I thought it was time to grow up and learn a proper  programming language. Python, in its similarities to matlab and easy-read syntax seemed a pretty friendly place to start.

I’ve done a fair amount  of programming before and know how to structure a for loop, but any time I ran into trouble I’ve found it a real headache to find the answers to my problems.

The python forums (like stackoverflow) can be useful, but more often than not it’s an absolute blummin’ nightmare trying to decipher the experts replies. Plus, you have to occasionally deal with users using handles like ’ICanHazSeks’ (who lets face it, probably cannot)  writing things like “I really fail to see how that error message is hard to understand”.

All I want is a clear answer to a clear question. For example,

“When using matplotlib, how do you change the textsize of the numbers (or ticks) along an axis?”

Answer

You have to loop through each individual label.

If ‘ax’ is your axis handle, and ‘size’ the desired textsize, you can use (one for each axis):

[tick.label1.set_fontsize(size) for tick in ax.xaxis.get_major_ticks()]

[tick.label1.set_fontsize(size) for tick in ax.yaxis.get_major_ticks()]

It took me 2 hours of googling to work that out.

So that’s what this blog is for, a record of all the difficulties I’ve encountered in learning how to write in python, and a library of my (admittedly inelegant) solutions.