<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title>/bin/hbar</title><subtitle>Blog of Jake Lishman</subtitle><link href="https://binhbar.com/"/><link rel="self" href="https://binhbar.com/atom.xml"/><id>https://binhbar.com/</id><updated>2024-12-08T16:19:23.505829+00:00</updated><author><name>Jake Lishman</name><uri>https://binhbar.com/</uri></author><category term="programming"/><category term="quantum computing"/><icon>/images/favicon-128.png</icon><entry>
<title>Signed zeroes and complex literals</title>
<link rel="alternate" href="https://binhbar.com/posts/2024/04/signed-zeroes-and-complex-literals/"/>
<id>https://binhbar.com/posts/2024/04/signed-zeroes-and-complex-literals/</id>
<updated>2024-04-02T00:33:00+01:00</updated>
<summary type="html">
&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Signed_zero&quot;&gt;IEEE-754 floats have the concept of a &amp;ldquo;signed zero&amp;rdquo;&lt;/a&gt;; &lt;code&gt;0.0&lt;/code&gt; has a different bit representation to &lt;code&gt;-0.0&lt;/code&gt;.
In most cases, &lt;code&gt;-0.0&lt;/code&gt; behaves the same way as &lt;code&gt;0.0&lt;/code&gt;, and it compares equal in arithmetic operations.
It becomes more obviously distinct in floating-point operations that involve some form of limiting behaviour.
For example, &lt;code&gt;x / 0.0&lt;/code&gt; and &lt;code&gt;x / -0.0&lt;/code&gt; are opposite-signed infinities.&lt;/p&gt;
&lt;p&gt;Along with the other IEEE-754 special values, like quiet/signalling NaN and infinities, these sorts of behaviours prevent compilers from making certain mathematical rewrites that would appear to be completely sound in regular arithmetic.
&lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mo&gt;−&lt;/ns0:mo&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;(&lt;/ns0:mo&gt;&lt;ns0:mi&gt;a&lt;/ns0:mi&gt;&lt;ns0:mo&gt;−&lt;/ns0:mo&gt;&lt;ns0:mi&gt;b&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;)&lt;/ns0:mo&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;-(a-b)&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; in regular arithmetic over the reals can be written as &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;b&lt;/ns0:mi&gt;&lt;ns0:mo&gt;−&lt;/ns0:mo&gt;&lt;ns0:mi&gt;a&lt;/ns0:mi&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;b-a&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.7778em;vertical-align:-0.0833em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.4306em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;a&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, and despite floats having a symmetrical range of positive and negative values (unlike two&amp;rsquo;s complement integers), this is an invalid transformation in IEEE-754 arithmetic regardless of whether a symmetric rounding mode is in effect.
In all rounding modes, the problem occurs at &lt;code&gt;a = b&lt;/code&gt;; all finite IEEE-754 floats satisfy &lt;code&gt;x - x = 0.0&lt;/code&gt;, therefore &lt;code&gt;a - b&lt;/code&gt; and &lt;code&gt;b - a&lt;/code&gt; are both &lt;code&gt;0.0&lt;/code&gt;, and negating one to make &lt;code&gt;-0.0&lt;/code&gt; makes it distinct.&lt;/p&gt;
&lt;p&gt;In most uses, this is largely a curiosity and has little impact.
It can be useful in general when the only information needed from the result of a long calculation is its sign.
If the result were to underflow, beyond even the subnormal floats, the resulting sign of the zero would still be able to indicate the correct direction.
The signed zero, then, can be thought of as representing the behaviour in the limit.&lt;/p&gt;
&lt;p&gt;Where it becomes far more than a curiosity, with major impactful results, is when the signed zero becomes involved in a calculation with a discontinuity at zero.
For real numbers, the most obvious of these is &lt;code&gt;atan2(y, x)&lt;/code&gt;, which is the floating-point version of &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;arctan&lt;/ns0:mi&gt;&lt;ns0:mo&gt;⁡&lt;/ns0:mo&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;(&lt;/ns0:mo&gt;&lt;ns0:mi&gt;y&lt;/ns0:mi&gt;&lt;ns0:mi mathvariant=&quot;normal&quot;&gt;/&lt;/ns0:mi&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;)&lt;/ns0:mo&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;\arctan(y/x)&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;arctan&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.03588em;&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; including the quadrant of the rotation.
Since the float &lt;code&gt;-0.0&lt;/code&gt; is then interpreted as &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:msub&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;lim&lt;/ns0:mi&gt;&lt;ns0:mo&gt;⁡&lt;/ns0:mo&gt;&lt;/ns0:mrow&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo&gt;→&lt;/ns0:mo&gt;&lt;ns0:msup&gt;&lt;ns0:mn&gt;0&lt;/ns0:mn&gt;&lt;ns0:mo&gt;−&lt;/ns0:mo&gt;&lt;/ns0:msup&gt;&lt;/ns0:mrow&gt;&lt;/ns0:msub&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;\lim_{x\to0^-} x&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.8444em;vertical-align:-0.15em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;&lt;span class=&quot;mop&quot;&gt;lim&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3419em;&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mrel mtight&quot;&gt;→&lt;/span&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.7027em;&quot;&gt;&lt;span style=&quot;top:-2.786em;margin-right:0.0714em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.5em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size3 size1 mtight&quot;&gt;&lt;span class=&quot;mbin mtight&quot;&gt;−&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;mdash;the limit as &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;x&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.4306em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; approaches zero by becoming less negative&amp;mdash;there is a natural distinction between &lt;code&gt;atan2(0.0, 0.0)&lt;/code&gt; and &lt;code&gt;atan2(0.0, -0.0)&lt;/code&gt;.
Treating these as being calculated within &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:msub&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;lim&lt;/ns0:mi&gt;&lt;ns0:mo&gt;⁡&lt;/ns0:mo&gt;&lt;/ns0:mrow&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;y&lt;/ns0:mi&gt;&lt;ns0:mo&gt;→&lt;/ns0:mo&gt;&lt;ns0:msup&gt;&lt;ns0:mn&gt;0&lt;/ns0:mn&gt;&lt;ns0:mo&gt;+&lt;/ns0:mo&gt;&lt;/ns0:msup&gt;&lt;/ns0:mrow&gt;&lt;/ns0:msub&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;\lim_{y\to0^+}&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.9805em;vertical-align:-0.2861em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;&lt;span class=&quot;mop&quot;&gt;lim&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3419em;&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot; style=&quot;margin-right:0.03588em;&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;mrel mtight&quot;&gt;→&lt;/span&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.7027em;&quot;&gt;&lt;span style=&quot;top:-2.786em;margin-right:0.0714em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.5em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size3 size1 mtight&quot;&gt;&lt;span class=&quot;mbin mtight&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.2861em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, it becomes sensible that &lt;code&gt;atan2(0.0, 0.0)&lt;/code&gt; would be a zero rotation, while &lt;code&gt;atan2(0.0, -0.0)&lt;/code&gt; approximates &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;π&lt;/ns0:mi&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;\pi&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.4306em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.03588em;&quot;&gt;π&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
</summary>
<category term="maths"/><category term="python"/><category term="C"/><category term="rust"/>
</entry><entry>
<title>Relicensing this blog under CC BY 4.0</title>
<link rel="alternate" href="https://binhbar.com/posts/2024/04/relicensing-this-blog-under-cc-by-40/"/>
<id>https://binhbar.com/posts/2024/04/relicensing-this-blog-under-cc-by-40/</id>
<updated>2024-04-01T00:10:00+01:00</updated>
<summary type="html">
&lt;p&gt;I originally licensed the text of all of my blog posts on this website under the &lt;a href=&quot;https://creativecommons.org/licenses/by-sa/4.0/&quot;&gt;Creative Commons Attribution Share-Alike version 4.0 (CC BY-SA 4.0)&lt;/a&gt;.
Concurrent with this blog post, I am changing the licence I distribute my posts on this website to &lt;a href=&quot;https://creativecommons.org/licenses/by/4.0/&quot;&gt;Creative Commons Attribution version 4.0 (CC BY 4.0)&lt;/a&gt;, dropping the share-alike requirement, including all previous posts.
You can still use blog posts dated before the 1st of April, 2024 under the terms of the CC BY-SA 4.0 that they were originally distributed with if you so choose, though to my understanding, CC BY 4.0 is strictly more permissive for the licensee.
Code samples remain licensed under the &lt;a href=&quot;/CODE_LICENCE.txt&quot;&gt;MIT licence&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The longer I have been a professional software developer, the less interest I have had in reciprocity conditions in licensing.
I originally used CC BY-SA 4.0 simply because that was what Wikipedia did at the time I needed to choose a licence, and that seemed fine to me.
Thinking again now, I see no benefit in enforcing the reciprocity condition.&lt;/p&gt;
</summary>
<category term="admin"/>
</entry><entry>
<title>Unusual effects of Python scoping</title>
<link rel="alternate" href="https://binhbar.com/posts/2022/11/unusual-effects-of-python-scoping/"/>
<id>https://binhbar.com/posts/2022/11/unusual-effects-of-python-scoping/</id>
<updated>2022-11-20T00:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;Python scoping can be deceptive, especially since it can appear fairly simple at first.
Generally, name resolution starts from an inner-most scope, and proceeds outwards until some outer scope either contains the name, or we run out of scopes to search.
This procedure is familiar to users of many programming languages.
Newcomers from other languages are often surprised that various control-flow constructs like &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;with&lt;/code&gt; &lt;em&gt;don&amp;rsquo;t&lt;/em&gt; open new scopes, so new variables defined with them are still accessible after the block has completed, but this is relatively simple to accept; Python often seems very permissive.
This post presents a few corners of Python&amp;rsquo;s scoping rules that are more unusual than this, some of which continue to trip me up.&lt;/p&gt;
</summary>
<category term="python"/>
</entry><entry>
<title>Mutating Python tuples by C FFI abuse</title>
<link rel="alternate" href="https://binhbar.com/posts/2022/11/mutating-python-tuples-by-c-ffi-abuse/"/>
<id>https://binhbar.com/posts/2022/11/mutating-python-tuples-by-c-ffi-abuse/</id>
<updated>2022-11-12T00:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;Tuples are as immutable as things get in Python.
They have special treatment being a core builtin, so unlike user-defined classes, there&amp;rsquo;s no private attribute that you can mutate if you really want to mess around.
However, Python is an interpreted language with no baked-in access control, and (almost) everything really is mutable if you try hard enough.&lt;/p&gt;
&lt;p&gt;All Python objects at the C level can be addressed by a pointer of type &lt;code&gt;PyObject *&lt;/code&gt;, which (for most regular CPython builds) has the elements
&lt;pre class=&quot;chl&quot;&gt;&lt;code class=&quot;line-numbers&quot; aria-hidden=&quot;true&quot;&gt;1
2
3
4&lt;/code&gt;&lt;code class=&quot;highlighted-code&quot;&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;_object&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ob_refcnt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyTypeObject&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ob_type&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;
These objects do what they say on the tin.
The different types of C object then have further elements after these, to specialise them.&lt;/p&gt;
&lt;p&gt;Tuples are a type of &amp;ldquo;variable-length&amp;rdquo; object, in the sense that the same C struct definition is used for all sizes.
With the head components inlined, this looks like
&lt;pre class=&quot;chl&quot;&gt;&lt;code class=&quot;line-numbers&quot; aria-hidden=&quot;true&quot;&gt;1
2
3
4
5
6&lt;/code&gt;&lt;code class=&quot;highlighted-code&quot;&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;_tuple&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ob_refcnt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyTypeObject&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ob_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ob_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ob_item&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;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
The first two arguments are the reference count and the Python type object, as before, then the next is the number of elements in the tuple, and the last one is an array of (pointers to) Python objects.
The array is declared as size one, but Python guarantees that enough space will actually have been allocated to hold &lt;code&gt;ob_size&lt;/code&gt; elements.
The items in the tuple are stored in this array.&lt;/p&gt;
&lt;p&gt;From Python space, &lt;code&gt;tuple.__setitem__&lt;/code&gt; raises a &lt;code&gt;TypeError&lt;/code&gt;, but in principle at the C level, there is no such immutability.
As of Python 3.11, the &lt;code&gt;tupleobject.h&lt;/code&gt; header file even &lt;a href=&quot;https://github.com/python/cpython/blob/3.11/Include/tupleobject.h#L9-L21&quot;&gt;has a comment explaining how &lt;code&gt;tuple&lt;/code&gt; can be used as a temporary array&lt;/a&gt;, and there is a &lt;a href=&quot;https://github.com/python/cpython/blob/92b531b8589b733c4e44e291f08271fa34947400/Objects/tupleobject.c#L111-L129&quot;&gt;&lt;code&gt;PyTuple_SetItem&lt;/code&gt; function implementation&lt;/a&gt;, although this has slightly different reference-counting semantics to normal.&lt;/p&gt;
&lt;p&gt;We can use the &lt;a href=&quot;https://docs.python.org/3.11/library/ctypes.html&quot;&gt;Python C FFI &lt;code&gt;ctypes&lt;/code&gt;&lt;/a&gt;, and the CPython implementation detail that &lt;a href=&quot;https://docs.python.org/3.11/library/functions.html#id&quot;&gt;&lt;code&gt;id&lt;/code&gt; returns the memory address of the Python object&lt;/a&gt; to cheekily define our own Python-space &lt;code&gt;tuple_setitem&lt;/code&gt;.
The &lt;code&gt;PyObject&lt;/code&gt; type in &lt;code&gt;ctypes&lt;/code&gt; doesn&amp;rsquo;t expose the internal fields of the object, but instead we can change the items we want by casting the integer output of &lt;code&gt;id&lt;/code&gt; into an &lt;code&gt;size_t&lt;/code&gt; (assuming, as in many systems that pointers have the same size as &lt;code&gt;size_t&lt;/code&gt;), and manually assigning into the &lt;code&gt;ob_item&lt;/code&gt; array.
To be better citizens of Python&amp;mdash;rampant pointer abuse aside&amp;mdash;we should also increment the reference count of the object we are inserting into the tuple, and decrement the count for the object we are removing.
This leaves us with the Python-space function
&lt;pre class=&quot;chl&quot;&gt;&lt;code class=&quot;line-numbers&quot; aria-hidden=&quot;true&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18&lt;/code&gt;&lt;code class=&quot;highlighted-code&quot;&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ctypes&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_size_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_ssize_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;POINTER&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Any&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tuple_setitem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Any&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;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&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;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tup&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;index&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tup&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;raise&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;IndexError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;tuple index out of range&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Decrement the old item&amp;#39;s refcount.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;old&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;old&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;POINTER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_ssize_t&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;mi&quot;&gt;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;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Increment the new item&amp;#39;s refcount.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&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;POINTER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_ssize_t&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;mi&quot;&gt;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;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Replace the item.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tup&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;POINTER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_size_t&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;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&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;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
Now let&amp;rsquo;s try it out:
&lt;pre class=&quot;chl&quot;&gt;&lt;code class=&quot;line-numbers&quot; aria-hidden=&quot;true&quot;&gt;1
2
3
4&lt;/code&gt;&lt;code class=&quot;highlighted-code&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tup&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;mi&quot;&gt;1&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;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tuple_setitem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tup&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;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tup&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&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;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
</summary>
<category term="python"/><category term="C"/>
</entry><entry>
<title>Some Small Useful Features of GitHub Actions</title>
<link rel="alternate" href="https://binhbar.com/posts/2021/05/small-useful-features-of-github-actions/"/>
<id>https://binhbar.com/posts/2021/05/small-useful-features-of-github-actions/</id>
<updated>2021-05-17T23:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;Due to &lt;a href=&quot;https://about.gitlab.com/blog/2021/05/17/prevent-crypto-mining-abuse/&quot;&gt;recent&lt;/a&gt; &lt;a href=&quot;https://github.blog/2021-04-22-github-actions-update-helping-maintainers-combat-bad-actors/&quot;&gt;problems&lt;/a&gt; &lt;a href=&quot;https://layerci.com/blog/crypto-miners-are-killing-free-ci/&quot;&gt;free&lt;/a&gt; &lt;a href=&quot;https://circleci.com/blog/how-we-find-block-and-prevent-cryptomining-at-circleci/&quot;&gt;CI&lt;/a&gt; &lt;a href=&quot;https://devblogs.microsoft.com/devops/change-in-azure-pipelines-grant-for-public-projects/&quot;&gt;servers&lt;/a&gt; have had with abuse from cryptocurrency miners, &lt;a href=&quot;https://blog.travis-ci.com/2020-11-02-travis-ci-new-billing&quot;&gt;Travis CI are scaling back what services are available to open source projects&lt;/a&gt;.
We used to use Travis for QuTiP, but the new pricing model has meant that we won&amp;rsquo;t be able to afford it once the old free tier &lt;a href=&quot;https://travis-ci.org&quot;&gt;travis-ci.org&lt;/a&gt; completely shuts down.
We trialled &lt;a href=&quot;https://github.com/features/actions&quot;&gt;GitHub Actions&lt;/a&gt; to &lt;a href=&quot;https://github.com/qutip/qutip/pull/1465&quot;&gt;build the 28 different wheels&lt;/a&gt; we now &lt;a href=&quot;https://pypi.org/project/qutip/&quot;&gt;distribute on PyPI&lt;/a&gt;, and then &lt;a href=&quot;https://github.com/qutip/qutip/pull/1551&quot;&gt;moved all our testing there as well&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here are few nice features that I&amp;rsquo;ve been using since we started.
These are mostly not unique to GitHub Actions, but all my example configuration code will be for it.&lt;/p&gt;
</summary>
<category term="github"/><category term="QuTiP"/>
</entry><entry>
<title>Generalised Fourier Series, Part 3: Comparing Series Expansions</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/11/generalised-fourier-series-part-3-comparing-series-expansions/"/>
<id>https://binhbar.com/posts/2020/11/generalised-fourier-series-part-3-comparing-series-expansions/</id>
<updated>2020-11-26T00:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;In &lt;a href=&quot;/posts/2020/11/generalised-fourier-series-part-2-making-series-expansions/&quot;&gt;the second part of this series on series expansions&lt;/a&gt;
(&lt;a href=&quot;/posts/2020/11/generalised-fourier-series/&quot;&gt;introduction&lt;/a&gt;), we found a way to make a series expansion using any
basis of orthogonal polynomials.  This is how the standard Fourier series is
defined, and then we also used it to make a series expansion out of the
&lt;a href=&quot;https://en.wikipedia.org/wiki/Legendre_polynomials&quot;&gt;Legendre polynomials&lt;/a&gt;.  Now we&amp;rsquo;re going to compare how these behave
with different numbers of terms.&lt;/p&gt;
&lt;p&gt;This is finally the fun part of the seminar!  My second-year physics students
should generally have already known the previous two articles from first-year
courses, so they started here, pretty much.  To interact with these series
expansions, you should load up my Jupyter notebook on either &lt;a href=&quot;https://colab.research.google.com/github/jakelishman/2020-imperial-yr2-differential-equations/blob/main/Series_Expansion_Approximations.ipynb&quot;&gt;Colab&lt;/a&gt; or
&lt;a href=&quot;https://mybinder.org/v2/gh/jakelishman/2020-imperial-yr2-differential-equations/main?filepath=Series_Expansion_Approximations.ipynb&quot;&gt;myBinder&lt;/a&gt;, and try plotting your own functions and seeing how the
expansions behave.&lt;/p&gt;
&lt;p&gt;In reality we don&amp;rsquo;t use the infinite form of series expansions, we only use the
first few terms to get a good approximation of a function.  The notebook is used
for investigating how different series expansions work with comparably few
terms, and which would be most appropriate in a given situation.  The series
there are the standard Fourier series, the Fourier&amp;ndash;Legendre series, and the
Taylor series about &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo&gt;=&lt;/ns0:mo&gt;&lt;ns0:mn&gt;0&lt;/ns0:mn&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;x=0&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.4306em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.6444em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; (which is formed by a different method to the other
two).  If you need a reminder of how these series look like, the Taylor and
Fourier series forms were given &lt;a href=&quot;/posts/2020/11/generalised-fourier-series/&quot;&gt;in the introduction&lt;/a&gt;, and the
Legendre series was derived &lt;a href=&quot;/posts/2020/11/generalised-fourier-series-part-2-making-series-expansions/&quot;&gt;in part two&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I asked the students to consider a couple of the following questions, and
discuss them in groups of around five:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Which series would you call the &amp;ldquo;most accurate&amp;rdquo;?  Why?  Does it depend on the
  function?&lt;/li&gt;
