Category

Showing posts with label Computer and Internet. Show all posts
Showing posts with label Computer and Internet. Show all posts

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

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
    

Thursday, April 5, 2012

How to toggle on/off touchpad in Ubuntu

Somehow touchpad inteferes a lot with typing in Ubuntu. After some experimenting, I found the following script can reliably toggle on/off the touchpad:

For thinkpad X220, we can use Fn+F8 to toggle on/off the touchpad.

I found that the best experience so far can be achieved by disabling touchpad and using keyboard+USB mouse instead.

Saturday, March 3, 2012

Install gettc on macbook

gettc is a very convenient tool for downloading TopCoder problems (description, test cases etc.) and practice offline. This post summarizes how I installed gettc on my macbook air.
  1. Mac OS X Lion ships with ruby 1.8.7, and gettc requires ruby>=1.9. So our first step is to install a newer ruby
    1. Install rvm, and use it to install new ruby. Follow the steps here
    2. Set the new ruby as default. Follow the steps here
  2. Next install prerequisites of gettc.
  3. Next install gettc. Follow the instructions here. After it's done, change the first line (location of ruby) in `which gettc`. The location of ruby can be found by `which ruby`.
  4. Set up the PATH by adding the following line to ~/.bash_profile:
  5. Modify "~/.gettc/template/solve/cpp/Makefile" to remove the option "-std=gnu++0x". Modify "~/.gettc/template/bin/runner.sh" to remove the option "-o $ostream".


Remarks:
If your machine is Ubuntu or other unix variants, and you don't have root privilege, you can use the above steps as well.

Friday, March 2, 2012

Getting used to Mac from a Windows/Linux background

Here are some tips for getting used to Mac from a Windows/Linux background. The following tips apply for the Mac OS X Lion on a Macbook Air.
  • Right click: use "control"+click instead.
  • Cut/Copy/Paste/Find: use "command"+X/C/V/F instead.
  • Home/End button
    • "control"+A for Home, and "control"+E for End works in both browsers and Terminal.
    • "fn"+Shift+left/right works in Terminal but not in browsers.
    • "command"+left/right works in browsers but not in Terminal.
    • In the Terminal, if you are using vim, you can use "^" for Home and "$" for End.
  • Select two files and all the files in between (multiple file selection): first switch from icon view to list view, and then do it in the Windows way, i.e. select the first file, press and hold "Shift" and select the last file.
  • Install the command-line-only gcc C/C++ compiler: download "Command Line Tools for Xcode" from Apple Developer Site, which is only 170M (or 280M after install).
  • Install latex: I used the basic version of MacTex and TeXShop.
    • To find a missing package, you can use ctan search.
    • To download the missing package, you can do "sudo tlmgr install [package name]".
    • To install a package manually, you can extract it into /usr/local/texlive/2011basic/texmf-local/ and then run "sudo texhash".
    • To do inverse search in TeXShop, you can use "command"+click on the pdf viewer.

Monday, February 20, 2012

How to set default screen brightness in Ubuntu 11.10

For some unknown reason, the setting in "System Settings->Screen->Brightness" is not saved in Ubuntu 11.10 of my HP laptop, and I had to adjust it manually each time when my computer starts. This has been really frustrating. Today I've finally googled a solution that works perfectly. The solution can be found here.

Thursday, February 16, 2012

How to run Mathematica in batch mode

  1. Let's say your code is saved in myscript.nb. First you have to convert it to plain text by copy and paste from Mathematica. Assume the plain text version is saved in myscript.m.
  2. Next, run the script in the terminal as follows:
    math -noprompt -script myscript.m
    

Saturday, February 11, 2012

How to write math formulas in blogger

MathJax provides an easy solution for displaying math formulas on web pages (including blogger). You can start writing blog posts with math formulas using latex syntax at blogger (blogspot) int the following two steps.
  1. At the dash board of blogger, click Layout->Add Gadget->HTML/JavaScript, and add the following code
    
    
    
    
  2. To write blog posts with math formulas, use \(..\) as delimiters for inline math symbols, and $$...$$ or \[...\] as delimiters for display style math equations.

How to enable syntax highlight for code blocks in blogger

