Skip to content

Commit 462f9c5

Browse files
import the BBM and BM stuff for tests
1 parent 2d3e1e5 commit 462f9c5

File tree

2 files changed

+66
-65
lines changed

2 files changed

+66
-65
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ julia = "1.6"
3434

3535
[extras]
3636
ArrayInterfaceBlockBandedMatrices = "5331f1e9-51c7-46b0-a9b0-df4434785e0a"
37+
ArrayInterfaceBandedMatrices = "2e50d22c-5be1-4042-81b1-c572ed69783d"
3738
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
3839
BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
3940
IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153"
@@ -46,4 +47,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4647
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
4748

4849
[targets]
49-
test = ["Test", "ArrayInterfaceBlockBandedMatrices", "BandedMatrices", "BlockBandedMatrices", "IterativeSolvers", "Pkg", "Random", "SafeTestsets", "Zygote", "SparsityDetection", "StaticArrays"]
50+
test = ["Test", "ArrayInterfaceBandedMatrices", "ArrayInterfaceBlockBandedMatrices", "BandedMatrices", "BlockBandedMatrices", "IterativeSolvers", "Pkg", "Random", "SafeTestsets", "Zygote", "SparsityDetection", "StaticArrays"]

test/test_ad.jl

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ using SparseDiffTools
22
using ForwardDiff: Dual, jacobian
33
using SparseArrays, Test
44
using LinearAlgebra
5-
using BlockBandedMatrices
6-
using BandedMatrices
5+
using BlockBandedMatrices, ArrayInterfaceBlockBandedMatrices
6+
using BandedMatrices, ArrayInterfaceBandedMatrices
77
using StaticArrays
88
using ArrayInterfaceStaticArrays
99

1010
fcalls = 0
11-
function f(dx,x)
11+
function f(dx, x)
1212
global fcalls += 1
1313
for i in 2:length(x)-1
1414
dx[i] = x[i-1] - 2x[i] + x[i+1]
@@ -30,8 +30,8 @@ function oopf(x)
3030
end
3131

3232
function nsqf(x)#length(dx)<length(x)
33-
global fcalls +=1
34-
dx = zero(x)[1:div(length(x),2)]
33+
global fcalls += 1
34+
dx = zero(x)[1:div(length(x), 2)]
3535
for i in 2:length(dx)
3636
dx[i] = x[i-1] - 2x[i] + x[i+1]
3737
end
@@ -40,48 +40,48 @@ function nsqf(x)#length(dx)<length(x)
4040
end
4141

4242
function nsqf2(x)#length(dx)>length(x)
43-
global fcalls +=1
44-
dx = zeros(eltype(x),length(x)*2)
43+
global fcalls += 1
44+
dx = zeros(eltype(x), length(x) * 2)
4545
for i in 2:length(x)-1
4646
dx[i] = x[i-1] - 2x[i] + x[i+1]
4747
end
4848
dx[1] = -2x[1] + x[2]
4949
dx
5050
end
5151

52-
function nsqf!(dx,x)
53-
global fcalls +=1
52+
function nsqf!(dx, x)
53+
global fcalls += 1
5454
for i in 2:length(dx)
5555
dx[i] = x[i-1] - 2x[i] + x[i+1]
5656
end
5757
dx[1] = -2x[1] + x[2]
5858
nothing
5959
end
6060

61-
function nsqf2!(dx,x)
62-
global fcalls +=1
61+
function nsqf2!(dx, x)
62+
global fcalls += 1
6363
for i in 2:length(x)-1
6464
dx[i] = x[i-1] - 2x[i] + x[i+1]
6565
end
6666
dx[1] = -2x[1] + x[2]
6767
nothing
6868
end
6969

70-
function staticf(x,N=length(x))
70+
function staticf(x, N=length(x))
7171
global fcalls += 1
72-
SVector{N}([i == 1 ? -2x[1]+x[2] : (i == N ? x[N-1]-2x[N] : x[i-1]-2x[i]+x[i+1]) for i in 1:N])
72+
SVector{N}([i == 1 ? -2x[1] + x[2] : (i == N ? x[N-1] - 2x[N] : x[i-1] - 2x[i] + x[i+1]) for i in 1:N])
7373
end
7474

75-
function staticnsqf(x,N=div(length(x),2))
75+
function staticnsqf(x, N=div(length(x), 2))
7676
global fcalls += 1
77-
SVector{N}(vcat([-2x[1]+x[2]],[x[i-1]-2x[i]+x[i+1] for i in 2:N]))
77+
SVector{N}(vcat([-2x[1] + x[2]], [x[i-1] - 2x[i] + x[i+1] for i in 2:N]))
7878
end
7979

