<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.2.0">Jekyll</generator><link href="https://xn--andreasvlker-cjb.de/feed.xml" rel="self" type="application/atom+xml" /><link href="https://xn--andreasvlker-cjb.de/" rel="alternate" type="text/html" /><updated>2024-02-28T14:48:39+01:00</updated><id>https://xn--andreasvlker-cjb.de/feed.xml</id><title type="html">Andreas Völker</title><subtitle>Some post on computers, math and physics by Andreas Völker</subtitle><entry><title type="html">Comparing JPEG, WebP and HEIC images after bit flips</title><link href="https://xn--andreasvlker-cjb.de/2024/02/28/image-formats-bitflips/" rel="alternate" type="text/html" title="Comparing JPEG, WebP and HEIC images after bit flips" /><published>2024-02-28T11:00:11+01:00</published><updated>2024-02-28T11:00:11+01:00</updated><id>https://xn--andreasvlker-cjb.de/2024/02/28/image-formats-bitflips</id><content type="html" xml:base="https://xn--andreasvlker-cjb.de/2024/02/28/image-formats-bitflips/">&lt;p&gt;In the last few months, I have thought a little about long-term archiving files, the effects
of random bit errors and how it interacts with compression. So today, I looked a little at
the different image formats.&lt;/p&gt;

&lt;p&gt;I am not an expert in modern image compression, so I ran a &lt;em&gt;totally unscientific&lt;/em&gt; experiment for fun.
I wrote a &lt;a href=&quot;https://aproblemsquared.libsyn.com&quot;&gt;terrible
a Python script&lt;/a&gt; that uses ImageMagick to convert an image into
different formats and flip either a single or four random bits. This is repeated nine times
and the images are converted to PNG and composed into a grid.&lt;/p&gt;

&lt;p&gt;I look at JPEG, WebP and HEIC (Apple’s format based on H265). My assumption is that JPEG uses
much older and less efficient compression algorithms that leave some redundancy in the file 
and might degrade less, while WebP and HEIC seem kind of similar to me.&lt;/p&gt;

&lt;h2 id=&quot;1-bit-flipped&quot;&gt;1 bit flipped&lt;/h2&gt;

&lt;p&gt;Click images to enlarge.&lt;/p&gt;

&lt;h3 id=&quot;jpeg&quot;&gt;JPEG&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;/assets/images/image-formats-bitflips/1/out.jpg.png&quot;&gt;
    &lt;img width=&quot;600px&quot; src=&quot;/assets/images/image-formats-bitflips/1/out.jpg.png&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Eight of the nine images look fine, while in the middle image, something important seems to be
destroyed. Maybe something is in the file header.&lt;/p&gt;

&lt;h3 id=&quot;webp&quot;&gt;WebP&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;/assets/images/image-formats-bitflips/1/out.webp.png&quot;&gt;
    &lt;img width=&quot;600px&quot; src=&quot;/assets/images/image-formats-bitflips/1/out.webp.png&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here the horrible blocky artifacts start from some line and everything after is broken, but
retains the basic color palette.&lt;/p&gt;

&lt;p&gt;Two images look fine, and two others have major artifacts (to me, comparable to old photographs with major
water damage), but if the motif was important to me, they seem kind of okay.&lt;/p&gt;

&lt;p&gt;The other five images are basically destroyed. You might recognize them when you know the image, but they seem mostly useless to me.&lt;/p&gt;

&lt;h3 id=&quot;heic&quot;&gt;HEIC&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;/assets/images/image-formats-bitflips/1/out.heic.png&quot;&gt;
    &lt;img width=&quot;600px&quot; src=&quot;/assets/images/image-formats-bitflips/1/out.heic.png&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This one confuses me. The artifacts seem only to wander down and to the right, and they are incredible horrible
to look at. Blocky geometric shapes in strange colors.&lt;/p&gt;

&lt;p&gt;Again, two images look fine. For the other images, I am at a loss how to categorize them.
There are more perfectly good pixels than in the WebP images, but the artifacts are horrible.
Some might be okay as mementos if you blur and fade out the color of the artifacts. Five might be salvageable in this way.&lt;/p&gt;

&lt;p&gt;One is a complete loss except for the bird head, but even that might be good in some cases.&lt;/p&gt;

&lt;h2 id=&quot;4-bits-flipped&quot;&gt;4 bits flipped&lt;/h2&gt;

&lt;p&gt;Click images to enlarge.&lt;/p&gt;

&lt;h3 id=&quot;jpeg-1&quot;&gt;JPEG&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;/assets/images/image-formats-bitflips/2/out.jpg.png&quot;&gt;
    &lt;img width=&quot;600px&quot; src=&quot;/assets/images/image-formats-bitflips/2/out.jpg.png&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Four images are fine. The other ones have a line where the color shift or the motif is shifted a little.
They are not great, but totally acceptable. This time, there was no header damage.&lt;/p&gt;

&lt;h3 id=&quot;webp-1&quot;&gt;WebP&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;/assets/images/image-formats-bitflips/2/out.webp.png&quot;&gt;
    &lt;img width=&quot;600px&quot; src=&quot;/assets/images/image-formats-bitflips/2/out.webp.png&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Three images are kind of okay with some major artifacts; five are for bird-head enthusiasts only, and the last one is a complete loss.&lt;/p&gt;

&lt;h3 id=&quot;heic-1&quot;&gt;HEIC&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;/assets/images/image-formats-bitflips/2/out.heic.png&quot;&gt;
    &lt;img width=&quot;600px&quot; src=&quot;/assets/images/image-formats-bitflips/2/out.heic.png&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a horror show.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;One image was too damaged to be an image and shows up as a white spot&lt;/li&gt;
  &lt;li&gt;One image is almost only an artifact without any relevance to the source image&lt;/li&gt;
  &lt;li&gt;Five images are again for the bird-head enjoyers&lt;/li&gt;
  &lt;li&gt;Two images have the main motif kind of undamaged, but there are again absolutely horrible artifacts beside it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bmp-with-1000-bit-flips&quot;&gt;BMP with 1000 bit flips&lt;/h2&gt;

&lt;p&gt;Now, for fun, let’s look at BMP as an uncompressed file format, but let’s introduce 1000 bit errors
instead of four.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/assets/images/image-formats-bitflips/out.bmp.png&quot;&gt;
    &lt;img width=&quot;600px&quot; src=&quot;/assets/images/image-formats-bitflips/out.bmp.png&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This looks perfectly fine. There are obviously some pixel errors, but they don’t destroy the image.
I think it would be trivial to find the worst of them and simply replace them with an average of
the surrounding pixels.&lt;/p&gt;

&lt;p&gt;Of course, at this bit-flip rate, we were lucky not to destroy the header fields.
Even if this happens, we might be able to fix the header with a text editor and calculator, because
BMP is a really simple format.&lt;/p&gt;

&lt;h2 id=&quot;restrictions&quot;&gt;Restrictions&lt;/h2&gt;
&lt;p&gt;Especially the comparison with the bitmap is kind of unfair. The uncompressed file is orders of magnitude
bigger and thus will have a much higher rate of bit error.&lt;/p&gt;

&lt;p&gt;If I had written less horrible Python code, I might have corrected the flip rate for different file sizes.&lt;/p&gt;

&lt;h3 id=&quot;what-did-we-learn-at-the-end&quot;&gt;What did we learn at the end?&lt;/h3&gt;

&lt;p&gt;Use redundant storage with checksums, so that there are no bit flips.&lt;/p&gt;

&lt;p&gt;JPEG is kind of an okayish compromise. It is still much more efficient to store five copies of every
JPEG instead of a BMP.&lt;/p&gt;

&lt;p&gt;Why are the artifacts so different between WebP and HEIC? This might be a question for an expert
in image compression, instead of some dubious experiment.&lt;/p&gt;</content><author><name></name></author><summary type="html">In the last few months, I have thought a little about long-term archiving files, the effects of random bit errors and how it interacts with compression. So today, I looked a little at the different image formats.</summary></entry><entry><title type="html">Readjusting watch hands on UTS DCF clockworks</title><link href="https://xn--andreasvlker-cjb.de/2023/06/08/uts-dcf/" rel="alternate" type="text/html" title="Readjusting watch hands on UTS DCF clockworks" /><published>2023-06-08T19:00:11+02:00</published><updated>2023-06-08T19:00:11+02:00</updated><id>https://xn--andreasvlker-cjb.de/2023/06/08/uts-dcf</id><content type="html" xml:base="https://xn--andreasvlker-cjb.de/2023/06/08/uts-dcf/">&lt;p&gt;&lt;img alt=&quot;Image of the backside of the UTS DCF clockwork. The reset pins are marked in the upper left corner and the pinhole in the lower middle.&quot; src=&quot;/assets/images/uhrwerk.jpg&quot; width=&quot;600&quot; /&gt;&lt;/p&gt;

&lt;p&gt;UTS DCF is a high-quality automatic clockwork based on the German &lt;a href=&quot;https://en.wikipedia.org/wiki/DCF77?useskin=vector&quot;&gt;DCF77&lt;/a&gt; standard. I have a clock using one of these I like very much, but due to unfortunate circumstances, the watch hands got into the wrong alignment&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;. So I very much wanted to fix this. This is what I did follow my instruction at your own risk.&lt;/p&gt;

&lt;p&gt;At first, you need to get the clockwork into the 0:00:00 position. To do this ensure that the clock has a battery and short the
reset pins. I used a small slow screwdriver. The clockwork will now go into the position where all hands
are supposed to point directly up&lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;. Then the clockwork stops and will do nothing.&lt;/p&gt;

&lt;p&gt;The next step is to pull out the battery and put a pin into the pinhole to lock the mechanism.
New clockworks tend to come with a nice pin that fits perfectly for this, but you can use any
small enough pin.&lt;/p&gt;

&lt;p&gt;Now you can adjust the hands with careful rotation or just pull them out and put them in anew. Adjust them
that all of them point directly up.&lt;/p&gt;

&lt;p&gt;Now plug out the pin and insert the battery. Don’t do this the other way around. Now the clock should start to move to 4:00:00.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mine did not.&lt;/strong&gt; So now comes a strange part that scares me, but worked perfectly for two of these clockworks.
Put the battery in &lt;em&gt;the wrong way&lt;/em&gt; wait a few seconds and put it in normally.&lt;/p&gt;

&lt;p&gt;After this, my clock did a full rotation of the second hand and then moved to 4:00:00. 
This is a little strange and most clockworks are supposed to go a full round to 0:00:00, but this one goes 
to the next interval round 4 hours. So if you plug out the battery again and reinsert it, it will go to
8:00:00 and on the next try to 0:00:00.&lt;/p&gt;

&lt;p&gt;Now you should wait a few minutes and the clock will show the correct time when it gets a signal.
It will usually go faster if you put the clock next to a window.&lt;/p&gt;

&lt;p&gt;I hope this will help you to make your clock work correctly in less than the two hours I needed.&lt;/p&gt;

&lt;hr /&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;You should think before you rip out clock hands. I didn’t. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;At least in Germany this is usually the 12. &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</content><author><name></name></author><summary type="html"></summary></entry><entry><title type="html">Using interval arithmetic for better error propagation</title><link href="https://xn--andreasvlker-cjb.de/2023/03/07/interval-error-propagation/" rel="alternate" type="text/html" title="Using interval arithmetic for better error propagation" /><published>2023-03-07T06:00:11+01:00</published><updated>2023-03-07T06:00:11+01:00</updated><id>https://xn--andreasvlker-cjb.de/2023/03/07/interval-error-propagation</id><content type="html" xml:base="https://xn--andreasvlker-cjb.de/2023/03/07/interval-error-propagation/">&lt;h2 id=&quot;errors-and-their-propagation&quot;&gt;Errors and their propagation&lt;/h2&gt;

