Pages

Sunday, January 26, 2020

Sknowflakes

For a moment it seemed like things were warming up here in Michigan, but we're back down to freezing again, and it's got ice crystals on my mind. Specifically, I was curious if I could model snowflake growth programmatically. I found this paper, which describes a simple technique from Stephen Wolfram (of Mathematica fame):

  1. On a hexagonal grid, mark one cell as "frozen" (1) and all other cells as "unfrozen" (0)
  2. Any unfrozen cell with exactly one frozen neighbor gets marked as frozen
  3. Repeat until the grid is full
I found the simplicity of this appealing, and I thought I'd try it before attempting the more complicated method the paper describes. Unfortunately, even this simple idea caused me no end of problems, due to the hexagonal grid.

We want to assign coordinates to each hex in the grid, which isn't too difficult:


The trouble comes when we want to find the neighbors of a particular cell. Depending on the row, the relationship changes. If you look at the cells around (2, 7) the one to the upper right is (+1, +1), but for (7, 4) the upper right is (0, +1).

Aside from that difficulty, I also misremembered the sides of a 30-60-90 triangle, but once Sally gave me a gentle reminder, I was all set!


I had hoped I could get different patterns by changing the initial set of filled cells, but it turned out the same shapes would establish themselves pretty quickly. Now that I have the hexagonal grid code sorted out, maybe I can come back to this in the future and try the more complicated models.

Saturday, January 18, 2020

Ocular Observatory


While walking Lorna one morning this week, I noticed the Sun and Moon in the sky at the same time, and I started wondering what sort of information I could get about the Solar System based only on some simple measurements of their positions. The picture I had in my head was this:
Earth-Sun-Moon System (Not to scale)
As long as they're both visible, we can measure the angle between the Sun and the Moon with a compass. What occurred to me though is that we can also measure the angle between the Sun and the Earth by looking at the phase of the Moon:

Based on Wulffnet from Wikipedia
We can translate that length into an angle with
With two angles, we know the third, and we can relate them to the sides using the Law of Sines (and some other trig identities):

It also occurred to me that, because the Sun and Moon have similar angular sizes, which allows us to get solar eclipses, we can relate these distances to the relative sizes.
where r is the radius of the Sun/Moon.

Unfortunately, once we try to put some numbers in here, the my idea of "primitive astronomy by eye" breaks down: The actual ratio of distances for the Moon and Sun is about 0.0026, which means you'd need to get the angles within about a tenth of a degree. Thinking about this type of analysis makes me respect people like Galileo and Copernicus all the more.

Sunday, January 5, 2020

Mathematics: The Shuffling

Recently, my brother-in-law Alex has been into the card game Magic: The Gathering. Over the holidays he had the whole family playing, and the question came up of how many times a deck needs to be shuffled to be "random". For an ordinary 52-card deck, the oft-quoted result of 7 was originally given in a New York Times article from 1990. Doing a bit more research, I found a paper following up on that article with a few more details. I wasn't entirely satisfied with that, both because the shuffles described didn't quite match my techniques, and because I found the probability calculations difficult to follow in my first jet-lagged, and now sickened state, so I wrote some code to simulate a series of shuffles. The two types of shuffle I usually do are:

Riffle: Split the deck in two, and interleave the piles, roughly alternating. I modeled this as a split using a Gaussian distribution around the center, and then picking from the two piles with 50/50 chance.

Overhand: Holding the deck in one hand, move packs of cards to the other. I modeled this as splitting the deck into a set number of unequal packs, and reversing the order of those packs.

Now we have to figure out the quality of these shuffles. I've talked about one way to measure randomness before, but that doesn't really apply here, since all the cards (in a standard deck anyway) are unique. For a deck labeled 1-52, the two quality measures I came up with are: the total number of cards in sequences, and the distance of each card from its original position. Over a series of shuffles, here are the means and standard deviations for those two measures on a set of 5000 decks:


For both distance measures, the riffle shuffle appears to reach a stable value in fewer shuffles. Breaking up runs is clearly not a strong suit of the overhand shuffle, but it moves cards from their original position pretty quickly. As far as the minimum number of shuffles though, it appears 7 is a reliable choice, but you could get away with fewer on average.