Friday, January 18, 2013

2012: Books

Read surprisingly few of those this year and did not keep up my implicit promise of reviewing all of them. Though few in number, they made up in the quality. It is hard to pick the best one from among the likes of Museum of InnocenceFlowers For AlgernonThe Long Earth and The Glass Bead Game.

The Museum of Innocence was a behemoth, taking me through vicissitudes of Kemal Bey, and has to be noted for the massive closure it brought me by its very last line. However, that does not make a book good, it just makes it worth while; the quality of the book was in its calmness.

Flowers for Algernon was a book which hit deep. The subject matter and the plot lies very close to my heart. However, before it became a novel, it was a short story and the slightly sketchy characters and slightly incomplete threads end up showing in the book. Also, though the perspective of Charlie Gordon accentuates the book's poignancy, it at times makes it difficult to enjoy the story as a whole.

The Long Earth was more a treatise on evolution, humanism, politics and life. However, it contained just enough surprises, emotions, and nuclear bombs to keep it from becoming a little drab like The Glass Bead Game. Joshua and Lobsang, an unlikely team, ended up becoming the one of the best partners I have ever encountered in fiction, reminding me of Rupert Birkin and Gerald Circh from D. H. Lawrence's classic Women in love. Another USP of this book was that it was co-written by Terry Pratchett and plays with the inchoate boundary between "hard" Sci-Fi and Fantasy. This is the book which game me the latest name of my laptop: EarthWestOne, and now I am eagerly awaiting the sequel: The Long War. I cannot wait to meet First Person Singular, Mark Trine, and the next iteration of Lobsang.

But, the book which stole the show for me was Seabiscuit: An American Legend. This book made my heart race and was far better than most works of fiction I have read when it came to suspense, character development, story, crescendo, feints, and climax. However, don't read the epilogue in the same siting as the rest of the book.

Karna: Part One

Another interesting and special book I read last year was Karna, the debut novel of my friend Kartik Kaipa. It is based on the great Indian epic: Mahabharata. The characters are the same, the events are the same, the story is the same, the setting is the same and even the super-natural elements are the same. What change is the narration and some relatively minor details. Contrary to the usual pious tone reserved for the religious epic, Kartik makes the story take a more down to earth, and at times blasphemous, mien. His narration is sharp, at times humorous, and honest to the point of making one's bones ache. There is a point where he even breaks character to say some words about equality between women and men and Indian culture.

The story is well researched, with enough accuracy to allow him to bend the details his way. The protagonist of the story, Karna, has seen his ascension and is about to set on his journey as this book ends. I am eagerly awaiting the next two books planned in the series.

Saturday, August 25, 2012

Book Review: Captain Blood

Captain Blood by Rafael Sabatini
While browsing through the audio books on LibriVox, I came across this book. I had just read some Sci-Fi (Permutation City, which was good) and I wanted something more epic in scale, like one of the Dicken's novels. So this was the best fit. It turned out to be a good choice, it kept me good company for the next week.

I listened to this book in twenty minute chunks during my commute to and from the office. All the voices who read the story back to me were clear and precise, though I did find it a little difficult to relate to the Captain's character after hearing multiple renditions of his voice.

The book is well written, even though a bit clichéd. The story is fairly simple but with some twists and turns which kept me on the edge of my seat. The story is coherent and well told. The narrator has gathered these stories of the Captain from the logs kept by one of his buccaneering companion (Jim). The primary theme of the novel is the love between Arabella Bishop and Captain Blood and the narrator breaks-character to give reader glimpses about certain future events which concern it. However, otherwise the narrator remains chronological.

Sunday, July 29, 2012

Pre-allocate your vectors

Or else welcome the good ol' friend, the O(n^2) back in your life.

There are three common ways people add elements to vectors in R. First way:

f1 <- function (n) {
    l <- list()
    for(idx in 1:n) {
        l <- append(l, idx)
    }
    return(l)
}

This is bad. It does what it looks like: adds one element to the vector and then copies it to another variable, which just happens to be the same variable 'l'. This is clearly O(n^2).

The second method is this:
f2 <- function (n) {
    l <- list()
    for(idx in 1:n) {
        l[[length(l) + 1]] <- idx
    }
    return(l)
}

