Thursday 29 April 2010

Problem loading Ubuntu 9.10 (Wubi) after update

Today I installed an Ubuntu update and rebooted the machine, only to find that I was unable to start Ubuntu. In stead, I was stuck within the "GRUB boot loader". After some intense frustration, followed by some grumbling research on another machine, I found the cause of the problem.

When I installed Ubuntu, I used Wubi, which installs Ubuntu as a Windows program. Wubi uses the GRUB boot loader, but there is a bug in GRUB which can potentially disrupt the Ubuntu boot process each time the linux kernel or GRUB is updated. The problem and its solution is described on this Sourceforge wiki page. In short, one needs to log into windows and replace the faulty file wubildr in the C:/ folder with this file.

Saturday 24 April 2010

Matlab figure with zoomed-in plot of area of interest

I came upon a Matlab file at the File Exchange website which lets you add additional subplots of zoomed-in areas in the figure. This looks hugely useful, for example for magnifying details on an image while still seeing "the big picture".

Thursday 22 April 2010

Changing text size in LaTeX

One of the advantages with LaTeX is that you don't really have to worry about font sizes, heading layout etc. - you can just structure your document, and the LaTeX compiler will take care of the rest. You can of course set the "global" font size when declaring the document class, for example

\documentclass[10pt]{article}

However, sometimes you want to change the relative size of some block of text. To do that, use the following syntax:

{\textsize Text with different relative size}

where you can replace \textsize with any of the following sizes:

\tiny \scriptsize \footnotesize \small \normalsize
\large \Large \LARGE \huge \Huge

Wednesday 21 April 2010

Jagged text and graphics for scanned PDF documents in Ubuntu

I download a lot of scanned articles in my work. These are usually in PDF format, and the default viewer for PDF files in Ubuntu is Evince. However, for the scanned articles, the text is really jagged and hard to read. It seems that this is due to "missing anti-aliasing", and judging from this forum thread, several other users are having the same problem. A fix seems to be on its way, but while I'm waiting, I've installed Okular (a document reader for the KDE desktop), which renders the scanned text beautifully.

Monday 19 April 2010

Comparing files in Ubuntu (graphical diff tool)

Today I found myself needing to compare two LaTeX files. I do my LaTeX editing in gedit, and was hoping that there might be a built-in diff tool. This is not the case (at least not yet), but after a bit of searching I found these programs:
  • Meld, for the GNOME desktop
  • KDiff, for the KDE desktop
Both programs can be downloaded from the Ubuntu repository (I was able to download it using the Ubuntu Download Centre), and both are able to compare and merge files and/or directories.

Note: There is also a command line diff tool (try diff --help), but I prefer a graphical interface for comparing files.

Sunday 18 April 2010

Compiling LaTeX code online

Have you ever needed to make a LaTeX document, without having access to a computer with a latex compiler? My wife had this problem today, and she emailed me and asked me to do the compilation. Of course some things had to be corrected, and we had to do a few email iterations before she was satisfied. Afterwards I started thinking that this must be quite a common problem, and that surely there must be some online service for compiling LaTeX code. And there is!
  • This one has quite a nice GUI, and the possibility to output the compiled document in several different formats (PDF, PNG, etc.)
  • This one is a lot simpler, but also works perfectly well.

Friday 16 April 2010

Installing Octave on Mac OS X

GNU Octave is a freeware alternative to Matlab which I have been curious about for some time now. I decided to try it out on my iMac, and downloaded it from Octave-Forge. At its core, Octave is a command-line program, but it uses GNUPlot for plotting figures. However, having installed both, I found that entering commands like

figure

or

plot(x,y)


had no effect at all. It seems that in order to work on Mac OS X, these two programs shuld be "glued together" by a third program called AquaTerm, which can be downloaded here. Having installed this, I am now able to make plots like the one below. Nice, eh? :-)

LaTeX in Blogger / Blogspot

UPDATE, 2012-02-13: The approach described under is now outdated. I would recommend taking a look at other alternatives, for example MathJax.

