Skip to content

Commit

Permalink
🤖 Format .jl files (#313)
Browse files Browse the repository at this point in the history
Co-authored-by: amontoison <[email protected]>
  • Loading branch information
github-actions[bot] and amontoison authored Nov 27, 2024
1 parent 06d623f commit 1fe420b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 57 deletions.
86 changes: 47 additions & 39 deletions src/sparsity_pattern.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,57 +74,65 @@ Supported backends include `SparseADJacobian`, `SparseADHessian`, and `SparseRev
* `S`: A sparse matrix of type `SparseMatrixCSC{Bool,Int}` indicating the sparsity pattern of the requested derivative.
"""
function get_sparsity_pattern(model::ADModel, derivative::Symbol)
get_sparsity_pattern(model, Val(derivative))
get_sparsity_pattern(model, Val(derivative))
end

function get_sparsity_pattern(model::ADModel, ::Val{:jacobian})
backend = model.adbackend.jacobian_backend
validate_sparse_backend(backend, SparseADJacobian, "Jacobian")
m = model.meta.ncon
n = model.meta.nvar
colptr = backend.colptr
rowval = backend.rowval
nnzJ = length(rowval)
nzval = ones(Bool, nnzJ)
SparseMatrixCSC(m, n, colptr, rowval, nzval)
backend = model.adbackend.jacobian_backend
validate_sparse_backend(backend, SparseADJacobian, "Jacobian")
m = model.meta.ncon
n = model.meta.nvar
colptr = backend.colptr
rowval = backend.rowval
nnzJ = length(rowval)
nzval = ones(Bool, nnzJ)
SparseMatrixCSC(m, n, colptr, rowval, nzval)
end

function get_sparsity_pattern(model::ADModel, ::Val{:hessian})
backend = model.adbackend.hessian_backend
validate_sparse_backend(backend, Union{SparseADHessian, SparseReverseADHessian}, "Hessian")
n = model.meta.nvar
colptr = backend.colptr
rowval = backend.rowval
nnzH = length(rowval)
nzval = ones(Bool, nnzH)
SparseMatrixCSC(n, n, colptr, rowval, nzval)
backend = model.adbackend.hessian_backend
validate_sparse_backend(backend, Union{SparseADHessian, SparseReverseADHessian}, "Hessian")
n = model.meta.nvar
colptr = backend.colptr
rowval = backend.rowval
nnzH = length(rowval)
nzval = ones(Bool, nnzH)
SparseMatrixCSC(n, n, colptr, rowval, nzval)
end

function get_sparsity_pattern(model::AbstractADNLSModel, ::Val{:jacobian_residual})
backend = model.adbackend.jacobian_residual_backend
validate_sparse_backend(backend, SparseADJacobian, "Jacobian of the residual")
m = model.nls_meta.nequ
n = model.meta.nvar
colptr = backend.colptr
rowval = backend.rowval
nnzJ = length(rowval)
nzval = ones(Bool, nnzJ)
SparseMatrixCSC(m, n, colptr, rowval, nzval)
backend = model.adbackend.jacobian_residual_backend
validate_sparse_backend(backend, SparseADJacobian, "Jacobian of the residual")
m = model.nls_meta.nequ
n = model.meta.nvar
colptr = backend.colptr
rowval = backend.rowval
nnzJ = length(rowval)
nzval = ones(Bool, nnzJ)
SparseMatrixCSC(m, n, colptr, rowval, nzval)
end

function get_sparsity_pattern(model::AbstractADNLSModel, ::Val{:hessian_residual})
backend = model.adbackend.hessian_residual_backend
validate_sparse_backend(backend, Union{SparseADHessian, SparseReverseADHessian}, "Hessian of the residual")
n = model.meta.nvar
colptr = backend.colptr
rowval = backend.rowval
nnzH = length(rowval)
nzval = ones(Bool, nnzH)
SparseMatrixCSC(n, n, colptr, rowval, nzval)
backend = model.adbackend.hessian_residual_backend
validate_sparse_backend(
backend,
Union{SparseADHessian, SparseReverseADHessian},
"Hessian of the residual",
)
n = model.meta.nvar
colptr = backend.colptr
rowval = backend.rowval
nnzH = length(rowval)
nzval = ones(Bool, nnzH)
SparseMatrixCSC(n, n, colptr, rowval, nzval)
end

function validate_sparse_backend(backend::B, expected_type, derivative_name::String) where {B <: ADBackend}
if !(backend isa expected_type)
error("The current backend $B doesn't compute a sparse $derivative_name.")
end
function validate_sparse_backend(
backend::B,
expected_type,
derivative_name::String,
) where {B <: ADBackend}
if !(backend isa expected_type)
error("The current backend $B doesn't compute a sparse $derivative_name.")
end
end
8 changes: 4 additions & 4 deletions test/sparse_hessian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ dt = (Float32, Float64)

if (backend == ADNLPModels.SparseADHessian) || (backend == ADNLPModels.SparseReverseADHessian)
H_sp = get_sparsity_pattern(nlp, :hessian)
@test H_sp == SparseMatrixCSC{Bool, Int}(
[ 1 0 ;
1 1 ]
)
@test H_sp == SparseMatrixCSC{Bool, Int}([
1 0
1 1
])
end

nlp = ADNLPModel!(
Expand Down
8 changes: 4 additions & 4 deletions test/sparse_hessian_nls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ dt = (Float32, Float64)

if (backend == ADNLPModels.SparseADHessian) || (backend == ADNLPModels.SparseReverseADHessian)
H_sp = get_sparsity_pattern(nls, :hessian_residual)
@test H_sp == SparseMatrixCSC{Bool, Int}(
[ 1 0 ;
0 0 ]
)
@test H_sp == SparseMatrixCSC{Bool, Int}([
1 0
0 0
])
end

nls = ADNLPModels.ADNLSModel!(F!, x0, 3, matrix_free = true; kw...)
Expand Down
10 changes: 5 additions & 5 deletions test/sparse_jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ dt = (Float32, Float64)

if backend == ADNLPModels.SparseADJacobian
J_sp = get_sparsity_pattern(nlp, :jacobian)
@test J_sp == SparseMatrixCSC{Bool, Int}(
[ 1 0 ;
1 1 ;
0 1 ]
)
@test J_sp == SparseMatrixCSC{Bool, Int}([
1 0
1 1
0 1
])
end

nlp = ADNLPModel!(x -> sum(x), x0, c!, zeros(T, ncon), zeros(T, ncon), matrix_free = true; kw...)
Expand Down
10 changes: 5 additions & 5 deletions test/sparse_jacobian_nls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ dt = (Float32, Float64)

if backend == ADNLPModels.SparseADJacobian
J_sp = get_sparsity_pattern(nls, :jacobian_residual)
@test J_sp == SparseMatrixCSC{Bool, Int}(
[ 1 0 ;
1 1 ;
0 1 ]
)
@test J_sp == SparseMatrixCSC{Bool, Int}([
1 0
1 1
0 1
])
end

nls = ADNLPModels.ADNLSModel!(F!, x0, 3, matrix_free = true; kw...)
Expand Down

0 comments on commit 1fe420b

Please sign in to comment.