How to solve a least squares (underdetermined system) quickly?

后端 未结 1 754
不知归路
不知归路 2021-02-04 10:30

I have a program in R that is computing a large amount of least squares solutions (>10,000: typically 100,000+) and, after profiling, these are the current bottlenecks for the p

1条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 11:13

    How about Rcpp?

    library(Rcpp)
    cppFunction(depends='RcppArmadillo', code='
                arma::mat fRcpp (arma::mat A, arma::mat b) {
                arma::mat betahat ;
                betahat = (A.t() * A ).i() * A.t() * b ;
                return(betahat) ;
                }                                
                ')
    
    all.equal(f1(A, b), f2(A, b), fRcpp(A, b))
    #[1] TRUE
    microbenchmark(f1(A, b), f2(A, b), fRcpp(A, b))
    #Unit: microseconds
    #        expr    min     lq     mean  median      uq     max neval
    #    f1(A, b) 55.110 57.136 67.42110 59.5680 63.0120 160.873   100
    #    f2(A, b) 34.444 37.685 43.86145 39.7120 41.9405 117.920   100
    # fRcpp(A, b)  3.242  4.457  7.67109  8.1045  8.9150  39.307   100
    

    0 讨论(0)
提交回复
热议问题