Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary type information in identity and null operators #155

Merged
merged 1 commit into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SciMLOperators"
uuid = "c0aeaf25-5076-4817-a8d5-81caf7dfa961"
authors = ["Vedant Puri <vedantpuri@gmail.com>"]
version = "0.1.22"
version = "0.2.0"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down
118 changes: 61 additions & 57 deletions src/basic.jl
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
"""
$(TYPEDEF)
"""
struct IdentityOperator{N} <: AbstractSciMLOperator{Bool} end
struct IdentityOperator <: AbstractSciMLOperator{Bool}
len::Int
end

# constructors
IdentityOperator(u::AbstractArray) = IdentityOperator{size(u,1)}()
IdentityOperator(u::AbstractArray) = IdentityOperator(size(u,1))

function Base.one(L::AbstractSciMLOperator)
@assert issquare(L)
N = size(L, 1)
IdentityOperator{N}()
IdentityOperator(N)
end

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

# traits
Base.size(::IdentityOperator{N}) where{N} = (N, N)
Base.size(ii::IdentityOperator) = (ii.len, ii.len)
Base.adjoint(A::IdentityOperator) = A
Base.transpose(A::IdentityOperator) = A
Base.conj(A::IdentityOperator) = A
LinearAlgebra.opnorm(::IdentityOperator{N}, p::Real=2) where{N} = true
LinearAlgebra.opnorm(::IdentityOperator, p::Real=2) = true
for pred in (
:issymmetric, :ishermitian, :isposdef,
)
Expand All @@ -38,79 +40,81 @@ has_ldiv!(::IdentityOperator) = true
for op in (
:*, :\,
)
@eval function Base.$op(::IdentityOperator{N}, u::AbstractVecOrMat) where{N}
@assert size(u, 1) == N
@eval function Base.$op(ii::IdentityOperator, u::AbstractVecOrMat)
@assert size(u, 1) == ii.len
copy(u)
end
end

function LinearAlgebra.mul!(v::AbstractVecOrMat, ::IdentityOperator{N}, u::AbstractVecOrMat) where{N}
@assert size(u, 1) == N
function LinearAlgebra.mul!(v::AbstractVecOrMat, ii::IdentityOperator, u::AbstractVecOrMat)
@assert size(u, 1) == ii.len
copy!(v, u)
end

function LinearAlgebra.mul!(v::AbstractVecOrMat, ::IdentityOperator{N}, u::AbstractVecOrMat, α, β) where{N}
@assert size(u, 1) == N
function LinearAlgebra.mul!(v::AbstractVecOrMat, ii::IdentityOperator, u::AbstractVecOrMat, α, β)
@assert size(u, 1) == ii.len
mul!(v, I, u, α, β)
end

function LinearAlgebra.ldiv!(v::AbstractVecOrMat, ::IdentityOperator{N}, u::AbstractVecOrMat) where{N}
@assert size(u, 1) == N
function LinearAlgebra.ldiv!(v::AbstractVecOrMat, ii::IdentityOperator, u::AbstractVecOrMat)
@assert size(u, 1) == ii.len
copy!(v, u)
end

function LinearAlgebra.ldiv!(::IdentityOperator{N}, u::AbstractVecOrMat) where{N}
@assert size(u, 1) == N
function LinearAlgebra.ldiv!(ii::IdentityOperator, u::AbstractVecOrMat)
@assert size(u, 1) == ii.len
u
end

# operator fusion with identity returns operator itself
for op in (
:*, :∘,
)
@eval function Base.$op(::IdentityOperator{N}, A::AbstractSciMLOperator) where{N}
@assert size(A, 1) == N
@eval function Base.$op(ii::IdentityOperator, A::AbstractSciMLOperator)
@assert size(A, 1) == ii.len
A
end

@eval function Base.$op(A::AbstractSciMLOperator, ::IdentityOperator{N}) where{N}
@assert size(A, 2) == N
@eval function Base.$op(A::AbstractSciMLOperator, ii::IdentityOperator)
@assert size(A, 2) == ii.len
A
end
end

