jlm-blog
~jlm

4-Mar-2008

Song chart

Filed under: humor, math — jlm @ 20:18

I recently was amused by the “song chart” meme, so decided to give it a try.

[Chart]

28-Jan-2008

Newcomb’s Paradox, part II

Filed under: philosophy — jlm @ 10:46

It’s been pointed out I’m not really addressing what’s wrong with the two arguments which give you different “correct” choices for the Newcomb scenario. So here we go…

Expected Outcome
This argument assumes that the predictor has foreseen our choice [with high probability]. If however we assume that free willed choices are by definition unpredictable, this is a logical impossibility.

Dominant Outcome
This argument assumes that the choice we make is not correlated with the predictor’s choice, because that would involve reverse causation. However, if we assume that we’re not free willed, or that freely willed choices can be predicted (whatever that would involve), then there’s a common cause: Our mind’s tendency to select choice X in the scenario causes both our selection of X when we’re in the scenario and the predictor to anticipate this selection. So, our choice wouldn’t be independent of the predictor’s, and dominance wouldn’t hold.

27-Jan-2008

Musings on Newcomb’s Paradox

Filed under: philosophy — jlm @ 11:45

So, I’ve been thinking about Newcomb’s Paradox lately.
I think part of the issue is it conflates a couple issues, and it might be useful to consider them separately. So, think on these paradoxes:

Determinism vs. Nondeterminism
Consider a superhuman predictor and a fair coin. The predictor predicts what the coin will show, then you flip it. The predictor is [nearly] always right.

Removal of free will
A computer has been programmed to maximize its expected score as a player in the Newcomb scenario, given that the predictor has a copy of the program to analyze, run in simulation or on another computer, etc. How will it play?

My take on Newcomb’s paradox? It reduces to the question of whether free will makes our choices inherently unpredictable, and the paradox is thorny because free will isn’t well defined enough to provide a clear answer.
If we have no free will, it’s just the computer scenario. If we assume free willed actions are inherently unpredictable, then the existence of a predictor contradicts that assumption, just like it contradicts the assumption that the outcome of a fair coin flip cannot be predicted.

10-May-2007

Tip: compression

Filed under: general — jlm @ 16:28

It doesn’t help to zip already compressed file formats, like jpeg or mpeg, it only wastes CPU. Uncompressed video, audio, or still images will compress better with algorithms which are dedicated for video, audio, or images than they will with general compressors. JPEG is for photos — for images which are line drawings use PNG.

3-May-2007

Periodic table of *

Filed under: science — jlm @ 13:36

Okay, so the Periodic Table of the Elements is one of the iconic symbols of Chemistry. Even if you flunked your high school Chem, you’ll recognize it instantly. Unfortunately, if you flunked high school Chem, you won’t actually know what it means.

Not recognizing that it’s a systematic arrangement of all the chemical elements, elucidating their properties in terms of their electron structure, you’re tempted to fill it in instead with items from whatever interest you do have. Make a Periodic Table of Fruit, or a Periodic Table of Government, or a Periodic Table of Rhetoric, then feel like you’ve somehow contributed something useful to Fruit classification. But what does this non-Elemental table mean? Do the properties of fruit recur periodically as something corresponding to atomic number increases? What even corresponds to atomic number? Why should fruit group as if it had electron shells? Does your table actually explain anything? Or are you just capitalizing on the fact that atoms do have electron shells and so elemental properties do arrange into a periodic table which is tremendously useful for chemistry, and applying it to something completely inappropriate?

30-Mar-2007

Distance between points on a sphere

Filed under: math — jlm @ 15:22

If you try and find out how you compute the distance between points on a sphere, you’ll get a bunch of sites which offer to calculate it for you if you enter the coordinates. If you search harder, you can even get the formula. But no one seems to be offering a derivation. So here you go.

First, some preliminaries. The half angle formulas give us 2 sin² ½ψ = 1-cos ψ. (Easily derivable from cos 2α = cos² α – sin² α.) The straight-line distance (cutting through the inside of the sphere) between two points on a unit sphere with an angle ψ between them is 2 sin ½ψ. (Bisect the triangle formed by the two points and the center of the sphere.)

[Geometric diagram of a sphere]

Consider A and B to be our two points we want to get the distance between, with latitudes φA and φB, and longitudes which differ by Θ. We’ll operate mostly on the disk formed by the parallel through B. The point where it intersects the pole is D, and the projection of A onto it is C. Let EB be perpendicular to DC.

The radius of the parallel through A is cos φA = DC; the one through B is cos φB = DB. Angle CDB is Θ, so EB = rB sin Θ and ED = rB cos Θ.
EC = CD – ED = rA – rB cos Θ.
BC² = EC² – EB² = rA² – 2 rA rB cos Θ + rB² cos² Θ + rB² sin² Θ = rA² + rB² – 2 rA rB cos Θ.

AC = |sin φA – sin φB|.
AB² = BC² + AC² = rA² + rB² – 2 rA rB cos Θ + sin² φA + sin² φB – 2 sin φA sin φB
   = 2 – 2 rA rB cos Θ – 2 sin φA sin φB.

If ψ is the angle AOB (which is also the along-surface distance between A and B), then
AB² = 4 sin² ½ψ = 2 – 2 cos ψ.
So, 1 – rA rB cos Θ – sin φA sin φB = 1 – cos ψ.
ψ = arccos(cos φA cos φB cos Θ + sin φA sin φB).

