Merge lp:~honzik/minieigen/minieigen into lp:~eudoxos/minieigen/trunk

Proposed by Václav Šmilauer
Status: Merged
Merge reported by: Václav Šmilauer
Merged at revision: not available
Proposed branch: lp:~honzik/minieigen/minieigen
Merge into: lp:~eudoxos/minieigen/trunk
Diff against target: 110 lines (+72/-0)
1 file modified
minieigen/minieigen.cpp (+72/-0)
To merge this branch: bzr merge lp:~honzik/minieigen/minieigen
Reviewer Review Type Date Requested Status
Václav Šmilauer Approve
Review via email: mp+175521@code.launchpad.net

Description of the change

Add various decompositions

To post a comment you must log in.
Revision history for this message
Václav Šmilauer (eudoxos) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'minieigen/minieigen.cpp'
2--- minieigen/minieigen.cpp 2013-04-18 21:04:34 +0000
3+++ minieigen/minieigen.cpp 2013-07-18 10:20:35 +0000
4@@ -426,6 +426,7 @@
5 static Vector3r Matrix3r_leviCivita(const Matrix3r& self){ return leviCivita(self); }
6 #endif
7
8+
9 /*** wrapping methods which are defined by eigen, but return expression template, which does not work with python ***/
10 static Vector3r Quaternionr_Rotate(Quaternionr& q, const Vector3r& u){ return q*u; }
11 // supposed to return raw pointer (or auto_ptr), boost::python takes care of the lifetime management
12@@ -466,6 +467,57 @@
13 template<typename VT> Eigen::Matrix<typename VT::Scalar,VT::RowsAtCompileTime,VT::RowsAtCompileTime>
14 Vector_asDiagonal(const VT& self){ return self.asDiagonal(); }
15
16+
17+/****** matrix decompositions ******/
18+// svd decomposition
19+template<typename MatrixInType, typename MatrixUType, typename MatrixSType, typename MatrixVType>
20+py::tuple Matrix_jacobiSVD(const MatrixInType& in) {
21+ MatrixUType u;
22+ MatrixVType v;
23+ MatrixSType s;
24+ Eigen::JacobiSVD<MatrixInType> svd(in, Eigen::ComputeThinU | Eigen::ComputeThinV);
25+ u = svd.matrixU();
26+ v = svd.matrixV();
27+ s = svd.singularValues().asDiagonal();
28+ return py::make_tuple(u,s,v);
29+}
30+static py::tuple Matrix3r_jacobiSVD(const Matrix3r& in) { return Matrix_jacobiSVD<Matrix3r,Matrix3r,Matrix3r,Matrix3r>(in); }
31+static py::tuple Matrix6r_jacobiSVD(const Matrix6r& in) { return Matrix_jacobiSVD<Matrix6r,Matrix6r,Matrix6r,Matrix6r>(in); }
32+static py::tuple MatrixXr_jacobiSVD(const MatrixXr& in) { return Matrix_jacobiSVD<MatrixXr,MatrixXr,MatrixXr,MatrixXr>(in); }
33+
34+// polar decomposition
35+template<typename MatrixType>
36+py::tuple Matrix_computeUnitaryPositive(const MatrixType& in) {
37+ assert(in.rows()==in.cols());
38+ MatrixType u, s, v, retU, retP;
39+ Eigen::JacobiSVD<MatrixType> svd(in, Eigen::ComputeThinU | Eigen::ComputeThinV);
40+ u = svd.matrixU();
41+ v = svd.matrixV();
42+ s = svd.singularValues().asDiagonal();
43+ retU = u*v.transpose();
44+ retP = v*s*v.transpose();
45+ return py::make_tuple(retU,retP);
46+}
47+static py::tuple Matrix3r_computeUnitaryPositive(const Matrix3r& in) { return Matrix_computeUnitaryPositive<Matrix3r>(in); }
48+static py::tuple Matrix6r_computeUnitaryPositive(const Matrix6r& in) { return Matrix_computeUnitaryPositive<Matrix6r>(in); }
49+static py::tuple MatrixXr_computeUnitaryPositive(const MatrixXr& in) { return Matrix_computeUnitaryPositive<MatrixXr>(in); }
50+
51+// eigen decomposition
52+template<typename MatrixType, typename VectorType>
53+py::tuple Matrix_selfAdjointEigenDecomposition(const MatrixType& in) {
54+ MatrixType eigVecs;
55+ VectorType eigVals;
56+ Eigen::SelfAdjointEigenSolver<MatrixType> a(in); eigVecs=a.eigenvectors(); eigVals=a.eigenvalues();
57+ return py::make_tuple(eigVecs,eigVals);
58+}
59+static py::tuple Matrix3r_selfAdjointEigenDecomposition(const Matrix3r& in) { return Matrix_selfAdjointEigenDecomposition<Matrix3r,Vector3r>(in); }
60+static py::tuple Matrix6r_selfAdjointEigenDecomposition(const Matrix6r& in) { return Matrix_selfAdjointEigenDecomposition<Matrix6r,Vector6r>(in); }
61+static py::tuple MatrixXr_selfAdjointEigenDecomposition(const MatrixXr& in) { return Matrix_selfAdjointEigenDecomposition<MatrixXr,VectorXr>(in); }
62+
63+
64+
65+
66+
67 #undef IDX_CHECK
68
69 // those are again needed to wrap the result (TODO: use some templates here)
70@@ -647,6 +699,12 @@
71 .add_static_property("Zero",&Matrix3r_Zero)
72 .add_static_property("Ones",&Matrix3r_Ones)
73 .def("Random",&Matrix3r_Random).staticmethod("Random")
74+ .def("jacobiSVD",&Matrix3r_jacobiSVD,"Compute SVD decomposition of matrix, retuns (U,S,V) such that self=U*S*V.transpose()")
75+ .def("svd",&Matrix3r_jacobiSVD,"Shortcut for jacobiSVD")
76+ .def("computeUnitaryPositive",&Matrix3r_computeUnitaryPositive,"Compute polar decomposition (unitary matrix U and positive semi-definite symmetric matrix P such that self=U*P")
77+ .def("polarDecomposition",&Matrix3r_computeUnitaryPositive,"Shortcut for computeUnitaryPositive")
78+ .def("selfAdjointEigenDecomposition",&Matrix3r_selfAdjointEigenDecomposition,"Compute eigen (spectral) decomposition of symmetric matrix, returns (eigVecs,eigVals). eigVecs is orthogonal Matrix3 with columns ar normalized eigenvectors, eigVals is Vector3 with corresponding eigenvalues. self=eigVecs*diag(eigVals)*eigVecs.transpose()")
79+ .def("spectralDecomposition",&Matrix3r_selfAdjointEigenDecomposition,"Shortcut for selfAdjointEigenDecomposition")
80 #if 0
81 .def("polarDecomposition",&Matrix3r_polarDecomposition)
82 .def("symmEigen",&Matrix3r_symmEigen)
83@@ -675,6 +733,13 @@
84 .def("ur",&Matrix6r_ur)
85 .def("ll",&Matrix6r_ll)
86 .def("lr",&Matrix6r_lr)
87+ //
88+ .def("jacobiSVD",&Matrix6r_jacobiSVD,"Compute SVD decomposition of matrix, retuns (U,S,V) such that self=U*S*V.transpose()")
89+ .def("svd",&Matrix6r_jacobiSVD,"Shortcut for jacobiSVD")
90+ .def("computeUnitaryPositive",&Matrix6r_computeUnitaryPositive,"Compute polar decomposition (unitary matrix U and positive semi-definite symmetric matrix P such that self=U*P")
91+ .def("polarDecomposition",&Matrix6r_computeUnitaryPositive,"Shortcut for computeUnitaryPositive")
92+ .def("selfAdjointEigenDecomposition",&Matrix6r_selfAdjointEigenDecomposition,"Compute eigen (spectral) decomposition of symmetric matrix, returns (eigVecs,eigVals). eigVecs is orthogonal Matrix6 with columns ar normalized eigenvectors, eigVals is Vector6 with corresponding eigenvalues. self=eigVecs*diag(eigVals)*eigVecs.transpose()")
93+ .def("spectralDecomposition",&Matrix6r_selfAdjointEigenDecomposition,"Shortcut for selfAdjointEigenDecomposition")
94
95 //
96 .def("__neg__",&Matrix6r__neg__)
97@@ -763,6 +828,13 @@
98 .def("pruned",&Matrix_pruned<MatrixXr>,py::arg("absTol")=1e-6)
99 .def("maxAbsCoeff",&Matrix_maxAbsCoeff<MatrixXr>)
100 .def("sum",&Matrix_sum<MatrixXr>)
101+ //
102+ .def("jacobiSVD",&MatrixXr_jacobiSVD,"Compute SVD decomposition of matrix, retuns (U,S,V) such that self=U*S*V.transpose()")
103+ .def("svd",&MatrixXr_jacobiSVD,"Shortcut for jacobiSVD")
104+ .def("computeUnitaryPositive",&MatrixXr_computeUnitaryPositive,"Compute polar decomposition (unitary matrix U and positive semi-definite symmetric matrix P such that self=U*P")
105+ .def("polarDecomposition",&MatrixXr_computeUnitaryPositive,"Shortcut for computeUnitaryPositive")
106+ .def("selfAdjointEigenDecomposition",&MatrixXr_selfAdjointEigenDecomposition,"Compute eigen (spectral) decomposition of symmetric matrix, returns (eigVecs,eigVals). eigVecs is orthogonal MatrixX with columns ar normalized eigenvectors, eigVals is VectorX with corresponding eigenvalues. self=eigVecs*diag(eigVals)*eigVecs.transpose()")
107+ .def("spectralDecomposition",&MatrixXr_selfAdjointEigenDecomposition,"Shortcut for selfAdjointEigenDecomposition")
108
109 //
110 .def("__neg__",&MatrixXr__neg__)

Subscribers

People subscribed via source and target branches