Another common trap is the precedence between == and & | ^
The expression a & b == 0 is interpreted as a & (b==0) instead of (a & b) == 0, quite different from a + b == 0.
Conclusion: always add brackets when evaluating the bitwise operators & | ^
Monday, October 29, 2007
Monday, October 01, 2007
Compile log: SRILM
SRILM is a language modelling package, used by the moses translation system. The compilation is quite straightforward, except two things:
1. The current path in the Makefile needs to be changed
2. In common/Makefile.machine.xxx, there are paths to gcc and g++.
In the first line, GCC_FLAGS might need editing as well. For example, -mtune=pentium3.
1. The current path in the Makefile needs to be changed
2. In common/Makefile.machine.xxx, there are paths to gcc and g++.
In the first line, GCC_FLAGS might need editing as well. For example, -mtune=pentium3.
Sunday, September 16, 2007
vim note: grep
The handy tool grep from Linux can also be used in VIM, by just typing ":grep PATTERN FILES" in the command mode. It finds the string according to the input pattern from all designated files. Note that FILES needs to be in the format of absolute path. Wildcard, such as "c:\\files\\*.txt" can be used. In version 7.0, "\\" is required for the path splitter under windows. "/" does not seem to work. Regular expression could be used for PATTERN.
By default, only the first search result is shown. Use ":cn" to navigate to the next search result, and use ":cp" to navigate to the previous result. The navigation can jump from one file to another, of course. When multiple buffers (or files) are opened, use ":bn" and ":bp" to jump from one file to the other. Use ":bd" to remove a file from buffers.
By default, only the first search result is shown. Use ":cn" to navigate to the next search result, and use ":cp" to navigate to the previous result. The navigation can jump from one file to another, of course. When multiple buffers (or files) are opened, use ":bn" and ":bp" to jump from one file to the other. Use ":bd" to remove a file from buffers.
Saturday, September 15, 2007
mod_python note: sessions
mod_python has its own modules for Session and Cookies. The mod_python documentation contains consise explanations about the usage. Cookies are used for session maintenance. An introduction can be found here.
Python supports cookies by a built in module, which is probably developed for CGI programming before adopted into Python core. mod_python can use this module as well for cookies.
Python supports cookies by a built in module, which is probably developed for CGI programming before adopted into Python core. mod_python can use this module as well for cookies.
Python note: using the windows clipboard
The windows clipboard allows HTML content to be processed. A specification about the specific format can be found here.
A python support for the windows clipboard is found in this site package.
A recipe for using the clipboard, written by Phillip Piper, can be found from this web page. I gave a simplified version of putting something into the clipboard at here.
A python support for the windows clipboard is found in this site package.
A recipe for using the clipboard, written by Phillip Piper, can be found from this web page. I gave a simplified version of putting something into the clipboard at here.
Sunday, September 09, 2007
Apach note: mod_python global objects
The term "global objects" in this article means the objects that exists from the starting of the web server until the shutdown. An example of such global objects is a database proxy, which is initialized at server start, and handles database calls during all the serving session.
At the first look, it appeared to be no place for defining global objects, for mod_python runs on the per-request basis, with each request being mapped to a call.
However, mod_python has the advantage over CGI that a python interpreter is created for one virtual server to handle all requests to server. The interpreter starts when the server starts, and last until the server is shut down. Here is a reference for the multiple interpreter mechanism for the mod_python module.
To take this advantage, global variables can be place in the global namespace for the Python interpreter. For example, it could be intitilized from a module's namespace.
At the first look, it appeared to be no place for defining global objects, for mod_python runs on the per-request basis, with each request being mapped to a call.
However, mod_python has the advantage over CGI that a python interpreter is created for one virtual server to handle all requests to server. The interpreter starts when the server starts, and last until the server is shut down. Here is a reference for the multiple interpreter mechanism for the mod_python module.
To take this advantage, global variables can be place in the global namespace for the Python interpreter. For example, it could be intitilized from a module's namespace.
Apache note: set up a mod_python server
Though the building of mod_python can be a little daunting under UNIX, it's quite easy to start a mod_python server with apache under the windows platform.
Suppose that a machine has python installed.
First download the apache http server from the apache web site. (you need to find the download site by following the links from the main site) Current version is 2.2.
Second download the mod_python binaries for windows from the mod_python web site. Current version is 3.3
Third install apache following the instructions.
Fourth install mod_python following the instructions. The information in the last page is important. The LoadModule command must be added to the httpd.conf file for apache so that the http server recognizes mod_python.
The installation test can be found the documentation Note that http://127.0.0.1/test/*.py will work. This is because the request url is handed to mptest.py as parameters.
Suppose that a machine has python installed.
First download the apache http server from the apache web site. (you need to find the download site by following the links from the main site) Current version is 2.2.
Second download the mod_python binaries for windows from the mod_python web site. Current version is 3.3
Third install apache following the instructions.
Fourth install mod_python following the instructions. The information in the last page is important. The LoadModule command must be added to the httpd.conf file for apache so that the http server recognizes mod_python.
The installation test can be found the documentation Note that http://127.0.0.1/test/*.py will work. This is because the request url is handed to mptest.py as parameters.
Latex note: how to shrink tables
To reduce the size of a table, two general methods can be used.
1. Shrink the the font size.
For example, use \small{content} instead of content in the table cell.
2. Squeeze space between columns.
For example, a table could be written in the following way
\begin{tabular}
\setlength{\tabcolsep}{1pt}
...
\end{tabular}
1. Shrink the the font size.
For example, use \small{content} instead of content in the table cell.
2. Squeeze space between columns.
For example, a table could be written in the following way
\begin{tabular}
\setlength{\tabcolsep}{1pt}
...
\end{tabular}
Saturday, August 25, 2007
C++ note: unsigned int
Suppose a is an unsigned int, the expression a>-1 is always false. This is because the type of -1 is decided by the type of a, and it becomes maxint.
Hint: remember the unsigned int variables in the code and do not compare them with a minus number. The comparison itself is not necessary.
Hint: remember the unsigned int variables in the code and do not compare them with a minus number. The comparison itself is not necessary.
Wednesday, August 01, 2007
Linux note: how to disable output
One way is adding a redirection to the command to execute:
command >/dev/null
In the above line, command is executed with its output redirected to /dev/null.
More notes about the above syntax. ">" is the operator to direct the output of command to a file. In this case, the file is the special device /dev/null. The standard output (using ">") and the error output ("2>") can both be redirected. To redirect the standard output and the error output at the same time, use ("&>"), and to append the redirected output to a file, use (">>").
Disabling output is necessary in many situations. For example, when using nohup, the system output is put into the file nohup.out. If the disk space is limited, we may want this file to be as small as possible. Therefore, we may put necessary information into the error output, and redirect the standard output to null.
command >/dev/null
In the above line, command is executed with its output redirected to /dev/null.
More notes about the above syntax. ">" is the operator to direct the output of command to a file. In this case, the file is the special device /dev/null. The standard output (using ">") and the error output ("2>") can both be redirected. To redirect the standard output and the error output at the same time, use ("&>"), and to append the redirected output to a file, use (">>").
Disabling output is necessary in many situations. For example, when using nohup, the system output is put into the file nohup.out. If the disk space is limited, we may want this file to be as small as possible. Therefore, we may put necessary information into the error output, and redirect the standard output to null.
Saturday, June 16, 2007
VIM note: viewport
It's sometimes necessary to split the view of a file into many windows. Below is a cheatsheet copied from here.
:sp will split the Vim window horizontally. Can be written out entirely as :split .
:vsp will split the Vim window vertically. Can be written out as :vsplit .
Ctrl-w Ctrl-w moves between Vim viewports.
Ctrl-w j moves one viewport down.
Ctrl-w k moves one viewport up.
Ctrl-w h moves one viewport to the left.
Ctrl-w l moves one viewport to the right.
Ctrl-w = tells Vim to resize viewports to be of equal size.
Ctrl-w - reduce active viewport by one line.
Ctrl-w + increase active viewport by one line.
Ctrl-w q will close the active window.
Ctrl-w r will rotate windows to the right.
Ctrl-w R will rotate windows to the left.
:sp will split the Vim window horizontally. Can be written out entirely as :split .
:vsp will split the Vim window vertically. Can be written out as :vsplit .
Ctrl-w Ctrl-w moves between Vim viewports.
Ctrl-w j moves one viewport down.
Ctrl-w k moves one viewport up.
Ctrl-w h moves one viewport to the left.
Ctrl-w l moves one viewport to the right.
Ctrl-w = tells Vim to resize viewports to be of equal size.
Ctrl-w - reduce active viewport by one line.
Ctrl-w + increase active viewport by one line.
Ctrl-w q will close the active window.
Ctrl-w r will rotate windows to the right.
Ctrl-w R will rotate windows to the left.
Tuesday, June 12, 2007
C++ note: mix with C code
The easiest way to make use of C modules is including the header files with
extern "C"
{
#include <a_c_module.h>
}
Then the C++ compiler knows that the header defines the structures in the c way, and will compile references in the C++ module in a compatible way.
For the standard libraries name "cblabla", extern "C" is already included in the header files. This is the reason why modules etc can be included directly.
For the linker, different vendors might have different standards to mangle the refernece names. Therefore, it needs to be confirmed that the C modules are compiled in a compatible way as the C++ module.
Making use of C++ modules from C modules is harder than the other way around. For the C++ programmers, it might be possible to turn some necessary C files into the C++ format and then compile the whole project as C++. Many files can be kept as the C modules and linked explicitly.
extern "C"
{
#include <a_c_module.h>
}
Then the C++ compiler knows that the header defines the structures in the c way, and will compile references in the C++ module in a compatible way.
For the standard libraries name "cblabla", extern "C" is already included in the header files. This is the reason why modules
For the linker, different vendors might have different standards to mangle the refernece names. Therefore, it needs to be confirmed that the C modules are compiled in a compatible way as the C++ module.
Making use of C++ modules from C modules is harder than the other way around. For the C++ programmers, it might be possible to turn some necessary C files into the C++ format and then compile the whole project as C++. Many files can be kept as the C modules and linked explicitly.
Monday, June 04, 2007
Javascript note: about the regular expressions
Javascript supports regular expressions as a built in language feature. Regular expressions are enclosed between two "/".
Here is a tutorial.
Here is a tutorial.
Tuesday, May 29, 2007
Thursday, May 24, 2007
C++ note: static variables
C++ is different from Java and csharp in the way classes are defined. The header files in C++ are not actually compiled into the object - they are only an indicator of how the data structures are organized, i.e. the reference to a member is the base address plus how much offside. All variables and functions to appear inside objects must be defined in the cpp files. The methods in header files are treated as inline methods.
Now static variables are those that occur in the heap. They exist from the beginning until the finishing of the running of program. All variables defined in a cpp file are static variables, they are by default external linked. If a variable defined in a cpp file has explicit "static" modifier, it is internal linked and can only be accessed by the module. Static variables can also be defined inside functions and methods, in which case they are still present all through the running of a program; however, they can only be accessed from the name scope in which they are defined. The accessibility is the only difference between static variables. Lastly, constants in header files are also taken as static variables. This is why values can be assigned to them in the header files.
Now static variables are those that occur in the heap. They exist from the beginning until the finishing of the running of program. All variables defined in a cpp file are static variables, they are by default external linked. If a variable defined in a cpp file has explicit "static" modifier, it is internal linked and can only be accessed by the module. Static variables can also be defined inside functions and methods, in which case they are still present all through the running of a program; however, they can only be accessed from the name scope in which they are defined. The accessibility is the only difference between static variables. Lastly, constants in header files are also taken as static variables. This is why values can be assigned to them in the header files.
Tuesday, May 22, 2007
g++ note: don't forget -c option
I made a mistake when writing Makefile today, by missing to put -c for the compile-only step. It led to a lot of unresolved external references.
Monday, May 14, 2007
Latex note: generating letter size papers
latex paper
dvips -t letterSize -Ppdf -G0 paper
ps2pdf -sPAPERSIZE=letter paper.ps
If the last step doesn't work, try the following command:
ps2pdf paper.ps
It should work because the ps file already contains size information.
dvips -t letterSize -Ppdf -G0 paper
ps2pdf -sPAPERSIZE=letter paper.ps
If the last step doesn't work, try the following command:
ps2pdf paper.ps
It should work because the ps file already contains size information.
Thursday, April 12, 2007
Latex note: more on making EPS graphs from MS Office
Please see my previous article about how to generate an eps chart from from MS Excel.
You can also insert Word pictures into eps. The mechanism is the same as Excel. First, make an empty word document or page to draw the vector graphs. Then print the page, using general eps printer. Lastly, modify the page bounding box of the eps file so that it fits the graph.
There is one thing to notice though: the coordinate counts from bottom left to top right! If the four numbers represent top left and bottom right, the picture will still show in latex document, but it will be sqeezed together with text because the size is below zero!
You can also insert Word pictures into eps. The mechanism is the same as Excel. First, make an empty word document or page to draw the vector graphs. Then print the page, using general eps printer. Lastly, modify the page bounding box of the eps file so that it fits the graph.
There is one thing to notice though: the coordinate counts from bottom left to top right! If the four numbers represent top left and bottom right, the picture will still show in latex document, but it will be sqeezed together with text because the size is below zero!
Latex note: use minipage to align a figure of text to center, while keeping text aligned to left
Sometimes we need to align text with multiple lines to the center of page.
\begin{figure}[t]
\centering
this is first line \\
\hspace*{0.5cm} this is second line \\
this is third line \\
this is fourth line
\caption{caption}
\label{l}
\end{figure}
The above may not work because all lines are aligned to center, and they look messy on the left border. The solution is using a minipage to align text inside figure.
\begin{figure}[t]
\centering
\begin{minipage}[t]{0.5\textwidth}
this is first line \\
\hspace*{0.5cm} this is second line \\
this is third line \\
this is fourth line
\end{minipage}
\caption{caption}
\label{l}
\end{figure}
\begin{figure}[t]
\centering
this is first line \\
\hspace*{0.5cm} this is second line \\
this is third line \\
this is fourth line
\caption{caption}
\label{l}
\end{figure}
The above may not work because all lines are aligned to center, and they look messy on the left border. The solution is using a minipage to align text inside figure.
\begin{figure}[t]
\centering
\begin{minipage}[t]{0.5\textwidth}
this is first line \\
\hspace*{0.5cm} this is second line \\
this is third line \\
this is fourth line
\end{minipage}
\caption{caption}
\label{l}
\end{figure}
Wednesday, April 11, 2007
Latex note: protect citations in caption
\caption{xxx \cite{yyy}} won't work, it won't compile with latex sometimes.
use \caption{xxx {\protect \cite{yyy}}} instead.
use \caption{xxx {\protect \cite{yyy}}} instead.
Subscribe to:
Posts (Atom)