Skip to content

Commit 843f1e6

Browse files
Merge pull request #155 from SciML/type_info
Remove unnecessary type information in identity and null operators
2 parents 31f195b + 01c226d commit 843f1e6

File tree

8 files changed

+87
-84
lines changed

8 files changed

+87
-84
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SciMLOperators"
22
uuid = "c0aeaf25-5076-4817-a8d5-81caf7dfa961"
33
authors = ["Vedant Puri <vedantpuri@gmail.com>"]
4-
version = "0.1.22"
4+
version = "0.2.0"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/basic.jl

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
"""
22
$(TYPEDEF)
33
"""
4-
struct IdentityOperator{N} <: AbstractSciMLOperator{Bool} end
4+
struct IdentityOperator <: AbstractSciMLOperator{Bool}
5+
len::Int
6+
end
57

68
# constructors
7-
IdentityOperator(u::AbstractArray) = IdentityOperator{size(u,1)}()
9+
IdentityOperator(u::AbstractArray) = IdentityOperator(size(u,1))
810

911
function Base.one(L::AbstractSciMLOperator)
1012
@assert issquare(L)
1113
N = size(L, 1)
12-
IdentityOperator{N}()
14+
IdentityOperator(N)
1315
end
1416

15-
Base.convert(::Type{AbstractMatrix}, ::IdentityOperator{N}) where{N} = Diagonal(ones(Bool, N))
17+
Base.convert(::Type{AbstractMatrix}, ii::IdentityOperator) = Diagonal(ones(Bool, ii.len))
1618

1719
# traits
18-
Base.size(::IdentityOperator{N}) where{N} = (N, N)
20+
Base.size(ii::IdentityOperator) = (ii.len, ii.len)
1921
Base.adjoint(A::IdentityOperator) = A
2022
Base.transpose(A::IdentityOperator) = A
2123
Base.conj(A::IdentityOperator) = A
22-
LinearAlgebra.opnorm(::IdentityOperator{N}, p::Real=2) where{N} = true
24+
LinearAlgebra.opnorm(::IdentityOperator, p::Real=2) = true
2325
for pred in (
2426
:issymmetric, :ishermitian, :isposdef,
2527
)
@@ -38,79 +40,81 @@ has_ldiv!(::IdentityOperator) = true
3840
for op in (
3941
:*, :\,
4042
)
41-
@eval function Base.$op(::IdentityOperator{N}, u::AbstractVecOrMat) where{N}
42-
@assert size(u, 1) == N
43+
@eval function Base.$op(ii::IdentityOperator, u::AbstractVecOrMat)
44+
@assert size(u, 1) == ii.len
4345
copy(u)
4446
end
4547
end
4648

47-
function LinearAlgebra.mul!(v::AbstractVecOrMat, ::IdentityOperator{N}, u::AbstractVecOrMat) where{N}
48-
@assert size(u, 1) == N
49+
function LinearAlgebra.mul!(v::AbstractVecOrMat, ii::IdentityOperator, u::AbstractVecOrMat)
50+
@assert size(u, 1) == ii.len
4951
copy!(v, u)
5052
end
5153

52-
function LinearAlgebra.mul!(v::AbstractVecOrMat, ::IdentityOperator{N}, u::AbstractVecOrMat, α, β) where{N}
53-
@assert size(u, 1) == N
54+
function LinearAlgebra.mul!(v::AbstractVecOrMat, ii::IdentityOperator, u::AbstractVecOrMat, α, β)
55+
@assert size(u, 1) == ii.len
5456
mul!(v, I, u, α, β)
5557
end
5658

57-
function LinearAlgebra.ldiv!(v::AbstractVecOrMat, ::IdentityOperator{N}, u::AbstractVecOrMat) where{N}
58-
@assert size(u, 1) == N
59+
function LinearAlgebra.ldiv!(v::AbstractVecOrMat, ii::IdentityOperator, u::AbstractVecOrMat)
60+
@assert size(u, 1) == ii.len
5961
copy!(v, u)
6062
end
6163

62-
function LinearAlgebra.ldiv!(::IdentityOperator{N}, u::AbstractVecOrMat) where{N}
63-
@assert size(u, 1) == N
64+
function LinearAlgebra.ldiv!(ii::IdentityOperator, u::AbstractVecOrMat)
65+
@assert size(u, 1) == ii.len
6466
u
6567
end
6668

6769
# operator fusion with identity returns operator itself
6870
for op in (
6971
:*, :,
7072
)
71-
@eval function Base.$op(::IdentityOperator{N}, A::AbstractSciMLOperator) where{N}
72-
@assert size(A, 1) == N
73+
@eval function Base.$op(ii::IdentityOperator, A::AbstractSciMLOperator)
74+
@assert size(A, 1) == ii.len
7375
A
7476
end
7577

