77[ ![ Build Status] ( https://github.com/SciML/PreallocationTools.jl/workflows/CI/badge.svg )] ( https://github.com/SciML/PreallocationTools.jl/actions?query=workflow%3ACI )
88[ ![ Build status] ( https://badge.buildkite.com/8e62ff2622721bf7a82aa5effb466d311d53fe63dc89bf2f34.svg )] ( https://buildkite.com/julialang/preallocationtools-dot-jl )
99
10- [ ![ ColPrac: Contributor's Guide on Collaborative Practices for Community Packages] ( https://img.shields.io/badge/ColPrac-Contributor's %20Guide-blueviolet )] ( https://github.com/SciML/ColPrac )
10+ [ ![ ColPrac: Contributor's Guide on Collaborative Practices for Community Packages] ( https://img.shields.io/badge/ColPrac-Contributor%27s %20Guide-blueviolet )] ( https://github.com/SciML/ColPrac )
1111[ ![ SciML Code Style] ( https://img.shields.io/static/v1?label=code%20style&message=SciML&color=9558b2&labelColor=389826 )] ( https://github.com/SciML/SciMLStyle )
1212
1313PreallocationTools.jl is a set of tools for helping build non-allocating
@@ -30,7 +30,7 @@ needed.
3030### Using DiffCache
3131
3232``` julia
33- DiffCache (u:: AbstractArray , N:: Int = ForwardDiff. pickchunksize (length (u)); levels:: Int = 1 )
33+ DiffCache (u:: AbstractArray , N:: Int = ForwardDiff. pickchunksize (length (u)); levels:: Int = 1 )
3434DiffCache (u:: AbstractArray , N:: AbstractArray{<:Int} )
3535```
3636
@@ -73,7 +73,7 @@ randmat = rand(5, 3)
7373sto = similar (randmat)
7474stod = DiffCache (sto)
7575
76- function claytonsample! (sto, τ, α; randmat= randmat)
76+ function claytonsample! (sto, τ, α; randmat = randmat)
7777 sto = get_tmp (sto, τ)
7878 sto .= randmat
7979 τ == 0 && return sto
@@ -82,8 +82,8 @@ function claytonsample!(sto, τ, α; randmat=randmat)
8282 for i in 1 : n
8383 v = sto[i, 2 ]
8484 u = sto[i, 1 ]
85- sto[i, 1 ] = (1 - u^ (- τ) + u^ (- τ)* v^ (- (τ/ (1 + τ))))^ (- 1 / τ) * α
86- sto[i, 2 ] = (1 - u^ (- τ) + u^ (- τ)* v^ (- (τ/ (1 + τ))))^ (- 1 / τ)
85+ sto[i, 1 ] = (1 - u^ (- τ) + u^ (- τ) * v^ (- (τ / (1 + τ))))^ (- 1 / τ) * α
86+ sto[i, 2 ] = (1 - u^ (- τ) + u^ (- τ) * v^ (- (τ / (1 + τ))))^ (- 1 / τ)
8787 end
8888 return sto
8989end
@@ -108,7 +108,7 @@ function foo(du, u, (A, tmp), t)
108108 @. du = u + tmp
109109 nothing
110110end
111- prob = ODEProblem (foo, ones (5 , 5 ), (0. , 1.0 ), (ones (5 ,5 ), zeros (5 ,5 )))
111+ prob = ODEProblem (foo, ones (5 , 5 ), (0.0 , 1.0 ), (ones (5 , 5 ), zeros (5 , 5 )))
112112solve (prob, TRBDF2 ())
113113```
114114
@@ -125,8 +125,11 @@ function foo(du, u, (A, tmp), t)
125125 nothing
126126end
127127chunk_size = 5
128- prob = ODEProblem (foo, ones (5 , 5 ), (0. , 1.0 ), (ones (5 ,5 ), DiffCache (zeros (5 ,5 ), chunk_size)))
129- solve (prob, TRBDF2 (chunk_size= chunk_size))
128+ prob = ODEProblem (foo,
129+ ones (5 , 5 ),
130+ (0.0 , 1.0 ),
131+ (ones (5 , 5 ), DiffCache (zeros (5 , 5 ), chunk_size)))
132+ solve (prob, TRBDF2 (chunk_size = chunk_size))
130133```
131134
132135or just using the default chunking:
@@ -140,9 +143,10 @@ function foo(du, u, (A, tmp), t)
140143 nothing
141144end
142145chunk_size = 5
143- prob = ODEProblem (foo, ones (5 , 5 ), (0. , 1.0 ), (ones (5 ,5 ), DiffCache (zeros (5 ,5 ))))
146+ prob = ODEProblem (foo, ones (5 , 5 ), (0.0 , 1.0 ), (ones (5 , 5 ), DiffCache (zeros (5 , 5 ))))
144147solve (prob, TRBDF2 ())
145148```
149+
146150### DiffCache Example 3: Nested AD calls in an optimization problem involving a Hessian matrix
147151
148152``` julia
@@ -157,8 +161,8 @@ function foo(du, u, p, t)
157161end
158162
159163coeffs = - collect (0.1 : 0.1 : 0.4 )
160- cache = DiffCache (zeros (2 ,2 ), levels = 3 )
161- prob = ODEProblem (foo, ones (2 , 2 ), (0. , 1.0 ), (coeffs, cache))
164+ cache = DiffCache (zeros (2 , 2 ), levels = 3 )
165+ prob = ODEProblem (foo, ones (2 , 2 ), (0.0 , 1.0 ), (coeffs, cache))
162166realsol = solve (prob, TRBDF2 (), saveat = 0.0 : 0.1 : 10.0 , reltol = 1e-8 )
163167
164168function objfun (x, prob, realsol, cache)
@@ -169,15 +173,16 @@ function objfun(x, prob, realsol, cache)
169173 if any ((s. retcode != :Success for s in sol))
170174 ofv = 1e12
171175 else
172- ofv = sum ((sol.- realsol). ^ 2 )
176+ ofv = sum ((sol .- realsol) .^ 2 )
173177 end
174178 return ofv
175179end
176- fn (x,p) = objfun (x, p[1 ], p[2 ], p[3 ])
180+ fn (x, p) = objfun (x, p[1 ], p[2 ], p[3 ])
177181optfun = OptimizationFunction (fn, Optimization. AutoForwardDiff ())
178182optprob = OptimizationProblem (optfun, zeros (length (coeffs)), (prob, realsol, cache))
179183solve (optprob, Newton ())
180184```
185+
181186Solves an optimization problem for the coefficients, ` coeffs ` , appearing in a differential equation.
182187The optimization is done with [ Optim.jl] ( https://github.com/JuliaNLSolvers/Optim.jl ) 's ` Newton() `
183188algorithm. Since this involves automatic differentiation in the ODE solver and the calculation
@@ -206,7 +211,7 @@ construct.
206211## LazyBufferCache
207212
208213``` julia
209- LazyBufferCache (f:: F = identity)
214+ LazyBufferCache (f:: F = identity)
210215```
211216
212217A ` LazyBufferCache ` is a ` Dict ` -like type for the caches which automatically defines
@@ -230,14 +235,14 @@ function foo(du, u, (A, lbc), t)
230235 @. du = u + tmp
231236 nothing
232237end
233- prob = ODEProblem (foo, ones (5 , 5 ), (0. , 1.0 ), (ones (5 ,5 ), LazyBufferCache ()))
238+ prob = ODEProblem (foo, ones (5 , 5 ), (0.0 , 1.0 ), (ones (5 , 5 ), LazyBufferCache ()))
234239solve (prob, TRBDF2 ())
235240```
236241
237242## GeneralLazyBufferCache
238243
239244``` julia
240- GeneralLazyBufferCache (f= identity)
245+ GeneralLazyBufferCache (f = identity)
241246```
242247
243248A ` GeneralLazyBufferCache ` is a ` Dict ` -like type for the caches which automatically defines
@@ -255,7 +260,7 @@ a DifferentialEquations `ODEIntegrator` object. This object is the one created v
255260` DifferentialEquations.init(ODEProblem(ode_fnc, y₀, (0.0, T), p), Tsit5(); saveat = t) ` , and we
256261want to optimize ` p ` in a way that changes its type to ForwardDiff. Thus what we can do is make a
257262GeneralLazyBufferCache which holds these integrator objects, defined by ` p ` , and indexing it with
258- ` p ` in order to retrieve the cache. The first time it's called it will build the integrator, and
263+ ` p ` in order to retrieve the cache. The first time it's called it will build the integrator, and
259264in subsequent calls it will reuse the cache.
260265
261266Defining the cache as a function of ` p ` to build an integrator thus looks like:
269274then ` lbc[p] ` will be smart and reuse the caches. A full example looks like the following:
270275
271276``` julia
272- using Random, DifferentialEquations, LinearAlgebra, Optimization, OptimizationNLopt, OptimizationOptimJL, PreallocationTools
277+ using Random, DifferentialEquations, LinearAlgebra, Optimization, OptimizationNLopt,
278+ OptimizationOptimJL, PreallocationTools
273279
274280lbc = GeneralLazyBufferCache (function (p)
275281 DifferentialEquations. init (ODEProblem (ode_fnc, y₀, (0.0 , T), p), Tsit5 (); saveat = t)
@@ -290,13 +296,17 @@ function loglik(θ, data, integrator)
290296 reinit! (integrator, u0)
291297 solve! (integrator)
292298 ε = yᵒ .- integrator. sol. u
293- ℓ = - 0.5 n * log (2 π * σ^ 2 ) - 0.5 / σ^ 2 * sum (ε.^ 2 )
299+ ℓ = - 0.5 n * log (2 π * σ^ 2 ) - 0.5 / σ^ 2 * sum (ε .^ 2 )
294300end
295301θ₀ = [- 1.0 , 0.5 , 19.73 ]
296302negloglik = (θ, p) -> - loglik (θ, p, lbc[θ[1 ]])
297303fnc = OptimizationFunction (negloglik, Optimization. AutoForwardDiff ())
298304ε = zeros (n)
299- prob = OptimizationProblem (fnc, θ₀, (yᵒ, n, ε), lb= [- 10.0 , 1e-6 , 0.5 ], ub= [10.0 , 10.0 , 25.0 ])
305+ prob = OptimizationProblem (fnc,
306+ θ₀,
307+ (yᵒ, n, ε),
308+ lb = [- 10.0 , 1e-6 , 0.5 ],
309+ ub = [10.0 , 10.0 , 25.0 ])
300310solve (prob, LBFGS ())
301311```
302312
0 commit comments