/
types.jl
255 lines (213 loc) · 7.19 KB
/
types.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"""
ScaledHotVector{T}
Represents a vector of at most one value different from 0.
"""
struct ScaledHotVector{T} <: AbstractVector{T}
active_val::T
val_idx::Int
len::Int
end
Base.size(v::ScaledHotVector) = (v.len,)
@inline function Base.getindex(v::ScaledHotVector{T}, idx::Integer) where {T}
@boundscheck if !(1 ≤ idx ≤ length(v))
throw(BoundsError(v, idx))
end
if v.val_idx != idx
return zero(T)
end
return v.active_val
end
Base.sum(v::ScaledHotVector) = v.active_val
function LinearAlgebra.dot(v1::ScaledHotVector{<:Number}, v2::AbstractVector{<:Number})
return conj(v1.active_val) * v2[v1.val_idx]
end
function LinearAlgebra.dot(v1::ScaledHotVector{<:Number}, v2::SparseArrays.SparseVector{<:Number})
return conj(v1.active_val) * v2[v1.val_idx]
end
LinearAlgebra.dot(v1::AbstractVector{<:Number}, v2::ScaledHotVector{<:Number}) = conj(dot(v2, v1))
LinearAlgebra.dot(v1::SparseArrays.SparseVector{<:Number}, v2::ScaledHotVector{<:Number}) =
conj(dot(v2, v1))
function LinearAlgebra.dot(v1::ScaledHotVector{<:Number}, v2::ScaledHotVector{<:Number})
if length(v1) != length(v2)
throw(DimensionMismatch("v1 and v2 do not have matching sizes"))
end
return conj(v1.active_val) * v2.active_val * (v1.val_idx == v2.val_idx)
end
LinearAlgebra.norm(v::ScaledHotVector) = abs(v.active_val)
function Base.:*(v::ScaledHotVector, x::Number)
return ScaledHotVector(v.active_val * x, v.val_idx, v.len)
end
Base.:*(x::Number, v::ScaledHotVector) = v * x
function Base.:+(x::ScaledHotVector, y::AbstractVector)
if length(x) != length(y)
throw(DimensionMismatch())
end
yc = Base.copymutable(y)
@inbounds yc[x.val_idx] += x.active_val
return yc
end
Base.:+(y::AbstractVector, x::ScaledHotVector) = x + y
function Base.:+(x::ScaledHotVector{T1}, y::ScaledHotVector{T2}) where {T1,T2}
n = length(x)
T = promote_type(T1, T2)
if n != length(y)
throw(DimensionMismatch())
end
res = spzeros(T, n)
@inbounds res[x.val_idx] = x.active_val
@inbounds res[y.val_idx] += y.active_val
return res
end
Base.:-(x::ScaledHotVector{T}) where {T} = ScaledHotVector{T}(-x.active_val, x.val_idx, x.len)
Base.:-(x::AbstractVector, y::ScaledHotVector) = +(x, -y)
Base.:-(x::ScaledHotVector, y::AbstractVector) = +(x, -y)
Base.:-(x::ScaledHotVector, y::ScaledHotVector) = +(x, -y)
Base.similar(v::ScaledHotVector{T}) where {T} = spzeros(T, length(v))
function Base.convert(::Type{Vector{T}}, v::ScaledHotVector) where {T}
vc = zeros(T, v.len)
vc[v.val_idx] = v.active_val
return vc
end
function Base.isequal(a::ScaledHotVector, b::ScaledHotVector)
return a.len == b.len && a.val_idx == b.val_idx && isequal(a.active_val, b.active_val)
end
function Base.copyto!(dst::SparseArrays.SparseVector, src::ScaledHotVector)
for idx in eachindex(src)
dst[idx] = 0
end
SparseArrays.dropzeros!(dst)
dst[src.val_idx] = src.active_val
return dst
end
function active_set_update_scale!(x::IT, lambda, atom::ScaledHotVector) where {IT}
x .*= (1 - lambda)
x[atom.val_idx] += lambda * atom.active_val
return x
end
"""
RankOneMatrix{T, UT, VT}
Represents a rank-one matrix `R = u * vt'`.
Composes like a charm.
"""
struct RankOneMatrix{T,UT<:AbstractVector,VT<:AbstractVector} <: AbstractMatrix{T}
u::UT
v::VT
end
function RankOneMatrix(u::UT, v::VT) where {UT,VT}
T = promote_type(eltype(u), eltype(v))
return RankOneMatrix{T,UT,VT}(u, v)
end
# not checking indices
Base.@propagate_inbounds function Base.getindex(R::RankOneMatrix, i, j)
@boundscheck (checkbounds(R.u, i); checkbounds(R.v, j))
@inbounds R.u[i] * R.v[j]
end
Base.size(R::RankOneMatrix) = (length(R.u), length(R.v))
function Base.:*(R::RankOneMatrix, v::AbstractVector)
temp = fast_dot(R.v, v)
return R.u * temp
end
function Base.:*(R::RankOneMatrix, M::AbstractMatrix)
temp = R.v' * M
return RankOneMatrix(R.u, temp')
end
function Base.:*(R1::RankOneMatrix, R2::RankOneMatrix)
# middle product
temp = fast_dot(R1.v, R2.u)
return RankOneMatrix(R1.u * temp, R2.v)
end
Base.Matrix(R::RankOneMatrix) = R.u * R.v'
Base.collect(R::RankOneMatrix) = Matrix(R)
Base.copymutable(R::RankOneMatrix) = Matrix(R)
Base.copy(R::RankOneMatrix) = RankOneMatrix(copy(R.u), copy(R.v))
function Base.convert(::Type{<:RankOneMatrix{T,Vector{T},Vector{T}}}, R::RankOneMatrix) where {T}
return RankOneMatrix(convert(Vector{T}, R.u), convert(Vector{T}, R.v))
end
function LinearAlgebra.dot(
R::RankOneMatrix{T1},
S::SparseArrays.AbstractSparseMatrixCSC{T2},
) where {T1<:Real,T2<:Real}
(m, n) = size(R)
T = promote_type(T1, T2)
if (m, n) != size(S)
throw(DimensionMismatch("Size mismatch"))
end
s = zero(T)
if m * n == 0
return s
end
rows = SparseArrays.rowvals(S)
vals = SparseArrays.nonzeros(S)
@inbounds for j in 1:n
for ridx in SparseArrays.nzrange(S, j)
i = rows[ridx]
v = vals[ridx]
s += v * R.u[i] * R.v[j]
end
end
return s
end
LinearAlgebra.dot(R::RankOneMatrix, M::Matrix) = dot(R.u, M, R.v)
LinearAlgebra.dot(M::Matrix, R::RankOneMatrix) = conj(dot(R, M))
Base.@propagate_inbounds function Base.:-(a::RankOneMatrix, b::RankOneMatrix)
@boundscheck size(a) == size(b) || throw(DimensionMismatch())
r = similar(a)
@inbounds for j in 1:size(a, 2)
for i in 1:size(a, 1)
r[i, j] = a.u[i] * a.v[j] - b.u[i] * b.v[j]
end
end
return r
end
Base.:-(x::RankOneMatrix) = RankOneMatrix(-x.u, x.v)
Base.:*(x::Number, m::RankOneMatrix) = RankOneMatrix(x * m.u, m.v)
Base.:*(m::RankOneMatrix, x::Number) = RankOneMatrix(x * m.u, m.v)
Base.@propagate_inbounds function Base.:+(a::RankOneMatrix, b::RankOneMatrix)
@boundscheck size(a) == size(b) || throw(DimensionMismatch())
r = similar(a)
@inbounds for j in 1:size(a, 2)
for i in 1:size(a, 1)
r[i, j] = a.u[i] * a.v[j] + b.u[i] * b.v[j]
end
end
return r
end
LinearAlgebra.norm(R::RankOneMatrix) = norm(R.u) * norm(R.v)
Base.@propagate_inbounds function Base.isequal(a::RankOneMatrix, b::RankOneMatrix)
if size(a) != size(b)
return false
end
if isequal(a.u, b.u) && isequal(a.v, b.v)
return true
end
# needs to check actual values
@inbounds for j in 1:size(a, 2)
for i in 1:size(a, 1)
if !isequal(a.u[i] * a.v[j], b.u[i] * b.v[j])
return false
end
end
end
return true
end
Base.@propagate_inbounds function muladd_memory_mode(::InplaceEmphasis, d::Matrix, x::Union{RankOneMatrix, Matrix}, v::RankOneMatrix)
@boundscheck size(d) == size(x) || throw(DimensionMismatch())
@boundscheck size(d) == size(v) || throw(DimensionMismatch())
m, n = size(d)
@inbounds for j in 1:n
for i in 1:m
d[i,j] = x[i,j] - v[i,j]
end
end
return d
end
Base.@propagate_inbounds function muladd_memory_mode(::InplaceEmphasis, x::Matrix, gamma::Real, d::RankOneMatrix)
@boundscheck size(d) == size(x) || throw(DimensionMismatch())
m, n = size(x)
@inbounds for j in 1:n
for i in 1:m
x[i,j] -= gamma * d[i,j]
end
end
return x
end