test_QR.py
Go to the documentation of this file.
1 import numpy as np
2 
3 import eigenpy
4 
5 rows = 20
6 cols = 100
7 rng = np.random.default_rng()
8 
9 A = rng.random((rows, cols))
10 
11 # Test HouseholderQR decomposition
12 householder_qr = eigenpy.HouseholderQR()
13 householder_qr = eigenpy.HouseholderQR(rows, cols)
14 householder_qr = eigenpy.HouseholderQR(A)
15 
16 householder_qr_eye = eigenpy.HouseholderQR(np.eye(rows, rows))
17 X = rng.random((rows, 20))
18 assert householder_qr_eye.absDeterminant() == 1.0
19 assert householder_qr_eye.logAbsDeterminant() == 0.0
20 
21 Y = householder_qr_eye.solve(X)
22 assert (X == Y).all()
23 
24 # Test FullPivHouseholderQR decomposition
25 fullpiv_householder_qr = eigenpy.FullPivHouseholderQR()
26 fullpiv_householder_qr = eigenpy.FullPivHouseholderQR(rows, cols)
27 fullpiv_householder_qr = eigenpy.FullPivHouseholderQR(A)
28 
29 fullpiv_householder_qr = eigenpy.FullPivHouseholderQR(np.eye(rows, rows))
30 X = rng.random((rows, 20))
31 assert fullpiv_householder_qr.absDeterminant() == 1.0
32 assert fullpiv_householder_qr.logAbsDeterminant() == 0.0
33 
34 Y = fullpiv_householder_qr.solve(X)
35 assert (X == Y).all()
36 assert fullpiv_householder_qr.rank() == rows
37 
38 fullpiv_householder_qr.setThreshold(1e-8)
39 assert fullpiv_householder_qr.threshold() == 1e-8
40 assert eigenpy.is_approx(np.eye(rows, rows), fullpiv_householder_qr.inverse())
41 
42 # Test ColPivHouseholderQR decomposition
43 colpiv_householder_qr = eigenpy.ColPivHouseholderQR()
44 colpiv_householder_qr = eigenpy.ColPivHouseholderQR(rows, cols)
45 colpiv_householder_qr = eigenpy.ColPivHouseholderQR(A)
46 
47 colpiv_householder_qr = eigenpy.ColPivHouseholderQR(np.eye(rows, rows))
48 X = rng.random((rows, 20))
49 assert colpiv_householder_qr.absDeterminant() == 1.0
50 assert colpiv_householder_qr.logAbsDeterminant() == 0.0
51 
52 Y = colpiv_householder_qr.solve(X)
53 assert (X == Y).all()
54 assert colpiv_householder_qr.rank() == rows
55 
56 colpiv_householder_qr.setThreshold(1e-8)
57 assert colpiv_householder_qr.threshold() == 1e-8
58 assert eigenpy.is_approx(np.eye(rows, rows), colpiv_householder_qr.inverse())
59 
60 # Test CompleteOrthogonalDecomposition
61 cod = eigenpy.CompleteOrthogonalDecomposition()
62 cod = eigenpy.CompleteOrthogonalDecomposition(rows, cols)
63 cod = eigenpy.CompleteOrthogonalDecomposition(A)
64 
65 cod = eigenpy.CompleteOrthogonalDecomposition(np.eye(rows, rows))
66 X = rng.random((rows, 20))
67 assert cod.absDeterminant() == 1.0
68 assert cod.logAbsDeterminant() == 0.0
69 
70 Y = cod.solve(X)
71 assert (X == Y).all()
72 assert cod.rank() == rows
73 
74 cod.setThreshold(1e-8)
75 assert cod.threshold() == 1e-8
76 assert eigenpy.is_approx(np.eye(rows, rows), cod.pseudoInverse())
eigenpy::is_approx
EIGEN_DONT_INLINE bool is_approx(const Eigen::SparseMatrixBase< MatrixType1 > &mat1, const Eigen::SparseMatrixBase< MatrixType2 > &mat2)
Definition: is-approx.hpp:36


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Nov 2 2024 02:14:45