<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://pgarner.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://pgarner.github.io/" rel="alternate" type="text/html" /><updated>2026-04-08T16:44:56+00:00</updated><id>https://pgarner.github.io/feed.xml</id><title type="html">Home</title><subtitle>These pages contain information about my work, publications and software.
</subtitle><author><name>Phil Garner</name></author><entry><title type="html">Windows</title><link href="https://pgarner.github.io/2023/01/03/Windows.html" rel="alternate" type="text/html" title="Windows" /><published>2023-01-03T00:00:00+00:00</published><updated>2023-01-03T00:00:00+00:00</updated><id>https://pgarner.github.io/2023/01/03/Windows</id><content type="html" xml:base="https://pgarner.github.io/2023/01/03/Windows.html"><![CDATA[<h1 id="windows">Windows</h1>

<h2 id="background">Background</h2>
<p>I’m a desktop Linux user who has used a Mac for a while because Linux doesn’t really work on laptops.  Thinking that perhaps Linux ought to have caught up I bought a Lenovo Yoga and installed <a href="https://archlinux.org/">Arch</a> on it.  Arch can’t handle the screen resolution or the tablet mode and the battery only lasts 4 hours, so let’s try Windows 11.</p>

<h2 id="installation">Installation</h2>
<p>It’s necessary to create a bootable USB stick.  There is a <a href="https://www.microsoft.com/en-us/software-download/windows11/">download page</a> with an ISO image, but that image is for optical disks.  If you put it onto a USB stick the firmware will recognise it but the Windows bootloader won’t (it complains about missing drivers during the process).  To create a USB installation you should go through the <em>Windows</em> image creating process to create the image.  I used a virtual machine, although apparently it is possible to use <a href="https://github.com/WoeUSB/WoeUSB">woeusb</a> too.</p>

<p>The installation creates four partitions:</p>
<ol>
  <li>Small EFI partition</li>
  <li>16G partition, presumably for hibernation (the machine has 16G RAM)</li>
  <li>The main partition</li>
  <li>Small recovery partition</li>
</ol>

<h2 id="admin-account">Admin account</h2>
<p>The installation process requires you to use a Microsoft account; this gives you an Admin account that is tied to this MS account.  It’s possible to convert to a local account but the home path remains as an abbreviation of the MS account name.  However, you can create a new local account then use that account to delete the MS one.</p>

<h2 id="encryption">Encryption</h2>
<p>Before doing much more it’s appropriate to encrypt the disk.  The current way is enable <em>bitlocker</em>.  For me this created an extra recovery partition on top of the one that the MS install created.  So now there are five partitions.</p>

<h2 id="terminal">Terminal</h2>
<p>There is a terminal app that knows about several shells.  Two pertinent ones are:</p>
<ol>
  <li><code class="language-plaintext highlighter-rouge">cmd.exe</code>, the original command prompt</li>
  <li>PowerShell.  This is the one to use.
The terminal has a profile for each one.</li>
</ol>

<p>One thing to know about PowerShell is that it functions on aliases; things like <code class="language-plaintext highlighter-rouge">ls</code> are aliases to complicated looking calls to some API.  You can add your own in a file:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>~\Documents\WindowsPowerShell\profile.ps1
</code></pre></div></div>
<p>with the format</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>set-alias less more
</code></pre></div></div>

<h2 id="package-manager">Package manager</h2>
<p>Windows 11 has a package manager; awesome, use it!  The syntax is along the lines of</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>winget search thunderbird
</code></pre></div></div>
<p>and then</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>winget install thunderbird
</code></pre></div></div>
<p>and it knows about quite a lot of packages.  The syntax can be wordy, but basically it works well.  This gets you things like Zoom too.  Also Visual Studio Code; I think I typed</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>winget install vscode
</code></pre></div></div>
<p>but I could be wrong.</p>

<h2 id="wsl--linux">WSL &amp; Linux</h2>
<p>Windows 11 comes with WSL: Windows Subsystem for Linux.  This is a lightweight virtual machine that runs a standard Linux distribution with a slightly modified kernel.
From powershell you can type</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>wsl --install
</code></pre></div></div>
<p>which lists the available distributions, then something like</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>wsl --install Debian
</code></pre></div></div>
<p>Thereafter you can type <code class="language-plaintext highlighter-rouge">bash</code> in powershell and Debian will appear.  The terminal app also gains a configuration to run Debian directly from the terminal.  The Windows filesystem is visible as <code class="language-plaintext highlighter-rouge">/mnt/c</code> and the Linux filesystem is in the file manager.</p>

