Skip to content

Commit

Permalink
Sparse matrices with Int indices (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle authored May 14, 2024
1 parent 95607e0 commit 57e3e73
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/adtypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Singleton struct for integration with the sparsity detection framework of ADType
julia> using ADTypes, SparseConnectivityTracer
julia> ADTypes.jacobian_sparsity(diff, rand(4), TracerSparsityDetector())
3×4 SparseArrays.SparseMatrixCSC{Bool, UInt64} with 6 stored entries:
3×4 SparseArrays.SparseMatrixCSC{Bool, Int64} with 6 stored entries:
1 1 ⋅ ⋅
⋅ 1 1 ⋅
⋅ ⋅ 1 1
Expand All @@ -21,7 +21,7 @@ julia> using ADTypes, SparseConnectivityTracer
julia> f(x) = x[1] + x[2]*x[3] + 1/x[4];
julia> ADTypes.hessian_sparsity(f, rand(4), TracerSparsityDetector())
4×4 SparseArrays.SparseMatrixCSC{Bool, UInt64} with 3 stored entries:
4×4 SparseArrays.SparseMatrixCSC{Bool, Int64} with 3 stored entries:
⋅ ⋅ ⋅ ⋅
⋅ ⋅ 1 ⋅
⋅ 1 ⋅ ⋅
Expand Down
20 changes: 10 additions & 10 deletions src/pattern.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ julia> x = rand(3);
julia> f(x) = [x[1]^2, 2 * x[1] * x[2]^2, sign(x[3])];
julia> connectivity_pattern(f, x)
3×3 SparseArrays.SparseMatrixCSC{Bool, UInt64} with 4 stored entries:
3×3 SparseArrays.SparseMatrixCSC{Bool, Int64} with 4 stored entries:
1 ⋅ ⋅
1 1 ⋅
⋅ ⋅ 1
Expand Down Expand Up @@ -82,8 +82,8 @@ function connectivity_pattern_to_mat(
xt::AbstractArray{T}, yt::AbstractArray{<:Number}
) where {T<:ConnectivityTracer}
n, m = length(xt), length(yt)
I = UInt64[] # row indices
J = UInt64[] # column indices
I = Int[] # row indices
J = Int[] # column indices
V = Bool[] # values
for (i, y) in enumerate(yt)
if y isa T
Expand Down Expand Up @@ -113,7 +113,7 @@ julia> x = rand(3);
julia> f(x) = [x[1]^2, 2 * x[1] * x[2]^2, sign(x[3])];
julia> jacobian_pattern(f, x)
3×3 SparseArrays.SparseMatrixCSC{Bool, UInt64} with 3 stored entries:
3×3 SparseArrays.SparseMatrixCSC{Bool, Int64} with 3 stored entries:
1 ⋅ ⋅
1 1 ⋅
⋅ ⋅ ⋅
Expand Down Expand Up @@ -143,8 +143,8 @@ function jacobian_pattern_to_mat(
xt::AbstractArray{T}, yt::AbstractArray{<:Number}
) where {T<:JacobianTracer}
n, m = length(xt), length(yt)
I = UInt64[] # row indices
J = UInt64[] # column indices
I = Int[] # row indices
J = Int[] # column indices
V = Bool[] # values
for (i, y) in enumerate(yt)
if y isa T
Expand Down Expand Up @@ -174,7 +174,7 @@ julia> x = rand(5);
julia> f(x) = x[1] + x[2]*x[3] + 1/x[4] + 1*x[5];
julia> hessian_pattern(f, x)
5×5 SparseArrays.SparseMatrixCSC{Bool, UInt64} with 3 stored entries:
5×5 SparseArrays.SparseMatrixCSC{Bool, Int64} with 3 stored entries:
⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ 1 ⋅ ⋅
⋅ 1 ⋅ ⋅ ⋅
Expand All @@ -184,7 +184,7 @@ julia> hessian_pattern(f, x)
julia> g(x) = f(x) + x[2]^x[5];
julia> hessian_pattern(g, x)
5×5 SparseArrays.SparseMatrixCSC{Bool, UInt64} with 7 stored entries:
5×5 SparseArrays.SparseMatrixCSC{Bool, Int64} with 7 stored entries:
⋅ ⋅ ⋅ ⋅ ⋅
⋅ 1 1 ⋅ 1
⋅ 1 ⋅ ⋅ ⋅
Expand All @@ -204,8 +204,8 @@ function hessian_pattern_to_mat(
) where {TI,S,D}
# Allocate Hessian matrix
n = length(xt)
I = UInt64[] # row indices
J = UInt64[] # column indices
I = Int[] # row indices
J = Int[] # column indices
V = Bool[] # values

for i in keys(yt.inputs)
Expand Down
5 changes: 3 additions & 2 deletions test/adtypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ y = zeros(9)
J1 = jacobian_sparsity(diff, x, sd)
J2 = jacobian_sparsity((y, x) -> y .= diff(x), y, x, sd)
@test J1 == J2
@test J1 isa SparseMatrixCSC
@test J2 isa SparseMatrixCSC
@test J1 isa SparseMatrixCSC{Bool,Int}
@test J2 isa SparseMatrixCSC{Bool,Int}
@test nnz(J1) == nnz(J2) == 18

H1 = hessian_sparsity(x -> sum(diff(x)), x, sd)
Expand All @@ -20,6 +20,7 @@ H1 = hessian_sparsity(x -> sum(diff(x)), x, sd)
x = rand(5)
f(x) = x[1] + x[2] * x[3] + 1 / x[4] + 1 * x[5]
H2 = hessian_sparsity(f, x, sd)
@test H2 isa SparseMatrixCSC{Bool,Int}
@test H2 [
0 0 0 0 0
0 0 1 0 0
Expand Down

0 comments on commit 57e3e73

Please sign in to comment.