-
Notifications
You must be signed in to change notification settings - Fork 41
Add FillMaps #113
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
Merged
Merged
Add FillMaps #113
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a0d558d
draft for fillmap
dkarrasch 0bad761
fix typos, remove obsolete code
dkarrasch 90c600d
add tests
dkarrasch 611ada9
add conversion
dkarrasch 92cab57
general edits
dkarrasch a22e705
conversion fix
dkarrasch 62883c6
minor fix
dkarrasch b7c2051
minor simplifications
dkarrasch 2cf8c33
minor fix for Julia v1.0
dkarrasch 40037b2
improve coverage
dkarrasch 6145ec4
fix test
dkarrasch 90559a4
add show method
dkarrasch d88e0fc
include documentation
dkarrasch 8a9fc16
improve coverage
dkarrasch b344a97
fix fillmap show
dkarrasch f6cd91e
final fixes
dkarrasch 649ed54
typo fix
dkarrasch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
struct FillMap{T} <: LinearMap{T} | ||
λ::T | ||
size::Dims{2} | ||
function FillMap(λ::T, dims::Dims{2}) where {T} | ||
(dims[1]>=0 && dims[2]>=0) || throw(ArgumentError("dims of FillMap must be non-negative")) | ||
return new{T}(λ, dims) | ||
end | ||
end | ||
|
||
# properties | ||
Base.size(A::FillMap) = A.size | ||
MulStyle(A::FillMap) = FiveArg() | ||
LinearAlgebra.issymmetric(A::FillMap) = A.size[1] == A.size[2] | ||
LinearAlgebra.ishermitian(A::FillMap) = isreal(A.λ) && A.size[1] == A.size[2] | ||
LinearAlgebra.isposdef(A::FillMap) = (size(A, 1) == size(A, 2) == 1 && isposdef(A.λ)) | ||
Base.:(==)(A::FillMap, B::FillMap) = A.λ == B.λ && A.size == B.size | ||
|
||
LinearAlgebra.adjoint(A::FillMap) = FillMap(adjoint(A.λ), reverse(A.size)) | ||
LinearAlgebra.transpose(A::FillMap) = FillMap(transpose(A.λ), reverse(A.size)) | ||
|
||
function Base.:(*)(A::FillMap, x::AbstractVector) | ||
T = typeof(oneunit(eltype(A)) * (zero(eltype(x)) + zero(eltype(x)))) | ||
return fill(iszero(A.λ) ? zero(T) : A.λ*sum(x), A.size[1]) | ||
end | ||
|
||
function _unsafe_mul!(y::AbstractVecOrMat, A::FillMap, x::AbstractVector) | ||
return fill!(y, iszero(A.λ) ? zero(eltype(y)) : A.λ*sum(x)) | ||
end | ||
|
||
function _unsafe_mul!(y::AbstractVecOrMat, A::FillMap, x::AbstractVector, α::Number, β::Number) | ||
if iszero(α) | ||
!isone(β) && rmul!(y, β) | ||
else | ||
temp = A.λ * sum(x) * α | ||
if iszero(β) | ||
y .= temp | ||
elseif isone(β) | ||
y .+= temp | ||
else | ||
y .= y .* β .+ temp | ||
end | ||
end | ||
return y | ||
end | ||
|
||
Base.:(+)(A::FillMap, B::FillMap) = A.size == B.size ? FillMap(A.λ + B.λ, A.size) : throw(DimensionMismatch()) | ||
Base.:(-)(A::FillMap) = FillMap(-A.λ, A.size) | ||
Base.:(*)(λ::Number, A::FillMap) = FillMap(λ * A.λ, size(A)) | ||
Base.:(*)(A::FillMap, λ::Number) = FillMap(A.λ * λ, size(A)) | ||
Base.:(*)(λ::RealOrComplex, A::FillMap) = FillMap(λ * A.λ, size(A)) | ||
Base.:(*)(A::FillMap, λ::RealOrComplex) = FillMap(A.λ * λ, size(A)) | ||
|
||
function Base.:(*)(A::FillMap, B::FillMap) | ||
check_dim_mul(A, B) | ||
return FillMap(A.λ*B.λ*size(A, 2), (size(A, 1), size(B, 2))) | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using LinearMaps, LinearAlgebra, Test | ||
|
||
@testset "filled maps" begin | ||
M, N = 2, 3 | ||
μ = rand() | ||
for λ in (true, false, 3, μ, μ + 2im) | ||
L = LinearMap(λ, (M, N)) | ||
@test L == LinearMap(λ, M, N) | ||
@test occursin("$M×$N LinearMaps.FillMap{$(typeof(λ))} with fill value: $λ", sprint((t, s) -> show(t, "text/plain", s), L)) | ||
@test LinearMaps.MulStyle(L) === LinearMaps.FiveArg() | ||
A = fill(λ, (M, N)) | ||
x = rand(typeof(λ) <: Real ? Float64 : ComplexF64, 3) | ||
X = rand(typeof(λ) <: Real ? Float64 : ComplexF64, 3, 4) | ||
w = similar(x, 2) | ||
W = similar(X, 2, 4) | ||
@test size(L) == (M, N) | ||
@test adjoint(L) == LinearMap(adjoint(λ), (3,2)) | ||
@test transpose(L) == LinearMap(λ, (3,2)) | ||
@test Matrix(L) == A | ||
@test L * x ≈ A * x | ||
@test mul!(w, L, x) ≈ A * x | ||
@test mul!(W, L, X) ≈ A * X | ||
for α in (true, false, 1, 0, randn()), β in (true, false, 1, 0, randn()) | ||
@test mul!(copy(w), L, x, α, β) ≈ fill(λ * sum(x) * α, M) + w * β | ||
@test mul!(copy(W), L, X, α, β) ≈ λ * reduce(vcat, sum(X, dims=1) for _ in 1:2) * α + W * β | ||
end | ||
end | ||
@test issymmetric(LinearMap(μ + 1im, (3, 3))) | ||
@test ishermitian(LinearMap(μ + 0im, (3, 3))) | ||
@test isposdef(LinearMap(μ, (1,1))) == isposdef(μ) | ||
@test !isposdef(LinearMap(μ, (3,3))) | ||
α = rand() | ||
β = rand() | ||
@test LinearMap(μ, (M, N)) + LinearMap(α, (M, N)) == LinearMap(μ + α, (M, N)) | ||
@test LinearMap(μ, (M, N)) - LinearMap(α, (M, N)) == LinearMap(μ - α, (M, N)) | ||
@test α*LinearMap(μ, (M, N)) == LinearMap(α * μ, (M, N)) | ||
@test LinearMap(μ, (M, N))*α == LinearMap(μ * α, (M, N)) | ||
@test LinearMap(μ, (M, N))*LinearMap(μ, (N, M)) == LinearMap(μ^2*N, (M, M)) | ||
@test Matrix(LinearMap(μ, (M, N))*LinearMap(μ, (N, M))) ≈ fill(μ, (M, N))*fill(μ, (N, M)) | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,5 @@ include("kronecker.jl") | |
include("conversion.jl") | ||
|
||
include("left.jl") | ||
|
||
include("fillmap.jl") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the only somewhat controversial things are these constructors. They are formally unambiguous, but are they unambiguous enough for users (sufficiently distinct from
UniformScalinMap
constructor?)? Or should we export theFillMap
construct and not include these?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is one of few the
LinearMap
types that you want to construct explicitly, I guess it would make sense to export theFillMap
constructor explicitly. It's somewhat asymmetric thatFunctionMap
is then still constructed viaLinearMap
. Just usingLinearMap(scalar, ...)
is also fine by me, no strong preference.I do wonder what this is used for though :-) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know either what this is used for, but (a) there is an entire package (
FillArrays.jl
) devoted to such arrays, and (b) I do notice that our simple imlementation of matvecmul and matmatmul is even faster than theirs.And yes, I agree that this is so specific that it is perhaps better to have an explicit constructor. It was more "sentimentality" that made me consider keeping the "one constructor for all types" status.