function Base.:\(::IdentityOperator{N}, A::AbstractSciMLOperator) where{N}
@assert size(A, 1) == N
function Base.:\(::IdentityOperator, A::AbstractSciMLOperator)
@assert size(A, 1) == ii.len
A
end

function Base.:/(A::AbstractSciMLOperator, ::IdentityOperator{N}) where{N}
@assert size(A, 2) == N
function Base.:/(A::AbstractSciMLOperator, ::IdentityOperator)
@assert size(A, 2) == ii.len
A
end

"""
$(TYPEDEF)
"""
struct NullOperator{N} <: AbstractSciMLOperator{Bool} end
struct NullOperator <: AbstractSciMLOperator{Bool}
len::Int
end

# constructors
NullOperator(u::AbstractArray) = NullOperator{size(u,1)}()
NullOperator(u::AbstractArray) = NullOperator(size(u,1))

function Base.zero(L::AbstractSciMLOperator)
@assert issquare(L)
N = size(L, 1)
NullOperator{N}()
NullOperator(N)
end

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

# traits
Base.size(::NullOperator{N}) where{N} = (N, N)
Base.size(nn::NullOperator) = (nn.len, nn.len)
Base.adjoint(A::NullOperator) = A
Base.transpose(A::NullOperator) = A
Base.conj(A::NullOperator) = A
LinearAlgebra.opnorm(::NullOperator{N}, p::Real=2) where{N} = false
LinearAlgebra.opnorm(::NullOperator, p::Real=2) = false
for pred in (
:issymmetric, :ishermitian,
)
Expand All @@ -126,44 +130,44 @@ has_adjoint(::NullOperator) = true
has_mul!(::NullOperator) = true

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

function LinearAlgebra.mul!(v::AbstractVecOrMat, ::NullOperator{N}, u::AbstractVecOrMat) where{N}
@assert size(u, 1) == size(v, 1) == N
function LinearAlgebra.mul!(v::AbstractVecOrMat, nn::NullOperator, u::AbstractVecOrMat)
@assert size(u, 1) == size(v, 1) == nn.len
lmul!(false, v)
end

function LinearAlgebra.mul!(v::AbstractVecOrMat, ::NullOperator{N}, u::AbstractVecOrMat, α, β) where{N}
@assert size(u, 1) == size(v, 1) == N
function LinearAlgebra.mul!(v::AbstractVecOrMat, nn::NullOperator, u::AbstractVecOrMat, α, β)
@assert size(u, 1) == size(v, 1) == nn.len
lmul!(β, v)
end

# operator fusion, composition
for op in (
:*, :∘,
)
@eval function Base.$op(::NullOperator{N}, A::AbstractSciMLOperator) where{N}
@assert size(A, 1) == N
NullOperator{N}()
@eval function Base.$op(nn::NullOperator, A::AbstractSciMLOperator)
@assert size(A, 1) == nn.len
NullOperator(nn.len)
end

@eval function Base.$op(A::AbstractSciMLOperator, ::NullOperator{N}) where{N}
@assert size(A, 2) == N
NullOperator{N}()
@eval function Base.$op(A::AbstractSciMLOperator, nn::NullOperator)
@assert size(A, 2) == nn.len
NullOperator(nn.len)
end
end

# operator addition, subtraction with NullOperator returns operator itself
for op in (
:+, :-,
)
@eval function Base.$op(::NullOperator{N}, A::AbstractSciMLOperator) where{N}
@assert size(A) == (N, N)
@eval function Base.$op(nn::NullOperator, A::AbstractSciMLOperator)
@assert size(A) == (nn.len, nn.len)
A
end

