Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Friday, July 31, 2009

ekee - LaTeX equations on Linux


Basically Ekee is the Linux counterpart of LaTexIt on mac. You could type an equation on the fly and generate png/pdf files for it, then paste it on docs/presentations.

Installation on ubuntu:
  1. sudo aptitude update
  2. sudo aptitude install dvipng
  3. sudo aptitude install libqt4-ruby
  4. sudo aptitude install pstoedit
  5. sudo aptitude install texlive-extra-utils
  6. Download the deb file from ekee_2.0.2-1_all.deb
  7. sudo dpkg -i ekee_2.0.2-1_all.deb
Website: http://rlehy.free.fr/

Monday, April 21, 2008

find process id

Example: find the process id of all runing firefox for zo2.
pgrep -u zo2 firefox
More details check the wiki page.

Thursday, September 27, 2007

Using C functions in R

Method:
convert .c file to shared library .so via link ready object file .o, then use dyn.load() in R to call the shared library, and use .C() to convert it to R function.

Example 1, conv
Feature: simple c example without external library.

1.1 C code:
/* conv.c */
void convolve(double *a, int *na,
double *b, int *nb, double *ab){
int i, j, nab=*na + *nb -1;

for (i=0; i<nab; i++)
ab[i] = 0.0;

for (i=0; i<*na; i++)
for (j=0; j<*nb; j++)
ab[i+j] = ab[i+j] + a[i]*b[j];

1.2 Terminal Process:
gcc -c conv.c
R CMD SHLIB conv.o

R CMD SHLIB conv.c

1.3 R code:
# conv.r
dyn.load("conv.so");

conv <- function(a, b){ .C("convolve", as.double(a), as.integer(length(a)), as.double(b), as.integer(length(b)), ab = double(length(a)+length(b)-1))$ab } x <- 1:3 y <- 1:5 z <- conv(x,y)



Example 2, Exp1
Feature: c example with gsl library.

2.1 C code:
/* exp1.c */
#include
void myexp(double *x, int *nx, double *y){
int i;
for (i=0; i<*nx; i++){ y[i] = gsl_sf_expint_E1(x[i]); } }


2.2 Terminal Process:

gcc -c exp1.c
gcc -shared -o exp1.so exp1.o -lgsl -lblas -lm

R CMD SHLIB exp1.o /usr/lib/libgsl.a

2.3 R code:

# exp1.r
dyn.load("exp1.so");

myexp <- function(x){
.C("myexp", as.double(x), as.integer(length(x)),
y = double(length(x)))$y;
}
myexp(1:4);


Example 3, Exp1 in C++
Feature: C++ example with gsl library.
How: just wrap all functions in extern “C”{}.

3.1 C code:
/* exp1.cpp */
#include <gsl/gsl_sf_expint.h>
extern “C”{
void myexp(double *x, int *nx, double *y){
int i;
for (i=0; i<*nx; i++){ y[i] = gsl_sf_expint_E1(x[i]); } } }


3.2 Terminal Process:
g++ -c exp1.cpp
g++ -shared -o exp1.so exp1.o -lgsl -lblas -lm

R CMD SHLIB exp1.o /usr/lib/libgsl.a

3.3 R code:
# exp1.r
dyn.load("exp1.so");
myexp <- function(x){ .C("myexp", as.double(x), as.integer(length(x)), y = double(length(x)))$y; } myexp(1:4);

Install R packages locally

在没有权限写R的安装目录的时候,可以在user的目录下局部安装R的软件包,比较方便的做法是如下四步(如果已经完成了前两步,就只需执行最后两步):

  1. 在$HOME下建立一个文件夹存放自己的R package:
    cd
    mkdir .Rlibs
  2. 在$HOME下建立环境文件.Renviron,使得每次启动R的时候自动识别package所在的目录文件。文件.Renviron只有如下一行文字:
    R_LIBS=$HOME/.Rlibs
  3. 下载相应的软件包,比如 gsl_1.6.6.tar.gz

  4. 在下载目录下安装:
    R CMD INSTALL -l $HOME/.Rlibs gsl
如果前两步已经设置好,也可以直接使用如下命令安装
R # and run next line inside R
install.packages("packagename", lib=LIBPATH)