&lt;p&gt;If an experiment measures a number, the number usually contains some uncertainty that is often called the value’s error. This might result from the limited resolution of the measurement device or resulting from statistical analysis of multiple measurements.&lt;/p&gt;

&lt;p&gt;For example for a value $x=5$, we call its error $\Delta x=0.2$. This is sometimes written as $x = 5\pm 0.2$.&lt;/p&gt;

&lt;p&gt;If we want to derive another value $y = f(x)$, the error in $x$ will result in an error $\Delta y$ for $y$. The process of calculating this new error is called error propagation.&lt;/p&gt;

&lt;p&gt;The usual method to do this in natural science is &lt;a href=&quot;https://en.wikipedia.org/wiki/Propagation_of_uncertainty&quot;&gt;Gauss error propagation law&lt;/a&gt;. I think it is a badly behaved approximation and should basically never be used. &lt;a href=&quot;https://en.wikipedia.org/wiki/Interval_arithmetic&quot;&gt;Interval arithmetic&lt;/a&gt; is conceptionally simpler and always exact.&lt;/p&gt;

&lt;h3 id=&quot;our-example&quot;&gt;Our example&lt;/h3&gt;
&lt;p&gt;We will look at a value $x=0$ with $\Delta x = 2\pi$ and $y=\sin(x)$ and $z=\cos(x)$ for demonstration. This is kind of an evil example because it uses a transcendental function with an error larger than the value.&lt;/p&gt;

&lt;p&gt;A less extreme version of this problem might happen in the physics of resonance. I vaguely remember using an in-principle similar function for a &lt;a href=&quot;https://en.wikipedia.org/wiki/Spin_wave&quot;&gt;spin wave&lt;/a&gt; experiment.&lt;/p&gt;

&lt;h2 id=&quot;gaussian-error-propagation&quot;&gt;Gaussian error propagation&lt;/h2&gt;
&lt;p&gt;Gauss had some clever ideas&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; to approximate the error, but the resulting formulas are easy enough. In this article we will only need the case for one input variable which is very simple:&lt;/p&gt;

\[\begin{align*}
\Delta y = |f&apos;(x)|\Delta x
\end{align*}\]

&lt;h3 id=&quot;our-example-1&quot;&gt;Our example&lt;/h3&gt;
&lt;p&gt;The derivative of $\sin$ and $\cos$ are easy enough to calculate so we get:&lt;/p&gt;

\[\begin{align*}
\Delta y &amp;amp;= |\cos(x)|\Delta x  = 2\pi \cos(0) = 2\pi \\
\Delta z &amp;amp;= |-\sin(x)|\Delta x  = 2\pi \sin(0) = 0
\end{align*}\]

&lt;p&gt;Both of these are insane. $y = 0 \pm 2\pi$ is bigger than the possible range of $sin(x)\in [0,1]$. $z = 1 \pm 0$ does not have any error no matter what.&lt;/p&gt;

&lt;p&gt;The value with its error comes from the full range of $\sin$ and $\cos$ and so the result should be able to take every possible resulting value of the functions and their ranges must be $y = z = 0\pm 1$.&lt;/p&gt;

&lt;h2 id=&quot;interval-arithmetic&quot;&gt;Interval arithmetic&lt;/h2&gt;

&lt;p&gt;With interval arithmetic, we stop calculating with numbers and only consider intervals in which a number must be. For example, we can get an interval from a value and error as $x_I = [x-\Delta x, x+\Delta x]$ For our initial example of $x = 5\pm 0.2$ we get $x_I = [4.8, 5.2]$.&lt;/p&gt;

&lt;p&gt;We can always&lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; recover a value and error from an interval $x_I=[x_1, x_2]$ trivially as $x = \frac{x_1+x_2}{2}$ and $\Delta x = \frac{x_2-x_1}{2}$.&lt;/p&gt;

&lt;p&gt;The general formula for error propagation with intervals is to calculate the &lt;a href=&quot;https://en.wikipedia.org/wiki/Image_(mathematics)#Image_of_a_subset&quot;&gt;image&lt;/a&gt; of the function on the interval&lt;/p&gt;

\[\begin{align*}
f[x_I] = \{f(x): x \in x_I\}
\end{align*}\]

&lt;p&gt;Then the resulting interval is $y_I = [\inf f[x_I], \sup f[x_I]]$.&lt;/p&gt;

&lt;p&gt;This simplifies to something much simpler in most cases, where you only have to consider the interval bounds. For example, the multiplication for two intervals is&lt;/p&gt;

\[\begin{align*}
[a, b] \cdot [c, d] = [\min \{ac, ad, bc, bd\}, \max \{ac, ad, bc, bd\}]
\end{align*}\]

&lt;p&gt;which is trivial to calculate.&lt;/p&gt;

&lt;p&gt;For a function like $\cos(x)$ it is a little more tricky:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Shift $x$ by some multiple of $2\pi$, so it is in $[0, 2\pi]$.&lt;/li&gt;
  &lt;li&gt;Find the endpoint $x_{max}$ of the interval that is nearest to either $0$ or $2\pi$.&lt;/li&gt;
  &lt;li&gt;Find the point $x_{min}$ that is nearest to $\pi$. This might be either of the endpoints of the interval, or the interval might contain this $\pi$.&lt;/li&gt;
  &lt;li&gt;The resulting interval is $y_I = [\cos(x_{min}), \cos(x_{max})]$.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That is not so hard.&lt;/p&gt;

&lt;h3 id=&quot;our-example-2&quot;&gt;Our example&lt;/h3&gt;
&lt;p&gt;Considering the example, we get the interval $x_I = [-2\pi, 2\pi]$. This still is obviously the full range of a period of our functions, and we get:&lt;/p&gt;

\[\begin{align*}
y_I = z_I = [-1, 1]
\end{align*}\]

&lt;h2 id=&quot;practical-considerations-using-python&quot;&gt;Practical considerations: Using python&lt;/h2&gt;

&lt;p&gt;No one should ever do error propagation by hand, except to torture junior students. The most popular and sane&lt;sup id=&quot;fnref:3&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:3&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; tool to analyze measurements is python. I will demonstrate how similar it is using appropriate packages.&lt;/p&gt;

&lt;p&gt;For example, we will use some values $x \in [0, 2]$ with error $\Delta x = \frac{x}{2}$ and calculate $\sin x$.&lt;/p&gt;

&lt;h3 id=&quot;gaussian-error-propagation-1&quot;&gt;Gaussian error propagation&lt;/h3&gt;
&lt;p&gt;Using the package &lt;a href=&quot;https://pythonhosted.org/uncertainties/&quot;&gt;uncertainties,&lt;/a&gt; the resulting code is:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;uncertainties&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ufloat&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;uncertainties.umath&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;umath&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;xsu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ufloat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ysu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;umath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xsu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;interval-arithmetic-1&quot;&gt;Interval arithmetic&lt;/h3&gt;
&lt;p&gt;Using the package &lt;a href=&quot;https://pyinterval.readthedocs.io/en/latest/&quot;&gt;interval,&lt;/a&gt; the resulting code is very similar:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;interval&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;interval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;imath&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;xsi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;interval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ysi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xsi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;plotting-it&quot;&gt;Plotting it&lt;/h3&gt;
&lt;p&gt;Now after we calculate if it is easy enough to plot it and compare them graphically.
&lt;img src=&quot;/assets/images/interval-vs-gauss.svg&quot; alt=&quot;Plot of the calculation of the two arrays above.&quot; /&gt;
As before the Gaussian error propagation has error bars that are above $1$, and they vanish around $x=\frac{\pi}{2} \approx 1.57$.&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;While using computers, interval arithmetic is just as easy as conventional error propagation and it avoids many strange artifacts of Gaussian error propagation.&lt;/p&gt;

&lt;hr /&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;The basic idea of gauss was that errors are very small (which is absolutely not always true) and linearize the function that derives the new value and calculates a spheroid around the value based on the input errors. Nobody really explains this ever, because it is very confusing and clever. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Except if the interval is infinite on at least one side. This is a good thing because this makes intervals fundamentally more powerful than value and error. &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:3&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://julialang.org&quot;&gt;Julia&lt;/a&gt; might be slightly more sane than python &lt;a href=&quot;#fnref:3&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</content><author><name></name></author><summary type="html">Errors and their propagation</summary></entry><entry><title type="html">Release of my first iOS app</title><link href="https://xn--andreasvlker-cjb.de/2023/02/08/released-first-ios-app/" rel="alternate" type="text/html" title="Release of my first iOS app" /><published>2023-02-08T06:00:11+01:00</published><updated>2023-02-08T06:00:11+01:00</updated><id>https://xn--andreasvlker-cjb.de/2023/02/08/released-first-ios-app</id><content type="html" xml:base="https://xn--andreasvlker-cjb.de/2023/02/08/released-first-ios-app/">&lt;p&gt;I release my first app for iPhone/iPad on the app store. In principle, it is really simple and it helps you to figure out when to ventilate your home without messing up the humidity.&lt;/p&gt;

&lt;p&gt;It currently only works in Germany, but you can get it here:&lt;/p&gt;

&lt;p&gt;&lt;a style=&quot;margin: 0 auto;&quot; href=&quot;https://apps.apple.com/us/app/lüften-jetzt/id6443673652&quot;&gt;&lt;img src=&quot;/assets/appstorebadge/en.svg&quot; height=&quot;60&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here come some thoughts on how it went.&lt;/p&gt;

&lt;h2 id=&quot;programming-language-swift&quot;&gt;Programming language: Swift&lt;/h2&gt;
&lt;p&gt;These days &lt;a href=&quot;https://swift.org&quot;&gt;swift&lt;/a&gt; is the language of choice for native iOS apps. In the end, I also used swift for the relatively simple server component to gather the data from the weather service.&lt;/p&gt;

&lt;p&gt;I think it is a really nice language. It has all the features I want without being too complicated. The syntax is nice to look at for me and easy enough to read.&lt;/p&gt;

&lt;p&gt;Of cause it is a little messy, due to its compatibility with some parts of &lt;a href=&quot;https://de.wikipedia.org/wiki/Objective-C&quot;&gt;Objective-C&lt;/a&gt; which was preferred by Apple before developing Swift. It is very much a multi-paradigm language with all the pros and cons of that.&lt;/p&gt;

&lt;p&gt;I had to implement some basic data structures like a &lt;a href=&quot;https://en.wikipedia.org/wiki/Pairing_heap&quot;&gt;Pairing heap&lt;/a&gt; and a &lt;a href=&quot;https://en.wikipedia.org/wiki/Bounding_volume_hierarchy&quot;&gt;Bounding volume hierarchy&lt;/a&gt; for arbitrary dimensional data. From my experience, this can get messy fast in most languages, but it was unexpectedly simple.&lt;/p&gt;