30-Jan-2007

Washington’s farewell address

Filed under: politics — jlm @ 17:36

Randall Munroe, of xkcd fame, has gone and translated George Washington’s farewell address into everyday speech. It’s an utterly fascinating read, and much more tractable than trying to read the whole thing (it’s quite long) in his original text. Washington’s ideas on what makes good government are well argued by him, and Munroe has made it accessable. Give it a read, you won’t regret it!

29-Jan-2007

Cell biology animation

Filed under: science — jlm @ 07:33

The folks at Harvard have put together an amazing CGI video showing some of the biochemical dynamics involved in our cells: The Inner Life of the Cell

There’s other good informative bio videos there, but nothing else with as tasty eye-candy.

17-Dec-2006

Hectic holidays

Filed under: misc — jlm @ 16:37

A friend of mine called this time of year the “hectic holidays”, and it’s absolutely correct.
I love the getting together with my family, the Christmas feast, the time off… But the gifting is very stressful. It’s hard to pick out gifts for people, it’s hard to ask for gifts, and shopping is a huge pain: Even when you know what you want to buy, going to retail stores right now is an exercise in frustration. And then the effort is wasted because they’re out of stock!
How many people can we get together under the idea that Christmas is better off without the gifts, so we can focus on nobler ideals. Can we make Christmas a holiday that’s not about commerce, but a celebration of our brotherhood with our fellow man?

16-Dec-2006

The C aliasing rules

Filed under: programming — jlm @ 11:49

A lot of people are confused by the pointer aliasing rules introduced by C99. (Sometimes called “strict aliasing”.) First, what is aliasing? When two pointers refer to the same location, they’re called “aliases”.
void fcn(int *p) { int *q = p; ... }
Here p and q are aliases. The general rule is that pointers to the same type are allowed to alias, and pointers to different types are not. (Differences like signed/unsigned and const/non-const aren’t considered significant here– they’re all in the same “aliasing class”.)
void fcn(float *p) { int *q = (int *) p; ... }
This is illegal, because p and q are aliases, but one points to float while the other points to int. In practical terms, the optimizer will reorder accesses to *p and *q across each other, or optimize out writing/reading the value back into/from memory, because they’re in different alias classes, and you’ll get mysterious bugs.
There are two exceptions to this rule: void * and char *. Pointers of these two types are allowed to alias any kind of pointer. void * can be assigned from and to any pointer without a cast, but you’re only allowed to assign it to a type which reflects the original pointer (or char *).
void intfcn(void *p) { int *q = p; ... }
void floatfcn(void *p) { float *q = p; ... }
void fcn(void) {
  int i;
  intfcn(&i);  /*  OK: int * -> void * -> int *  */
  floatfnc(&i);  /*  Bad: int * -> void * -> float *  */
}

This style is used a lot in callbacks, where it’ll look something like:
void callsoon(void (*fcn)(void *), void *arg);  /* Call fcn(arg) in one second */
void fcn(void) {
  static int i = 1;
  static float f = 0.1;
  callsoon(intfcn, &i);
  callsoon(floatfcn, &f);
}

The compiler is still assuming your int * and float * pointers won’t alias here: It’s up to you to ensure that the void * which got a float * doesn’t get turned into an int * and vice-versa. The void * is telling the compiler “I can ensure non-aliasing on my own, without you checking”. You should not use it to silence aliasing warnings, because you get those warnings in situations where the compiler is going to re-order accesses, and the void * gives it free license to do so silently!
What do you do when you really need to alias across types? First, don’t do a straight pointer typecast or go through a void * typecast for the reasons above:
void fcn(float *p) { int *q = (void *)p; ... }
void fcn(float f) { int i = *(int *)&f; ... }
void fcn(float f) { int i = *(int *)(void *)&f; ... }  /* All illegal aliasing */

Another common but illegal technique is to go through a union:
union { int i; float f; } x; x.f = f; i = x.i;  /* Illegal: unions can only be read from the last type assigned to them */
So, what do you do? One of the most common use cases is for transferring byte representations, and memcpy() works fine for this (any modern compiler will optimize it into stores).
void fcn(float *p) { int i; memcpy(&i, p, sizeof(i)); ... }  /* Safe if sizeof(int) == sizeof(float) */
void fcn(float f) { int i; memcpy(&i, &f, sizeof(i)); ... }  /* Ditto */

void fcn(float *p) { int *q; memcpy(&q, &p, sizeof(q)); ... }  /* Illegal: just a fancy way of writing q = (int *)p */
If you need fine-grained access, this is where char * comes in. It’s allowed to alias any other pointer, so the compiler can’t re-order pointer accesses across char * accesses unless it can ensure non-aliasing by other means.
void fcn(float *p) { char *q = (char *)p; ... }  /* Safe */
Note that just going though a char * on your way to an illegal alias doesn’t make it legal:
void fcn(float *p) {
  char *q = (char *)p;  /* q now validly aliases p */
  int *r = (int *)q;  /* r now validly aliases q, but p and r are still in different alias classes and *p and *r can be re-ordered across each other */
  ...
}

void fcn(float *p) { int *q = (int *)(char *)p; ... }  /* Illegal aliasing of p and q, same as q = (int *)p */
char * is safe, as long as you do your references through it, as chars (bytes), but just having a char * there doesn’t make non-char * references legal.

Powered by WordPress