### Full Example: Optimization with Lazy Buffer Cache Source: https://github.com/sciml/preallocationtools.jl/blob/master/docs/src/index.md This comprehensive example demonstrates using GeneralLazyBufferCache within an optimization workflow. It sets up an ODE problem, defines a negative log-likelihood function that utilizes the cached integrator, creates an OptimizationProblem, and solves it. This showcases how to integrate lazy caching into a complex scientific computing task. ```julia using Random, DifferentialEquations, LinearAlgebra, Optimization, OptimizationNLopt, OptimizationOptimJL, PreallocationTools lbc = GeneralLazyBufferCache(function (p) DifferentialEquations.init(ODEProblem(ode_fnc, y₀, (0.0, T), p), Tsit5(); saveat = t) end) Random.seed!(2992999) λ, y₀, σ = -0.5, 15.0, 0.1 T, n = 5.0, 200 Δt = T / n t = [j * Δt for j in 0:n] y = y₀ * exp.(λ * t) yᵒ = y .+ [0.0, σ * randn(n)...] ode_fnc(u, p, t) = p * u function loglik(θ, data, integrator) yᵒ, n, ε = data λ, σ, u0 = θ integrator.p = λ reinit!(integrator, u0) solve!(integrator) ε = yᵒ .- integrator.sol.u ℓ = -0.5n * log(2π * σ^2) - 0.5 / σ^2 * sum(ε .^ 2) end θ₀ = [-1.0, 0.5, 19.73] negloglik = (θ, p) -> -loglik(θ, p, lbc[θ[1]]) fnc = OptimizationFunction(negloglik, Optimization.AutoForwardDiff()) ε = zeros(n) prob = OptimizationProblem(fnc, θ₀, (yᵒ, n, ε), lb = [-10.0, 1e-6, 0.5], ub = [10.0, 10.0, 25.0]) solve(prob, LBFGS()) ``` -------------------------------- ### Full Example with GeneralLazyBufferCache for Optimization Source: https://github.com/sciml/preallocationtools.jl/blob/master/README.md A comprehensive example demonstrating the use of GeneralLazyBufferCache within an optimization problem. It involves defining an objective function that uses a cached ODE integrator and then solving the optimization problem. ```julia using Random, DifferentialEquations, LinearAlgebra, Optimization, OptimizationNLopt, OptimizationOptimJL, PreallocationTools lbc = GeneralLazyBufferCache(function (p) DifferentialEquations.init(ODEProblem(ode_fnc, y₀, (0.0, T), p), Tsit5(); saveat = t) end) Random.seed!(2992999) λ, y₀, σ = -0.5, 15.0, 0.1 T, n = 5.0, 200 Δt = T / n t = [j * Δt for j in 0:n] y = y₀ * exp.(λ * t) yᵒ = y .+ [0.0, σ * randn(n)...] ode_fnc(u, p, t) = p * u function loglik(θ, data, integrator) yᵒ, n, ε = data λ, σ, u0 = θ integrator.p = λ reinit!(integrator, u0) solve!(integrator) ε = yᵒ .- integrator.sol.u ℓ = -0.5n * log(2π * σ^2) - 0.5 / σ^2 * sum(ε .^ 2) end θ₀ = [-1.0, 0.5, 19.73] negloglik = (θ, p) -> -loglik(θ, p, lbc[θ[1]]) fnc = OptimizationFunction(negloglik, Optimization.AutoForwardDiff()) ε = zeros(n) prob = OptimizationProblem(fnc, θ₀, (yᵒ, n, ε), lb = [-10.0, 1e-6, 0.5], ub = [10.0, 10.0, 25.0]) solve(prob, LBFGS()) ``` -------------------------------- ### Example Usage of LazyBufferCache Source: https://github.com/sciml/preallocationtools.jl/blob/master/docs/src/index.md Demonstrates the use of `LazyBufferCache` within an ODE problem. The cache is automatically created and managed as needed during the ODE solve. ```julia using LinearAlgebra, OrdinaryDiffEq, PreallocationTools function foo(du, u, (A, lbc), t) tmp = lbc[u] mul!(tmp, A, u) @. du = u + tmp nothing end prob = ODEProblem(foo, ones(5, 5), (0.0, 1.0), (ones(5, 5), LazyBufferCache())) solve(prob, TRBDF2()) ``` -------------------------------- ### DiffCache Direct Usage Example Source: https://github.com/sciml/preallocationtools.jl/blob/master/README.md Demonstrates direct usage of DiffCache for automatic differentiation with ForwardDiff.jl. The cache size is inferred from the input matrix. ```julia using ForwardDiff, PreallocationTools randmat = rand(5, 3) sto = similar(randmat) stod = DiffCache(sto) function claytonsample!(sto, τ, α; randmat = randmat) sto = get_tmp(sto, τ) sto .= randmat τ == 0 && return sto n = size(sto, 1) for i in 1:n v = sto[i, 2] u = sto[i, 1] sto[i, 1] = (1 - u^(-τ) + u^(-τ) * v^(-(τ / (1 + τ))))^(-1 / τ) * α sto[i, 2] = (1 - u^(-τ) + u^(-τ) * v^(-(τ / (1 + τ))))^(-1 / τ) end return sto end ForwardDiff.derivative(τ -> claytonsample!(stod, τ, 0.0), 0.3) ForwardDiff.jacobian(x -> claytonsample!(stod, x[1], x[2]), [0.3; 0.0]) ``` -------------------------------- ### FixedSizeDiffCache Constructor Example Source: https://github.com/sciml/preallocationtools.jl/blob/master/README.md Shows the constructor for FixedSizeDiffCache, which preallocates dual numbers in its cache. It is suitable for non-Array types and when the chunk size is known. ```julia FixedSizeDiffCache(u::AbstractArray, chunk_size = Val{ForwardDiff.pickchunksize(length(u))}) FixedSizeDiffCache(u::AbstractArray, chunk_size::Integer) ``` -------------------------------- ### Display Package Status Source: https://github.com/sciml/preallocationtools.jl/blob/master/docs/src/index.md This snippet shows how to display the current status of installed packages using Pkg.status(). It's typically used for debugging or understanding project dependencies. ```julia using Pkg # hide Pkg.status() # hide ``` -------------------------------- ### GeneralLazyBufferCache for ODE Integrator Caching Source: https://github.com/sciml/preallocationtools.jl/blob/master/README.md Example of using GeneralLazyBufferCache to cache ODE integrator objects. The cache is defined as a function of the parameter `p` to build the integrator, and subsequent calls with the same `p` reuse the cached integrator. ```julia lbc = GeneralLazyBufferCache(function (p) DifferentialEquations.init(ODEProblem(ode_fnc, y₀, (0.0, T), p), Tsit5(); saveat = t) end) ``` -------------------------------- ### Get Temporary Cache from DiffCache Source: https://github.com/sciml/preallocationtools.jl/blob/master/README.md Retrieves the appropriate temporary cache from a DiffCache object. Returns the dual version if `u` contains dual numbers, otherwise returns the standard cache. ```julia get_tmp(tmp::DiffCache, u) ``` -------------------------------- ### Show enlargediffcache! Documentation Source: https://github.com/sciml/preallocationtools.jl/blob/master/docs/src/internals.md Displays the documentation for the internal `enlargediffcache!` function. This is an internal helper function and may change without notice. ```julia ```@docs PreallocationTools.enlargediffcache! ``` ``` -------------------------------- ### Display Julia Version Information Source: https://github.com/sciml/preallocationtools.jl/blob/master/docs/src/index.md This snippet displays detailed information about the Julia environment, including the version, operating system, and architecture. It's useful for reproducibility and debugging. ```julia using InteractiveUtils # hide versioninfo() # hide ``` -------------------------------- ### Display Manifest and Project File Status Source: https://github.com/sciml/preallocationtools.jl/blob/master/docs/src/index.md This snippet displays the status of all dependencies, including those in the manifest file. It provides a comprehensive view of the project's environment, useful for ensuring reproducibility. ```julia using Pkg # hide Pkg.status(; mode = PKGMODE_MANIFEST) # hide ``` -------------------------------- ### ODE Integration with DiffCache (Initial Attempt) Source: https://github.com/sciml/preallocationtools.jl/blob/master/docs/src/index.md An initial attempt at defining an ODE function that fails due to the temporary array `tmp` not being compatible with dual numbers during automatic differentiation. ```julia using LinearAlgebra, OrdinaryDiffEq function foo(du, u, (A, tmp), t) mul!(tmp, A, u) @. du = u + tmp nothing end prob = ODEProblem(foo, ones(5, 5), (0.0, 1.0), (ones(5, 5), zeros(5, 5))) solve(prob, TRBDF2()) ``` -------------------------------- ### DiffCache for ODEs with Default Chunk Size Source: https://github.com/sciml/preallocationtools.jl/blob/master/README.md Illustrates using DiffCache with an ODEProblem where the chunk size for automatic differentiation is determined by default settings. The DiffCache is initialized without an explicit chunk size. ```julia using LinearAlgebra, OrdinaryDiffEq, PreallocationTools function foo(du, u, (A, tmp), t) tmp = get_tmp(tmp, u) mul!(tmp, A, u) @. du = u + tmp nothing end chunk_size = 5 prob = ODEProblem(foo, ones(5, 5), (0.0, 1.0), (ones(5, 5), DiffCache(zeros(5, 5)))) solve(prob, TRBDF2()) ``` -------------------------------- ### DiffCache for Nested AD in Optimization Source: https://github.com/sciml/preallocationtools.jl/blob/master/README.md Demonstrates using a multi-level DiffCache for an optimization problem involving nested automatic differentiation, such as calculating Hessians in ODE-based optimization. The cache is initialized with `levels = 3`. ```julia using LinearAlgebra, OrdinaryDiffEq, PreallocationTools, Optimization, OptimizationOptimJL function foo(du, u, p, t) tmp = p[2] A = reshape(p[1], size(tmp.du)) tmp = get_tmp(tmp, u) mul!(tmp, A, u) @. du = u + tmp nothing end coeffs = -collect(0.1:0.1:0.4) cache = DiffCache(zeros(2, 2), levels = 3) prob = ODEProblem(foo, ones(2, 2), (0.0, 1.0), (coeffs, cache)) realsol = solve(prob, TRBDF2(), saveat = 0.0:0.1:10.0, reltol = 1e-8) function objfun(x, prob, realsol, cache) prob = remake(prob, u0 = eltype(x).(prob.u0), p = (x, cache)) sol = solve(prob, TRBDF2(), saveat = 0.0:0.1:10.0, reltol = 1e-8) ofv = 0.0 if any((s.retcode != :Success for s in sol)) ofv = 1e12 else ofv = sum((sol .- realsol) .^ 2) end return ofv end fn(x, p) = objfun(x, p[1], p[2], p[3]) optfun = OptimizationFunction(fn, Optimization.AutoForwardDiff()) optprob = OptimizationProblem(optfun, zeros(length(coeffs)), (prob, realsol, cache)) solve(optprob, Newton()) ``` -------------------------------- ### DiffCache for ODEs with Explicit Chunk Size Source: https://github.com/sciml/preallocationtools.jl/blob/master/README.md Shows how to use DiffCache with an ODEProblem when automatic differentiation requires a specific chunk size. The cache is explicitly initialized with the chunk size. ```julia using LinearAlgebra, OrdinaryDiffEq, PreallocationTools function foo(du, u, (A, tmp), t) tmp = get_tmp(tmp, u) mul!(tmp, A, u) @. du = u + tmp nothing end chunk_size = 5 prob = ODEProblem(foo, ones(5, 5), (0.0, 1.0), (ones(5, 5), DiffCache(zeros(5, 5), chunk_size))) solve(prob, TRBDF2(chunk_size = chunk_size)) ``` -------------------------------- ### GeneralLazyBufferCache Constructor Source: https://github.com/sciml/preallocationtools.jl/blob/master/README.md Constructs a GeneralLazyBufferCache. The function `f` generates the cache matching the type of `u`, and subsequent indexing reuses that cache if that type of `u` has already been seen. This is the most general but slowest preallocation method. ```julia GeneralLazyBufferCache(f = identity) ``` -------------------------------- ### Using LazyBufferCache in ODEProblem Source: https://github.com/sciml/preallocationtools.jl/blob/master/README.md Demonstrates using LazyBufferCache to preallocate temporary arrays within an ODE solver. The cache automatically creates and manages temporary arrays needed by the `foo` function. ```julia using LinearAlgebra, OrdinaryDiffEq, PreallocationTools function foo(du, u, (A, lbc), t) tmp = lbc[u] mul!(tmp, A, u) @. du = u + tmp nothing end prob = ODEProblem(foo, ones(5, 5), (0.0, 1.0), (ones(5, 5), LazyBufferCache())) solve(prob, TRBDF2()) ``` -------------------------------- ### Nested AD Calls in Optimization with DiffCache Source: https://github.com/sciml/preallocationtools.jl/blob/master/docs/src/index.md Solves an optimization problem for coefficients in a differential equation using nested automatic differentiation. The DiffCache is configured with `levels = 3` to handle three levels of automatic differentiation. ```julia using LinearAlgebra, OrdinaryDiffEq, PreallocationTools, Optim, Optimization function foo(du, u, p, t) tmp = p[2] A = reshape(p[1], size(tmp.du)) tmp = get_tmp(tmp, u) mul!(tmp, A, u) @. du = u + tmp nothing end coeffs = -collect(0.1:0.1:0.4) cache = DiffCache(zeros(2, 2), levels = 3) prob = ODEProblem(foo, ones(2, 2), (0.0, 1.0), (coeffs, cache)) realsol = solve(prob, TRBDF2(), saveat = 0.0:0.1:10.0, reltol = 1e-8) function objfun(x, prob, realsol, cache) prob = remake(prob, u0 = eltype(x).(prob.u0), p = (x, cache)) sol = solve(prob, TRBDF2(), saveat = 0.0:0.1:10.0, reltol = 1e-8) ofv = 0.0 if any((s.retcode != :Success for s in sol)) ofv = 1e12 else ofv = sum((sol .- realsol) .^ 2) end return ofv end fn(x, p) = objfun(x, p[1], p[2], p[3]) optfun = OptimizationFunction(fn, Optimization.AutoForwardDiff()) optprob = OptimizationProblem(optfun, zeros(length(coeffs)), (prob, realsol, cache)) solve(optprob, Newton()) ``` -------------------------------- ### Initialize DiffCache Source: https://github.com/sciml/preallocationtools.jl/blob/master/README.md Constructs a DiffCache object for pre-allocated vectors compatible with ForwardDiff.jl. Specify the chunk size `N` to match ForwardDiff's chunk size or provide an array for nested differentiation levels. ```julia DiffCache(u::AbstractArray, N::Int = ForwardDiff.pickchunksize(length(u)); levels::Int = 1) DiffCache(u::AbstractArray, N::AbstractArray{<:Int}) ``` -------------------------------- ### ODE Integration with DiffCache (Corrected) Source: https://github.com/sciml/preallocationtools.jl/blob/master/docs/src/index.md Corrected ODE function using DiffCache to ensure the temporary array `tmp` is compatible with dual numbers during automatic differentiation. This version correctly handles the dual number cache. ```julia using LinearAlgebra, OrdinaryDiffEq, PreallocationTools function foo(du, u, (A, tmp), t) tmp = get_tmp(tmp, u) mul!(tmp, A, u) @. du = u + tmp nothing end chunk_size = 5 prob = ODEProblem(foo, ones(5, 5), (0.0, 1.0), (ones(5, 5), DiffCache(zeros(5, 5), chunk_size))) solve(prob, TRBDF2(chunk_size = chunk_size)) ``` -------------------------------- ### LazyBufferCache Constructor Source: https://github.com/sciml/preallocationtools.jl/blob/master/README.md Constructs a LazyBufferCache. The function `f` maps `size_of_cache = f(size(u))`, which by default creates cache arrays of the same size. An optional initializer function can be supplied. ```julia LazyBufferCache(f::F = identity) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.