Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
SparseNullSpace.h
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3*
4 * MoDELib is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef _model_SparseNullSpace_h_
9#define _model_SparseNullSpace_h_
10
11#include <iostream>
12#include <chrono>
13
14#include <Eigen/Sparse>
15#include <Eigen/SparseQR>
16//#include <Eigen/SparseLU> // only for square matrics
17#include <Eigen/OrderingMethods>
18//
19
20// http://stackoverflow.com/questions/2181418/computing-the-null-space-of-a-matrix-as-fast-as-possible
21
22// To get just the matrix Z, and not Y
23// https://stackoverflow.com/questions/34662940/how-to-compute-basis-of-nullspace-with-eigen-library
24
25namespace model
26{
33 template<typename SparseMatrixType>
35 {
36
37 typedef typename SparseMatrixType::Scalar Scalar;
38
39 SparseMatrixType Z;
40 SparseMatrixType Y;
41
42 public:
43
44 SparseNullSpace(const SparseMatrixType& C,
45 const Scalar& tol=Eigen::NumTraits<Scalar>::dummy_precision())
46 {
49
50// const auto t1= std::chrono::system_clock::now();
51// std::cout<<"Computing null-space... "<<std::flush;
52// Eigen::SparseLU<SparseMatrixType,Eigen::COLAMDOrdering<int> > lu(C.transpose());
53// std::cout<<" ["<<(std::chrono::duration<double>(std::chrono::system_clock::now()-t1)).count()<<" sec]"<<std::endl;
54
55
56// const auto t0= std::chrono::system_clock::now();
57// std::cout<<"Computing QR... "<<std::flush;
58
59
60
61// Eigen::SparseQR<SparseMatrixType,Eigen::COLAMDOrdering<int> > qr(C.transpose());
62 Eigen::SparseQR<SparseMatrixType,Eigen::COLAMDOrdering<int> > qr(C.transpose());
63
64// std::cout<<"done qr... "<<std::flush;
65
66
67 int nnz=0; // number of non-zero diagonal elements
68 for(int c=0;c<qr.matrixR().cols();++c)
69 {
70 if(std::fabs(qr.matrixR().coeff(c,c))<= tol)
71 {
72 break;
73 }
74 else
75 {
76 nnz++;
77 }
78 }
79
80 assert(nnz==qr.rank());
81
82// std::cout<<"done loop... "<<std::flush;
83
84
85 SparseMatrixType Q;
86 Q=qr.matrixQ();
87
88// std::cout<<"done Q... "<<std::flush;
89
90 Z=Q.rightCols(Q.cols()-nnz);
91
92// std::cout<<"done Z... "<<std::flush;
93
94 Y=Q.leftCols(nnz);
95
96// std::cout<<"done Y... "<<std::flush;
97
98
99// std::cout<<" ["<<(std::chrono::duration<double>(std::chrono::system_clock::now()-t0)).count()<<" sec]"<<std::endl;
100
101
102 }
103
104 const SparseMatrixType& matrixZ() const
105 {
106 return Z;
107 }
108
109 const SparseMatrixType& matrixY() const
110 {
111 return Y;
112 }
113
114 };
115
116}
117
118#endif
const SparseMatrixType & matrixZ() const
SparseNullSpace(const SparseMatrixType &C, const Scalar &tol=Eigen::NumTraits< Scalar >::dummy_precision())
const SparseMatrixType & matrixY() const
c
Definition int_R.m:15
Q