@@ -16,11 +16,6 @@ BlockMap{T}(maps::As, rows::S) where {T,As<:Tuple{Vararg{LinearMap}},S} = BlockM
16
16
17
17
MulStyle (A:: BlockMap ) = MulStyle (A. maps... )
18
18
19
- function check_dim (A:: LinearMap , dim, n)
20
- n == size (A, dim) || throw (DimensionMismatch (" Expected $n , got $(size (A, dim)) " ))
21
- return nothing
22
- end
23
-
24
19
"""
25
20
rowcolranges(maps, rows)
26
21
51
46
52
47
Base. size (A:: BlockMap ) = (last (last (A. rowranges)), last (last (A. colranges)))
53
48
54
- # ###########
55
- # concatenation
56
- # ###########
57
-
58
- for k in 1 : 8 # is 8 sufficient?
59
- Is = ntuple (n-> :($ (Symbol (:A ,n)):: UniformScaling ), Val (k- 1 ))
60
- L = :($ (Symbol (:A ,k)):: LinearMap )
61
- args = ntuple (n-> Symbol (:A ,n), Val (k))
62
-
63
- @eval begin
64
- Base. hcat ($ (Is... ), $ L, As:: Union{LinearMap,UniformScaling} ...) = _hcat ($ (args... ), As... )
65
- Base. vcat ($ (Is... ), $ L, As:: Union{LinearMap,UniformScaling} ...) = _vcat ($ (args... ), As... )
66
- Base. hvcat (rows:: Tuple{Vararg{Int}} , $ (Is... ), $ L, As:: Union{LinearMap,UniformScaling} ...) = _hvcat (rows, $ (args... ), As... )
67
- end
68
- end
69
-
70
49
# ###########
71
50
# hcat
72
51
# ###########
73
52
"""
74
- hcat(As::Union{LinearMap,UniformScaling}...)::BlockMap
53
+ hcat(As::Union{LinearMap,UniformScaling,AbstractVecOrMat }...)::BlockMap
75
54
76
55
Construct a (lazy) representation of the horizontal concatenation of the arguments.
77
- `UniformScaling` objects are promoted to `LinearMap` automatically. To avoid fallback
78
- to the generic `Base.hcat`, there must be a `LinearMap` object among the first 8 arguments.
56
+ All arguments are promoted to `LinearMap`s automatically.
79
57
80
58
# Examples
81
59
```jldoctest; setup=(using LinearMaps)
@@ -90,9 +68,7 @@ julia> L * ones(Int, 6)
90
68
6
91
69
```
92
70
"""
93
- Base. hcat
94
-
95
- function _hcat (As:: Union{LinearMap,UniformScaling} ...)
71
+ function Base. hcat (As:: Union{LinearMap,UniformScaling,AbstractVecOrMat} ...)
96
72
T = promote_type (map (eltype, As)... )
97
73
nbc = length (As)
98
74
112
88
# vcat
113
89
# ###########
114
90
"""
115
- vcat(As::Union{LinearMap,UniformScaling}...)::BlockMap
91
+ vcat(As::Union{LinearMap,UniformScaling,AbstractVecOrMat }...)::BlockMap
116
92
117
93
Construct a (lazy) representation of the vertical concatenation of the arguments.
118
- `UniformScaling` objects are promoted to `LinearMap` automatically. To avoid fallback
119
- to the generic `Base.vcat`, there must be a `LinearMap` object among the first 8 arguments.
94
+ All arguments are promoted to `LinearMap`s automatically.
120
95
121
96
# Examples
122
97
```jldoctest; setup=(using LinearMaps)
@@ -134,9 +109,7 @@ julia> L * ones(Int, 3)
134
109
3
135
110
```
136
111
"""
137
- Base. vcat
138
-
139
- function _vcat (As:: Union{LinearMap,UniformScaling} ...)
112
+ function Base. vcat (As:: Union{LinearMap,UniformScaling,AbstractVecOrMat} ...)
140
113
T = promote_type (map (eltype, As)... )
141
114
nbr = length (As)
142
115
@@ -157,12 +130,11 @@ end
157
130
# hvcat
158
131
# ###########
159
132
"""
160
- hvcat(rows::Tuple{Vararg{Int}}, As::Union{LinearMap,UniformScaling}...)::BlockMap
133
+ hvcat(rows::Tuple{Vararg{Int}}, As::Union{LinearMap,UniformScaling,AbstractVecOrMat }...)::BlockMap
161
134
162
135
Construct a (lazy) representation of the horizontal-vertical concatenation of the arguments.
163
136
The first argument specifies the number of arguments to concatenate in each block row.
164
- `UniformScaling` objects are promoted to `LinearMap` automatically. To avoid fallback
165
- to the generic `Base.hvcat`, there must be a `LinearMap` object among the first 8 arguments.
137
+ All arguments are promoted to `LinearMap`s automatically.
166
138
167
139
# Examples
168
140
```jldoctest; setup=(using LinearMaps)
@@ -185,7 +157,7 @@ julia> L * ones(Int, 6)
185
157
"""
186
158
Base. hvcat
187
159
188
- function _hvcat (rows:: Tuple{Vararg{Int}} , As:: Union{LinearMap,UniformScaling} ...)
160
+ function Base . hvcat (rows:: Tuple{Vararg{Int}} , As:: Union{LinearMap,UniformScaling,AbstractVecOrMat } ...)
189
161
nr = length (rows)
190
162
T = promote_type (map (eltype, As)... )
191
163
sum (rows) == length (As) || throw (ArgumentError (" mismatch between row sizes and number of arguments" ))
@@ -237,6 +209,13 @@ function _hvcat(rows::Tuple{Vararg{Int}}, As::Union{LinearMap,UniformScaling}...
237
209
return BlockMap {T} (promote_to_lmaps (n, 1 , 1 , As... ), rows)
238
210
end
239
211
212
+ function check_dim (A, dim, n)
213
+ n == size (A, dim) || throw (DimensionMismatch (" Expected $n , got $(size (A, dim)) " ))
214
+ return nothing
215
+ end
216
+
217
+ promote_to_lmaps_ (n:: Int , dim, A:: AbstractMatrix ) = (check_dim (A, dim, n); LinearMap (A))
218
+ promote_to_lmaps_ (n:: Int , dim, A:: AbstractVector ) = (check_dim (A, dim, n); LinearMap (reshape (A, length (A), 1 )))
240
219
promote_to_lmaps_ (n:: Int , dim, J:: UniformScaling ) = UniformScalingMap (J. λ, n)
241
220
promote_to_lmaps_ (n:: Int , dim, A:: LinearMap ) = (check_dim (A, dim, n); A)
242
221
promote_to_lmaps (n, k, dim) = ()
292
271
293
272
Base.:(== )(A:: BlockMap , B:: BlockMap ) = (eltype (A) == eltype (B) && A. maps == B. maps && A. rows == B. rows)
294
273
295
- # special transposition behavior
296
-
297
- LinearAlgebra. transpose (A:: BlockMap ) = TransposeMap (A)
298
- LinearAlgebra. adjoint (A:: BlockMap ) = AdjointMap (A)
299
-
300
274
# ###########
301
275
# multiplication helper functions
302
276
# ###########
@@ -310,6 +284,7 @@ function _blockmul!(y, A::BlockMap, x, α, β)
310
284
return __blockmul! (MulStyle (A), y, A, x, α, β)
311
285
end
312
286
287
+ # provide one global intermediate storage vector if necessary
313
288
__blockmul! (:: FiveArg , y, A, x, α, β) = ___blockmul! (y, A, x, α, β, nothing )
314
289
__blockmul! (:: ThreeArg , y, A, x, α, β) = ___blockmul! (y, A, x, α, β, similar (y))
315
290
@@ -401,7 +376,6 @@ for (intype, outtype) in ((AbstractVector, AbstractVecOrMat), (AbstractMatrix, A
401
376
function _unsafe_mul! (y:: $outtype , wrapA:: $maptype , x:: $intype ,
402
377
α:: Number , β:: Number )
403
378
require_one_based_indexing (y, x)
404
-
405
379
return _transblockmul! (y, wrapA. lmap, x, α, β, $ transform)
406
380
end
407
381
end
@@ -437,22 +411,24 @@ BlockDiagonalMap{T}(maps::As) where {T,As<:Tuple{Vararg{LinearMap}}} =
437
411
BlockDiagonalMap (maps:: LinearMap... ) =
438
412
BlockDiagonalMap {promote_type(map(eltype, maps)...)} (maps)
439
413
414
+ # since the below methods are more specific than the Base method,
415
+ # they would redefine Base/SparseArrays behavior
440
416
for k in 1 : 8 # is 8 sufficient?
441
- Is = ntuple (n-> :($ (Symbol (:A ,n)):: AbstractMatrix ), Val (k- 1 ))
417
+ Is = ntuple (n-> :($ (Symbol (:A ,n)):: AbstractVecOrMat ), Val (k- 1 ))
442
418
# yields (:A1, :A2, :A3, ..., :A(k-1))
443
419
L = :($ (Symbol (:A ,k)):: LinearMap )
444
420
# yields :Ak
445
- mapargs = ntuple (n -> :(LinearMap ( $ (Symbol (:A ,n) ))), Val (k- 1 ))
421
+ mapargs = ntuple (n -> :($ (Symbol (:A ,n))), Val (k- 1 ))
446
422
# yields (:LinearMap(A1), :LinearMap(A2), ..., :LinearMap(A(k-1)))
447
423
448
424
@eval begin
449
- function SparseArrays. blockdiag ($ (Is... ), $ L, As:: Union{LinearMap,AbstractMatrix } ...)
450
- return BlockDiagonalMap ($ (mapargs... ), $ (Symbol (:A ,k)), convert_to_lmaps (As... )... )
425
+ function SparseArrays. blockdiag ($ (Is... ), $ L, As:: Union{LinearMap,AbstractVecOrMat } ...)
426
+ return BlockDiagonalMap (convert_to_lmaps ( $ (mapargs... )) ... , $ (Symbol (:A ,k)), convert_to_lmaps (As... )... )
451
427
end
452
428
453
- function Base. cat ($ (Is... ), $ L, As:: Union{LinearMap,AbstractMatrix } ...; dims:: Dims{2} )
429
+ function Base. cat ($ (Is... ), $ L, As:: Union{LinearMap,AbstractVecOrMat } ...; dims:: Dims{2} )
454
430
if dims == (1 ,2 )
455
- return BlockDiagonalMap ($ (mapargs... ), $ (Symbol (:A ,k)), convert_to_lmaps (As... )... )
431
+ return BlockDiagonalMap (convert_to_lmaps ( $ (mapargs... )) ... , $ (Symbol (:A ,k)), convert_to_lmaps (As... )... )
456
432
else
457
433
throw (ArgumentError (" dims keyword in cat of LinearMaps must be (1,2)" ))
458
434
end
@@ -461,7 +437,7 @@ for k in 1:8 # is 8 sufficient?
461
437
end
462
438
463
439
"""
464
- blockdiag(As::Union{LinearMap,AbstractMatrix }...)::BlockDiagonalMap
440
+ blockdiag(As::Union{LinearMap,AbstractVecOrMat }...)::BlockDiagonalMap
465
441
466
442
Construct a (lazy) representation of the diagonal concatenation of the arguments.
467
443
To avoid fallback to the generic `SparseArrays.blockdiag`, there must be a `LinearMap`
@@ -470,7 +446,7 @@ object among the first 8 arguments.
470
446
SparseArrays. blockdiag
471
447
472
448
"""
473
- cat(As::Union{LinearMap,AbstractMatrix }...; dims=(1,2))::BlockDiagonalMap
449
+ cat(As::Union{LinearMap,AbstractVecOrMat }...; dims=(1,2))::BlockDiagonalMap
474
450
475
451
Construct a (lazy) representation of the diagonal concatenation of the arguments.
476
452
To avoid fallback to the generic `Base.cat`, there must be a `LinearMap`
0 commit comments