<p>I’m told that you can then install more or less anything you want under Linux.  However, for me this is missing the point; it’s Windows so let’s see what works native.</p>

<h2 id="git--ssh">Git &amp; SSH</h2>
<p>VSCode is quite well integrated with Git, it’s not there though.  However, SSH is.  To start the <code class="language-plaintext highlighter-rouge">OpenSSH</code> agent, find the Services app, scroll to OpenSSH and right click <code class="language-plaintext highlighter-rouge">Properties</code>.  Select <code class="language-plaintext highlighter-rouge">Automatic (Delayed Start)</code>.  The config is in <code class="language-plaintext highlighter-rouge">~/.ssh</code>.  After a restart you can then say <code class="language-plaintext highlighter-rouge">ssh-add</code> as under Linux.</p>

<p>For Git, run <code class="language-plaintext highlighter-rouge">winget install git.git</code>.  This gets you a windows version of git.  The gotcha is that it comes compiled with its own version of SSH.  It’s necessary to force it to use the the “native” one by setting the environment variable <code class="language-plaintext highlighter-rouge">GIT_SSH</code>; this can be done on the command line:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>setx GIT_SSH C:\Windows\System32\OpenSSH\ssh.exe
</code></pre></div></div>
<p>Now Git and SSH should work.</p>

<h2 id="latex">LaTeX</h2>
<p>Many references suggest using <code class="language-plaintext highlighter-rouge">texlive</code>.  However, I find that MikTeX is better:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>winget install miktex
</code></pre></div></div>
<p>VSCode now works fine with LaTeX.</p>

<h2 id="pdf-viewer">PDF viewer</h2>
<p>The “native” viewer is Edge.  However, it’s a bit heavy and it doesn’t automatically reload the document when it changes.  Current solution is SumatraPDF:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>winget install sumatrapdf
</code></pre></div></div>
<p>which seems to work fine with LaTeX, except for the ugly yellow logo.</p>

<h2 id="emacs">Emacs</h2>
<p>Generally Not Used Except by Middle Aged Computer Scientists; however, still very useful.  Install with <code class="language-plaintext highlighter-rouge">winget</code>.  By default it has no idea where your home directory is, but it understands <code class="language-plaintext highlighter-rouge">HOME</code>, so</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>setx HOME $env:USERPROFILE
</code></pre></div></div>
<p>It still doesn’t default to loading from there, but it understands <code class="language-plaintext highlighter-rouge">~</code>, so not so painful.  <code class="language-plaintext highlighter-rouge">auctex</code> works well with MikTeX.</p>

<h2 id="dev-tools">Dev tools</h2>
<p>For software development you can install Visual Studio and it’s enormous.  For just command line things it’s possible to install just the build tools.  For this, I <em>think</em> I did</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>winget install Microsoft.VisualStudio.2022.BuildTools
</code></pre></div></div>
<p>This gives you a graphical installer from which you can install just the C++ compiler.  I’m still lost with this.  For instance, Visual Studio includes cmake and ninja depending on how much of it you install.  I have at least the <code class="language-plaintext highlighter-rouge">cl.exe</code> compiler and <code class="language-plaintext highlighter-rouge">nmake</code>, and this is added manually to the path:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64
</code></pre></div></div>
<p>which seems stupidly long, but does appear to “work”.  Right now <code class="language-plaintext highlighter-rouge">nmake</code> works with LaTeX, but I’m yet to get compilation up.
This process adds another option to the terminal app: “Developer VS 2022” for both <code class="language-plaintext highlighter-rouge">cmd</code> and PowerShell.  This in turn is just a shell with the dev tools on the path.</p>]]></content><author><name>Phil Garner</name></author><summary type="html"><![CDATA[Windows]]></summary></entry><entry><title type="html">CUDA</title><link href="https://pgarner.github.io/2022/06/01/CUDA.html" rel="alternate" type="text/html" title="CUDA" /><published>2022-06-01T00:00:00+00:00</published><updated>2022-06-01T00:00:00+00:00</updated><id>https://pgarner.github.io/2022/06/01/CUDA</id><content type="html" xml:base="https://pgarner.github.io/2022/06/01/CUDA.html"><![CDATA[<h1 id="cuda">CUDA</h1>

<p>NVidia chips and CUDA infrastructure are complicated.  There are at least <em>five</em> different things that need to be compatible before a program will run:</p>
<ol>
  <li>Hardware<br />
A <a href="https://en.wikipedia.org/wiki/List_of_Nvidia_graphics_processing_units">chip</a> with a specific <a href="https://developer.nvidia.com/cuda-gpus"><em>compute capability</em></a>, e.g., compute 8.6, and a more general architecture, e.g., Ampere.  In fact the chip itself will have a code name, e.g., GA102, and a product name, e.g., RTX 3090.</li>
  <li>Kernel driver<br />