This approach looks decent because, coming from a Python and Java background, a good implementation would allow appending elements to vectors in amortized O(1) time. However, this holds an unpleasant surprize. Another good ol' O(n^2) in lurking in the shadows here.

Time taken to create a vector of size n
The method f2 is arguably better than f1, but none of them is any match for f3 which simply uses pre-allocation:

f3 <- function (n) {
    l <- vector("list", n)
    for(idx in 1:n) {
        l[[idx]] <- idx
    }
    return(l)
}


Of course these are just cooked up examples, but it is not difficult to imagine situations where such incremental creation of list is inevitable (think reading from DB cursors). For this particular example, the fastest solution involves a sapply but is not much faster then f3:

f4 <- function (n) {
    return(as.list(sapply(1:n, function (idx) idx)))
}

So in most cases it'll be better to overestimate the number of items in a list and then truncating it to the right size than dynamically expanding it.

And don't forget to smile and wave at the hidden O(n^2)!

~
musically_ut

PS: If you want to run the profile yourself, here's the gist of the functions and the profiling code.

Colors in R console

Update 2: The colorout package has moved again and is now available on GitHub.

install.packages('devtools')
library(devtools)
install_github('jalvesaq/colorout')

Update: The colorout package on CRAN has not been updated to be compatible with R version 3.x.x yet. However, if you compile and install it yourself, it still works.

download.file("http://www.lepem.ufc.br/jaa/colorout_1.0-1.tar.gz", destfile = "colorout_1.0-1.tar.gz")
install.packages("colorout_1.0-1.tar.gz", type = "source", repos = NULL)

 R console


Let's face it, the R-console is one of the more uninviting things I have seen, perhaps second only to the Ocaml console which comes without readline.

This is what my (and probably your) R console looks like this:
R console without color
What I see on the console is a single color which, firstly, makes it a challenge to separate stderr, output, warnings and errors, and, secondly, is just boring. I did not know what I could do about it and I stuck with it because the only other option seemed to be moving to a GUI (JGR or Rstudio). This is not to say that there is something wrong with the GUIs, but I prefer working with only my keyboard and, hence, shun GUIs almost as a rule. (Eclipse for Java is an exception.)

But it changed when I discovered the package colorout on CRAN, which makes my console look like this:
R console with color (using colorout)
This makes it remarkably easier to differentiate different forms of output.
The coloring is not perfect (notice that the second '-' in the interval outputted for tmp30 is assumed to be a negative sign instead of being a separator), but I would choose it any given day over the drab single color.
I did need a little tweak to my .Rprofile file since I did not like the way the default settings display the error:

~
musically_ut

Monday, June 11, 2012

Book Review: Seabiscuit - An American legend

This book is about a horse and four people around it: the owner, the trainer and two riders.

To be honest, I did not start the book with high hopes. To begin with, it was non-fiction and then it was about a horse. About 10 pages deep, I had an inkling that Laura Hillenbrand was luring me into a trap. I distinctly remember thinking "This is not how one writes a historical account" but I could not figure out what was amiss. The facts were all there, the cross-references seemed to match up and I could find nothing wrong apart from some poetic exaggerations which brought forth gentle chuckles. I circumspectly read through the account of earthquake in San Francisco without realizing anything.

It was just after the first race in Tijuana that I realised what was wrong with the book: It wasn't boring!

Friday, May 25, 2012

Book review: The Museum of Innocence

The first 100 pages or so made me consider many times why I was reading the book at all (it came highly recommended) and why didn't Kemal bey just move-on. Why stay there? And moreover, why call it innocence when nothing could have been further away? Why color something purportedly black and white like carnal desires in shades of gray by waving brushes of grandeur on it? Why not just admit it for what it was, why not just smack the monkey, massage the seal, walk the cobra and then do something else?

However, by the time his first year of misery was over, I understood him better. His misery was not a choice. His misery was a way of life. His innocence was blindness and denial, but it still reeked of innocence. He lived in Turkey, he lived the life of a rich man, and though the poor there thought that wealth would cure them of their illnesses, he lived not very differently.

Wednesday, April 4, 2012

Umlauts and accents in Gnome 3

The extra characters can usually be typed with the help of a compose key. In Gnome, one can choose one of the keys on the keyboard to be a compose key in this way.