&lt;li&gt;What sorts of functions are the different expansions best at approximating?
  Which are they bad at?&lt;/li&gt;
&lt;li&gt;The Legendre series often seems to &amp;ldquo;give up&amp;rdquo; in the middle of some shapes at
  low orders (&lt;em&gt;e.g.&lt;/em&gt; sinusoids).  Why is this, and are there any things the
  series is still useful for?&lt;/li&gt;
&lt;li&gt;The Taylor series almost invariably has the largest pointwise error.  Why is
  this?  What is the Taylor series useful for?&lt;/li&gt;
&lt;/ul&gt;
</summary>
<category term="physics"/><category term="teaching"/><category term="python"/>
</entry><entry>
<title>Generalised Fourier Series, Part 2: Making Series Expansions</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/11/generalised-fourier-series-part-2-making-series-expansions/"/>
<id>https://binhbar.com/posts/2020/11/generalised-fourier-series-part-2-making-series-expansions/</id>
<updated>2020-11-25T22:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;In &lt;a href=&quot;/posts/2020/11/generalised-fourier-series-part-1-linear-algebra-basics/&quot;&gt;the first part of this series on series expansions&lt;/a&gt;
(&lt;a href=&quot;/posts/2020/11/generalised-fourier-series/&quot;&gt;introduction&lt;/a&gt;), we defined several concepts from first-year linear
algebra that we need to work in a general, abstract setting rather than having
to repeat the same derivations over and over again.  We are now looking at the
topic at the centre of this series; generalising the Fourier series method.&lt;/p&gt;
&lt;p&gt;Amazingly, the few definitions I gave in the previous article let us define a
whole family of different functional series expansions.  Like before, we will
consider only continuous, finite functions defined on the interval &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;[&lt;/ns0:mo&gt;&lt;ns0:mo&gt;−&lt;/ns0:mo&gt;&lt;ns0:mn&gt;1&lt;/ns0:mn&gt;&lt;ns0:mo separator=&quot;true&quot;&gt;,&lt;/ns0:mo&gt;&lt;ns0:mn&gt;1&lt;/ns0:mn&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;]&lt;/ns0:mo&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;[-1, 1]&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.
Let&amp;rsquo;s say we have a basis of functions that are orthogonal under the inner
product&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math display=&quot;block&quot;&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;⟨&lt;/ns0:mo&gt;&lt;ns0:mi&gt;f&lt;/ns0:mi&gt;&lt;ns0:mo separator=&quot;true&quot;&gt;,&lt;/ns0:mo&gt;&lt;ns0:mi&gt;g&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;⟩&lt;/ns0:mo&gt;&lt;ns0:mo&gt;=&lt;/ns0:mo&gt;&lt;ns0:msubsup&gt;&lt;ns0:mo&gt;∫&lt;/ns0:mo&gt;&lt;ns0:mrow&gt;&lt;ns0:mo&gt;−&lt;/ns0:mo&gt;&lt;ns0:mn&gt;1&lt;/ns0:mn&gt;&lt;/ns0:mrow&gt;&lt;ns0:mn&gt;1&lt;/ns0:mn&gt;&lt;/ns0:msubsup&gt;&lt;ns0:mi&gt;f&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;(&lt;/ns0:mo&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;)&lt;/ns0:mo&gt;&lt;ns0:mi&gt;g&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;(&lt;/ns0:mo&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;)&lt;/ns0:mo&gt;&lt;ns0:mtext&gt; &lt;/ns0:mtext&gt;&lt;ns0:mi mathvariant=&quot;normal&quot;&gt;d&lt;/ns0:mi&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mi mathvariant=&quot;normal&quot;&gt;.&lt;/ns0:mi&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;
    \langle f,g\rangle = \int_{-1}^1 f(x) g(x)\,\mathrm dx.
