Saturday, October 27, 2007

Linux programming

# gcc
gcc -o cahn cahn.c
gcc -o cahn cahn.c -lm /* mathematical library */

gcc -o p2 p2.cpp -lstdc++
g++ -o p2 p2.cpp
ldd p2

gcc -I/usr/openwin/include cahn.c /* include header files in subdirectories or nonstandard places by the -I flag */

gcc -o x11cahn -L/usr/openwin/lib x11cahn.c -lx11 /* compile and link a program "x11cahn" using the version of the library "libx11" found in the /usr/openwin/lib */

# Make a libaray
ar crv libfoo.a bill.o fred.o /* creat the archive and add object files to it */

ranlib libfoo.a /* no need in Linux */

gcc -o program program.c libfoo.a
gcc -o program program.c -L. -lfoo /* libfoo is in the current directory */

grep EXIT_ *.h /* search all the files in *.h for the string EXIT_ */

man gcc
info gcc


# Install GSL
./configure ; make; make check; make install

# Complie & Link
g++ -Wall -I/usr/local/include -c example.cpp
or
g++ -Wall -c example.cpp


g++ -L/usr/local/lib example.o -lgsl -lgslcblas
or
g++ -o example example.o -lgsl -lgslcblas

g++ -o example example.cpp -lgsl -lgslcblas

# GSL in baobab
gcc -o example -I/afs/isis/pkg/gsl/include example.c
-L/afs/isis/pkg/gsl//lib -lgsl -lgslcblas

export LD_LIBRARY_PATH=/afs/isis/pkg/gsl/lib:${LD_LIBRARY_PATH}
or put "export ~" at ~/public/.kshrc.personal

Linux

su : switch to the root user

scp brcpc01.umbc.edu:~/analysis/output/parser.zip . /* download */
scp *.* hiv@bios.unc.edu:~/doc /* upload */

top : task check

yum list available
yum install packagename
yum update packagename
yum check-update
yum search

rpm -Uvh filename.rpm : install
rpm -ivh filename.rpm : install the package already installed
rpm-ivh --replacefiles filename.rmp : conflicting files
rpm -qa | grep R : check if R is installed or not
rpm -e foo : uninstall foo

rm -r cahn/ : deltree the directory cahn/
rm -rf cahn/ : deltree the directory cahn/

tar -cf archive.tar foo bar : Create archive.tar from files foo and bar.
tar -tvf archive.tar : List all files in archive.tar verbosely.
tar -xf archive.tar : Extract all files from archive.tar.
tar cvf cahn.tar cahn/ : cahn/ directory and all its subdirectories -> cahn.tar

gzip filename : compress a file as filename.gz
gunzip filename : uncompress
gzip -d filename : uncompress
gzip -r filename.gz file1 file2 file3 /cahn/redhat

zip -r filename.zip filesdir
upzip filename.zip
zip -r filename.zip file1 file2 file3 /cahn/redhat

vim .bash_profile : to add the directory to my PATH
source .bash_profile : activation

cat text_file_name : view
cat > filename : make text file, [Ctrl]-[D] to quit
cat f1 f2 > f2 : concatenate files
cat f1 >> f2 : append f1 to f2

head -20 filename
tail -20 filename
grep tar manual : find "tar" in the file "manual"

/usr/share/doc : documentation for packages installed
mount /mnt/cdrom
umount /mnt/cdrom


man ls : manual for the command "ls"
ls : -a, -l, -F(file type),-r(reverse), -R(recursive), -s(size)
ls | more, ls | less

locate finger : locate the files and directories with the word "finger"
history | grep man : a history of all the commands with the word "man"


# Access to the hard in Windows system
Accessing a Windows Partition (should be FAT32)
login as root (su)
mkdir /mnt/data
mount -t vfat /dev/hda5 /mnt/data
cd /mnt/data

To automatically mount a windows partition every time
login as root (su)
vim /etc/fstab

