1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

パッケージの使用

Last updated at Posted at 2023-10-15

『1週間で学べる!Julia数値計算プログラミング』の2.8~2.9章について試したことをメモ書きした。

パッケージのインストールの仕方

JuliaのREPLを立ち上げ、

julia>

]キーを押すと、

(@v1.9) pkg>

パッケージモードに切り替わる。ここで、addコマンドでパッケージをインストールできる。

(@v1.9) pkg> add DifferentialEquations

インストール結果は

インストール結果
   Installed ForwardDiff ────────────────────── v0.10.36
   Installed FastBroadcast ──────────────────── v0.2.7
   Installed QuadGK ─────────────────────────── v2.9.1
   Installed FillArrays ─────────────────────── v1.6.1
   Installed RecursiveFactorization ─────────── v0.2.20
   Installed Parameters ─────────────────────── v0.12.3
   Installed SortingAlgorithms ──────────────── v1.1.1
   Installed SparseDiffTools ────────────────── v2.8.0
   Installed Optim ──────────────────────────── v1.7.8
   Installed OrdinaryDiffEq ─────────────────── v6.58.0
   Installed Sparspak ───────────────────────── v0.3.9
   Installed Distributions ──────────────────── v0.25.102
   Installed Krylov ─────────────────────────── v0.9.4
  Downloaded artifact: Sundials
  Downloaded artifact: IntelOpenMP
  Downloaded artifact: Rmath
  Downloaded artifact: OpenSpecFun
    Updating `C:\Users\81906\.julia\environments\v1.9\Project.toml`
  [0c46a032] + DifferentialEquations v7.11.0
    Updating `C:\Users\81906\.julia\environments\v1.9\Manifest.toml`
  [47edcb42] + ADTypes v0.2.4
  [79e6a3ab] + Adapt v3.6.2
  [ec485272] + ArnoldiMethod v0.2.0
  [4fba245c] + ArrayInterface v7.4.11
  [30b0a656] + ArrayInterfaceCore v0.1.29
  [62783981] + BitTwiddlingConvenienceFunctions v0.1.5
  [764a87c0] + BoundaryValueDiffEq v5.1.0