8080
function second_derivative_stencil(N)
81-
A = zeros(N,N)
81+
A = zeros(N, N)
8282
for i in 1:N, j in 1:N
83-
(j-i==-1 || j-i==1) && (A[i,j]=1)
84-
j-i==0 && (A[i,j]=-2)
83+
(j - i == -1 || j - i == 1) && (A[i, j] = 1)
84+
j - i == 0 && (A[i, j] = -2)
8585
end
8686
A
8787
end
@@ -98,140 +98,140 @@ _J = sparse(J)
9898

9999
fcalls = 0
100100
_J1 = similar(_J)
101-
forwarddiff_color_jacobian!(_J1, f, x, colorvec = repeat(1:3,10))
101+
forwarddiff_color_jacobian!(_J1, f, x, colorvec=repeat(1:3, 10))
102102
@test _J1 J
103103
@test fcalls == 1
104104

105105
@info "second passed"
106106

107107
fcalls = 0
108-
_J1 = forwarddiff_color_jacobian(oopf, x, colorvec = repeat(1:3,10), sparsity = _J, jac_prototype = _J)
108+
_J1 = forwarddiff_color_jacobian(oopf, x, colorvec=repeat(1:3, 10), sparsity=_J, jac_prototype=_J)
109109
@test _J1 J
110110
@test typeof(_J1) == typeof(_J)
111111
@test fcalls == 1
112112

113113
@info "third passed"
114114

115115
fcalls = 0
116-
_J1 = forwarddiff_color_jacobian(oopf, x, colorvec = repeat(1:3,10), sparsity = _J)
116+
_J1 = forwarddiff_color_jacobian(oopf, x, colorvec=repeat(1:3, 10), sparsity=_J)
117117
@test _J1 J
118118
@test fcalls == 1
119119

120120
#oop with in-place Jacobian
121121
fcalls = 0
122122
_oop_jacout = sparse(1.01 .* J) # want to be nonzero to check that the pre-allocated matrix is overwritten properly
123-
forwarddiff_color_jacobian(_oop_jacout, oopf, x; colorvec = repeat(1:3,10), sparsity = _J, jac_prototype = _J)
123+
forwarddiff_color_jacobian(_oop_jacout, oopf, x; colorvec=repeat(1:3, 10), sparsity=_J, jac_prototype=_J)
124124
@test _oop_jacout J
125125
@test typeof(_oop_jacout) == typeof(_J)
126126
@test fcalls == 1
127127

128128
# BandedMatrix
129129
_oop_jacout = BandedMatrix(-1 => diag(J, -1) .* 1.01, 0 => diag(J, 0) .* 1.01,
130-
1 => diag(J, 1) .* 1.01) # check w/BandedMatrix instead of sparse
130+
1 => diag(J, 1) .* 1.01) # check w/BandedMatrix instead of sparse
131131
fcalls = 0
132-
forwarddiff_color_jacobian(_oop_jacout, oopf, x; colorvec = repeat(1:3,10), sparsity = _J)
132+
forwarddiff_color_jacobian(_oop_jacout, oopf, x; colorvec=repeat(1:3, 10), sparsity=_J)
133133
@test _oop_jacout J
134134
@test isa(_oop_jacout, BandedMatrix)
135135
@test fcalls == 1
136136

137137
@info "4th passed"
138138

139139
fcalls = 0
140-
_J1 = forwarddiff_color_jacobian(staticf, SVector{30}(x), colorvec = repeat(1:3,10), sparsity = _J, jac_prototype = SMatrix{30,30}(_J))
140+
_J1 = forwarddiff_color_jacobian(staticf, SVector{30}(x), colorvec=repeat(1:3, 10), sparsity=_J, jac_prototype=SMatrix{30,30}(_J))
141141
@test _J1 J
142142
@test fcalls == 1
143143

144144
@info "5"
145145

146-
_J1 = forwarddiff_color_jacobian(staticf, SVector{30}(x), jac_prototype = SMatrix{30,30}(_J))
146+
_J1 = forwarddiff_color_jacobian(staticf, SVector{30}(x), jac_prototype=SMatrix{30,30}(_J))
147147
@test _J1 J
148-
_J1 = forwarddiff_color_jacobian(oopf, x, jac_prototype = similar(_J))
148+
_J1 = forwarddiff_color_jacobian(oopf, x, jac_prototype=similar(_J))
149149
@test _J1 J
150150
_J1 = forwarddiff_color_jacobian(oopf, x)
151151
@test _J1 J
152152

