-
Notifications
You must be signed in to change notification settings - Fork 8
/
LinearAlgebra.jl
487 lines (431 loc) · 13.1 KB
/
LinearAlgebra.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# Copyright (c) 2019 MutableArithmetics.jl contributors
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
# one at http://mozilla.org/MPL/2.0/.
# This file contains methods to implement the MutableArithmetics API for types
# in the LinearAlgebra stdlib.
mutability(::Type{<:Array}) = IsMutable()
mutable_copy(A::Array) = copy_if_mutable.(A)
# Sum
# By default, we assume the return value is an `Array` because having a
# different method for all combinations of cases would be cumbersome. A more
# specific method could be implemented for specific cases.
function promote_operation(
op::Union{typeof(+),typeof(-)},
::Type{<:AbstractArray{S,N}},
::Type{<:AbstractArray{T,M}},
) where {S,T,N,M}
# If `N != M`, we need the axes between `min(N,M)+1` and `max(N,M)` to be
# `Base.OneTo(1)`. In any cases, the axes from `1` to `min(N,M)` must also
# match.
return Array{promote_operation(op, S, T),max(N, M)}
end
function promote_operation(
op::Union{typeof(+),typeof(-)},
::Type{LinearAlgebra.UniformScaling{S}},
::Type{Matrix{T}},
) where {S,T}
return Matrix{promote_operation(op, S, T)}
end
function promote_operation(
op::Union{typeof(+),typeof(-)},
::Type{Matrix{T}},
::Type{LinearAlgebra.UniformScaling{S}},
) where {S,T}
return Matrix{promote_operation(op, S, T)}
end
# Only `Scaling`
function operate!(
op::Union{typeof(+),typeof(-)},
A::Matrix,
B::LinearAlgebra.UniformScaling,
)
n = LinearAlgebra.checksquare(A)
for i in 1:n
A[i, i] = operate!!(op, A[i, i], B)
end
return A
end
function operate!(
op::AddSubMul,
A::Matrix,
B::Scaling,
C::Scaling,
D::Vararg{Scaling,N},
) where {N}
return operate!(add_sub_op(op), A, *(B, C, D...))
end
# TODO(odow): these are the only cases that appear in all of JuliaHub. They
# should become private.
mul_rhs(::typeof(+)) = add_mul
mul_rhs(::typeof(-)) = sub_mul
# We redirect the mutable `A + B` into `A .+ B`.
# To be consistent with Julia Base, we first call `promote_shape`
# which throws an error if the broadcasted dimension are not singleton
# and we check that the axes of `A` are indeed the axes of the array
# that would be returned in Julia Base (maybe we could relax this ?).
function _check_dims(A, B)
if axes(A) != promote_shape(A, B)
throw(
DimensionMismatch(
"Cannot sum or substract a matrix of axes `$(axes(B))` into" *
" matrix of axes `$(axes(A))`, expected axes" *
" `$(promote_shape(A, B))`.",
),
)
end
return
end
function operate!(op::Union{typeof(+),typeof(-)}, A::Array, B::AbstractArray)
_check_dims(A, B)
return broadcast!(op, A, B)
end
# We call `scaling_to_number` as `UniformScaling` do not support broadcasting
function operate!(
op::AddSubMul,
A::Array,
B::AbstractArray,
α::Vararg{Scaling,M},
) where {M}
_check_dims(A, B)
return broadcast!(op, A, B, scaling_to_number.(α)...)
end
function operate!(
op::AddSubMul,
A::Array,
α::Scaling,
B::AbstractArray,
β::Vararg{Scaling,M},
) where {M}
_check_dims(A, B)
return broadcast!(op, A, scaling_to_number(α), B, scaling_to_number.(β)...)
end
function operate!(
op::AddSubMul,
A::Array,
α1::Scaling,
α2::Scaling,
B::AbstractArray,
β::Vararg{Scaling,M},
) where {M}
_check_dims(A, B)
return broadcast!(
op,
A,
scaling_to_number(α1),
scaling_to_number(α2),
B,
scaling_to_number.(β)...,
)
end
# Fallback, we may be able to be more efficient in more cases by adding more
# specialized methods.
function operate!(op::AddSubMul, A::Array, x, y)
return operate!(op, A, x * y)
end
function operate!(op::AddSubMul, A::Array, x, y, args::Vararg{Any,N}) where {N}
@assert N > 0
return operate!(op, A, x, *(y, args...))
end
# Product
function similar_array_type(
::Type{LinearAlgebra.Symmetric{T,MT}},
::Type{S},
) where {S,T,MT}
return LinearAlgebra.Symmetric{S,similar_array_type(MT, S)}
end
similar_array_type(::Type{Array{T,N}}, ::Type{S}) where {S,T,N} = Array{S,N}
function promote_operation(
op::typeof(*),
A::Type{<:AbstractArray{T}},
::Type{S},
) where {S,T}
return similar_array_type(A, promote_operation(op, T, S))
end
function promote_operation(
op::typeof(*),
::Type{S},
A::Type{<:AbstractArray{T}},
) where {S,T}
return similar_array_type(A, promote_operation(op, S, T))
end
# `{S}` and `{T}` are used to avoid ambiguity with above methods.
function promote_operation(
::typeof(*),
A::Type{<:AbstractArray{S}},
B::Type{<:AbstractArray{T}},
) where {S,T}
return promote_array_mul(A, B)
end
function promote_sum_mul(T::Type, S::Type)
U = promote_operation(*, T, S)
return promote_operation(+, U, U)
end
function promote_array_mul(::Type{Matrix{S}}, ::Type{Vector{T}}) where {S,T}
return Vector{promote_sum_mul(S, T)}
end
function promote_array_mul(
::Type{<:AbstractMatrix{S}},
::Type{<:AbstractMatrix{T}},
) where {S,T}
return Matrix{promote_sum_mul(S, T)}
end
function promote_array_mul(
::Type{<:AbstractVector{S}},
::Type{<:LinearAlgebra.Adjoint{T,<:AbstractVector{T}}},
) where {S,T}
return Matrix{promote_sum_mul(S, T)}
end
function promote_array_mul(
::Type{<:AbstractMatrix{S}},
::Type{<:AbstractVector{T}},
) where {S,T}
return Vector{promote_sum_mul(S, T)}
end
################################################################################
# We roll our own matmul here (instead of using Julia's generic fallbacks)
# because doing so allows us to accumulate the expressions for the inner loops
# in-place.
# Additionally, Julia's generic fallbacks can be finnicky when your array
# elements aren't `<:Number`.
# This method of `mul!` is adapted from upstream Julia. Note that we
# confuse transpose with adjoint.
#=
> Copyright (c) 2009-2018: Jeff Bezanson, Stefan Karpinski, Viral B. Shah,
> and other contributors:
>
> https://github.com/JuliaLang/julia/contributors
>
> Permission is hereby granted, free of charge, to any person obtaining
> a copy of this software and associated documentation files (the
> "Software"), to deal in the Software without restriction, including
> without limitation the rights to use, copy, modify, merge, publish,
> distribute, sublicense, and/or sell copies of the Software, and to
> permit persons to whom the Software is furnished to do so, subject to
> the following conditions:
>
> The above copyright notice and this permission notice shall be
> included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
> OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
> WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=#
function _dim_check(C::AbstractVector, A::AbstractMatrix, B::AbstractVector)
mB = length(B)
mA, nA = size(A)
if mB != nA
throw(
DimensionMismatch(
"matrix A has dimensions ($mA,$nA), vector B has length $mB",
),
)
end
if mA != length(C)
throw(
DimensionMismatch(
"result C has length $(length(C)), needs length $mA",
),
)
end
return
end
function _dim_check(C::AbstractMatrix, A::AbstractMatrix, B::AbstractMatrix)
mB, nB = size(B)
mA, nA = size(A)
if mB != nA
throw(
DimensionMismatch(
"matrix A has dimensions ($mA,$nA), matrix B has dimensions ($mB,$nB)",
),
)
end
if size(C, 1) != mA || size(C, 2) != nB
throw(
DimensionMismatch(
"result C has dimensions $(size(C)), needs ($mA,$nB)",
),
)
end
return
end
function _add_mul_array(buffer, C::Vector, A::AbstractMatrix, B::AbstractVector)
Astride = size(A, 1)
# We need a buffer to hold the intermediate multiplication.
@inbounds begin
for k in eachindex(B)
aoffs = (k - 1) * Astride
b = B[k]
for i in Base.OneTo(size(A, 1))
C[i] = buffered_operate!!(buffer, add_mul, C[i], A[aoffs+i], b)
end
end
end # @inbounds
return C
end
# This is incorrect if `C` is `LinearAlgebra.Symmetric` as we modify twice the
# same diagonal element.
function _add_mul_array(buffer, C::Matrix, A::AbstractMatrix, B::AbstractMatrix)
@inbounds begin
for i in 1:size(A, 1), j in 1:size(B, 2)
Ctmp = C[i, j]
for k in 1:size(A, 2)
Ctmp =
buffered_operate!!(buffer, add_mul, Ctmp, A[i, k], B[k, j])
end
C[i, j] = Ctmp
end
end # @inbounds
return C
end
function buffered_operate!(
buffer,
::typeof(add_mul),
C::VecOrMat,
A::AbstractMatrix,
B::AbstractVecOrMat,
)
_dim_check(C, A, B)
return _add_mul_array(buffer, C, A, B)
end
function buffer_for(
::typeof(add_mul),
::Type{<:VecOrMat{S}},
::Type{<:AbstractMatrix{T}},
::Type{<:AbstractVecOrMat{U}},
) where {S,T,U}
return buffer_for(add_mul, S, T, U)
end
function operate!(
::typeof(add_mul),
C::VecOrMat,
A::AbstractMatrix,
B::AbstractVecOrMat,
)
buffer = buffer_for(add_mul, typeof(C), typeof(A), typeof(B))
return buffered_operate!(buffer, add_mul, C, A, B)
end
function operate!(::typeof(zero), C::Union{Vector,Matrix})
# C may contain undefined values so we cannot call `zero!`
for i in eachindex(C)
@inbounds C[i] = zero(eltype(C))
end
return
end
function operate_to!(
C::AbstractArray,
::typeof(*),
A::AbstractArray,
B::AbstractArray,
)
operate!(zero, C)
return operate!(add_mul, C, A, B)
end
function undef_array(::Type{Array{T,N}}, axes::Vararg{Base.OneTo,N}) where {T,N}
return Array{T,N}(undef, length.(axes))
end
# This method is for things like StaticArrays which return something other than
# Base.OneTo for their axes. It isn't typed because there can be a mix of axes
# in the call.
function undef_array(::Type{T}, axes...) where {T}
return undef_array(T, convert.(Base.OneTo, axes)...)
end
# Does what `LinearAlgebra/src/matmul.jl` does for abstract matrices and
# vectors: estimate the resulting element type, allocate the resulting array but
# it redirects to `mul_to!` instead of `LinearAlgebra.mul!`.
function operate(
::typeof(*),
A::AbstractMatrix{S},
B::AbstractVector{T},
) where {T,S}
C = undef_array(promote_array_mul(typeof(A), typeof(B)), axes(A, 1))
return operate_to!(C, *, A, B)
end
function operate(
::typeof(*),
A::AbstractMatrix{S},
B::AbstractMatrix{T},
) where {T,S}
C = undef_array(
promote_array_mul(typeof(A), typeof(B)),
axes(A, 1),
axes(B, 2),
)
return operate_to!(C, *, A, B)
end
const _TransposeOrAdjoint{T,MT} =
Union{LinearAlgebra.Transpose{T,MT},LinearAlgebra.Adjoint{T,MT}}
function _mirror_transpose_or_adjoint(x, ::LinearAlgebra.Transpose)
return LinearAlgebra.transpose(x)
end
function _mirror_transpose_or_adjoint(x, ::LinearAlgebra.Adjoint)
return LinearAlgebra.adjoint(x)
end
function _mirror_transpose_or_adjoint(
A::Type{<:AbstractArray{T}},
::Type{<:LinearAlgebra.Transpose},
) where {T}
return LinearAlgebra.Transpose{T,A}
end
function _mirror_transpose_or_adjoint(
A::Type{<:AbstractArray{T}},
::Type{<:LinearAlgebra.Adjoint},
) where {T}
return LinearAlgebra.Adjoint{T,A}
end
function similar_array_type(
TA::Type{<:_TransposeOrAdjoint{T,A}},
::Type{S},
) where {S,T,A}
return _mirror_transpose_or_adjoint(similar_array_type(A, S), TA)
end
# dot product
function promote_array_mul(
::Type{<:_TransposeOrAdjoint{S,<:AbstractVector}},
::Type{<:AbstractVector{T}},
) where {S,T}
return promote_sum_mul(S, T)
end
function promote_array_mul(
A::Type{<:_TransposeOrAdjoint{S,V}},
M::Type{<:AbstractMatrix{T}},
) where {S,T,V<:AbstractVector}
B = promote_array_mul(_mirror_transpose_or_adjoint(M, A), V)
return _mirror_transpose_or_adjoint(B, A)
end
function operate(
::typeof(*),
x::LinearAlgebra.Adjoint{<:Any,<:AbstractVector},
y::AbstractVector,
)
return operate(LinearAlgebra.dot, parent(x), y)
end
function operate(
::typeof(*),
x::_TransposeOrAdjoint{<:Any,<:AbstractVector},
y::AbstractMatrix,
)
return _mirror_transpose_or_adjoint(
operate(*, _mirror_transpose_or_adjoint(y, x), parent(x)),
x,
)
end
function operate(
::typeof(*),
x::_TransposeOrAdjoint{<:Any,<:AbstractVector},
y::AbstractVector,
)
return fused_map_reduce(add_mul, x, y)
end
function operate(
::typeof(LinearAlgebra.dot),
x::AbstractArray,
y::AbstractArray,
)
return fused_map_reduce(add_dot, x, y)
end