Add the following on a new line (replacing /dev/hda1 with the Windows partition you found via Hardware Browser):
/dev/hda5 /mnt/data vfat auto,utf8,umask=0 0 0

# Change the default boot
su
cd /etc
vim grub.conf
default=0 # 0 -> 1

# Chage the default system session to KDE
edit /etc/sysconfig/desktop

vim

x - delete a character
dd - delete a line
i - insert mode
esc - escape from insert mode
yy - copy a line
sift p - paste
: - go to command line
w - save
sq -exit and save
q! - exit without saving
hjkl - move
^f - forward
^b - backward

:%s/,//g - change all ',' to ''
:%s/^M//g - ^M (control-v M)

:/X - search 'X'
n - next search
^g - where I am

Thursday, October 25, 2007

Matlab/Octave - Vector and Matrix

# Vector
v = [1 2 3 4] - row vector
v = 1:4
v = [1; 2; 3; 4] - column vector;
v = [1:4]' - transpose

# Matrix
A = [1 2; 3 4]
b = [5; 6]
M = [A b]
eye(3), eye(4,3) - diagonal = 1, otherwise 0
zeros(5,3)
ones(5,3)
rand(5,3) - uniform distribution

Mathematica - Continuous Distributions

# Call package
<< Statistics`ContinuousDistributions`

# Distrubutions
NormalDistribution[μ, σ]
ChiSquareDistribution[n]
StudentTDistribution[n]
FRatioDistribution[n1, n2]
UniformDistribution[min, max]
ExponentialDistribution[lambda]
GammaDistribution[alpha, lambda]
BetaDistribution[alpha, beta]
WeibullDistribution[alpha, beta]
LogisticDistribution[mu, beta]
LogNormalDistribution[mu, sigma]
CauchyDistribution[a, b]
ChiDistribution[n]
ExtremeValueDistribution[alpha, beta]
HalfNormalDistribution[theta]
LaplaceDistribution[mu, beta]
ParetoDistribution[k, alpha]
RayleighDistribution[sigma]
NoncentralChiSquareDistribution[n, lambda]
NoncentralStudentTDistribution[n, lambda]
NoncentralFRatioDistribution[n1,n2, lambda]

# Functions
PDF[dist, x]
CDF[dist, x]
Quantile[dist, q]
Domain[dist]
Mean[dist]
Variance[dist]
StandardDeviation[dist]
Skewness[dist]
Kurtosis[dist]
KurtosisExcess[dist]
CharacteristicFunction[dist, t]
ExpectedValue[f, dist]
ExpectedValue[f, dist, x]
Random[dist]
RandomArray[dist, dims]

# example
RandomArray[NormalDistribution[3, 1], 10]
Quantile[NormalDistribution[0, 1], 0.975]
CDF[NormalDistribution[0, 1], 1.28155]

Mathematica

Sift+Enter : evaluation

1. Numerical calculations
2.3*10^70 : 2.3 10^70 or 2.3*^70
2 ^ 100 = 1267650600228229401496703205376
2 ^ 100 //N = 1.26765*10^30
1/3+1/7 = 10/21
1/3+1/7 //N = 1./3+1/7 = 0.47619

2. Funtions
Sqrt[x], Exp[x], Log[x], Lob[b,x],
Sin[x], Cos[x], Tan[x],ArcSin[x], ArcCos[x], ArcTan[x]
n!, Abs[x], Round[x], Mod[n,m], Random[]
ax[x1,...,xn], Min[x1,...,xn], FactorInteger[n]
Pi, E, Degree(=Pi/180), I(=Sqrt[-1]), Infinity
ex) Sin[30 Degree] = Sin[Pi/6]
N[Pi] = 3.14159, N[Pi,7] = 3.141593 : precision
N[Exp[Pi Sqrt[163]], 40], 2 6 = 12
Sqrt[-4] = 2 I, (4 + 3 I) / (2 - I) = 1+2 I
x+y I = x+iy
Re[], Im[], Conjugate[], Abs[], Arg[]
%, %%, %n(=Out[n])

