-
Notifications
You must be signed in to change notification settings - Fork 39
Handle Statistics and SparseArrays as extensions #286
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc5ed85
Handle Statistics and SparseArrays as extensions
dkarrasch 8d61af5
review comments
dkarrasch 209a1ba
clean-ip
dkarrasch 3cc7063
add deprecation
dkarrasch 7c7a333
error or load
dkarrasch 6147c9e
Revert "error or load"
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| module FillArraysSparseArraysExt | ||
|
|
||
| using SparseArrays | ||
| import Base: convert, kron | ||
| using FillArrays | ||
| using FillArrays: RectDiagonalFill, RectOrDiagonalFill, ZerosVector, ZerosMatrix, getindex_value | ||
| using LinearAlgebra | ||
|
|
||
| ################## | ||
| ## Sparse arrays | ||
| ################## | ||
| SparseVector{T}(Z::ZerosVector) where T = spzeros(T, length(Z)) | ||
| SparseVector{Tv,Ti}(Z::ZerosVector) where {Tv,Ti} = spzeros(Tv, Ti, length(Z)) | ||
|
|
||
| convert(::Type{AbstractSparseVector}, Z::ZerosVector{T}) where T = spzeros(T, length(Z)) | ||
| convert(::Type{AbstractSparseVector{T}}, Z::ZerosVector) where T= spzeros(T, length(Z)) | ||
|
|
||
| SparseMatrixCSC{T}(Z::ZerosMatrix) where T = spzeros(T, size(Z)...) | ||
| SparseMatrixCSC{Tv,Ti}(Z::Zeros{T,2,Axes}) where {Tv,Ti<:Integer,T,Axes} = spzeros(Tv, Ti, size(Z)...) | ||
|
|
||
| convert(::Type{AbstractSparseMatrix}, Z::ZerosMatrix{T}) where T = spzeros(T, size(Z)...) | ||
| convert(::Type{AbstractSparseMatrix{T}}, Z::ZerosMatrix) where T = spzeros(T, size(Z)...) | ||
|
|
||
| convert(::Type{AbstractSparseArray}, Z::Zeros{T}) where T = spzeros(T, size(Z)...) | ||
| convert(::Type{AbstractSparseArray{Tv}}, Z::Zeros{T}) where {T,Tv} = spzeros(Tv, size(Z)...) | ||
| convert(::Type{AbstractSparseArray{Tv,Ti}}, Z::Zeros{T}) where {T,Tv,Ti} = spzeros(Tv, Ti, size(Z)...) | ||
| convert(::Type{AbstractSparseArray{Tv,Ti,N}}, Z::Zeros{T,N}) where {T,Tv,Ti,N} = spzeros(Tv, Ti, size(Z)...) | ||
|
|
||
| SparseMatrixCSC{Tv}(Z::Eye{T}) where {T,Tv} = SparseMatrixCSC{Tv}(I, size(Z)...) | ||
| # works around missing `speye`: | ||
| SparseMatrixCSC{Tv,Ti}(Z::Eye{T}) where {T,Tv,Ti<:Integer} = | ||
| convert(SparseMatrixCSC{Tv,Ti}, SparseMatrixCSC{Tv}(I, size(Z)...)) | ||
|
|
||
| convert(::Type{AbstractSparseMatrix}, Z::Eye{T}) where {T} = SparseMatrixCSC{T}(I, size(Z)...) | ||
| convert(::Type{AbstractSparseMatrix{Tv}}, Z::Eye{T}) where {T,Tv} = SparseMatrixCSC{Tv}(I, size(Z)...) | ||
|
|
||
| convert(::Type{AbstractSparseArray}, Z::Eye{T}) where T = SparseMatrixCSC{T}(I, size(Z)...) | ||
| convert(::Type{AbstractSparseArray{Tv}}, Z::Eye{T}) where {T,Tv} = SparseMatrixCSC{Tv}(I, size(Z)...) | ||
|
|
||
|
|
||
| convert(::Type{AbstractSparseArray{Tv,Ti}}, Z::Eye{T}) where {T,Tv,Ti} = | ||
| convert(SparseMatrixCSC{Tv,Ti}, Z) | ||
| convert(::Type{AbstractSparseArray{Tv,Ti,2}}, Z::Eye{T}) where {T,Tv,Ti} = | ||
| convert(SparseMatrixCSC{Tv,Ti}, Z) | ||
|
|
||
| function SparseMatrixCSC{Tv}(R::RectOrDiagonalFill) where {Tv} | ||
| SparseMatrixCSC{Tv,eltype(axes(R,1))}(R) | ||
| end | ||
| function SparseMatrixCSC{Tv,Ti}(R::RectOrDiagonalFill) where {Tv,Ti} | ||
| Base.require_one_based_indexing(R) | ||
| v = parent(R) | ||
| J = getindex_value(v)*I | ||
| SparseMatrixCSC{Tv,Ti}(J, size(R)) | ||
| end | ||
|
|
||
| # TODO: remove in v2.0 | ||
| @deprecate kron(E1::RectDiagonalFill, E2::RectDiagonalFill) kron(sparse(E1), sparse(E2)) | ||
|
|
||
| end # module | ||
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,33 @@ | ||
| module FillArraysStatisticsExt | ||
|
|
||
| import Statistics: mean, var, cov, cor | ||
| using LinearAlgebra: diagind | ||
|
|
||
| using FillArrays | ||
| using FillArrays: AbstractFill, AbstractFillVector, AbstractFillMatrix, getindex_value | ||
|
|
||
| mean(A::AbstractFill; dims=(:)) = mean(identity, A; dims=dims) | ||
| function mean(f::Union{Function, Type}, A::AbstractFill; dims=(:)) | ||
| val = float(f(getindex_value(A))) | ||
| dims isa Colon ? val : | ||
| Fill(val, ntuple(d -> d in dims ? 1 : size(A,d), ndims(A))...) | ||
| end | ||
|
|
||
|
|
||
| function var(A::AbstractFill{T}; corrected::Bool=true, mean=nothing, dims=(:)) where {T<:Number} | ||
| dims isa Colon ? zero(float(T)) : | ||
| Zeros{float(T)}(ntuple(d -> d in dims ? 1 : size(A,d), ndims(A))...) | ||
| end | ||
|
|
||
| cov(::AbstractFillVector{T}; corrected::Bool=true) where {T<:Number} = zero(float(T)) | ||
| cov(A::AbstractFillMatrix{T}; corrected::Bool=true, dims::Integer=1) where {T<:Number} = | ||
| Zeros{float(T)}(size(A, 3-dims), size(A, 3-dims)) | ||
|
|
||
| cor(::AbstractFillVector{T}) where {T<:Number} = one(float(T)) | ||
| function cor(A::AbstractFillMatrix{T}; dims::Integer=1) where {T<:Number} | ||
| out = fill(float(T)(NaN), size(A, 3-dims), size(A, 3-dims)) | ||
| out[diagind(out)] .= 1 | ||
| out | ||
| end | ||
|
|
||
| end # module |
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
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.
Uh oh!
There was an error while loading. Please reload this page.