<pre class="languageName:nocontrols" name="code">
code
</pre>
indent paragraph:
<div class="indentparagraph">
content goes here
</div>
Monday, January 28, 2008
A Groovy sample
Sometimes, I use MS visual studio to write programs. MS VS automatically generates additional files and directories which are not necessary when I release my code. For example, debug directory and .ncb file. When I finish my program, I want to delete these unnecessary files/directories. To delete them manually is not a good choice considering number of programs I have. So I decide to write a Groovy script to do this task and following is my code.
/**
* This Groovy script is used to delete directories named 'debug'.
* Usually, this directory is generated by MS Visual Studio when
* you compile your code in debug mode. After you release your
* code, debug information can be discarded. Or else, it occupies
* non-trivial amount of hard disk space.
* Moreover, MS VS generates .ncb files.
* Besides debug directory and .ncb file, you can easily change
* the pattern of files/directories you want to delete.
*/
def baseDir = 'E:\\my_program'
dirPattern = /[d|D][e|E][b|B][u|U][g|G]/
fileNamePattern = /.*\.ncb$/
/** This closure delete a specified directory or file
* Note: here File means a regular file or a directory.
*/
deleteFile= {
currentObj ->
if( currentObj.isDirectory() ){
currentDir.eachFile{
currentFile ->
if( currentFile.isFile() ){
//first file
if( currentFile.delete() == false )
println "file " + currentFile + " failed to be deleted"
}else if( currentFile.isDirectory() ){
//then recursively delete dirs
deleteDir( currentFile )
}
}
}
//delete this dir
if( currentObj.delete() == false )
println "dir " + currentDir + " failed to be deleted"
}
/** delele directories corresponding to pattern specified by variable
* dirPattern.
*/
deleteDirs ={
_baseDir ->
_baseDir.eachDir{
currentDir ->
def dirName = currentDir.getName()
if( dirName ==~ dirPattern){
//find it, just delete it
deleteFile(currentDir)
}else//recursivly look for it
deleteDirs(currentDir)
}
}
/** delele files corresponding to pattern specified by variable
* fileNamePattern.
*/
deleteFiles ={
_baseDir ->
_baseDir.eachFile{
currentFile ->
if( currentFile.isFile() ){
def fileName = currentFile.getName()
println fileName
if( fileName ==~ fileNamePattern ){
//find it, just delete it
deleteFile(currentFile)
}
}else//recursivly look for it
deleteFiles(currentFile)
}
}
/** Sample usage */
deleteDirs(new File(baseDir))
deleteFiles(new File(baseDir));
Tuesday, January 22, 2008
Matlab
Some issues in the course of using Matlab:
(1) random number generation
functions:
rand:
generate random numbers in range [0, 1];
randn:
Random numbers are drawn from a normal distribution with mean zero and standard deviation one. So generated numbers may be negative.
Usage hints:
In description above, those functions ouput decimal numbers. If you want to generate integers in range, following usage may be useful:
output = ceil(max.*rand(n1, n2, ...ni))
output = min + ceil((max-min).*rand(n1,n2,...ni))
Some key points from Matlab help:
The sequence of numbers produced by RAND is determined by the internal state of the generator. Setting the generator to the same fixed state allows computations to be repeated. Setting the generator to different states leads to unique computations, however, it does not improve any statistical properties. Since MATLAB resets the state at start-up, RAND will generate the same sequence of numbers in each session unless the state is changed.
So, it is a better to set the initial state of random number generator before random numbers are generated. This can be done in following format:
rand('state', sum(100*clock));
More random number generation functions which use specific statistic models:
betarnd 贝塔分布的随机数生成器
binornd 二项分布的随机数生成器
chi2rnd 卡方分布的随机数生成器
exprnd 指数分布的随机数生成器
frnd f分布的随机数生成器
gamrnd 伽玛分布的随机数生成器
geornd 几何分布的随机数生成器
hygernd 超几何分布的随机数生成器
lognrnd 对数正态分布的随机数生成器
nbinrnd 负二项分布的随机数生成器
ncfrnd 非中心f分布的随机数生成器
nctrnd 非中心t分布的随机数生成器
ncx2rnd 非中心卡方分布的随机数生成器
normrnd 正态(高斯)分布的随机数生成器
poissrnd 泊松分布的随机数生成器
raylrnd 瑞利分布的随机数生成器
trnd 学生氏t分布的随机数生成器
unidrnd 离散均匀分布的随机数生成器
unifrnd 连续均匀分布的随机数生成器
weibrnd 威布尔分布的随机数生成器
(2)permutation
This topic is related to how to generate permutations.
functions:
(1) randperm(n)
a random permutation of the integers 1:n.
(3) Dimension rearrangement
functions:
(1) permute(A, dim_order)
This function is useful when you want to adjust order of different dimensions.
(2) ipermute(A, dim_order)
Inverse permute the dimensions of an array.
If B = ipermute(A, dim_order) then A=permute(B,dim_order).
(4) Matrix metadata retrieval
functions:
(1) ndims
Number of array dimensions. It is equal to length(size(array)).
(2) size
(5) Matrix data retrieval
functions:
(1) find
(2) nonzeros
(3) min
(4) max
(6) Array index conversion
(1) sub2ind
(2) ind2sub
(7) Array generation
(1) zeros
(2) ones
(3) linspace
(4) logspace
(8) Array transformation
(1) reshapce
(2) shiftdim
(3) circshift
(4) squeeze
(5) repmat
(9) Bitwise operations
(1) bitshift
(10) Image related
(1) imread, imwrite
(11) color related
(1) rgb2gray, rgb2hsv, rgb2ind, rgb2ntsc, rgb2ycbcr
Sunday, January 06, 2008
Probabilistic algorithms
Recently, I read some articles which describe probabilistic(randomized) algorithms I have not heard of before. This cluster of algorithms are really interesting. There are four main categories: Monte Carlo algorithms, Las Vegas algorithms, Sherwood algorithms and Numerical approximation algorithms.
This article(http://www.cs.man.ac.uk/~david/courses/advalgorithms/probabilistic.pdf) describes probabilistic algorithms with concrete examples. It is a good tutorial for newbies.
Here, I would like to excerpt part of the original article.
(1) Monte Carlo algorithms
"Algorithms which always return a result, but the result may not always be correct. We attempt to minimise the probability of an incorrect result, and using the random element, multiple runs of the algorithm will reduce the probability of incorrect results."
Examples:
(*)Majority element in an array
(*)Matrix multiplication(whether A*B=C)
(*)Testing the primality of numbers
(*)Deciding set equality
(2) Las Vegas algorithms
"Algorithms that never return an incorrect result, but may not produce results at all on some runs. Again, we wish to minimise the probability of no result, and, because of the random element, multiple runs will reduce the probability of no result. "
Examples:
(*) Eight Queens Problem
(*) Factorization of integers
(3) Sherwood algorithms
"Algorithms which always return a result and the correct result, but where a random element increases the eciency, by avoiding or reducing the probability of worst-case behaviour. Useful for algorithms which have a poor worst-case behaviour but a good average-case behaviour."
Examples:
(*)Quicksort
choose a random pivot or randomly shuffle the input numbers.
(4)Numerical approximation algorithms
"Here the random element allows us to get approximate numerical results, often much faster than direct methods, and multiple runs will provide increasing approximation."
Examples:
(*) Calculation of integrals
Start to use this blog as well
From now on, some articles will be posted at this blog. Most posts will be related to computer science.
