Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient Triangular Backsubstitution in R

I am interested in solving the linear system of equations Ax=b where A is a lower-triangular matrix (n × n) and b is a (n × 1) vector where n ≈ 600k.

I coded up backsubstitution in R and it works fast for matrices of size upto 1000, but is really slow for larger n (≈ 600k). I know that the naive backsubstitution is O(n^2).

My R function is below; does anyone know of a more efficient (vectorized, parallelized etc.) way of doing it, which scales to large n?

Backsubstitution

backsub=function(X,y)
{ 
 l=dim(X)
 n=l[1]  
 p=l[2]
 y=as.matrix(y)
    
   for (j in seq(p,1,-1))
    {  
      y[j,1]=y[j,1]/X[j,j]
      if((j-1)>0)
         y[1:(j-1),1]=y[1:(j-1),1]-(y[j,1]*X[1:(j-1),j])
    }  
    return(y)
}
like image 296
Blade Runner Avatar asked Dec 15 '25 02:12

Blade Runner


1 Answers

How about the R function backsolve? It calls the Level 3 BLAS routine dtrsm which is probably what you want to be doing. In general, you won't beat BLAS/LAPACK linear algebra routines: they're insanely optimized.

like image 56
Matthew Gunn Avatar answered Dec 16 '25 18:12

Matthew Gunn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!