Skip to content

Commit 3d9f8f2

Browse files
committed
format markdown
1 parent 9db36fe commit 3d9f8f2

File tree

4 files changed

+99
-62
lines changed

4 files changed

+99
-62
lines changed

.JuliaFormatter.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
style = "sciml"
1+
style = "sciml"
2+
format_markdown = true

LICENSE.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
The PreallocationTools.jl package is licensed under the MIT "Expat" License:
22

33
> Copyright (c) 2016-2020: ChrisRackauckas, Julia Computing.
4-
>
4+
>
55
> Permission is hereby granted, free of charge, to any person obtaining a copy
66
> of this software and associated documentation files (the "Software"), to deal
77
> in the Software without restriction, including without limitation the rights
88
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99
> copies of the Software, and to permit persons to whom the Software is
1010
> furnished to do so, subject to the following conditions:
11-
>
11+
>
1212
> The above copyright notice and this permission notice shall be included in all
1313
> copies or substantial portions of the Software.
14-
>
14+
>
1515
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
> SOFTWARE.
22-
>

README.md

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
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

1313
PreallocationTools.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)
3434
DiffCache(u::AbstractArray, N::AbstractArray{<:Int})
3535
```
3636

@@ -73,7 +73,7 @@ randmat = rand(5, 3)
7373
sto = similar(randmat)
7474
stod = 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
8989
end
@@ -108,7 +108,7 @@ function foo(du, u, (A, tmp), t)
108108
@. du = u + tmp
109109
nothing
110110
end
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)))
112112
solve(prob, TRBDF2())
113113
```
114114

@@ -125,8 +125,11 @@ function foo(du, u, (A, tmp), t)
125125
nothing
126126
end
127127
chunk_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

132135
or just using the default chunking:
@@ -140,9 +143,10 @@ function foo(du, u, (A, tmp), t)
140143
nothing
141144
end
142145
chunk_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))))
144147
solve(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)
157161
end
158162

159163
coeffs = -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))
162166
realsol = solve(prob, TRBDF2(), saveat = 0.0:0.1:10.0, reltol = 1e-8)
163167

164168
function 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
175179
end
176-
fn(x,p) = objfun(x, p[1], p[2], p[3])
180+
fn(x, p) = objfun(x, p[1], p[2], p[3])
177181
optfun = OptimizationFunction(fn, Optimization.AutoForwardDiff())
178182
optprob = OptimizationProblem(optfun, zeros(length(coeffs)), (prob, realsol, cache))
179183
solve(optprob, Newton())
180184
```
185+
181186
Solves an optimization problem for the coefficients, `coeffs`, appearing in a differential equation.
182187
The optimization is done with [Optim.jl](https://github.com/JuliaNLSolvers/Optim.jl)'s `Newton()`
183188
algorithm. 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

212217
A `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
232237
end
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()))
234239
solve(prob, TRBDF2())
235240
```
236241

237242
## GeneralLazyBufferCache
238243

239244
```julia
240-
GeneralLazyBufferCache(f=identity)
245+
GeneralLazyBufferCache(f = identity)
241246
```
242247

243248
A `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
256261
want to optimize `p` in a way that changes its type to ForwardDiff. Thus what we can do is make a
257262
GeneralLazyBufferCache 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
259264
in subsequent calls it will reuse the cache.
260265

261266
Defining the cache as a function of `p` to build an integrator thus looks like:
@@ -269,7 +274,8 @@ end)
269274
then `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

274280
lbc = 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.5n * log(2π * σ^2) - 0.5 / σ^2 * sum.^2)
299+
= -0.5n * log(2π * σ^2) - 0.5 / σ^2 * sum .^ 2)
294300
end
295301
θ₀ = [-1.0, 0.5, 19.73]
296302
negloglik = (θ, p) -> -loglik(θ, p, lbc[θ[1]])
297303
fnc = 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])
300310
solve(prob, LBFGS())
301311
```
302312

0 commit comments

Comments
 (0)