&lt;h2 id=&quot;ui-framework-swiftui&quot;&gt;UI Framework: SwiftUI&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://developer.apple.com/xcode/swiftui/&quot;&gt;SwiftUI&lt;/a&gt; is Apple’s relatively new UI Framework. It kind of follows the idea of &lt;a href=&quot;https://reactjs.org&quot;&gt;react&lt;/a&gt; and &lt;a href=&quot;https://vuejs.org&quot;&gt;vue.js&lt;/a&gt;, but I like it much more than these two. A big part of this is that there are specific features built into Swift to make it nicer to use, which Javascript misses at the moment. I also think it took some ideas from &lt;a href=&quot;https://en.wikipedia.org/wiki/Immediate_mode_GUI&quot;&gt;Immediate mode GUI&lt;/a&gt; that work very well. I think it is very well designed.&lt;/p&gt;

&lt;h2 id=&quot;data-source-germany-public-weather-service&quot;&gt;Data source: Germany public weather service&lt;/h2&gt;
&lt;p&gt;I get my data from the &lt;a href=&quot;https://www.dwd.de/&quot;&gt;DWD&lt;/a&gt;. It has an amazing amount of freely accessible data for Germany. There is also a huge amount of data with a worldwide application, but it is not as nicely preprocessed as the local data.&lt;/p&gt;

&lt;p&gt;The documentation and data formats are a little old-school at first, but easy to parse when you figured out what you need. &lt;strong&gt;Tip&lt;/strong&gt;: If nothing works check if that data uses &lt;a href=&quot;https://de.wikipedia.org/wiki/ISO_8859-1&quot;&gt;latin1&lt;/a&gt; encoding instead of &lt;a href=&quot;https://de.wikipedia.org/wiki/UTF-8&quot;&gt;UTF-8&lt;/a&gt;. As far as I figured out this is not documented anywhere…&lt;/p&gt;</content><author><name></name></author><summary type="html">I release my first app for iPhone/iPad on the app store. In principle, it is really simple and it helps you to figure out when to ventilate your home without messing up the humidity.</summary></entry><entry><title type="html">A proof for the closed form of triangular numbers</title><link href="https://xn--andreasvlker-cjb.de/2022/12/31/triangular-number-proof/" rel="alternate" type="text/html" title="A proof for the closed form of triangular numbers" /><published>2022-12-31T06:00:11+01:00</published><updated>2022-12-31T06:00:11+01:00</updated><id>https://xn--andreasvlker-cjb.de/2022/12/31/triangular-number-proof</id><content type="html" xml:base="https://xn--andreasvlker-cjb.de/2022/12/31/triangular-number-proof/">&lt;p&gt;The triangular numbers&lt;/p&gt;

\[T_n = \sum_{i=1}^n i, \text{for }n&amp;gt;0\]

&lt;p&gt;have the generally known closed form&lt;/p&gt;

\[T_n = \frac{1}{2}n(n+1)\]

&lt;p&gt;This can be easily proven by &lt;a href=&quot;https://proofwiki.org/wiki/Closed_Form_for_Triangular_Numbers/Proof_by_Induction&quot;&gt;induction&lt;/a&gt; or &lt;a href=&quot;https://proofwiki.org/wiki/Closed_Form_for_Triangular_Numbers/Direct_Proof&quot;&gt;young Gauss’s regrouping of terms&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Calculating some sums, I found another way using the sum of the squares. Even though I haven’t found it on the web yet, it is probably well-known.&lt;/p&gt;

&lt;h2 id=&quot;proof&quot;&gt;Proof&lt;/h2&gt;

&lt;p&gt;We look at the sum of squares&lt;/p&gt;

\[S = \sum_{i=0}^{n+1} i^2\]

&lt;p&gt;On the one hand, we can remove the last term from the series:&lt;/p&gt;

\[S = (n+1)^2 + \sum_{i=0}^n i^2\]

&lt;p&gt;On the other hand, we can remove the first term and shift indices:&lt;/p&gt;

\[\begin{aligned}
S &amp;amp;= 0 + \sum_{i=1}^{n+1} i^2 = \sum_{i=0}^n (i+1)^2 \\
 &amp;amp;= \sum_{i=0}^n (i^2+2i+1) = \sum_{i=0}^n i^2+\sum_{i=0}^n 2i+\sum_{i=0}^n 1 \\
 &amp;amp;= \sum_{i=0}^n i^2 + 2\sum_{i=0}^n i + (n+1)
\end{aligned}\]

&lt;p&gt;Equating both sides we get&lt;/p&gt;

\[\begin{aligned}
&amp;amp;(n+1)^2 + \sum_{i=0}^n i^2 = \sum_{i=0}^n i^2 + 2\sum_{i=0}^n i + (n+1) \\
\Leftrightarrow &amp;amp;(n+1)^2 = n+1+ 2\sum_{i=0}^n i \\
\Leftrightarrow &amp;amp;2\sum_{i=0}^n i = (n+1)^2-(n+1) = n(n+1) \\
\Leftrightarrow &amp;amp;\sum_{i=0}^n i = \sum_{i=1}^n i = \frac{1}{2}n(n+1)
\end{aligned}\]

&lt;p&gt;QED&lt;/p&gt;

&lt;h2 id=&quot;some-thoughts&quot;&gt;Some thoughts&lt;/h2&gt;
&lt;p&gt;This is kind of similar to the &lt;a href=&quot;https://proofwiki.org/wiki/Closed_Form_for_Triangular_Numbers/Proof_by_Telescoping_Sum&quot;&gt;proof by telescoping series&lt;/a&gt;. A similar idea to remove the first and last element of a series is often used to calculate the partial sums of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Geometric_series&quot;&gt;geometric series&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This proof does not give as much insight as Gauss’s proof, but it is relatively easy for a proof by manipulation of sums. It also generalizes to the sum of $i^k$ for $k \in \mathbb{N}$ by always using the sum of $i^{k+1}$ and the binomic expansion of $(i+1)^{k+1}$.&lt;/p&gt;</content><author><name></name></author><summary type="html">The triangular numbers</summary></entry><entry><title type="html">A simple script for cron Prometheus export</title><link href="https://xn--andreasvlker-cjb.de/2022/11/30/prometheus-for-cron/" rel="alternate" type="text/html" title="A simple script for cron Prometheus export" /><published>2022-11-30T06:00:11+01:00</published><updated>2022-11-30T06:00:11+01:00</updated><id>https://xn--andreasvlker-cjb.de/2022/11/30/prometheus-for-cron</id><content type="html" xml:base="https://xn--andreasvlker-cjb.de/2022/11/30/prometheus-for-cron/">&lt;p&gt;I like &lt;a href=&quot;https://prometheus.io&quot;&gt;Prometheus&lt;/a&gt; for collecting metrics from servers and programs. Sadly the &lt;a href=&quot;https://en.wikipedia.org/wiki/Cron&quot;&gt;cron deamon&lt;/a&gt; does not support it. So I wrote a simple python script to report some metrics:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Start time&lt;/li&gt;
  &lt;li&gt;Duration of time&lt;/li&gt;
  &lt;li&gt;Exit code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It works with the help of the &lt;a href=&quot;https://github.com/prometheus/node_exporter&quot;&gt;node exporter&lt;/a&gt; that is most likely used in most Prometheus setups.&lt;/p&gt;

&lt;h2 id=&quot;the-script&quot;&gt;The script&lt;/h2&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/env python3
&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;prometheus_client&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REGISTRY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write_to_textfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Gauge&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;prometheus_client&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;subprocess&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# debian/ubuntu
#OUTPUT_DIR = &quot;/var/lib/prometheus/node-exporter&quot;
#freebsd
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUTPUT_DIR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/var/tmp/node_exporter&quot;&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;jobname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;output_file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT_DIR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jobname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;.prom&quot;&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;prometheus_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REGISTRY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unregister&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prometheus_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GC_COLLECTOR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;prometheus_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REGISTRY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unregister&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prometheus_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PLATFORM_COLLECTOR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;prometheus_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REGISTRY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unregister&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prometheus_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PROCESS_COLLECTOR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;start_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;completed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;end_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;exit_code&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;completed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;returncode&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Gauge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;cron_starttime&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Start time of cron job&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;job&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jobname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Gauge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;cron_duration&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Duration of cron job in seconds&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;job&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jobname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end_time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Gauge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;cron_exitcode&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Exit code of cron job&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;job&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jobname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exit_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;write_to_textfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;registry&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REGISTRY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;how-to-install&quot;&gt;How to install&lt;/h2&gt;
&lt;p&gt;This assumes basic familiarity with Prometheus and node_exporter.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Install, setup and run the prometheus node_exporter&lt;/li&gt;
  &lt;li&gt;Install the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prometheus_client&lt;/code&gt;package for python &lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
  &lt;li&gt;Copy the script to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/bin/cron_exporter&lt;/code&gt; &lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
  &lt;li&gt;Make it executable&lt;/li&gt;
  &lt;li&gt;Adjust the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OUTPUT_DIR&lt;/code&gt; variable for your system. I already have examples for your&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;usage&quot;&gt;Usage&lt;/h2&gt;
&lt;p&gt;Edit your crontab as usual, but put the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cron_exporter&lt;/code&gt; and the name of the job in front of every line. So for example&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;@daily /usr/bin/certbot renew
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;should become&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;@daily cron_exporter certbot /usr/bin/certbot renew
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;access-from-prometheus-with-promql&quot;&gt;Access from prometheus with PromQL&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;Duration of certbot: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cron_duration{exported_job=&quot;certbot&quot;}&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Time since the last run for all jobs (in minutes): &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(time()-cron_starttime)/60&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Exit codes for a job named backup: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cron_exitcode{exported_job=&quot;backup&quot;}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;You can simply to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip install prometheus-client&lt;/code&gt; as root. Most people seem to think this is a bad idea, but it works for me. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;I think in principle it would belong into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/bin&lt;/code&gt;, but that does not work out with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; in Debian and Ubuntu &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</content><author><name></name></author><summary type="html">I like Prometheus for collecting metrics from servers and programs. Sadly the cron deamon does not support it. So I wrote a simple python script to report some metrics: Start time Duration of time Exit code</summary></entry><entry><title type="html">Solving a $x^2+\varepsilon x=1$ with perturbation theory for two regimes</title><link href="https://xn--andreasvlker-cjb.de/2022/11/27/quadratic-equation-perturbation/" rel="alternate" type="text/html" title="Solving a $x^2+\varepsilon x=1$ with perturbation theory for two regimes" /><published>2022-11-27T06:00:11+01:00</published><updated>2022-11-27T06:00:11+01:00</updated><id>https://xn--andreasvlker-cjb.de/2022/11/27/quadratic-equation-perturbation</id><content type="html" xml:base="https://xn--andreasvlker-cjb.de/2022/11/27/quadratic-equation-perturbation/">&lt;h2 id=&quot;solving-the-equation-directly&quot;&gt;Solving the equation directly&lt;/h2&gt;
&lt;p&gt;Let’s look at the equation
\(x^2+\varepsilon x=1\)
We can rewrite it as
\(x^2+\varepsilon x-1=0\)
for convenience. As with all quadratic equations, this equation has two solutions. Luckily both solutions of our equation are real numbers for any $\varepsilon$.&lt;/p&gt;

&lt;p&gt;In this article, we will assume $\varepsilon &amp;gt; 0$ and we will only look at the bigger solution. So we &lt;a href=&quot;https://www.wolframalpha.com/input?key=&amp;amp;i=x%5E2%2Bepsilon*x%3D1&quot;&gt;obviously&lt;/a&gt; get
\(x(\varepsilon)=\frac{1}{2}\big(\sqrt{\varepsilon^2+4}-\varepsilon\big)\).&lt;/p&gt;

