import LinearAlgebra
LinearAlgebra.inv([4 5 ; 3 4])2×2 Matrix{Float64}:
4.0 -5.0
-3.0 4.0
ENPC Course — Scientific Computing in Practice
Session Structure
Each session will include
a theoretical part with slides,
a practical part on computer.
Assessment
3 assignments (3 × 10%) as Jupyter notebooks:
Assignment 1 due today,
Assignment 2 due in the 3rd session (April 29),
Assignment 3 due in the 5th session (May 13).
Final exam on computer, without AI (70%)
Detailed Schedule
| Date | Topic |
|---|---|
| Tuesday, April 8 | Introduction to Julia (assignment 1 \(\rightarrow\) Monday April 14) |
| Tuesday, April 15 | Interpolation and integration |
| Tuesday, April 29 | Linear systems (assignment 2 \(\rightarrow\) Monday May 12) |
| Tuesday, May 6 | Eigenvalue problems |
| Tuesday, May 13 | Nonlinear systems (assignment 3 \(\rightarrow\) Monday May 26) |
| Tuesday, May 20 | Ordinary differential equations |
| Tuesday, May 27 | Final exam |
Important Notes
The content of the first notebook is essential for the rest of the course.
The course moves quickly and requires regular work.
Attendance is mandatory (−2/20 per unjustified absence).
$$
% % %
%
$$
\[ \definecolor{gris_C}{RGB}{96,96,96} \definecolor{blanc_C}{RGB}{255,255,255} \definecolor{pistache_C}{RGB}{176,204,78} \definecolor{pistache2_C}{RGB}{150,171,91} \definecolor{jaune_C}{RGB}{253,235,125} \definecolor{jaune2_C}{RGB}{247,208,92} \definecolor{orange_C}{RGB}{244,157,84} \definecolor{orange2_C}{RGB}{239,119,87} \definecolor{bleu_C}{RGB}{126,151,206} \definecolor{bleu2_C}{RGB}{90,113,180} \definecolor{vert_C}{RGB}{96,180,103} \definecolor{vert2_C}{RGB}{68,141,96} \definecolor{pistache_light_C}{RGB}{235,241,212} \definecolor{jaune_light_C}{RGB}{254,250,224} \definecolor{orange_light_C}{RGB}{251,230,214} \definecolor{bleu_light_C}{RGB}{222,229,241} \definecolor{vert_light_C}{RGB}{216,236,218} \]
Programming language created in 2009 at MIT
By Jeff Bezanson, Stefan Karpinski, Viral B. Shah, Alan Edelman
Current version 1.12.5
General documentation for Julia: https://docs.julialang.org/en/v1/
Julia → two optionsjuliaupinstall juliaup from https://github.com/JuliaLang/juliaup
add versions, for example the latest one
PS [path] juliaup add releaseRecommended option: VSCode https://code.visualstudio.com/
Install Jupyter https://jupyter.org/install
Install the IJulia library → https://github.com/JuliaLang/IJulia.jl
See D. Anthoff’s videos on using VSCode for Julia, e.g. link 1 or link 2
REPL = read–eval–print loop → Julia console
The REPL is accessible
as a standalone executable or by typing julia in a terminal (if PATH is set)
integrated in VSCode by pressing Shift+Ctrl+P and searching for Julia: Start REPL
In VSCode, you can type your code lines in a *.jl file and run them one by one in the integrated console with Shift+Enter.
From the REPL, type
] gives access to the package manager
; gives access to shell mode
? gives access to online help
⟵ returns to normal mode
Tab for automatic completion
the up and down arrows to navigate history (optionally filtered by the beginning of the typed line)
To call an already installed package
importusing (no need to prefix the package name afterwards)To install a new package: 2 options
] thenImportant
Installing a package in an environment is only done once.
Any attempt to reinstall is not only useless but also time-consuming.
Julia?| Python | Matlab | Julia | C | |
|---|---|---|---|---|
| Free/open source? | ✔️ | ❌ | ✔️ | ✔️ |
| Fast? | ❔ | ❔ | ✔️ | ✔️ |
| Easy to learn? | ✔️ | ✔️ | ✔️ | ❌ |
| Suited for scientific computing? | ❔ | ✔️ | ✔️ | ❌ |
| Rich ecosystem? | ✔️ | ❔ | ❔ | ✔️ |
Julia is easy to learn, read and maintain (comparable to Python)
Julia is fast (comparable to C/C++, Fortran)
Julia is efficient (multiple dispatch, macros, …)
Julia is well suited for scientific computing
Julia is easy to learn, read and maintain1 is a Int64
π is a Irrational{:π}
1//2 is a Rational{Int64}
4.641592653589793 is a Float64
lambda) or anonymous functionsThe standard library contains arrays and common operations (+, -, *, \), then LinearAlgebra for det, eigen…
Array comprehension (for inside the array)
Σaᵢⱼ² = sum(A .^ 2) = 958.0725
√Σaᵢⱼ² = 30.952746243265718
Julia is fastJust-in-time compilation
Benchmarks (cf. https://julialang.org/benchmarks)
⚠ These results do not account for compilation time.
The vertical axis shows each benchmark time normalized against the C implementation. The benchmark data shown above were computed with Julia v1.0.0, SciLua v1.0.0-b12, Rust 1.27.0, Go 1.9, Java 1.8.0_17, Javascript V8 6.2.414.54, Matlab R2018a, Anaconda Python 3.6.3, R 3.5.0, and Octave 4.2.2. C and Fortran are compiled with gcc 7.3.1, taking the best timing from all optimization levels (-O0 through -O3). C, Fortran, Go, Julia, Lua, Python, and Octave use OpenBLAS v0.2.20 for matrix operations; Mathematica uses Intel MKL. The Python implementations of matrix_statistics and matrix_multiply use NumPy v1.14.0 and OpenBLAS v0.2.20 functions; the rest are pure Python implementations. Raw benchmark numbers in CSV format are available under https://github.com/JuliaLang/Microbenchmarks.
These micro-benchmark results were obtained on a single core (serial execution) on an Intel Core i7-3960X 3.30GHz CPU with 64GB of 1600MHz DDR3 RAM, running openSUSE LEAP 15.0 Linux.
Julia is efficientA major advantage is multiple dispatch
Example taken from the talk The Unreasonable Effectiveness of Multiple Dispatch by Stefan Karpinski
abstract type Animal end
struct Chien <: Animal; nom::String end
struct Chat <: Animal; nom::String end
function rencontre(a::Animal,b::Animal)
verb = agit(a,b)
println("$(a.nom) rencontre $(b.nom) et $verb")
end
agit(a::Chien,b::Chien) = "le renifle" ; agit(a::Chien,b::Chat) = "le chasse"
agit(a::Chat,b::Chien) = "s'enfuit" ; agit(a::Chat,b::Chat) = "miaule"
medor = Chien("Médor") ; 🐶 = Chien("🐶")
felix = Chat("Félix") ; 🐱 = Chat("🐱")
rencontre(medor,🐶)
rencontre(🐶,felix)
rencontre(felix,🐶)
rencontre(🐱,felix)Médor rencontre 🐶 et le renifle
🐶 rencontre Félix et le chasse
Félix rencontre 🐶 et s'enfuit
🐱 rencontre Félix et miaule
Example inspired by Mosè Giordano’s blog
abstract type HandShape end
struct Rock <: HandShape end
struct Paper <: HandShape end
struct Scissors <: HandShape end
play(::Type{Paper}, ::Type{Rock}) = println("Paper VS Rock ⇒ Paper wins")
play(::Type{Scissors}, ::Type{Paper}) = println("Scissors VS Paper ⇒ Scissors wins")
play(::Type{Rock}, ::Type{Scissors}) = println("Rock VS Scissors ⇒ Rock wins")
play(::Type{T}, ::Type{T}) where {T<: HandShape} = println("Tie between $(T), try again")
play(a::Type{<:HandShape}, b::Type{<:HandShape}) = play(b, a) # Commutativity
play(Paper, Rock)
play(Scissors, Scissors)
play(Paper, Scissors)Paper VS Rock ⇒ Paper wins
Tie between Main.Notebook.Scissors, try again
Scissors VS Paper ⇒ Scissors wins
It is easy to add new types after the fact
Example inspired by the talk The Unreasonable Effectiveness of Multiple Dispatch by Stefan Karpinski
\[\sum_{i=1}^p\mathbf{\boldsymbol{v}}_i^\mathrm{T} \mathsf{A} \mathbf{\boldsymbol{v}}_i, \quad \mathsf{A}∈{\mathbb{{R}}}^{n×n},\;\mathbf{\boldsymbol{v}}_i∈{\mathbb{{R}}}^n\]
6.530119 seconds (227.91 k allocations: 18.700 MiB, 1.29% compilation time)
1.2492995156471162e9
Special case of vectors \(\mathbf{\boldsymbol{v}}_i=(0,\cdots,0,1,0,\cdots,0)^\mathrm{T}\)
\[\mathbf{\boldsymbol{v}}^\mathrm{T} \mathsf{A} \mathbf{\boldsymbol{w}} = A_{\mathrm{ind}(\mathbf{\boldsymbol{v}}), \mathrm{ind}(\mathbf{\boldsymbol{w}})}\]
size and getindex must be defined for standard matrix computations)innerJulia is well suited for scientific computing$$
% % %
%
$$
… Euler-Lagrange eq. \(\frac{{\mathrm{d}}}{{\mathrm{d}}t}\left(\frac{∂ℒ}{∂\dot{q_i}}\right) -\frac{∂ℒ}{∂q_i}=0\)
…
using SymPy, LinearAlgebra, TensND
Spherical = coorsys_spherical()
θ, ϕ, r = getcoords(Spherical)
𝐞ᶿ, 𝐞ᵠ, 𝐞ʳ = unitvec(Spherical)
@set_coorsys Spherical
𝕀, 𝕁, 𝕂 = ISO() ; 𝟏 = tensId2()
k, μ = symbols("k μ", positive = true)
λ = k -2μ/3
u = SymFunction("u", real = true)
𝛆 = SYMGRAD(u(r) * 𝐞ʳ)
𝛆 |> intrinsic(u(r)/r)𝐞ᶿ⊗𝐞ᶿ + (u(r)/r)𝐞ᵠ⊗𝐞ᵠ + (Derivative(u(r), r))𝐞ʳ⊗𝐞ʳ
\(u{\left(r \right)} = \frac{C_{1}}{r^{2}} + C_{2} r\)
Official documentation → https://docs.julialang.org/en/v1/
Learn X in Y minutes → https://learnxinyminutes.com/docs/julia
Intro to Julia tutorial (version 1.0) by Jane Herriman → https://youtu.be/8h8rQyEpiZA
The Unreasonable Effectiveness of Multiple Dispatch → https://www.youtube.com/watch?v=kc9HwsxE1OY