Characterised by a terse version number, e.g, 510.39.01, and may be tied to a specific range of Linux kernels.</li>
  <li>The library <code class="language-plaintext highlighter-rouge">libcuda.so</code> (see below)</li>
  <li><a href="https://docs.nvidia.com/cuda/">CUDA Toolkit</a><br />
Includes libraries such as <code class="language-plaintext highlighter-rouge">libcublas.so</code> and carries the familiar version number, e.g., 11.3.</li>
  <li>Application, such as <a href="https://pytorch.org/">pytorch</a><br />
Typically an application will match the API of a specific toolkit version.</li>
</ol>

<p>Of the above, 1 and 2 are kernel-side things; the system manager will make sure that the kernel driver is compatible with the hardware.</p>

<p>Items 4 and 5 are user-side things.  Any package manager, notably conda, will take care of that compatibility; any application in conda will have a dependency on the appropriate toolkit.  Of course you need to make sure that the <a href="https://docs.google.com/spreadsheets/d/1GabzFueP8A_qkMQqezB4ct6p6kJpU7bud1s3uAJdXVs/edit#gid=0">toolkit matches the hardware capability</a>.</p>

<p>I single out item 3, <code class="language-plaintext highlighter-rouge">libcuda.so</code>, because it’s something that is associated with the application and toolkit.  It needs to be present for an application to link.  However, <code class="language-plaintext highlighter-rouge">libcuda.so</code> is distributed by NVidia with the <em>driver</em>, item 2.  Following the links in Debian, <code class="language-plaintext highlighter-rouge">libcuda.so.1 -&gt; libcuda.so.510.39.01</code>.  Bizarrely, an application cannot link unless the <em>driver package</em> is installed on the machine.</p>

<p>That last requirement can be averted by a package called <code class="language-plaintext highlighter-rouge">nvidia-compat</code>.  The <a href="https://docs.nvidia.com/deploy/cuda-compatibility/index.html#forward-compatibility-title">compatibility package</a> is actually meant to enable new applications to work on older drivers, but it happens to contain a version of <code class="language-plaintext highlighter-rouge">libcuda.so</code> that should allow an application to link when otherwise lacking a driver installation.</p>

<p>The kernel-side installation need not coincide exactly with the user-side.  For instance, kernel driver 510.39.01 is a CUDA 11.6-capable driver, but works perfectly well with CUDA <em>toolkit</em> 11.X and several previous versions.  This is important in the context of compatibility with applications: e.g.,  <a href="https://pytorch.org/">pytorch</a> currently supports the 11.3 toolkit, but had issues with 11.2.  This was independent of the driver.</p>]]></content><author><name>Phil Garner</name></author><summary type="html"><![CDATA[CUDA]]></summary></entry><entry><title type="html">Partitions</title><link href="https://pgarner.github.io/2022/04/27/partitions.html" rel="alternate" type="text/html" title="Partitions" /><published>2022-04-27T00:00:00+00:00</published><updated>2022-04-27T00:00:00+00:00</updated><id>https://pgarner.github.io/2022/04/27/partitions</id><content type="html" xml:base="https://pgarner.github.io/2022/04/27/partitions.html"><![CDATA[<h1 id="the-old-ages">The old ages</h1>

<p>Back in the day, in order to install Linux, I would partition a hard drive with four partitions:</p>

<ol>
  <li>A dedicated, small, <em>boot</em> partition, because either <a href="https://en.wikipedia.org/wiki/Master_boot_record">MBR</a> or <a href="https://en.wikipedia.org/wiki/GNU_GRUB">grub</a> can’t see very far into the disk</li>
  <li>A dedicated <em>swap</em> partition, because the memory was small and a partition was taken to perform better than a swap file.</li>
  <li>A main <em>root</em> partition</li>
  <li>A distinct <em>home</em> partition for user directories, because sooner or later there would be a bad sector, and it was just easier if it didn’t take out both the home directories and the OS.  Partitions and indeed encryption are robust to hardware errors; filesystems are not (although see below).</li>
</ol>

<p>It was <a href="https://en.wikipedia.org/wiki/BIOS">BIOS</a>, so it only knew about MBR, and MBR only supported four (primary) partitions.  It all made sense.  All the partitions were formatted ext3 or ext4, except perhaps the boot partition that didn’t need a journal.</p>

<h1 id="the-middle-ages">The middle ages</h1>

