## Wednesday, June 24, 2009

### Matrices 2

I’ll continue today with a little series for beginners. On my first post I forgot something important. One of the very first things I do with a class is create a toString method. This way, I get instant feedback on how my class is doing, as I paste it into the REPL, and try out the methods I defined.

So, let’s think of a toString method. Since all data in my class is stored in the private val “self”, we could simply delegate the task of producing a string:

`override def toString = self.toString`

That might do at first, but it would be unreasonable to expect something better formatted than that. First, let’s see if we can’t get the output in multiple lines, using mkString:

`override def toString = self mkString “\n”`

Unfortunately, the output of that looks like this:

`res6: String =[I@196a753[I@1c36f46`

This wouldn’t happen with a List, but an Array is implemented to be compatible with Java, and Java had Arrays have some intrinsic limitations, as they were present in the language from the very beginning, much before generics, and some design decisions about them proved to be unfortunate. Anyway, let’s fix that:

`override def toString = self map (_ mkString “ “) mkString “\n”`

Better, but we can improve on that still. For one thing, it would be much better if the columns were aligned. Now, to align the columns, we first must know how big a column can get. Or, in other words, we must know the size of the number with the biggest string representation in the matrix.

So, summing it up, we have to look through all of the matrix elements, get their sizes, and choose the biggest one. Getting a number’s size is easy: number.toString.size. Now, if we had a list, this would be easy, but we have an array of arrays. When we have nested collections and we want to unnest them, we use flatMap. For instance:

`scala> Array(Array(15,0),Array(2,-30)) flatMap (x => x)res19: Array[Int] = Array(15, 0, 2, -30)`

The flatMap operation we used, (x => x), simply returns what it was given. If all you want is unnest the collections, this suffices. But, actually, we want the number sizes. Since “x” is an Array, not a number, we can’t do flatMap (x => x.toString.size). But we can replace the numbers with their sizes, and then unnest the arrays, like this:

`scala> Array(Array(15,0),Array(2,-30)) flatMap (_ map (_.toString.size))res20: Array[Int] = Array(2, 1, 1, 3)`

Next, we want to get the biggest one. We have a collection of number sizes, and we want to reduce it to the single biggest one. We have two such methods in Scala: reduceLeft and reduceRight. Since “max” is commutative, both will produce the same result. But reduceLeft has a better performance, as it iterates over the collection in the order given. The final code should look like this, then:

`val maxNumSize = self flatMap (_ map (_.toString.size)) reduceLeft (_ max _)`

Pretty easy, eh? My first version had three lines, and was much more confusing to read. It helps stating what your problem is – you’ll often see you can do exactly that in Scala. So, we have a size, what do we do with it? Well, let’s just make sure all numbers are printed to that size. If we format them with “%2d”, for instance, every number will take at least two characters, with the left side being filled with a space as needed. To produce such formatted numbers, we’ll make use of RichString’s “format” method:

`override def toString = { val maxNumSize = self.projection flatMap (_ map (_.toString.size)) reduceLeft (_ max _) val numFormat = "%"+maxNumSize+"d" self map (_ map (numFormat format _) mkString “ “) mkString “\n”}`

That’s it! I added a “projection” there to make it a bit more performatic, as it doesn’t produce intermediate arrays. On Scala 2.8, that method gets replaced with “view”, with “projection” remaining as deprecated. Well, that was so easy I want to do more. First, I want to produce little vertical bars on the sides, to make clear the matrix boundaries, in case we are printing them side by side. That just requires changing the inner mkString to mkString(“| “, “ “, “ |”). But I also want to bend the bars up and down, to indicate the top and bottom lines, as well as produce something special for one-line matrices.

For all of them, we are just making small changes in the inner mkString. We have one for the top line, one for the intermediate ones, and one for the bottom line. We could make a list of all these lines, and then use mkString on the list, but we have to be careful, because the “bottom” line and the “intermediate” lines might not exist, and the top line has two different formats depending on whether the matrix has more than one line or not. Taking that into account, here is a much larger, but, hopefully, nicer toString method:

` override def toString = {   val maxNumSize = self.projection flatMap (_ map (_.toString.size)) reduceLeft (_ max _)   val numFormat = "%"+maxNumSize+"d"   def formatNumber(n: Int) = numFormat format n   val top = rows match {     case 1 => _row(0) map formatNumber mkString ("< ", " ", " >")     case _ => _row(0) map formatNumber mkString ("/ ", " ", " \\")   }   val middle = rows match {     case 1 | 2 => Nil     case _ => self.toList.tail.init map (_ map formatNumber mkString("| "," "," |"))   }   val bottom = rows match {     case 1 => Nil     case _ => List(_row(rows - 1) map formatNumber mkString ("\\ ", " ", " /"))   }     top :: middle ::: bottom mkString "\n" }`

In a parting note, someone suggested one technique to be able to parametrize the class. There are many, but they are slower than using "AnyVal" types directly. If I happen to be mistaken, please do post about it at length!