Skip to content

implement to_vec for sparse arrays #202

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 3 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
name = "FiniteDifferences"
uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
version = "0.12.22"
version = "0.12.23"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Richardson = "708f8203-808e-40c0-ba2d-98a6953ed40d"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
Expand Down
1 change: 1 addition & 0 deletions src/FiniteDifferences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using LinearAlgebra
using Printf
using Random
using Richardson
using SparseArrays
using StaticArrays

export to_vec, grad, jacobian, jvp, j′vp
Expand Down
24 changes: 24 additions & 0 deletions src/to_vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,30 @@ function to_vec(X::T) where {T<:PermutedDimsArray}
return x_vec, PermutedDimsArray_from_vec
end

function to_vec(v::SparseVector)
inds, _ = findnz(v)
sizes = size(v)

x_vec, back = to_vec(collect(v))
function SparseVector_from_vec(x_v)
v_values = back(x_v)
return sparsevec(inds, v_values[inds], sizes...)
end
return x_vec, SparseVector_from_vec
end

function to_vec(m::SparseMatrixCSC)
is, js, _ = findnz(m)
sizes = size(m)

x_vec, back = to_vec(collect(m))
function SparseMatrixCSC_from_vec(x_v)
v_values = back(x_v)
return sparse(is, js, [v_values[i, j] for (i, j) in zip(is, js)], sizes...)
end
return x_vec, SparseMatrixCSC_from_vec
end

# Factorizations

function to_vec(x::F) where {F <: SVD}
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using FiniteDifferences
using LinearAlgebra
using Printf
using Random
using SparseArrays
using StaticArrays
using Test

Expand Down
5 changes: 5 additions & 0 deletions test/to_vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ end
)
end

@testset "SparseArrays" begin
test_to_vec(sparsevec([1 2 0; 0 0 3; 0 4 0.0]))
test_to_vec(sparse([1 2 0; 0 0 3; 0 4 0.0]))
end

@testset "Factorizations" begin
# (100, 100) is needed to test for the NaNs that can appear in the
# qr(M).T matrix
Expand Down