<p>Since about 2010, a few things changed:</p>

<ol>
  <li>BIOS has become <a href="https://en.wikipedia.org/wiki/Unified_Extensible_Firmware_Interface">UEFI</a>, in turn UEFI knows about <a href="https://en.wikipedia.org/wiki/GUID_Partition_Table">GPT</a> and <a href="https://en.wikipedia.org/wiki/File_Allocation_Table#FAT32">FAT32</a>, it makes sense to use GPT and keep the old boot partition but formatted FAT32.  So now the firmware knows about the boot partition.</li>
  <li>UEFI also means no need for grub.  There was something called <a href="https://en.wikipedia.org/wiki/Systemd-boot">gummiboot</a> that could be loaded directly by UEFI; that is now <code class="language-plaintext highlighter-rouge">systemd-boot</code>.  It comes with the OS; use it.</li>
  <li>Eventhough GPT allows lots of partitions, it’s easier to simply have one more and use <a href="https://en.wikipedia.org/wiki/Logical_Volume_Manager_(Linux)">LVM</a> to create swap, root and home.  In principle they can be dynamically resized.</li>
</ol>

<p>So, there are still four partitions.  But that last point also enables encryption: LVM can sit on top of <a href="https://gitlab.com/cryptsetup/cryptsetup/blob/master/README.md">LUKS</a>.  So the second GPT partition is first encrypted as one big block, then LVM further partitions things once decrypted.</p>

<p>The boot partition had to be FAT32 (no problem); I tended to format root and home as <a href="https://en.wikipedia.org/wiki/Btrfs">BTRFS</a>, but simply as a basic filesystem.</p>

<h1 id="modern-times">Modern times</h1>

<p>In the incarnation above, we still have a swap partition and two main partitions.  However, in 2022 (but I am late!) there are two other important considerations:</p>

<ol>
  <li>A contiguous swap file is now <a href="https://wiki.archlinux.org/title/Swap">just as efficient</a> as a dedicated partition.  As long as fragmentation can be avoided, it makes much more sense to allocate it as a file.  A laptop can even hibernate to that file.</li>
  <li>BTRFS claims to be error resilient.  This in principle removes the main justification for separating the root and home partitions from way back.</li>
</ol>

<p>The way BTRFS mimics the partitions that LVM would normally manage is by use of <a href="https://btrfs.readthedocs.io/en/latest/Subvolumes.html">subvolumes</a>.  A subvolume is not a partition; it’s just a separate filesystem in the same “namespace”.  Although I can’t find an authoritative answer, it is reasonable to assume that a hardware error should only affect one subvolume.  As long as that assumption holds then there is no need for partitions other than boot and “main” (I tend to name it “data”).</p>

<p>One more consideration is that my machines tend to contain two hard disks.  Typically this might be an SSD with boot, root and home, and a larger HDD with “common” things.  Common things can be a media library or backup partition.  This leads to the root partition containing two subvolumes acting as mountpoints: <code class="language-plaintext highlighter-rouge">@home</code> with the home directories, and <code class="language-plaintext highlighter-rouge">@data</code> with assorted common things.</p>

<h1 id="btrfs">BTRFS</h1>

<p>A key tool that has become part of the BTRFS infrastructure is <a href="http://snapper.io/">snapper</a>.  The BTRFS literature makes a big thing of subvolumes and the ability to make snapshots of them.  Especially the <a href="https://documentation.suse.com/sles/12-SP3/html/SLES-all/cha-advdisk.html">OpenSuse infrastructure</a> is built around the root partition being snapshotted.  This in turn results in lots of other subvolumes that only exist to prevent them being snapshotted as part of the root subvolume.  If (read: like me) you don’t want to snapshot the root partition, then this can be nonsensical.</p>

<p>Rather, for me, there are two main and one minor reasons to use subvolumes:</p>

<ol>
  <li>Top level subvolumes are analogous to what would have been partitions in one of the older schemes above.  They tend to be called <code class="language-plaintext highlighter-rouge">@</code>, <code class="language-plaintext highlighter-rouge">@home</code>, &amp;c, and are mounted using <code class="language-plaintext highlighter-rouge">fstab</code>.</li>
  <li>Lower level subvolumes, notably user directories, are not individually mounted, but exist so that snapshots can be made.  The <code class="language-plaintext highlighter-rouge">useradd</code> utility has an explicit option to add a user as an explicit subvolume.  This is sensible.</li>
  <li>Within that hierarchy, it is possible to make subvolumes for things that either need not be snapshotted or have particular attributes.  Good examples are virtual box’s <code class="language-plaintext highlighter-rouge">Virtual Machines</code> directory and the <code class="language-plaintext highlighter-rouge">@swap</code> top level volume.</li>
