@@ -10,21 +10,21 @@ using FiniteDiff, StaticArrays
1010
1111fcalls = 0
1212function f (dx,x) # in-place
13- global fcalls += 1
14- for i in 2 : length (x)- 1
15- dx[i] = x[i- 1 ] - 2 x[i] + x[i+ 1 ]
16- end
17- dx[1 ] = - 2 x[1 ] + x[2 ]
18- dx[end ] = x[end - 1 ] - 2 x[end ]
19- nothing
13+ global fcalls += 1
14+ for i in 2 : length (x)- 1
15+ dx[i] = x[i- 1 ] - 2 x[i] + x[i+ 1 ]
16+ end
17+ dx[1 ] = - 2 x[1 ] + x[2 ]
18+ dx[end ] = x[end - 1 ] - 2 x[end ]
19+ nothing
2020end
2121
2222const N = 10
2323handleleft (x,i) = i== 1 ? zero (eltype (x)) : x[i- 1 ]
2424handleright (x,i) = i== length (x) ? zero (eltype (x)) : x[i+ 1 ]
2525function g (x) # out-of-place
26- global fcalls += 1
27- @SVector [handleleft (x,i) - 2 x[i] + handleright (x,i) for i in 1 : N]
26+ global fcalls += 1
27+ @SVector [handleleft (x,i) - 2 x[i] + handleright (x,i) for i in 1 : N]
2828end
2929```
3030
@@ -37,7 +37,7 @@ x = @SVector rand(N)
3737FiniteDiff. finite_difference_jacobian (g,x)
3838
3939#=
40- 10×10 SArray{Tuple{ 10,10}, Float64,2, 100} with indices SOneTo(10)×SOneTo(10):
40+ 10×10 SMatrix{ 10, 10, Float64, 100} with indices SOneTo(10)×SOneTo(10):
4141 -2.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
4242 1.0 -2.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
4343 0.0 1.0 -2.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
@@ -65,7 +65,7 @@ FiniteDiff.finite_difference_jacobian!(output,f,x)
6565output
6666
6767#=
68- 10×10 Array {Float64,2 }:
68+ 10×10 Matrix {Float64}:
6969 -2.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
7070 1.0 -2.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
7171 0.0 1.0 -2.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
@@ -175,8 +175,8 @@ we get the analytical solution to the optimal matrix colors for our structured
175175Jacobian. Now we can use this in our differencing routines:
176176
177177``` julia
178- tridiagcache = FiniteDiff. JacobianCache (x,colorvec= colors,sparsity= tridiagjac)
179- FiniteDiff. finite_difference_jacobian! (tridiagjac,f,x, tridiagcache)
178+ tridiagcache = FiniteDiff. JacobianCache (x, colorvec= colors, sparsity= tridiagjac)
179+ FiniteDiff. finite_difference_jacobian! (tridiagjac, f, x, tridiagcache)
180180```
181181
182182It'll use a special iteration scheme dependent on the matrix type to accelerate
@@ -189,14 +189,16 @@ differential equations, with a function like:
189189
190190``` julia
191191function pde (out, x)
192- x = reshape (x, 100 , 100 )
193- out = reshape (out, 100 , 100 )
194- for i in 1 : 100
195- for j in 1 : 100
196- 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 ))]
197- end
198- end
199- return vec (out)
192+ x = reshape (x, 100 , 100 )
193+ out = reshape (out, 100 , 100 )
194+ m = size (x, 1 )
195+ n = size (x, 2 )
196+ for i in 1 : 100
197+ for j in 1 : 100
198+ out[i, j] = x[i, j] + x[max (i- 1 , 1 ), j] + x[min (i+ 1 , m), j] + x[i, max (j- 1 , 1 )] + x[i, min (j+ 1 , n)]
199+ end
200+ end
201+ return vec (out)
200202end
201203x = rand (10000 )
202204```
@@ -212,4 +214,4 @@ bbbcache = FiniteDiff.JacobianCache(x,colorvec=colorsbbb,sparsity=Jbbb)
212214FiniteDiff. finite_difference_jacobian! (Jbbb, pde, x, bbbcache)
213215```
214216
215- And boom, a fast Jacobian filling algorithm on your special matrix.
217+ And boom, a fast Jacobian filling algorithm on your special matrix.
0 commit comments