Archive for January, 2009
SpiderSynth
Gradual progress at last. Yesterday evening marked an event in the scala rewrite of SpiderSynth. The code finally generated its first sound. Yippee!
It did lead me to ponder the productivity differences between Java and Scala. Particularly what lets each one down. I have talked enough about the problems with IDEs and Scala in the past so won’t revist that topic. Finding a good IDE can become a time consuming task that I have promised myself I won’t revisit until the summer in the hope that in that time things will have improved.
I have a list of features to implement in SpiderSynth before I actually do another release, but the basic idea is to get it feature equivalent to the java version as soon as possible.
Imaginary Numbers in scala
A Little code dump… An imaginary number class for scala, written in a functional style.
case class Im(val re:Double, val im:Double)
{
def +(rhs:Im) = Im(re+rhs.re, im+rhs.im)
def -(rhs:Im) = Im(re-rhs.re, im-rhs.im)
def *(rhs:Im) = {
val re_part = rhs.re*re - rhs.im*im
val im_part = rhs.im*re + rhs.re*im
Im(re_part, im_part)
}
def /(rhs:Im) = {
val denom = rhs.re*rhs.re + rhs.im*rhs.im
val re_part = (re*rhs.re+ im*rhs.im)/ denom
val im_part = (im*rhs.re - re*rhs.im) / denom
Im(re_part, im_part)
}
def *(factor:Double) = Im(re*factor, im*factor)
def /(factor:Double) = Im(re/factor, im/factor)
def conjugate = new Im(re, -im)
def magnitude = Math.sqrt(magnitudeSqrd)
def magnitudeSqrd = re*re +im*im
def inverse = conjugate/ magnitudeSqrd
}
And here is all the tests for it using scalacheck.
import org.scalacheck._
object imaginarySpecification extends Properties("Im (imaginary numbers)") {
implicit def arbPoint:Arbitrary[Im] = Arbitrary {
for{
re <- Gen.choose(-10000.0, 10000.0)
im <- Gen.choose(-10000.0, 10000.0)
} yield new Im(re, im)
}
specify("complex conjugate", (i:Im) => i.conjugate == new Im(i.re, -i.im) )
specify("Add", (i1:Im, i2:Im) => i1+i2==new Im(i1.re+i2.re, i1.im+i2.im) )
specify("Sub", (i1:Im, i2:Im) => i1-i2==new Im(i1.re-i2.re, i1.im-i2.im) )
specify("division", (i1:Im, i2:Im) => {
val denom = i2.re*i2.re + i2.im*i2.im
i1/i2 == (i1*i2.conjugate)/denom
})
specify("mult by factor", (i1:Im, f:Double)=> i1*f==new Im(i1.re*f, i1.im*f) )
specify("divide by factor", (i1:Im, f:Double)=> i1/f==new Im(i1.re/f, i1.im/f) )
specify("magnitude", (i:Im) => i.magnitude == Math.sqrt(i.re*i.re + i.im*i.im))
// Here are a seies of identities that hold for complex numbers and conjugates. Should give the
// Im class a really good work out. Taken from http://en.wikipedia.org/wiki/Complex_number
specify("conjugate adding", (i1:Im, i2:Im) => (i1+i2).conjugate== i1.conjugate + i2.conjugate )
specify("conjugate multiplication", (i1:Im, i2:Im) => (i1*i2).conjugate == i1.conjugate * i2.conjugate )
specify("conjugate division", (i1:Im, i2:Im) => (i1/i2).conjugate == i1.conjugate / i2.conjugate )
specify("conjugate reversable", (i:Im) => i == i.conjugate.conjugate )
specify("Real == half i + conjugate", (i:Im) => i.re == 0.5*(i + i.conjugate).re )
specify("Im == 1/2i(i-conjugate)", (i:Im) => i.im == ((i - i.conjugate)/Im(0,2)).re)
specify("magnitudes the same", (i:Im) => i.magnitude == i.conjugate.magnitude )
specify("magnitude sqrd", (i:Im) => i.magnitudeSqrd == (i*i.conjugate).re)
}
Another Night with SpiderSynth Rewrite
Yes I have been a little lax in more my programming project forward. Tonight I managed to spend a few hours with scala and SpiderSynth. I was tracking down some bug in some of my geometry routines. I have a lot of scalacheck tests for these, but one was new one was not passing – if was failing on its first attempt.
I needed unit tests to check the basics were working. I used ScalaTest and sbt just integrated in instantly without a problem. My tests put in place and with some pondering I came to the conculsion that a function in my Line class needed rewriting. Using this as a chance to make it more function in nature I reduced approximately 15 lines down to about 5.
The test still did not pass but with a more functional style of code it was much easier to reason about the logic and while sipping a cup of tea I managed to spot the mistake and get both the unit and property tests to pass. Ok I am not really much of a functional programmer and perhaps having scala allowing me to write in non functional ways slows down my development of this side of my brain. Small examples like this where a functional style make it clearer to the point of actually make it easy to spot bug definitely encourages me. You read about it in books but until you start to encounter it in your code it does not really start to sink in – or is that just me?
Book: The Art of Learning
Prior to Christmas I had been pondering the learning process. My other half got me “The Art of Learning” book for christmas. It is a fascinating book that is part biography of Josh Waitzkin, a talented chess player who later became a very sucessful martial arts competitor, and part a lesson on how to learn.
I was particularly interesting in the how to learn part and admit while Josh’s achivements are impressive I would have prefered that that part was removed so I could focus more on the “art of learning”. Having said that I suspect without the historical context of his life perhaps some of the finer points would not have hit home. None the less it is a very good book to read.
I was quite pleased a lot of my pondering prior to reading this book were mentioned so a lot of the ideas where already gestating in my mind. It was good to see some else reach similar conculsions with much more thought/reasoning put in on his part. Fundamentally the main thing behind his success seems to have been his ability to focus and the desire to understand something fully before moving onto the next thing.
I highly recomend this book, particularly if you have not given much thought to the process of learning. I supsect I will be lending this book to quite a few friends over the next couple of months.
End of Indie Games
Fascinating forum thread about the future of independently developed games, it touches on browser based gaming and has many developers revealing thier earning for last year – which is quite a range. Good read if a little bit long.