I was wondering if it would be possible to display latex formulas in Blogger - and of course it is. :-) I found a post by Watchmath describing how this can be done by adding a HTML/Javascript Gadget (like the "Blog Archive" on the right) with a little piece of javascript code. If it works, the following code

X(k) = \sum\limits_{i=0}^{N-1}x(n)e^{-2\pi ik \frac{n}{N}}

should magically turn into a beautiful LaTeX equation:

\[X(k) = \sum\limits_{i=0}^{N-1}x(n)e^{-2\pi ik \frac{n}{N}}\]

Nice! What's actually happening here is that the LaTeX expression is sent to a server for compilation, and then returned as an image which is pasted into the blog post. If you want to do the same, get the code and instructions here.

Note: This option does not work with "Preview" while editing - the code is compiled when the post is published, not before.

Browsing files in Ubuntu with superuser / root status

The file browser in Ubuntu is called Nautilus, and it works just like the Windows Explorer or Mac Finder. However, using Nautilus, I found that I was only able to move and delete files and folders within my home folder. To do anything outside this folder, I need "superuser" status. In the terminal, this "create folder"operation will not work:

mkdir /usr/share/newfolder

because I don't have write access to /usr/share. However, if I use "sudo" before the command, like this;

sudo mkdir /usr/share/newfolder

I am prompted for my password, and after entering it, I get superuser status and the folder is created. This works well for manipulating files and folders from the terminal, but I want to do the same using the graphical file browser Nautilus. The solution? Create a launcher on the desktop with the command

gksudo nautilus

gksudo is similar to sudo, but is used for graphical applications, as explained here. A window will pop up, asking for the password, and after entering it, Nautilus opens with superuser status.

Wednesday 14 April 2010

Implementing linear interpolation for arrays


Here is a problem I recently came across in my work. How do you implement linear interpolation for arrays, and what is the corresponding computational complexity? I will come back to exactly what I mean by interpolation for arrays, but first, consider the image on the right.

We have two known points, (x1,y1), and (x2,y2), and we want to calculate the linearly interpolated value yi, given the x value xi lying between x1 and x2. The steepness of the line between the two points is given by a = (y2-y1)/(x2-x1), and yi can be expressed as yi = y1 + a*(xi-x1).

Assume now that we have an array x = [0 1 2 3 4 5] with a set of corresponding y values, and an array xi = [1.5 3.1 3.7] that we want to interpolate for. How can the above linear interpolation be implemented effectively for such arrays? Although the calculation of yi is easy enough, we first need to find (x1,y1) and (x2,y2) to be able to do it. After studying the Matlab function interp1q, I think I have some idea about how this may be done.

Let us take a simple example, where x = [0 1 2 3 4 5] and xi = [1.5 3.1 3.7]. We stack these arrays on top of each other, and sort them:

[xSorted,sortInd] = sort([x;xi])

yielding xSorted = [0 1 1.5 2 3 3.1 3.7 4 5], and sortInd = [1 2 7 3 4 8 9 5 6]. The values 1-6 in sortInd correspond to x, while the values 7-9 correspond to xi. Now, to interpolate, we want to know x1 x value that precedes each xi value. In the example above we can see that these values are [1 3 3], but how can we calculate them automatically?

We start by creating an index, called r, of which place the numbers in sortInd have in the array. This can be done in a for loop,

for ii = 1:length(sortInd)
r(sortInd(ii)) = ii;
end

but this operation can also be vectorized, using the (slightly confusing) Matlab statement

r(sortInd) = 1:length(sortInd)

which yields r = [1 2 4 5 8 9 3 6 7]. Compare this now with xSorted = [0 1 1.5 2 3 3.1 3.7 4 5]. Note that the three last values of r, [3 6 7], represent the position of the xi values in xSorted. We call these values ri, and "cut them out" by the following statement

ri = r((length(x)+1):end)

Now, let us take a look at which x values precede each xi value. For xi(1), 1.5, the preceding x value is given by x(2) = x(ri(1)-1) = 1. For xi(2), 3.1, the preceding value is given by x(4) = x(ri(2)-2) = 3, and for xi(3), the preceding value is given by x(4) = x(ri(3)-3) = 3. Thus, we see a pattern emerging here. The index for the preceding x value for xi(ii) is given by ri(ii)-ii. We call the index for the preceding x values pInd. They can be computed in a for loop,

