Wednesday, November 20, 2013

Auto Encoder and Matlab tricks

Today, I went through the Auto Encoder exercise by Andrew Ng's CS294A class at Stanford: http://ufldl.stanford.edu/wiki/index.php/Exercise:Sparse_Autoencoder.

The idea of auto encoder is unsupervised feature learning by learning a identity function with sparsity constraints. This idea is very cool by itself, and the exercise is extremely well written. What surprised me is that the quality of the Matlab skeleton code provided and how many Matlab tricks are there that I don't know before. Here is a few of them:


  • It turns out that you can do currying in Matlab.
  • If you call a function that returns multiple values and receive the return using only one value, then the first returned value is received. This is quite handy sometimes.
  • You can add a search path for scripts/functions, so you don't need to put all scripts/functions in the current directory!
  • You can save plots to file simply using the 'print' command.

Friday, November 15, 2013

Random Java Tips (1)

This series of blog posts consists of various Java tips and language features that are illusive to normal programmers.

Throwing of exceptions
In Java, there are two types of exceptions, checked exception and unchecked exception. Checked exception, such as FileNotFoundException, must be either caught or re-thrown. However, for unchecked exception, you do not need to do that. But it is still syntactically OK to declare the throwing of them. In fact, you can add "throws X" to any method if X is subclass of Throwable, even if X is an unchecked exception, or if X is an checked exception that is not really thrown at all.

Initialization of arrays
In Java, an array can be declared and initialized at the same time, in the following syntax:
int[] a = new int[] {1, 2, 3, 4}
or
int[] a = {1, 2, 3, 4}  // short-hand of previous one

However, if you can not initialize and designate array size at the same time. I.e. you can not write
int[] a = new int[4] {1, 2, 3, 4}

Syntax for array type
When denoting a variable a of array of type X in Java, you can write either 'X[] a' or 'X a[]'.

For example we can write
int a [] = {1, 2, 3, 4}
But this is not preferred and thought as less readable by most programmers.



Monday, November 11, 2013

Using Bitmap to store canvas paint to optimize UI fluency in Android

This week, I was writing an android program where I have an ImageView that is being updated very frequently. The MVC pattern is used there. Basically, I have a model that stores the location and color of many circles and the ImageView will repaint each of them on its onDraw method, which looks like this: Unfortunately, this scales badly and gets really slow when the circle list in the model gets large. The key to a better solution is that the model only changes a little each time in most cases. In my case, I am removing one circle and adding 4 new circles each time. A better solution will be creating a Bitmap and store it either in the model or the view, so that it is regularly updated when the model changes. Each time the model is changed, we only need to erase one circle and draw another 4 on this Bitmap. For the ImageView, the onDraw method simply load the bitmap, which internally is just a memcpy and takes only a few millisecond. In other words, you don't redraw what is not changed. Finally, you can draw circle on the Bitmap easily by first create a Canvas onto it then draws on the Canvas in exactly the same way you normally do in an onDraw method. The following code snippet is what I used in my program. When I create a Bitmap: When the model changes: This will modify the underlying Bitmap. Of course, the Bitmap must be mutable.

Monday, October 21, 2013

CharSequence in Java

As the first blog in the series, I will start with a rather simple concept, CharSequence in Java.

CharSequence is an interface, that is implemented by useful classes that represent an array of characters, such as CharBuffer, Segment, String , StringBuffer and StringBuilder.

An implementation of CharSequence needs to support only 4 methods, charAt(), length(), substring() and toString(). For method that takes a parameter which is a merely a sequence of characters and if the logic of that method doesn't care about the underlying implementation for that sequence of characters, then a CharSequence shall be used. For example, String class has the following method:

public boolean contains (CharSequence s) 

You can pass any object whose class implemented the CharSequence interface. If String is used as the parameter type, then you have to convert the actual parameter to String if it is not already, which might be a waste of CPU time.

Given its convenience, one shall not use CharSequence as key to map or hash table, because the equals() and hashCode() of CharSequence are not refined by the interface. The original equals() and hashCode() of the class that implements CharSequence is likely to fail when comparing against object of another class that implements CharSequence.

Unlike what its name suggests, a char array, i.e. char[], is not a CharSequence for obvious reason (one is an array of primitive type, the other is an object). There are two ways to convert a char[] carray to a CharSequence:

  • CharSequence cs = new String(carray)
  • CharSequence cs = CharBuffer.wrap(carray)