/
view-subarray.jl
102 lines (92 loc) · 3.04 KB
/
view-subarray.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
export view_subarray_nls
"""
view_subarray_nls(nls; exclude = [])
Check that the API work with views, and that the results is correct.
"""
function view_subarray_nls(nls; exclude = [])
@testset "Test view subarray of NLSs" begin
n, ne = nls.meta.nvar, nls.nls_meta.nequ
N = 2n
Vidxs = [1:n, n .+ (1:n), 1:2:N, collect(N:-2:1)]
N = 2ne
Fidxs = [1:ne, ne .+ (1:ne), 1:2:N, collect(N:-2:1)]
# Inputs
x = [-(-1.1)^i for i = 1:(2n)] # Instead of [1, -1, …], because it needs to
v = [-(-1.1)^i for i = 1:(2n)] # access different parts of the vector and
y = [-(-1.1)^i for i = 1:(2ne)] # make a difference
# Outputs
F = zeros(ne)
F2 = zeros(2ne)
jv = zeros(ne)
jv2 = zeros(2ne)
jty = zeros(n)
jty2 = zeros(2n)
hv = zeros(n)
hv2 = zeros(2n)
for I in Vidxs
xv = @view x[I]
for foo in setdiff([residual, jac_residual], exclude)
@test foo(nls, x[I]) ≈ foo(nls, xv)
end
# Inplace methods can have input and output as view, so 4 possibilities
if residual ∉ exclude
for J in Fidxs
Fv = @view F2[J]
residual!(nls, x[I], F)
residual!(nls, x[I], Fv)
@test F ≈ F2[J]
residual!(nls, xv, Fv)
@test F ≈ F2[J]
residual!(nls, xv, F)
@test F ≈ F2[J]
end
end
if jprod_residual ∉ exclude
for J in Fidxs, K in Vidxs
vv = @view v[K]
jvv = @view jv2[J]
@test jprod_residual(nls, x[I], v[K]) ≈ jprod_residual(nls, xv, vv)
jprod_residual!(nls, x[I], v[K], jv)
jprod_residual!(nls, x[I], v[K], jvv)
@test jv ≈ jv2[J]
jprod_residual!(nls, xv, vv, jvv)
@test jv ≈ jv2[J]
jprod_residual!(nls, xv, vv, jv)
@test jv ≈ jv2[J]
end
end
if jtprod_residual ∉ exclude
for J in Fidxs, K in Vidxs
yv = @view y[J]
jtyv = @view jty2[K]
@test jtprod_residual(nls, x[I], y[J]) ≈ jtprod_residual(nls, xv, yv)
jtprod_residual!(nls, x[I], y[J], jty)
jtprod_residual!(nls, x[I], y[J], jtyv)
@test jty ≈ jty2[K]
jtprod_residual!(nls, xv, yv, jtyv)
@test jty ≈ jty2[K]
jtprod_residual!(nls, xv, yv, jty)
@test jty ≈ jty2[K]
end
end
for i = 1:ne
@test jth_hess_residual ∈ exclude ||
jth_hess_residual(nls, x[I], i) ≈ jth_hess_residual(nls, xv, i)
if hprod_residual ∉ exclude
for J in Vidxs, K in Vidxs
vv = @view v[J]
hvv = @view hv2[K]
@test hprod_residual(nls, x[I], i, v[J]) ≈ hprod_residual(nls, xv, i, vv)
hprod_residual!(nls, x[I], i, v[J], hv)
hprod_residual!(nls, x[I], i, v[J], hvv)
@test hv ≈ hv2[K]
hprod_residual!(nls, xv, i, vv, hvv)
@test hv ≈ hv2[K]
hprod_residual!(nls, xv, i, vv, hv)
@test hv ≈ hv2[K]
end
end
end
end
end
end