With SyntaxHighlighter, we can easily include a block of code in blogger (blogspot).
  1. First install the javascript code in your blogger.In the Design->Layout->Add a Gadget->HTML/JavaScript, enter the following code
    
     
    
    
    
    
    
    
     
    
    
    
     
    
    
    
    
    In the above example, I've chosen to use the free hosted version. You can also download and host the javascripts yourself, as described in the author's website . In the above example, I included the following brushes: Bash, JavaScript, C++, Python and LaTeX. More supported languages can be found in here.
  2. Next, to write new posts with code blocks. You can use either the <pre> tag or the <script> tag as described here .

Tuesday, January 31, 2012

How to let Google Chrome automatically handle Java Web Start in Ubuntu

A previous post describes a way to run TopCoder Arena without launching from browsers. This post gives another way to launch Topcoder Arena in Google Chrome browser under Ubuntu. It should work for other Java Web Start applications, too.
  1. Intall the latest sun java 6 and browser plugins by following the instructions here.
  2. Set JAVA_HOME and PATH in ~/.profile. An example is the following
    export JAVA_HOME="/usr/lib/jvm/jdk1.6.0_31"
    export PATH="$JAVA_HOME/bin:$PATH"
    
  3. Install the mime type for "jnlp" files. In the terminal, type
    xdg-mime install $JAVA_HOME/jre/lib/desktop/mime/packages/x-java-jnlp-file.xml
  4. Install the desktop file for "jnlp" files. In the terminal, type
    cp $JAVA_HOME/jre/lib/desktop/applications/sun-javaws.desktop ~/.local/share/applications/
    
    And make sure it contains the following line
    Exec=javaws %u
  5. Relate the mime type and the desktop file. Add the following line to ~/.local/share/applications/mimeapps.list
    application/x-java-jnlp-file=sun-javaws.desktop
  6. Configure Google Chrome browser to open the "jnlp" files automatically after downloading. Download any jnlp file in Google Chrome browser, and select "always open files of this type" in Google Chrome.

Wednesday, December 21, 2011

How to download Topcoder problems for offline viewing

If you just want to download one single problem at a time for practice, the tool gettc is perfect for that. It takes the problem ID as input and can automatically download problem description and generate C++/Java templates. You can even run demo/system tests from within the terminal.

If you want to download multiple problems

  1. Install gettc

  2. Select your desired subset of problems from here and download the html files.

  3. Say list*.html saves the list of problems. The following steps extract the problem ID's from the html files.

    1. grep -o 'pm=[0-9]*' list*.html > tmp.txt 

    2. Edit tmp.txt using vim and keep only the problem ID's.

    3. cat tmp.txt | sort -u > list.txt 

    Now the file list.txt contains unique problem ID's.

  4. Download the problems from Topcoder using the following command.

  5. for num in `cat list.txt`; do gettc $num; done



To create an ebook for reading in Kindle

  1. Create a dir called 'tmp'

  2. Run the following command

  3. for dir in `ls`; do mkdir tmp/$dir; cp -a $dir/prob tmp/$dir/; done

  4. Change to tmp, and generate index.html using the following scripts
    echo "<ul>" > index.html
    find . | grep html | sort > tmplist.txt
    for file in `cat tmplist.txt`; do
      name=`echo $file | awk -F/ '{print $2}'`
      echo "<li><a href=\"$file\">$name</a></li>" >> index.html
    done
    echo "</ul>" >> index.html
    rm tmplist.txt

  5. Zip the directory 'tmp' and feed it to Calibre.

Tuesday, December 20, 2011

How to run Topcoder Arena from Ubuntu

Somehow Topcoder Arena does not work in chrome or firefox browsers in Ubuntu 11.10.

This is what you can do instead:
(1) Download http://www.topcoder.com/contest/classes/ContestApplet.jar to your local directory.
(2) Run the following code in a terminal.
java -cp ContestApplet.jar com.topcoder.client.contestApplet.runner.generic www.topcoder.com 5001

This method works in openjdk as well.

Update: this seems no longer working for Java 7. The ultimate solution is to configure Ubuntu/Mac etc. to open .jnlp files with javaws, similar to the instructions in this post.

Wednesday, December 7, 2011

How to obtain high quality ebooks for reading in Kindle

