@@ -4,7 +4,7 @@ function Base.Matrix(A::LinearMap)
4
4
T = eltype (A)
5
5
mat = Matrix {T} (undef, (M, N))
6
6
v = fill (zero (T), N)
7
- @inbounds for i = 1 : N
7
+ @inbounds for i in 1 : N
8
8
v[i] = one (T)
9
9
mul! (view (mat, :, i), A, v)
10
10
v[i] = zero (T)
@@ -17,35 +17,6 @@ Base.convert(::Type{Array}, A::LinearMap) = convert(Matrix, A)
17
17
Base. convert (:: Type{AbstractMatrix} , A:: LinearMap ) = convert (Matrix, A)
18
18
Base. convert (:: Type{AbstractArray} , A:: LinearMap ) = convert (AbstractMatrix, A)
19
19
20
- # special cases
21
- Base. convert (:: Type{AbstractMatrix} , A:: WrappedMap ) = convert (AbstractMatrix, A. lmap)
22
- Base. convert (:: Type{Matrix} , A:: WrappedMap ) = convert (Matrix, A. lmap)
23
- function Base. convert (:: Type{Matrix} , ΣA:: LinearCombination{<:Any,<:Tuple{Vararg{MatrixMap}}} )
24
- if length (ΣA. maps) <= 10
25
- return (+ ). (map (A-> getfield (A, :lmap ), ΣA. maps)... )
26
- else
27
- S = zero (first (ΣA. maps). lmap)
28
- for A in ΣA. maps
29
- S .+ = A. lmap
30
- end
31
- return S
32
- end
33
- end
34
- function Base. convert (:: Type{Matrix} , AB:: CompositeMap{<:Any,<:Tuple{MatrixMap,MatrixMap}} )
35
- B, A = AB. maps
36
- return A. lmap* B. lmap
37
- end
38
- function Base. convert (:: Type{Matrix} , λA:: CompositeMap{<:Any,<:Tuple{MatrixMap,UniformScalingMap}} )
39
- A, J = λA. maps
40
- return J. λ* A. lmap
41
- end
42
- function Base. convert (:: Type{Matrix} , Aλ:: CompositeMap{<:Any,<:Tuple{UniformScalingMap,MatrixMap}} )
43
- J, A = Aλ. maps
44
- return A. lmap* J. λ
45
- end
46
-
47
- Base. Matrix (A:: BlockMap ) = hvcat (A. rows, convert .(AbstractMatrix, A. maps)... )
48
-
49
20
# sparse: create sparse matrix representation of LinearMap
50
21
function SparseArrays. sparse (A:: LinearMap{T} ) where {T}
51
22
M, N = size (A)
@@ -55,7 +26,7 @@ function SparseArrays.sparse(A::LinearMap{T}) where {T}
55
26
v = fill (zero (T), N)
56
27
Av = Vector {T} (undef, M)
57
28
58
- for i = 1 : N
29
+ @inbounds for i in 1 : N
59
30
v[i] = one (T)
60
31
mul! (Av, A, v)
61
32
js = findall (! iszero, Av)
@@ -70,7 +41,92 @@ function SparseArrays.sparse(A::LinearMap{T}) where {T}
70
41
71
42
return SparseMatrixCSC (M, N, colptr, rowind, nzval)
72
43
end
73
-
74
44
Base. convert (:: Type{SparseMatrixCSC} , A:: LinearMap ) = sparse (A)
45
+
46
+ # special cases
47
+
48
+ # UniformScalingMap
49
+ Base. Matrix (J:: UniformScalingMap ) = Matrix (J. λ* I, size (J))
50
+ Base. convert (:: Type{AbstractMatrix} , J:: UniformScalingMap ) = Diagonal (fill (J. λ, size (J, 1 )))
51
+
52
+ # WrappedMap
53
+ Base. Matrix (A:: WrappedMap ) = Matrix (A. lmap)
54
+ Base. convert (:: Type{AbstractMatrix} , A:: WrappedMap ) = convert (AbstractMatrix, A. lmap)
55
+ Base. convert (:: Type{Matrix} , A:: WrappedMap ) = convert (Matrix, A. lmap)
56
+ SparseArrays. sparse (A:: WrappedMap ) = sparse (A. lmap)
75
57
Base. convert (:: Type{SparseMatrixCSC} , A:: WrappedMap ) = convert (SparseMatrixCSC, A. lmap)
76
- Base. convert (:: Type{SparseMatrixCSC} , A:: BlockMap ) = hvcat (A. rows, convert .(SparseMatrixCSC, A. maps)... )
58
+
59
+ # TransposeMap & AdjointMap
60
+ for (TT, T) in ((AdjointMap, adjoint), (TransposeMap, transpose))
61
+ @eval Base. convert (:: Type{AbstractMatrix} , A:: $TT ) = $ T (convert (AbstractMatrix, A. lmap))
62
+ @eval SparseArrays. sparse (A:: $TT ) = $ T (convert (SparseMatrixCSC, A. lmap))
63
+ end
64
+
65
+ # LinearCombination
66
+ for (TT, T) in ((Type{Matrix}, Matrix), (Type{SparseMatrixCSC}, SparseMatrixCSC))
67
+ @eval function Base. convert (:: $TT , ΣA:: LinearCombination{<:Any,<:Tuple{Vararg{MatrixMap}}} )
68
+ maps = ΣA. maps
69
+ mats = map (A-> getfield (A, :lmap ), maps)
70
+ return convert ($ T, sum (mats))
71
+ end
72
+ end
73
+
74
+ # CompositeMap
75
+ for (TT, T) in ((Type{Matrix}, Matrix), (Type{SparseMatrixCSC}, SparseMatrixCSC))
76
+ @eval function Base. convert (:: $TT , AB:: CompositeMap{<:Any,<:Tuple{MatrixMap,MatrixMap}} )
77
+ B, A = AB. maps
78
+ return convert ($ T, A. lmap* B. lmap)
79
+ end
80
+ @eval function Base. convert (:: $TT , λA:: CompositeMap{<:Any,<:Tuple{MatrixMap,UniformScalingMap}} )
81
+ A, J = λA. maps
82
+ return convert ($ T, J. λ* A. lmap)
83
+ end
84
+ @eval function Base. convert (:: $TT , Aλ:: CompositeMap{<:Any,<:Tuple{UniformScalingMap,MatrixMap}} )
85
+ J, A = Aλ. maps
86
+ return convert ($ T, A. lmap* J. λ)
87
+ end
88
+ end
89
+
90
+ # BlockMap & BlockDiagonalMap
91
+ Base. Matrix (A:: BlockMap ) = hvcat (A. rows, convert .(Matrix, A. maps)... )
92
+ Base. convert (:: Type{AbstractMatrix} , A:: BlockMap ) = hvcat (A. rows, convert .(AbstractMatrix, A. maps)... )
93
+ function Base. convert (:: Type{SparseMatrixCSC} , A:: BlockMap )
94
+ return hvcat (
95
+ A. rows,
96
+ convert (SparseMatrixCSC, first (A. maps)),
97
+ convert .(AbstractMatrix, Base. tail (A. maps))...
98
+ )
99
+ end
100
+ Base. Matrix (A:: BlockDiagonalMap ) = cat (convert .(Matrix, A. maps)... ; dims= (1 ,2 ))
101
+ Base. convert (:: Type{AbstractMatrix} , A:: BlockDiagonalMap ) = sparse (A)
102
+ function SparseArrays. sparse (A:: BlockDiagonalMap )
103
+ return blockdiag (convert .(SparseMatrixCSC, A. maps)... )
104
+ end
105
+
106
+ # KroneckerMap & KroneckerSumMap
107
+ Base. Matrix (A:: KroneckerMap ) = kron (convert .(Matrix, A. maps)... )
108
+ Base. convert (:: Type{AbstractMatrix} , A:: KroneckerMap ) = kron (convert .(AbstractMatrix, A. maps)... )
109
+ function SparseArrays. sparse (A:: KroneckerMap )
110
+ return kron (
111
+ convert (SparseMatrixCSC, first (A. maps)),
112
+ convert .(AbstractMatrix, Base. tail (A. maps))...
113
+ )
114
+ end
115
+ function Base. Matrix (L:: KroneckerSumMap )
116
+ A, B = L. maps
117
+ IA = Diagonal (ones (Bool, size (A, 1 )))
118
+ IB = Diagonal (ones (Bool, size (B, 1 )))
119
+ return kron (Matrix (A), IB) + kron (IA, Matrix (B))
120
+ end
121
+ function Base. convert (:: Type{AbstractMatrix} , L:: KroneckerSumMap )
122
+ A, B = L. maps
123
+ IA = Diagonal (ones (Bool, size (A, 1 )))
124
+ IB = Diagonal (ones (Bool, size (B, 1 )))
125
+ return kron (convert (AbstractMatrix, A), IB) + kron (IA, convert (AbstractMatrix, B))
126
+ end
127
+ function SparseArrays. sparse (L:: KroneckerSumMap )
128
+ A, B = L. maps
129
+ IA = sparse (Diagonal (ones (Bool, size (A, 1 ))))
130
+ IB = sparse (Diagonal (ones (Bool, size (B, 1 ))))
131
+ return kron (convert (AbstractMatrix, A), IB) + kron (IA, convert (AbstractMatrix, B))
132
+ end
0 commit comments