-
Thomae's function
Thomae’s function (a.k.a. Riemann function) is defined on the interval (0, 1) as follows
Here is the graph of this function with some points highlighted as plus symbols for better view.
This function has interesting property: it’s continuous at all irrational points. It’s easy to see this if you notice that for any positive ε there is a finite number of dots above the line y = ε. That means for any irrational number x0 you can always construct a δ-neighbourhood that doesn’t contain any dot from the area above the line y = ε.
To generate the data file with point coordinates I wrote Common Lisp program:
To create the images I used gnuplot commands:
plot "thomae.dat" using 1:2 with dots plot "thomae.dat" using 1:2 with points
and Photoshop.
-
Math and Physics of Benderama
The last episode of Futurama has interesting formula involved. The entire plot is based on the Professor’s latest invention — Banach-Tarski Dupla-Shrinker — the machine that produces two copies of any object at a 60% scale. It was just a matter of time when Bender found a proper usage of this machine: to replicate himself. Then two small copies of Bender replicated themselves making four smaller copies, and so forth. At some point the Professor horrified the crew that if they don’t stop this unlimited growth, the total mass of all Benders will eventually be so big that the entire Earth will be consumed during the process of replication. As a proof he demonstrated this formula of the mass of all generations of Bender
This is a perfect toy for a science geek. The first obvious question it brings: is this formula mathematically correct? As it turns out, it is not. Considering the scale of 60%, the cubic dependency of volume on linear dimension, and the constant density of all copies, the formula should be the following
As you can see the total mass of infinite number of Benders actually converges to approximately 1.76 M0. So from Math perspective there is nothing to worry about. But what if our assumption of constant density is invalid. Would it be a problem from Physics perspective? Let’s see.
Knowing that every new copy has a size of 0.6 of the original it was made from, we have the following formula for the size of Bender in the nth generation
This exponential function becomes very small pretty soon. In the 154th generation it already reaches the Planck length, after which the further replication is physically impossible. If we calculate the total mass of 154 Bender’s generations using the Professor’s formula, we get H(154) × 238 kg ≈ 1,337.56 kg, which is nothing comparing to the Earth mass.
So we have to admit that from both Math and Physics perspective the Professor was wrong, and there was no real threat to the Earth.
Although the Professor’s formula doesn’t describe the replication process adequately, it’s still a beautiful piece of Math because it’s a formula of harmonic series. If you want to know why harmonic series is beautiful and which real processes it describes, read this nice article of John H. Webb.
And don’t miss the next episode of Futurama this Thursday :-)
-
Functional Groovy switch statement
In the previous post I showed how to replace chained if-else statements in Groovy with one concise switch. It was done for the special case of if-stement where every branch was evaluated using the same condition function. Today I want to make a generalization of that technique by allowing to use different conditionals.
Suppose your code looks like this:
As long as every condition operates on the same parameter, you can replace the entire chain with a switch. In this scenario
param
becomes a switch parameter and conditions becomecase
parameters of Closure type. The only thing we need to do is to overrideClosure.isCase()
method as I described in the previous post. The safest way to do it is to create a category class:Now we can replace if-statement with the following switch:
We can actually go further and extract in-line closures:
After which the code becomes even more readable:
-
Nothing new under the Sun
Every generation of software developers needs its own fad. For my generation it was Agile, for generation before it was OOP, and before that it was another big thing. Gerald Weinberg, one of the most influential people in our industry, blogged yesterday about this issue. With over 50 years of experience in software development he knows what he is talking about. Read his blog post — he has a very good point.
P.S. I’m wondering what will be the next big thing. Will it be Cloud or Big Data?
-
Multimethods in Groovy
Every time I switch from Groovy to Java I have to remind myself that some things that seem so natural and work as expected in Groovy, don’t work in Java. One of such differences is method dispatching. Groovy supports multiple dispatch, while Java does not. Therefore the following code works differently in Groovy and Java:
public class A { public void foo(A a) { System.out.println("A/A"); } public void foo(B b) { System.out.println("A/B"); } } public class B extends A { public void foo(A a) { System.out.println("B/A"); } public void foo(B b) { System.out.println("B/B"); } } public class Main { public static void main(String[] args) { A a = new A(); A b = new B(); a.foo(a); b.foo(b); } } $ java Main A/A B/A $ groovy Main.groovy A/A B/B