3. Building up calculations
pi=N[Pi,10], pi^2
x=value, x=y=value, x=. (remove any value assigned to x)
x=2; y=3, x^2y=12, x^(2y)=64
x={1,3,5}, Exp[x] // N, x/(x+1), x[[2]]=Part[x,2], x[[{2,3}]]={2,3}

4. Algebraic manipulation
# replacement
1 + 2x /. x -> 3 = 7
1 + x + x^2 /. x -> 2 - y = 3+(2-y)^2-y
x -> 3 + y; x^2 - 9 /. %
(x + y) (x - y)^2 /. {x -> 3, y -> 1 - a}
t = 1 + x^2; t /. x -> 2; t /. x -> 5a; t /. x -> Pi //N
Expand[(x+2)(x-2)]; Factor[%]
e = (x - 1)^2 (2 + x) / ((1 + x) (x - 3)^2)
Expand[e]; ExpandAll[e]; Together[%]; Apart[%]
v = Expand[(3 + 2 x)^2 (x + 2 y)^2]
Collect[v, x]; Collect[v, y]
FactorTerms[v, y]

5. Calculus
Integrate[1/(x^3 - 1), x]; Plot[%, {x,1,2}]
Integrate[Exp[-x^2], {x, 0, Infinity}]
Integrate[ x^2 + y^2, {x, 0, 1}, {y, 0, x} ]
NIntegrate[f[x], {x, min, max}]
D[Log[x],x]; D[%,x]; Simplify[%]; FullSimplify[%]
D[x^2 Log[x + a], x]
D[(x+2y)^3, y, x]
D[Exp[f[x]]/(1+Exp[f[x]+1]),x]
D[ x^n, {x, 3} ] : differentiate 3 times
Dt[ x^n, x ] : total derivative (n can depend on x)
Dt[ x^n] : total derivative
DSolve[ y'[x] == a y[x] + 1, y[x], x ] : no boundary condition
DSolve[ {y'[x] == a y[x] + 1, y[0] == 0}, y[x], x ]
DSolve[ {x'[t] == y[t], y'[t] == x[t]}, {x[t], y[t]}, t ]
Sum[i,{i,1,n}]
Product[x + i, {i, 1, 4}]
Solve[y+2x==3-y, x]
Solve[{x^2 + y^2 == 1, x + 3 y == 0}, {x, y}] ; x + y /. %
Reduce[2*x>2, x]
FindRoot[ Cos[x] == x, {x, 1} ] : search for a solution starting x=1.
FindMinimum[ (x-3)(x+2), {x, -4} ]
Series[Exp[x+y], {x, x0, order}]
Limit[Log[x],{x->0}]
N[expr] : numerical value of an expression
NSum[f[i], {i, min, Infinity}]
NProduct[f[i], {i, min, Infinity}]
NSolve[lhs==rhs, x] : numerical approximation to the solutions of a polynomial equation
%n or Out[n] - the value of the nth output
InString[n] - the text of the nth input
In[n] - the nth input, for re-evaluation

6. Linear Algebra
Inverse[]
. : matrix multiply

# Making functions
f[x_] := x^2
Clear[f]
Do[ Print[i!], {i, 5} ]
Do[expr, {i, min, max, di}]

7. Factorial Related
n!=Gamma[n+1]
Binomail[n,m]
Multinomail[n1,n2,...,nk]
Pochhammer[a,n]=a(a-1)...(a+n-1)

8. Recurrence Equations
a(1)=1, a(n) = 3*a(n-1) + 1
RSolve[{a[n] == 3 a[n-1]+1, a[1]==1}, a[n], n]
RSolve[{a[n] == a[n-1] + a[n-2], a[1] == a[2] == 1}, a[n], n]

FortranForm[%]
CForm[%]