@@ -2,13 +2,13 @@ using SparseDiffTools
2
2
using ForwardDiff: Dual, jacobian
3
3
using SparseArrays, Test
4
4
using LinearAlgebra
5
- using BlockBandedMatrices
6
- using BandedMatrices
5
+ using BlockBandedMatrices, ArrayInterfaceBlockBandedMatrices
6
+ using BandedMatrices, ArrayInterfaceBandedMatrices
7
7
using StaticArrays
8
8
using ArrayInterfaceStaticArrays
9
9
10
10
fcalls = 0
11
- function f (dx,x)
11
+ function f (dx, x)
12
12
global fcalls += 1
13
13
for i in 2 : length (x)- 1
14
14
dx[i] = x[i- 1 ] - 2 x[i] + x[i+ 1 ]
@@ -30,8 +30,8 @@ function oopf(x)
30
30
end
31
31
32
32
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 )]
35
35
for i in 2 : length (dx)
36
36
dx[i] = x[i- 1 ] - 2 x[i] + x[i+ 1 ]
37
37
end
@@ -40,48 +40,48 @@ function nsqf(x)#length(dx)<length(x)
40
40
end
41
41
42
42
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 )
45
45
for i in 2 : length (x)- 1
46
46
dx[i] = x[i- 1 ] - 2 x[i] + x[i+ 1 ]
47
47
end
48
48
dx[1 ] = - 2 x[1 ] + x[2 ]
49
49
dx
50
50
end
51
51
52
- function nsqf! (dx,x)
53
- global fcalls += 1
52
+ function nsqf! (dx, x)
53
+ global fcalls += 1
54
54
for i in 2 : length (dx)
55
55
dx[i] = x[i- 1 ] - 2 x[i] + x[i+ 1 ]
56
56
end
57
57
dx[1 ] = - 2 x[1 ] + x[2 ]
58
58
nothing
59
59
end
60
60
61
- function nsqf2! (dx,x)
62
- global fcalls += 1
61
+ function nsqf2! (dx, x)
62
+ global fcalls += 1
63
63
for i in 2 : length (x)- 1
64
64
dx[i] = x[i- 1 ] - 2 x[i] + x[i+ 1 ]
65
65
end
66
66
dx[1 ] = - 2 x[1 ] + x[2 ]
67
67
nothing
68
68
end
69
69
70
- function staticf (x,N= length (x))
70
+ function staticf (x, N= length (x))
71
71
global fcalls += 1
72
- SVector {N} ([i == 1 ? - 2 x[1 ]+ x[2 ] : (i == N ? x[N- 1 ]- 2 x[N] : x[i- 1 ]- 2 x[i]+ x[i+ 1 ]) for i in 1 : N])
72
+ SVector {N} ([i == 1 ? - 2 x[1 ] + x[2 ] : (i == N ? x[N- 1 ] - 2 x[N] : x[i- 1 ] - 2 x[i] + x[i+ 1 ]) for i in 1 : N])
73
73
end
74
74
75
- function staticnsqf (x,N= div (length (x),2 ))
75
+ function staticnsqf (x, N= div (length (x), 2 ))
76
76
global fcalls += 1
77
- SVector {N} (vcat ([- 2 x[1 ]+ x[2 ]],[x[i- 1 ]- 2 x[i]+ x[i+ 1 ] for i in 2 : N]))
77
+ SVector {N} (vcat ([- 2 x[1 ] + x[2 ]], [x[i- 1 ] - 2 x[i] + x[i+ 1 ] for i in 2 : N]))
78
78
end
79
79
80
80
function second_derivative_stencil (N)
81
- A = zeros (N,N)
81
+ A = zeros (N, N)
82
82
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 )
85
85
end
86
86
A
87
87
end
@@ -98,140 +98,140 @@ _J = sparse(J)
98
98
99
99
fcalls = 0
100
100
_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 ))
102
102
@test _J1 ≈ J
103
103
@test fcalls == 1
104
104
105
105
@info " second passed"
106
106
107
107
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)
109
109
@test _J1 ≈ J
110
110
@test typeof (_J1) == typeof (_J)
111
111
@test fcalls == 1
112
112
113
113
@info " third passed"
114
114
115
115
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)
117
117
@test _J1 ≈ J
118
118
@test fcalls == 1
119
119
120
120
# oop with in-place Jacobian
121
121
fcalls = 0
122
122
_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)
124
124
@test _oop_jacout ≈ J
125
125
@test typeof (_oop_jacout) == typeof (_J)
126
126
@test fcalls == 1
127
127
128
128
# BandedMatrix
129
129
_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
131
131
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)
133
133
@test _oop_jacout ≈ J
134
134
@test isa (_oop_jacout, BandedMatrix)
135
135
@test fcalls == 1
136
136
137
137
@info " 4th passed"
138
138
139
139
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))
141
141
@test _J1 ≈ J
142
142
@test fcalls == 1
143
143
144
144
@info " 5"
145
145
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))
147
147
@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))
149
149
@test _J1 ≈ J
150
150
_J1 = forwarddiff_color_jacobian (oopf, x)
151
151
@test _J1 ≈ J
152
152
153
153
# Non-square Jacobian
154
154
# length(dx)<length(x)
155
- nsqJ = jacobian (nsqf,x)
155
+ nsqJ = jacobian (nsqf, x)
156
156
spnsqJ = sparse (nsqJ)
157
- _nsqJ = forwarddiff_color_jacobian (nsqf, x, dx = nothing )
157
+ _nsqJ = forwarddiff_color_jacobian (nsqf, x, dx= nothing )
158
158
@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)
160
160
@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))
162
162
@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))
164
164
@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))
166
166
@test _nsqJ ≈ nsqJ
167
167
@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))
169
169
@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)
171
171
@test _nsqJ ≈ nsqJ
172
172
_nsqJ = similar (nsqJ)
173
173
forwarddiff_color_jacobian! (_nsqJ, nsqf!, x)
174
174
@test _nsqJ ≈ nsqJ
175
175
_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)
177
177
@test _nsqJ ≈ nsqJ
178
178
179
179
# length(dx)>length(x)
180
- nsqJ = jacobian (nsqf2,x)
180
+ nsqJ = jacobian (nsqf2, x)
181
181
spnsqJ = sparse (nsqJ)
182
- _nsqJ = forwarddiff_color_jacobian (nsqf2, x, dx = nothing )
182
+ _nsqJ = forwarddiff_color_jacobian (nsqf2, x, dx= nothing )
183
183
@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)
185
185
@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))
187
187
@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))
189
189
@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))
191
191
@test _nsqJ ≈ nsqJ
192
192
_nsqJ = similar (nsqJ)
193
193
forwarddiff_color_jacobian! (_nsqJ, nsqf2!, x)
194
194
@test _nsqJ ≈ nsqJ
195
195
_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)
197
197
@test _nsqJ ≈ nsqJ
198
198
199
199
fcalls = 0
200
200
_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)
202
202
forwarddiff_color_jacobian! (_J1, f, x, jac_cache)
203
203
@test _J1 ≈ J
204
204
@test fcalls == 1
205
205
206
206
fcalls = 0
207
207
_J1 = similar (_J)
208
208
_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)
210
210
@test _denseJ1 ≈ J
211
211
@test fcalls == 1
212
212
213
213
fcalls = 0
214
214
_J1 = similar (_J)
215
215
_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)
217
217
forwarddiff_color_jacobian! (_denseJ1, f, x, jac_cache)
218
218
@test _denseJ1 ≈ J
219
219
@test fcalls == 1
220
220
221
221
_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)
223
223
@test _Jt ≈ J
224
224
225
225
# https://github.com/JuliaDiff/FiniteDiff.jl/issues/67#issuecomment-516871956
226
226
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)
235
235
end
236
236
x = rand (10000 )
237
237
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)
242
242
@test J ≈ Jsparse
243
243
244
244
# Non vector input
245
- x = rand (2 ,2 )
245
+ x = rand (2 , 2 )
246
246
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 )
250
250
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 )
253
253
254
254
# 1x1 SVector test
255
- x = SVector {1} ([1. ])
255
+ x = SVector {1} ([1.0 ])
256
256
f (x) = x
257
- J = forwarddiff_color_jacobian (f,x)
257
+ J = forwarddiff_color_jacobian (f, x)
258
258
@test J isa SArray
259
- @test J ≈ SMatrix {1,1} ([1. ])
259
+ @test J ≈ SMatrix {1,1} ([1.0 ])
0 commit comments