vec = function(...){vals = c(...); if (length(vals) == 0){ return(c()); } matrix(c(vals),length(vals),1) } mat = function(...){ d = list(...); if (length(d)==0) return(c()); if (length(d)==1) return(vec(d[[1]])); if (length(d[[1]]) == 0) return(c()); contents = c(d[[1]]); rows = length(d[[1]]); for (k in 2:length(d)) { if ( length(d[[k]]) != rows ) { stop('Unequal length vectors given as arguments.'); } contents = c(contents, d[[k]]); } matrix(contents, rows, length(d)); } project = function(A,b) { foo = dim(A); goo = dim(b); if (goo[1] !=1 & goo[2] != 1){ stop('b must be a vector'); } if (foo[1] != length(b)) { stop('A must contain vectors of the same dimension as b'); } if (foo[2] <= foo[1]) { return( vec(lsfit(A,b,intercept=F)$coef ) ); } n = svd(A); zeros = n$d < .0000001; recip = n$d; recip[!zeros] = 1/recip[!zeros]; n$v%*%diag(recip)%*%t(n$u)%*%b } dot = function(u,v){ if (length(u) != length(v)) { stop('Vectors not of equal length.'); } return( sum(u*v) ) } singvals = function(A){ # formulated to give one singular value for each column in A foo = La.svd(A, nv=ncol(A), nu=ncol(A))$d; c( foo, rep(0,ncol(A) - length(foo))); }