-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support iterate over nonzeros & rewrite IMatrix (#71)
- Loading branch information
Showing
28 changed files
with
454 additions
and
335 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,29 +1,32 @@ | ||
export IMatrix | ||
|
||
""" | ||
IMatrix{N, Tv}() | ||
IMatrix{N}() where N = IMatrix{N, Int64}() | ||
IMatrix{Tv} | ||
IMatrix(n) -> IMatrix | ||
IMatrix(A::AbstractMatrix{T}) where T -> IMatrix | ||
IMatrix matrix, with size N as label, use `Int64` as its default type, both `*` and `kron` are optimized. | ||
""" | ||
struct IMatrix{N,Tv} <: AbstractMatrix{Tv} end | ||
IMatrix{N}() where {N} = IMatrix{N,Bool}() | ||
IMatrix(N::Int) = IMatrix{N}() | ||
struct IMatrix{Tv} <: AbstractMatrix{Tv} | ||
n::Int | ||
end | ||
IMatrix(n::Integer) = IMatrix{Bool}(n) | ||
|
||
size(A::IMatrix{N}, i::Int) where {N} = N | ||
size(A::IMatrix{N}) where {N} = (N, N) | ||
getindex(A::IMatrix{N,T}, i::Integer, j::Integer) where {N,T} = T(i == j) | ||
Base.size(A::IMatrix, i::Int) = (@assert i == 1 || i == 2; A.n) | ||
Base.size(A::IMatrix) = (A.n, A.n) | ||
Base.getindex(::IMatrix{T}, i::Integer, j::Integer) where {T} = T(i == j) | ||
|
||
Base.:(==)(d1::IMatrix{Na}, d2::IMatrix{Nb}) where {Na,Nb} = Na == Nb | ||
Base.isapprox(d1::IMatrix{Na}, d2::IMatrix{Nb}; kwargs...) where {Na,Nb} = Na == Nb | ||
Base.:(==)(d1::IMatrix, d2::IMatrix) = d1.n == d2.n | ||
Base.isapprox(d1::IMatrix, d2::IMatrix; kwargs...) = d1 == d2 | ||
|
||
####### sparse matrix ###### | ||
nnz(M::IMatrix{N}) where {N} = N | ||
nonzeros(M::IMatrix{N,T}) where {N,T} = ones(T, N) | ||
findnz(M::IMatrix{N,T}) where {N,T} = (collect(1:N), collect(1:N), ones(T, N)) | ||
ishermitian(D::IMatrix) = true | ||
isdense(::IMatrix) = false | ||
Base.similar(A::IMatrix{Tv}, ::Type{T}) where {Tv,T} = IMatrix{T}(A.n) | ||
function Base.copyto!(A::IMatrix, B::IMatrix) | ||
if A.n != B.n | ||
throw(DimensionMismatch("matrix dimension mismatch, got $(A.n) and $(B.n)")) | ||
end | ||
A | ||
end | ||
LinearAlgebra.ishermitian(D::IMatrix) = true | ||
|
||
similar(::IMatrix{N,Tv}, ::Type{T}) where {N,Tv,T} = IMatrix{N,T}() | ||
copyto!(A::IMatrix{N}, B::IMatrix{N}) where {N} = A | ||
####### sparse matrix ###### | ||
nnz(M::IMatrix) = M.n | ||
findnz(M::IMatrix{T}) where {T} = (collect(1:M.n), collect(1:M.n), ones(T, M.n)) |
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
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
Oops, something went wrong.
401ba18
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.
Released via Ion
@JuliaRegistrator register branch=master
401ba18
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.
Registration pull request created: JuliaRegistries/General/63056
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: