Phrase of the week

September 29, 2013 at 8:04 AMMichele Mottini

In this do not fail. For such is our pleasure.

King Louis XIII in a letter to Samuel Champlain, quoted in 'Champlain Dream' by David Hackett Fisher

Posted in: Opinion

Tags:

Phrase of the week

September 22, 2013 at 11:48 AMMichele Mottini

..the financier...who claimed to have found a way to banish risk when in fact they had simply lost track of it

The Economist in 'Crash course'

Posted in: Opinion

Tags:

Relativistic billiard problems

September 18, 2013 at 7:57 AMMichele Mottini

I did not find anything ‘pre cooked’ to convert my billiard simulator to use special relativity, so I started to work on the necessary math from scratch – that turned out to be problematic (to say the least).

For example, the collision of a ball with the side of the table become something like this:

ballside

because – in the frame of reference of the table – the ball contracts along its direction of motion. This deformation affects the computation of the collision time and changes the dynamic of the collision. The same collision seen in the frame of reference of the ball becomes instead:

ballside2

because now it is the distance from the ball to the side that contracts, changing the collision angle.

A ball-ball collision is even worse, being something like this (in the table frame of reference):

ballball

I found some pretty good notes on relativistic dynamics by an Oberlin college professor. The last chapter has a section about ‘hard sphere forces’ – e.g. billiard balls – that concludes that the whole idea of handling them within special relativity is ‘ludicrous’. At the same  time I found one article about rigid bodies in special relativity. I think that some kind of solution should be possible, but I have to study more.

For the moment being I am going through the relativistic dynamics notes (including the exercises!) and then I’ll have a look at the rigid body article.

Posted in: Physics

Tags: , ,

Phrase of the week

September 15, 2013 at 1:37 PMMichele Mottini

We buy junk and sell antiques

Slogan of a flea market in Perth, Ontario

Posted in: Opinion

Tags:

Phrase of the week

September 8, 2013 at 1:31 PMMichele Mottini

Mrs Merkel is likely to stick to her usual television style, of talking soothingly without saying anything

The Economist in 'Dog eats dog'

Posted in: Opinion

Tags:

Sequence expressions and side-effects in F#

September 6, 2013 at 4:11 PMMichele Mottini

I discovered an interesting problem when using sequence expression to read a text. The original case was more complex, but here is a simple example to reproduce the problem – a function reading all the lines of text from a text stream, returning them as a sequence of string using a sequence expression:

let readlines (reader: System.IO.TextReader) = 
  seq {
    let line = ref (reader.ReadLine())
    while !line <> null do 
      yield !line
      line := reader.ReadLine()
 }

Reading a two-lines text:

readlines (new System.IO.StringReader"A
B") 

produces as expected:

val it : seq<string> = seq ["A"; "B"]

Now define a simple function that checks if a sequence is empty and return either the string “Empty” or the string “XX items”:

let test s = 
  if Seq.isEmpty s then 
    "Empty"
  else
    sprintf "%d items" (Seq.length s)

Apply it to the same sequence as above:

readlines (new System.IO.StringReader"A
B") |> test

and the result is

val it : string = "1 items"

that is very wrong: the sequence contains 2 lines, not 1.

The problem is that the call to Seq.isEmpty reads the first line, moving the text reader to the second line (i.e. causing a side-effect) – and so causing subsequent uses of the sequence to skip the first line.

In this case the problem is with a text reader, but any call causing side-effects within a sequence expression is bound to cause the same unexpected behavior.

One possible solution is to cache the resulting sequence:

let readlines (reader: System.IO.TextReader) = 
  seq {
    let line = ref (reader.ReadLine())
    while !line <> null do 
      yield !line
      line := reader.ReadLine()
  } |> Seq.cache

that prevents double calls to read the same element in the sequence, fixing the problem.

Another solution is to create the reader within the sequence expression:

let readlinesStr str = 
  seq {
    use reader = new System.IO.StringReader(str)
    let line = ref (reader.ReadLine())
    while !line <> null do 
      yield !line
      line := reader.ReadLine()
  }

that causes a new reader to be created each time sequence is accessed, avoiding side-effects.

Posted in: Programming

Tags: , , ,

Phrase of the week

September 1, 2013 at 12:38 PMMichele Mottini

I have distinct reservations as to how good "the good old days" were.

Franklin D. Roosevelt in a speech to the Canadian parliament.

Posted in: Opinion

Tags: