-
Notifications
You must be signed in to change notification settings - Fork 143
/
linalg.jl
418 lines (342 loc) · 12.6 KB
/
linalg.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import Base: +, -, *, /, \
# TODO: more operators, like AbstractArray
# Unary ops
@inline -(a::StaticArray) = map(-, a)
# Binary ops
# Between arrays
@inline +(a::StaticArray, b::StaticArray) = map(+, a, b)
@inline +(a::AbstractArray, b::StaticArray) = map(+, a, b)
@inline +(a::StaticArray, b::AbstractArray) = map(+, a, b)
@inline -(a::StaticArray, b::StaticArray) = map(-, a, b)
@inline -(a::AbstractArray, b::StaticArray) = map(-, a, b)
@inline -(a::StaticArray, b::AbstractArray) = map(-, a, b)
# Scalar-array
@inline +(a::Number, b::StaticArray) = broadcast(+, a, b)
@inline +(a::StaticArray, b::Number) = broadcast(+, a, b)
@inline -(a::Number, b::StaticArray) = broadcast(-, a, b)
@inline -(a::StaticArray, b::Number) = broadcast(-, a, b)
@inline *(a::Number, b::StaticArray) = broadcast(*, a, b)
@inline *(a::StaticArray, b::Number) = broadcast(*, a, b)
@inline /(a::StaticArray, b::Number) = broadcast(/, a, b)
@inline \(a::Number, b::StaticArray) = broadcast(\, a, b)
# With UniformScaling
@generated function +(a::StaticMatrix, b::UniformScaling)
n = size(a,1)
newtype = similar_type(a, promote_type(eltype(a), eltype(b)))
if size(a,2) != n
error("Dimension mismatch")
end
exprs = [i == j ? :(a[$(sub2ind(size(a), i, j))] + b.λ) : :(a[$(sub2ind(size(a), i, j))]) for i = 1:n, j = 1:n]
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function +(a::UniformScaling, b::StaticMatrix)
n = size(b,1)
newtype = similar_type(b, promote_type(eltype(a), eltype(b)))
if size(b,2) != n
error("Dimension mismatch")
end
exprs = [i == j ? :(a.λ + b[$(sub2ind(size(b), i, j))]) : :(b[$(sub2ind(size(b), i, j))]) for i = 1:n, j = 1:n]
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function -(a::StaticMatrix, b::UniformScaling)
n = size(a,1)
newtype = similar_type(a, promote_type(eltype(a), eltype(b)))
if size(a,2) != n
error("Dimension mismatch")
end
exprs = [i == j ? :(a[$(sub2ind(size(a), i, j))] - b.λ) : :(a[$(sub2ind(size(a), i, j))]) for i = 1:n, j = 1:n]
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function -(a::UniformScaling, b::StaticMatrix)
n = size(b,1)
newtype = similar_type(b, promote_type(eltype(a), eltype(b)))
if size(b,2) != n
error("Dimension mismatch")
end
exprs = [i == j ? :(a.λ - b[$(sub2ind(size(b), i, j))]) : :(-b[$(sub2ind(size(b), i, j))]) for i = 1:n, j = 1:n]
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@inline *(a::UniformScaling, b::Union{StaticMatrix,StaticVector}) = a.λ * b
@inline *(a::StaticMatrix, b::UniformScaling) = a * b.λ
@inline \(a::UniformScaling, b::Union{StaticMatrix,StaticVector}) = a.λ \ b
@inline /(a::StaticMatrix, b::UniformScaling) = a / b.λ
# Transpose, conjugate, etc
# TODO different methods for v0.5, v0.6 (due to `RowVector`)
@inline conj(a::StaticArray) = map(conj, a)
@generated function transpose(v::StaticVector)
n = length(v)
newtype = similar_type(v, Size(1,n))
exprs = [:(v[$j]) for j = 1:n]
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function ctranspose(v::StaticVector)
n = length(v)
newtype = similar_type(v, Size(1,n))
exprs = [:(conj(v[$j])) for j = 1:n]
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function transpose(m::StaticMatrix)
(s1,s2) = size(m)
if s1 == s2
newtype = m
else
newtype = similar_type(m, Size(s2,s1))
end
exprs = [:(m[$j1, $j2]) for j2 = 1:s2, j1 = 1:s1]
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function ctranspose(m::StaticMatrix)
(s1,s2) = size(m)
if s1 == s2
newtype = m
else
newtype = similar_type(m, Size(s2,s1))
end
exprs = [:(conj(m[$j1, $j2])) for j2 = 1:s2, j1 = 1:s1]
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@inline vcat(a::Union{StaticVector,StaticMatrix}) = a
@generated function vcat(a::Union{StaticVector, StaticMatrix}, b::Union{StaticVector,StaticMatrix})
if size(a,2) != size(b,2)
error("Dimension mismatch")
end
if a <: StaticVector && b <: StaticVector
newtype = similar_type(a, Size(length(a) + length(b)))
exprs = vcat([:(a[$i]) for i = 1:length(a)],
[:(b[$i]) for i = 1:length(b)])
else
newtype = similar_type(a, Size(size(a,1) + size(b,1), size(a,2)))
exprs = [((i <= size(a,1)) ? ((a <: StaticVector) ? :(a[$i]) : :(a[$i,$j]))
: ((b <: StaticVector) ? :(b[$(i-size(a,1))]) : :(b[$(i-size(a,1)),$j])))
for i = 1:(size(a,1)+size(b,1)), j = 1:size(a,2)]
end
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
# TODO make these more efficient
@inline vcat(a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix}, c::Union{StaticVector,StaticMatrix}) =
vcat(vcat(a,b), c)
@inline vcat(a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix}, c::Union{StaticVector,StaticMatrix}...) =
vcat(vcat(a,b), c...)
@generated function hcat(a::StaticVector)
newtype = similar_type(a, Size(length(a),1))
exprs = [:(a[$i]) for i = 1:length(a)]
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@inline hcat(a::StaticMatrix) = a
@generated function hcat(a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix})
if size(a,1) != size(b,1)
error("Dimension mismatch")
end
exprs1 = [:(a[$i]) for i = 1:length(a)]
exprs2 = [:(b[$i]) for i = 1:length(b)]
newtype = similar_type(a, Size(size(a,1), size(a,2) + size(b,2)))
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs1..., exprs2...)))
end
end
# TODO make these more efficient
@inline hcat(a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix}, c::Union{StaticVector,StaticMatrix}) =
hcat(hcat(a,b), c)
@inline hcat(a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix}, c::Union{StaticVector,StaticMatrix}...) =
hcat(hcat(a,b), c...)
@inline Base.zero{SA <: StaticArray}(a::SA) = zeros(SA)
@inline Base.zero{SA <: StaticArray}(a::Type{SA}) = zeros(SA)
@inline one{SM <: StaticMatrix}(::SM) = one(SM)
@generated function one{SM <: StaticArray}(::Type{SM})
s = size(SM)
if (length(s) != 2) || (s[1] != s[2])
error("multiplicative identity defined only for square matrices")
end
T = eltype(SM)
if T == Any
T = Float64
end
e = eye(T, s...)
return quote
$(Expr(:meta, :inline))
$(Expr(:call, SM, Expr(:tuple, e...)))
end
end
@inline eye{SM <: StaticMatrix}(::SM) = eye(SM)
@generated function eye{SM <: StaticArray}(::Type{SM})
s = size(SM)
if length(s) != 2
error("`eye` is only defined for matrices")
end
T = eltype(SM)
if T == Any
T = Float64
end
e = eye(T, s...)
return quote
$(Expr(:meta, :inline))
$(Expr(:call, SM, Expr(:tuple, e...)))
end
end
@generated function diagm(v::StaticVector)
T = eltype(v)
exprs = [i == j ? :(v[$i]) : zero(T) for i = 1:length(v), j = 1:length(v)]
newtype = similar_type(v, Size(length(v), length(v)))
return quote
$(Expr(:meta, :inline))
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function cross(a::StaticVector, b::StaticVector)
S = typeof(zero(eltype(a))*zero(eltype(b)))
if length(a) === 3 && length(b) === 3
if S <: Unsigned
return quote
$(Expr(:meta, :inline))
similar_type(a, $(typeof(Signed(zero(eltype(a))*zero(eltype(b))))))((Signed(a[2]*b[3])-Signed(a[3]*b[2]), Signed(a[3]*b[1])-Signed(a[1]*b[3]), Signed(a[1]*b[2])-Signed(a[2]*b[1])))
end
else
return quote
$(Expr(:meta, :inline))
similar_type(a, $S)((a[2]*b[3]-a[3]*b[2], a[3]*b[1]-a[1]*b[3], a[1]*b[2]-a[2]*b[1]))
end
end
elseif length(a) === 2 && length(b) === 2
return quote
$(Expr(:meta, :inline))
promote_type(eltype(a), eltype(b))(a[1]*b[2]-a[2]*b[1])
end
elseif length(a) === 2 && length(b) === 2
return quote
$(Expr(:meta, :inline))
promote_type(eltype(a), eltype(b))(a[1]*b[2]-a[2]*b[1])
end
else
error("Cross product only defined for 3-vectors")
end
end
@generated function dot(a::StaticVector, b::StaticVector)
if length(a) == length(b)
expr = :(conj(a[1]) * b[1])
for j = 2:length(a)
expr = :($expr + conj(a[$j]) * b[$j])
end
return quote
$(Expr(:meta, :inline))
@inbounds return $expr
end
else
error("dot() expects vectors of same length. Got lengths $(length(a)) and $(length(b)).")
end
end
@generated function vecdot(a::StaticArray, b::StaticArray)
if length(a) == length(b)
expr = :(conj(a[1]) * b[1])
for j = 2:length(a)
expr = :($expr + conj(a[$j]) * b[$j])
end
return quote
$(Expr(:meta, :inline))
@inbounds return $expr
end
else
error("vecdot() expects arrays of same length. Got sizes $(size(a)) and $(size(b)).")
end
end
@inline norm(v::StaticVector) = vecnorm(v)
@inline norm(v::StaticVector, p::Real) = vecnorm(v, p)
@inline Base.LinAlg.norm_sqr(v::StaticVector) = mapreduce(abs2, +, zero(real(eltype(v))), v)
@generated function vecnorm(a::StaticArray)
if length(a) == 0
return zero(real(eltype(a)))
end
expr = :(real(conj(a[1]) * a[1]))
for j = 2:length(a)
expr = :($expr + real(conj(a[$j]) * a[$j]))
end
return quote
$(Expr(:meta, :inline))
@inbounds return sqrt($expr)
end
end
_norm_p0(x) = x == 0 ? zero(x) : one(x)
@generated function vecnorm(a::StaticArray, p::Real)
if length(a) == 0
return zero(real(eltype(a)))
end
expr = :(abs(a[1])^p)
for j = 2:length(a)
expr = :($expr + abs(a[$j])^p)
end
expr_p1 = :(abs(a[1]))
for j = 2:length(a)
expr_p1 = :($expr_p1 + abs(a[$j]))
end
return quote
$(Expr(:meta, :inline))
if p == Inf
return mapreduce(abs, max, $(zero(real(eltype(a)))), a)
elseif p == 1
@inbounds return $expr_p1
elseif p == 2
return vecnorm(a)
elseif p == 0
return mapreduce(_norm_p0, +, $(zero(real(eltype(a)))), a)
else
@inbounds return ($expr)^(inv(p))
end
end
end
@inline Base.normalize(a::StaticVector) = inv(vecnorm(a))*a
@inline Base.normalize(a::StaticVector, p::Real) = inv(vecnorm(a, p))*a
@inline Base.normalize!(a::StaticVector) = (a .*= inv(vecnorm(a)))
@inline Base.normalize!(a::StaticVector, p::Real) = (a.*= inv(vecnorm(a, p)))
@generated function trace(a::StaticMatrix)
s = size(a)
if s[1] != s[2]
error("matrix is not square")
end
if s[1] == 0
return zero(eltype(a))
end
exprs = [:(a[$(sub2ind(s, i, i))]) for i = 1:s[1]]
total = reduce((ex1, ex2) -> :($ex1 + $ex2), exprs)
return quote
$(Expr(:meta, :inline))
@inbounds return $total
end
end
@inline Size{T,SA<:StaticArray}(::Union{Symmetric{T,SA}, Type{Symmetric{T,SA}}}) = Size(SA)
@inline Size{T,SA<:StaticArray}(::Union{Hermitian{T,SA}, Type{Hermitian{T,SA}}}) = Size(SA)
# some micro-optimizations
@inline Base.LinAlg.checksquare{SM<:StaticMatrix}(::SM) = _checksquare(Size(SM))
@inline Base.LinAlg.checksquare{SM<:StaticMatrix}(::Type{SM}) = _checksquare(Size(SM))
@pure _checksquare{S}(::Size{S}) = (S[1] == S[2] || error("marix must be square"); S[1])
@inline Base.LinAlg.Symmetric(A::StaticMatrix, uplo::Char='U') = (Base.LinAlg.checksquare(A);Symmetric{eltype(A),typeof(A)}(A, uplo))
@inline Base.LinAlg.Hermitian(A::StaticMatrix, uplo::Char='U') = (Base.LinAlg.checksquare(A);Hermitian{eltype(A),typeof(A)}(A, uplo))