From 528b427c432571b10b46b6039ff239fd78aaa413 Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Tue, 13 Nov 2018 22:48:36 +0100 Subject: [PATCH] Add MathJax rendering --- _site/archive.html | 2 ++ _site/contact.html | 2 ++ _site/css/syntax.css | 1 + _site/cv.html | 2 ++ _site/index.html | 2 ++ _site/posts/ising-apl.html | 24 +++++++++++++----------- _site/posts/ising-model.html | 30 ++++++++++++++++-------------- _site/posts/lsystems.html | 24 +++++++++++++----------- _site/skills.html | 2 ++ site.hs | 2 +- templates/default.html | 1 + 11 files changed, 55 insertions(+), 37 deletions(-) create mode 100644 _site/css/syntax.css diff --git a/_site/archive.html b/_site/archive.html index da9eca2..2dc01b8 100644 --- a/_site/archive.html +++ b/_site/archive.html @@ -6,6 +6,8 @@ Dimitri Lozeve - Archives + +
diff --git a/_site/contact.html b/_site/contact.html index e8ac3aa..419bdcd 100644 --- a/_site/contact.html +++ b/_site/contact.html @@ -6,6 +6,8 @@ Dimitri Lozeve - Contact + +
diff --git a/_site/css/syntax.css b/_site/css/syntax.css new file mode 100644 index 0000000..624e0ce --- /dev/null +++ b/_site/css/syntax.css @@ -0,0 +1 @@ +a.sourceLine{display:inline-block;line-height:1.25}a.sourceLine{pointer-events:none;color:inherit;text-decoration:inherit}a.sourceLine:empty{height:1.2em}.sourceCode{overflow:visible}code.sourceCode{white-space:pre;position:relative}div.sourceCode{margin:1em 0}pre.sourceCode{margin:0}@media screen{div.sourceCode{overflow:auto}}@media print{code.sourceCode{white-space:pre-wrap}a.sourceLine{text-indent:-1em;padding-left:1em}}pre.numberSource a.sourceLine{position:relative;left:-4em}pre.numberSource a.sourceLine::before{content:attr(title);position:relative;left:-1em;text-align:right;vertical-align:baseline;border:none;pointer-events:all;display:inline-block;-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;padding:0 4px;width:4em;color:#aaaaaa}pre.numberSource{margin-left:3em;border-left:1px solid #aaaaaa;padding-left:4px}div.sourceCode{}@media screen{a.sourceLine::before{text-decoration:underline}}code span.al{color:#ff0000;font-weight:bold}code span.an{color:#60a0b0;font-weight:bold;font-style:italic}code span.at{color:#7d9029}code span.bn{color:#40a070}code span.bu{}code span.cf{color:#007020;font-weight:bold}code span.ch{color:#4070a0}code span.cn{color:#880000}code span.co{color:#60a0b0;font-style:italic}code span.cv{color:#60a0b0;font-weight:bold;font-style:italic}code span.do{color:#ba2121;font-style:italic}code span.dt{color:#902000}code span.dv{color:#40a070}code span.er{color:#ff0000;font-weight:bold}code span.ex{}code span.fl{color:#40a070}code span.fu{color:#06287e}code span.im{}code span.in{color:#60a0b0;font-weight:bold;font-style:italic}code span.kw{color:#007020;font-weight:bold}code span.op{color:#666666}code span.ot{color:#007020}code span.pp{color:#bc7a00}code span.sc{color:#4070a0}code span.ss{color:#bb6688}code span.st{color:#4070a0}code span.va{color:#19177c}code span.vs{color:#4070a0}code span.wa{color:#60a0b0;font-weight:bold;font-style:italic} \ No newline at end of file diff --git a/_site/cv.html b/_site/cv.html index 25e1bc7..5f85739 100644 --- a/_site/cv.html +++ b/_site/cv.html @@ -6,6 +6,8 @@ Dimitri Lozeve - Curriculum Vitæ + +
diff --git a/_site/index.html b/_site/index.html index ac437e3..c12f2ed 100644 --- a/_site/index.html +++ b/_site/index.html @@ -6,6 +6,8 @@ Dimitri Lozeve - Home + +
diff --git a/_site/posts/ising-apl.html b/_site/posts/ising-apl.html index 38a2e8d..076ce51 100644 --- a/_site/posts/ising-apl.html +++ b/_site/posts/ising-apl.html @@ -6,6 +6,8 @@ Dimitri Lozeve - Ising model simulation in APL + +
@@ -37,7 +39,7 @@

The Ising model in APL

I needed a small project to try APL while I was learning. Something array-based, obviously. Since I already implemented a Metropolis-Hastings simulation of the Ising model, which is based on a regular lattice, I decided to reimplement it in Dyalog APL.

It is only a few lines long, but I will try to explain what it does step by step.

-

The first function simply generates a random lattice filled by elements of {1,+1}\{-1,+1\}.

+

The first function simply generates a random lattice filled by elements of \(\{-1,+1\}\).

L←{(2×?⍵ ⍵⍴2)-3}
 

Let’s deconstruct what is done here:

@@ -45,7 +47,7 @@
  • ⍵ is the argument of our function.
  • We generate a ⍵×⍵ matrix filled with 2, using the function: ⍵ ⍵⍴2
  • ? draws a random number between 1 and its argument. We give it our matrix to generate a random matrix of 1 and 2.
  • -
  • We multiply everything by 2 and subtract 3, so that the result is in {1,+1}\{-1,+1\}.
  • +
  • We multiply everything by 2 and subtract 3, so that the result is in \(\{-1,+1\}\).
  • Finally, we assign the result to the name L.
  • Sample output:

    @@ -71,9 +73,9 @@
  • We extract the x and y coordinates of the site.
  • N is the size of the lattice.
  • xn and yn are respectively the vertical and lateral neighbours of the site. N| takes the coordinates modulo N (so the lattice is actually a torus). (Note: we used ⎕IO←0 to use 0-based array indexing.)
  • -
  • +/ sums over all neighbours of the site, and then we multiply by the value of the site itself to get ΔE\Delta E.
  • +
  • +/ sums over all neighbours of the site, and then we multiply by the value of the site itself to get \(\Delta E\).
  • -

    Sample output, for site (3,3)(3, 3) in a random 5×55\times 5 lattice:

    +

    Sample output, for site \((3, 3)\) in a random \(5\times 5\) lattice:

          3 3ising.∆E ising.L 5
     ¯4
     
    @@ -88,21 +90,21 @@ }
      -
    • ⍺ is the β\beta parameter of the Ising model, ⍵ is the lattice.
    • -
    • We draw a random site (x,y)(x,y) with the ? function.
    • -
    • new is the lattice but with the (x,y)(x,y) site flipped.
    • -
    • We compute the probability α=exp(βΔE)\alpha = \exp(-\beta\Delta E) using the * function (exponential) and our previous ∆E function.
    • -
    • ?0 returns a uniform random number in [0,1)[0,1). Based on this value, we decide whether to update the lattice, and we return it.
    • +
    • ⍺ is the \(\beta\) parameter of the Ising model, ⍵ is the lattice.
    • +
    • We draw a random site \((x,y)\) with the ? function.
    • +
    • new is the lattice but with the \((x,y)\) site flipped.
    • +
    • We compute the probability \(\alpha = \exp(-\beta\Delta E)\) using the * function (exponential) and our previous ∆E function.
    • +
    • ?0 returns a uniform random number in \([0,1)\). Based on this value, we decide whether to update the lattice, and we return it.

    We can now bring everything together for display:

    Ising←{' ⌹'[1+1=({10 U ⍵}⍣⍵)L ⍺]}
     
    • We draw a random lattice of size ⍺ with L ⍺.
    • -
    • We apply to it our update function, with $β$=10, ⍵ times (using the function, which applies a function nn times.
    • +
    • We apply to it our update function, with $β$=10, ⍵ times (using the function, which applies a function \(n\) times.
    • Finally, we display -1 as a space and 1 as a domino ⌹.
    -

    Final output, with a 80×8080\times 80 random lattice, after 50000 update steps:

    +

    Final output, with a \(80\times 80\) random lattice, after 50000 update steps:

          80ising.Ising 50000
        ⌹⌹⌹⌹ ⌹⌹⌹⌹⌹⌹⌹⌹⌹⌹      ⌹⌹⌹⌹⌹⌹       ⌹⌹⌹⌹⌹      ⌹⌹⌹⌹       ⌹⌹⌹⌹⌹⌹⌹⌹⌹⌹⌹          
        ⌹⌹⌹⌹      ⌹⌹⌹⌹⌹⌹     ⌹⌹⌹⌹⌹        ⌹⌹⌹⌹⌹      ⌹⌹⌹⌹⌹⌹ ⌹⌹⌹⌹⌹⌹⌹⌹⌹⌹⌹⌹⌹⌹           
    diff --git a/_site/posts/ising-model.html b/_site/posts/ising-model.html
    index e521987..b7b73a6 100644
    --- a/_site/posts/ising-model.html
    +++ b/_site/posts/ising-model.html
    @@ -6,6 +6,8 @@
         
         Dimitri Lozeve - Ising model simulation
         
    +    
    +    
       
       
         
    @@ -32,23 +34,23 @@

    The Ising model is a model used to represent magnetic dipole moments in statistical physics. Physical details are on the Wikipedia page, but what is interesting is that it follows a complex probability distribution on a lattice, where each site can take the value +1 or -1.

    Mathematical definition

    -

    We have a lattice Λ\Lambda consisting of sites kk. For each site, there is a moment σk{1,+1}\sigma_k \in \{ -1, +1 \}. σ=(σk)kΛ\sigma = -(\sigma_k)_{k\in\Lambda} is called the configuration of the lattice.

    -

    The total energy of the configuration is given by the Hamiltonian H(σ)=ijJijσiσj, +

    We have a lattice \(\Lambda\) consisting of sites \(k\). For each site, there is a moment \(\sigma_k \in \{ -1, +1 \}\). \(\sigma = +(\sigma_k)_{k\in\Lambda}\) is called the configuration of the lattice.

    +

    The total energy of the configuration is given by the Hamiltonian \[ H(\sigma) = -\sum_{i\sim j} J_{ij}\, \sigma_i\, \sigma_j, - where iji\sim j denotes neighbours, and JJ is the interaction matrix.

    -

    The configuration probability is given by: πβ(σ)=eβH(σ)Zβ +\] where \(i\sim j\) denotes neighbours, and \(J\) is the interaction matrix.

    +

    The configuration probability is given by: \[ \pi_\beta(\sigma) = \frac{e^{-\beta H(\sigma)}}{Z_\beta} - where β=(kBT)1\beta = (k_B T)^{-1} is the inverse temperature, and ZβZ_\beta the normalisation constant.

    -

    For our simulation, we will use a constant interaction term J>0J > 0. If σi=σj\sigma_i = \sigma_j, the probability will be proportional to exp(βJ)\exp(\beta J), otherwise it would be exp(βJ)\exp(\beta J). Thus, adjacent spins will try to align themselves.

    +\] where \(\beta = (k_B T)^{-1}\) is the inverse temperature, and \(Z_\beta\) the normalisation constant.

    +

    For our simulation, we will use a constant interaction term \(J > 0\). If \(\sigma_i = \sigma_j\), the probability will be proportional to \(\exp(\beta J)\), otherwise it would be \(\exp(\beta J)\). Thus, adjacent spins will try to align themselves.

    Simulation

    The Ising model is generally simulated using Markov Chain Monte Carlo (MCMC), with the Metropolis-Hastings algorithm.

    The algorithm starts from a random configuration and runs as follows:

      -
    1. Select a site ii at random and reverse its spin: σi=σi\sigma'_i = -\sigma_i
    2. -
    3. Compute the variation in energy (hamiltonian) ΔE=H(σ)H(σ)\Delta E = H(\sigma') - H(\sigma)
    4. +
    5. Select a site \(i\) at random and reverse its spin: \(\sigma'_i = -\sigma_i\)
    6. +
    7. Compute the variation in energy (hamiltonian) \(\Delta E = H(\sigma') - H(\sigma)\)
    8. If the energy is lower, accept the new configuration
    9. -
    10. Otherwise, draw a uniform random number u]0,1[u \in ]0,1[ and accept the new configuration if u<min(1,eβΔE)u < \min(1, e^{-\beta \Delta E}).
    11. +
    12. Otherwise, draw a uniform random number \(u \in ]0,1[\) and accept the new configuration if \(u < \min(1, e^{-\beta \Delta E})\).

    Implementation

    The simulation is in Clojure, using the Quil library (a Processing library for Clojure) to display the state of the system.

    @@ -57,7 +59,7 @@ H(\sigma) = -\sum_{i\sim j} J_{ij}\, \sigma_i\, \sigma_j, (:require [quil.core :as q] [quil.middleware :as m]))

    The application works with Quil’s functional mode, with each function taking a state and returning an updated state at each time step.

    -

    The setup function generates the initial state, with random initial spins. It also sets the frame rate. The matrix is a single vector in row-major mode. The state also holds relevant parameters for the simulation: β\beta, JJ, and the iteration step.

    +

    The setup function generates the initial state, with random initial spins. It also sets the frame rate. The matrix is a single vector in row-major mode. The state also holds relevant parameters for the simulation: \(\beta\), \(J\), and the iteration step.

    -

    Given a site ii, we reverse its spin to generate a new configuration state.

    +

    Given a site \(i\), we reverse its spin to generate a new configuration state.

    -

    In order to decide whether to accept this new state, we compute the difference in energy introduced by reversing site ii: ΔE=Jσijiσj. \Delta E = -J\sigma_i \sum_{j\sim i} \sigma_j.

    +

    In order to decide whether to accept this new state, we compute the difference in energy introduced by reversing site \(i\): \[ \Delta E = +J\sigma_i \sum_{j\sim i} \sigma_j. \]

    The filter some? is required to eliminate sites outside of the boundaries of the lattice.

    (defn get-neighbours [state idx]
       "Return the values of a cell's neighbours"
    diff --git a/_site/posts/lsystems.html b/_site/posts/lsystems.html
    index d7ebb83..e07aa59 100644
    --- a/_site/posts/lsystems.html
    +++ b/_site/posts/lsystems.html
    @@ -6,6 +6,8 @@
         
         Dimitri Lozeve - Generating and representing L-systems
         
    +    
    +    
       
       
         
    @@ -40,29 +42,29 @@

    Definition

    An L-system is a set of rewriting rules generating sequences of symbols. Formally, an L-system is a triplet of:

      -
    • an alphabet VV (an arbitrary set of symbols)
    • -
    • an axiom ω\omega, which is a non-empty word of the alphabet (ωV+\omega \in V^+)
    • -
    • a set of rewriting rules (or productions) PP, each mapping a symbol to a word: PV×V*P \subset V \times V^*. Symbols not present in PP are assumed to be mapped to themselves.
    • +
    • an alphabet \(V\) (an arbitrary set of symbols)
    • +
    • an axiom \(\omega\), which is a non-empty word of the alphabet (\(\omega \in V^+\))
    • +
    • a set of rewriting rules (or productions) \(P\), each mapping a symbol to a word: \(P \subset V \times V^*\). Symbols not present in \(P\) are assumed to be mapped to themselves.
    -

    During an iteration, the algorithm takes each symbol in the current word and replaces it by the value in its rewriting rule. Not that the output of the rewriting rule can be absolutely anything in V*V^*, including the empty word! (So yes, you can generate symbols just to delete them afterwards.)

    +

    During an iteration, the algorithm takes each symbol in the current word and replaces it by the value in its rewriting rule. Not that the output of the rewriting rule can be absolutely anything in \(V^*\), including the empty word! (So yes, you can generate symbols just to delete them afterwards.)

    At this point, an L-system is nothing more than a way to generate very long strings of characters. In order to get something useful out of this, we have to give them meaning.

    Drawing instructions and representation

    -

    Our objective is to draw the output of the L-system in order to visually inspect the output. The most common way is to interpret the output as a sequence of instruction for a LOGO-like drawing turtle. For instance, a simple alphabet consisting only in the symbols FF, ++, and - could represent the instructions “move forward”, “turn right by 90°”, and “turn left by 90°” respectively.

    +

    Our objective is to draw the output of the L-system in order to visually inspect the output. The most common way is to interpret the output as a sequence of instruction for a LOGO-like drawing turtle. For instance, a simple alphabet consisting only in the symbols \(F\), \(+\), and \(-\) could represent the instructions “move forward”, “turn right by 90°”, and “turn left by 90°” respectively.

    Thus, we add new components to our definition of L-systems:

      -
    • a set of instructions, II. These are limited by the capabilities of our imagined turtle, so we can assume that they are the same for every L-system we will consider: +
    • a set of instructions, \(I\). These are limited by the capabilities of our imagined turtle, so we can assume that they are the same for every L-system we will consider:
      • Forward makes the turtle draw a straight segment.
      • TurnLeft and TurnRight makes the turtle turn on itself by a given angle.
      • Push and Pop allow the turtle to store and retrieve its position on a stack. This will allow for branching in the turtle’s path.
      • Stay, which orders the turtle to do nothing.
    • -
    • a distance d+d \in \mathbb{R_+}, i.e. how long should each forward segment should be.
    • -
    • an angle θ\theta used for rotation.
    • -
    • a set of representation rules RV×IR \subset V \times I. As before, they will match a symbol to an instruction. Symbols not matched by any rule will be associated to Stay.
    • +
    • a distance \(d \in \mathbb{R_+}\), i.e. how long should each forward segment should be.
    • +
    • an angle \(\theta\) used for rotation.
    • +
    • a set of representation rules \(R \subset V \times I\). As before, they will match a symbol to an instruction. Symbols not matched by any rule will be associated to Stay.
    -

    Finally, our complete L-system, representable by a turtle with capabilities II, can be defined as L=(V,ω,P,d,θ,R). L = (V, \omega, P, d, \theta, -R).

    +

    Finally, our complete L-system, representable by a turtle with capabilities \(I\), can be defined as \[ L = (V, \omega, P, d, \theta, +R). \]

    One could argue that the representation is not part of the L-system, and that the same L-system could be represented differently by changing the representation rules. However, in our setting, we won’t observe the L-system other than by displaying it, so we might as well consider that two systems differing only by their representation rules are different systems altogether.

    Implementation details

    The LSystem data type

    diff --git a/_site/skills.html b/_site/skills.html index 30f1774..be07964 100644 --- a/_site/skills.html +++ b/_site/skills.html @@ -6,6 +6,8 @@ Dimitri Lozeve - Skills in Statistics, Data Science and Machine Learning + +
    diff --git a/site.hs b/site.hs index 1240cc4..84407fc 100644 --- a/site.hs +++ b/site.hs @@ -78,6 +78,6 @@ customPandocCompiler = newExtensions = defaultExtensions `mappend` customExtensions writerOptions = defaultHakyllWriterOptions { writerExtensions = newExtensions - , writerHTMLMathMethod = MathML + , writerHTMLMathMethod = MathJax "" } in pandocCompilerWith defaultHakyllReaderOptions writerOptions diff --git a/templates/default.html b/templates/default.html index 2606a35..638121a 100644 --- a/templates/default.html +++ b/templates/default.html @@ -7,6 +7,7 @@ Dimitri Lozeve - $title$ +