&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;⟨&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.10764em;&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.03588em;&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;⟩&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:2.5343em;vertical-align:-0.9703em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;&lt;span class=&quot;mop op-symbol large-op&quot; style=&quot;margin-right:0.44445em;position:relative;top:-0.0011em;&quot;&gt;∫&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.564em;&quot;&gt;&lt;span style=&quot;top:-1.7881em;margin-left:-0.4445em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mord mtight&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.8129em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.9703em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.10764em;&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.03588em;&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathrm&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;p&gt;One example of this is the trigonometric functions &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;sin&lt;/ns0:mi&gt;&lt;ns0:mo&gt;⁡&lt;/ns0:mo&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;(&lt;/ns0:mo&gt;&lt;ns0:mi&gt;k&lt;/ns0:mi&gt;&lt;ns0:mi&gt;π&lt;/ns0:mi&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;)&lt;/ns0:mo&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;\sin(k\pi x)&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.03588em;&quot;&gt;kπ&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and
&lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;cos&lt;/ns0:mi&gt;&lt;ns0:mo&gt;⁡&lt;/ns0:mo&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;(&lt;/ns0:mo&gt;&lt;ns0:mi&gt;k&lt;/ns0:mi&gt;&lt;ns0:mi&gt;π&lt;/ns0:mi&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;)&lt;/ns0:mo&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;\cos(k \pi x)&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.03588em;&quot;&gt;kπ&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; for all non-negative integers &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;k&lt;/ns0:mi&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;k&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.6944em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.03148em;&quot;&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.  It&amp;rsquo;s beyond the scope
of undergraduate physics courses to &lt;a href=&quot;https://math.stackexchange.com/a/317004/206819&quot;&gt;prove that the trigonometric functions
span this space&lt;/a&gt;, but they do, and you also can verify that the
inner-product integral is indeed zero for any unequal pair.  We&amp;rsquo;ll refer to
elements of this basis as &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:msub&gt;&lt;ns0:mi&gt;ϕ&lt;/ns0:mi&gt;&lt;ns0:mi&gt;n&lt;/ns0:mi&gt;&lt;/ns0:msub&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;(&lt;/ns0:mo&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;)&lt;/ns0:mo&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;\phi_n(x)&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;ϕ&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.1514em;&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, where &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;n&lt;/ns0:mi&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.4306em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is just a unique label.&lt;/p&gt;
&lt;p&gt;Now, since the functions span the space of functions we can write down any
function &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;f&lt;/ns0:mi&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;f&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.8889em;vertical-align:-0.1944em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.10764em;&quot;&gt;f&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; in terms of our basis &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:msub&gt;&lt;ns0:mi&gt;ϕ&lt;/ns0:mi&gt;&lt;ns0:mi&gt;n&lt;/ns0:mi&gt;&lt;/ns0:msub&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;\phi_n&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.8889em;vertical-align:-0.1944em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;ϕ&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.1514em;&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and some scalar coefficients
&lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:msub&gt;&lt;ns0:mi&gt;c&lt;/ns0:mi&gt;&lt;ns0:mi&gt;n&lt;/ns0:mi&gt;&lt;/ns0:msub&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;c_n&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.5806em;vertical-align:-0.15em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.1514em;&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; as&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math display=&quot;block&quot;&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;f&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;(&lt;/ns0:mo&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;)&lt;/ns0:mo&gt;&lt;ns0:mo&gt;=&lt;/ns0:mo&gt;&lt;ns0:munderover&gt;&lt;ns0:mo&gt;∑&lt;/ns0:mo&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;n&lt;/ns0:mi&gt;&lt;ns0:mo&gt;=&lt;/ns0:mo&gt;&lt;ns0:mn&gt;0&lt;/ns0:mn&gt;&lt;/ns0:mrow&gt;&lt;ns0:mi mathvariant=&quot;normal&quot;&gt;∞&lt;/ns0:mi&gt;&lt;/ns0:munderover&gt;&lt;ns0:msub&gt;&lt;ns0:mi&gt;c&lt;/ns0:mi&gt;&lt;ns0:mi&gt;n&lt;/ns0:mi&gt;&lt;/ns0:msub&gt;&lt;ns0:msub&gt;&lt;ns0:mi&gt;ϕ&lt;/ns0:mi&gt;&lt;ns0:mi&gt;n&lt;/ns0:mi&gt;&lt;/ns0:msub&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;(&lt;/ns0:mo&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;)&lt;/ns0:mo&gt;&lt;ns0:mo separator=&quot;true&quot;&gt;,&lt;/ns0:mo&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;
    f(x) = \sum_{n=0}^\infty c_n \phi_n(x),
