|
| 1 | +function test_sparsity(ba::AbstractADType, ::typeof(jacobian), scen::Scenario{false};) |
| 2 | + (; f, x, y, ref) = new_scen = deepcopy(scen) |
| 3 | + extras = prepare_jacobian(f, ba, x) |
| 4 | + jac_true = if ref isa AbstractADType |
| 5 | + jacobian(f, ref, x) |
| 6 | + else |
| 7 | + ref.jacobian(x) |
| 8 | + end |
| 9 | + |
| 10 | + _, jac1 = value_and_jacobian(f, ba, x, extras) |
| 11 | + _, jac2 = value_and_jacobian!!(f, mysimilar(jac_true), ba, x, extras) |
| 12 | + |
| 13 | + jac3 = jacobian(f, ba, x, extras) |
| 14 | + jac4 = jacobian!!(f, mysimilar(jac_true), ba, x, extras) |
| 15 | + |
| 16 | + @testset "Sparse type" begin |
| 17 | + @test jac1 isa SparseMatrixCSC |
| 18 | + @test jac2 isa SparseMatrixCSC |
| 19 | + @test jac3 isa SparseMatrixCSC |
| 20 | + @test jac4 isa SparseMatrixCSC |
| 21 | + end |
| 22 | + @testset "Sparsity pattern" begin |
| 23 | + @test nnz(jac1) < length(jac_true) |
| 24 | + @test nnz(jac2) < length(jac_true) |
| 25 | + @test nnz(jac3) < length(jac_true) |
| 26 | + @test nnz(jac4) < length(jac_true) |
| 27 | + end |
| 28 | + return nothing |
| 29 | +end |
| 30 | + |
| 31 | +function test_sparsity(ba::AbstractADType, ::typeof(jacobian), scen::Scenario{true};) |
| 32 | + (; f, x, y, dy, ref) = new_scen = deepcopy(scen) |
| 33 | + f! = f |
| 34 | + extras = prepare_jacobian(f!, ba, y, x) |
| 35 | + jac_shape = Matrix{eltype(y)}(undef, length(y), length(x)) |
| 36 | + jac_true = if ref isa AbstractADType |
| 37 | + last(value_and_jacobian!!(f!, mysimilar(y), mysimilar(jac_shape), ref, x)) |
| 38 | + else |
| 39 | + ref.jacobian(x) |
| 40 | + end |
| 41 | + |
| 42 | + y10 = mysimilar(y) |
| 43 | + _, jac1 = value_and_jacobian!!(f!, y10, mysimilar(jac_true), ba, x, extras) |
| 44 | + |
| 45 | + @testset "Sparse type" begin |
| 46 | + @test jac1 isa SparseMatrixCSC |
| 47 | + end |
| 48 | + @testset "Sparsity pattern" begin |
| 49 | + @test nnz(jac1) < length(jac_true) |
| 50 | + end |
| 51 | + return nothing |
| 52 | +end |
0 commit comments