&lt;h3 id=&quot;limits-for-0-and-infty&quot;&gt;Limits for $0$ and $+\infty$&lt;/h3&gt;
&lt;p&gt;Let’s look at some limits of the function. At $0$ we trivially get
\(\lim_{\varepsilon\rightarrow\infty}x(\varepsilon)=\frac{1}{2}\big(\sqrt{0^2+4}-0\big)=1\).&lt;/p&gt;

&lt;p&gt;$\lim_{\varepsilon\rightarrow\infty}x(\varepsilon)=0$ is more tricky to proof. The &lt;a href=&quot;https://en.wikipedia.org/wiki/Limit_of_a_function#Limits_at_infinity&quot;&gt;definition&lt;/a&gt; for this is&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

\[(\forall C &amp;gt; 0) (\exists \varepsilon &amp;gt; 0)(\forall s&amp;gt;\varepsilon) x(s) &amp;lt; C\]

&lt;p&gt;Because $x(\varepsilon)$ is strictly monotonically decreasing&lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; and bigger then $0$ this all comes down to finding an $\varepsilon$ so that $x(\varepsilon) &amp;lt; C$ for any $C &amp;gt; 0$. So let’s check:&lt;/p&gt;

\[\begin{aligned}
&amp;amp;\frac{1}{2}(\sqrt{\varepsilon^2+4}-\varepsilon) &amp;lt; C\\
\Leftrightarrow &amp;amp;\sqrt{\varepsilon^2+4} &amp;lt; 2C+\varepsilon\\
\Leftrightarrow &amp;amp;\varepsilon^2+4 &amp;lt; (2C+\varepsilon)^2 = 4C^2+4C\varepsilon+\varepsilon^2 \\
\Leftrightarrow &amp;amp; 4 &amp;lt; 4C^2+4C\varepsilon\\
\Leftrightarrow &amp;amp;\frac{1-C^2}{C} &amp;lt; \varepsilon
\end{aligned}\]

&lt;p&gt;QED&lt;/p&gt;

&lt;h2 id=&quot;pertrubation-theory-for-small-varepsilon&quot;&gt;Pertrubation theory for small $\varepsilon$&lt;/h2&gt;

&lt;p&gt;So let’s assume $\varepsilon$ is small. If we assume $\varepsilon = 0$ we can solve the resulting equation $x^2 - 1 = 0$ instantly. This is nice and we can formalize this by using a &lt;a href=&quot;https://en.wikipedia.org/wiki/Power_series&quot;&gt;power series&lt;/a&gt; for $x$ as&lt;/p&gt;

\[x = \sum_{n=0}^\infty x_n\varepsilon^n = x_0 + \varepsilon x_1 + \varepsilon^2 x_2 + \varepsilon^3 x_3 + \varepsilon^4 x_4+\dots\]

&lt;p&gt;with unknown coefficients $x_n$.&lt;/p&gt;

&lt;p&gt;So we can plug this in our equation and get
\(\begin{aligned}0&amp;amp;=x^2+\varepsilon x-1\\
&amp;amp;=(x_0 + \varepsilon x_1 + \varepsilon^2 x_2 + \varepsilon^3 x_3 + \varepsilon^4 x_4+\dots)(x_0 + \varepsilon x_1 + \varepsilon^2 x_2 + \varepsilon^3 x_3 + \varepsilon^4 x_4+\dots)\\
&amp;amp;\quad+\varepsilon(x_0 + \varepsilon x_1 + \varepsilon^2 x_2 + \varepsilon^3 x_3 + \varepsilon^4 x_4+\dots)-1\end{aligned}\)&lt;/p&gt;

&lt;p&gt;We can multiply this out
\(\begin{aligned}0=&amp;amp;x_1^2+2\varepsilon x_0x_1+2\varepsilon^2 x_0x_2+\varepsilon^2 x_1^2+2\varepsilon^3x_0x_3+2\varepsilon^3x_1x_2+\\
&amp;amp;2\varepsilon^4x_0x_4+2\varepsilon^4x_1x_3+\varepsilon^4x_2^2+\\
&amp;amp;\varepsilon x_0+\varepsilon^2 x_1+\varepsilon^3 x_2+\varepsilon^4 x_3-1+\mathcal{O}(\varepsilon^5)
\end{aligned}\)&lt;/p&gt;

&lt;p&gt;Grouping together by powers of $\varepsilon$ we get
\(\begin{aligned}
0=&amp;amp;(x_1^2-1)+\varepsilon(2x_0x_1+x_0)+\varepsilon^2(2x_0x_2+x_1^2+x_1)\\
&amp;amp;\varepsilon^3(2x_0x_3+2x_1x_2+x_2)+\varepsilon^4(2x_0x_4+2x_1x_3+x_2^2+x_3)+\mathcal O(\varepsilon^5)
\end{aligned}\)&lt;/p&gt;

&lt;p&gt;This equation needs to be zero for an arbitrary $\varepsilon$. This is only possible if each factor is equal to $0$. So we will look at each factor.&lt;/p&gt;

&lt;h3 id=&quot;varepsilon01&quot;&gt;$\varepsilon^0=1$&lt;/h3&gt;
&lt;p&gt;We get $x_0^2-1=0$. Again we only want the larger solution so we take $x_0=1$. After this the higher orders are &lt;a href=&quot;https://www.youtube.com/watch?v=SxdOUGdseq4&quot;&gt;simpler (but mostly not easier)&lt;/a&gt;, due to only being linear equations.&lt;/p&gt;

&lt;h3 id=&quot;varepsilon&quot;&gt;$\varepsilon$&lt;/h3&gt;
&lt;p&gt;We have $2x_0x_1+x_0=0$. Solving for $x_1$ we get 
\(x_1 = -\frac{1}{2x_0}x_0=-\frac{1}{2}\).&lt;/p&gt;

&lt;h3 id=&quot;varepsilon2&quot;&gt;$\varepsilon^2$&lt;/h3&gt;
&lt;p&gt;We have $2x_0x_2+x_1^2+x_1$ and solving for $x_2$ we get
\(x_2 = -\frac{1}{2x_0}(x_1^2+x_1)\).&lt;/p&gt;

&lt;p&gt;Now we have a small problem. There are still variables in our formula for $x_2$. Luckily (and by construction) we already know $x_0$ and $x_1$ and can plug them in. So with easy calculation $x_2 = \frac{1}{8}$.&lt;/p&gt;

&lt;h3 id=&quot;varepsilon3&quot;&gt;$\varepsilon^3$&lt;/h3&gt;
&lt;p&gt;\(2x_0x_3+2x_1x_2+x_2 \Rightarrow x_3 = -\frac{1}{2x_0}(2x_1x_2+x_2)=-\frac{1}{32}\)&lt;/p&gt;

&lt;h3 id=&quot;higher-orders&quot;&gt;Higher orders&lt;/h3&gt;
&lt;p&gt;We can continue this schema as long as we want. The equations get increasingly longer but stay in principle simple.&lt;/p&gt;

&lt;h3 id=&quot;summing-up-the-series&quot;&gt;Summing up the series&lt;/h3&gt;
&lt;p&gt;So I calculated two more orders before I got bored. Let’s plug them into our definition of the power series again:&lt;/p&gt;

\[x = 1-\frac{1}{2}\varepsilon+\frac{1}{8}\varepsilon^2-\frac{1}{32}\varepsilon^3
+0\varepsilon^4+\frac{1}{512}\varepsilon^5+\mathcal O (\varepsilon^6)\]

&lt;p&gt;To get an approximation we just ignore all orders we did not calculate:&lt;/p&gt;

\[x \approx 1-\frac{1}{2}\varepsilon+\frac{1}{8}\varepsilon^2-\frac{1}{32}\varepsilon^3
+0\varepsilon^4+\frac{1}{512}\varepsilon^5\]

&lt;p&gt;&lt;img src=&quot;/assets/images/quadratic-equation-perturbation/simple.svg&quot; alt=&quot;Plot of the function&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So for $\varepsilon$ smaller $1$ our approximation works out well and gets worse for larger $\varepsilon$. We assumed $\varepsilon$ is small at the beginning so all this is as expected.&lt;/p&gt;

&lt;p&gt;If we look at the limits, at $\varepsilon\rightarrow0$ everything fits, and at $\varepsilon\rightarrow\infty$ our series diverges.&lt;/p&gt;

&lt;p&gt;This is all rather well-known. If you have some formal training in quantum physics or advanced ordinary differential equations you will have heard this before. In the next part, I will show you something quite similar, but it looks funny and it is a little surprising that it works.&lt;/p&gt;

&lt;h2 id=&quot;pertrubation-theory-for-large-varepsilon&quot;&gt;Pertrubation theory for large $\varepsilon$&lt;/h2&gt;
&lt;p&gt;In the last step, we looked at a power series that works well for $\varepsilon$ small. Now let us construct a power series that works well when $\varepsilon$ becomes very large. In this case, $\varepsilon^{-1}=\frac{1}{\varepsilon}$ becomes very small, so let’s use it for a series:
\(x=\sum_{n=0}^{\infty}x_n\varepsilon^{-n}\)&lt;/p&gt;

&lt;p&gt;We can again plug this into our quadratic equation and look at the resulting powers of $\varepsilon$.&lt;/p&gt;

&lt;h3 id=&quot;varepsilon-1&quot;&gt;$\varepsilon$&lt;/h3&gt;
&lt;p&gt;Somewhat suprisingly we still have a positive power of $\varepsilon$ (from the term with $\varepsilon$ in the equation):
\(x_0=0\)&lt;/p&gt;

&lt;p&gt;Nothing to do here. Turns out we could have started our series at $n=1$.&lt;/p&gt;

&lt;h3 id=&quot;varepsilon01-1&quot;&gt;$\varepsilon^0=1$&lt;/h3&gt;
&lt;p&gt;We get $x_0^2+x_1-1=0$, so $x_1=1$.&lt;/p&gt;

&lt;p&gt;Another surprise is that we don’t get to choose between two solutions this time. I think this could be because the smaller solution diverges at infinity. &lt;a href=&quot;https://www.youtube.com/watch?v=9PYgCN2kIsg&quot;&gt;If you work it out tell me what you find&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;varepsilon-1&quot;&gt;$\varepsilon^{-1}$&lt;/h3&gt;
&lt;p&gt;We get $2x_0x_1+x_2=0$ and so $x_2=0$. It turns out all even order terms in the series are zero. If you don’t believe me, try to prove it by induction. It is not that hard, but messy to write down correctly.&lt;/p&gt;

&lt;h3 id=&quot;varepsilon-2&quot;&gt;$\varepsilon^{-2}$&lt;/h3&gt;
&lt;p&gt;Now we get to the first order without a surprise. The equation is $2x_0x_2+x_1^2+x_3=0 \Rightarrow x_3 = -x_1^2 = -1$. Now we can go on for the higher(lower) orders.&lt;/p&gt;

&lt;h3 id=&quot;summing-up&quot;&gt;Summing up&lt;/h3&gt;
&lt;p&gt;With some more orders, I got&lt;/p&gt;

&lt;p&gt;\(x = \varepsilon^{-1}-\varepsilon^{-3}+3\varepsilon^{-5}\).&lt;/p&gt;

&lt;p&gt;In most cases, it is a very bad omen, when the coefficients in a power series increase, but it is not that bad in this case. For $\epsilon \rightarrow 0$ this diverges badly anyway.&lt;/p&gt;

&lt;p&gt;Due to $x_0=0$, this converges at $\varepsilon\rightarrow\infty$ to $0$ as it should.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/quadratic-equation-perturbation/both.svg&quot; alt=&quot;Plot of the function&quot; /&gt;
If we plot this function, as expected it works well for large $\varepsilon$ (in this case larger than $3$) and does not work for small values.&lt;/p&gt;