</ol>

<p>Note that all this is considered stable in BTRFS.  By contrast <a href="https://arstechnica.com/gadgets/2021/09/examining-btrfs-linuxs-perpetually-half-finished-filesystem/">managment of multiple disks</a> is not, along with other features such as compression.  The <a href="https://wiki.debian.org/Btrfs">Debian wiki</a> is quite explicit about this.  The <a href="https://wiki.archlinux.org/title/Snapper">Arch wiki</a> is, as ever, the voice of reason.</p>]]></content><author><name>Phil Garner</name></author><summary type="html"><![CDATA[The old ages]]></summary></entry><entry><title type="html">Kalman basics</title><link href="https://pgarner.github.io/2018/05/08/kalman.html" rel="alternate" type="text/html" title="Kalman basics" /><published>2018-05-08T00:00:00+00:00</published><updated>2018-05-08T00:00:00+00:00</updated><id>https://pgarner.github.io/2018/05/08/kalman</id><content type="html" xml:base="https://pgarner.github.io/2018/05/08/kalman.html"><![CDATA[<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  TeX: { equationNumbers: { autoNumber: "all" } },
  tex2jax: {
    inlineMath: [['$','$'], ['\\(','\\)']],
    processEscapes: true
  }
});
</script>
<script type="text/javascript" async
  src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML">
</script>


$$
\nonumber
\def\Pr#1{P\left(#1\right)}
\def\Li#1{p\left(#1\right)}
\def\CondPr#1#2{P\left(#1\mid#2\right)}
\def\CondLi#1#2{p\left(#1\mid#2\right)}
\def\scalar{x}
\newcommand{\st}{\lambda}
\newcommand{\ob}{\scalar}
$$

<p>The Kalman smoother arises when you have a sequence of <span class="math inline">\(N\)</span> observations, <span class="math inline">\(\scalar_1\dots\scalar_N\)</span>, and you want to infer a sequence of unobserved states, <span class="math inline">\(\lambda_1\dots\lambda_N\)</span>; the states follow a first order Markov process, then the observations depend on the states (see the diagram below). Explanations of Kalman smoothers tend to start with the filter and then obfuscate it by blurring in a bunch of tricky Gaussian convolutions. Here we start at the top and derive the recursions without worrying about the actual distributions; they can be added later.</p>

<p><img src="/assets/posts/tikz-kalman.svg" alt="image" /></p>
<p>State <span class="math inline">\(i\)</span> trivially depends upon state <span class="math inline">\(i-1\)</span>, but there is a less trivial dependency on state <span class="math inline">\(i+1\)</span>. So the first thing is integrate it out; this defines the Kalman smoother: <span class="math display">\[\begin{aligned}
  \underbrace{\CondLi{\lambda_i}{\scalar_1\dots\scalar_N}}_{\text{Smoother }i}
  &amp;= \int d\lambda_{i+1}\,
    \CondLi{\lambda_i}{\lambda_{i+1},\scalar_1\dots\scalar_N}
    \CondLi{\lambda_{i+1}}{\scalar_1\dots\scalar_N}, \\
  &amp;= \int d\lambda_{i+1}\,
    \CondLi{\lambda_i}{\lambda_{i+1},\scalar_1\dots\scalar_i}
    \underbrace{\CondLi{\lambda_{i+1}}{\scalar_1\dots\scalar_N}}_{\text{Smoother }i+1}.\end{aligned}\]</span> So it’s recursive in the smoother term. Notice the conditional independence in the second line: given state <span class="math inline">\(i+1\)</span>, we don’t need the observations after that. The inference is the wrong way around in the first term though, so <span class="math display">\[\begin{aligned}
  \CondLi{\lambda_i}{\lambda_{i+1},\scalar_1\dots\scalar_i}
  &amp;= \frac{
    \CondLi{\lambda_{i+1}}{\lambda_i,\scalar_1\dots\scalar_i}
    \CondLi{\lambda_i}{\scalar_1\dots\scalar_i}
  }{
    \CondLi{\lambda_{i+1}}{\scalar_1\dots\scalar_i}
  }, \\
  &amp;= \frac{
    \CondLi{\lambda_{i+1}}{\lambda_i}\CondLi{\lambda_i}{\scalar_1\dots\scalar_i}
  }{
    \CondLi{\lambda_{i+1}}{\scalar_1\dots\scalar_i}
  }.\end{aligned}\]</span> That final term in the numerator is now similar to the original question, but independent of future observatons; that is the Kalman filter. It is evaluated by considering the current observation: <span class="math display">\[\begin{aligned}
  \underbrace{\CondLi{\lambda_i}{\scalar_1\dots\scalar_i}}_{\text{Filter }i}
  &amp;= \frac{
    \CondLi{\scalar_i}{\lambda_i,\scalar_1\dots\scalar_{i-1}}
    \CondLi{\lambda_i}{\scalar_1\dots\scalar_{i-1}}
    }{\CondLi{\scalar_i}{\scalar_1\dots\scalar_{i-1}}}, \\
  &amp;= \frac{
    \CondLi{\scalar_i}{\lambda_i}
    \CondLi{\lambda_i}{\scalar_1\dots\scalar_{i-1}}
    }{\CondLi{\scalar_i}{\scalar_1\dots\scalar_{i-1}}}.\end{aligned}\]</span> Again, some observations become redundant given knowledge of state <span class="math inline">\(i\)</span>. The final term in the numerator is the Kalman predictor; it is evaluated using the trivial relationship between states <span class="math inline">\(i\)</span> and <span class="math inline">\(i-1\)</span>: <span class="math display">\[\begin{aligned}
  \underbrace{\CondLi{\lambda_i}{\scalar_1\dots\scalar_{i-1}}}_{\text{Predictor }i}
  &amp;= \int d\lambda_{i-1}\,
    \CondLi{\lambda_i}{\lambda_{i-1},\scalar_1\dots\scalar_{i-1}}
    \CondLi{\lambda_{i-1}}{\scalar_1\dots\scalar_{i-1}}, \\
  &amp;= \int d\lambda_{i-1}\,
    \CondLi{\lambda_i}{\lambda_{i-1}}
    \underbrace{\CondLi{\lambda_{i-1}}{\scalar_1\dots\scalar_{i-1}}}_{\text{Filter }i-1}.\end{aligned}\]</span> So the filter evaluation is recursive given the predictor.</p>