In this post I record some tips for getting high quality ebooks for reading in Kindle. All the tools described in this post can be easily found in Ubuntu Linux. However, most of these tools should have Windows or Mac equivalent, too.
  • azw and mobi formats are the native formats of Kindle. Ebooks in these formats can usually be directly opened in Kindle. However, some azw files are DRM protected. In that case, there is not much you can do.
  • txt, html (zipped or unzipped), chm and epub formats. Although these formats are not native to Kindle, they can usually be converted to high quality mobi formats easily using Calibre. Some complicated structure chm books might have some trouble. However, since chm files are essentially a zipped html file, you can always decompile it and then process using Calibre.
  • pdf files. These files are no good for Kindle, for the following reasons. (1)The font size cannot be adjusted. (2)Dictionary look-up usually does not work (sometimes it does work). (3)Table of contents and internal links do not work either, which means it is very difficult to navigate inside the pdf document.
    • For pdf files with 100% copyable text (such as novels), you can try Calibre. Usually you can get very good results.
    • For pdf files that are scanned or have a lot of math equations, figures, tables, the results are usually unsatisfactory. What you can do, is to cut the white margins and read it in the landscape mode. I usually use pdfcrop to cut the white margins, which can use different bounding box for odd and even pages. For non-scanned pdf files, the white margins can usually be automatically calculated by pdfcrop.pl (even so, I usually add 2 to the margins of top and bottom, so that the black pdf bounding box of Kindle won't hide the content over those margins). For scanned pdf files, you usually have to find the bounding boxes manually using trial and error. For scanned pdf files with uneven margins, this process can be painful and non-practical at all.
    • For dual-column pdf files, I first trim the white margins using pdfcrop.pl, and then use pdfquarter to split each page in the middle. The outcome is usually very good, because the fonts are larger than those single-column ones.
    • The cropped pdf can usually be very large (100M+ in some cases). After applying pdfcrop.pl, I usually then apply a nautilus script to compress the pdf. The pdf size can usually be significantly reduced.
    • For pdf files with non-embedded Chinese fonts. I usually first print it to ps from acroread, and then use "ps2pdf -dEmbedAllFonts=true ***.ps" to get a pdf file with the fonts embedded. The resulting pdf file can then be processed using pdfcrop.pl. See this web page for more details.
  • latex files. There are many latex to html utilities, such as latex2html, tex4ht, tth, pandoc, plastex etc. You can also import to WYSIWYG scientific writing software such as TeXmacs and LyX, and then export to html from there. For small and non-complex document, usually you can get very good quality html files. And then you can create mobi ebooks using Calibre. However, most of these utilities are far from satisfactory. For example, most of them seem to only accept eps figures, and do not like pdf figures. Plus, only standard latex packages are supported. Another way to import latex documents to Kindle is to compile them to pdf files, but use layout for small screen (such as A5) instead of large A4 paper. The following forum thread provides several such templates.
  • By the way, in case you don't know already, Calibre can fetch news from online and produce high quality newspapers and send them to your Kindle over wifi. You just choose from the 1000+ news source inside Calibre, and configure Calibre to send them to "yourname@free.kindle.com". Then every morning, when you turn on the wifi on Kindle, the selected newspapers will be automatically downloaded to your Kindle. Isn't that amazing? Just remember that you have to keep Calibre running to get this done (kind of obvious).

Tuesday, December 6, 2011

Convert inkscape svg images to eps perserving latex formulas

1. Save Inkscape svg images to pdf+latex
2. Convert to eps using the following BASH script, which contains the following steps: (1) use pdflatex to generate pdf (2) use pdfcrop to crop the pdf (3) use pdftops to convert to eps.


This the header.tex:

This is the tail.tex:

The tool pdfcrop.pl can be downloaded from http://www.ctan.org/tex-archive/support/pdfcrop.

Friday, September 9, 2011

C++ namespace: global variables

Suppose you have a "mynamespace.h", which defines your own namespace called "mynamespace", and you have a "mynamespace.cpp", which includes the implementation. Then you include "mynamespace.h" in your main.cpp. You should avoid defining global variables in "mynamespace.h", such as

namespace mynamespace {
double length;
}

otherwise you will get errors like "multiple definition of ***" during the linking stage. What you should do, is to declare the global variables, for example

namespace mynamespace {
extern double length;
}

And then you define the global variables in "mynamespace.cpp".

Wednesday, August 31, 2011

How to merge several pdf files for printing

I have 20 pdf files to print. Is there a way to print all of them once, resulting in the same printouts as if I have printed them separately? I could use pdftk to combine the pdf files into one single pdf file, but some files have odd number of pages, which results in pages from two files printing on the same sheet of paper.

The solution is to pad an empty page to those files with odd number of pages, and then use pdftk to combine the pdf files. Here is the script (the file "emptypage.pdf" is just an empty page, and can be created using LaTeX).

Wednesday, June 22, 2011

Another reason for linear convergence of Newton's method

It's well-known that Newton's method has quadratic convergence if the function is well-behaved and the initial guess is close to the solution. However, sometimes we only get linear (slow) convergence.

One possible reason is that the derivative f' is singular at the solution x*. Here singular means f'=0 in 1D case or f' is a singular matrix in higher dimensions. In the 1D case, we can still get quadratic convergence if we use the iteration \(\triangle x = -m f(x_n)/f'(x_n)\) with m equal to the multiplicity of the root x*.

