Saturday, 25 December 2010

Package Bibentry

Bibtex is a powerful tool for referencing in scientific publications and many other type of academic documents. One nice package (bibentry) that enables us to use bibtex entries within the text. Simply add the package as follows:


When you need to refer to needed bibtex key information, just use:


Monday, 27 September 2010

Reverse 32-bit Hexadecimal Value (with C)

A 32-bit hexadecimal value such as 0xABCD1234 may need to be reversed as 0x4321DCBA. This might be needed. The following is a naive implementation of reversing a hexadecimal. It would be interesting quiz for undgrad CS student to write a n-bit version of this.

signed int reverse_hex(signed int num) {
/* stupid reverse hex */
int rev= 0x00000000;
int digit= 0x00000000;
int mask1=0x0f000000;
int mask2=0x00f00000;
int mask3=0x000f0000;
int mask4=0x0000f000;
int mask5=0x00000f00;
int mask6=0x000000f0;
int mask7=0x0000000f;

digit=num << 28;
rev=num << 20;
rev=rev & mask1;
digit=(num << 12) & mask2;
digit=(num << 4) & mask3;
digit=(num >> 4) & mask4;
digit=(num >> 12) & mask5;
digit=(num >> 20) & mask6;
digit=(num >> 28) & mask7;

However, the above procedure is not so usual while it is reversed by chunks of 4-bit. More realistic situation is reversing from between big and little endian representation. Such as, 0xABCD1234 would be reversed as 0x3412CDAB, so byte ordering matters. The following is the C function doing this. Similarly n-byte version of
this function will be left as a further exercise.

signed int reverse_hex_byte(signed int num) {
/* stupid reverse hex */
int rev= 0x00000000;
int digit= 0x00000000;
int mask1=0x000000ff;
int mask2=0x0000ff00;
int mask3=0x00ff0000;
int mask4=0xff000000;

/* Move 1st byte */
digit=num >> 24;
rev=digit & mask1;
/* Move 2nd byte */
digit= num >>8;
digit=digit & mask2;
/* Move 3rd byte */
digit= num <<8 br="br"> digit=digit & mask3;
/* Move 4rd byte */
digit= num <<24 br="br"> digit=digit & mask4;

Wednesday, 25 August 2010

Vodafone (telsim Cyprus) 3G with Ubuntu

Sometime ago I have given some instructions on how to connect to movistar,Spain
3G network [here]. The similar connection can be made for Vodafone (telsim Cyprus)
with the following changes:

1. Modem USB settings command should be read:
modprobe usbserial vendor=0x12d1 product=0x1001
2. In wvdial.conf entry for telsim should look like:
[Dialer telsim]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CGDCONT=1,"IP",""
Modem Type = Analog Modem
Baud = 115200
New PPPD = yes
Modem = /dev/ttyUSB0
ISDN = 0
Dial Command = ATDT
Phone = "*99***1#"

Friday, 23 April 2010

More efficient diag() function

GNU R's diag() function provides a tool to produce diagonal matrices, however for
large vectors this is quite memory inefficient. Here is a version which uses at least
25% less memory;

mydiag<- function(A) {

# Given a a vector generate
# a diagonal matrix
# (Consume at least 1/4 less memory then R's diag)
A <- as.vector(A) ;
B <- matrix(0,nrow=length(A),ncol=length(A)) ;
row <- nrow(B) ;
col <- ncol(B) ;
size <- col*row ;
row <- row+1 ;
B[seq(1,size,row)] <- A ;

Wednesday, 17 March 2010

Repeat chunks of a sequence arbitrary times with GNU R

Repeating arbitrary size chunks multiple times in a given sequence with preserving order of chunks would be needed in certain computations. For example in matrix multiplication that is reduced to a 1D problem. Mapping your sequence into a matrix and repeating certain column or generating another sequence in the appropriate loop are straight forward solutions indeed. However these approaches will suffer immensely in memory consumption with increasing sequence size and would generate the original problem it seek to solve. More efficient and the general way to achieve repeating chunks is solved with the following R code:

manyrep <-function(a,f,n) {
i <- as.integer(length(a)/f);
q <- f
for(j in 1:i) {
p <- 1;
while(p <= (n-1)) {
q<-q+f ;

For example if sequence a = {1,2,3,4}, frequency or chunk size f=2 and number of repetition n=1, hence manyrep(1:4,2,1) the output should look like {1,2,1,2,3,4,3,4}. If n was 2 output would be {1,2,1,2,1,2,3,4,3,4,3,4}.

Wednesday, 10 March 2010

Append to a vector in regular intervals efficiently in R

In many instances one may need to append a character or a value in a given sequence, usually represented as a vector or array. It is pretty easy to achieve this naively. However, the point is not to consume any more memory. This is one of the simple solutions I can propose in GNU R.

# This function appends a word into vector a in every f element
appendfreq<-function(a,f,word) { i <- as.integer(length(a)/f-1); q<-f; for(j in 1:i) { a<-append(a,word,after=q); q<-q+f+1; } ;return(a); }

Tuesday, 12 January 2010

GNU R Garbage Collection

On GNU R if you delete your objects by


This action won't free any memory associated with those objects. For this reason, R provides a utility for garbage collection, simply apply


This will free not used allocations. If you like to apply this whenever R allocates memory, apply this:

gctorture(on = TRUE)

Consult with R Internals manual for further details.
(c) Copyright 2008-2017 Mehmet Suzen (suzen at acm dot org)

Creative Commons Licence
This work is licensed under a Creative Commons Attribution 3.0 Unported License.