## Friday, July 31, 2009

### A small project I'm working on

I'm currently working on a small project that's turned out pretty well. A functional language: It's basically lisp with pattern-matching grafted onto it. This has turned out to make the language very powerful. Nearly nothing needs to come built-in, except the definition, matching and expansion of patterns.

To explain the code examples in this post, I'll first outline the rough grammar of the definition statement.

The overall grammar is the following:
define ( pattern ) name expansion.

In the pattern, atoms may be prefixed with
~
Not to be evaluated until it is absolutely unavoidable (otherwise, they are evaluated on an as-needed basis)
@
Only allowed in the last atom, meaning that it is to correspond with what is left on the stack
=
Matching against the following token, and only the following token

In the expansion, the only prefix that exists is @, which appends the token onto the stack, instead of adding a list cell pointing to the value on the stack.

Some examples of the language.
Basic lisp-style arithmetics
( + 23 14 ( * 7 2 ) ) 

Conditional evaluation:
define ( =true @statement ) if   @statement.define ( other @statement ) if .

Factorial function:
define ( 0 ) fac 1 .define ( n ) fac ( * n ( fac ( + n -1 ) ) ).

Map function:
define ( ( @op ) ( top @rest ) ) map      ( @op top ) map op rest.define ( ( @op ) ( last ) ) map      ( @op last )

Mapping factorial function to some numbers:
print ( map ( fac ) ( 1 2 3 4 5 6 7 8 9 10 ) ).( 1 2 6 24 120 720 5040 40320 362880 3628800  )

Mapping a lambda function corresponding to squaring to same numbers:
print ( map ( ,\ ( x ) ( * x x ) )  ( 1 2 3 4 5 6 7 8 9 10 ) ) .( 1 4 9 16 25 36 49 64 81 100  ) 

A more complicated example:
define ( ( a @as ) ( b @bs ) ) zip         ( a b ) zip as bs .define ( ( a ) ( b ) ) zip        ( a b ) .define ten ( 1 2 3 4 5 6 7 8 9 10 )print ( zip ( map ( fac ) ten ) ten ) . (  ( 1 1  )  ( 2 2  )  ( 6 3  )  ( 24 4  )  ( 120 5  )  ( 720 6  )  ( 5040 7  )  ( 40320 8  )  ( 362880 9  )  ( 3628800 10  )  )

I'll post more details later. Mostly because the details are in such heavy flux right now.

## Sunday, July 26, 2009

### The Blob

Yesterday, something most peculiar happened. I was outside of my apartment, when a black object fell from the sky. I'd estimate it's radius to be around 5 cm and it's height to be 2 cm. It was jet black, hard, and I nudged it with my shoe, and found it had the same approximate density as a rock.

Throughout the day I observed this object. As time progressed, it became increasingly reflective, and a pool of viscous black liquid formed around it. Today all that was left was a pool of this viscous goop.

I snapped some pictures of the sludge, and something that first seemed related, but at a closer look at the pictures doesn't appear to have anything to do with it. It looks more blue in the pictures than it is in reality, since it is highly reflective, and the sky was blue at the time I took the pictures. For size comparison, the light gray rock in the pavement to the upper left of the puddle is about the same size as a larger coin.

I took a sample with some Q-tips and put it in an air-tight glass jar to preserve this substance since science demanded it of me. It was very viscous, and appeared semi-transparent gray against the head of the Q-tips.

I have no idea what it is. Did it come off an airplane[1]? From space? Is it organic? Though I suppose it's not the strangest thing to ever happen.

1 This is actually pretty unlikely. I live close to an airport, but perpendicular to it's runway, so I literally never get any aircraft flying overhead.

## Saturday, July 4, 2009

### Serendipitous bug

I was fooling around with a procedurally generated model of a tree, and went on to add some physics so as to made it bend under it's own weight. Somewhere along the line, I used the wrong angle in the simulation of the torsion springs I used to model the branches with, and I ended up with a pretty disturbing, but highly pareidolic result that's slap bang in the middle of nightmarish and amusing to watch.

## Thursday, July 2, 2009

### Double spring pendulum pRNG

You can make a pretty decent random number generator through physical simulation of a double spring pendulum. Double pendulums are chaotic systems, and as such, express a high variation of state after a given time, based on small variations on the initial dataset (the so called butterfly effect).

The motion used to generate the random numbers looks like this:

What makes this a decent random number generator is the lack of a closed form solution to the motion of the pendulum, combined with five hidden degrees of freedom.

The algorithm loosely looks like this:

1. Simulate for N timesteps (I the Euler method for integration, but it's probably a lot smarter to use a less sensitive algorithm -- or not, the errors introduced by the algorithm might make the data more random.)

2. Let θ be the inner pendulum's angle (scaled to the range 0 ... 1)

3. Let φ be the outer pendulum's angle relative to the inner angle (scaled to the range 0 ... 1)

4. Let the data be ((θ * 1000016) ∧ ff16) ⊕ ((φ * 1000016) ∧ ff16)

5. Let N be some constant (the higher the more random the data) + data * (some other constant)

6. Yield 'data'

7. Repeat.

Using 'ent' to analyze the data, these are typical values:

Entropy = 7.997207 bits per byte.Optimum compression would reduce the sizeof this 70346 byte file by 0 percent.Chi square distribution for 70346 samples is 271.07, and randomlywould exceed this value 23.38 percent of the times.Arithmetic mean value of data bytes is 127.4919 (127.5 = random).Monte Carlo value for Pi is 3.135107472 (error 0.21 percent).Serial correlation coefficient is 0.000481 (totally uncorrelated = 0.0).

versus /dev/urandom on Linux:
Entropy = 7.997121 bits per byte.Optimum compression would reduce the sizeof this 70346 byte file by 0 percent.Chi square distribution for 70346 samples is 281.53, and randomlywould exceed this value 12.19 percent of the times.Arithmetic mean value of data bytes is 127.3213 (127.5 = random).Monte Carlo value for Pi is 3.148072330 (error 0.21 percent).Serial correlation coefficient is -0.001095 (totally uncorrelated = 0.0).

The randomness is on par or slightly better than Linux' (pretty dubious) urandom-pRNG, depending on configuration. :-)

This whole project got started as a means of visually telling large numbers like checksums apart through setting the initial parameters of a spring coupled double pendulum according to the number, and then tracing it's motion for a couple of thousand iterations. That did do the trick satisfactory, but still needs more work before it's useful.

## Wednesday, July 1, 2009

### The (not-so) secret of .Xdefaults (or rather, .Xresources)

Several people commented that .Xdefaults—the original subject of the post—is an obsolete file, and that .Xresources should be used instead. I stand corrected, and so does the blog post.

There's a startling amount of people who have missed out on a pretty integral part of X11, namely the X resources database.

What the xrdb does is allow you to set various settings that you would otherwise have to pass with command line arguments to the programs. Clearly, it's pretty cumbersome to start xterm with the command xterm -fg gray -bg black just to escape the horrors of the black on white default color settings on xterm. Reverse video makes the defaults marginally less painful, but the contrast is still not particularly endearing.

Figure 1: A change of defaults makes a world of difference.

There's a wonderful file named ~/.Xresources that allows you to get around this. It is loaded automatically when the X session starts, and can be forced to reload with xrdb -merge ~/.Xresources

My .Xresources file looks like this:
XTerm*background: blackXTerm*foreground: grayXTerm*title: terminalXTerm*saveLines: 1024

If you read the man page for xterm, you'll find a rather intimidating list of settings you can alter with .Xresources.