⌅ [fa961155] + CEnum v0.4.2
  [2a0fbf3d] + CPUSummary v0.2.4
  [49dc2e85] + Calculus v0.5.1
  [d360d2e6] + ChainRulesCore v1.16.0
  [fb6a15b2] + CloseOpenIntervals v0.1.12
  [38540f10] + CommonSolve v0.2.4
  [bbf7d656] + CommonSubexpressions v0.3.0
  [34da2185] + Compat v4.10.0
  [2569d6c7] + ConcreteStructs v0.2.3
  [187b0558] + ConstructionBase v1.5.4
  [adafc99b] + CpuId v0.3.1
  [9a962f9c] + DataAPI v1.15.0
  [864edb3b] + DataStructures v0.18.15
  [e2d170a0] + DataValueInterfaces v1.0.0
  [bcd4f6db] + DelayDiffEq v5.43.1
  [2b5f629d] + DiffEqBase v6.133.0
  [459566f4] + DiffEqCallbacks v2.33.0
  [77a26b50] + DiffEqNoiseProcess v5.19.0
  [163ba53b] + DiffResults v1.1.0
  [b552c78f] + DiffRules v1.15.1
  [0c46a032] + DifferentialEquations v7.11.0
  [b4f34e82] + Distances v0.10.10
  [31c24e10] + Distributions v0.25.102
  [ffbed154] + DocStringExtensions v0.9.3
  [fa6b7ba4] + DualNumbers v0.6.8
  [4e289a0a] + EnumX v1.0.4
  [f151be2c] + EnzymeCore v0.6.0
  [d4d017d3] + ExponentialUtilities v1.25.0
  [e2ba6199] + ExprTools v0.1.10
  [7034ab61] + FastBroadcast v0.2.7
  [9aa1b823] + FastClosures v0.3.2
  [29a986be] + FastLapackInterface v2.0.0
  [1a297f60] + FillArrays v1.6.1
  [6a86dc24] + FiniteDiff v2.21.1
  [f6369f11] + ForwardDiff v0.10.36
  [069b7b12] + FunctionWrappers v1.1.3
  [77dc65aa] + FunctionWrappersWrappers v0.1.3
  [d9f16b24] + Functors v0.4.5
  [46192b85] + GPUArraysCore v0.1.5
  [c145ed77] + GenericSchur v0.5.3
  [86223c79] + Graphs v1.9.0
  [3e5b6fbb] + HostCPUFeatures v0.1.16
  [34004b35] + HypergeometricFunctions v0.3.23
  [615f187c] + IfElse v0.1.1
  [d25df0c9] + Inflate v0.1.4
  [92d709cd] + IrrationalConstants v0.2.2
  [82899510] + IteratorInterfaceExtensions v1.0.0
  [ccbc3e58] + JumpProcesses v9.8.0
  [ef3ab10e] + KLU v0.4.1
  [ba0b0d4f] + Krylov v0.9.4
  [10f19ff3] + LayoutPointers v0.1.14
  [50d2b5c4] + Lazy v0.15.1
  [2d8b4e74] + LevyArea v1.0.0
  [d3d80556] + LineSearches v7.2.0
  [7ed4a6bd] + LinearSolve v2.9.2
  [2ab3a3ac] + LogExpFunctions v0.3.26
  [bdcacae8] + LoopVectorization v0.12.165
  [1914dd2f] + MacroTools v0.5.11
  [d125e4d3] + ManualMemory v0.1.8
  [e1d29d7a] + Missings v1.1.0
  [46d2c3a1] + MuladdMacro v0.2.4
  [d41bc354] + NLSolversBase v7.8.3
  [2774e3e8] + NLsolve v4.5.1
  [77ba4419] + NaNMath v1.0.2
  [8913a72c] + NonlinearSolve v2.2.0
  [6fe1bfb0] + OffsetArrays v1.12.10
  [429524aa] + Optim v1.7.8
  [bac558e1] + OrderedCollections v1.6.2
  [1dea7af3] + OrdinaryDiffEq v6.58.0
  [90014a1f] + PDMats v0.11.25
  [65ce6f38] + PackageExtensionCompat v1.0.2
  [d96e819e] + Parameters v0.12.3
  [e409e4f3] + PoissonRandom v0.4.4
  [f517fe37] + Polyester v0.7.8
  [1d0040c9] + PolyesterWeave v0.2.1
  [85a6dd25] + PositiveFactorizations v0.2.4
  [d236fae5] + PreallocationTools v0.4.12
  [1fd47b50] + QuadGK v2.9.1
  [74087812] + Random123 v1.6.1
  [e6cf234a] + RandomNumbers v1.5.3
  [3cdcf5f2] + RecipesBase v1.3.4
  [731186ca] + RecursiveArrayTools v2.38.10
  [f2c3362d] + RecursiveFactorization v0.2.20
  [189a3867] + Reexport v1.2.2
  [ae029012] + Requires v1.3.0
  [ae5879a3] + ResettableStacks v1.1.1
  [79098fc4] + Rmath v0.7.1
  [7e49a35a] + RuntimeGeneratedFunctions v0.5.12
  [94e857df] + SIMDTypes v0.1.0
  [476501e8] + SLEEFPirates v0.6.39
  [0bca4576] + SciMLBase v2.4.0
  [e9a6253c] + SciMLNLSolve v0.1.9
  [c0aeaf25] + SciMLOperators v0.3.6
  [efcf1570] + Setfield v1.1.1
  [727e6d20] + SimpleNonlinearSolve v0.1.20
  [699a6c99] + SimpleTraits v0.9.4
  [ce78b400] + SimpleUnPack v1.1.0
  [66db9d55] + SnoopPrecompile v1.0.3
  [a2af1166] + SortingAlgorithms v1.1.1
  [47a9eef4] + SparseDiffTools v2.8.0
  [e56a9233] + Sparspak v0.3.9
  [276daf66] + SpecialFunctions v2.3.1
  [aedffcd0] + Static v0.8.8
  [0d7ed370] + StaticArrayInterface v1.4.1
  [90137ffa] + StaticArrays v1.6.5
  [1e83bf80] + StaticArraysCore v1.4.2
  [82ae8749] + StatsAPI v1.7.0
  [2913bbd2] + StatsBase v0.34.2
  [4c63d2b9] + StatsFuns v1.3.0
  [9672c7b4] + SteadyStateDiffEq v1.16.1
  [789caeaf] + StochasticDiffEq v6.63.0
