@@ -4,56 +4,17 @@ using LinearAlgebra
4
4
using Random
5
5
using SparseArrays
6
6
7
- import Base: size, eltype, similar, copyto!, fill!, length
7
+ import Base: size, eltype, similar, copyto!, fill!, length, axes, getindex, setindex!
8
8
import LinearAlgebra: norm, mul!, rmul!, lmul!
9
9
10
10
# Type used in Dampenedtest
11
- # solve (A'A + diag(v).^2 ) x = b
12
- # using LSMR in the augmented space A' = [A ; diag(v)] b' = [b; zeros(size(A, 2)]
13
- mutable struct DampenedVector{Ty, Tx}
14
- y:: Ty
15
- x:: Tx
16
- end
17
-
18
- eltype (a:: DampenedVector ) = promote_type (eltype (a. y), eltype (a. x))
19
- norm (a:: DampenedVector ) = sqrt (norm (a. y)^ 2 + norm (a. x)^ 2 )
20
-
21
- function Base. Broadcast. broadcast! (f:: Tf , to:: DampenedVector , from:: DampenedVector , args... ) where {Tf}
22
- to. x .= f .(from. x, args... )
23
- to. y .= f .(from. y, args... )
24
- to
25
- end
26
-
27
- function copyto! (a:: DampenedVector{Ty, Tx} , b:: DampenedVector{Ty, Tx} ) where {Ty, Tx}
28
- copyto! (a. y, b. y)
29
- copyto! (a. x, b. x)
30
- a
31
- end
32
-
33
- function fill! (a:: DampenedVector , α:: Number )
34
- fill! (a. y, α)
35
- fill! (a. x, α)
36
- a
37
- end
38
-
39
- lmul! (α:: Number , a:: DampenedVector ) = rmul! (a, α)
40
-
41
- function rmul! (a:: DampenedVector , α:: Number )
42
- rmul! (a. y, α)
43
- rmul! (a. x, α)
44
- a
45
- end
46
-
47
- similar (a:: DampenedVector , T) = DampenedVector (similar (a. y, T), similar (a. x, T))
48
- length (a:: DampenedVector ) = length (a. y) + length (a. x)
49
-
50
- mutable struct DampenedMatrix{TA, Tx}
11
+ # solve (A'A + diag(v).^2 ) x = A'b
12
+ # using LSMR in the augmented space à = [A ; diag(v)] b̃ = [b; zeros(size(A, 2)]
13
+ struct DampenedMatrix{Tv,TA<: AbstractMatrix{Tv} ,TD<: AbstractVector{Tv} } <: AbstractMatrix{Tv}
51
14
A:: TA
52
- diagonal:: Tx
15
+ diagonal:: TD
53
16
end
54
17
55
- eltype (A:: DampenedMatrix ) = promote_type (eltype (A. A), eltype (A. diagonal))
56
-
57
18
function size (A:: DampenedMatrix )
58
19
m, n = size (A. A)
59
20
l = length (A. diagonal)
63
24
function size (A:: DampenedMatrix , dim:: Integer )
64
25
m, n = size (A. A)
65
26
l = length (A. diagonal)
66
- dim == 1 ? (m + l) :
67
- dim == 2 ? n : 1
27
+ dim == 1 ? (m + l) : (dim == 2 ? n : 1 )
68
28
end
69
29
70
- function mul! (b:: DampenedVector{Ty, Tx} , mw:: DampenedMatrix{TA, Tx} , a:: Tx ,
71
- α:: Number , β:: Number ) where {TA, Tx, Ty}
72
- if β != 1.
73
- if β == 0.
74
- fill! (b, 0. )
75
- else
76
- rmul! (b, β)
77
- end
78
- end
79
- mul! (b. y, mw. A, a, α, 1.0 )
80
- map! ((z, x, y)-> z + α * x * y, b. x, b. x, a, mw. diagonal)
81
- return b
30
+ function mul! (y:: AbstractVector{Tv} , mw:: DampenedMatrix , x:: AbstractVector{Tv} ) where {Tv}
31
+ m₁ = size (mw. A, 1 )
32
+ m₂ = size (mw, 1 )
33
+ mul! (view (y, 1 : m₁), mw. A, x)
34
+ y[m₁+ 1 : m₂] .= mw. diagonal .* x
35
+ return y
82
36
end
83
37
84
- function mul! (b:: Tx , mw:: Adjoint{DampenedMatrix{TA, Tx}} , a:: DampenedVector{Ty, Tx} ,
85
- α:: Number , β:: Number ) where {TA, Tx, Ty}
86
- if β != 1.
87
- if β == 0.
88
- fill! (b, 0. )
89
- else
90
- rmul! (b, β)
91
- end
92
- end
93
- mul! (b, adjoint (mw. A), a. y, α, 1.0 )
94
- map! ((z, x, y)-> z + α * x * y, b, b, a. x, mw. diagonal)
95
- return b
38
+ function mul! (y:: AbstractVector , mw:: Adjoint{Tv,<:DampenedMatrix} , x:: AbstractVector ) where {Tv}
39
+ m₁ = size (mw. parent. A, 1 )
40
+ m₂ = size (mw. parent, 1 )
41
+ mul! (y, adjoint (mw. parent. A), view (x, 1 : m₁))
42
+ y .+ = mw. parent. diagonal .* view (x, m₁+ 1 : m₂)
43
+ return y
96
44
end
97
45
98
46
"""
138
86
@test norm (b - A * x) ≤ 1e-4
139
87
end
140
88
141
- # @testset "Dampened test" for (m, n) = ((10, 10), (20, 10))
142
- # # Test used to make sure A, b can be generic matrix / vector
143
- # b = rand(m)
144
- # A = rand(m, n)
145
- # v = rand(n)
146
- # Adampened = DampenedMatrix(A, v)
147
- # bdampened = DampenedVector(b, zeros(n))
148
- # x, ch = lsmr(Adampened, bdampened , log=true)
149
- # @test norm((A'A + Matrix(Diagonal(v)) .^ 2)x - A'b) ≤ 1e-3
150
- # end
89
+ @testset " Dampened test" for (m, n) = ((10 , 10 ), (20 , 10 ))
90
+ # Test used to make sure A, b can be generic matrix / vector
91
+ b = rand (m)
92
+ A = rand (m, n)
93
+ v = rand (n)
94
+ A′ = DampenedMatrix (A, v)
95
+ b′ = [b; zeros (n)]
96
+ x, ch = lsmr (A′, b′ , log= true )
97
+ @test norm ((A' A + Matrix (Diagonal (v)) .^ 2 )x - A' b) ≤ 1e-3
98
+ end
151
99
end
0 commit comments