2. 13/08/31 2
AgendaAgenda
●
IntroductionIntroduction
●
Eigenvalue problem and SVDEigenvalue problem and SVD
●
Examples of SVDExamples of SVD
●
How to solve SVDHow to solve SVD
●
Randomized SVDRandomized SVD
●
EigenEigen
●
R and c++R and c++
●
RcppRcpp
●
RcppEigenRcppEigen
●
RedSVDRedSVD
●
ReferencesReferences
15. 13/08/31 15
About Eigen
● Eigen 3.2 has been released on July 24, 2013.
– Since Eigen 3.1, the key new features of this version are: a built-in supernodal sparse
LU solver adapted from SuperLU, a rank-revealing sparse QR factorization with
numerical column pivoting, a RealQZ factorization, a GeneralizedEigenSolver, and a
Ref<> class allowing to write non templated function taking various kind of Eigen
dense objects without copies.
– This release also includes a few new functions for dense and sparse matrices, built-in
COLAMD ordering, support to SuiteSparse QR and Metis, as well as some accuracy
and performance improvements.
16. 13/08/31 16
The power of Eigen
● Eigen is fast (in some cases)
http://eigen.tuxfamily.org/index.php?title=Benchmark
一般的な行列計算ではEigenは他のライブラリより高速
17. 13/08/31 17
The power of Eigen
● Eigen is fast (in some cases)
http://eigen.tuxfamily.org/index.php?title=Benchmark
三角化など特定の計算では他のライブラリのほうが高速
18. 13/08/31 18
R and c++
● Rで特定の処理を高速化したい場合C++で記述する。
– ここでは固有値問題、SVDに伴う行列計算
● R
– 抽象度が高い、豊富なライブラリ、低速
● C++
– 高速、コンパイルが必要、むずい(特にメモリ管理)、ライブ
ラリもむずい(boost...)
19. 13/08/31 19
#include <Rcpp.h>
using namespace Rcpp;
RcppExport SEXP convolveCpp(SEXP aa,SEXP bb){
NumericVector a(aa);
NumericVector b(bb);
int na = a.size(), nb = b.size();
int nab = na + nb - 1;
NumericVector xab(nab);
for (int i = 0; i < na; i++)
for (int j = 0; j < nb; j++)
xab[i + j] += a[i] * b[j];
return wrap(xab);
}
Rcpp package
● Rcppはc++で書いたコードとRコードを橋
渡ししてくれるパッケージ
詳しくは”RとC/C++の連携”
http://www.slideshare.net/sfchaos/tokyor7rcc
A<-c(1,2,3,4)
B<-c(5,6,7,8)
C<-convolveCpp(A,B)
C++ R
20. 13/08/31 20
Rcpp package
● Rstudioから簡単コンパイル&実行
– 詳しくはTokyo.RTokyo.R 白熱教室「これからの白熱教室「これからの
RcppRcppの話をしよう」の話をしよう」
● http://www.slideshare.net/teramonagi/tokyor-rcpp-16709700
● http://www.rstudio.com/ide/docs/advanced/using_rcpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]// [[Rcpp::export]]
NumericVector convolveCpp(NumericVector a, NumericVector b) {
int na = a.size(), nb = b.size();
int nab = na + nb - 1;
NumericVector xab(nab);
for (int i = 0; i < na; i++)
for (int j = 0; j < nb; j++)
xab[i + j] += a[i] * b[j];
return xab;
}
21. 13/08/31 21
RcppEigen
● RcppをつかってC++(Eigen)をwrap
● Features
– 入力: as でRのオブジェクトに変換
– 出力: wrapでSEXPに変換
– listを返すこともできる
– float型は使用できない
Fast and Elegant Numerical Linear Algebra Using
the RcppEigen Package
http://www.jstatsoft.org/v52/i05/paper
22. 13/08/31 22
RcppEigen
Fast and Elegant Numerical Linear Algebra Using the RcppEigen
Package
●
Contents(抜粋)
●
3. Some simple examples
– 3.1. Transpose of an integer matrix
– 3.2. Products and cross-products
– 3.3. Crossproduct of a single matrix
– 3.4. Cholesky decomposition of the crossprod
●
4. Least squares solutions
●
5. Delayed evaluation
● 6. Sparse matrices
http://www.jstatsoft.org/v52/i05/paper
24. 13/08/31 24
Randomized SVD
● In R and Eigen
直接法
(LU法、QR法)
Lanczos法
Jacobi-Davidson法
Randmized法
R svd,
eigen(LAPACK)
igraph(ARPACK) なし?
Eigen JacobiSVD なし?
(ConjugateGradient)
RedSVD(external)