@@ -63,39 +63,23 @@ function tridiag(T::Type, m::Integer, n::Integer)
63
63
end
64
64
tridiag (m:: Integer , n:: Integer ) = tridiag (Float64, m:: Integer , n:: Integer )
65
65
66
- function randn_float64 (m:: Integer , n:: Integer )
67
- a= randn (m,n)
68
- b = Matrix {Float64} (undef, m, n)
69
- for i= 1 : n
70
- for j= 1 : m
71
- b[j,i]= convert (Float64,a[j,i])
72
- end
73
- end
74
- return b
75
- end
76
-
77
- function randn_float32 (m:: Integer , n:: Integer )
78
- a= randn (m,n)
79
- b = Matrix {Float32} (undef, m, n)
80
- for i= 1 : n
81
- for j= 1 : m
82
- b[j,i]= convert (Float32,a[j,i])
83
- end
84
- end
85
- return b
86
- end
87
-
66
+ function test_pinv (a,tol1,tol2)
67
+ m,n = size (a)
88
68
89
- function test_pinv (a,m,n,tol1,tol2,tol3)
90
69
apinv = @inferred pinv (a)
91
-
70
+ @test size (apinv) == (n,m)
92
71
@test norm (a* apinv* a- a)/ norm (a) ≈ 0 atol= tol1
93
- x0 = randn (n); b = a* x0; x = apinv* b
72
+ @test norm (apinv* a* apinv- apinv)/ norm (apinv) ≈ 0 atol= tol1
73
+ b = a* randn (n)
74
+ x = apinv* b
94
75
@test norm (a* x- b)/ norm (b) ≈ 0 atol= tol1
95
- apinv = pinv (a,sqrt (eps (real (one (eltype (a))))))
96
76
77
+ apinv = @inferred pinv (a,sqrt (eps (real (one (eltype (a))))))
78
+ @test size (apinv) == (n,m)
97
79
@test norm (a* apinv* a- a)/ norm (a) ≈ 0 atol= tol2
98
- x0 = randn (n); b = a* x0; x = apinv* b
80
+ @test norm (apinv* a* apinv- apinv)/ norm (apinv) ≈ 0 atol= tol2
81
+ b = a* randn (n)
82
+ x = apinv* b
99
83
@test norm (a* x- b)/ norm (b) ≈ 0 atol= tol2
100
84
end
101
85
104
88
default_tol = (real (one (eltya))) * max (m,n) * 10
105
89
tol1 = 1e-2
106
90
tol2 = 1e-5
107
- tol3 = 1e-5
108
91
if real (eltya) == Float32
109
92
tol1 = 1e0
110
93
tol2 = 1e-2
111
- tol3 = 1e-2
112
94
end
113
95
@testset " dense/ill-conditioned matrix" begin
114
- # ## a = randn_float64(m,n) * hilb(eltya,n)
115
96
a = hilb (eltya, m, n)
116
- test_pinv (a, m, n, tol1, tol2, tol3 )
97
+ test_pinv (a, tol1, tol2)
117
98
end
118
99
@testset " dense/diagonal matrix" begin
119
100
a = onediag (eltya, m, n)
120
- test_pinv (a, m, n, default_tol, default_tol, default_tol)
101
+ test_pinv (a, default_tol, default_tol)
121
102
end
122
103
@testset " dense/tri-diagonal matrix" begin
123
104
a = tridiag (eltya, m, n)
124
- test_pinv (a, m, n, default_tol, tol2, default_tol )
105
+ test_pinv (a, default_tol, tol2)
125
106
end
126
107
@testset " Diagonal matrix" begin
127
108
a = onediag_sparse (eltya, m)
128
- test_pinv (a, m, m, default_tol, default_tol, default_tol)
109
+ test_pinv (a, default_tol, default_tol)
129
110
end
130
111
@testset " Vector" begin
131
112
a = rand (eltya, m)
164
145
@test C ≈ ones (2 ,2 )
165
146
end
166
147
148
+ @testset " non-square diagonal matrices" begin
149
+ A = eltya[1 0 ; 0 1 ; 0 0 ]
150
+ B = pinv (A)
151
+ @test A* B* A ≈ A
152
+ @test B* A* B ≈ B
153
+
154
+ A = eltya[1 0 0 ; 0 1 0 ]
155
+ B = pinv (A)
156
+ @test A* B* A ≈ A
157
+ @test B* A* B ≈ B
158
+ end
159
+
167
160
if eltya <: LinearAlgebra.BlasReal
168
161
@testset " sub-normal numbers/vectors/matrices" begin
169
162
a = pinv (floatmin (eltya)/ 100 )
0 commit comments