@eval function Base.$op(A::AbstractSciMLOperator, ::NullOperator{N}) where{N}
@assert size(A) == (N, N)
@eval function Base.$op(A::AbstractSciMLOperator, nn::NullOperator)
@assert size(A) == (nn.len, nn.len)
A
end
end
Expand Down Expand Up @@ -198,7 +202,7 @@ for T in SCALINGNUMBERTYPES
λ = ScalarOperator(λ) * L.λ
ScaledOperator(λ, L.L)
end

for LT in SCALINGCOMBINETYPES
@eval Base.:*(λ::$T, L::$LT) = ScaledOperator(λ, L)
@eval Base.:*(L::$LT, λ::$T) = ScaledOperator(λ, L)
Expand Down Expand Up @@ -347,14 +351,14 @@ for op in (
@eval function Base.$op(L::$LT, λ::$T)
@assert issquare(L)
N = size(L, 1)
Id = IdentityOperator{N}()
Id = IdentityOperator(N)
AddedOperator(L, $op(λ)*Id)
end

@eval function Base.$op(λ::$T, L::$LT)
@assert issquare(L)
N = size(L, 1)
Id = IdentityOperator{N}()
Id = IdentityOperator(N)
AddedOperator(λ*Id, $op(L))
end
end
Expand Down Expand Up @@ -459,24 +463,24 @@ for op in (
:*, :∘,
)
# identity
@eval function Base.$op(::IdentityOperator{N}, A::ComposedOperator) where{N}
@assert size(A, 1) == N
@eval function Base.$op(ii::IdentityOperator, A::ComposedOperator)
@assert size(A, 1) == ii.len
A
end

@eval function Base.$op(A::ComposedOperator, ::IdentityOperator{N}) where{N}
@assert size(A, 2) == N
@eval function Base.$op(A::ComposedOperator, ii::IdentityOperator)
@assert size(A, 2) == ii.len
A
end

# null operator
@eval function Base.$op(::NullOperator{N}, A::ComposedOperator) where{N}
@assert size(A, 1) == N
@eval function Base.$op(nn::NullOperator, A::ComposedOperator)
@assert size(A, 1) == nn.len
zero(A)
end

@eval function Base.$op(A::ComposedOperator, ::NullOperator{N}) where{N}
@assert size(A, 2) == N
@eval function Base.$op(A::ComposedOperator, nn::NullOperator)
@assert size(A, 2) == nn.len
zero(A)
end

Expand Down Expand Up @@ -561,7 +565,7 @@ function cache_self(L::ComposedOperator, u::AbstractVecOrMat)
cache = (vec, cache...)
end
elseif has_ldiv(L)
m = size(L, 1)
m = size(L, 1)
k = size(u, 2)
vec = u isa AbstractMatrix ? similar(u, (m, k)) : similar(u, (m,))
cache = ()
Expand Down
4 changes: 2 additions & 2 deletions src/matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ end
"""
function AddVector(b::AbstractVecOrMat; update_func = DEFAULT_UPDATE_FUNC)
N = size(b, 1)
Id = IdentityOperator{N}()
Id = IdentityOperator(N)

AffineOperator(Id, Id, b; update_func=update_func)
end
Expand All @@ -265,7 +265,7 @@ end
"""
function AddVector(B, b::AbstractVecOrMat; update_func = DEFAULT_UPDATE_FUNC)
N = size(B, 1)
Id = IdentityOperator{N}()
Id = IdentityOperator(N)

AffineOperator(Id, B, b; update_func=update_func)
end
Expand Down
10 changes: 5 additions & 5 deletions src/scalar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ SCALINGNUMBERTYPES = (
:UniformScaling,
)

#=
#=
The identity operator must be listed here
so that rules for combination with scalar
operators take precedence over rules for
Expand All @@ -18,7 +18,7 @@ the two are combined together.
=#
SCALINGCOMBINETYPES = (
:AbstractSciMLOperator,
:(IdentityOperator{N} where {N})
:IdentityOperator
)

Base.size(α::AbstractSciMLScalarOperator) = ()
Expand Down Expand Up @@ -229,7 +229,7 @@ has_ldiv!(α::ComposedScalarOperator) = all(has_ldiv!, α.ops)
Lazy inversion of Scalar Operators
"""
#=
Keeping with the style, we avoid use of the generic InvertedOperator and instead
Keeping with the style, we avoid use of the generic InvertedOperator and instead
have a specialized type for this purpose that subtypes AbstractSciMLScalarOperator.
=#
struct InvertedScalarOperator{T,λType} <: AbstractSciMLScalarOperator{T}
Expand All @@ -246,10 +246,10 @@ for op in (
)
for T in SCALINGNUMBERTYPES[2:end]
@eval Base.$op(α::AbstractSciMLScalarOperator, x::$T) = α * inv(ScalarOperator(x))
@eval Base.$op(x::$T, α::AbstractSciMLScalarOperator) = ScalarOperator(x) * inv(α)
@eval Base.$op(x::$T, α::AbstractSciMLScalarOperator) = ScalarOperator(x) * inv(α)
end

@eval Base.$op(α::AbstractSciMLScalarOperator, β::AbstractSciMLScalarOperator) = α * inv(β)
@eval Base.$op(α::AbstractSciMLScalarOperator, β::AbstractSciMLScalarOperator) = α * inv(β)
end

for op in (
Expand Down
2 changes: 1 addition & 1 deletion src/tensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ end
TensorProductOperator(ops...) = reduce(TensorProductOperator, ops)
TensorProductOperator(op::AbstractSciMLOperator) = op
TensorProductOperator(op::AbstractMatrix) = MatrixOperator(op)
TensorProductOperator(::IdentityOperator{No}, ::IdentityOperator{Ni}) where{No,Ni} = IdentityOperator{No*Ni}()
TensorProductOperator(ii1::IdentityOperator, ii2::IdentityOperator) = IdentityOperator(ii1.len * ii2.len)

# overload ⊗ (\otimes)
⊗(ops::Union{AbstractMatrix,AbstractSciMLOperator}...) = TensorProductOperator(ops...)
Expand Down
18 changes: 9 additions & 9 deletions test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ K = 12
u = rand(N,K)
α = rand()
β = rand()
Id = IdentityOperator{N}()
Id = IdentityOperator(N)

@test issquare(Id)
@test islinear(Id)
@test IdentityOperator(u) isa IdentityOperator{N}
@test one(A) isa IdentityOperator{N}
@test IdentityOperator(u) isa IdentityOperator
@test one(A) isa IdentityOperator
@test convert(AbstractMatrix, Id) == Matrix(I, N, N)

@test iscached(Id)
@test size(Id) == (N, N)
@test Id' isa IdentityOperator{N}
@test Id' isa IdentityOperator
@test isconstant(Id)

for op in (
Expand Down Expand Up @@ -63,18 +63,18 @@ end
u = rand(N,K)
α = rand()
β = rand()
Z = NullOperator{N}()
Z = NullOperator(N)

@test issquare(Z)
@test islinear(Z)
@test NullOperator(u) isa NullOperator{N}
@test NullOperator(u) isa NullOperator
@test isconstant(Z)
@test zero(A) isa NullOperator{N}
@test zero(A) isa NullOperator
@test convert(AbstractMatrix, Z) == zeros(size(Z))

@test iscached(Z)
@test size(Z) == (N, N)
@test Z' isa NullOperator{N}
@test Z' isa NullOperator

@test Z * u ≈ zero(u)

Expand Down Expand Up @@ -229,7 +229,7 @@ end
# We can now test that caching does not rely on matmul
op = inner_op * factorize(MatrixOperator(rand(N, N)))
@test !iscached(op)
@test_nowarn op = cache_operator(op, rand(N))
@test_nowarn op = cache_operator(op, rand(N))
@test iscached(op)
u = rand(N)
@test ldiv!(rand(N), op, u) ≈ op \ u
Expand Down
Loading