76-
@eval function Base.$op(A::AbstractSciMLOperator, ::IdentityOperator{N}) where{N}
77-
@assert size(A, 2) == N
78+
@eval function Base.$op(A::AbstractSciMLOperator, ii::IdentityOperator)
79+
@assert size(A, 2) == ii.len
7880
A
7981
end
8082
end
8183

82-
function Base.:\(::IdentityOperator{N}, A::AbstractSciMLOperator) where{N}
83-
@assert size(A, 1) == N
84+
function Base.:\(::IdentityOperator, A::AbstractSciMLOperator)
85+
@assert size(A, 1) == ii.len
8486
A
8587
end
8688

87-
function Base.:/(A::AbstractSciMLOperator, ::IdentityOperator{N}) where{N}
88-
@assert size(A, 2) == N
89+
function Base.:/(A::AbstractSciMLOperator, ::IdentityOperator)
90+
@assert size(A, 2) == ii.len
8991
A
9092
end
9193

9294
"""
9395
$(TYPEDEF)
9496
"""
95-
struct NullOperator{N} <: AbstractSciMLOperator{Bool} end
97+
struct NullOperator <: AbstractSciMLOperator{Bool}
98+
len::Int
99+
end
96100

97101
# constructors
98-
NullOperator(u::AbstractArray) = NullOperator{size(u,1)}()
102+
NullOperator(u::AbstractArray) = NullOperator(size(u,1))
99103

100104
function Base.zero(L::AbstractSciMLOperator)
101105
@assert issquare(L)
102106
N = size(L, 1)
103-
NullOperator{N}()
107+
NullOperator(N)
104108
end
105109

106-
Base.convert(::Type{AbstractMatrix}, ::NullOperator{N}) where{N} = Diagonal(zeros(Bool, N))
110+
Base.convert(::Type{AbstractMatrix}, nn::NullOperator) = Diagonal(zeros(Bool, nn.len))
107111

108112
# traits
109-
Base.size(::NullOperator{N}) where{N} = (N, N)
113+
Base.size(nn::NullOperator) = (nn.len, nn.len)
110114
Base.adjoint(A::NullOperator) = A
111115
Base.transpose(A::NullOperator) = A
112116
Base.conj(A::NullOperator) = A
113-
LinearAlgebra.opnorm(::NullOperator{N}, p::Real=2) where{N} = false
117+
LinearAlgebra.opnorm(::NullOperator, p::Real=2) = false
114118
for pred in (
115119
:issymmetric, :ishermitian,
116120
)
@@ -126,44 +130,44 @@ has_adjoint(::NullOperator) = true
126130
has_mul!(::NullOperator) = true
127131

128132
# opeator application
129-
Base.:*(::NullOperator{N}, u::AbstractVecOrMat) where{N} = (@assert size(u, 1) == N; zero(u))
133+
Base.:*(nn::NullOperator, u::AbstractVecOrMat) = (@assert size(u, 1) == nn.len; zero(u))
130134

131-
function LinearAlgebra.mul!(v::AbstractVecOrMat, ::NullOperator{N}, u::AbstractVecOrMat) where{N}
132-
@assert size(u, 1) == size(v, 1) == N
135+
function LinearAlgebra.mul!(v::AbstractVecOrMat, nn::NullOperator, u::AbstractVecOrMat)
136+
@assert size(u, 1) == size(v, 1) == nn.len
133137
lmul!(false, v)
134138
end
135139

136-
function LinearAlgebra.mul!(v::AbstractVecOrMat, ::NullOperator{N}, u::AbstractVecOrMat, α, β) where{N}
137-
@assert size(u, 1) == size(v, 1) == N
140+
function LinearAlgebra.mul!(v::AbstractVecOrMat, nn::NullOperator, u::AbstractVecOrMat, α, β)
141+
@assert size(u, 1) == size(v, 1) == nn.len
138142
lmul!(β, v)
139143
end
140144

141145
# operator fusion, composition
142146
for op in (
143147
:*, :,
144148
)
145-
@eval function Base.$op(::NullOperator{N}, A::AbstractSciMLOperator) where{N}
146-
@assert size(A, 1) == N
147-
NullOperator{N}()
149+
@eval function Base.$op(nn::NullOperator, A::AbstractSciMLOperator)
150+
@assert size(A, 1) == nn.len
151+
NullOperator(nn.len)
148152
end
149153

150-
@eval function Base.$op(A::AbstractSciMLOperator, ::NullOperator{N}) where{N}
151-
@assert size(A, 2) == N
152-
NullOperator{N}()
154+
@eval function Base.$op(A::AbstractSciMLOperator, nn::NullOperator)
155+
@assert size(A, 2) == nn.len
156+
NullOperator(nn.len)
153157
end
154158
end
155159