<p>The evaluation of the whole thing involves working through the equations in the opposite order to which they’re derived:</p>
<ol>
<li><p>Define an initial predictor, <span class="math inline">\(\Li{\lambda_0}\)</span>, to be some distribution.</p></li>
<li><p>Recurse the filter using the predictor from state <span class="math inline">\(1\)</span> to state <span class="math inline">\(N\)</span>. This yields filter values for each state.</p></li>
<li><p>Initialise the smoother using the last filter value; recurse the smoother backwards from state <span class="math inline">\(N\)</span> to state <span class="math inline">\(1\)</span>.</p></li>
</ol>]]></content><author><name>Phil Garner</name></author><summary type="html"><![CDATA[]]></summary></entry><entry><title type="html">From python script to package</title><link href="https://pgarner.github.io/2017/02/16/pypac.html" rel="alternate" type="text/html" title="From python script to package" /><published>2017-02-16T00:00:00+00:00</published><updated>2017-02-16T00:00:00+00:00</updated><id>https://pgarner.github.io/2017/02/16/pypac</id><content type="html" xml:base="https://pgarner.github.io/2017/02/16/pypac.html"><![CDATA[<h1 id="start-with-just-a-script">Start with just a script</h1>

<p>Your project is about spamming things; you kind of know a-priori that
in C++ it’d be a library called libspam; however, you’re using python
for some reason.  To this end, start with a simple script called
<code class="language-plaintext highlighter-rouge">spam.py</code> in a directory called <code class="language-plaintext highlighter-rouge">spam</code>.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>spam
└── spam.py
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">spam.py</code> is just a script; it doesn’t have any functions.</p>

<h1 id="progress-to-a-module">Progress to a module</h1>

<p>After a little while, the spamming gets quite large.  You need another
script called <code class="language-plaintext highlighter-rouge">bar.py</code> that also spams, but differently, so you want
to split the spamming out to a library.</p>

<p>To do this, move the common code into functions in the same file
called <code class="language-plaintext highlighter-rouge">spam.py</code>, but move the different bits into distinct scripts:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>spam
├── bar.py
├── foo.py
└── spam.py
</code></pre></div></div>

<p>So, <code class="language-plaintext highlighter-rouge">spam.py</code> contains a common function called <code class="language-plaintext highlighter-rouge">spam()</code>.</p>

<p>Then in <code class="language-plaintext highlighter-rouge">foo.py</code> you can say</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import spam
</code></pre></div></div>