153153
#Non-square Jacobian
154154
#length(dx)<length(x)
155-
nsqJ = jacobian(nsqf,x)
155+
nsqJ = jacobian(nsqf, x)
156156
spnsqJ = sparse(nsqJ)
157-
_nsqJ = forwarddiff_color_jacobian(nsqf, x, dx = nothing)
157+
_nsqJ = forwarddiff_color_jacobian(nsqf, x, dx=nothing)
158158
@test _nsqJ nsqJ
159-
_nsqJ = forwarddiff_color_jacobian(nsqf, x, colorvec = repeat(1:3,10), sparsity = spnsqJ)
159+
_nsqJ = forwarddiff_color_jacobian(nsqf, x, colorvec=repeat(1:3, 10), sparsity=spnsqJ)
160160
@test _nsqJ nsqJ
161-
_nsqJ = forwarddiff_color_jacobian(nsqf, x, jac_prototype = similar(nsqJ))
161+
_nsqJ = forwarddiff_color_jacobian(nsqf, x, jac_prototype=similar(nsqJ))
162162
@test _nsqJ nsqJ
163-
_nsqJ = forwarddiff_color_jacobian(nsqf, x, colorvec = repeat(1:3,10), sparsity = spnsqJ, jac_prototype = similar(nsqJ))
163+
_nsqJ = forwarddiff_color_jacobian(nsqf, x, colorvec=repeat(1:3, 10), sparsity=spnsqJ, jac_prototype=similar(nsqJ))
164164
@test _nsqJ nsqJ
165-
_nsqJ = forwarddiff_color_jacobian(nsqf, x, jac_prototype = SMatrix{15,30}(nsqJ))
165+
_nsqJ = forwarddiff_color_jacobian(nsqf, x, jac_prototype=SMatrix{15,30}(nsqJ))
166166
@test _nsqJ nsqJ
167167
@test typeof(_nsqJ) == typeof(SMatrix{15,30}(nsqJ))
168-
_nsqJ = forwarddiff_color_jacobian(staticnsqf, SVector{30}(x), jac_prototype = SMatrix{15,30}(nsqJ))
168+
_nsqJ = forwarddiff_color_jacobian(staticnsqf, SVector{30}(x), jac_prototype=SMatrix{15,30}(nsqJ))
169169
@test _nsqJ nsqJ
170-
_nsqJ = forwarddiff_color_jacobian(staticnsqf, SVector{30}(x), jac_prototype = SMatrix{15,30}(nsqJ), colorvec = repeat(1:3,10), sparsity = spnsqJ)
170+
_nsqJ = forwarddiff_color_jacobian(staticnsqf, SVector{30}(x), jac_prototype=SMatrix{15,30}(nsqJ), colorvec=repeat(1:3, 10), sparsity=spnsqJ)
171171
@test _nsqJ nsqJ
172172
_nsqJ = similar(nsqJ)
173173
forwarddiff_color_jacobian!(_nsqJ, nsqf!, x)
174174
@test _nsqJ nsqJ
175175
_nsqJ = similar(nsqJ)
176-
forwarddiff_color_jacobian!(_nsqJ, nsqf!, x, colorvec = repeat(1:3,10), sparsity = spnsqJ )
176+
forwarddiff_color_jacobian!(_nsqJ, nsqf!, x, colorvec=repeat(1:3, 10), sparsity=spnsqJ)
177177
@test _nsqJ nsqJ
178178

179179
#length(dx)>length(x)
180-
nsqJ = jacobian(nsqf2,x)
180+
nsqJ = jacobian(nsqf2, x)
181181
spnsqJ = sparse(nsqJ)
182-
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, dx = nothing)
182+
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, dx=nothing)
183183
@test _nsqJ nsqJ
184-
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, colorvec = repeat(1:3,10), sparsity = spnsqJ)
184+
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, colorvec=repeat(1:3, 10), sparsity=spnsqJ)
185185
@test _nsqJ nsqJ
186-
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, jac_prototype = similar(nsqJ))
186+
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, jac_prototype=similar(nsqJ))
187187
@test _nsqJ nsqJ
188-
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, colorvec = repeat(1:3,10), sparsity = spnsqJ, jac_prototype = similar(nsqJ))
188+
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, colorvec=repeat(1:3, 10), sparsity=spnsqJ, jac_prototype=similar(nsqJ))
189189
@test _nsqJ nsqJ
190-
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, jac_prototype = SMatrix{60,30}(nsqJ))
190+
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, jac_prototype=SMatrix{60,30}(nsqJ))
191191
@test _nsqJ nsqJ
192192
_nsqJ = similar(nsqJ)
193193
forwarddiff_color_jacobian!(_nsqJ, nsqf2!, x)
194194
@test _nsqJ nsqJ
195195
_nsqJ = similar(nsqJ)
196-
forwarddiff_color_jacobian!(_nsqJ, nsqf2!, x, colorvec = repeat(1:3,10), sparsity = spnsqJ )
196+
forwarddiff_color_jacobian!(_nsqJ, nsqf2!, x, colorvec=repeat(1:3, 10), sparsity=spnsqJ)
197197
@test _nsqJ nsqJ
198198

