{"id":430,"date":"2009-09-05T23:55:23","date_gmt":"2009-09-05T20:55:23","guid":{"rendered":"https:\/\/yehar.com\/blog\/?p=430"},"modified":"2016-11-29T09:28:33","modified_gmt":"2016-11-29T07:28:33","slug":"margin-notes","status":"publish","type":"post","link":"https:\/\/yehar.com\/blog\/?p=430","title":{"rendered":"Marginal notes"},"content":{"rendered":"<p>These are miscellaneous notes and recipes.<\/p>\n<h2><span style=\"font-size: 1.5em;\">Mapping a pair of integers to an integer<\/span><\/h2>\n<p>2011-05-26<\/p>\n<p>How to map from a pair of integers to an integer? Negative or positive, independent of the size of the two integers, you can convert them into <a href=\"http:\/\/en.wikipedia.org\/wiki\/Balanced_ternary\">balanced ternary<\/a> to get rid of the signs and then interleave the digits starting from the least significant digits. Pad with leading zeros if the other integer is longer.<\/p>\n<p><figure id=\"attachment_2105\" aria-describedby=\"caption-attachment-2105\" style=\"width: 924px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/ternarymatrix.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2105\" title=\"ternarymatrix\" src=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/ternarymatrix.png\" alt=\"\" width=\"924\" height=\"448\" srcset=\"https:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/ternarymatrix.png 924w, https:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/ternarymatrix-300x145.png 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption id=\"caption-attachment-2105\" class=\"wp-caption-text\">Interleaving the balanced ternary trits of a pair of integers gives a fractal structure.<\/figcaption><\/figure><\/p>\n<p>When working with non-negative numbers or on a fixed binary bit depth, interleaving bits works fine (see <a href=\"http:\/\/en.wikipedia.org\/wiki\/Z-order_%28curve%29\">Z-order curve<\/a>).<\/p>\n<h2>Logarithm of a sum of large numbers<\/h2>\n<p>2010-03-30<\/p>\n<p>When dealing with large numbers, it can be useful, as an approximation, not to deal with the numbers themselves, but with their logarithms. This has the added benefit that multiplication of two numbers corresponds to taking the sum of their logarithms. But what if you want to take the sum of the numbers, $a$ and $b$, how can you avoid dealing with the possibly horrendously large numbers themselves? This problem may be encountered for example when computing the total unnormalized probability (a Boltzmann factor or partition function) of mutually exclusive events. Here's a way:<\/p>\n<p>Presume that $a&gt;0$, $b&gt;0$, and $a \\ge b$. If $a &lt; b$ you can always swap the two numbers.<\/p>\n<p>$\\mathrm{ln}(a + b) = \\mathrm{ln}\\big(a(1+\\frac{b}{a})\\big) = \\mathrm{ln}\\,a + \\mathrm{ln}(1+\\frac{b}{a}) = \\mathrm{ln}\\, a + \\mathrm{ln}\\big(1 + \\mathrm{e{x}p}(\\mathrm{ln}\\,b - \\mathrm{ln}\\,a)\\big) $.<\/p>\n<p>If $\\mathrm{ln}\\,b - \\mathrm{ln}\\,a$ is a very large negative number, then $\\mathrm{ln}(a + b) \\approx \\mathrm{ln}\\,a$.<\/p>\n<p>For the logarithm of the difference of $a$ and $b$, we would proceed similarly:<\/p>\n<p>Presume that $a&gt;0$, $b&gt;0$, and $a &gt; b$. These conditions are necessarily satisfied if $\\mathrm{log}\\,a$, $\\mathrm{log}\\,b$, and $\\mathrm{log}(a - b)$ can be calculated.<\/p>\n<p>$\\mathrm{ln}(a - b) = \\mathrm{ln}\\big(a(1-\\frac{b}{a})\\big) = \\mathrm{ln}\\,a + \\mathrm{ln}(1-\\frac{b}{a}) = \\mathrm{ln}\\, a + \\mathrm{ln}\\big(1 - \\mathrm{e{x}p}(\\mathrm{ln}\\,b - \\mathrm{ln}\\,a)\\big) $.<\/p>\n<p>If $\\mathrm{ln}\\,b - \\mathrm{ln}\\,a$ is a very large negative number, then $\\mathrm{ln}(a - b) \\approx \\mathrm{ln}\\,a$.<\/p>\n<p>$\\mathrm{ln}\\big(1 + \\mathrm{e{x}p}(x)\\big)$ and $\\mathrm{ln}\\big(1 - \\mathrm{e{x}p}(x)\\big)$\u00a0 are called Gaussian logarithms, because Carl Friedrich Gauss was the first to publish printed tables of them.<\/p>\n<p>One application for a Gaussian logarithm is in calculation of the transition energy to a state from its complement state that covers the remainder of the phase space or thermodynamic ensemble. When the Boltzmann factor of the state and the partition function of the system are known, but both expressed as energy ($E_{\\mathrm{state}}$ and $E_{\\mathrm{system}}$, respectively), the calculation for the transition energy $E_{\\mathrm{transition}}$ turns out as: $E_{\\mathrm{transition}} = E_{\\mathrm{state}} - E_{\\mathrm{system}} - k_{\\beta} T\\ \\mathrm{ln}\\big(1-\\mathrm{e{x}p}(-\\frac{E_{\\mathrm{state}} - E_{\\mathrm{system}}}{k_{\\beta}T})\\big)$, where $k_{\\beta}$ is the <a title=\"Wikipedia: Boltzmann constant\" href=\"http:\/\/en.wikipedia.org\/wiki\/Boltzmann_constant\">Boltzmann constant<\/a> and T is temperature. The transition the other way would simply change the sign of the transition energy.<\/p>\n<h2>Newbie bug with C++ nested templates<\/h2>\n<p>2010-03-02<\/p>\n<p>Say, you want to create a map from stings to vectors of strings. Why does this (with the appropriate STL includes)<strong>:\n<\/strong>\n<code><\/p>\n<pre>typedef std::vector VectorOfStrings; typedef std::map StringToVectorOfStrings;<\/pre>\n<p><\/code>\nwork, while this doesn't:\n<code><\/p>\n<pre>typedef std::map&gt; StringToVectorOfStrings;<\/pre>\n<p><\/code>\nThe latter works if you modify it a little bit, so that the consecutive <span style=\"color: #0000ff;\">&gt;<\/span>'s are not recognized as<span style=\"color: #0000ff;\"> &gt;&gt;<\/span>:\n<code><\/p>\n<pre>typedef std::map &gt; StringToVectorOfStrings;<\/pre>\n<p><\/code><\/p>\n<h2>Two IRC channels in different windows using EPIC IRC client<\/h2>\n<p>2009-10-15<\/p>\n<p>Start your IRC session like this:\n<code><\/p>\n<pre>\/window new\r\n\/window show 1\r\n\/bind ^I parse_command {\/window size 6} {\/window last} {\/window move 1}<\/pre>\n<p><\/code>\nNow you have two windows on top of each other, and you are doing things in the bottom one. You can switch between the windows by pressing tab.<\/p>\n<p>The normal usage is to join on two different channels:\n<code><\/p>\n<pre>\/join #firstchannel<\/pre>\n<p><\/code>\nPress tab.\n<code><\/p>\n<pre>\/join #secondchannel<\/pre>\n<p><\/code>\nWhenever you want to view or talk on the other channel, press tab.<\/p>\n<h2>Embedding a Java Applet in a WordPress 2.8.4 post (also works in 3.2.1)<\/h2>\n<p>2009-09-12<\/p>\n<p>In the HTML editor:<\/p>\n<p><code>&lt;div id=\"dyemixer\"&gt;APPLET HERE&lt;script type=\"text\/javascript\"&gt;\/\/ &lt;![CDATA[ document.getElementById('dyemixer').innerHTML = '&lt;applet width=\"550\" height=\"420\" code=\"DyeMixer.class\" archive=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/DyeMixer.jar\" alt=\"To use this applet, you need a Java virtual machine (Java plug-in) for your web browser.\"&gt;'; \/\/ ]]&gt;&lt;\/script&gt;&lt;\/div&gt;<\/code><\/p>\n<p>This also survives editing the post in the visual editor. You can even center the applet like you would a normal paragraph.\u00a0 Note that the div's id and the argument to getElementById must be identical. Using a similar JavaScript script, you should be able to put also other arbitrary HTML in a WordPress post or page.<\/p>\n<h2>Generation of random numbers from a truncated exponential distribution<\/h2>\n<p>2009-07-30<\/p>\n<p>To generate a random number with a truncated exponential distribution:<\/p>\n<ol>\n<li>Generate a random number $x$ from an exponential distribution with the rate parameter you want<\/li>\n<li>Calculate $x$ modulo where-to-truncate. This would not be integer modulo but for example fmod() floating-point remainder.<\/li>\n<\/ol>\n<p>Could be useful if you have exponentially distributed random numbers handy.<\/p>\n<h2>Anti-imaged wavelet transform<\/h2>\n<p>2002-11-25<\/p>\n<p>This is an idea of a wavelet-type audio processing scheme, where you decompose the audio into octave bands, each octave sampled at half the sampling frequency compared to the one-up octave band. Then you process the bands in whatever way you wish, and reconstruct the signal from the bands. The benefit of the method compared to usual wavelets is the low level of imaging noise when bands are processed separately. The original music-dsp posting has the details:<\/p>\n<p>http:\/\/aulos.calarts.edu\/pipermail\/music-dsp\/2002-November\/<a href=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/018600.html\">018600.html<\/a><\/p>\n<p><figure id=\"attachment_704\" aria-describedby=\"caption-attachment-704\" style=\"width: 820px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/multi.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-704\" title=\"multi\" src=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/multi.gif\" alt=\"multi\" width=\"820\" height=\"271\" srcset=\"https:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/multi.gif 820w, https:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/multi-300x99.gif 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-704\" class=\"wp-caption-text\">The processing scheme (\"\/2\" means decimation by 2, \"*2\" means dilution by zeros to 2x samplerate)<\/figcaption><\/figure><\/p>\n<p><figure id=\"attachment_703\" aria-describedby=\"caption-attachment-703\" style=\"width: 831px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fir.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-703\" title=\"fir\" src=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fir.gif\" alt=\"fir\" width=\"831\" height=\"571\" srcset=\"https:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fir.gif 831w, https:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fir-300x206.gif 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-703\" class=\"wp-caption-text\">Frequency responses of filters that could be used in the scheme<\/figcaption><\/figure><\/p>\n<h2>Frequency shifting (audio effect)<\/h2>\n<p>2001-09-02<\/p>\n<p>Frequency shifting can be an interesting audio effect, although it does not usually preserve harmonic relations. Here is how to do it the wrong and the right way:<\/p>\n<p><figure id=\"attachment_693\" aria-describedby=\"caption-attachment-693\" style=\"width: 692px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fs1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-693\" title=\"fs1\" src=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fs1.png\" alt=\"blah\" width=\"692\" height=\"311\" srcset=\"https:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fs1.png 692w, https:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fs1-300x134.png 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-693\" class=\"wp-caption-text\">Complex exponential modulation illustrated on z-plane unit circle<\/figcaption><\/figure><\/p>\n<p><figure id=\"attachment_696\" aria-describedby=\"caption-attachment-696\" style=\"width: 200px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fs31.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-696\" title=\"fs3\" src=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fs31.png\" alt=\"Complex exponential modulation illustrated on z-plane unit  circle\" width=\"200\" height=\"200\" srcset=\"https:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fs31.png 200w, https:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fs31-150x150.png 150w\" sizes=\"(max-width: 200px) 85vw, 200px\" \/><\/a><figcaption id=\"caption-attachment-696\" class=\"wp-caption-text\">The ugly mixture. Complicated aliasing caused by discarding the imaginary part from a complex exponential modulated signal.<\/figcaption><\/figure><\/p>\n<p><figure id=\"attachment_694\" aria-describedby=\"caption-attachment-694\" style=\"width: 743px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fs2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-694\" title=\"fs2\" src=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fs2.png\" alt=\"baa\" width=\"743\" height=\"763\" srcset=\"https:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fs2.png 743w, https:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/fs2-292x300.png 292w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-694\" class=\"wp-caption-text\">Anti-aliased frequency shifting illustrated on z-plane unit circle<\/figcaption><\/figure><\/p>\n<h2>Daubechies wavelets 1-38 in a .h file<\/h2>\n<p>2000-08-10<\/p>\n<p>I copied some daubechies wavelets from a published database into <a href=\"http:\/\/yehar.com\/blog\/wp-content\/uploads\/2009\/09\/daub.h\">this C header file<\/a>, confirmed the data to be error-free and wrote a little introduction into wavelets in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>These are miscellaneous notes and recipes. Mapping a pair of integers to an integer 2011-05-26 How to map from a pair of integers to an integer? Negative or positive, independent of the size of the two integers, you can convert them into balanced ternary to get rid of the signs and then interleave the digits &hellip; <a href=\"https:\/\/yehar.com\/blog\/?p=430\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Marginal notes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2105,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/yehar.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/430"}],"collection":[{"href":"https:\/\/yehar.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yehar.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yehar.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yehar.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=430"}],"version-history":[{"count":6,"href":"https:\/\/yehar.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/430\/revisions"}],"predecessor-version":[{"id":3994,"href":"https:\/\/yehar.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/430\/revisions\/3994"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yehar.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/2105"}],"wp:attachment":[{"href":"https:\/\/yehar.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yehar.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yehar.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}