# NOT RUN {
library(ff)
#Tall data
y_tall<-matrix(rnorm(5000),500,10) #y tall
x_tall<-matrix(rnorm(5000),500,10)
y_wide<-t(y_tall)
x_wide<-t(x_tall)
y_tall_ff<-as.ff(y_tall) #y tall and ff
x_tall_ff<-as.ff(x_tall)
y_wide_ff<-as.ff(y_wide) #y tall and ff
x_wide_ff<-as.ff(x_wide)
#Set options to ensure that block matrix algebra is actually done,
#and the entire algebra isn't just one in one step.
#Compare ffmatrixmult against output from standard methods
options('ffbytesize'=100)
#small final matrices
#x'x
range( crossprod(x_tall) - ffmatrixmult(x_tall_ff, xt=TRUE) )
range( tcrossprod(x_wide) - ffmatrixmult(x_wide_ff, yt=TRUE) )
range( crossprod(x_tall,y_tall) - ffmatrixmult(x_tall_ff,y_tall_ff, xt=TRUE) )
range( tcrossprod(x_wide,y_wide) - ffmatrixmult(x_wide_ff,y_wide_ff, yt=TRUE) )
range( (x_wide%*%y_tall) - ffmatrixmult(x_wide_ff,y_tall_ff) )
#ff + small data
s_tall <- matrix(rnorm(80),10,8)
s_wide <- matrix(rnorm(80),8,10)
#tall output
range( crossprod(x_wide, s_tall) - ffmatrixmult(x_wide_ff, s_tall,xt=TRUE)[] )
range( tcrossprod(x_tall, s_wide) - ffmatrixmult(x_tall_ff, s_wide,yt=TRUE)[] )
range( x_tall%*%s_tall - ffmatrixmult(x_tall_ff, s_tall)[])
#Wide output
range( crossprod(s_tall, y_wide) - ffmatrixmult( s_tall, y_wide_ff,xt=TRUE)[] )
range( tcrossprod(s_wide, y_tall) - ffmatrixmult( s_wide,y_tall_ff,yt=TRUE)[] )
range( s_wide%*%y_wide - ffmatrixmult(s_wide,y_wide_ff)[])
#Reset options for more practical use
options('ffbytesize'=16777216)
# }
Run the code above in your browser using DataLab