<p>and it will find your new library and import it.  In python-speak,
<code class="language-plaintext highlighter-rouge">spam.py</code> is now a module.  Python will find it even without setting
<code class="language-plaintext highlighter-rouge">PYTHONPATH</code>.  However, your function is now accessed as
<code class="language-plaintext highlighter-rouge">spam.spam()</code>, which is stupid.  So, this is better:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>from spam import *
</code></pre></div></div>

<p>Or this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>from spam import spam
</code></pre></div></div>

<p>Now it’s just <code class="language-plaintext highlighter-rouge">spam()</code>.</p>

<h1 id="and-to-a-package">And to a package</h1>

<p>After a while, <code class="language-plaintext highlighter-rouge">spam.py</code> might get quite big, including <code class="language-plaintext highlighter-rouge">Spam</code> and
<code class="language-plaintext highlighter-rouge">Eggs</code> classes (classes are CamelCase in python).  So you tend to want
to split it up.  The first thing might be to put the eggy bits into a
file called <code class="language-plaintext highlighter-rouge">eggs.py</code>:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>spam
├── bar.py
├── foo.py
├── spam.py
└── eggs.py
</code></pre></div></div>

<p>However, there’s now no relationship between these for python.  To
enforce that, put them both in a directory like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>spam
├── bar.py
├── foo.py
└── spam
    ├── __init__.py
    ├── spam.py
    └── eggs.py
</code></pre></div></div>

<p>In python speak, <code class="language-plaintext highlighter-rouge">spam</code> is now a package.  <code class="language-plaintext highlighter-rouge">__init__.py</code> can be
empty; it just means “this is a package” to the python interpretter.
<code class="language-plaintext highlighter-rouge">import spam</code> still finds everything.  Python will still find it
without setting <code class="language-plaintext highlighter-rouge">PYTHONPATH</code>.</p>

<p>There are now a lot of things called <code class="language-plaintext highlighter-rouge">spam</code> though.  You can say
(somewhat absurdly)</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import spam
s = new spam.spam.Spam
</code></pre></div></div>

<p>It’s tricky to see a way around this.</p>

<p>As a C++ programmer, one sensible way seems to be to put classes in
files named for the class, so there’s a class <code class="language-plaintext highlighter-rouge">Spam</code> in a file
<code class="language-plaintext highlighter-rouge">spam.py</code> in the directory <code class="language-plaintext highlighter-rouge">spam</code> (as implied above).  Then put this
in <code class="language-plaintext highlighter-rouge">__init__.py</code>:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>from spam import *
from eggs import *
</code></pre></div></div>

<p>And in <code class="language-plaintext highlighter-rouge">foo.py</code> you can say</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>from spam import Spam
from spam import Eggs
s = new Spam
e = new Eggs
</code></pre></div></div>

<p>So, the <code class="language-plaintext highlighter-rouge">__init__.py</code> file can squash some of the namespace madness
that the file hierarchy introduces.  Python people don’t appear to
think this is sensible though.  They would have you put related
classes into one file.  This has the side-effect of leading to big
files that are tricky to navigate.</p>]]></content><author><name>Phil Garner</name></author><summary type="html"><![CDATA[Start with just a script]]></summary></entry><entry><title type="html">English</title><link href="https://pgarner.github.io/2013/08/24/english.html" rel="alternate" type="text/html" title="English" /><published>2013-08-24T00:00:00+00:00</published><updated>2013-08-24T00:00:00+00:00</updated><id>https://pgarner.github.io/2013/08/24/english</id><content type="html" xml:base="https://pgarner.github.io/2013/08/24/english.html"><![CDATA[<h1 id="background">Background</h1>

<p>I get asked English grammar questions sometimes.  They are usually difficult
for the same reason that Feynman’s differentiating under the integral worked:
the person asking has already used all the tools to which they have access, and
failed.  I don’t have Feynman’s method though; they’re just tricky questions.</p>

<p>I also get asked to proof read things a lot.</p>

<h1 id="the-second-of-two-verbs-is-not-necessarily-an-infinitive">The second of two verbs is not necessarily an infinitive</h1>

<p>We tend to learn that the second of two verbs is in the infinitive; it’s not
entirely true.  The second of two verbs is actually likely to be a noun phrase
forming the object of a transitive verb.  There are (at least) three general
ways to do this:</p>

<ol>
  <li>I like to shop. (Infinitive)</li>
  <li>I like shopping. (Gerund)</li>
  <li>I like that I shop. (Explicit nominalisation using another phrase)</li>
</ol>

<p>These are all grammatically correct, albeit with slightly different nuances.
However, consider this example:</p>

<ol>
  <li>I recommend to shop.</li>
  <li>I recommend shopping.</li>
  <li>I recommend that you shop.</li>
</ol>

