Scientific Computing

in Lean

Tomáš Skřivan

Carnegie Mellon University

Hoskinson Center for Formal Mathematics

18.10.2024

Why Lean

for Scientific Computing?

The language of science is mathematics

... and Lean speaks it!

 

Something interesting has to come out of it!

Why Lean?

  • mathematical abstraction
    • generality, composability
    • seamless mix of symbolic and numerical methods
  • interactivity and (ab)use of tactic mode
    • computer algebra system/source code transformations
    • specification to implementation
    • code optimization
  • extensible syntax
  • formal correctness

Warning: SciLean is not (primarily) formalization project, it is answering the question:

"We have this nice library of formalized mathematics, what do we do with it?"

Outline of the talk

  • wave equation examples
    • demonstrates specification to implementation behind SciLean
  • working with arrays
    • SciLean's approach to tensors/n-dimensional arrays
  • symbolic and automatic differentiation​

Wave Equation

  • Newton's second law

 

 

 

  • For systems driven by potential energy \(\phi\)

 

 

  • Hamiltonian formulation​

 

 

\begin{align*} m \ddot x &= F \end{align*}
\begin{align*} \dot x &= v \\ m \dot v &= F \end{align*}
\begin{align*} \dot x &= v \\ m \dot v &= - \nabla \phi \end{align*}
\begin{align*} H(p,x) = \frac{1}{2 m} p^2 + \phi(x) \end{align*}
\begin{align*} \dot x &= \frac{\partial H}{\partial p} \\ \dot p &= - \frac{\partial H}{\partial x} \end{align*}

Differential Equations in SciLean

  • Differential equation: given \(f,t_0,y_0\) find \(y\) such that

 

 

 

  • For Hamiltonian system

 

 

 

 

  • SciLean's noncomputable function \(\texttt{odeSolve}\)
\begin{align*} \dot x &= \frac{\partial H}{\partial p} \\ \dot p &= - \frac{\partial H}{\partial x} \end{align*}
\begin{align*} \dot y(t) &= f(t, y(t))) \\ y(t_0) &= y_0 \end{align*}
\begin{align*} y(t) &= (x(t), p(t)) \\ f(t,y) = f(t,(x,p)) &= \left(\frac{\partial H}{\partial p}, - \frac{\partial H}{\partial x} \right) \end{align*}
y(t) = \texttt{odeSolve f t₀ t y₀}
m \ddot x_i = \sum_j G \frac{m_i m_j}{r_{ij}^2}\hat r_{ij}
\begin{align*} v_i^{n+1} &= v_i^n + \Delta t \, \sum_j F(x_i^n,x_j^n) \\ x_i^{n+1} &= x_i^n + \Delta t \, v_i^{n+1} \end{align*}
def update (x v : Array Vec3) :=
  v := v + Δt * force x
  x := x + Δt * v
  (x,v)

+

+

+

SciLean

  • website: https://github.com/lecopivo/SciLean
  • work in progress book/documentation: https://github.com/lecopivo/scientific-computing-lean/
  • design principles of SciLean
    1. usability, composability and generality
    2. performance
    3. formal correctness​
  • Lean is an amazing new language and many of its features designed for theorem proving offer a new and exciting design space for a scientific computing language