Wednesday, June 29, 2016

Happy Tau Day, 2016!

Happy Tau Day everyone! I've talked about tau before, but if you're new (or don't remember), tau is what pi should have been: instead of the ratio of the circumference of a circle to the diameter, tau is the ratio of the circumference to the radius, which is much more natural given circles are defined by their radii. Mathematically, \(\tau=C/r\). Numerically, the first couple of digits are 6.283185… (hence why June 28 is Tau Day!).

For an excellent introduction to tau and why it's a better choice than pi, look no further than The Tau Manifesto by Michael Hartl. He explains much better, and in much more detail than I can here, why tau makes much sense mathematically, and, perhaps even more importantly, how it simplifies learning more advanced math like trigonometry.

I've talked about tau before, so today I'm just going to briefly explain an experiment you can do to estimate tau. This uses a problem known as Buffon's needle, originally posed in the 18th century. The problem can be stated as:
Suppose we have a floor made of parallel strips of wood, each the same width t, and we drop a needle of length l onto the floor. What is the probability that the needle will lie across a line between two strips?
If we assume that the needle is shorter than the distance between two lines (not necessary, but it simplifies things), then the answer (given in full on the Wikipedia page linked above) is simple: the probability P is given by \(P=\frac{2l}{t\pi}\). This formula can be rearranged to find pi: \(\pi=\frac{2l}{t P}\). Remembering that \(\pi=\frac{1}{2}\tau\), we get \(\tau=\frac{4l}{t P}\).

If you drop n needles (or toothpicks, or matchsticks, etc.), and h of them cross a line, then the probability P can be approximated by \(h/n\). This mean you can approximate tau by:
\[\tau\approx \frac{4l\,n}{t\,h}\]The more needles you drop, the better your estimate is likely to be (barring some sort of systematic bias in how you're dropping the needles).

Anyway, that's it for today! Have a great Tau Day, and do let me know in the comments if you actually try this experiment!

Sunday, June 19, 2016

Flag Speed-vectorization: Sadiya

Today I've got another speed-vectorization video, this time for the tiny nation of Sadiya in the region of Assam. Historically this kingdom lasted from 1187 till about 1524 when it was absorbed by a neighbor. (The flag shown on Wikipedia looks different from Sadiya's flag in Europa Universalis IV, but I think the one on Wikipedia is actually the coat of arms for the Sutiyah dynasty that ruled Sadiya.)

Sadiya's location and extent in 1444.
While trying to upload it, I discovered Blogger has a limit of 100 MB on uploaded videos, which the original version of the video exceeded. One reason I haven't utilized much video content on this blog over the years has been this hazy awareness in the back of my mind that such a limit existed, and since I think I'd like to have a bit more video content in the future I decided to just go ahead and put videos up on YouTube, then embed them here, so I don't need to worry about trying to keep them small. (Although, amusingly enough, I re-did the video after noticing an error in it and the new version is back below 100 MB.)


Hope you enjoyed it! I've got another such video that I took footage for and just need to edit, and I may do a few more as they're a lot of fun to do. But definitely let me know if you found it interesting! A hui hou!

Sunday, June 12, 2016

Problem Solving with Python

Last year I took over the task of finding short messages for the sign out in front of my church building. It's not a particularly frequent or regular task; I usually try to find something new to put up approximately every 2–4 weeks.

Now:
  1. The church building is only about 3/4 mile from my house.
  2. In an effort to get more exercise I prefer to walk there to change the sign (usually in the cool of the evening) rather than driving.
  3. This makes carrying the box containing all the letters impractical, as it's just a bit too large and unwieldy to carry that far by hand.
  4. However, carrying only the letters required (usually a few dozen) is pretty easy.
This all leads to me needing to figure out how many of each letter I'm going to need to take with me every few weeks. This week I finally got tired of counting manually and dashed off a quick script I can use in the future. It's pretty simple, so I figured I'd post here for anyone interested. I've tried to comment it pretty thoroughly (a lot more thoroughly than I normally do, if I'm honest).

#!/usr/bin/python3

# Script to print the number of each letter in a given string.
from string import ascii_uppercase, ascii_letters
import argparse

# Use the argparse module to handle command line arguments.
parser = argparse.ArgumentParser(description='Count letters in a string.')

# Add an argument for the string to be read and counted.
parser.add_argument('count_string', type=str, action='store',
                    help='The string to count the letters in.')
args = parser.parse_args()

# Take the command line argument and convert it to an uppercase
# string.
input_string = args.count_string.upper()

# Create a variable to track the number of letters:
total = 0
for character in input_string:
    # Check if each character is a letter, and update the total if so.
    if character in ascii_letters:
        total += 1

# Check against each uppercase character:
for char1 in ascii_uppercase:
    # Create a variable to store the number of this particular character.
    num = 0
    for char2 in input_string:
        # Check each character in the input against the given character in the
        # list. If they match, increment the variable by 1.
        if char1 == char2:
            num += 1

    if num > 0: # Don't bother reporting zero occurrences.
        # Print the results for the character.
        print("# of {letter}'s: {number}".format(number=num, letter=char1))

print('------')
print('Total number of letters: {0}'.format(total))

exit(0)


A typical output looks like this:

$ char_count 'A simple string to test the new script!'
# of A's: 1
# of C's: 1
# of E's: 4
# of G's: 1
# of H's: 1
# of I's: 3
# of L's: 1
# of M's: 1
# of N's: 2
# of O's: 1
# of P's: 2
# of R's: 2
# of S's: 4
# of T's: 6
# of W's: 1
------
Total number of letters: 31

For the script syntax highlighting I'm trying out this Javascript libary I found called SyntaxHighlighter. It unfortunately doesn't show up while previewing the post, but seems to work well enough in the finished posted product.