&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.10764em;&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:2.9185em;vertical-align:-1.2671em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop op-limits&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.6514em;&quot;&gt;&lt;span style=&quot;top:-1.8829em;margin-left:0em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mrel mtight&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mord mtight&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em;&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;span class=&quot;mop op-symbol large-op&quot;&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-4.3em;margin-left:0em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;∞&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.2671em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.1514em;&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;ϕ&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.1514em;&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;p&gt;and this series is unique.  For convenience, we&amp;rsquo;ll call the series
representation &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:msub&gt;&lt;ns0:mi&gt;F&lt;/ns0:mi&gt;&lt;ns0:mi&gt;f&lt;/ns0:mi&gt;&lt;/ns0:msub&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;F_f&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.9694em;vertical-align:-0.2861em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.13889em;&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3361em;&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot; style=&quot;margin-right:0.10764em;&quot;&gt;f&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.2861em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; to distinguish it from &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;f&lt;/ns0:mi&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;f&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.8889em;vertical-align:-0.1944em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.10764em;&quot;&gt;f&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; while we&amp;rsquo;re still
determining the coefficients.&lt;/p&gt;
</summary>
<category term="physics"/><category term="teaching"/>
</entry><entry>
<title>Generalised Fourier Series, Part 1: Linear Algebra Basics</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/11/generalised-fourier-series-part-1-linear-algebra-basics/"/>
<id>https://binhbar.com/posts/2020/11/generalised-fourier-series-part-1-linear-algebra-basics/</id>
<updated>2020-11-25T21:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;In &lt;a href=&quot;/posts/2020/11/generalised-fourier-series/&quot;&gt;the introduction to this series of articles&lt;/a&gt;, we introduced the idea
of a series expansion, and saw the explicit forms of the Taylor and Fourier
series.  We are building towards a generalised series expansions based on
&amp;ldquo;orthogonal&amp;rdquo; polynomials, but first we have to define some concepts from linear
algebra.  This article is roughly at the level of early first-year physics
undergradutes.&lt;/p&gt;
&lt;p&gt;Students at this level have come across the word &amp;ldquo;orthogonal&amp;rdquo; before when
talking about Euclidean (&amp;ldquo;normal&amp;rdquo;) vectors.  Here it means the same thing as
&amp;ldquo;perpendicular&amp;rdquo; or &amp;ldquo;at right angles&amp;rdquo;, at least while you have three or fewer
dimensions.  Once you have more than that, or you&amp;rsquo;re dealing with some other
type of vector, the definition is a little more abstract.&lt;/p&gt;
</summary>
<category term="physics"/><category term="teaching"/>
</entry><entry>
<title>Generalised Fourier Series</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/11/generalised-fourier-series/"/>
<id>https://binhbar.com/posts/2020/11/generalised-fourier-series/</id>
<updated>2020-11-25T20:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;In all my time as a PhD student I have been involved with teaching undergraduate
and postgraduate physics students at Imperial College London (they even &lt;a href=&quot;https://www.imperial.ac.uk/natural-sciences/education-and-teaching/fons-annual-prizes-for-excellence/&quot;&gt;gave me
a prize&lt;/a&gt; in 2018!) as a teaching assistant in classroom-style
tutorials and seminars.  For my final year, though, I&amp;rsquo;ve also moved up into
helping write the teaching materials, particularly for the differential
equations part of the second-year course.&lt;/p&gt;
&lt;p&gt;The first differential equations seminar we had was showing off some of the
properties and uses of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Legendre_polynomials&quot;&gt;Legendre polynomials&lt;/a&gt;, which the students
had just met by solving Legendre&amp;rsquo;s equation.  My part of the seminar was
illustrating how any orthogonal basis of functions can be used to make a series
expansion, and then getting the students to investigate how different types of
functional expansion behave at different orders.  If you just want to play with
this, load up the Jupyter notebook on &lt;a href=&quot;https://colab.research.google.com/github/jakelishman/2020-imperial-yr2-differential-equations/blob/main/Series_Expansion_Approximations.ipynb&quot;&gt;Colab&lt;/a&gt; or &lt;a href=&quot;https://mybinder.org/v2/gh/jakelishman/2020-imperial-yr2-differential-equations/main?filepath=Series_Expansion_Approximations.ipynb&quot;&gt;myBinder&lt;/a&gt;.
The &lt;a href=&quot;https://github.com/jakelishman/2020-imperial-yr2-differential-equations/&quot;&gt;source code for this notebook&lt;/a&gt; is available on GitHub.  You can see
an example plot of these below.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/posts/2020/11/generalised-fourier-series-part-3-comparing-series-expansions/polynomial-high.svg&quot; alt=&quot;Series approximations of a
twelfth-order polynomial using many terms in the expansion.&quot;&gt;&lt;/p&gt;
&lt;p&gt;The end result we&amp;rsquo;ll achieve mathematically is an abstract way of making series
expansions.  In general, a series expansion approximates a function &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;f&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;(&lt;/ns0:mo&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;)&lt;/ns0:mo&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;f(x)&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.10764em;&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
by using a (possibly infinite) sequence of terms, where each term is a constant
multiplied by some basis function.  Different series expansions use different
bases and different methods of determining the coefficients.&lt;/p&gt;
&lt;p&gt;Perhaps the most familiar example is the Taylor series.  The Taylor series
&lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:msub&gt;&lt;ns0:mi&gt;T&lt;/ns0:mi&gt;&lt;ns0:mi&gt;f&lt;/ns0:mi&gt;&lt;/ns0:msub&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;(&lt;/ns0:mo&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;)&lt;/ns0:mo&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;T_f(x)&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1.0361em;vertical-align:-0.2861em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.13889em;&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3361em;&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot; style=&quot;margin-right:0.10764em;&quot;&gt;f&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.2861em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; that approximates a function &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:mi&gt;f&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;(&lt;/ns0:mo&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mo stretchy=&quot;false&quot;&gt;)&lt;/ns0:mo&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;f(x)&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.10764em;&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; around some point &lt;span class=&quot;katex&quot;&gt;&lt;span xmlns:ns0=&quot;http://www.w3.org/1998/Math/MathML&quot; class=&quot;katex-mathml&quot;&gt;&lt;ns0:math&gt;&lt;ns0:semantics&gt;&lt;ns0:mrow&gt;&lt;ns0:msub&gt;&lt;ns0:mi&gt;x&lt;/ns0:mi&gt;&lt;ns0:mn&gt;0&lt;/ns0:mn&gt;&lt;/ns0:msub&gt;&lt;/ns0:mrow&gt;&lt;ns0:annotation encoding=&quot;application/x-tex&quot;&gt;x_0&lt;/ns0:annotation&gt;&lt;/ns0:semantics&gt;&lt;/ns0:math&gt;&lt;/span&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;katex-html&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.5806em;vertical-align:-0.15em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3011em;&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is&lt;/p&gt;
</summary>
<category term="physics"/><category term="teaching"/><category term="python"/>
</entry><entry>
<title>QuTiP data layer and the end of Google Summer of Code 2020</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/08/qutip-data-layer-summer-of-code-round-up/"/>
<id>https://binhbar.com/posts/2020/08/qutip-data-layer-summer-of-code-round-up/</id>
<updated>2020-08-27T23:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;This post is the final permalink for the work I&amp;rsquo;ve done for &lt;a href=&quot;http://qutip.org/&quot;&gt;QuTiP&lt;/a&gt; in
the &lt;a href=&quot;https://summerofcode.withgoogle.com/&quot;&gt;Google Summer of Code 2020&lt;/a&gt;.  My mentors have been Alex Pitchford,
Eric Giguère and Nathan Shammah, and QuTiP is under the &lt;a href=&quot;https://numfocus.org/&quot;&gt;numFOCUS&lt;/a&gt;
umbrella.&lt;/p&gt;
&lt;p&gt;The main aim of the project was to make &lt;code&gt;Qobj&lt;/code&gt;, the primary data type in QuTiP,
able to use both sparse and dense representations of matrices and have them
interoperate seamlessly.  This was a huge undertaking that had far-reaching
implications all across the library, but we have now succeeded.  There is still
plenty of work to be done in additional development documentation and on
sanding out the edges to improve the UX, but we are moving towards a public
beta of a major version update next year.&lt;/p&gt;
</summary>
<category term="QuTiP"/><category term="GSoC"/><category term="python"/>
</entry><entry>
<title>Overview of the new QuTiP data layer</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/08/overview-of-the-new-qutip-data-layer/"/>
<id>https://binhbar.com/posts/2020/08/overview-of-the-new-qutip-data-layer/</id>
<updated>2020-08-19T23:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;This post is partial documentation for the implementation of the data-layer that
I wrote in the last week or so as part of &lt;a href=&quot;https://summerofcode.withgoogle.com/&quot;&gt;Google Summer of Code&lt;/a&gt; with
&lt;a href=&quot;http://qutip.org/&quot;&gt;QuTiP&lt;/a&gt;.  I may return to talk a bit more about how various
algorithms are achieved internally, but for now, this is some indication of what
I&amp;rsquo;ve been working on.&lt;/p&gt;
&lt;p&gt;I had previously replaced the old &lt;code&gt;fast_csr_matrix&lt;/code&gt; type with the new, custom
&lt;code&gt;CSR&lt;/code&gt; type as the data backing for &lt;code&gt;Qobj&lt;/code&gt;, and all internal QuTiP data
representations.  This produced some speed-ups in some places due to improved
algorithms and better cache usage in places, but its principle advantage was the
massive reduction in overhead for function calls between Python and C space,
which largely affected small objects.&lt;/p&gt;
&lt;p&gt;The full aim, however, is to have QuTiP 5 support many different data
representations as the backing of &lt;code&gt;Qobj&lt;/code&gt;, and use the most suitable
representation for the given data. This will not require every single QuTiP
function to have an exponential number of versions for every possible
combination of inputs, but only to have specialisations for the most common data
combinations.  This concept is the &amp;ldquo;data layer&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;All code examples in this post are prefixed with&lt;/p&gt;
&lt;pre class=&quot;chl&quot;&gt;&lt;code class=&quot;line-numbers&quot; aria-hidden=&quot;true&quot;&gt;1&lt;/code&gt;&lt;code class=&quot;highlighted-code&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;qutip&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;core&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
</summary>
<category term="QuTiP"/><category term="python"/><category term="cython"/><category term="GSoC"/>
</entry><entry>
<title>Contiguous memory and finding segfaults in Cython</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/07/contiguous-memory-and-finding-segfaults-in-cython/"/>
<id>https://binhbar.com/posts/2020/07/contiguous-memory-and-finding-segfaults-in-cython/</id>
<updated>2020-07-26T23:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;One of the main advantages of using Cython in the algebraic core of scientific
code is fast, non-bounds-checked access to contiguous memory.  Of course, when
you elide safety-checking, it&amp;rsquo;s not surprising when you start to get segfaults
due to out-of-bounds access and use-after-free bugs in your Cython code.&lt;/p&gt;
&lt;p&gt;This post talks a little about why we chose to use raw pointers to access our
memory in the upcoming major version of &lt;a href=&quot;http://qutip.org/&quot;&gt;QuTiP&lt;/a&gt; rather than
other possible methods, and how I tracked down a couple of memory-safety
bugs in my code which would have been caught had we been using safer
alternatives.&lt;/p&gt;
</summary>
<category term="cython"/><category term="python"/><category term="C"/><category term="GSoC"/>
</entry><entry>
<title>Design draft: improving tensor-product dimensions in QuTiP</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/07/design-draft-improving-tensor-product-dimensions-in-qutip/"/>
<id>https://binhbar.com/posts/2020/07/design-draft-improving-tensor-product-dimensions-in-qutip/</id>
<updated>2020-07-12T23:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;&lt;code&gt;Qobj&lt;/code&gt; instantiation and mathematical operations have a large overhead, mostly
because of handling the &lt;code&gt;dims&lt;/code&gt; parameter in tensor-product spaces.  I&amp;rsquo;m
proposing one possible way to speed this up, while also gaining some additional
safety and knowledge about mathematical operations on tensor-product spaces.&lt;/p&gt;
&lt;p&gt;The steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;rigourously define the &amp;ldquo;grammar&amp;rdquo; of &lt;code&gt;dims&lt;/code&gt;, and allow all of &lt;code&gt;dimensions.py&lt;/code&gt;
   to assume that this grammar is followed to speed up parsing&lt;/li&gt;
