Shortcuts for Ubuntu Shell

Posted March 19, 2013 by Lego
Categories: Ubuntu Shell

Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Go to the end of the line you are currently typing on
Ctrl + L Clears the Screen, similar to the clear command
Ctrl + U / Ctrl + K Clears the line before/after the cursor position.
Ctrl + W / Alt + D Delete the word before/after the cursor
Ctrl + R Let’s you search through previously used commands
Ctrl + C Kill whatever you are running
Ctrl + D Exit the current shell
Ctrl + Y Insert pasted stuff
Alt + Y Insert previously pasted stuff
Ctrl + T Swap the last two characters before the cursor
Esc + T Swap the last two words before the cursor
Alt + F Move cursor forward one word on the current line
Alt + B Move cursor backward one word on the current line
Tab Auto-complete files and folder names

mv T*.txt, 使用 C-x * 显示所有名为 T*.txt 的文件

Advertisements

Ubuntu Configuration

Posted March 16, 2013 by Lego
Categories: Ubuntu Shell

1. Shell 提示符字体颜色改变。设置环境变量PS1 by adding the following sentences to ~/.bashrc

# 修改Shell提示符颜色
PS1=”\[\e[32m\]\u@\h:\w$ \[\e[m\]”

2. When copy files from Windows to Ubuntu using SSH Secure Shell, Chinese characters are not displayed properly and are regarded as invalid encoding. This is because in Ubuntu encoding type is UTF-8 but windows is GBK.

Solution:
1. install convmv by: sudo apt-get install convmv
2. convert all invalid file names under current directory by:
convmv -f GBK -t UTF-8 –notest -r ./

C/C++ Compilation Command in Unix

Posted March 15, 2013 by Lego
Categories: C/C++

gcc -Wall -g -lm main.c -o main

gdb main

./main

Shell Script

Posted January 28, 2013 by Lego
Categories: Ubuntu Shell

1. 如果将命令行下输入的命令用()括号括起来,那么也会fork出一个子Shell执行小括号中的命令,一行中可以输入由分号;隔开的多个命令,比如:
$ (cd ..;ls -l)
cd ..命令改变的是子Shell的PWD,而不会影响到交互式Shell。然而命令
$ cd ..;ls -l
则有不同的效果,cd ..命令是直接在交互式Shell下执行的,改变交互式Shell的PWD,然而这种方式相当于这样执行Shell脚本:
$ source ./script.sh 或者 $ .  ./script.sh
source或者.命令是Shell的内建命令,这种方式也不会创建子Shell,而是直接在交互式Shell下逐行执行脚本中的命令。

2. 文件名代换(Globbing) *?[]

3. 命令代换 反引号`或$()

4. 算术代换 $(())

5.  比如,要将当前目录下的chap0chap1chap2等文件名改为chap0~chap1~chap2~等(按惯例,末尾有~字符的文件名表示临时文件),这个命令可以这样写:

$ for FILENAME in chap?; do mv $FILENAME $FILENAME~; done

也可以这样写:

$ for FILENAME in `ls chap?`; do mv $FILENAME $FILENAME~; done

6. Bash也可以通过键入Esc、Tab键来完成对历史事件的字符扩展。和标准命令行扩展特性一样,键入历史事件的部分字符串,然后键入Esc,再键入Tab键,与刚才键入的字符串相匹配的历史事件将自动扩展并回显到命令行处。

[1] 详细介绍Linux shell脚本基础学习 http://os.51cto.com/art/201003/186816.htm

[2] linux 标准输入/输出和重定向 (shell命令) http://hi.baidu.com/dosttyy/item/52f8230a82c34c12cc34ea9e


	

EMACS Shortcut

Posted January 14, 2013 by Lego
Categories: EMACS

C/C++模式下

  • M-x c-set-style 设置缩进风格
  • C-M-h: mark function
  • C-M-a, C-M-e: beginning/end of function
  • C-x ;      set comment column 与 M-; 配合使用

一般情况下

  • M-/: auto complete word
  • M-^: delete indentation
  • C-M-\, C-M-q: indent region, indent expression
  • C-x h: mark whole buffer
  • To quit a partially entered command, type C-g or press Esc 3 times. Use C-g to stop a command which is taking too long to execute.
  • C-u The way you give a command a repeat count is by typing C-u and then the digits before you type the command. Ex 1, C-u 8 C-f. Ex 2, Type C-u 8 * to insert ********
  • M-< or M-> Move to the beginning or end of the current file. (you must use shift key for < or >)
  • Undo: C-x u or C-_ or C-/
  • C-x o or C-x 5 o Give another window or frame focus; C-M-v scroll another window when the cursor is in the current window
  • C-x 4 C-f Find file in other window
  • C-q TAB
  • Incremental Search: C-s, C-w, C-g, M-p/M-n, M-y to paste text to your search string, C-y to append text till the end of line to string.
  • Nonincremental Search: C-s RET
  • Regular Expression Search: C-M-s/C-M-r
  • Query Replace: M-% then press y, n, ! or comma. ^ (the caret character)–This moves the cursor back to the previous replacement. See Sam’s book. 
  • M-x speedbar
  • Rectangular Operation: C-x r C-SPC, C-x r C-x, C-x r C-w, C-x r M-w. There is mouse support for rectangle highlighting by dragging the mouse while holding down the shift key.
  • C-f, C-b; C-p, C-n; M-f, M-b; C-right, C-left; C-d, Delback; M-d, M-Delback; C-k, M-k;
  • M-m Back to indentation; M-{, M-} Move to the beginning/end of paragraph;
  • C-M-a, C-M-e Move to the beginning/end of function; C-M-f, C-M-b forward/backward sexp;
  • C-t, M-t; M-c, M-u, M-l;
  • C-h l View lossage to see what keys you have pressed
  • C-l
  • help: C-h c; C-h k; C-h f; C-h m To get a list of keybindings for major mode;
  • Fill: M-x auto-fill-mode; Use C-x f to set the margin; M-q to fill-region or fill-paragraph; C-x . To set-fill-prefix
    If you dislike adaptive filling, you can disable it by inserting the following: (setq adaptive-fill-mode nil)
    Ref 1. http://docs.huihoo.com/homepage/shredderyin/emacs_fill.html
    Ref 2. Sam’s book on Hour 12 Filling

Shortcut for Latex

  • C-c C-u 到最近的上一级 \begin{…} 处。
  • C-c } 到最近的上一级匹配 {…} 处。

EMACS “.emacs” Configuration

Posted January 14, 2013 by Lego
Categories: EMACS

From Sams Book:

  • Enhanced page up and page down scrolling. It contains two group of functions by requiring the pager package.
  • require redo package for undo and redo
  • (global-set-key [(meta left)] ‘backward-sexp), (global-set-key [(meta right)] ‘forward-sexp) – These two functions for going to the matching starting or ending brace. 
  • (global-set-key [(meta g)] ‘goto-line)
  • (global-set-key [(meta q)] ‘sams-fill) (setq adaptive-fill-mode nil)

From Yin Wang’s (王垠) notes:

  • (require ‘session) (add-hook ‘after-init-hook ‘session-initialize)
    使用了这个扩展之后,你上次离开 Emacs 时的全局变量 (kill-ring,命令记录……),局部变量,寄存器,打开的文件,修 改过的文件和最后修改的位置,…… 全部都会被记录下来。增加后菜单会多出两项:最近访问过的文件,最近修改过的文件。
  •  (desktop-save-mode 1) enables automatic saving of the desktop when you exit Emacs, and automatic restoration of the last saved desktop when Emacs starts.
  • 临时记号: C-. 时就做了一个记号。然后可以到别处,按 C-, 就可以在这两点之 间来回跳转了
  • go-to-char: 有了这段代码之后,当按 C-c a x (x 是任意一个字符) 时,光 标就会到下一个 x 处。再次按 x,光标就到下一个 x。比如 C-c a w w w w …, C-c a b b b b b b …
  • %: 括号匹配,当 % 在括号上按下时,那么匹配括号,否则输 入一个 %
  • setnu – 显示文件行号。M-x setnu-mode toggles the line number mode on and off. Example: (add-hook ‘text-mode-hook ‘turn-on-setnu-mode) to automatically turn on line numbering when enterting text-mode.
  • AUCTeX, preview-latex等等更多包,请见王垠的配置

Fast Dual-Based Linearized Bregman Algorithm

Posted January 12, 2013 by Lego
Categories: Uncategorized

Linearized Bregman (LB) algorithm is known for its efficiency in solving the following problem

min \|\mathbf{x}\|_1 s.t. \mathbf{A}\mathbf{x}=\mathbf{b},             (1)

especially when the problem considered is of very large-scale. Recently, Yin has shown in [1] that the LB method is equivalent to a gradient descent algorithm applied to the Lagrangian dual of

min \|\mathbf{x}\|_1+\dfrac{1}{2\mu} \|\mathbf{x}\|^2 s.t. \mathbf{A}\mathbf{x}=\mathbf{b}     (2)

When \mu is large, problem (2) can be regarded as equivalent to problem (1), and we can solve (2) in order to obtain the minimizer for (1). The Lagrangian dual of (2) assumes the form

min E(\mathbf{y}) = -\{\|\tilde{\mathbf{x}}\|_1 + \dfrac{1}{2\mu} \|\tilde{\mathbf{x}}\|^2 -<\mathbf{y},\mathbf{A}\tilde{\mathbf{x}}-\mathbf{b}>\}   (3)

where

\tilde{\mathbf{x}}=argmin \{\|\mathbf{x}\|_1+\dfrac{1}{2\mu}\|\mathbf{x}\|^2\}-<\mathbf{y},\mathbf{A}\mathbf{x}-\mathbf{b}>\}

To be clear, we call the gradient descent algorithm applied to the Lagrangian dual (3) as the Dual-Based LB Algorithm, which is equivalent to the LB method as mentioned earlier.

In our paper published in IASTED-VIIP [2], we show that the Dual-Based LB Algorithm possesses a convergence rate of O(1/k). Further, inspired by Beck and Teboulle [3], we propose a fast iteration scheme by carrying out FISTA type of iterations in the dual space (3), denoted as Fast Dual-Based LB Algorithm. We proved that the convergence rate of the fast method is O(1/k^2). For readers interested to know more, please refer to [2] for algorithmic details and convergence proof.

Let’s apply the fast algorithm for compressive sensing of a synthetic image \mathbf{X}^* of size 512 by 512, which was produced by retaining its K = 7*10^3 largest wavelet coefficients of an original image known as “man”. Thus \mathbf{X}^* is sparse in the wavelet domain as 97.33% of its wavelet coefficients are zeros. Next we adopted a sampling matrix \mathbf{A} which was a partial 2-D DCT matrix with randomly chosen rows of size M \times N with M = \lceil 0.2N\rceil and N = 512^2.The CS reconstructed image with 20% of DCT sampled coefficients using Fast Dual-Based LB is illustrated as follows

recovered_synman512

The difference between the reconstructed image and the original image is hardly noticeable. It took the proposed fast algorithm 217 iterations (41.1 seconds) to converge to a relative contraint error \|\mathbf{A}\mathbf{x}^k-\mathbf{b}\|/\|\mathbf{b}\| < 10^{-2}. The relative reconstruction error measured by (\|\mathbf{X}_p-\mathbf{X}^*\|)/\|\mathbf{X}^*\| was found to be 0.0116.

By comparison, a total of 3168 iterations (601.6 seconds) were needed for the conventional LB algorithm to produced a reconstructed image of relative reconstruction error 0.0118.

As was demonstrated by this example, the fast algorithm is almost 15 times faster than the conventional method.

[1]  W. Yin, “Analysis and generalizations of the linearized Bregman method,” SIAM Journal on Imaging Sciences, vol. 3, no. 4, pp. 856–877, 2010.
[2] J. Yan and W.-S. Lu, “Fast dual-based linearized Bregman algorithm for compressive sensing of digital images,” Proceedings of the IASTED International Conference on Visualization, Imaging and Image Processing (VIIP 2012), pp. 43-49, Banff, Canada, July 2012.
[3]  A. Beck and M. Teboulle, “A fast iterative shrinkage thresholding algorithm for linear inverse problems,” SIAM Journal on Imaging Sciences, vol. 2, no. 1, pp. 183–202, 2009.