<p>In this case the first is wrong, but it’s not obvious why.  The second and
third are correct.  It’s the third that explains why the first is wrong: the
subject of the second verb is different to that of the first (you instead of
I); the subjects were the same in the “like” case.  The infinitive cannot
express that, even though it’s clear from the use of “recommend”.  The gerund
one is OK because gerunds don’t have subjects.</p>

<p>This tends to happen with verbs like “recommend” and “allow” in a passive
sense.  In these cases the right answer is often to use the verb in an explicit
nominal form:</p>

<ol>
  <li>I recommend to apply…  (wrong since the subject of apply is missing)</li>
  <li>I recommend applying… (OK but a bit ambiguous)</li>
  <li>I recommend that you apply… (OK, but it’s not passive)</li>
  <li>I recommend application of… (OK and explicitly passive)</li>
</ol>]]></content><author><name>Phil Garner</name></author><summary type="html"><![CDATA[Background]]></summary></entry><entry><title type="html">XSmell</title><link href="https://pgarner.github.io/2009/05/08/xsmell.html" rel="alternate" type="text/html" title="XSmell" /><published>2009-05-08T00:00:00+00:00</published><updated>2009-05-08T00:00:00+00:00</updated><id>https://pgarner.github.io/2009/05/08/xsmell</id><content type="html" xml:base="https://pgarner.github.io/2009/05/08/xsmell.html"><![CDATA[<h1 id="whats-that-smell">What’s that smell?</h1>

<p>There is a school of thought that XML is a be all and end all file format,
suitable for anything requiring any kind of structured text file.  This is OK
when the text to be marked up is, well, text.  Like this document.  What
bothers me is that there is a very very common class of file that stores
essentially key-value pairs.  Typically this is configuration information for a
program.  Crucially, it’s also typically information that requires hand
editing.</p>

<p>An example is assigning values to variables.  In a programming
language, it might be done like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Value = 1;
Colour = "red";
</code></pre></div></div>

<p>where you don’t actually need the equals signs, the quotes or the
semi-colons.  In XML, this tends to come out like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>&lt;Value&gt;1&lt;/Value&gt;
&lt;Colour&gt;red&lt;/Colour&gt;
</code></pre></div></div>

<p>or this</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>&lt;entry Value="1" Colour="red" /&gt;
</code></pre></div></div>

<p>or this</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>&lt;entry key="Value" value="1" /&gt;
&lt;entry key="Colour" value="red" /&gt;
</code></pre></div></div>

<p>or, even</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>&lt;entry&gt;
  &lt;key&gt;Value&lt;/key&gt;
  &lt;value&gt;1&lt;/value&gt;
&lt;/entry&gt;
&lt;entry&gt;
  &lt;key&gt;Colour&lt;/key&gt;
  &lt;value&gt;red&lt;/value&gt;
&lt;/entry&gt;
</code></pre></div></div>

<p>In fact, if you take the view that the content should still be there
when the markup is removed then that last one is the “right” one.</p>

<h1 id="alternatives">Alternatives</h1>

<p>There are a whole bunch of far more suitable formats for key-value
pairs.  One of the most persuasive (to me) is the .ini format that
used to come with Windows:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># This is a comment
[Section]
Value = 1
Colour = red
</code></pre></div></div>

<p>In fact, it’s this format that’s also used by Torvalds in git:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[core]
        repositoryformatversion = 0
        filemode = true
[remote "origin"]
        url = git+ssh://www.site.org/blah.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
</code></pre></div></div>

<p>ALSA has a nice enough format:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pcm.!default {
   type             plug
   slave.pcm       "softvol"
}

pcm.softvol {
   type            softvol
   slave {
       pcm         hw:UA25EX
   }
   control {
       name        "SoftMaster"
       card        1
   }
}
</code></pre></div></div>

<p>Not even Timber Nerdsley uses XML for key-value pairs.  CSS is
key-value pairs and looks like this:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>body {
        margin-left: 3em;
        margin-right: 3em;
        margin-top: 3em;
        margin-bottom: 3em;
        font-family: sans-serif;
}
img.mugshot {
        margin-right: 1ex;
}
</code></pre></div></div>

<p>Even the guys who write XML Schema acknowledge that XML is not the
thing to use to write it:  http://en.wikipedia.org/wiki/RELAX_NG</p>

<p>Edit: (years later) JSON is the thing, it’s awesome.
<a href="https://github.com/pgarner/libube">Lube</a> speaks JSON natively.</p>]]></content><author><name>Phil Garner</name></author><summary type="html"><![CDATA[What’s that smell?]]></summary></entry></feed>