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:
\usepackage{bibentry}
When you need to refer to needed bibtex key information, just use:
\bibentry{needed_bibtex_key}
Saturday, 25 December 2010
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.
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(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;
rev=digit+rev;
digit=(num << 12) & mask2;
rev=digit+rev;
digit=(num << 4) & mask3;
rev=digit+rev;
digit=(num >> 4) & mask4;
rev=digit+rev;
digit=(num >> 12) & mask5;
rev=digit+rev;
digit=(num >> 20) & mask6;
rev=digit+rev;
digit=(num >> 28) & mask7;
rev=digit+rev;
return(rev);
}
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;
rev=digit+rev;
/* Move 3rd byte */
digit= num <<8 br="br"> digit=digit & mask3;
rev=digit+rev;
/* Move 4rd byte */
digit= num <<24 br="br"> digit=digit & mask4;
rev=digit+rev;
return(rev);
}
24>8>
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:
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=0x10012. 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","edge.kktctelsim.com"
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 ;
return(B)
}
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 ;
return(B)
}
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) {
a<-append(a,a[(q-f+1):q],after=q);
q<-q+2*f
p <- 1;
while(p <= (n-1)) {
a<-append(a,a[(q-2*f+1):(q-f)],after=(q-f));
q<-q+f ;
p<-p+1
}
}
return(a)
}
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}.
manyrep <-function(a,f,n) {
i <- as.integer(length(a)/f);
q <- f
for(j in 1:i) {
a<-append(a,a[(q-f+1):q],after=q);
q<-q+2*f
p <- 1;
while(p <= (n-1)) {
a<-append(a,a[(q-2*f+1):(q-f)],after=(q-f));
q<-q+f ;
p<-p+1
}
}
return(a)
}
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); }
# 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
rm(list=ls())
This action won't free any memory associated with those objects. For this reason, R provides a utility for garbage collection, simply apply
gc()
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.
rm(list=ls())
This action won't free any memory associated with those objects. For this reason, R provides a utility for garbage collection, simply apply
gc()
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.
Subscribe to:
Posts (Atom)