# R/C/C++ library for N-dimensional arrays

I'm looking for a library that is either in R or easily wrappable with R, that can do the following things:

• construct and subset N-dimensional arrays
• perform operations such as min, max, sum, sd along any dimension
• is fast and ideally has a BLAS backend

And (this is important), is able to define multilevel-masks along any dimension (and combinations thereof) where the above operations are summarised. E.g.,

1 3 5
7 8 9


or:

1 3 5                                                      3 7 11
2 4 6   -(rowlevels=['a','a','b'])-(op=sum)-(axis=rows)->  7 8 9
7 8 9


and ideally a combination of the two as well.

I've started coding this in core R but it gets a mess quickly.

Any suggestions?

• By $n$-dimensional matrices, do you mean order $n$ tensors? (Or $n$-dimensional arrays?) Usually, matrices are 2-dimensional constructs ($m$ rows by $n$ columns). Jan 29 '14 at 22:44
• I meant n-dimensional arrays, should have probably used that word there. Jan 30 '14 at 0:04

The DyND library might interest you. It comes out of the Scientific Python ecosystem as a numpy replacement but I believe that it is straight C++ and so should be easily wrappable in R.

Actually, having a single numeric library shared between the two languages might have unintended benefits.

By far the best solution for R is DataTable

http://cran.r-project.org/web/packages/data.table/index.html

I'm not sure about BLAS backend, but DataTable is wickedly fast. I had a job that used aggregation from data.frame that took about 24 hours. I simply replaced everything with data tables and it finished in about 20 minutes. This was 2 years ago when the package was in its infancy.

The array function in base R provides this functionality:

x <- array(c(1, 2, 7, 3, 4, 8, 5, 6, 9), dim = c(3, 3, 1))
max(x[1,,])
min(x[,1,])
sd(x[,,1])


The slam package provides a format for sparse arrays:

s <- as.simple_sparse_array(x)
max(s[1,,])
min(s[,1,])
sd(as.vector(s[,,1]))


I haven't ever used slam, so I'm not familiar with the ins and outs. If your arrays are going to be pretty dense, just use R arrays.

Between array indexing and the apply function, you should be able to do most of what you want.