Sunday, December 30, 2012

How to keep ssh alive in terminal of Macbook

In macbook, I usually use ssh in terminal to connect to remote servers. However recently the terminal often becomes unresponsive after being idle for a while, then it prints the error "Write failed: Broken pipe" and quits the ssh connection.

The solution I found is to add the following line in ~/.ssh/config:
ServerAliveInterval 120


How to install and link with the package "Geometric Tools" in Linux

Here is what I did to install and link with the package Geometric Tools:
2. Add the following line into ~/.bashrc, and run "sh ~/.bashrc" or log out and log in again.
3. Change to the directory GeometricTools/WildMagic5, and type
make CFG=Release -f makefile.wm5

4. To compile, we can use the following command

Sunday, December 9, 2012

How to correctly take square root in programming

If we simply call "sqrt()" function to take square root, very often we'll be caught by surprise.

The first common mistake is to not check negativity before taking square root. For example, Instead of getting 0.0 in both cases, one of the case will yield "-nan". This is because y is actually not exactly $$\sqrt{2}$$, but within machine precision of $$\sqrt{2}$$. Therefore, we should always check negativity before taking square root.

You may notice that, in one of the above two cases, even if the answer is not "-nan", the accuracy is quite poor, only within $$10^{-8}$$ of zero, rather than within machine precision of zero. This is because although $$y^2 - 2$$ is within machine precision zero, after taking square root, you'll only get square root of machine precision. This is another common mistake.

So how do we take square root accurately and error free? Assume you want to compute $$x = \sqrt{2 - y^2}$$, you can do something like the following

Wednesday, December 5, 2012

Determine whether an ellipse intersect a horizontal or vertical line

Assume an ellipse of width $$\sigma$$ and length $$\kappa \sigma$$ is centered at $$(x_0, y_0)$$, and has angle $$\theta_0$$ with the $$x$$-axis. How do we determine whether it intersects a horizontal line or a vertical line?

It turns out the criteria is very simple
• The ellipse intersects a horizontal line $$y = y_1$$ if and only if the following equation hods: $\triangle_1 = \sigma^2 \left(\cos^2(\theta_0) + \kappa^2 \sin^2(\theta_0)\right) - (y_1-y_0)^2 \geq 0$
• The ellipse intersects a vertical line $$x = x_1$$ if and only if the following equation hods: $\triangle_2 = \sigma^2 \left(\sin^2(\theta_0) + \kappa^2 \cos^2(\theta_0)\right) - (x_1-x_0)^2 \geq 0$

Sunday, October 28, 2012

How to make vim start faster remotely

I like to use "ssh -Y" to log into my university's server to remotely submit computation jobs, and vim is my indispensible editor. However, even if vim is a command line editor, it still tries to load X server on start, and this can take a few seconds (on my MacBook Air). A simple solution is to add the following line in your ~/.bashrc (in your account at the server):

In this way, vim won't try to load X server and starts much faster.

Wednesday, May 2, 2012

How to disable page forward/backward key in Thinkpad

It's a design flaw for Thinkpad to put page forward/backward together with the arrow keys. This frequently leads to loss of unpublished posts. This is what you can do in Ubuntu to disable those two keys:
1. Create the file ~/.Xmodmap with the following contents
keycode 166=
keycode 167=

2. (Depending on your distribution, this step may not be necessary.) Add the following code to your ~/.profile
# keyboard modifier
if [ -f $HOME/.Xmodmap ]; then /usr/bin/xmodmap$HOME/.Xmodmap
fi