for ii = 1:length(ri)
xpInd(ii) = ri(ii)-ii;
end

or, equvivalently,

pInd = ri-(1:length(ri));

yielding pInd = [1 3 3]. Using this, we can calculate the interpolated y values with the following statement:

a = (y(pInd+1)-y(pInd)) ./ (x(pInd+1)-x(pInd));
yi = y(pInd) + a.*(xi-x(pInd));

To summarize, the interpolation algorithm can be executed as follows

[xSorted,sortInd] = sort([x;xi]);
r(sortInd) = 1:length(sortInd);
ri = r((length(x)+1):end);
pInd = ri-(1:length(ri));

a = (y(pInd+1)-y(pInd)) ./ (x(pInd+1)-x(pInd));
yi = y(pInd) + a.*(xi-x(pInd));

My main reason for explaining this to myself was that I wanted to have some estimate of the algorithmic complexity of this kind of linear interpolation. The most computationally demanding operation in this algorithm is the sort. If N = length(x) and M = length(xi), the computational complexity of the sorting algorithm (and thus also the linear interpolation) is O[(N+M) log (N+M)].

UPDATE (2010-04-16):
I came across this post on linear interpolation on the MathWorks blog "Loren on the art of Matlab". It has a good explanation of the "binning" necessary to do the interpolation, plus a load of interesting comments / replies.

Tuesday 13 April 2010

Installing Spotify on Ubuntu

OK, maybe Spotify isn't a "tool" as such, but it was nevertheless one of the first programs I tried to install on my new Ubuntu OS. There is no Spotify application directly available to Linux users, but it is possible to use the Windows application through a Windows emulator called Wine. With Wine installed, Windows programs can be installed and used in Ubuntu, without having to install a complete "guest" Windows OS (as you would do with e.g. VirtualBox).

Installing Spotify under Wine was really easy, but when I tried to run it, the sound was kind of distorted, and it would only play one song and then freeze. It turns out that this is a common problem in Ubuntu 9.10. Luckily, someone has already fixed it! This post on Jens' codelog describes what to do. In short, I had to uninstall Wine and install a new, modified version made by Neil Wilson. Now Spotify works like a charm, finally bringing some music into my office. Great!

A litte detail: There are no shortcuts or icons automatically created for Spotify. To create a shortcut on the desktop, right-click on the desktop and choose "create launcher". If you want a "proper" Spotify icon, you can use this SVG file created by Kalle Persson.

You can find more information on how to install and use Wine at this Ubuntu documentation page. There is also some more information on using Spotify under Wine on this Spotify FAQ.

Monday 12 April 2010

Switching to Linux

As a first short post, I'll just mention that I recently installed Linux (Ubuntu) on my work laptop (a ThinkPad T61). Up 'till now, I've been using Windows XP (work policy), but I have been curious about Linux for some time now, and wanted to give it a try. Seeing as I also constantly have to deal with servers running on Linux anyway, I saw this as a nice opportunity to learn something new.

I always assumed that in order to install a second operating system, a second partition on the hard drive would also be needed. I was pleasantly surprised to find that Ubuntu can actually be installed as a Windows application. That's right, a whole operating system can be downloaded and installed as a regular application, and if you don't like it, you can also uninstall it as an application. In stead of using a separate partition, the Ubuntu operation system is saved in one big file, which works as a separate disk/partition when you are inside Ubuntu. The Windows installer can be downloaded here.

After installing, I restarted my machine, and a "boot loader" pops up, asking if I wanted to use Windows or Ubuntu. Choosing Ubuntu, I was amazed at how fast the startup process was (20-30 seconds), and having logged in, I found a beautiful, responsive GUI with a very nice selection of preinstalled programs. Everything worked "out of the box", and I was connected to the internet (wifi) and surfing within half a minute.

This "Ubuntu Windows Application" is highly recommended for anyone curious about Linux. I will keep posting my experiences with it as I go.