Number theory determinant and SymPy

Let σ(n) be the sum of the positive divisors of n and let gcd(a, b) be the greatest common divisor of a and b.

Form an n by n matrix M whose (i, j) entry is σ(gcd(i, j)). Then the determinant of M is n!.

The following code shows that the theorem is true for a few values of n and shows how to do some common number theory calculations in SymPy.

from sympy import gcd, divisors, Matrix, factorial

def f(i, j):
    return sum( divisors( gcd(i, j) ) )

def test(n):
    r = range(1, n+1)
    M = Matrix( [ [f(i, j) for j in r] for i in r] )
    return M.det() - factorial(n)

for n in range(1, 11):
    print( test(n) )

As expected, the test function returns zeros.

If we replace the function σ above by τ where τ(n) is the number of positive divisors of n, the corresponding determinant is 1. To test this, replace sum by len in the definition of f and replace factorial(n) by 1.

In case you’re curious, both results are special cases of the following more general theorem. I don’t know whose theorem it is. I found it here.

For any arithmetic function f(m), let g(m) be defined for all positive integers m by

g(m) = \sum_{d \,\mid \,m} \mu(d) f\left(\frac{m}{d}\right)

Let M be the square matrix of order n with ij element f(gcd(i, j)). Then

\det M = \prod_i^n g(j)

Here μ is the Möbius function. The two special cases above correspond to g(m) = m and g(m) = 1.

I before E?

How well does the spelling rule “i before e except after c” hold? I searched the 5,000 most common English words (from here) to see.

70% of the words containing ‘ie” or “ei” follow the rule.

If you weigh the word counts by word frequency, the rule only holds 54% of the time.

There’s a longer version of the rule that adds “or when sounding as ‘a’ as in neighbor or weigh.”  This version holds for 79% of the words in my list. And when weighted by frequency, the rule holds 85% of the time.

Update: Here’s an even more accurate version from Merriam-Webster:

i before e,
except after c,
or when sounded as a,
as in ‘neighbor’ and ‘weigh’,
or when it appears in comparatives and superlatives like ‘fancier’,
or when the c sounds as sh as in ‘glacier’,
or when the vowel sounds like ee as in ‘seize’,
or i as in ‘height’,
or when it shows up in compound words such as ‘albeit’,
or when it shows up in –ing inflections of verbs that end in e, like queueing,
or occasionally in technical words that have a strong etymological link to their parent languages such as ‘cuneiform’ and ‘caffeine’,
and in numerous other random exceptions such as ‘science’, ‘forfeit’, and ‘weird.’

 

“I got the easy ones wrong”

This morning my daughter told me that she did well on a spelling test, but she got the easiest words wrong. Of course that’s not exactly true. The words that are hardest for her to spell are the ones she in fact did not spell correctly. She probably meant that she missed the words she felt should have been easy. Maybe they were short words. Children can be intimidated by long words, even though long words tend to be more regular and thus easier to spell.

Our perceptions of what is easy are often upside-down. We feel that some things should be easy even though our experience tells us otherwise.

Sometimes the trickiest parts of a subject come first, but we think that because they come first they should be easy. For example, force-body diagrams come at the beginning of an introductory physics class, but they can be hard to get right. Newton didn’t always get them right. More advanced physics, say celestial mechanics, is in some ways easier, or at least less error-prone.

“Elementary” and “easy” are not the same. Sometimes they’re opposites. Getting off the ground, so to speak, may be a lot harder than flying.

Mathematics and one-handed pottery

From Michael Atiyah’s essay Trends in Pure Mathematics.

In any given field of mathematics there are always some very fine points which present great technical challenges to the specialist but not are usually of interest to the general mathematician. To make an analogy, if you want to buy some hand-made pottery you are usually not interested in whether the potter used two hands or only one, though the potter would no doubt take great pride in his single-handed effort. In general the mathematical results that have the widest impact are not technically the most difficult.

This essay appears in the first volume of Atiyah’s collected works.

See his comments on this passage in this recent interview.

Hard-working lazy people

“It was a favorite theme of C. S. Lewis that only lazy people work hard. By lazily abdicating the essential work of deciding and directing, establishing values and setting goals, other people do it for us; then we find ourselves frantically, at the last minute, trying to satisfy a half dozen demands on our time, none of which is essential to our vocation, to stave off the disaster of disappointing someone.” — Eugene Peterson

 

Extract audio from video

If I bookmark a video, it may be months before I watch it, if ever, but audio I get around to more quickly. I have more time for listening to audio than watching video, so I’ve started going through my video backlog by downloading the files and extracting the audio to put on my iPod.

I download the video with Video DownloadHelper, a Firefox plug-in, then extract the audio to an MP3 file using Sound Converter. (I assume DownloadHelper works cross-platform, but Sound Converter only runs on Linux.)

DownloadHelper has an option to download and convert in one step, but I haven’t gotten that to work. Also, you can use ffmpeg or avconv to convert video to audio, but they have failed more often than not in my experience. So far Sound Converter has always worked for me.

Update: TubGet will let you enter a video URL and download an MP3 file. I seems to work OK, and is faster than downloading video. There’s also ListenToYouTube but apparently it only works with YouTube while TubGet also works with Vimeo and others.

Reducing development friction

Diomidis Spinellis gave an insightful list of ways to reduce software development friction in the Tools of the Trade podcast episode The Frictionless Development Environment Scorecard.

The first item on his list grabbed my attention:

Are my personal settings and preferences consistent on all the computers I’m using? Are they stored under version control? Can I install them on a new computer using a single command?

Listening to the podcast provoked me to finally sync my .emacs files on all my computers so that I now have the exact same file on all computers, maintained under version control. (Xah Lee gave me some sample code for creating the branching logic I needed for a few differences between Windows and Linux.)

Here is a small sample of questions from the podcast.

  • Are my files getting backed up? Is the backup tested, accessible, off site, in multiple media, with regularly retained copies?
  • Can I use the same editor for all my code and documentation editing tasks?
  • Can I get context-sensitive help and code completion?
  • Can I search recursively down a directory tree? Ignoring case? Only in a subset of files? With a regular expression?
  • Can I open a shell from the graphical file explorer and vice versa?
  • Can I quickly build the application I’m working on after a change? Can I test the application with a single command?
  • Can I automatically check my code for common or tricky errors? Are these checks run by default? Are they clean?
  • Does my application log its actions?
  • Is documentation for the tools and APIs I use readily available? Is it hyperlinked? Available offline?

The last question from the podcast summarizes the whole list:

Do I regularly evaluate my development environment to pinpoint and eliminate the sources of friction? Do I help my colleagues do the same?