|
18 | 18 | end
|
19 | 19 |
|
20 | 20 | @testset "Sparsity detector" begin
|
21 |
| - sd = NoSparsityDetector() |
22 |
| - |
23 | 21 | f_jac1(x) = vcat(x, x)
|
24 | 22 | f_jac2(x) = hcat(x, x)
|
25 | 23 | function f_jac! end
|
26 | 24 | f_hess(x) = sum(x)
|
27 | 25 |
|
28 |
| - for x in (rand(2), rand(2, 3)), f in (f_jac1, f_jac2) |
29 |
| - y = f(x) |
30 |
| - Js = jacobian_sparsity(f, x, sd) |
31 |
| - @test Js isa AbstractMatrix |
32 |
| - @test size(Js) == (length(y), length(x)) |
33 |
| - @test all(isone, Js) |
| 26 | + @testset "NoSparsityDetector" begin |
| 27 | + sd = NoSparsityDetector() |
| 28 | + |
| 29 | + for x in (rand(2), rand(2, 3)), f in (f_jac1, f_jac2) |
| 30 | + y = f(x) |
| 31 | + Js = jacobian_sparsity(f, x, sd) |
| 32 | + @test Js isa AbstractMatrix |
| 33 | + @test size(Js) == (length(y), length(x)) |
| 34 | + @test all(isone, Js) |
| 35 | + end |
| 36 | + |
| 37 | + for x in (rand(2), rand(2, 3)), y in (rand(5), rand(5, 6)) |
| 38 | + Js = jacobian_sparsity(f_jac!, y, x, sd) |
| 39 | + @test Js isa AbstractMatrix |
| 40 | + @test size(Js) == (length(y), length(x)) |
| 41 | + @test all(isone, Js) |
| 42 | + end |
| 43 | + |
| 44 | + for x in (rand(2), rand(2, 3)) |
| 45 | + Hs = hessian_sparsity(f_hess, x, sd) |
| 46 | + @test Hs isa AbstractMatrix |
| 47 | + @test size(Hs) == (length(x), length(x)) |
| 48 | + @test all(isone, Hs) |
| 49 | + end |
34 | 50 | end
|
35 | 51 |
|
36 |
| - for x in (rand(2), rand(2, 3)), y in (rand(5), rand(5, 6)) |
37 |
| - Js = jacobian_sparsity(f_jac!, y, x, sd) |
38 |
| - @test Js isa AbstractMatrix |
39 |
| - @test size(Js) == (length(y), length(x)) |
40 |
| - @test all(isone, Js) |
| 52 | + @testset "KnownJacobianSparsityDetector" begin |
| 53 | + @testset "Jacobian sparsity detection" begin |
| 54 | + @testset "Out-of-place functions" begin |
| 55 | + for sx in ((2,), (2, 3)), f in (f_jac1, f_jac2) |
| 56 | + x = rand(sx...) |
| 57 | + nx = length(x) |
| 58 | + Jref = rand(Bool, 2 * nx, nx) |
| 59 | + sd = KnownJacobianSparsityDetector(Jref) |
| 60 | + Js = jacobian_sparsity(f, x, sd) |
| 61 | + @test Js isa AbstractMatrix |
| 62 | + @test size(Js) == (2 * nx, nx) |
| 63 | + @test Js === Jref |
| 64 | + end |
| 65 | + end |
| 66 | + @testset "In-place functions" begin |
| 67 | + for sx in ((2,), (2, 3)), sy in ((5,), (5, 6)) |
| 68 | + x, y = rand(sx...), rand(sy...) |
| 69 | + nx, ny = length(x), length(y) |
| 70 | + Jref = rand(Bool, ny, nx) |
| 71 | + sd = KnownJacobianSparsityDetector(Jref) |
| 72 | + Js = jacobian_sparsity(f_jac!, y, x, sd) |
| 73 | + @test Js isa AbstractMatrix |
| 74 | + @test size(Js) == (ny, nx) |
| 75 | + @test Js === Jref |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + @testset "Exceptions: Hessian sparsity detection not supported" begin |
| 80 | + for sx in ((2,), (2, 3)) |
| 81 | + x = rand(sx...) |
| 82 | + nx = length(x) |
| 83 | + Href = rand(Bool, nx, nx) |
| 84 | + sd = KnownJacobianSparsityDetector(Href) |
| 85 | + @test_throws ArgumentError hessian_sparsity(f_hess, x, sd) |
| 86 | + end |
| 87 | + end |
| 88 | + @testset "Exceptions: DimensionMismatch" begin |
| 89 | + sd = KnownJacobianSparsityDetector(rand(Bool, 6, 7)) # wrong Jacobian size |
| 90 | + for x in (rand(2), rand(2, 3)), f in (f_jac1, f_jac2) |
| 91 | + @test_throws DimensionMismatch jacobian_sparsity(f, x, sd) |
| 92 | + end |
| 93 | + for x in (rand(2), rand(2, 3)), y in (rand(5), rand(5, 6)) |
| 94 | + @test_throws DimensionMismatch jacobian_sparsity(f_jac!, y, x, sd) |
| 95 | + end |
| 96 | + end |
41 | 97 | end
|
42 | 98 |
|
43 |
| - for x in (rand(2), rand(2, 3)) |
44 |
| - Hs = hessian_sparsity(f_hess, x, sd) |
45 |
| - @test Hs isa AbstractMatrix |
46 |
| - @test size(Hs) == (length(x), length(x)) |
47 |
| - @test all(isone, Hs) |
| 99 | + @testset "KnownHessianSparsityDetector" begin |
| 100 | + @testset "Hessian sparsity detection" begin |
| 101 | + for sx in ((2,), (2, 3)) |
| 102 | + x = rand(sx...) |
| 103 | + nx = length(x) |
| 104 | + Href = rand(Bool, nx, nx) |
| 105 | + sd = KnownHessianSparsityDetector(Href) |
| 106 | + |
| 107 | + Hs = hessian_sparsity(f_hess, x, sd) |
| 108 | + @test Hs isa AbstractMatrix |
| 109 | + @test size(Hs) == (nx, nx) |
| 110 | + @test Hs === Href |
| 111 | + end |
| 112 | + end |
| 113 | + @testset "Exceptions: Jacobian sparsity detection not supported" begin |
| 114 | + @testset "Out-of-place functions" begin |
| 115 | + for sx in ((2,), (2, 3)), f in (f_jac1, f_jac2) |
| 116 | + x = rand(sx...) |
| 117 | + nx = length(x) |
| 118 | + Jref = rand(Bool, 2 * nx, nx) |
| 119 | + sd = KnownHessianSparsityDetector(Jref) |
| 120 | + @test_throws ArgumentError jacobian_sparsity(f, x, sd) |
| 121 | + end |
| 122 | + end |
| 123 | + @testset "In-place functions" begin |
| 124 | + for sx in ((2,), (2, 3)), sy in ((5,), (5, 6)) |
| 125 | + x, y = rand(sx...), rand(sy...) |
| 126 | + nx, ny = length(x), length(y) |
| 127 | + Jref = rand(Bool, ny, nx) |
| 128 | + sd = KnownHessianSparsityDetector(Jref) |
| 129 | + @test_throws ArgumentError jacobian_sparsity(f_jac!, y, x, sd) |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + @testset "Exceptions: DimensionMismatch" begin |
| 134 | + sd = KnownHessianSparsityDetector(rand(Bool, 2, 3)) #wrong Hessian size |
| 135 | + for x in (rand(2), rand(2, 3)) |
| 136 | + @test_throws DimensionMismatch hessian_sparsity(f_hess, x, sd) |
| 137 | + end |
| 138 | + end |
48 | 139 | end
|
49 | 140 | end
|
50 | 141 |
|
|
0 commit comments