&lt;h2 id=&quot;the-strange-case-of-varepsilonapprox-1&quot;&gt;The strange case of $\varepsilon\approx 1$&lt;/h2&gt;

&lt;p&gt;So we found a good approximation for small and large $\varepsilon$ with simple calculations, but they both break down at $\varepsilon=1$. That seems strange to me. After some months of thought, I have not found something similar that works there.&lt;/p&gt;

&lt;p&gt;Of cause, you can calculate more orders and the approximation at $1$ will become good enough for practical applications.&lt;/p&gt;

&lt;p&gt;You might try a series of the form
\(x = \sum_{n=0}^\infty x_n(\varepsilon-1)^n\),
but this looks so ugly to me, that I did not even want to work it out for fun.&lt;sup id=&quot;fnref:3&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:3&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;h2 id=&quot;is-this-useful&quot;&gt;Is this useful?&lt;/h2&gt;

&lt;p&gt;Not really for quadratic (or other algebraic) equations.&lt;/p&gt;

&lt;p&gt;The principle for small $\varepsilon$ is used in &lt;a href=&quot;https://en.wikipedia.org/wiki/Feynman_diagram&quot;&gt;many calculations in high energy physics&lt;/a&gt;. You can think of this quadratic equation as one of the simplest possible model systems for the method.&lt;/p&gt;

&lt;p&gt;If one could work out the the method for large $\varepsilon$ for quantum mechanics, it could possibly help for some quantum-systems with &lt;a href=&quot;https://en.wikipedia.org/wiki/Coupling_constant#Weak_and_strong_coupling&quot;&gt;strong coupling&lt;/a&gt;. Maybe.&lt;/p&gt;

&lt;hr /&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Don’t be confused: the names of the variables look kind of strange because we already used $x$ and $\varepsilon$ &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;For $\varepsilon \ge 0$&lt;/p&gt;

\[\begin{aligned}
  &amp;amp;x&apos;(\varepsilon) = \frac{1}{2}\big(\frac{\varepsilon}{\sqrt{\varepsilon^2+4}}-1\big) &amp;lt; 0\\
  \Leftrightarrow &amp;amp; \frac{\varepsilon}{\sqrt{\varepsilon^2+4}} &amp;lt; 1\\
  \Leftrightarrow &amp;amp; \varepsilon &amp;lt; \sqrt{\varepsilon^2+4}
  \end{aligned}\]

      &lt;p&gt;QED &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:3&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;I think it would be beautiful if you can make the combined series
  \(\sum_{n=-\infty}^{\infty}a_n\varepsilon^n\)
  work, but I just get an infinite number of terms for each order. &lt;a href=&quot;#fnref:3&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</content><author><name></name></author><summary type="html">Solving the equation directly Let’s look at the equation \(x^2+\varepsilon x=1\) We can rewrite it as \(x^2+\varepsilon x-1=0\) for convenience. As with all quadratic equations, this equation has two solutions. Luckily both solutions of our equation are real numbers for any $\varepsilon$.</summary></entry><entry><title type="html">Interpolation on earth surface (far from the dateline)</title><link href="https://xn--andreasvlker-cjb.de/2022/10/27/interpolation-on-earth/" rel="alternate" type="text/html" title="Interpolation on earth surface (far from the dateline)" /><published>2022-10-27T07:00:11+02:00</published><updated>2022-10-27T07:00:11+02:00</updated><id>https://xn--andreasvlker-cjb.de/2022/10/27/interpolation-on-earth</id><content type="html" xml:base="https://xn--andreasvlker-cjb.de/2022/10/27/interpolation-on-earth/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;If you want to get the current temperature you could go outside, but you can also approximate it with interpolation. All over the world are public weather stations where you can get the current temperature there.&lt;/p&gt;

&lt;p&gt;You could just take the nearest to your home and believe it, which often is fine, but let’s do better anyway. You can take the ones around you and use the average of them. Even better you weigh them and take give the values nearer you more relevance. In other words: You interpolate their temperatures at your current location.&lt;/p&gt;

&lt;p&gt;In this article, I will discuss how you figure out which stations are around you and how you weigh them and how you can do that kind of fast with a computer.&lt;/p&gt;

&lt;p&gt;“Around you” is surprisingly hard to define and figure out. The simplest way is to take the k nearest stations, but this does ignore a central problem: occlusion. If there are two information sources in a line from you, you always only want to look at the first of them and the farther behind and occluded ones should almost always be ignored.&lt;/p&gt;

&lt;p&gt;Now the title of this post says we only do interpolation far from the date line. As you almost surely know a point on the earth is often addressed with a latitude and longitude which are basically angles. As always with angles they skip somewhere from 360 degrees back to 0 degrees. On the earth, this is defined to happen in the region of the pacific. This is messy to handle and you don’t always need it. For example, I work with the data from the Germanys public weather service which is only really good inside of Germany and Germany is very far from the pacific. Luckily Germany also does not have colonies anymore.&lt;/p&gt;

&lt;h2 id=&quot;why-not-do-natural-neighbor-interpolation&quot;&gt;Why not do natural neighbor interpolation&lt;/h2&gt;

&lt;p&gt;(You can safely skip this paragraph if you don’t know what natural neighbor interpolation is or you know what it is and don’t like it at all)&lt;/p&gt;

&lt;p&gt;If you have ever looked at multidimensional interpolation you heard of &lt;a href=&quot;https://en.wikipedia.org/wiki/Natural_neighbor_interpolation&quot;&gt;natural neighbor interpolation&lt;/a&gt;. This is a really clever algorithm based on Voronoi diagrams. It automatically handles occlusion and it gives a kind of natural way to get the weights without having to mess around.&lt;/p&gt;

&lt;p&gt;I think it is a brilliant idea and I don’t like it at all. I collaborated on a based on this principle some time ago and I don’t like it very much anymore. This algorithm is horrible to implement fast and even then it is still slow. It is a mess of numerical problems if points have too much symmetry. And implementation is only the smallest problem.&lt;/p&gt;

&lt;p&gt;Naturalness is very nice because you don’t have to tune any parameters. Until your and the algorithm’s definition of naturalness is not the same. Then you want to tune parameters and you can’t. For example, on earth, the distance on the surface is not the same as the distance between the coordinates as vectors. There is no direct way to make this work with natural neighbor interpolation.&lt;/p&gt;

&lt;p&gt;Someone clever might figure out how to make this work for this special case. I’m sure that will make the algorithm even more horrible to implement efficiently and you can’t reuse it for the next modification. For example, you might want to include the different elevations of the weather stations, because it turns out the temperature on the next mountain is not that applicable in the valley below. Let us use something else.&lt;/p&gt;

&lt;h2 id=&quot;getting-the-weights&quot;&gt;Getting the weights&lt;/h2&gt;

&lt;p&gt;Getting the weights between the at a target location from the source stations is easy from their distance. For any station, you take the distance and apply a decreasing function to it.&lt;/p&gt;

&lt;p&gt;Classically you use a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;weight[i] = 1/pow(distance[i], e)&lt;/code&gt; for some exponent &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e&lt;/code&gt; which is usually at least 2&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;. Now the sum of all weights is not 1 as it should be so you have to calculate their sum and divide each weight by the sum.&lt;/p&gt;

&lt;p&gt;In swift, this could look like the following&lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;interpolate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;datapoints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;distances&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;weights&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;distances&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;weightSum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;normalizedWeights&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weightSum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;interpolated&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;normalizedWeights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datapoints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;interpolated&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can use any decreasing function of the distance. I kind of like a shifted and mirrored &lt;a href=&quot;https://en.wikipedia.org/wiki/Sigmoid_function&quot;&gt;sigmoid functions&lt;/a&gt;, but it is fun and easy to try different functions that come to your mind.&lt;/p&gt;

&lt;p&gt;We can also put anything else into this formula. For example, we might add 100m more distance for every 1m of elevation difference.&lt;/p&gt;

&lt;p&gt;To get the distance it might work fine to ignore the curvature of the earth and assume longitude and latitude are a flat grid&lt;sup id=&quot;fnref:3&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:3&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; and take their distance with the Pythagoras theorem. If your stations are near enough this works fine, but we can do better and use the real distance on the earth’s surface. This is easy to calculate by using the (Haversine formula)[https://rosettacode.org/wiki/Haversine_formula]&lt;sup id=&quot;fnref:4&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:4&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;h2 id=&quot;making-it-faster-with-nearest-neighbors&quot;&gt;Making it faster with nearest neighbors&lt;/h2&gt;

&lt;p&gt;So until now, we look at every station in (potentially) the world and this sounds like a lot of useless work. So let us just get the nearest ones. In most cases &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k=5-10&lt;/code&gt; stations are enough.&lt;/p&gt;

&lt;p&gt;A simple way to do this is to loop through all stations and put them into a max-heap ordered by the distance. Whenever the heap contains more than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt; values you can remove the most distant one to keep the heap small and efficient. So in the end the heap will contain the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt; nearest stations.&lt;/p&gt;

&lt;p&gt;This can be done much more efficiently by using bounding volume hierarchys&lt;sup id=&quot;fnref:5&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:5&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;. If the heap contains at least &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt; element its max is always the farthest a relevant point can ever be. We can use its distance to prune large parts of the bounding volume hierarchy.&lt;/p&gt;

&lt;p&gt;We could use the correct distance, but this will get messy real fast when we want to use the bounding volume hierarchy. Just use the simple Pythagorean distance!&lt;/p&gt;

&lt;p&gt;The simplest implementation of this might look something like this:&lt;/p&gt;
&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;KNN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Vec2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sources&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Station&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Station&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;heap&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BinaryHeap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Station&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;withComparision&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())})&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;point&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sources&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;heap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;heap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;extract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Station&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;heap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;extract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;occlusion&quot;&gt;Occlusion&lt;/h2&gt;

