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)
}

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}.

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

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.
(c) Copyright 2008-2024 Mehmet Suzen (suzen at acm dot org)

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License