199199
fcalls = 0
200200
_J1 = similar(_J)
201-
jac_cache = ForwardColorJacCache(f,x,colorvec = repeat(1:3,10), sparsity = _J1)
201+
jac_cache = ForwardColorJacCache(f, x, colorvec=repeat(1:3, 10), sparsity=_J1)
202202
forwarddiff_color_jacobian!(_J1, f, x, jac_cache)
203203
@test _J1 J
204204
@test fcalls == 1
205205

206206
fcalls = 0
207207
_J1 = similar(_J)
208208
_denseJ1 = collect(_J1)
209-
forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec = repeat(1:3,10), sparsity = _J1)
209+
forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec=repeat(1:3, 10), sparsity=_J1)
210210
@test _denseJ1 J
211211
@test fcalls == 1
212212

213213
fcalls = 0
214214
_J1 = similar(_J)
215215
_denseJ1 = collect(_J1)
216-
jac_cache = ForwardColorJacCache(f,x,colorvec = repeat(1:3,10), sparsity = _J1)
216+
jac_cache = ForwardColorJacCache(f, x, colorvec=repeat(1:3, 10), sparsity=_J1)
217217
forwarddiff_color_jacobian!(_denseJ1, f, x, jac_cache)
218218
@test _denseJ1 J
219219
@test fcalls == 1
220220

221221
_Jt = similar(Tridiagonal(J))
222-
forwarddiff_color_jacobian!(_Jt, f, x, colorvec = repeat(1:3,10), sparsity = _Jt)
222+
forwarddiff_color_jacobian!(_Jt, f, x, colorvec=repeat(1:3, 10), sparsity=_Jt)
223223
@test _Jt J
224224

225225
#https://github.com/JuliaDiff/FiniteDiff.jl/issues/67#issuecomment-516871956
226226
function f(out, x)
227-
x = reshape(x, 100, 100)
228-
out = reshape(out, 100, 100)
229-
for i in 1:100
230-
for j in 1:100
231-
out[i, j] = x[i, j] + x[max(i -1, 1), j] + x[min(i+1, size(x, 1)), j] + x[i, max(j-1, 1)] + x[i, min(j+1, size(x, 2))]
232-
end
233-
end
234-
return vec(out)
227+
x = reshape(x, 100, 100)
228+
out = reshape(out, 100, 100)
229+
for i in 1:100
230+
for j in 1:100
231+
out[i, j] = x[i, j] + x[max(i - 1, 1), j] + x[min(i + 1, size(x, 1)), j] + x[i, max(j - 1, 1)] + x[i, min(j + 1, size(x, 2))]
232+
end
233+
end
234+
return vec(out)
235235
end
236236
x = rand(10000)
237237
J = BandedBlockBandedMatrix(Ones(10000, 10000), fill(100, 100), fill(100, 100), (1, 1), (1, 1))
@@ -242,18 +242,18 @@ forwarddiff_color_jacobian!(Jsparse, f, x, colorvec=colors)
242242
@test J Jsparse
243243

244244
# Non vector input
245-
x = rand(2,2)
245+
x = rand(2, 2)
246246
oopf(x) = x
247-
iipf(fx,x) = (fx.=x)
248-
J = forwarddiff_color_jacobian(oopf,x)
249-
@test J Matrix(I,4,4)
247+
iipf(fx, x) = (fx .= x)
248+
J = forwarddiff_color_jacobian(oopf, x)
249+
@test J Matrix(I, 4, 4)
250250
J = zero(J)
251-
forwarddiff_color_jacobian!(J,iipf,x,dx=similar(x))
252-
@test J Matrix(I,4,4)
251+
forwarddiff_color_jacobian!(J, iipf, x, dx=similar(x))
252+
@test J Matrix(I, 4, 4)
253253

254254
#1x1 SVector test
255-
x = SVector{1}([1.])
255+
x = SVector{1}([1.0])
256256
f(x) = x
257-
J = forwarddiff_color_jacobian(f,x)
257+
J = forwarddiff_color_jacobian(f, x)
258258
@test J isa SArray
259-
@test J SMatrix{1,1}([1.])
259+
@test J SMatrix{1,1}([1.0])

0 commit comments

Comments
 (0)