156160
# operator addition, subtraction with NullOperator returns operator itself
157161
for op in (
158162
:+, :-,
159163
)
160-
@eval function Base.$op(::NullOperator{N}, A::AbstractSciMLOperator) where{N}
161-
@assert size(A) == (N, N)
164+
@eval function Base.$op(nn::NullOperator, A::AbstractSciMLOperator)
165+
@assert size(A) == (nn.len, nn.len)
162166
A
163167
end
164168

165-
@eval function Base.$op(A::AbstractSciMLOperator, ::NullOperator{N}) where{N}
166-
@assert size(A) == (N, N)
169+
@eval function Base.$op(A::AbstractSciMLOperator, nn::NullOperator)
170+
@assert size(A) == (nn.len, nn.len)
167171
A
168172
end
169173
end
@@ -198,7 +202,7 @@ for T in SCALINGNUMBERTYPES
198202
λ = ScalarOperator(λ) * L.λ
199203
ScaledOperator(λ, L.L)
200204
end
201-
205+
202206
for LT in SCALINGCOMBINETYPES
203207
@eval Base.:*::$T, L::$LT) = ScaledOperator(λ, L)
204208
@eval Base.:*(L::$LT, λ::$T) = ScaledOperator(λ, L)
@@ -347,14 +351,14 @@ for op in (
347351
@eval function Base.$op(L::$LT, λ::$T)
348352
@assert issquare(L)
349353
N = size(L, 1)
350-
Id = IdentityOperator{N}()
354+
Id = IdentityOperator(N)
351355
AddedOperator(L, $op(λ)*Id)
352356
end
353357

354358
@eval function Base.$op::$T, L::$LT)
355359
@assert issquare(L)
356360
N = size(L, 1)
357-
Id = IdentityOperator{N}()
361+
Id = IdentityOperator(N)
358362
AddedOperator*Id, $op(L))
359363
end
360364
end
@@ -459,24 +463,24 @@ for op in (
459463
:*, :,
460464
)
461465
# identity
462-
@eval function Base.$op(::IdentityOperator{N}, A::ComposedOperator) where{N}
463-
@assert size(A, 1) == N
466+
@eval function Base.$op(ii::IdentityOperator, A::ComposedOperator)
467+
@assert size(A, 1) == ii.len
464468
A
465469
end
466470

467-
@eval function Base.$op(A::ComposedOperator, ::IdentityOperator{N}) where{N}
468-
@assert size(A, 2) == N
471+
@eval function Base.$op(A::ComposedOperator, ii::IdentityOperator)
472+
@assert size(A, 2) == ii.len
469473
A
470474
end
471475

472476
# null operator
473-
@eval function Base.$op(::NullOperator{N}, A::ComposedOperator) where{N}
474-
@assert size(A, 1) == N
477+
@eval function Base.$op(nn::NullOperator, A::ComposedOperator)
478+
@assert size(A, 1) == nn.len
475479
zero(A)
476480
end
477481

478-
@eval function Base.$op(A::ComposedOperator, ::NullOperator{N}) where{N}
479-
@assert size(A, 2) == N
482+
@eval function Base.$op(A::ComposedOperator, nn::NullOperator)
483+
@assert size(A, 2) == nn.len
480484
zero(A)
481485
end
482486

@@ -561,7 +565,7 @@ function cache_self(L::ComposedOperator, u::AbstractVecOrMat)
561565
cache = (vec, cache...)
562566
end
563567
elseif has_ldiv(L)
564-
m = size(L, 1)
568+
m = size(L, 1)
565569
k = size(u, 2)
566570
vec = u isa AbstractMatrix ? similar(u, (m, k)) : similar(u, (m,))
567571
cache = ()

src/matrix.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ end
254254
"""
255255
function AddVector(b::AbstractVecOrMat; update_func = DEFAULT_UPDATE_FUNC)
256256
N = size(b, 1)
257-
Id = IdentityOperator{N}()
257+
Id = IdentityOperator(N)
258258

259259
AffineOperator(Id, Id, b; update_func=update_func)
260260
end
@@ -265,7 +265,7 @@ end
265265
"""
266266
function AddVector(B, b::AbstractVecOrMat; update_func = DEFAULT_UPDATE_FUNC)
267267
N = size(B, 1)
268-
Id = IdentityOperator{N}()
268+
Id = IdentityOperator(N)
269269

270270
AffineOperator(Id, B, b; update_func=update_func)
271271
end

src/scalar.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SCALINGNUMBERTYPES = (
99
:UniformScaling,
1010
)
1111