&lt;li&gt;maintain a private data structure type &lt;code&gt;dimensions._Parsed&lt;/code&gt; inside &lt;code&gt;Qobj&lt;/code&gt;
   which is constructed once, and keeps all details of the parsing so they need
   not be repeated.  Determine &lt;code&gt;Qobj.type&lt;/code&gt; from this data structure&lt;/li&gt;
&lt;li&gt;maintain knowledge of the individual &lt;code&gt;type&lt;/code&gt; of every subspace in the full
   Hilbert space (e.g. with a list).  There is still a &amp;ldquo;global&amp;rdquo; &lt;code&gt;Qobj.type&lt;/code&gt;, but
   this can now be one in the set &lt;code&gt;{&#x27;bra&#x27;, &#x27;ket&#x27;, &#x27;oper&#x27;, &#x27;scalar&#x27;, &#x27;super&#x27;,
   &#x27;other&#x27;}&lt;/code&gt;.  &lt;code&gt;&#x27;other&#x27;&lt;/code&gt; is for when the individual elements do not all match
   each other.  Individual elements cannot be &lt;code&gt;&#x27;other&#x27;&lt;/code&gt;.  &lt;code&gt;&#x27;scalar&#x27;&lt;/code&gt; is added to
   operations can keep track of tensor elements which have been contracted, say
   by a &lt;code&gt;bra-ket&lt;/code&gt; product&amp;mdash;operations will then broadcast scalar up to the
   correct dimensions on certain operations.&lt;/li&gt;
&lt;li&gt;dimension parsing is now sped up by using the operation-specific type
   knowledge.  For example, &lt;code&gt;bra + bra -&amp;gt; bra&lt;/code&gt;, and &lt;code&gt;ket.dag() -&amp;gt; bra&lt;/code&gt;.  Step 3
   is necessary to allow matrix multiplication to work.  These lookups could be
   done with enum values instead of string hashing.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; this is part of a design discussion for the next major release of QuTiP.
I originally wrote this on 2020-07-13, and any further discussion may be found
at &lt;a href=&quot;https://github.com/qutip/qutip/issues/1320&quot;&gt;the corresponding GitHub issue&lt;/a&gt;.&lt;/p&gt;
</summary>
<category term="GSoC"/><category term="QuTiP"/><category term="python"/>
</entry><entry>
<title>Sphinx and the QuTiP Developers' Guide</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/06/sphinx-doc-and-the-qutip-developers-guide/"/>
<id>https://binhbar.com/posts/2020/06/sphinx-doc-and-the-qutip-developers-guide/</id>
<updated>2020-06-28T23:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;Overhauling the internals of a mathematical library is no good if no other
developers on the team know how to use the new systems you&amp;rsquo;ve put in place, and
don&amp;rsquo;t know why you&amp;rsquo;ve made the choices you&amp;rsquo;ve made.  In the last week I&amp;rsquo;ve been
writing a new QuTiP developers&amp;rsquo; guide to the new data layer that I&amp;rsquo;m creating
as part of my Google Summer of Code project, which has involved learning a lot
more about the &lt;a href=&quot;https://www.sphinx-doc.org/en/master/&quot;&gt;Sphinx&lt;/a&gt; documentation tool, and a little bit of GitHub
esoterica.&lt;/p&gt;
&lt;p&gt;Currently we don&amp;rsquo;t have a complete plan for how this guide will be merged into
the QuTiP documentation, and where exactly it will go, so for now it is hosted
on &lt;a href=&quot;https://github.com/jakelishman/qutip-devguide/&quot;&gt;my own GitHub repository&lt;/a&gt;.  I have also put up a properly
rendered version on &lt;a href=&quot;https://jakelishman.github.io/qutip-devguide/&quot;&gt;a GitHub pages site linked to the
repository&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;My Sphinx &lt;a href=&quot;https://github.com/jakelishman/qutip-devguide/blob/master/conf.py&quot;&gt;&lt;code&gt;conf.py&lt;/code&gt;&lt;/a&gt; file for this repository is not (at the time of
commit &lt;a href=&quot;https://github.com/jakelishman/qutip-devguide/tree/0edf49e&quot;&gt;0edf49e&lt;/a&gt;) very exciting.  Fortunately, Sphinx largely just
works out-of-the-box as one would expect from a mature Python project.  Perhaps
the boldest part of that file is the &lt;code&gt;intersphinx_mapping&lt;/code&gt; dictionary, which
uses the &lt;code&gt;intersphinx&lt;/code&gt; built-in to link to other projects&amp;rsquo; documentation also
built with Sphinx.&lt;/p&gt;
&lt;p&gt;Right now, the &lt;code&gt;intersphinx&lt;/code&gt; documentation is perhaps a little lacking, and
sometimes seems to just involve some hope (and some disappointment).  In
particular, I have several external references set up as&lt;/p&gt;
&lt;pre class=&quot;chl&quot;&gt;&lt;code class=&quot;line-numbers&quot; aria-hidden=&quot;true&quot;&gt;1
2
3
4
5
6
7&lt;/code&gt;&lt;code class=&quot;highlighted-code&quot;&gt;&lt;span class=&quot;n&quot;&gt;intersphinx_mapping&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;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;qutip&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&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;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http://qutip.org/docs/latest/&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&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;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;python&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&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;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;https://docs.python.org/3&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&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;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;numpy&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&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;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;https://numpy.org/doc/stable/&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&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;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;scipy&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&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;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;https://docs.scipy.org/doc/scipy/reference/&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&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;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;cython&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&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;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;https://cython.readthedocs.io/en/latest/&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&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;
</summary>
<category term="GSoC"/><category term="python"/><category term="QuTiP"/>
</entry><entry>
<title>Design draft: data structure specifications</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/06/design-draft-data-structure-specifications/"/>
<id>https://binhbar.com/posts/2020/06/design-draft-data-structure-specifications/</id>
<updated>2020-06-12T15:40:00+00:00</updated>
<summary type="html">
&lt;p&gt;These are some of the discussion topics around how new linear algebra data
structures will be defined in &lt;a href=&quot;http://qutip.org&quot;&gt;QuTiP&lt;/a&gt; as part of my Google
Summer of Code project.&lt;/p&gt;
&lt;p&gt;After some discussion, including some further designs of how the new dispatcher
methods would function, we are trying to pursue a &amp;ldquo;light&amp;rdquo; data structure
strategy.  This will hopefully have very lightweight instances of the data, and
dispatch methods are simplified; mulitple dispatch is a difficult concept to fit
into a true object-oriented style, and we believe that dispatcher methods will
make adding new data types and dispatched functions significantly easier.&lt;/p&gt;
&lt;p&gt;Originally written on the 8th of June, 2020.&lt;/p&gt;
</summary>
<category term="GSoC"/><category term="QuTiP"/><category term="python"/>
</entry><entry>
<title>Design draft: data layer separation</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/06/design-draft-data-layer-separation/"/>
<id>https://binhbar.com/posts/2020/06/design-draft-data-layer-separation/</id>
<updated>2020-06-12T15:35:00+00:00</updated>
<summary type="html">
&lt;p&gt;This is an early design document about the separation of data layer in
&lt;a href=&quot;http://qutip.org&quot;&gt;QuTiP&lt;/a&gt; as part of my Google Summer of Code project.  This is
a very early-stage document, which is significantly liable to change, but is
indicative of the direction we were planning to go.&lt;/p&gt;
&lt;p&gt;Originally written on the 1st of June, 2020.&lt;/p&gt;
</summary>
<category term="GSoC"/><category term="QuTiP"/><category term="python"/>
</entry><entry>
<title>Compiling OpenMP libraries on macOS</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/06/compiling-openmp-libraries-on-macos/"/>
<id>https://binhbar.com/posts/2020/06/compiling-openmp-libraries-on-macos/</id>
<updated>2020-06-02T23:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;In &lt;a href=&quot;http://qutip.org&quot;&gt;QuTiP&lt;/a&gt; we have some optional &lt;a href=&quot;https://www.openmp.org&quot;&gt;OpenMP&lt;/a&gt; components, which can be
used if the C extensions are built with OpenMP support at compile time.
Typically this should be achievable just by adding the &lt;code&gt;-fopenmp&lt;/code&gt; flag at
compile and link time, but unfortunately the &lt;code&gt;llvm&lt;/code&gt; &lt;code&gt;clang&lt;/code&gt; distribution that
Apple ship with macOS is not built with OpenMP support.&lt;/p&gt;
</summary>
<category term="GSoC"/><category term="C"/>
</entry><entry>
<title>Getting the Cython debugger running on macOS</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/06/getting-the-cython-debugger-running-on-macos/"/>
<id>https://binhbar.com/posts/2020/06/getting-the-cython-debugger-running-on-macos/</id>
<updated>2020-05-31T23:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;Debuggers are complicated, especially ones that attach to compiled executables.
In Python &lt;code&gt;pdb&lt;/code&gt; runs inside the interpreter, so it does not need to control
other processes, but this will not work once we have &lt;a href=&quot;https://cython.org/&quot;&gt;Cython&lt;/a&gt;-compiled
components (or other C extensions) in our Python programmes.  Unfortunately,
Cython offers us plenty of opportunities to shoot ourselves in the foot, and it
is more likely we will need to debug use-after-free, double-free or other nasty
segfault situations.&lt;/p&gt;
&lt;p&gt;Cython does come with a debugger, which is more properly a set of Python
extensions to &lt;a href=&quot;https://www.gnu.org/software/gdb/&quot;&gt;&lt;code&gt;gdb&lt;/code&gt;, the GNU debugger&lt;/a&gt;.  Macs come with &lt;code&gt;lldb&lt;/code&gt; installed
and functional, but sadly getting &lt;code&gt;gdb&lt;/code&gt; running is a bit tricky.  At the time
of writing, I have macOS 10.14 Mojave, the current version of &lt;code&gt;gdb&lt;/code&gt; is
9.2, and Cython is 0.29.14.  Further, the Cython extensions require Python 2.7,
but I need to be able to debug Python 3 programmes since it&amp;rsquo;s the only
supported version of Python.&lt;/p&gt;
&lt;p&gt;As of right now, I do not have a fully working debugger, but hopefully I will
update this post after rebuilding &lt;code&gt;gdb&lt;/code&gt; with some minor patches.&lt;/p&gt;
</summary>
<category term="cython"/><category term="GSoC"/>
</entry><entry>
<title>Understanding old git branches</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/05/understanding-old-git-branches/"/>
<id>https://binhbar.com/posts/2020/05/understanding-old-git-branches/</id>
<updated>2020-05-16T23:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;I will be refactoring and reorganising &lt;a href=&quot;http://qutip.org&quot;&gt;QuTiP&amp;rsquo;s&lt;/a&gt; internal data
structures, a large task that was previously attempted by someone else but one
that never quite got completed and lives in a disused branch on their fork.  In
the intervening year or so, the codebase has moved on significantly, so GitHub
now sounds the death knell&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;This branch is 85 commits ahead, 366 commits behind qutip:master. &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I want to know what changes they had made, without being inundated by unrelated
changes on the &lt;code&gt;master&lt;/code&gt; branch.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s assume that the old branch of interest is called &lt;code&gt;old-feature&lt;/code&gt; and lives
in a forked repository which I have added as a remote called &lt;code&gt;fork&lt;/code&gt;.&lt;/p&gt;
</summary>
<category term="git"/><category term="GSoC"/>
</entry><entry>
<title>Welcome to Google Summer of Code 2020</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/05/welcome-to-google-summer-of-code-2020/"/>
<id>https://binhbar.com/posts/2020/05/welcome-to-google-summer-of-code-2020/</id>
<updated>2020-05-12T23:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;For this summer, I will be working full-time on the open-source project
&lt;a href=&quot;http://www.qutip.org&quot;&gt;QuTiP&lt;/a&gt; with a stipend provided by Google as part of the &lt;a href=&quot;https://summerofcode.withgoogle.com&quot;&gt;Summer of Code
2020&lt;/a&gt; project to the umbrella organisation &lt;a href=&quot;https://numfocus.org&quot;&gt;numFOCUS&lt;/a&gt;.  One
requirement of the stipend is that I blog about what I am working on throughout
the project, and on any interesting parts of team programming I encounter, so
that future applicants to the programme have an idea of what to expect.  I will
be doing that here, under the &amp;ldquo;&lt;a href=&quot;/tags/gsoc/&quot;&gt;GSoC&lt;/a&gt;&amp;rdquo; tag.&lt;/p&gt;
&lt;p&gt;QuTiP is a Python library for dynamics simulations of open quantum systems,
something that I have made heavy use of during my PhD studies.  At its core is
the &lt;code&gt;Qobj&lt;/code&gt; class, which represents all quantum objects.  Currently, the
underlying data storage format is always a compressed-sparse-row matrix,
which allows efficient simulation of very large tensor-product spaces, but
introduces significant overhead when handling smaller, few-qubit systems.  The
aim of the project is to abstract out the data layer so that higher-level
components can function seamlessly without worrying about the representation
format, but the advanced user will be able to access accelerated functions by
using the right tool for the job.  You can also read my &lt;a href=&quot;/posts/2020/05/welcome-to-google-summer-of-code-2020/proposal.pdf&quot;&gt;full project
proposal&lt;/a&gt; in PDF format hosted here.&lt;/p&gt;
</summary>
<category term="GSoC"/><category term="QuTiP"/>
</entry><entry>
<title>Detecting star imports in Python</title>
<link rel="alternate" href="https://binhbar.com/posts/2020/04/detecting-star-imports-in-python/"/>
<id>https://binhbar.com/posts/2020/04/detecting-star-imports-in-python/</id>
<updated>2020-04-12T23:00:00+00:00</updated>
<summary type="html">
&lt;p&gt;Star (or wildcard) imports are one of the methods for importing libraries in
Python, and although they are generally discouraged, they are rather prevalent
in a lot of notebook-style scientific code.  Within a package being imported,
there is no &lt;em&gt;official&lt;/em&gt; way of knowing whether this is by a star import, but
since Python allows overriding just about everything, we can detect slight
differences in the methods and inject arbitrary code if the wildcard is used.&lt;/p&gt;
&lt;p&gt;In &lt;a href=&quot;http://qutip.org&quot;&gt;QuTiP&lt;/a&gt;, we currently suffer from long import times and want to
move to a &lt;code&gt;scipy&lt;/code&gt;-like style where fewer symbols are in the global package
namespace, and submodules are only imported if explicitly requested.  We want
to issue a warning to people currently using the &lt;code&gt;from qutip import *&lt;/code&gt; syntax,
because the number of symbols available to them will soon decrease.&lt;/p&gt;
</summary>
<category term="python"/>
</entry></feed>
