-
Notifications
You must be signed in to change notification settings - Fork 42
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
Add IndexableMap #145
Closed
Closed
Add IndexableMap #145
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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,49 @@ | ||
struct IndexableMap{T,A<:LinearMap{T},F} <: LinearMap{T} | ||
lmap::A | ||
getind::F | ||
end | ||
|
||
MulStyle(A::IndexableMap) = MulStyle(A.lmap) | ||
|
||
Base.size(A::IndexableMap) = size(A.lmap) | ||
LinearAlgebra.issymmetric(A::IndexableMap) = issymmetric(A.lmap) | ||
LinearAlgebra.ishermitian(A::IndexableMap) = ishermitian(A.lmap) | ||
LinearAlgebra.isposdef(A::IndexableMap) = isposdef(A.lmap) | ||
|
||
Base.:(==)(A::IndexableMap, B::IndexableMap) = A.lmap == B.lmap | ||
|
||
Base.adjoint(A::IndexableMap) = IndexableMap(adjoint(A.lmap), (i,j) -> adjoint(A.getind(j,i))) | ||
Base.transpose(A::IndexableMap) = IndexableMap(transpose(A.lmap), (i,j) -> transpose(A.getind(j,i))) | ||
# rewrapping preserves indexability but redefines, e.g., symmetry properties | ||
LinearMap(A::IndexableMap; getind=nothing, kwargs...) = IndexableMap(LinearMap(A.lmap; kwargs...), getind) | ||
# addition/subtraction/scalar multiplication preserve indexability | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
Base.:(+)(A::IndexableMap, B::IndexableMap) = | ||
IndexableMap(A.lmap + B.lmap, (i,j) -> A.getind(i,j) + B.getind(i,j)) | ||
Base.:(-)(A::IndexableMap, B::IndexableMap) = | ||
IndexableMap(A.lmap - B.lmap, (i,j) -> A.getind(i,j) - B.getind(i,j)) | ||
for typ in (RealOrComplex, Number) | ||
@eval begin | ||
Base.:(*)(α::$typ, A::IndexableMap) = IndexableMap(α * A.lmap, (i,j) -> α*A.getind(i,j)) | ||
Base.:(*)(A::IndexableMap, α::$typ) = IndexableMap(A.lmap * α, (i,j) -> A.getind(i,j)*α) | ||
end | ||
end | ||
Base.:(*)(A::IndexableMap, J::UniformScalingMap) = | ||
size(A, 2) == J.M ? A*J.λ : throw(DimensionMismatch("*")) | ||
Base.:(*)(J::UniformScalingMap, A::IndexableMap) = | ||
size(A, 1) == J.M ? J.λ*A : throw(DimensionMismatch("*")) | ||
|
||
Base.@propagate_inbounds Base.getindex(A::IndexableMap, ::Colon, ::Colon) = A.getind(1:size(A, 1), 1:size(A, 2)) | ||
Base.@propagate_inbounds Base.getindex(A::IndexableMap, rows, ::Colon) = A.getind(rows, 1:size(A, 2)) | ||
Base.@propagate_inbounds Base.getindex(A::IndexableMap, ::Colon, cols) = A.getind(1:size(A, 1), cols) | ||
Base.@propagate_inbounds Base.getindex(A::IndexableMap, rows, cols) = A.getind(rows, cols) | ||
|
||
for (In, Out) in ((AbstractVector, AbstractVecOrMat), (AbstractMatrix, AbstractMatrix)) | ||
@eval begin | ||
function _unsafe_mul!(y::$Out, A::IndexableMap, x::$In) | ||
return _unsafe_mul!(y, A.lmap, x) | ||
end | ||
function _unsafe_mul!(y::$Out, A::IndexableMap, x::$In, α::Number, β::Number) | ||
return _unsafe_mul!(y, A.lmap, x, α, β) | ||
end | ||
end | ||
end |
This file contains 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 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -6,12 +6,16 @@ struct UniformScalingMap{T} <: LinearMap{T} | |||||
return new{typeof(λ)}(λ, M) | ||||||
end | ||||||
end | ||||||
UniformScalingMap(λ::Number, M::Int, N::Int) = | ||||||
(M == N ? | ||||||
UniformScalingMap(λ, M) : error("UniformScalingMap needs to be square")) | ||||||
UniformScalingMap(λ::T, sz::Dims{2}) where {T} = | ||||||
(sz[1] == sz[2] ? | ||||||
UniformScalingMap(λ, sz[1]) : error("UniformScalingMap needs to be square")) | ||||||
@deprecate UniformScalingMap(λ::Number, M::Int, N::Int) UniformScalingMap(λ, M) false | ||||||
@deprecate UniformScalingMap(λ::Number, sz::Dims{2}) UniformScalingMap(λ, sz[1]) false | ||||||
# the following methods are misleading: they introduce the opportunity to pass 2 dims, | ||||||
# but throw if the dims are unequal! | ||||||
# UniformScalingMap(λ::Number, M::Int, N::Int) = | ||||||
# (M == N ? | ||||||
# UniformScalingMap(λ, M) : error("UniformScalingMap needs to be square")) | ||||||
# UniformScalingMap(λ::T, sz::Dims{2}) where {T} = | ||||||
# (sz[1] == sz[2] ? | ||||||
# UniformScalingMap(λ, sz[1]) : error("UniformScalingMap needs to be square")) | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Seems like something like this should be the natural default for this type. |
||||||
MulStyle(::UniformScalingMap) = FiveArg() | ||||||
|
||||||
|
@@ -27,25 +31,32 @@ Base.:(==)(A::UniformScalingMap, B::UniformScalingMap) = (A.λ == B.λ && A.M == | |||||
|
||||||
# special transposition behavior | ||||||
LinearAlgebra.transpose(A::UniformScalingMap) = A | ||||||
LinearAlgebra.adjoint(A::UniformScalingMap) = UniformScalingMap(conj(A.λ), size(A)) | ||||||
LinearAlgebra.adjoint(A::UniformScalingMap) = UniformScalingMap(conj(A.λ), A.M) | ||||||
|
||||||
# multiplication with scalar | ||||||
Base.:(*)(A::UniformScaling, B::LinearMap) = A.λ * B | ||||||
Base.:(*)(A::LinearMap, B::UniformScaling) = A * B.λ | ||||||
Base.:(*)(α::Number, J::UniformScalingMap) = UniformScalingMap(α * J.λ, size(J)) | ||||||
Base.:(*)(J::UniformScalingMap, α::Number) = UniformScalingMap(J.λ * α, size(J)) | ||||||
Base.:(*)(α::Number, J::UniformScalingMap) = UniformScalingMap(α * J.λ, J.M) | ||||||
Base.:(*)(J::UniformScalingMap, α::Number) = UniformScalingMap(J.λ * α, J.M) | ||||||
# needed for disambiguation | ||||||
Base.:(*)(α::RealOrComplex, J::UniformScalingMap) = UniformScalingMap(α * J.λ, size(J)) | ||||||
Base.:(*)(J::UniformScalingMap, α::RealOrComplex) = UniformScalingMap(J.λ * α, size(J)) | ||||||
Base.:(*)(α::RealOrComplex, J::UniformScalingMap) = UniformScalingMap(α * J.λ, J.M) | ||||||
Base.:(*)(J::UniformScalingMap, α::RealOrComplex) = UniformScalingMap(J.λ * α, J.M) | ||||||
|
||||||
# multiplication with vector | ||||||
Base.:(*)(J::UniformScalingMap, x::AbstractVector) = | ||||||
length(x) == J.M ? J.λ * x : throw(DimensionMismatch("*")) | ||||||
# multiplication with matrix | ||||||
# multiplication with map/matrix | ||||||
Base.:(*)(J::UniformScalingMap, B::LinearMap) = | ||||||
size(B, 1) == J.M ? J.λ * B : throw(DimensionMismatch("*")) | ||||||
Base.:(*)(J::UniformScalingMap, B::AbstractMatrix) = | ||||||
size(B, 1) == J.M ? J.λ * LinearMap(B) : throw(DimensionMismatch("*")) | ||||||
Base.:(*)(A::AbstractMatrix, J::UniformScalingMap) = | ||||||
size(A, 2) == J.M ? LinearMap(A) * J.λ : throw(DimensionMismatch("*")) | ||||||
size(B, 1) == J.M ? J.λ * convert_to_lmap_(B) : throw(DimensionMismatch("*")) | ||||||
Base.:(*)(A::LinearMap, J::UniformScalingMap) = | ||||||
size(A, 2) == J.M ? A * J.λ : throw(DimensionMismatch("*")) | ||||||
Base.:(*)(A::VecOrMat, J::UniformScalingMap) = | ||||||
size(A, 2) == J.M ? convert_to_lmap_(A) * J.λ : throw(DimensionMismatch("*")) | ||||||
Base.:(*)(U::UniformScalingMap, J::UniformScalingMap) = | ||||||
U.M == J.M ? UniformScalingMap(U.λ*J.λ, J.M) : throw(DimensionMismatch("*")) | ||||||
|
||||||
# disambiguation | ||||||
Base.:(*)(xc::LinearAlgebra.AdjointAbsVec, J::UniformScalingMap) = xc * J.λ | ||||||
Base.:(*)(xt::LinearAlgebra.TransposeAbsVec, J::UniformScalingMap) = xt * J.λ | ||||||
|
Empty file.
This file contains 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 |
---|---|---|
|
@@ -34,3 +34,5 @@ include("conversion.jl") | |
include("left.jl") | ||
|
||
include("fillmap.jl") | ||
|
||
include("indexablemap.jl") |
This file contains 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 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
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.
For a
WrappedMap
could the defaultgetind
simply beBase.getindex
because presumably that is the expected behavior for a matrix or even for anyAbstractArray
that already has agetindex
method.