&lt;p&gt;Now we got a few nearest points left we can do occlusion. So let’s just do the simplest thing possible. For each point we define an area behind it, that is occluded from the view of our target location. So for every station, we check which other stations are in the occluded area and throw them away.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/interpolation-on-earth/OccluderDiagram.png&quot; alt=&quot;Occlusion&quot; width=&quot;500px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We same basic linear algebra and trigonometry you can work this out and you might find the following:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;func isOccluded(from source: Vec2, occluder: Vec2, occluded: Vec2, openingAngle: Double = 45) -&amp;gt; Bool{
    let limit = cos(openingAngle*Double.pi/180)
    if occluded == occluded{
        return false
    }
    let normal = (occluder-source).normalize()
    let cos = normal.dot(occluded-occluder)/Vec2.distance(occluder, occluded)
    return cos &amp;gt; limit
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we can check each pair of stations and reject all that is occluded in any way and we are done. Because we only have a few nearest stations this is fast enough for most practical applications and we don’t need to think of a more clever algorithm to do this.&lt;/p&gt;

&lt;p&gt;I use an opening angle of 45°, but you might tune this to your liking and application.&lt;/p&gt;

&lt;h2 id=&quot;how-to-incorporate-some-curvature&quot;&gt;How to incorporate some curvature&lt;/h2&gt;

&lt;p&gt;Until now we ignored the curvature of the earth for the most part, but this is simple enough to fix.&lt;/p&gt;

&lt;p&gt;We can take something like 30% more nearest neighbors than we really want. Now we sort these by the correct surface distance and only take the nearest &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt; of them. So after that, we have almost surely the nearest points considering curvature with very little more work.&lt;/p&gt;

&lt;h2 id=&quot;review&quot;&gt;Review&lt;/h2&gt;

&lt;p&gt;So let us review what we do&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Think of a target point for our interpolation&lt;/li&gt;
  &lt;li&gt;Find a little more than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt; nearest stations with some approximations maybe with the help of a bounding volume hierarchy&lt;/li&gt;
  &lt;li&gt;Select the real nearest &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt; from them&lt;/li&gt;
  &lt;li&gt;Check which ones are occluded by others and ignore them&lt;/li&gt;
  &lt;li&gt;Calculate the weights from the distance to the target point and make them sum up to 1&lt;/li&gt;
  &lt;li&gt;Get the weighted average of the temperature&lt;/li&gt;
  &lt;li&gt;Go for a walk with temperature adequate cloth&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This obviously works for any other positions on earth than stations and any other quantity than temperature.&lt;/p&gt;

&lt;h2 id=&quot;how-to-work-near-the-dateline&quot;&gt;How to work near the dateline&lt;/h2&gt;

&lt;p&gt;So this only works when the latitude and longitude are near enough to normal rectangular coordinates. This definitely does not hold near the date line. So what to do there?&lt;/p&gt;

&lt;p&gt;The trick is not to use latitude and longitude. We can simply use a 3D coordinate system for the earth which are always well behaved. It is simple to &lt;a href=&quot;https://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates&quot;&gt;calculate the positions in real space&lt;/a&gt; and it turns out bounding volume hierarchies and our occlusion code generalizes without any problems to 3D. It only gets a little slower to calculate.&lt;/p&gt;

&lt;p&gt;For the calculation of the weights, we might want to go back to the latitude and longitude because that formula works perfectly well with the date line.&lt;/p&gt;

&lt;hr /&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;This is called &lt;a href=&quot;https://en.wikipedia.org/wiki/Inverse_distance_weighting&quot;&gt;Shepard’s method&lt;/a&gt;. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;For this to work sum must be defined something like this&lt;/p&gt;
      &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Sequence&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BinaryFloatingPoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;      &lt;/div&gt;
      &lt;p&gt;&lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:3&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;This is basically a &lt;a href=&quot;https://en.wikipedia.org/wiki/Mercator_projection&quot;&gt;Mercator projection&lt;/a&gt;, which is often used for world maps. &lt;a href=&quot;#fnref:3&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:4&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Vincenty%27s_formulae&quot;&gt;Vincenty’s formula&lt;/a&gt; is even more precise and considers that the earth is more an ellipsoid and not a perfect sphere. I’m pretty sure you don’t need this for interpolation. &lt;a href=&quot;#fnref:4&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:5&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://jacco.ompf2.com/2022/04/13/how-to-build-a-bvh-part-1-basics/&quot;&gt;This article series&lt;/a&gt; is a really good overview for conventional bounding volume hierarchies. I like to do this rather differently and will try to write it down one day. &lt;a href=&quot;#fnref:5&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</content><author><name></name></author><summary type="html">Introduction</summary></entry><entry><title type="html">SI model stability with unusual perturbation theory</title><link href="https://xn--andreasvlker-cjb.de/2022/04/12/stability-perturbation/" rel="alternate" type="text/html" title="SI model stability with unusual perturbation theory" /><published>2022-04-12T07:00:11+02:00</published><updated>2022-04-12T07:00:11+02:00</updated><id>https://xn--andreasvlker-cjb.de/2022/04/12/stability-perturbation</id><content type="html" xml:base="https://xn--andreasvlker-cjb.de/2022/04/12/stability-perturbation/">&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Compartmental_models_in_epidemiology#The_SIR_model&quot;&gt;SI model&lt;/a&gt; is one of the simplest models
to describe the spread of infectious disease using differential equations. I figured out a
funny way to look at its steady states. It reproduces the well-known and fairly obvious result.&lt;/p&gt;

&lt;h2 id=&quot;the-model&quot;&gt;The model&lt;/h2&gt;

&lt;p&gt;The model is defined by the equations&lt;/p&gt;

\[\begin{aligned}
\dot I(t) &amp;amp;= \beta S(t) I(t) - \gamma I(t)\\
\dot S(t) &amp;amp;= -\beta S(t) I(t).
\end{aligned}\]

&lt;p&gt;where $S(t) \in [0,1]$ is the still suseptible part of the population and $I(t) \in [0, 1]$ the currently infected part. 
$\beta &amp;gt;0$ and $\gamma&amp;gt;0$ are constants for the rates of infection and resolution of infections.&lt;/p&gt;

&lt;p&gt;The idea is that the rate of new infections is proportional to the chance of a susceptible individual meeting an infected individual.
We assume the infected are only infectious for some time and shrink exponentially in absence of new infections.
This is a really basic model that ignores reinfections, interventions, inhomogeneous populations, spatial dynamics…&lt;/p&gt;

&lt;p&gt;As with almost any nonlinear equation, there is no practical general solution, so lets
at least look at the steady states of the system.&lt;/p&gt;

&lt;h2 id=&quot;steady-states-and-their-stability&quot;&gt;Steady states and their stability&lt;/h2&gt;

&lt;p&gt;The obvious steady state for this model is $S=\bar S$ arbitrary and $I=0$. If there are no infections nobody
else can get infected. The slightly unusual thing, in this case, is, that there are an infinite number of
dense steady states.&lt;/p&gt;

&lt;p&gt;Now we will try to see if these are stable, i.e. if a little perturbation shrinks back to zero or explodes.
The usual tool for this is &lt;a href=&quot;https://en.wikipedia.org/wiki/Linear_stability&quot;&gt;linear stability analysis&lt;/a&gt;. It looks at the eigenvalues of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Jacobian_matrix_and_determinant&quot;&gt;jacobian&lt;/a&gt; so let’s calculate it for the steady states:&lt;/p&gt;

\[J = \begin{bmatrix}\beta \bar S &amp;amp; \gamma\\\\ 0 &amp;amp; 0\end{bmatrix}\]

&lt;p&gt;The last row of this matrix is $0$ and this implies that $0$ is one of its eigenvalues. 
Sadly, in this case, linear stability analysis can’t make any conclusion about the stability so we need another tool: perturbation theory.&lt;/p&gt;

&lt;h2 id=&quot;perturbation-theory&quot;&gt;Perturbation theory&lt;/h2&gt;

&lt;p&gt;The idea of &lt;a href=&quot;https://pirsa.org/speaker/Carl-Bender&quot;&gt;perturbation theory&lt;/a&gt; is to transform a hard problem into a
infinite series of easy problems and get an approximation to arbitrary precision by solving these.
As is usual we insert a small parameter $\varepsilon$ into our system
and develop the solution as power series of it. 
So let’s do this:&lt;/p&gt;

\[\begin{aligned}
I(t) &amp;amp;= \sum_{i=0}^{\infty} \varepsilon^n I_n(t)\\
S(t) &amp;amp;= \sum_{i=0}^{\infty} \varepsilon^n S_n(t)
\end{aligned}\]

&lt;p&gt;Now we can plug these into our equations:&lt;/p&gt;

\[\begin{aligned}
\dot I_0 +\varepsilon \dot I_1+  \cdots &amp;amp;= \beta (S_0+\varepsilon S_1+\dots)(I_0+\varepsilon I_1+\dots)-\gamma(I_0+\varepsilon I_1+\dots) \\ &amp;amp;=\beta S_0I_0-\gamma I_0+\varepsilon(\beta I_0 S_1+\beta I_1 S_0-\gamma I_1)+\dots\\
\dot S_0 +\varepsilon \dot S_1+ \dots &amp;amp;= -\beta (S_0+\varepsilon S_1+\dots)(I_0+\varepsilon I_1+\dots) \\
 &amp;amp;= -\beta S_0I_0+\varepsilon(\beta I_0 S_1+\beta I_1 S_0)+\dots
\end{aligned}\]

&lt;p&gt;If you have some experience with perturbation theory you might think we should have put an $\varepsilon$ into our equations.
Even worse: if we look at all terms without an $\varepsilon$ you find we still have the old equations from before
and we still don’t know how to solve them.&lt;/p&gt;

&lt;p&gt;Now here comes my unusual trick: We put an $\varepsilon$ into the initial condition of the equations and set $S(0) = \bar S$ 
and $I(0) = \varepsilon \bar I$ with an arbitrary (and in the end boring) $\bar I$. Now let’s solve our system by order!&lt;/p&gt;

&lt;h3 id=&quot;varepsilon0-order&quot;&gt;$\varepsilon^0$ Order&lt;/h3&gt;

&lt;p&gt;If we collect everything without an $\varepsilon$ we get:&lt;/p&gt;

\[\begin{aligned}
\dot I_0 &amp;amp;= \beta S_0 I_0 - \gamma I_0 \\
\dot S_0 &amp;amp;= -\beta S I_0 \\
S_0(0) &amp;amp;= \bar S \text{ and }  I_0(0) = 0 \\
\end{aligned}\]

&lt;p&gt;Now if we look at the first equation we see an $I_0$ in every term and with the initial condition it turns out this equation
can be trivially be solved as&lt;/p&gt;

\[I_0(t) = 0.\]

&lt;p&gt;Plugging this in the second equation the right side becomes zero and so the solution of the equation is a constant.
With the initial condition, we directly get&lt;/p&gt;

\[S_0(t) = \bar S.\]

&lt;p&gt;By solving the 0th order we basically showed that the steady-state is steady.&lt;/p&gt;

&lt;h3 id=&quot;varepsilon1-order&quot;&gt;$\varepsilon^1$ order&lt;/h3&gt;

&lt;p&gt;Now we come to the interesting terms. Collecting everything with $\varepsilon$ we get&lt;/p&gt;

\[\begin{aligned}
\dot I_1 &amp;amp;= \beta S_0 I_1 + \beta S_1 I_0 -\gamma I_1= (\beta \bar S-\gamma) I_1 \\
\dot S_1 &amp;amp;= -\beta S_0 I_1 - \beta S_1 I_0 = \beta \bar S I_1 \\
S_1(0) &amp;amp;= 0 \text{ and }  I_1(0) = \bar I
\end{aligned}\]

&lt;p&gt;So the first equation is linear, homogeneous with constant coefficients and the solution is well known and easy to check.
When plugging in the initial condition we get&lt;/p&gt;

\[I_1(t) = \bar I e^{(\beta\bar S-\gamma) t}\]

&lt;p&gt;If we plug this into the second equation it becomes an integrable equation:&lt;/p&gt;

\[\dot S_1 = -\beta \bar S \bar I e^{(\beta\bar S-\gamma) t} \Rightarrow S_1 = -\beta \bar I \bar S \int dt\, e^{(\beta\bar S-\gamma) t}\]

&lt;p&gt;and with the initial condition, we get&lt;/p&gt;

\[S_1(t) = -\frac{\beta \bar I \bar S}{\beta\bar S-\gamma}\left(e^{(\beta\bar S-\gamma) t}-1\right).\]

&lt;h3 id=&quot;higher-orders&quot;&gt;Higher orders&lt;/h3&gt;

&lt;p&gt;With the same principle we always get the equations for each order and could in principle solve them fairly easily (using the substitution $I_n(t) = \tilde I(t) e^{(\beta \bar S-\gamma)t}$). 
This gets increasingly messy and I have not figured out the general form of them.
But with some thought, we can see $I_N$ und $S_N$ have the form&lt;/p&gt;

\[\sum_{n=0}^N c_n e^{n(\beta \bar S-\gamma) t}\]

&lt;p&gt;with constants $c_n$. This means these higher terms have similar but faster growth/shrink behavior as the first orders.&lt;/p&gt;

&lt;p&gt;Now a mathematician should check the radius of convergence which is not obvious at all. 
Luckily I come from a physics background and can ignore such “details”.&lt;/p&gt;

&lt;h2 id=&quot;what-that-means-for-stability&quot;&gt;What that means for stability&lt;/h2&gt;

&lt;p&gt;So let’s collect what we figured out:&lt;/p&gt;

\[\begin{aligned}
I(t) &amp;amp;= \varepsilon \bar I e^{(\beta\bar S-\gamma) t} +\mathcal O (\varepsilon^2)\\
S(t) &amp;amp;= \bar S -\varepsilon\frac{\beta \bar I \bar S}{\beta\bar S-\gamma}\left(e^{(\beta\bar S-\gamma) t}-1\right)+\mathcal O (\varepsilon^2)
\end{aligned}\]

&lt;p&gt;Looking at the exponential function they shrink exactly if $\beta \bar S-\gamma &amp;lt; 0 \Leftrightarrow \bar S &amp;lt; \frac{\gamma}{\beta}=:\frac{1}{R_0}$. If the sign is flipped the number of infections explodes. The newly defined $R_0$ is these days fairly well known as the &lt;a href=&quot;https://en.wikipedia.org/wiki/Basic_reproduction_number&quot;&gt;basic reproduction rate&lt;/a&gt;. Our condition reproduces exactly the condition for herd immunity, which is nice.&lt;/p&gt;

&lt;p&gt;Going back to our equation we can see if this condition holds infection fall back to zero and it seems to be stable. 
But we also need to look at the stability of the susceptible population $S(t)$. 
Turns out it does not return to $\bar S$ and shrinks somewhat. This is not compatible with the definition of stability!&lt;/p&gt;

&lt;p&gt;So in the classical sense there exist no stable steady states. But above the herd immunity threshold, small perturbations are fairly inconsequential and only move us a little down the line of steady states. For practical concerns, I would call this stable.&lt;/p&gt;

&lt;h2 id=&quot;checking-with-numerics&quot;&gt;Checking with numerics&lt;/h2&gt;

&lt;p&gt;Now we got a solution we should check if this first-order works at all.
The model is easy to solve with numerics &lt;a href=&quot;https://gist.github.com/a-voel/3dfb7acd5e65606bd51a1c148650ae36&quot;&gt;(Sourcecode)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Overall it seems to work well, if the exponent $E = \beta \bar S - \gamma &amp;lt;0 $ and $\bar I$ is smaller. If this gets near $0$ there
are significant deviations. The explanation for this is, that with a big $E$ or $\bar I$ the changes of $S(t)$ are bigger
and the first order basically assumes $S(t) = \bar S$. This might improve in the higher orders.&lt;/p&gt;

&lt;h3 id=&quot;e-002-bar-i0001&quot;&gt;$E=-0.02$ $\bar I=0.001$&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/stability-perturbation/peturbation.svg&quot; width=&quot;550px&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;e-002-bar-i0001-1&quot;&gt;$E=-0.02$ $\bar I=0.001$&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/stability-perturbation/peturbation2.svg&quot; width=&quot;550px&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In some cases, you can do perturbation theory by putting your perturbation constant $\varepsilon$ only into the initial conditions.
To my astonishment, it solves the equation, does not produce contradictions, and is in this case the most straightforward 
method to show and analyze the stability I found.&lt;/p&gt;

&lt;p&gt;The great thing with perturbation theory is you can put an $\varepsilon$ in all kinds of creative places and see what happens
and sometimes it even works out.&lt;/p&gt;</content><author><name></name></author><summary type="html">The SI model is one of the simplest models to describe the spread of infectious disease using differential equations. I figured out a funny way to look at its steady states. It reproduces the well-known and fairly obvious result.</summary></entry><entry><title type="html">Writing scientific software by yourself</title><link href="https://xn--andreasvlker-cjb.de/2021/07/25/writing-scientific-software/" rel="alternate" type="text/html" title="Writing scientific software by yourself" /><published>2021-07-25T12:00:11+02:00</published><updated>2021-07-25T12:00:11+02:00</updated><id>https://xn--andreasvlker-cjb.de/2021/07/25/writing-scientific-software</id><content type="html" xml:base="https://xn--andreasvlker-cjb.de/2021/07/25/writing-scientific-software/">&lt;p&gt;Today many scientists have problems that are solved by developing new programs. Sadly most of them are not software developers,
so the quality of the resulting programs is mostly up to luck and individual interest. They mostly only have to take
basic courses into programming by other scientists. Often the professors mostly teach from books because they only wrote basic programs 30 years ago in their own Ph.D. time.&lt;/p&gt;

&lt;p&gt;I was in the situation to write some software for theoretical physics, except I studied computer science before and did some
work as a programmer on a larger project. In this article I will write about some ideas and techniques I found along the way.&lt;/p&gt;

&lt;p&gt;I will only look at programs written by a single researcher by himself, as it was done in my community. Projects with many researchers
and distributed to end-users are a much more complex problem. I will only look at program development, not project management.&lt;/p&gt;

&lt;h2 id=&quot;why-care-about-this&quot;&gt;Why care about this?&lt;/h2&gt;
&lt;p&gt;Scientists might ask why they should care. They might have some Fortran files they copied from someone else and mess around in till it mostly works. They are here to do science, not software. They might even dismiss their program as mere “codes”.
 I will try to give some reasons you
might want to care.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://science.sciencemag.org/content/338/6113/1426&quot;&gt;In this linked article&lt;/a&gt;, Freeman Dyson discusses if tools are as important as ideas to science.
Even if you disagree, it makes clear that tools are important and programs are clearly tools for science.&lt;/li&gt;
  &lt;li&gt;The programs are a relevant result of the research. They often contain all the messy details and tricks left out in articles and I think they should even be published with the articles. Sadly not even computer science does that so it might take decades for natural sciences to get there.&lt;/li&gt;
  &lt;li&gt;You will be faster and more comfortable using and modifying your programs for your research.&lt;/li&gt;
  &lt;li&gt;You will be (a little) more confident in your programs if they are less messy.&lt;/li&gt;
  &lt;li&gt;These days there are many jobs involving software if you ever leave academia.&lt;/li&gt;
  &lt;li&gt;Making better programs is fun.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;an-example-of-a-program&quot;&gt;An example of a program&lt;/h2&gt;
&lt;p&gt;To make the kind of programs discussed here more clear I will explain my own research. I look
at the time evolution of electrons in quantum wires described by differential equations. There
were multiple numerical schemas to solve these equations and many ways to combine them with
different models of electrodynamics. At first, I looked at some aspects only in 1D systems and
later extended them to 2D. These simulations take hours of calculation time and produced up to
a few gigabytes of raw data.&lt;/p&gt;

&lt;p&gt;The next step was a program to analyze the raw data and figure out how to get the relevant
information. I needed to do much experimentation to figure out what I could get out of the data.
Here every run might take a few minutes and output less than a megabyte of data.&lt;/p&gt;

&lt;p&gt;The last step is to take the results and make good-looking plots with clear labeling. This takes
a few seconds and does not contain any ideas relevant to physics.&lt;/p&gt;

&lt;h2 id=&quot;infrastructure&quot;&gt;Infrastructure&lt;/h2&gt;
&lt;p&gt;Let’s begin with something seemingly boring, that everyone working in software knows anyway. There is some
infrastructure around your programming. Let’s look at some stuff.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Programming language&lt;/strong&gt;: Look at the programming language you use. Spend some hours to learn what it can do. There are usually good tutorials online on their official website.
I use &lt;a href=&quot;https://isocpp.org/&quot;&gt;C++&lt;/a&gt; for the computationally complex part and &lt;a href=&quot;https://www.python.org/&quot;&gt;python&lt;/a&gt; for the rest because I know them well.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Text editor&lt;/strong&gt;: You will spend much time programming in a text editor or integrated development environment. Spend an hour to test a few of them and stay with the one you like. Don’t blindly use what everyone else in your group uses; it might be worse than &lt;a href=&quot;https://en.wikipedia.org/wiki/Turbo_Pascal&quot;&gt;what was used in the 90s&lt;/a&gt;. I like &lt;a href=&quot;https://code.visualstudio.com/&quot;&gt;Visual Studio Code&lt;/a&gt; because it works on most operating systems and has many cool features through plugins.&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Version control systems&lt;/strong&gt;: Every halfway reasonable software developer uses version control to put his software into and to have access to all previous changes. You should also do this. I think you should use &lt;a href=&quot;https://git-scm.com/&quot;&gt;git&lt;/a&gt;. It is the industry standard, it works and it is easy to get help with it.
There are even multiple graphical interfaces to use git, but I don’t know much about them.&lt;/p&gt;

    &lt;p&gt;Git allows you to have a remote server to sync with. This is especially useful if you work from different computers on the same program. You can check if your university provides such a server or use a (free) commercial service like &lt;a href=&quot;https://github.com/&quot;&gt;github&lt;/a&gt;. It might even work as (hopefully another) backup.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Build system&lt;/strong&gt;: You probably need to compile your software. Coping the command to do that from the first line of your source file or your shell history is a bad idea.
You should use a system for that as everyone else in software does for 40 years. At least use &lt;a href=&quot;https://en.wikipedia.org/wiki/Make_(software)&quot;&gt;Make&lt;/a&gt; or some shell script.&lt;/p&gt;

    &lt;p&gt;I use &lt;a href=&quot;https://cmake.org/&quot;&gt;CMake&lt;/a&gt; for my C++ programs. It is very flexible, works fine, and even has integration into my editor.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compilers&lt;/strong&gt;: If there are multiple good compilers for your programming language make your code work with more than one of them. 
This usually finds bugs in your programs and improves the chances that they conform to standards and will continue to work in a few years or on the cluster of your organization. I made sure my programs work with GCC and Clang on Linux and Visual C++ on Windows.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I know all of this sounds like tedious work, but it is all useful. You will see it in the end!&lt;/p&gt;

&lt;h2 id=&quot;modules&quot;&gt;Modules&lt;/h2&gt;
&lt;p&gt;It is good practice to split the software into modules and reuse these. The idea is to have the components in a library with well-structured implementations and use these to
build multiple programs. This prevents copying of code and enforces good interfaces.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/writing-scientific-software/diagram2.svg&quot; width=&quot;500px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In the diagram above you can see a library &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;simcore&lt;/code&gt;, that is used by multiple executables in the row below in my project. This library contains&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Multiple implementations of physical systems with 
different numerical implementation or dimensionality&lt;/li&gt;
  &lt;li&gt;Basic mathematical helper functions an&lt;/li&gt;
  &lt;li&gt;Many predefined laser pulses&lt;/li&gt;
  &lt;li&gt;Helpers for file output&lt;/li&gt;
  &lt;li&gt;…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The library has a few 1000 lines of code, while all the binaries are below 200 lines of code.
If I am doing something in multiple binaries, I search for an abstraction and put it into the libraries. Trivial examples of this are writing a matrix to disk, showing the progress of the program, or compression of output data.&lt;/p&gt;

&lt;p&gt;I solved this problem by building &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;simcore&lt;/code&gt; as a &lt;a href=&quot;https://en.wikipedia.org/wiki/Static_library&quot;&gt;static library&lt;/a&gt; that is linked to all the binaries, that there is no time wasted with compiling code multiple times. This is very easy with the build system CMake I use, but there are many more possibilities with different
infrastructure.&lt;/p&gt;

&lt;p&gt;The reason for having multiple binaries is to investigate your system in different environments. First, it is just the most basic thing possible and then you can add more interesting components. Of course, you can incrementally improve one program, but then you can’t go back to the basic program if you eventually don’t understand your complex problem and need to get back to the basics.&lt;/p&gt;

&lt;p&gt;The only hard part is that you must sometimes make sure you don’t break your old basic programs while improving the advanced ones. You should build all of them regularly.&lt;/p&gt;

&lt;h2 id=&quot;automatic-testing&quot;&gt;Automatic testing&lt;/h2&gt;
&lt;p&gt;These days it is usual to test software automatically. 
The classical approach is to test little parts of the software with
so-called unit tests. These can be very helpful and Kent Beck has written an
&lt;a href=&quot;https://www.oreilly.com/library/view/test-driven-development/0321146530/&quot;&gt;excellent book&lt;/a&gt;
about these.&lt;/p&gt;

&lt;p&gt;In my experience, there is another component to testing scientific software.
The underlying models often have special cases with know (approximate) solutions,
so you can run your general program to handle these cases and compare them to analytic results.&lt;/p&gt;

&lt;p&gt;For these tests perfect is the enemy of the good. It is better to have an incomplete
test case than none because it would be hard. Some example from my work are:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;I looked at wave packets moving with some speed. So I initialized a gaussian wavepacket and look where its maximum value was after some time and checked if it had the expected velocity. There was no fit to get the exact center of the wave or ensure its shape. Just a check if a rough approximation of the velocity is in
10% of the expected value&lt;/li&gt;
  &lt;li&gt;There are complex formulas to predict the state of a two-level quantum system excited with different lasers. I just looked at the simplest perfectly tuned pulse with the simplest formula to see 
if it worked the same with my numerics&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now if you structured your program into modules, as shown above, this is all very easy to do. Your tests can just be another binary besides the other programs to test all the 
complicated stuff in the library.&lt;/p&gt;

&lt;p&gt;Just run a simplified simulation, check the results in your program and print the
deviation if something wrong and a green checkmark or smiley face if everything worked.&lt;/p&gt;

&lt;p&gt;If at all possible make sure your tests can run in a few seconds. Otherwise, you might get frustrated to wait and stop using them regularly.&lt;/p&gt;

&lt;h2 id=&quot;configuration&quot;&gt;Configuration&lt;/h2&gt;
&lt;p&gt;Almost every scientific program needs many external constants to work. These might be physical constants, material parameters, setup constants, times, numerical parameters. 
Managing these in a well-structured manner can significantly help to keep an overview of everything at the time of development and even years later.
For me, a good configuration system should fulfill the following requirements&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Options should be named. Otherwise, you have to remember the 5. line is the speed of light and you will be confused eventually.&lt;/li&gt;
  &lt;li&gt;No information should be redundant/be copied. If you just want to run another simulation, you should not always have to reenter the speed of light.&lt;/li&gt;
  &lt;li&gt;Move some (simple) logic from your program into your configuration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One solution to this is use a scripting language like &lt;a href=&quot;https://www.python.org/&quot;&gt;Python&lt;/a&gt;, &lt;a href=&quot;https://www.lua.org/&quot;&gt;Lua&lt;/a&gt; or &lt;a href=&quot;https://en.wikipedia.org/wiki/Scheme_%28programming_language%29&quot;&gt;Scheme&lt;/a&gt; for configuration. In my experience, these are kind of complicated to integrate with a program, but otherwise
I like this solution a lot and it is &lt;a href=&quot;https://github.com/NanoComp/meep/&quot;&gt;sometimes used&lt;/a&gt;. It might be possible to make it a little easier to expose your numerics to python by something like &lt;a href=&quot;https://numpy.org/doc/stable/f2py/&quot;&gt;F2PY&lt;/a&gt; or &lt;a href=&quot;https://github.com/boostorg/python&quot;&gt;Boost.Python&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I prefer a simpler approach to define my own simple configuration format. I base the format on &lt;a href=&quot;https://en.wikipedia.org/wiki/INI_file&quot;&gt;.ini&lt;/a&gt; files, but variables can
contain formulas and it is possible to include base files. A configuration might look like this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Base.conf&lt;/em&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;c = 3e8

# space 
c = 300
Nx = 100
dx = x/Nx

#time
Nt = T/dt
dt = 0.98/c/sqrt(1/(dx^2)+1/(dy^2))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Sim.conf&lt;/em&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{Base.conf}
T = 30
x = 500
pulse_width = x/5
pulse_energy = exp(5*pi*acos(T/4))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In my implementation (&lt;em&gt;&lt;a href=&quot;https://gist.github.com/a-voel/913f246b2041c5685d564304314ec65b&quot;&gt;Sourcecode on github&lt;/a&gt;&lt;/em&gt;) I would use these file as&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#define AFPCFG_IMPL
#include &quot;avcfg.h&quot;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pulse_energy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pulse_location&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;cm&quot;&gt;/* ... */&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;load_config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mycfg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;avcfg_config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avcfg_load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Sim.conf&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;mycfg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pulse_energy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avcfg_getf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;pulse_energy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;cm&quot;&gt;/* Give default value if the variable is not defined in the configfile */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;mycfg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pulse_location&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avcfg_getf_op&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;pulse_location&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;cm&quot;&gt;/* ... */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;avcfg_free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mycfg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A nice trick is that you can now write the processed config (with only numerical values and no formulas in a single file) to disk with the output of your simulation.
Here you can check if the values are as expected and you can rerun exactly the same configuration. Because it is basically a .ini file there is a parser
for almost any programming language you could want to use for analysis.&lt;/p&gt;

&lt;h2 id=&quot;data-storage&quot;&gt;Data storage&lt;/h2&gt;
&lt;p&gt;Simulations can create huge quantities of data. It is in most cases favored to save as much
data as possible to do any imaginable analysis without having to rerun the slow simulations.&lt;/p&gt;

&lt;p&gt;It took me a long time to figure out a nice structure to store that simulation output and
derived analysis. I think in the end it would be optimal to have a versioned database that can run analysis in the background and dynamically move data between fast local storage to large network shares. But that would be a large system with many moving parts that would take many months of design and development from skilled
programmers.&lt;/p&gt;

&lt;p&gt;In the absence of that the best structure I found was something like:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/raw-data/ (Many gigabytes)
|- /Simulation run 1/
|- /Simulation run 2/
&apos;- ...
/analysis-results/ (A few Megabytes)
|- /Simulation run 1/
|- /Simulation run 2/
&apos;- ...
/plots/ (Some pictures)
|- /Simulation run 1/
|- /Simulation run 2/
&apos;- ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It might be counterintuitive to interleave the data of different simulation runs, but it has some nice properties.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The raw data can live on a slow (high latency) storage elsewhere, but it is trivial to just copy the analysis result onto your local computer to make plots faster.&lt;/li&gt;
  &lt;li&gt;You can also take the analysis data onto your laptop for a conference or home office where you might not fast (or any) internet.&lt;/li&gt;
  &lt;li&gt;If you want to write a paper you can simply copy all the plots folder without putting too much irrelevant stuff next to your Latex documents. If you redo your sim/analysis it is trivial to replace them without confusion and messing up.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This exactly replicates my description of time scales from above and also the different iteration numbers. You will
presumably, do much more iteration on your analysis than your simulation and hopefully even more iteration on your plots!&lt;/p&gt;

&lt;h2 id=&quot;numerics-and-algorithms&quot;&gt;Numerics and algorithms&lt;/h2&gt;
&lt;p&gt;This is the hard part you have to solve and I can’t help you. Sorry.&lt;/p&gt;

&lt;h2 id=&quot;neat-tricks&quot;&gt;Neat tricks&lt;/h2&gt;
&lt;p&gt;At last, I will share a few little ideas you can integrate into your program without too much hassle to make life a little easier.&lt;/p&gt;

&lt;h3 id=&quot;floating-point-precision&quot;&gt;Floating point precision&lt;/h3&gt;
&lt;p&gt;Almost any computer you will use has 32bit (float, single) and 64bit (double, full) floating-point numbers. I science most people
use 64 bit for more precision and fewer numerical problems. This is mostly a good idea, but in most cases these numbers are 
(kind of) twice as slow. This can be rather time-wasting while developing your program. Sadly most compilers don’t let you
switch automatically, but in C and C++ you can build that yourself easily. For example, I use:&lt;/p&gt;
&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#if 0 /*set to 1 for full precision */
typedef float F;
typedef std::complex&amp;lt;F&amp;gt; C;
typedef Eigen::ArrayXf VecF;
typedef Eigen::ArrayXcf VecC;
#else&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ArrayXd&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;VecF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ArrayXcd&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;VecC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#endif
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And you get very short type names for free!&lt;/p&gt;

&lt;p&gt;Also, see if &lt;a href=&quot;https://gcc.gnu.org/wiki/FloatingPointMath&quot;&gt;-ffast-math&lt;/a&gt; or something similar can 
speed up your test runs in development.&lt;/p&gt;

&lt;h3 id=&quot;simulation-progress&quot;&gt;Simulation progress&lt;/h3&gt;
&lt;p&gt;Sometimes you wait eagerly in front of your running simulation for results. I build myself a little
progress reporter for the command line. It can show percentage progress and extrapolated time
remaining, so you know if there is enough idle time for making tea or even lunch at the moment.&lt;/p&gt;

&lt;p&gt;My systems need to know the number of the total simulation steps at the beginning of the program 
and has to be notified after every step. 
To not use too much computation time I print the information only if 5 seconds elapsed or 5% of total progress were made.
There are many more possibilities how you can make this more awesome, but this was enough for me.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;config.ini   0.1% done in 0:06. Expected remaining time: 75:51
config.ini   0.3% done in 0:12. Expected remaining time: 76:34
config.ini   0.4% done in 0:18. Expected remaining time: 76:32
config.ini   0.5% done in 0:24. Expected remaining time: 76:04
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;My (not that advanced) implementation of this idea is on &lt;a href=&quot;https://gist.github.com/a-voel/9b0347397db95bc829953079ad047e74&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;program-version-in-data&quot;&gt;Program version in data&lt;/h3&gt;

&lt;p&gt;At the beginning of this article, I told you about some boring infrastructure stuff and now it is time
to build something cool with it!&lt;/p&gt;

&lt;p&gt;If you use version control your code always has a revision ID (kind of version number).
I found on the internet how to always put that ID into your code and resulting executable
with my build system. Now I told my program to always put a file with this revision number next
to the output files.&lt;/p&gt;

&lt;p&gt;If you also saved your configuration file as I did you now know exactly what program and what configuration you used to generate a simulation output. With this, you can trivially and with confidence reproduce the output
if you mess it up or investigate if it turns out to be strange a few months later.&lt;/p&gt;

&lt;p&gt;This might be useless in most cases, but it also might save you from a few sleepless panicked nights before a deadline.&lt;/p&gt;</content><author><name></name></author><summary type="html">Today many scientists have problems that are solved by developing new programs. Sadly most of them are not software developers, so the quality of the resulting programs is mostly up to luck and individual interest. They mostly only have to take basic courses into programming by other scientists. Often the professors mostly teach from books because they only wrote basic programs 30 years ago in their own Ph.D. time.</summary></entry></feed>