-
Notifications
You must be signed in to change notification settings - Fork 15
add transpose and adjoint #41
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
Open
PetrKryslUCSD
wants to merge
1
commit into
gridap:master
Choose a base branch
from
PetrKryslUCSD:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -134,6 +134,73 @@ function Base.copy(a::SparseMatrixCSR{Bi}) where Bi | |
| SparseMatrixCSR{Bi}(a.m,a.n,copy(a.rowptr),copy(a.colval),copy(a.nzval)) | ||
| end | ||
|
|
||
| widelength(x::SparseMatrixCSR) = prod(Int64.(size(x))) | ||
|
|
||
| function Base.sizehint!(S::SparseMatrixCSR, n::Integer) | ||
| nhint = min(n, widelength(S)) | ||
| sizehint!(getcolval(S), nhint) | ||
| sizehint!(nonzeros(S), nhint) | ||
| return S | ||
| end | ||
|
|
||
| Base.adjoint(A::SparseMatrixCSR) = Adjoint(A) | ||
| Base.transpose(A::SparseMatrixCSR) = Transpose(A) | ||
| Base.copy(A::Transpose{<:Any,<:SparseMatrixCSR}) = | ||
| ftranspose_csr(A.parent, identity) | ||
| Base.copy(A::Adjoint{<:Any,<:SparseMatrixCSR}) = | ||
| ftranspose_csr(A.parent, adjoint) | ||
|
|
||
| function ftranspose_csr(A::SparseMatrixCSR{Bi,TvA,Ti}, f::Function, eltype::Type{Tv} = TvA) where {Bi,Tv,TvA,Ti} | ||
| X = SparseMatrixCSR{Bi}(size(A, 2), size(A, 1), | ||
| ones(Ti, size(A, 2)+1), | ||
| Vector{Ti}(undef, 0), | ||
| Vector{Tv}(undef, 0)) | ||
| sizehint!(X, nnz(A)) | ||
| return halfperm_csr!(X, A, axes(A,1), f) | ||
| end | ||
|
|
||
| function halfperm_csr!(X::SparseMatrixCSR{Bi,Tv,Ti}, A::SparseMatrixCSR{Bi,TvA,Ti}, | ||
| q::AbstractVector{<:Integer}, f::F = identity) where {Bi,Tv,TvA,Ti,F<:Function} | ||
| _computecolptrs_halfperm_csr!(X, A) | ||
| _distributevals_halfperm_csr!(X, A, q, f) | ||
| return X | ||
| end | ||
|
|
||
| function _computecolptrs_halfperm_csr!(X::SparseMatrixCSR{Bi,Tv,Ti}, A::SparseMatrixCSR{Bi,TvA,Ti}) where {Bi,Tv,TvA,Ti} | ||
| # Compute transpose(A[:,q])'s row counts. Store shifted forward one position in X.rowptr. | ||
| fill!(X.rowptr, 0) | ||
| o = getoffset(A) | ||
| @inbounds for k in 1:nnz(A) | ||
| X.rowptr[A.colval[k] + o + 1] += 1 | ||
| end | ||
| # Compute transpose(A[:,q])'s row pointers. Store shifted forward one position in X.rowptr. | ||
| X.rowptr[1] = Bi | ||
| countsum = Bi | ||
| @inbounds for k in 2:(size(A, 2) + 1) | ||
| overwritten = X.rowptr[k] | ||
| X.rowptr[k] = countsum | ||
| countsum += overwritten | ||
| end | ||
| end | ||
|
|
||
| function _distributevals_halfperm_csr!(X::SparseMatrixCSR{Bi,Tv,Ti}, | ||
| A::SparseMatrixCSR{Bi,TvA,Ti}, q::AbstractVector{<:Integer}, f::F) where {Bi,Tv,TvA,Ti,F<:Function} | ||
| resize!(X.nzval, nnz(A)) | ||
| resize!(X.colval, nnz(A)) | ||
| o = getoffset(A) | ||
| @inbounds for Xi in axes(A,1) | ||
| Aj = q[Xi] | ||
| for Ak in nzrange(A, Aj) | ||
| Ai = A.colval[Ak] + o | ||
| Xk = X.rowptr[Ai + 1] | ||
| X.colval[Xk] = Xi - 1 + Bi # since colval are Bi-based | ||
| X.nzval[Xk] = f(A.nzval[Ak]) | ||
| X.rowptr[Ai + 1] += 1 | ||
| end | ||
| end | ||
| return | ||
| end | ||
|
|
||
| _copy_and_increment(x) = copy(x) .+ 1 | ||
|
|
||
| function LinearAlgebra.fillstored!(a::SparseMatrixCSR,v) | ||
|
|
@@ -220,6 +287,8 @@ getrowptr(S::SparseMatrixCSR) = S.rowptr | |
| getnzval(S::SparseMatrixCSR) = S.nzval | ||
| getcolval(S::SparseMatrixCSR) = S.colval | ||
|
|
||
| colvals(S::SparseMatrixCSR) = S.colval | ||
|
|
||
| """ | ||
| getBi(S::SparseMatrixCSR{Bi}) where {Bi} | ||
|
|
||
|
|
@@ -263,7 +332,7 @@ nonzeros(S::SparseMatrixCSR) = S.nzval | |
| nzvalview(S::SparseMatrixCSR) = view(nonzeros(S), 1:nnz(S)) | ||
|
|
||
| """ | ||
| colvals(S::SparseMatrixCSR{Bi}) where {Bi} | ||
| colvals(S::SparseMatrixCSR) | ||
|
|
||
| Return a vector of the col indices of `S`. The stored values are indexes to arrays | ||
| with `Bi`-based indexing, but the `colvals(S)` array itself is a standard 1-based | ||
|
|
@@ -273,15 +342,15 @@ Providing access to how the col indices are stored internally | |
| can be useful in conjunction with iterating over structural | ||
| nonzero values. See also [`nonzeros`](@ref) and [`nzrange`](@ref). | ||
| """ | ||
| colvals(S::SparseMatrixCSR) = S.colval | ||
|
|
||
| """ | ||
| nzrange(S::SparseMatrixCSR{Bi}, row::Integer) where {Bi} | ||
|
|
||
| Return the range of indices to the structural nonzero values of a | ||
| sparse matrix row. The returned range of indices is always 1-based even for `Bi != 1`. | ||
| """ | ||
| nzrange(S::SparseMatrixCSR{Bi}, row::Integer) where {Bi} = S.rowptr[row]+getoffset(S):S.rowptr[row+1]-Bi | ||
| nzrange(S::SparseMatrixCSR{Bi}, row::Integer) | ||
|
|
||
| Return the range of indices to the structural nonzero values of a | ||
| sparse matrix row. The returned range of indices is always 1-based even for `Bi != 1`. | ||
| """ | ||
| nzrange(S::SparseMatrixCSR{Bi}, row::Integer) where {Bi} = S.rowptr[row]+getoffset(S):S.rowptr[row+1]-Bi | ||
|
Member
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. I think this also got indented by mistake? Both function and docstring look weird. |
||
|
|
||
| """ | ||
| findnz(S::SparseMatrixCSR{Bi,Tv,Ti}) | ||
|
|
||
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.
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.
Is there a reason to split the docstring from the function? I think this might just be a mistake.
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.
Also, is there a reason you changed the docstring?
Bidoes get used within the documentation of the function itself, so it might be better if it stays there?