⌅ [7792a7ef] + StrideArraysCore v0.4.17
  [c3572dad] + Sundials v4.20.0
  [2efcf032] + SymbolicIndexingInterface v0.2.2
  [3783bdb8] + TableTraits v1.0.1
  [bd369af6] + Tables v1.11.0
  [8290d209] + ThreadingUtilities v0.5.2
  [a2a6695c] + TreeViews v0.3.0
  [d5829a12] + TriangularSolve v0.1.19
  [410a4b4d] + Tricks v0.1.8
  [781d530d] + TruncatedStacktraces v1.4.0
  [3a884ed6] + UnPack v1.0.2
  [3d5dd08c] + VectorizationBase v0.21.64
  [19fa3120] + VertexSafeGraphs v0.2.0
  [700de1a5] + ZygoteRules v0.2.3
  [1d5cc7b8] + IntelOpenMP_jll v2023.2.0+0
  [856f044c] + MKL_jll v2023.2.0+0
  [efe28fd5] + OpenSpecFun_jll v0.5.5+0
  [f50d1b31] + Rmath_jll v0.4.0+0
⌅ [fb77eaff] + Sundials_jll v5.2.1+0
  [8ba89e20] + Distributed
  [9fa8497b] + Future
  [4af54fe1] + LazyArtifacts
  [37e2e46d] + LinearAlgebra
  [1a1011a3] + SharedArrays
  [2f01184e] + SparseArrays
  [10745b16] + Statistics v1.9.0
  [4607b0f0] + SuiteSparse
  [e66e0078] + CompilerSupportLibraries_jll v1.0.5+0
  [4536629a] + OpenBLAS_jll v0.3.21+4
  [05823500] + OpenLibm_jll v0.8.1+0
  [bea87d4a] + SuiteSparse_jll v5.10.1+6
  [8e850b90] + libblastrampoline_jll v5.8.0+0
        Info Packages marked with ⌅ have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated -m`
Precompiling project...
  160 dependencies successfully precompiled in 1163 seconds. 20 already precompiled.
  2 dependencies had warnings during precompilation:
┌ MKL_jll [856f044c-d86e-5d09-b602-aeab76dc8ba7]
│   Downloading artifact: MKL
└  
┌ NonlinearSolve [8913a72c-1f9b-4ce2-8d82-65094dcecaec]
│  WARNING: method definition for #solve#52 at C:\Users\81906\.julia\packages\NonlinearSolve\lOnjn\src\ad.jl:36 declares type variable iip but does not use it.
└  

パッケージに必要な他パッケージの依存関係も解決してくれる。

インストールした情報は以下のファイルに書き込まれる。

  • C:/Users/(ユーザ名)/.julia/registories/General.toml
  • C:/Users/(ユーザ名)/.julia/environments/v1.9/Manifest.toml
  • C:/Users/(ユーザ名)/.julia/environments/v1.9/Project.toml

インストールしたパッケージ(.jl)は以下に配置される。

  • C:/Users/(ユーザ名)/.julia/packages/

precompile

パッケージインストールの結果にあるように、インストールしたパッケージはprecompileされる。そうすることで、moduleの読み込み時間を節約することができる。precompileされたファイルの拡張子は.jiとなる。

precompile結果は以下に格納される。

  • C:/Users/(ユーザ名)/.julia/compiled/v1.9/

REPL環境以外でのパッケージインストール

Google Colabratory上など、REPLが使えない場合は、以下のようにすることでパッケージインストールができる。

パッケージインストール
import Pkg
Pkg.add("DifferentialEquations")

常微分方程式を解く

問題設定

\begin{matrix}
\frac{du}{dt}=1.01u(t) & u(t=0)=0.5
\end{matrix}

これの厳密解は$u(t)=0.5*e^{1.01t}$となる。

これを解いて、厳密解との解を比較してみる。

常微分方程式を解く
using DifferentialEquations
using OrdinaryDiffEq

f(u,p,t)=1.01*u
u0 = 1/2
tspan=(0.0,1.0)
prob=ODEProblem(f,u0,tspan)
sol = solve(prob,Tsit5())
nt=50
time=range(0.0,stop=1.0,length=nt)
tolsum=0
for i=1:nt
  tolsum += abs(0.5*exp(1.01time[i]-sol(time[i]))/sol(time[i]))
end
println("平均相対誤差は",tolsum/nt)
解の誤差率
平均相対誤差は0.434218157985382

高い精度で解を求められていることがわかる。

DifferentialEquations自体は微分方程式を解くものではなく、解法に渡すための箱みたいなものである。
このパッケージが対応する数値解法は様々ある。それぞれの解放の使い方は、それぞれのGitHubページの例を見るのが良い。

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?