[Working Title]Learning to do things the right way, one project at a timeJekyll2014-12-15T14:08:41-05:00http://bkanuka.com/Bennett Kanukahttp://bkanuka.com/bkanuka@gmail.com<![CDATA[Reverse Proxy DD-WRT with Apache]]>http://bkanuka.com/articles/ddwrt-reverse-proxy2014-05-24T00:00:00-00:002014-06-24T00:00:00-04:00Bennett Kanukahttp://bkanuka.combkanuka@gmail.com<p>I have a domain which points to a home server.
I use (and highly recommend) <a href="duckdns.org">Duck DNS</a> for a dynamic DNS service.
For the purpose of this article, we’ll assume Duck DNS is set up, and my home domain is <code>example.duckdns.org</code>.
I wanted to access my router and settings at <code>example.duckdns.org/ddwrt</code></p>
<p>Apache 2.4 (included with Ubuntu 14.04)
<script src="https://gist.github.com/bkanuka/71741fc477f923054ef4.js"> </script></p>
<p><a href="http://bkanuka.com/articles/ddwrt-reverse-proxy">Reverse Proxy DD-WRT with Apache</a> was originally published by Bennett Kanuka at <a href="http://bkanuka.com">[Working Title]</a> on June 24, 2014.</p><![CDATA[Disable Touchpad When Using Trackpoint]]>http://bkanuka.com/articles/disable-touchpad2014-05-24T00:00:00-00:002014-05-24T00:00:00-04:00Bennett Kanukahttp://bkanuka.combkanuka@gmail.com<p>First of all, I’d like to get a naming convention out of the way.
Being that this is a public blog, I will be using the term “TrackPoint” to refer to the mouse in the middle of the keyboard.
The following XKCD offers other possible naming conventions:</p>
<p><img src="http://imgs.xkcd.com/comics/appropriate_term.png" alt="Appropriate Name" /></p>
<p>Maybe it’s me, or maybe the configuration of my laptop (I’m actually on a Dell right now, not a Thinkpad) but whenever I use the Trackpoint mouse, I tend to tap the touchpad and make erronious clicks.
In the past I’ve simply disabled the touchpad, which works for me, but tends to annoy anyone else who uses my laptop - even briefly.
Plus, more than anything, I liked the challenge.</p>
<p>All of the solutions I found of Google were inadequate for one reason or another, so I decided to craft my own.</p>
<script src="https://gist.github.com/bkanuka/71741fc477f923054ef4.js"> </script>
<p>After saving the above gist, you’ll have to set the global variables <code>TRACKPOINT_NAME</code> and <code>TRACKPAD_NAME</code>.
Run <code>xinput --list --name-only</code> to list the names of your attached input devices, and copy the name from there exactly.</p>
<p>In the script, to check if a device is in use we use the program <code>xxd</code> to read a single byte from the raw input device.
In order to do this we will make the <code>/dev/input</code> devices readable by the group <code>plugdev</code> (or any other group you belong to).
<strong>THIS IS NOT SECURE</strong> because it will allow anyone belonging to <code>plugdev</code> to read raw events, and it would be trivial to create something like a keylogger.
In my case, there are no other users of this laptop besides me, so I’m not too concerned about keylogging myself.</p>
<p>Create a file in called <code>/etc/udev/rules.d/99-input.rules</code> with the following content: </p>
<pre><code>KERNEL=="event*", NAME="input/%k", MODE="660", GROUP="plugdev"
</code></pre>
<p>After restarting your computer, the output of <code>ls -l /dev/input</code> should look something like this:</p>
<pre><code>total 0
drwxr-xr-x 2 root plugdev 120 Jun 23 07:37 by-id
drwxr-xr-x 2 root plugdev 120 Jun 23 07:37 by-path
crw-r----- 1 root plugdev 13, 64 Jun 19 18:31 event0
crw-r----- 1 root plugdev 13, 65 Jun 19 18:31 event1
crw-r----- 1 root plugdev 13, 66 Jun 19 18:31 event2
crw-r----- 1 root plugdev 13, 67 Jun 23 07:37 event3
crw-r----- 1 root plugdev 13, 68 Jun 23 07:37 event4
crw-r----- 1 root plugdev 13, 63 Jun 19 18:31 mice
crw-r----- 1 root plugdev 13, 32 Jun 19 18:31 mouse0
</code></pre>
<p>To make sure the script is executable, run <code>sudo chmod +x trackpad-watcher.sh</code>.
You may want to add the script to your startup. This can be different for every distro, so won’t be covered here.
If you have any issues, please don’t hesitate to email me.</p>
<p><a href="http://bkanuka.com/articles/disable-touchpad">Disable Touchpad When Using Trackpoint</a> was originally published by Bennett Kanuka at <a href="http://bkanuka.com">[Working Title]</a> on May 24, 2014.</p><![CDATA[Connect to OLAP with Python]]>http://bkanuka.com/articles/python-connect-to-olap2014-05-01T00:00:00-00:002014-05-01T00:00:00-04:00Bennett Kanukahttp://bkanuka.combkanuka@gmail.com<p>I have a Linux machine I’d like to run regular QA against different data sets.
Some of our data sets have duplicate data between them (either to ease access, or because it’s in a slightly different form), and I wanted to write some basic script that checks if the data is in sync.</p>
<p>Most of the guides for programmatically querying OLAP are written for <a href="http://community.pentaho.com/projects/mondrian/">Mondrian</a> which is a great OLAP, but unfortunately we’re using Microsoft SSAS OLAP.
Most of the guides for programmatically querying SSAS are written for C# running on Windows, but unfortunately this will be running on Linux.</p>
<h1 id="introducing-xmla">Introducing XMLA</h1>
<p>The trick is to get SSAS to listen to HTTP <a href="http://en.wikipedia.org/wiki/XML_for_Analysis">XMLA</a> queries.
XMLA is a basic XML format that basically takes an MDX query and wraps it in some XML.
The format of XMLA isn’t really important though, as we won’t be using XMLA directly.
What is important is that XMLA is a well documented format that any program or OS can read and write it.</p>
<h2 id="server-side">Server Side</h2>
<p>Server side configuration is found in this <a href="http://technet.microsoft.com/en-us/library/gg492140.aspx">Technet article</a>.
Similar configurations will work on SSAS 2005 also.
After follow this steps, you should be able to connect to your OLAP using <code>http://your-server-ip/OLAP/msmdpump.dll</code> in Excel or SQL Server Management Studio.</p>
<h2 id="client-side">Client Side</h2>
<p>On client side, we use the Python module simply named <a href="https://github.com/may-day/olap">olap.xmla</a>.
No need to install from github though; it can be installed by simply running <code>pip install xmla</code>.</p>
<h1 id="finishing-up">Finishing Up</h1>
<p>After <code>olap.xmla</code> is installed, you should be able to follow the documentation at <a href="https://github.com/may-day/olap">the github page</a>.
Below is an example specifically for connecting to a Microsoft SSAS OLAP:</p>
<script src="https://gist.github.com/11458189.js"> </script>
<p><a href="http://bkanuka.com/articles/python-connect-to-olap">Connect to OLAP with Python</a> was originally published by Bennett Kanuka at <a href="http://bkanuka.com">[Working Title]</a> on May 01, 2014.</p><![CDATA[Randomly Selecting with Average]]>http://bkanuka.com/articles/randomly-selecting-with-average2014-04-15T00:00:00-00:002014-04-15T00:00:00-04:00Bennett Kanukahttp://bkanuka.combkanuka@gmail.com<p>The goal of this project was to “randomly” select numbers from a predefined set, with replacement, in a way that the mean of the selected numbers would equal (or come close to) a specified number.
For example, and the original motivation, was to select 100 numbers from the set: <script type="math/tex"> X = \left\{0, 0.1, 0.25, 0.5, 0.75, 0.8, 1.0\right\} </script>
so that the mean of the selected numbers was <script type="math/tex"> \approx 0.75 </script></p>
<p>Said a different way, given <script type="math/tex"> n \in \mathbb{N}, X = \left\{x_1, x_2, \ldots, x_m\right\} </script> and <script type="math/tex"> \mu </script>, find <script type="math/tex"> a_1, a_2, \ldots, a_m \in \mathbb{N}</script> such that:</p>
<script type="math/tex; mode=display">
\frac{1}{n}\sum_{i = 1}^m a_i x_i \approx \mu
\quad \text{and} \quad
\sum_{i = 1}^m a_i = n
</script>
<p>Now obviously, this isn’t something that can be solved deterministically, and there might be many different ways of selecting our <script type="math/tex"> a_i </script>.
For example, consider <script type="math/tex"> X = \left\{0, 0.5, 1.0\right\} </script>, <script type="math/tex"> \mu = 0.5 </script>, and <script type="math/tex"> n = 20 </script>.
We would be right to select 20 “0.5”s, or 10 “0”s and 10 “1.0”s.
Both methods would create a mean exactly equal to 0.5.
Therefore, it would be nice to have some sort of parameter that determined the “shape” of our selection, whether the selection was all from the extremes, or tightly grouped around the mean.</p>
<p>In order to solve both these issues, I decided to randomly select numbers from a probability distribution with finite support on <script type="math/tex"> [\min(X), \max(X)] </script> and round to the nearest <script type="math/tex"> x \in X </script>.<br />
Consider the <a href="http://en.wikipedia.org/wiki/Beta_distribution">beta distribution</a> </p>
<script type="math/tex; mode=display"> f(x;\alpha,\beta) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{\int_0^1 u^{\alpha-1} (1-u)^{\beta-1}\, du} </script>
<p>where <script type="math/tex"> \alpha > 0 </script> and <script type="math/tex"> \beta > 0 </script>.
The beta distribution is a nice choice for this problem for two reasons.
First is because of its finite support on the interval <script type="math/tex"> [0, 1] </script>.
Second, its mean is very easy to calculate.</p>
<script type="math/tex; mode=display"> \mu = \frac{\alpha}{\alpha + \beta} </script>
<p>This means we can intelligently select <script type="math/tex"> \alpha </script> and <script type="math/tex"> \beta </script> (or select one and fix the other using the above), select random numbers, round to the nearest <script type="math/tex">x \in X </script> and the average should be pretty close to <script type="math/tex"> \mu </script>. This turned out to be good enough for my purposes, and the program to do this is written below.</p>
<script src="https://gist.github.com/10692062.js"> </script>
<p><a href="http://bkanuka.com/articles/randomly-selecting-with-average">Randomly Selecting with Average</a> was originally published by Bennett Kanuka at <a href="http://bkanuka.com">[Working Title]</a> on April 15, 2014.</p><![CDATA[Drawing Regular n-gons with Horizontal Bottom]]>http://bkanuka.com/articles/drawing-regular-n-gons2014-04-17T00:00:00-00:002014-04-13T00:00:00-04:00Bennett Kanukahttp://bkanuka.combkanuka@gmail.com<p>I was re-reading my post on <a href="/articles/drawing-a-pentagon-in-latex">drawing a pentagon in LaTeX</a> and realized I never explained how I got the coordinates of the pentagon.
I also didn’t generalize the solution to drawing n-gons.
I would like to correct those issues.</p>
<p>We start with the unit circle centered at (0,0).
The coordinates of any point on the circle are given by:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
x &= \cos (t) \\
y &= \sin (t)
\end{align*}
%]]></script>
<p>Therefore we can find the coordinates of the regular n-gon at:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
x &= \cos \left(k \frac{2 \pi}{n}\right) \\
y &= \sin \left(k \frac{2 \pi}{n}\right) \quad \text{where } k = 0, 1, \ldots n
\end{align*}
%]]></script>
<p>However, this does not guarantee that the n-gon’s bottom edge will be horizontal (something we’d want for a visually pleasing drawing).
To the above formula, we can apply a starting angle <script type="math/tex">t</script> measured from the x-axis.</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
x &= \cos \left(t + k \frac{2 \pi}{n}\right) \\
y &= \sin \left(t + k \frac{2 \pi}{n}\right)
\end{align*}
%]]></script>
<p>Changing <script type="math/tex">t</script> will rotate the n-gon’s starting vertex.
To guarantee the bottom edge is horizontal, we rotate the starting vertex to the bottom of the unit circle, and then one half of <script type="math/tex"> \frac{2 \pi}{n} </script>, or </p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
t &= \frac{-\pi}{2} + \frac{1}{2}\frac{2 \pi}{n} \\
&= \frac{-\pi}{2} + \frac{\pi}{n}
\end{align*}
%]]></script>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
x &= \cos \left(\frac{-\pi}{2} + \frac{\pi}{n} + k \frac{2 \pi}{n}\right) \\
y &= \sin \left(\frac{-\pi}{2} + \frac{\pi}{n} + k \frac{2 \pi}{n}\right)
\end{align*}
%]]></script>
<p>And using the trig identities,</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
\cos \left(\frac{-\pi}{2} + \theta\right)
&= \cos \left(-\left(\frac{\pi}{2} - \theta\right)\right)
= \cos \left(\frac{\pi}{2} - \theta\right)
= \sin (\theta) \\
\sin \left(\frac{-\pi}{2} + \theta\right)
&= \sin \left(-\left(\frac{\pi}{2} - \theta\right)\right)
= -\sin \left(\frac{\pi}{2} - \theta\right)
= -\cos (\theta)
\end{align*}
%]]></script>
<p>we can simplify the equations to:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
x &= \sin \left(\frac{\pi}{n} + k \frac{2 \pi}{n}\right) \\
y &= - \cos \left(\frac{\pi}{n} + k \frac{2 \pi}{n}\right)
\end{align*}
%]]></script>
<p>If we are interested in drawing an n-gon with circumcircle of radius <script type="math/tex">r</script>, centered at <script type="math/tex">(a,b)</script>
then we can simply multiply by <script type="math/tex">r</script> and add an offset:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
x &= a + r \sin \left(\frac{\pi}{n} + k \frac{2 \pi}{n}\right) \\
y &= b - r \cos \left(\frac{\pi}{n} + k \frac{2 \pi}{n}\right)
\end{align*}
%]]></script>
<p>This is easily written as a Python function:</p>
<div class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="k">def</span> <span class="nf">ngon</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">r</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="n">vertecies</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">r</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="o">/</span><span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">k</span><span class="o">*</span><span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span><span class="o">/</span><span class="n">n</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">b</span> <span class="o">-</span> <span class="n">r</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="o">/</span><span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">k</span><span class="o">*</span><span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span><span class="o">/</span><span class="n">n</span><span class="p">)</span>
<span class="n">vertecies</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">),)</span>
<span class="k">return</span> <span class="n">vertecies</span></code></pre></div>
<p><a href="http://bkanuka.com/articles/drawing-regular-n-gons">Drawing Regular n-gons with Horizontal Bottom</a> was originally published by Bennett Kanuka at <a href="http://bkanuka.com">[Working Title]</a> on April 13, 2014.</p><![CDATA[Native Looking matplotlib Plots in LaTeX]]>http://bkanuka.com/articles/native-latex-plots2014-04-15T00:00:00-00:002014-01-10T00:00:00-05:00Bennett Kanukahttp://bkanuka.combkanuka@gmail.com<p>I write most of my math/numerical analysis scripts in Python, and I tend to use <a href="http://matplotlib.org/">matplotlib</a> for plotting.
When including a matplotlib plot in LaTeX I got the highest quality results by saving the plot as a PDF and using <code>\includegraphics{plot.pdf}</code> in LaTeX.
However, it bothered me that the plot had different fonts and font sizes than the rest of the document.
Here’s how I fixed that.</p>
<h2 id="figure-width">Figure Width</h2>
<p>I always choose the size of my plots as a percentage of the text width.
For example <code>width=0.6\textwidth</code>.
This allows me to use <code>0.3\textwidth</code> for images that are going to be side-by-side and not worry about absolute sizes.
We want matplotlib to output the right size plot so we need to find what exactly the <code>textwidth</code> is and tell matplotlib.
Do this by writing <code>\the\textwidth</code> inside your LaTeX document (inside the document, <em>not</em> the preamble) and running it through <code>pdflatex</code> or whatever LaTeX engine you use.
You’ll find that LaTeX will replace the command with some number.
Record this number.</p>
<h2 id="generate-figures">Generate Figures</h2>
<p>For every LaTeX document that has plots, I write a script <code>figures.py</code> which creates all the plots.
Copy the following script into <code>figures.py</code> and save it into the same folder as your LaTeX document.
Replace <code>fig_width_pt</code> with whatever number you got from above.</p>
<script src="https://gist.github.com/10796230.js"> </script>
<p>You <em>must</em> <code>import matplotlib</code> and make any rc changes before importing <code>matplotlib.pyplot</code>.
matplotlib expresses sizes in inches, while LaTeX likes sizes to be in pt, so the first part of this script sets up sizes in matplotlib properly.
The figure height is determined by the golden ratio, which is highly aesthetic ratio (it’s a good default).</p>
<h2 id="latex">LaTeX</h2>
<p>Running the above with <code>python figures.py</code> produces two files: <code>ema.pdf</code> and <code>ema.pgf</code>.
The PDF file is used just to have a stand-alone version of the plot and make sure everything looks right.</p>
<p>To incorporate the plot into LaTeX, put <code>\usepackage{pgf}</code> in the preamble and insert using <code>\input{ema.pgf}</code>.
For example:</p>
<div class="highlight"><pre><code class="language-latex" data-lang="latex"><span class="k">\documentclass</span><span class="nb">{</span>article<span class="nb">}</span>
<span class="k">\usepackage</span><span class="nb">{</span>pgf<span class="nb">}</span>
<span class="k">\begin</span><span class="nb">{</span>document<span class="nb">}</span>
<span class="k">\begin</span><span class="nb">{</span>figure<span class="nb">}</span>
<span class="k">\caption</span><span class="nb">{</span>A simple EMA plot.<span class="k">\label</span><span class="nb">{</span>fig:ema1<span class="nb">}}</span>
<span class="k">\centering</span>
<span class="k">\input</span><span class="nb">{</span>ema.pgf<span class="nb">}</span>
<span class="k">\end</span><span class="nb">{</span>figure<span class="nb">}</span>
<span class="k">\end</span><span class="nb">{</span>document<span class="nb">}</span></code></pre></div>
<figure>
<img src="/images/ema.png" />
</figure>
<p><a href="http://bkanuka.com/articles/native-latex-plots">Native Looking matplotlib Plots in LaTeX</a> was originally published by Bennett Kanuka at <a href="http://bkanuka.com">[Working Title]</a> on January 10, 2014.</p><![CDATA[How I Use Offlineimap]]>http://bkanuka.com/articles/offlineimap-mutt2013-12-13T00:00:00-00:002013-12-13T00:00:00-05:00Bennett Kanukahttp://bkanuka.combkanuka@gmail.com<p>I use mutt to write my emails, and <a href="http://offlineimap.org/">offlineimap</a> to sync mail between my local machine and gmail.
offlineimap is buggy.
I tried very hard to convince myself that wasn’t the case, but after using it for a few months, I had to face the truth.
I’m sure it’s not completely the offlineimap developers’ faults, but rather the fact that they must support a seemingly infinite combination of partial IMAP implementations.
What I mean by that, is almost every mail carrier supports IMAP, but you’d be very hard pressed to find a client or server that implements everything to specification.</p>
<p>Anyway, the main issues I found with offlineimap all had to do with offlineimap keeping the connection open.
I’m not sure exactly what the problem is, but I think it had to do with a spotty connection, timeouts, and things like that.
offlineimap would always fail eventually.</p>
<h2 id="disable-idle-and-autorefresh">Disable IDLE and autorefresh</h2>
<p>The first step to “fixing” this is to make sure every time offlineimap runs, it runs once and stops.
I did this by commenting out <code>autorefresh</code>, <code>quick</code>, <code>idlefolders</code> and <code>holdconnectionopen</code> in my <code>.offlineimaprc</code> file.</p>
<h2 id="mail-sync-script">mail-sync script</h2>
<p>Next, I made a short script to sync my mail.
Every time you call it, it waits for any running offlineimap instances to exit, and then calls offlineimap.
Using this script instead of calling offlineimap directly ensures there aren’t multiple running instances (offlineimap hates that).</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c">#!/usr/bin/env bash</span>
<span class="k">while</span> pkill --signal <span class="m">0</span> offlineimap
<span class="k">do</span>
sleep 2
<span class="k">done</span>
offlineimap > ~/mail-log 2><span class="p">&</span><span class="m">1</span> <span class="p">&</span></code></pre></div>
<p>Copy this script and save it as <code>~/bin/mail-sync</code> (or somewhere else in your path) and give it execute permissions.</p>
<h2 id="mail-script">mail script</h2>
<p>I’d like to be able to open mutt and have offlineimap sync my mail in the background as long as mutt is open.
At the end of the day, I should be able to close mutt and have offlineimap stop also.
I also don’t want to mess around with (ana)cron scripts.
I called this script <code>mail</code>.
There is already a built-in Linux command <code>mail</code>, so maybe this bad practice, but I never used the built-in command so it hasn’t bit me yet.
If you don’t like naming it <code>mail</code>, name it something else.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c">#!/usr/bin/env bash</span>
<span class="k">while</span> <span class="nb">true</span> <span class="c"># run forever</span>
<span class="k">do</span>
offlineimap 2><span class="p">&</span><span class="m">1</span> > ~/var/log/offlineimap.log <span class="c"># run offlineimap and copy log to ~/mail-log</span>
sleep <span class="m">120</span> <span class="c"># sleep 2 minutes</span>
<span class="k">done</span> <span class="p">&</span> <span class="c"># run loop in background</span>
<span class="nv">LOOP_PID</span><span class="o">=</span><span class="nv">$!</span> <span class="c"># copy PID of loop</span>
mutt <span class="c"># run mutt in foreground (and waits for mutt to exit)</span>
<span class="nb">kill</span> <span class="nv">$LOOP_PID</span> <span class="c"># these two lines are a cool trick to kill the</span>
<span class="nb">wait</span> <span class="nv">$LOOP_PID</span> 2>/dev/null <span class="c"># infinite loop and hide the error that it generates</span>
mail-sync <span class="p">&</span> <span class="c"># sync mail once more after mutt exits</span>
<span class="nb">exit </span><span class="m">0</span> <span class="c"># force script to exit "cleanly"</span></code></pre></div>
<h2 id="mutt-config">mutt config</h2>
<p>Occasionally I’d like to force a mail sync from inside mutt.<br />
I added the following line to my <code>.muttrc</code></p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">macro index,pager s <span class="s1">'<sync-mailbox><shell-escape>mail-sync &<enter>'</span></code></pre></div>
<p>This mutt macro saves the current mailbox and runs our mail-sync in the background, which in turn (safely) calls offlineimap and syncs.
You can choose whatever key you’d like, but I chose <em>s</em> for <em>sync</em>.</p>
<p>Assuming offlineimap is configured properly, and mutt is looking for mail in the right directories, you should be good to go! (but you can always keep an eye on your logs just in case :-) )</p>
<p><a href="http://bkanuka.com/articles/offlineimap-mutt">How I Use Offlineimap</a> was originally published by Bennett Kanuka at <a href="http://bkanuka.com">[Working Title]</a> on December 13, 2013.</p><![CDATA[img2pdf: Merge and resize images into a PDF]]>http://bkanuka.com/articles/merge-images-letter-pdf2014-04-15T00:00:00-00:002013-12-03T00:00:00-05:00Bennett Kanukahttp://bkanuka.combkanuka@gmail.com<p>I was recently emailed a bunch of JPEG files of a scanned document.
There was a file for every page, and the image files were very large.
I wanted to get them all in a single PDF file on letter size paper.
Because I find PDF mystical and difficult to work with, I decided to stick to tools I know - and I know LaTeX.</p>
<p>In no time I learned how to use the ImageMagick tool <code>convert</code> to convert to PDF.
Assuming that our original files are named <code>page1.jpg</code>, <code>page2.jpg</code>, etc. I could do:</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">convert page* -format pdf merge.pdf</code></pre></div>
<p>However, this made a PDF sized to the images, rather than forcing a letter size page.</p>
<p>Instead, I did the following.
First, I resized the images to something reasonable using <code>convert</code> (they were insanely large).</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">convert page* -resize 50% -format jpg page.jpg</code></pre></div>
<p>This created files <code>page-0.jpg</code> through <code>page-4.jpg</code> with smaller sizes.</p>
<p>Then I wrote the following script, which I called <code>img2pdf</code> which takes images files as arguments and converts to PDF.
Save the GitHub gist to a file and mark it as executable.
<code>img2pdf -h</code> has usage instructions, but it’s simple enough to figure out.</p>
<script src="https://gist.github.com/10767052.js"> </script>
<p><a href="http://bkanuka.com/articles/merge-images-letter-pdf">img2pdf: Merge and resize images into a PDF</a> was originally published by Bennett Kanuka at <a href="http://bkanuka.com">[Working Title]</a> on December 03, 2013.</p><![CDATA[Convert Mathematica Equation to Python]]>http://bkanuka.com/articles/mathematica-to-python2014-04-15T00:00:00-00:002013-12-02T00:00:00-05:00Bennett Kanukahttp://bkanuka.combkanuka@gmail.com<p>I recently used Mathematica to solve a Lagrangian differential equation.
I wish I could have used <a href="http://www.sagemath.org/">Sage</a> but I was unable to find a simple way to program the Euler-Lagrange equation into a function.
See this <a href="http://trac.sagemath.org/ticket/6466">bug report</a>.
Mathematica on the other hand, came with a <a href="http://library.wolfram.com/infocenter/Demos/4656/">reference notebook</a> with the Lagrangian and E-L equations built-in.</p>
<p>I solved the equations in Mathematica, but needed to use the resulting equations in Python code.
I saved the notebook as a text file (using Save As) and tirmmed the resulting text file to just the parts I wanted to convert to Python.
A single line of output was something like this (trimmed):</p>
<p>{x[t]->1/(-1+Subscript[r, 1])^3 E^(-t-t Subscript[r, 1]) (-E^t s+E^(t+t Subscript[r, 1]) s-E^(t Subscript[r, 1]) t v-E^(t Subscript[r, 1]) y-E^(t Subscript[r, 1]) t y+E^t s Subscript[r, 1]+2 E^(t Subscript[r, 1]) s Subscript[r, 1]-3 E^(t+t Subscript[r, 1]) s Subscript[r, 1]+E^(t Subscript[r, 1]) s t Subscript[r, 1]+3 E^(t Subscript[r, 1]) t v Subscript[r, 1]+…</p>
<p>To convert this long equation to Python code, I wrote a Perl script with a bunch of substitution commands.</p>
<script src="https://gist.github.com/10744247.js"> </script>
<p>It can be run by feeding a document on stdin:</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">./nb_to_py.pl < notebook.txt</code></pre></div>
<p>Considering that the variables used in Mathematica will change for every notebook, this script will have to be adapted for every use.
However, the comments should make this fairly straight-forward.</p>
<p>An example output [trimmed]:</p>
<div class="highlight"><pre><code class="language-python" data-lang="python"><span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span> \
<span class="o">+</span> <span class="n">MIX_RATE</span><span class="p">)</span><span class="o">**</span><span class="mi">3</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span> <span class="o">-</span> <span class="n">t</span> <span class="o">*</span> <span class="n">MIX_RATE</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">t</span><span class="p">)</span> <span class="o">*</span> <span class="n">SCALE</span> \
<span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">t</span> <span class="o">+</span> <span class="n">t</span> <span class="o">*</span> <span class="n">MIX_RATE</span><span class="p">)</span> <span class="o">*</span> <span class="n">SCALE</span> \
<span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">t</span> <span class="o">*</span> <span class="n">MIX_RATE</span><span class="p">)</span> <span class="o">*</span> <span class="n">t</span> <span class="o">*</span> <span class="n">v_old</span> \
<span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">t</span> <span class="o">*</span> <span class="n">MIX_RATE</span><span class="p">)</span> <span class="o">*</span> <span class="n">ss_old</span> \
<span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">t</span> <span class="o">*</span> <span class="n">MIX_RATE</span><span class="p">)</span> <span class="o">*</span> <span class="n">t</span> <span class="o">*</span> <span class="n">ss_old</span> \
<span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">t</span><span class="p">)</span> <span class="o">*</span> <span class="n">SCALE</span> <span class="o">*</span> <span class="n">MIX_RATE</span> \</code></pre></div>
<p>It may be worth nothing this is the first time I’ve used Perl.
There may be far better ways to do this, but I wanted an excuse to learn some Perl.</p>
<p><a href="http://bkanuka.com/articles/mathematica-to-python">Convert Mathematica Equation to Python</a> was originally published by Bennett Kanuka at <a href="http://bkanuka.com">[Working Title]</a> on December 02, 2013.</p><![CDATA[Drawing a Pentagon in LaTeX]]>http://bkanuka.com/articles/drawing-a-pentagon-in-latex2012-03-30T00:00:00-00:002012-03-30T00:00:00-04:00Bennett Kanukahttp://bkanuka.combkanuka@gmail.com<p>I needed to draw a pentagon for a paper on Galois theory I’m working on. After some research, this is what I have come up with: </p>
<figure>
<img src="/images/2012-03-30-drawing-a-pentagon-in-latex/penta.png" />
</figure>
<p>In the header we use TikZ: </p>
<div class="highlight"><pre><code class="language-latex" data-lang="latex"><span class="k">\usepackage</span><span class="nb">{</span>tikz<span class="nb">}</span></code></pre></div>
<p>and then draw the pentagon with the following: </p>
<div class="highlight"><pre><code class="language-latex" data-lang="latex"><span class="k">\begin</span><span class="nb">{</span>tikzpicture<span class="nb">}</span>[scale=2.2]<span class="c">%change the size here</span>
<span class="c">%pentagon</span>
<span class="k">\draw</span><span class="na">[ultra thick]</span> (0,1)--(-0.9510565163,0.309017)--(-0.58778525229,-0.809017)--(0.58778525229,-0.809017)--(0.9510565163,0.309017)--cycle;
<span class="c">%pentagram</span>
<span class="k">\draw</span><span class="na">[dashed, ultra thick,color=black]</span> (-0.9510565163,0.309017)--(0.9510565163,0.309017)--(-0.58778525229,-0.809017)--(0,1)--(0.58778525229,-0.809017)--cycle;
<span class="c">%label nodes</span>
<span class="k">\node</span> [above] at (0,1) <span class="nb">{</span><span class="s">$</span><span class="nv">\alpha</span><span class="nb">_</span><span class="m">1</span><span class="s">$</span><span class="nb">}</span>;
<span class="k">\node</span> [right] at (0.9510565163,0.309017) <span class="nb">{</span><span class="s">$</span><span class="nv">\alpha</span><span class="nb">_</span><span class="m">2</span><span class="s">$</span><span class="nb">}</span>;
<span class="k">\node</span> [below right] at (0.58778525229,-0.809017) <span class="nb">{</span><span class="s">$</span><span class="nv">\alpha</span><span class="nb">_</span><span class="m">3</span><span class="s">$</span><span class="nb">}</span>;
<span class="k">\node</span> [below left] at (-0.58778525229,-0.809017) <span class="nb">{</span><span class="s">$</span><span class="nv">\alpha</span><span class="nb">_</span><span class="m">4</span><span class="s">$</span><span class="nb">}</span>;
<span class="k">\node</span> [left] at (-0.9510565163,0.309017) <span class="nb">{</span><span class="s">$</span><span class="nv">\alpha</span><span class="nb">_</span><span class="m">5</span><span class="s">$</span><span class="nb">}</span>;
<span class="k">\end</span><span class="nb">{</span>tikzpicture<span class="nb">}</span></code></pre></div>
<p>Hope this helps!</p>
<p><a href="http://bkanuka.com/articles/drawing-a-pentagon-in-latex">Drawing a Pentagon in LaTeX</a> was originally published by Bennett Kanuka at <a href="http://bkanuka.com">[Working Title]</a> on March 30, 2012.</p><![CDATA[Wikipedia + MathJax]]>http://bkanuka.com/articles/wikipedia-mathjax2011-07-19T00:00:00-00:002011-07-19T00:00:00-04:00Bennett Kanukahttp://bkanuka.combkanuka@gmail.com<p>I read a lot of math-centric articles on Wikipedia and I have a number of issues with how math is displayed on Wikipedia.</p>
<ul>
<li>Zooming into the page or the equation causes the image to blur and loose quality.</li>
<li>Selecting the equation selects an image of the equation (with a white background), so copy-paste does not always have the desired results.</li>
<li>Finding the LaTeX or MathML markup source for the equation is difficult or impossible.</li>
<li>Searching for text on the page does not search inside the equation.</li>
<li>Equations stand out as unnecessarily large.</li>
<li>Text inside an equation does not adhere to the default browser font.</li>
</ul>
<h2 id="mathjax">MathJax</h2>
<p>MathJax is a program written in JavaScript that displays math exactly as it should be displayed: as text, and without all the problems mentioned above. Wikipedia user <a href="http://en.wikipedia.org/wiki/User:Nageh">Nageh</a> wrote an experimental port of MathJax that works for Wikipedia.</p>
<p>To enable MathJax in Wikipedia, do the following:</p>
<ul>
<li>Log in to Wikipedia. If you don’t already have an account, you’ll have to make one. You can follow the link on the top right of any Wikipedia page.</li>
<li>Go to <a href="http://en.wikipedia.org/w/index.php?title=Special:MyPage/common.js&action=edit">this page</a>. This is a special page that lets you edit the way any Wikipedia page is displayed.
In the large text box on that page, paste the following code:</li>
</ul>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="nx">mathJax</span><span class="o">=</span><span class="p">{};</span>
<span class="nx">mathJax</span><span class="p">.</span><span class="nx">fontDir</span><span class="o">=</span><span class="s2">"http://cdn.mathjax.org/mathjax/latest/fonts"</span><span class="p">;</span>
<span class="nx">importScript</span><span class="p">(</span><span class="s1">'User:Nageh/mathJax.js'</span><span class="p">);</span></code></pre></div>
<ul>
<li>Press the Save Page button below the text box.</li>
<li>You may have to enter a CAPTCHA and press Save Page again. You should now see the following (but with your username):</li>
</ul>
<figure>
<a href="/images/wikipedia_mathjax/common_js.png">
<img src="/images/wikipedia_mathjax/common_js.png" />
</a>
</figure>
<ul>
<li>Go to “My preferences” near the top of the page. Then click on “Appearance”.</li>
<li>Under the Math section select the box “Leave it as TeX (for text browsers)” so that the section looks like the following: And press “Save”.</li>
</ul>
<figure>
<img src="/images/wikipedia_mathjax/leave_as_tex.png" />
</figure>
<p>You can now go to any Wikipedia math page and have equations properly displayed.</p>
<p><a href="http://bkanuka.com/articles/wikipedia-mathjax">Wikipedia + MathJax</a> was originally published by Bennett Kanuka at <a href="http://bkanuka.com">[Working Title]</a> on July 19, 2011.</p>