Another possible reason is that there is a bug in the computation of the right-hand-side \(f(x_n)\). If there is a deviance of \(O(h)\), Newton's method will also converge linearly.

Tuesday, March 29, 2011

How to determine the flat regime using R

Suppose the graph of (x,y) is first almost linear, then relatively flat, then almost linear again. How to find the middle flat regime?

# diff1 saves the difference of current value and predicted value using existing points from the left side

k<-length(x)
diff1<-c()
for(i in c(2:k)) {
tx<-x[1:i]
ty<-y[1:i]
tfitted<-glm(ty ~ tx)
diff1<-c(diff1, residuals(tfitted)[i])
}
diff1<-c(diff1[1],diff1)

# diff2 saves the difference of current value and predicted value using existing points from the right side
diff2<-c()
for(i in c((k-1):1)) {
tx<-x[i:k]
ty<-y[i:k]
tfitted<-glm(ty ~ tx)
diff2<-c(residuals(tfitted)[1], diff2)
}
diff2<-c(diff2,diff2[k-1])

Monday, March 28, 2011

How to plot curvature in R

tmp<-read.table("tmp.txt")
x<-tmp[,1]
y<-tmp[,2]
f<-y

# find 1st derivative using forward difference
k<-length(x)
y1<-c()
for(i in c(1:k-1)) {
t<-(y[i+1]-y[i])/(x[i+1]-x[i])
y1<-c(y1, t)
}
y1<-c(y1,t)
f1<-y1

# find 2nd derivative using backward difference of 1st derivative
y<-f1
y1<-c()
for(i in c(2:k)) {
t<-(y[i]-y[i-1])/(x[i]-x[i-1])
y1<-c(y1, t)
}
y1<-c(y1[1],y1)
f2<-y1

# find curvature and plot
kappa<-f2/(1+f1*f1)^1.5
plot(x,kappa)

-------------------------------------------------------------------------

We can also use the backward difference to approximate the 1st derivative:

tmp<-read.table("tmp.txt")
x<-tmp[,1]
y<-tmp[,2]
f<-y

# find 1st derivative using backward difference
k<-length(x)
y1<-c()
for(i in c(2:k)) {
t<-(y[i]-y[i-1])/(x[i]-x[i-1])
y1<-c(y1, t)
}
y1<-c(y1[1],y1)
f1<-y1

# find 2nd derivative using forward difference of 1st derivative
y<-f1
y1<-c()
for(i in c(1:k-1)) {
t<-(y[i+1]-y[i])/(x[i+1]-x[i])
y1<-c(y1, t)
}
y1<-c(y1,t)

f2<-y1

# find curvature and plot
kappa<-f2/(1+f1*f1)^1.5
plot(x,kappa)

-------------------------------------------------------------------------

Finally, we can use central difference to approximate both the 1st and 2nd derivative. Here we assume the mesh grid is uniform.

tmp<-read.table("tmp.txt")
x<-tmp[,1]
y<-tmp[,2]
f<-y

# find 1st derivative using central difference
k<-length(x)
y1<-c()
for(i in c(2:k-1)) {
t<-(y[i+1]-y[i-1])/(x[i+1]-x[i-1])
y1<-c(y1, t)
}
y1<-c(y1,t)
y1<-c(y1[1],y1)
f1<-y1

# find 2nd derivative using central difference
y<-f1
y1<-c()
for(i in c(2:k-1)) {
t<-(y[i+1]-2*y[i]+y[i-1])/(x[i+1]-x[i])^2
y1<-c(y1, t)
}
y1<-c(y1,t)
y1<-c(y1[1],y1)
f2<-y1

# find curvature and plot
kappa<-f2/(1+f1*f1)^1.5
plot(x,kappa)

Visitors