12-
#=
12+
#=
1313
The identity operator must be listed here
1414
so that rules for combination with scalar
1515
operators take precedence over rules for
@@ -18,7 +18,7 @@ the two are combined together.
1818
=#
1919
SCALINGCOMBINETYPES = (
2020
:AbstractSciMLOperator,
21-
:(IdentityOperator{N} where {N})
21+
:IdentityOperator
2222
)
2323

2424
Base.size::AbstractSciMLScalarOperator) = ()
@@ -229,7 +229,7 @@ has_ldiv!(α::ComposedScalarOperator) = all(has_ldiv!, α.ops)
229229
Lazy inversion of Scalar Operators
230230
"""
231231
#=
232-
Keeping with the style, we avoid use of the generic InvertedOperator and instead
232+
Keeping with the style, we avoid use of the generic InvertedOperator and instead
233233
have a specialized type for this purpose that subtypes AbstractSciMLScalarOperator.
234234
=#
235235
struct InvertedScalarOperator{T,λType} <: AbstractSciMLScalarOperator{T}
@@ -246,10 +246,10 @@ for op in (
246246
)
247247
for T in SCALINGNUMBERTYPES[2:end]
248248
@eval Base.$op::AbstractSciMLScalarOperator, x::$T) = α * inv(ScalarOperator(x))
249-
@eval Base.$op(x::$T, α::AbstractSciMLScalarOperator) = ScalarOperator(x) * inv(α)
249+
@eval Base.$op(x::$T, α::AbstractSciMLScalarOperator) = ScalarOperator(x) * inv(α)
250250
end
251251

252-
@eval Base.$op::AbstractSciMLScalarOperator, β::AbstractSciMLScalarOperator) = α * inv(β)
252+
@eval Base.$op::AbstractSciMLScalarOperator, β::AbstractSciMLScalarOperator) = α * inv(β)
253253
end
254254

255255
for op in (

src/tensor.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ end
4343
TensorProductOperator(ops...) = reduce(TensorProductOperator, ops)
4444
TensorProductOperator(op::AbstractSciMLOperator) = op
4545
TensorProductOperator(op::AbstractMatrix) = MatrixOperator(op)
46-
TensorProductOperator(::IdentityOperator{No}, ::IdentityOperator{Ni}) where{No,Ni} = IdentityOperator{No*Ni}()
46+
TensorProductOperator(ii1::IdentityOperator, ii2::IdentityOperator) = IdentityOperator(ii1.len * ii2.len)
4747

4848
# overload ⊗ (\otimes)
4949
(ops::Union{AbstractMatrix,AbstractSciMLOperator}...) = TensorProductOperator(ops...)

test/basic.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ K = 12
2525
u = rand(N,K)
2626
α = rand()
2727
β = rand()
28-
Id = IdentityOperator{N}()
28+
Id = IdentityOperator(N)
2929

3030
@test issquare(Id)
3131
@test islinear(Id)
32-
@test IdentityOperator(u) isa IdentityOperator{N}
33-
@test one(A) isa IdentityOperator{N}
32+
@test IdentityOperator(u) isa IdentityOperator
33+
@test one(A) isa IdentityOperator
3434
@test convert(AbstractMatrix, Id) == Matrix(I, N, N)
3535

3636
@test iscached(Id)
3737
@test size(Id) == (N, N)
38-
@test Id' isa IdentityOperator{N}
38+
@test Id' isa IdentityOperator
3939
@test isconstant(Id)
4040

4141
for op in (
@@ -63,18 +63,18 @@ end
6363
u = rand(N,K)
6464
α = rand()
6565
β = rand()
66-
Z = NullOperator{N}()
66+
Z = NullOperator(N)
6767

6868
@test issquare(Z)
6969
@test islinear(Z)
70-
@test NullOperator(u) isa NullOperator{N}
70+
@test NullOperator(u) isa NullOperator
7171
@test isconstant(Z)
72-
@test zero(A) isa NullOperator{N}
72+
@test zero(A) isa NullOperator
7373
@test convert(AbstractMatrix, Z) == zeros(size(Z))
7474

7575
@test iscached(Z)
7676
@test size(Z) == (N, N)
77-
@test Z' isa NullOperator{N}
77+
@test Z' isa NullOperator
7878

7979
@test Z * u zero(u)
8080

@@ -229,7 +229,7 @@ end
229229
# We can now test that caching does not rely on matmul
230230
op = inner_op * factorize(MatrixOperator(rand(N, N)))
231231
@test !iscached(op)
232-
@test_nowarn op = cache_operator(op, rand(N))
232+
@test_nowarn op = cache_operator(op, rand(N))
233233
@test iscached(op)
234234
u = rand(N)
235235
@test ldiv!(rand(N), op, u) op \ u

0 commit comments

Comments
 (0)