<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>kReese.net</title>
	<atom:link href="http://kreese.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://kreese.net</link>
	<description>Kristopher Reese: Personal updates, musings, and science</description>
	<lastBuildDate>Mon, 17 Jun 2013 19:54:22 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>D3.js: Creating a Polar Area Diagram (Radial Bar Chart)</title>
		<link>http://kreese.net/blog/2012/08/26/d3-js-creating-a-polar-area-diagram-radial-bar-chart/</link>
		<comments>http://kreese.net/blog/2012/08/26/d3-js-creating-a-polar-area-diagram-radial-bar-chart/#comments</comments>
		<pubDate>Sun, 26 Aug 2012 19:39:00 +0000</pubDate>
		<dc:creator>Kristopher Reese</dc:creator>
				<category><![CDATA[Visualization]]></category>
		<category><![CDATA[Polar Area Diagrams]]></category>
		<category><![CDATA[Radial Bar Graph]]></category>

		<guid isPermaLink="false">http://kreese.net/?p=1327</guid>
		<description><![CDATA[Recently I started a new job at the University of Louisville, and along with that I have had the opportunity to start the data visualization process of huge project at the school right now.  One of the visualizations that were &#8230; <a href="http://kreese.net/blog/2012/08/26/d3-js-creating-a-polar-area-diagram-radial-bar-chart/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Recently I started a new job at the University of Louisville, and along with that I have had the opportunity to start the data visualization process of huge project at the school right now.  One of the visualizations that were required by the lead of the project was a Radial Bar Chart (Polar Area Diagram).  And with this being a web portal into the project, we needed something that would allow us to create visualizations on the web &#8211; enter d3.js.</p>
<p>You can find all of the source code for the tutorial in working HTML/JS files in the below link:<br />
<a href='http://kreese.net/wordpress/wp-content/uploads/2012/08/PolarAreaDiagram.zip'>Polar Area Diagram Source Code</a></p>
<p>D3 provides a rich javascript library for data visualization.  Unfortunately, as I began to do more research, I noticed that no one had created a Polar Area Diagram.  Of course, this being a requirement, I have had to sit down and create a way of creating this chart.  Fortunately, it wasn&#8217;t nearly as difficult as I had initially thought, it was just a culmination of various pie charts combined with different outer radii, but I thought I would write something up to help someone else get started on something similar if it is needed.</p>
<p>Here&#8217;s the data creation for the charts discussed here:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> randomFromTo <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> randomFromTo<span style="color: #009900;">&#40;</span>from<span style="color: #339933;">,</span> to<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #000066; font-weight: bold;">return</span> <span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>to <span style="color: #339933;">-</span> from<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> from<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    timeseries <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    sdat <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    keys <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;x&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;y&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;z&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;w&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;u&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;t&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> time<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        series <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> axis<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            series<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> randomFromTo<span style="color: #009900;">&#40;</span>minVal<span style="color: #339933;">,</span>maxVal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span>numticks<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            sdat<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>maxVal<span style="color: #339933;">/</span>numticks<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> i<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        timeseries<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> series<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;m going to assume that if you are reading this, you at least have a little bit of knowledge of the D3 libarary. As with all charts in D3, we start by creating an SVG tag (or there are ways of using the canvas object as well).</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #000066; font-weight: bold;">var</span> w <span style="color: #339933;">=</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> h <span style="color: #339933;">=</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">;</span>
&nbsp;
    viz <span style="color: #339933;">=</span> d3.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#radial&quot;</span><span style="color: #009900;">&#41;</span>
            .<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'svg:svg'</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'width'</span><span style="color: #339933;">,</span> w<span style="color: #009900;">&#41;</span>
                .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'height'</span><span style="color: #339933;">,</span> h<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    viz.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;svg:rect&quot;</span><span style="color: #009900;">&#41;</span>
            .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'x'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
            .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'y'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
            .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'height'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
            .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'width'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
            .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'height'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    vizBody <span style="color: #339933;">=</span> viz.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;svg:g&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>From here we&#8217;ll have to do a little bit of mathematics to create the radial (circular) axis ticks. First we&#8217;ll create a padding for the image, and we&#8217;ll also, in this example, fix the maximum value to 6. We&#8217;ll also transform the image so that our centroid of the chart is located in the middle instead of the top-left corner of the image.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #000066; font-weight: bold;">var</span> vizPadding <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        top<span style="color: #339933;">:</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span>
        right<span style="color: #339933;">:</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span>
        bottom<span style="color: #339933;">:</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span>
        left<span style="color: #339933;">:</span> <span style="color: #CC0000;">25</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> maxVal <span style="color: #339933;">=</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #006600; font-style: italic;">//need a circle so find constraining dimension</span>
    heightCircleConstraint <span style="color: #339933;">=</span> h <span style="color: #339933;">-</span> vizPadding.<span style="color: #660066;">top</span> <span style="color: #339933;">-</span> vizPadding.<span style="color: #660066;">bottom</span><span style="color: #339933;">;</span>
    widthCircleConstraint <span style="color: #339933;">=</span> w <span style="color: #339933;">-</span> vizPadding.<span style="color: #660066;">left</span> <span style="color: #339933;">-</span> vizPadding.<span style="color: #660066;">right</span><span style="color: #339933;">;</span>
    circleConstraint <span style="color: #339933;">=</span> d3.<span style="color: #660066;">min</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>heightCircleConstraint<span style="color: #339933;">,</span> widthCircleConstraint<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// create a function for calculating the radius of a point from the center</span>
    radius <span style="color: #339933;">=</span> d3.<span style="color: #660066;">scale</span>.<span style="color: #660066;">linear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">domain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> maxVal<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">range</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>circleConstraint <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    radiusLength <span style="color: #339933;">=</span> radius<span style="color: #009900;">&#40;</span>maxVal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//attach everything to the group that is centered around middle</span>
    centerXPos <span style="color: #339933;">=</span> widthCircleConstraint <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">+</span> vizPadding.<span style="color: #660066;">left</span><span style="color: #339933;">;</span>
    centerYPos <span style="color: #339933;">=</span> heightCircleConstraint <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">+</span> vizPadding.<span style="color: #660066;">top</span><span style="color: #339933;">;</span>
&nbsp;
    vizBody.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;transform&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;translate(&quot;</span> <span style="color: #339933;">+</span> centerXPos <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;, &quot;</span> <span style="color: #339933;">+</span> centerYPos <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now we can start adding our bars to the charts, or if we want the axes to exist behind the bars, we could add those next. Up to now, I have assumed you have some knowledge of the d3.scale and creation of the svg object. I stay with some of those assumptions, but I&#8217;ll explain the bars in a little more depth here. First, the code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">pie <span style="color: #339933;">=</span> d3.<span style="color: #660066;">layout</span>.<span style="color: #660066;">pie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> d<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    d <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> timeseries<span style="color: #009900;">&#91;</span>val<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> d.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
    groups <span style="color: #339933;">=</span> vizBody.<span style="color: #660066;">selectAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.series'</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>d<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    groups.<span style="color: #660066;">enter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;svg:g&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'series'</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">style</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fill'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;blue&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">style</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'stroke'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;black&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    groups.<span style="color: #660066;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    bar <span style="color: #339933;">=</span> d3.<span style="color: #660066;">svg</span>.<span style="color: #660066;">arc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">innerRadius</span><span style="color: #009900;">&#40;</span> <span style="color: #CC0000;">0</span> <span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">outerRadius</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>d<span style="color: #339933;">,</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> radius<span style="color: #009900;">&#40;</span> timeseries<span style="color: #009900;">&#91;</span>val<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    arcs <span style="color: #339933;">=</span> groups.<span style="color: #660066;">selectAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.series g.arc&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span>pie<span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">enter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            .<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;g&quot;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;attr&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    arcs.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;path&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fill&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;blue&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;d&quot;</span><span style="color: #339933;">,</span> bar<span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">style</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;opacity&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0.4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now here is where we use a pie chart in a slightly different way. You&#8217;ll notice we created our pie object near the top of this code. Of course, since we have six axes, each one will likely have a different height. The <em>d</em> variable does exactly this. We&#8217;ll use this variable &#8211; which is just an array length of 6 where each element contains the value 1. What this does is ensure that the angle of the &#8220;bars&#8221; that we are creating are equal.</p>
<p>The next bit of code for the <em>group</em> variable simply loads the data and creates a &lt;g&gt; tag in the svg that we can place our paths. Just like a d3 pie chart, we&#8217;re going to create an arc(). However, we are going to change the outer radius of the pie wedge to match with the radius of our data&#8217;s value. Since we created the radius() previously, we can use this function to calculate the outer radius of this wedge. Instead of using the d value that gets passed into the function that is calculating the radius, we&#8217;re going to pass this function the timeseries variable data for that wedge &#8211; <em>i</em>.</p>
<p>From there everything is the same as a standard pie chart. Simply pass the bar object to the path&#8217;s <em>d</em> attribute as shown and we get bars.</p>
<p>We can also add axes, text, and circular ticks if we&#8217;d like, as shown below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #000066; font-weight: bold;">var</span> radialTicks <span style="color: #339933;">=</span> radius.<span style="color: #660066;">ticks</span><span style="color: #009900;">&#40;</span>numticks<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> lineAxes<span style="color: #339933;">;</span>
&nbsp;
  vizBody.<span style="color: #660066;">selectAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.line-ticks'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  lineAxes <span style="color: #339933;">=</span> vizBody.<span style="color: #660066;">selectAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.line-ticks'</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span>keys<span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">enter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'svg:g'</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;transform&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>d<span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;rotate(&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">/</span> axis <span style="color: #339933;">*</span> <span style="color: #CC0000;">360</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">90</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span>
              <span style="color: #3366CC;">&quot;)translate(&quot;</span> <span style="color: #339933;">+</span> radius<span style="color: #009900;">&#40;</span>maxVal<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;line-ticks&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  lineAxes.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'svg:line'</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;x2&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">*</span> radius<span style="color: #009900;">&#40;</span>maxVal<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">style</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;stroke&quot;</span><span style="color: #339933;">,</span> ruleColor<span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">style</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fill&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  lineAxes.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'svg:text'</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>d<span style="color: #339933;">,</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> keys<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;text-anchor&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;middle&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  vizBody.<span style="color: #660066;">selectAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.circle-ticks'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  circleAxes <span style="color: #339933;">=</span> vizBody.<span style="color: #660066;">selectAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.circle-ticks'</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span>sdat<span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">enter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'svg:g'</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;circle-ticks&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  circleAxes.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;svg:circle&quot;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;r&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>d<span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> radius<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;circle&quot;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">style</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;stroke&quot;</span><span style="color: #339933;">,</span> ruleColor<span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">style</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fill&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  circleAxes.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;svg:text&quot;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;text-anchor&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;left&quot;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dy&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">*</span> radius<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="">String</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>When everything is created, we should get a radial bar graph that looks like the chart below! Then you can just play around with the styling a bit more. You can add better looking headers for the text, align the axis text to be centered over the bar (with a little bit of trigonometry), and even build the bars backwards (inner radius is the &#8220;timeseries&#8221; value, and outer radius is the maximum Value) so that a gradient can be added to the &#8220;bars&#8221;. But hopefully this gets someone started on a graph like this.</p>
<div id="radial"></div>
<p><script type="text/javascript" src="http://kreese.net/wordpress/wp-content/uploads/2012/08/radar.js"></script><script type="text/javascript">// <![CDATA[
loadViz();
// ]]&gt;</script></p>
<p>Use these graphs sparingly! They don&#8217;t always show what you might want them to show, and comparing differences between bars is difficult in circular form. In most cases, I would recommend using bar charts. For more information on when to use various graphs, Stephen Few&#8217;s work in information visualization would be a good place to start.</p>
]]></content:encoded>
			<wfw:commentRss>http://kreese.net/blog/2012/08/26/d3-js-creating-a-polar-area-diagram-radial-bar-chart/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Karush-Kuhn-Tucker Conditions</title>
		<link>http://kreese.net/blog/2011/09/10/karush-kuhn-tucker-conditions/</link>
		<comments>http://kreese.net/blog/2011/09/10/karush-kuhn-tucker-conditions/#comments</comments>
		<pubDate>Sat, 10 Sep 2011 17:48:08 +0000</pubDate>
		<dc:creator>Kristopher Reese</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kreese.net/?p=1067</guid>
		<description><![CDATA[The Karush-Kuhn-Tucker (KKT) conditions, sometimes referred to as the Kuhn-Tucker conditions, are the conditions that a Nonlinear Programming problem need to meet in order to be optimal.  The KKT conditions extends the method of Lagrangian multipliers by allowing for inequality &#8230; <a href="http://kreese.net/blog/2011/09/10/karush-kuhn-tucker-conditions/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>The Karush-Kuhn-Tucker (KKT) conditions, sometimes referred to as the Kuhn-Tucker conditions, are the conditions that a Nonlinear Programming problem need to meet in order to be optimal.  The KKT conditions extends the method of Lagrangian multipliers by allowing for inequality constraints, as opposed to the Lagrange Multipliers which only allow equality constraints.</p>
<p>Let us consider a nonlinear optimization problem:</p>
<blockquote><p>Minimize <img src='http://s.wordpress.com/latex.php?latex=f%28x%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='f(x)' title='f(x)' class='latex' /><br />
subject to:<img src='http://s.wordpress.com/latex.php?latex=g_%7Bi%7D%28x%29%5Cleq%200%20%5C%5C%20h_%7Bj%7D%28x%29%20%3D%200&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='g_{i}(x)\leq 0 \\ h_{j}(x) = 0' title='g_{i}(x)\leq 0 \\ h_{j}(x) = 0' class='latex' /></p></blockquote>
<p>We will let <img src='http://s.wordpress.com/latex.php?latex=x%5E%7B%2A%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x^{*}' title='x^{*}' class='latex' /> represent a relative minimum point for our problem, which also satisfies some constraint qualification. With this, then we can assume that for every element there exists a vector <img src='http://s.wordpress.com/latex.php?latex=%5Clambda_j%20%5Cleft%20%28%20j%20%3D%201%2C%20%5Cldots%2C%20l%20%5Cright%20%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\lambda_j \left ( j = 1, \ldots, l \right )' title='\lambda_j \left ( j = 1, \ldots, l \right )' class='latex' />, where <em>l</em> represents the number of equality constraints, and <img src='http://s.wordpress.com/latex.php?latex=%5Cmu_i%5Cleft%20%28%20i%20%3D%201%2C%20%5Cldots%2C%20m%20%5Cright%20%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mu_i\left ( i = 1, \ldots, m \right )' title='\mu_i\left ( i = 1, \ldots, m \right )' class='latex' />, where <em>m</em> represents the number of inequality constraints. These constants, <img src='http://s.wordpress.com/latex.php?latex=%5Clambda_j&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\lambda_j' title='\lambda_j' class='latex' /> and <img src='http://s.wordpress.com/latex.php?latex=%5Cmu_i&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mu_i' title='\mu_i' class='latex' />, are called KKT multipliers.</p>
<p>In order for the KKT conditions to be held in a nonlinear programming problem (NLP), then each of three conditions must be met [1-4]. The Primal Feasibility restates what the problem states, that the inequality and equality constraints on <img src='http://s.wordpress.com/latex.php?latex=x%5E%2A&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x^*' title='x^*' class='latex' /> must be met in order for the problem to be optimal:</p>
<img src='http://s.wordpress.com/latex.php?latex=%5Cleft.%5Cbegin%7Bmatrix%7D%20%20h%5Cleft%20%28%20x%5E%2A%20%5Cright%20%29%3D0%5C%5C%20%20g%5Cleft%20%28%20x%5E%2A%20%5Cright%20%29%5Cleq0%20%20%5Cend%7Bmatrix%7D%5Cright%5C%7D%20%5Ctextrm%7Bprimal%20feasibility%20%28PF%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\left.\begin{matrix}  h\left ( x^* \right )=0\\  g\left ( x^* \right )\leq0  \end{matrix}\right\} \textrm{primal feasibility (PF)}' title='\left.\begin{matrix}  h\left ( x^* \right )=0\\  g\left ( x^* \right )\leq0  \end{matrix}\right\} \textrm{primal feasibility (PF)}' class='latex' />
<p>The second condition is known as the dual feasibility condition.  In this condition states, rather verbosely, that every element in <img src='http://s.wordpress.com/latex.php?latex=%5Cmu_i&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mu_i' title='\mu_i' class='latex' /> must be greater than zero, and that the stationarity of the problem must be equal to 0.</p>
<img src='http://s.wordpress.com/latex.php?latex=%5Cleft.%5Cbegin%7Bmatrix%7D%20%20%5Cbigtriangledown%20f%5Cleft%20%28%20x%5E%2A%20%5Cright%20%29%20%2B%20%5Csum_%7Bi%3D1%7D%5E%7Bm%7D%5Clambda_%7Bi%7D%5Cbigtriangledown%20h_%7Bi%7D%5Cleft%20%28%20x%5E%2A%20%5Cright%20%29%20%2B%20%5Csum_%7Bj%3D1%7D%5E%7Bl%7D%5Cmu_%7Bj%7D%5Cbigtriangledown%20g_%7Bj%7D%5Cleft%20%28%20x%5E%2A%20%5Cright%20%29%20%3D%200%5C%5C%20%20%5Clambda_i%20%5Cin%20%5Cmathbb%7BR%7D%5C%5C%20%20%5Cmu_i%20%5Cgeq%200%20%20%5Cend%7Bmatrix%7D%5Cright%5C%7D%5Ctextrm%7Bdual%20feasibility%20%28DF%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\left.\begin{matrix}  \bigtriangledown f\left ( x^* \right ) + \sum_{i=1}^{m}\lambda_{i}\bigtriangledown h_{i}\left ( x^* \right ) + \sum_{j=1}^{l}\mu_{j}\bigtriangledown g_{j}\left ( x^* \right ) = 0\\  \lambda_i \in \mathbb{R}\\  \mu_i \geq 0  \end{matrix}\right\}\textrm{dual feasibility (DF)}' title='\left.\begin{matrix}  \bigtriangledown f\left ( x^* \right ) + \sum_{i=1}^{m}\lambda_{i}\bigtriangledown h_{i}\left ( x^* \right ) + \sum_{j=1}^{l}\mu_{j}\bigtriangledown g_{j}\left ( x^* \right ) = 0\\  \lambda_i \in \mathbb{R}\\  \mu_i \geq 0  \end{matrix}\right\}\textrm{dual feasibility (DF)}' class='latex' />
<p>The stationarity of the problem is:</p>
<img src='http://s.wordpress.com/latex.php?latex=%5Cbigtriangledown%20f%5Cleft%20%28%20x%5E%2A%20%5Cright%20%29%20%2B%20%5Csum_%7Bi%3D1%7D%5E%7Bm%7D%5Clambda_%7Bi%7D%5Cbigtriangledown%20h_%7Bi%7D%5Cleft%20%28%20x%5E%2A%20%5Cright%20%29%20%2B%20%5Csum_%7Bj%3D1%7D%5E%7Bl%7D%5Cmu_%7Bj%7D%5Cbigtriangledown%20g_%7Bj%7D%5Cleft%20%28%20x%5E%2A%20%5Cright%20%29%20%3D%200&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\bigtriangledown f\left ( x^* \right ) + \sum_{i=1}^{m}\lambda_{i}\bigtriangledown h_{i}\left ( x^* \right ) + \sum_{j=1}^{l}\mu_{j}\bigtriangledown g_{j}\left ( x^* \right ) = 0' title='\bigtriangledown f\left ( x^* \right ) + \sum_{i=1}^{m}\lambda_{i}\bigtriangledown h_{i}\left ( x^* \right ) + \sum_{j=1}^{l}\mu_{j}\bigtriangledown g_{j}\left ( x^* \right ) = 0' class='latex' />
<p>While the other two are simply conditions that <em>λ</em> and <em>μ</em> must meet in order for <img src='http://s.wordpress.com/latex.php?latex=x%5E%2A&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x^*' title='x^*' class='latex' /> to be optimal.</p>
<p>The third condition that must be met is known as complementary slackness.  This condition simply states that for each mu and its respective inequality constraint, the product of the two should result in zero:</p>
<img src='http://s.wordpress.com/latex.php?latex=%5Cmu_i%20g_%7Bi%7D%20%5Cleft%20%28%20x%5E%2A%20%5Cright%20%29%20%3D%200&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mu_i g_{i} \left ( x^* \right ) = 0' title='\mu_i g_{i} \left ( x^* \right ) = 0' class='latex' />
<p>When these three conditions are met, we have met the KKT conditions and our solution, <img src='http://s.wordpress.com/latex.php?latex=x%5E%2A&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x^*' title='x^*' class='latex' />, is an optimal solution for the NLP problem.  There maybe more than one <em>x</em> in the space which meet the conditions.  Any point in the problem space where every element of <em>λ</em> and <em>μ</em>, such that the tuple (x, <em>λ</em>, <em>μ</em>) satisfy the KKT conditions are called KKT points.  Derivation of these constraints can be found in [1,2]</p>
<h2>Constraint Qualifications</h2>
<p>As mentioned previously, the points that we are testing need to meet some qualification in order for the point to be considered.  The most well known constraint Qualification is the Linear Independence constraint Qualification (LICQ), which simply states that <img src='http://s.wordpress.com/latex.php?latex=h_%7Bj%7D%28x%5E%2A%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='h_{j}(x^*)' title='h_{j}(x^*)' class='latex' /> and <img src='http://s.wordpress.com/latex.php?latex=g_%7Bi%7D%28x%5E%2A%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='g_{i}(x^*)' title='g_{i}(x^*)' class='latex' /> are linearly independent from the other at point <img src='http://s.wordpress.com/latex.php?latex=x%5E%2A&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x^*' title='x^*' class='latex' />.  The Mangasarian-Fromovitz constraint qualification (MFCQ) states similarly the LICQ with the addition of being positive-lineraly independent at <img src='http://s.wordpress.com/latex.php?latex=x%5E%2A&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x^*' title='x^*' class='latex' />. [5]</p>
<p>There are however other constraint qualifiers that relax the LICQ.  The Slater constraint qualifier can be used in convex problems.  If there exists a point x such that <img src='http://s.wordpress.com/latex.php?latex=h_%7Bj%7D%28x%5E%2A%29%20%3D%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='h_{j}(x^*) = ' title='h_{j}(x^*) = ' class='latex' /> and <img src='http://s.wordpress.com/latex.php?latex=g_%7Bi%7D%28x%5E%2A%29%20%3C%200&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='g_{i}(x^*) &lt; 0' title='g_{i}(x^*) &lt; 0' class='latex' /> for all i,j active in <img src='http://s.wordpress.com/latex.php?latex=x%5E%2A&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x^*' title='x^*' class='latex' />, then the slate condition holds. [5,6]</p>
<p>Other types of constraint qualifiers do exist, but these three seem to be the most commonly used in KKT qualification.  </p>
<p>[1] Kuhn, H. and Tucker, A., "Nonlinear Programming" <em>Proceedings of the 2nd Berkeley Symposium</em> 1951, pp. 481-492.<br />
[2] Karush, W., &#8220;Minima of Functions of Several Variables with Inequalities as Side Constraints&#8221;.  M.Sc. Dissertation, Univ. of Chicago, Chicago, Il, 1939.<br />
[3] Kuhn, M. &#8220;The Karush-Kuhn-Tucker Theorem&#8221;, Internet:  <a href="http://smp.if.uj.edu.pl/~kopiec/MT/Materialy/KarushKuhnTucker.pdf" title="The Karush-Kuhn-Tucker Theorem">http://smp.if.uj.edu.pl/~kopiec/MT/Materialy/KarushKuhnTucker.pdf</a>, <em>CDSEM Uni. Mannheim</em>, 2006.<br />
[4]McCarl, B. and Spreen, T., &#8220;Nonlinear Optimization Conditions&#8221;, Ch. 12, Applied Mathematical Programming Using Algebraic Systems.  Internet: <a href="http://agecon2.tamu.edu/people/faculty/mccarl-bruce/mccspr/thebook.pdf" title="Applied Mathematical programming using Algebraic Systems" target="_blank">http://agecon2.tamu.edu/people/faculty/mccarl-bruce/mccspr/thebook.pdf</a><br />
[5]Eustaquio, R. Karas, E. and Ribeiro, A. Constraint Qualification for Nonlinear Programming, Tech Report, Univ. of Parana.<br />
[6] Tiba, D. and Zalinescu, C. &#8220;On the Necessity of some Constraint Qualification Conditions in Convex Programming&#8221;, Journal of Convex Analysis, 11 (1), 2004. pp 95-110.</p>
]]></content:encoded>
			<wfw:commentRss>http://kreese.net/blog/2011/09/10/karush-kuhn-tucker-conditions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Isoperimetric problem (Dido&#8217;s Problem)</title>
		<link>http://kreese.net/blog/2011/09/05/isoperimetric-problem-didos-problem/</link>
		<comments>http://kreese.net/blog/2011/09/05/isoperimetric-problem-didos-problem/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 02:56:41 +0000</pubDate>
		<dc:creator>Kristopher Reese</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Optimization]]></category>

		<guid isPermaLink="false">http://kreese.net/?p=1039</guid>
		<description><![CDATA[History Dido (or Elissa, depending on the reference) is the legendary founder and first Queen of Carthage. Her accounts are mentioned by the historian Justin and later as a part of Virgil&#8217;s &#8220;Aeneid&#8221; poem. In this story, Elissa is said &#8230; <a href="http://kreese.net/blog/2011/09/05/isoperimetric-problem-didos-problem/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h2>History</h2>
<p>Dido (or Elissa, depending on the reference) is the legendary founder and first Queen of Carthage. Her accounts are mentioned by the historian Justin and later as a part of Virgil&#8217;s &#8220;Aeneid&#8221; poem. In this story, Elissa is said to have escaped Tyre with groups of her brother Pygmalion&#8217;s attendants and senators. Eventually Elissa&#8217;s party arrived on the coast of Northern Africa where Elissa had asked the local inhabitants for a small area of land for a refuge. The isoparametric problem came into existence. Elissa asked for only as much land as could be encompassed by an oxhide. She used a stroke of mathematical genius, cutting the hide into long, thin strips in order to encircle the entirety of the nearby hill. This land became what is Carthage, and Elissa (or Dido) became the Queen of the city.</p>
<p>The history of Elissa is mentioned by Justin, however, the problem is formulated from a passage in Virgil&#8217;s &#8220;Aeneid&#8221;:</p>
<blockquote><p>The Kingdom you see is Carthage, the Tyrians, the town of Agenor;<br />
But the country around is Libya, no folk to meet in war.<br />
Dido, who left the city of Tyre to escape her brother,<br />
Rules here &#8211; a long and labyrinthine tale of wrong<br />
Is hers, but I will touch on its salient points in order&#8230;<br />
Dido, in great disquiet, organized her friends for escape.<br />
They met together, all those who harshly hated the tyrant<br />
Or keenly feared him: they seized some ships which chanced to be ready&#8230;<br />
They came to this spot, where today you can behold the mighty<br />
Battlements and rising citadel of New Carthage,<br />
And purchased a site, which was named &#8220;Bull&#8217;s Hide&#8221; after the bargain<br />
By which they should get as much land as they could enclose with a bull&#8217;s hide. <span class="Apple-style-span" style="font-weight: 300; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-style: normal;">[1]</span></p></blockquote>
<h2>Mathematical Formulation</h2>
<p>Knowing the history helps us to see how this might be useful, but it is not formalized in any mathematical sense. If we think of the problem, the problem becomes similar to what is known in mathematics as the isoperimetric problem. In Dido&#8217;s problem, Dido cuts the pieces of the ox-hide into strips, effectively using the area of the hide as the perimeter. From this point, the goal of the Dido problem then is to find the closed curve that has maximal area for a given perimeter. The problem is at this point, the isoperimetric problem.</p>
<p>Isoperimetric, literally means &#8220;having the same perimeter&#8221;, therefore, we know that the perimeters are fixed and our goals is to simply maximize the area. The isoperimetric problem requires the use of the isoperimetric inequality (and quotient) where if we think planar geometry, we can define a figure to have an area <em>A</em> and a perimeter <em>p</em>. The quotient then is [2]:</p>
<img src='http://s.wordpress.com/latex.php?latex=Q%5Cequiv%20%5Cfrac%7B4%5Cpi%20A%7D%7Bp%5E2%7D%20%5Cleq%201&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='Q\equiv \frac{4\pi A}{p^2} \leq 1' title='Q\equiv \frac{4\pi A}{p^2} \leq 1' class='latex' />
<p>This quotient is derived from the ratio of the curve area to the area of a circle (<img src='http://s.wordpress.com/latex.php?latex=A%20%3D%20%5Cpi%20r_A%5E2&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='A = \pi r_A^2' title='A = \pi r_A^2' class='latex' />) with the same perimeter as the curve (<img src='http://s.wordpress.com/latex.php?latex=p%3D2%20%5Cpi%20r_p&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p=2 \pi r_p' title='p=2 \pi r_p' class='latex' />). We can therefore derive the quotient:</p>
<img src='http://s.wordpress.com/latex.php?latex=%5Cbegin%7Bmatrix%7D%20%20Q%20%26%20%5Cequiv%20%26%20%5Cfrac%7Br_A%7D%7Br_p%5E2%7D%5C%5C%20%20%26%20%3D%20%26%20%5Cfrac%7B%5Cleft%20%28%20%5Cfrac%7BA%7D%7B%5Cpi%7D%5Cright%20%29%7D%7B%20%5Cleft%20%28%20%5Cfrac%7Bp%7D%7B2%20%5Cpi%7D%20%5Cright%20%29%5E2%7D%20%5C%5C%20%20%26%20%3D%20%26%20%5Cfrac%7B4%20%5Cpi%20A%7D%7Bp%5E2%7D%20%20%5Cend%7Bmatrix%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\begin{matrix}  Q &amp; \equiv &amp; \frac{r_A}{r_p^2}\\  &amp; = &amp; \frac{\left ( \frac{A}{\pi}\right )}{ \left ( \frac{p}{2 \pi} \right )^2} \\  &amp; = &amp; \frac{4 \pi A}{p^2}  \end{matrix}' title='\begin{matrix}  Q &amp; \equiv &amp; \frac{r_A}{r_p^2}\\  &amp; = &amp; \frac{\left ( \frac{A}{\pi}\right )}{ \left ( \frac{p}{2 \pi} \right )^2} \\  &amp; = &amp; \frac{4 \pi A}{p^2}  \end{matrix}' class='latex' />
<p>Mathematics tells us that this inequality holds for the shape of a circle only. Mathematical Proofs for this concept representing a can be found at [3-6]</p>
<h2>Usefulness in Computer Science</h2>
<p>In combinatorics and computer science, the use of Isoperimetric inequalities (the basis of the Dido problem) plays a role in designing robust computer networks, several applications in complexity theory, and error-correcting codes, through the use of expander graphs.[7]  Expander graphs are a sparse graph with strong connectivity properties.  The isoperimetric problems in graph theory (described on page 470 of [7]) then have usefulness in graph optimization problems for expander graphs.  </p>
<p>There are perhaps many other applications of the Isoperimetric problem and the Dido problem, as the Dido problem is in itself an optimization of maximal area given a perimeter. </p>
<h2>Works Cited:</h2>
<p>[1] Virgil. Trans. by C.D. Lewis. Book 1, lines 307-372 in <em>The Aeneid</em>. New York: Doubleday, pp 22-23. 1953.<br />
[2] Osserman, R. &#8220;Isoperimetric Inequalities.&#8221; Appendix 3, Sec 3 in <em>A Survey of Minimal Surfaces.</em> New York: Dover, pp. 147-148, 1986.<br />
[3] Bonnesen, <em>Les Probl&egrave;mes des Isop&eacute;r&icirc;m&egrave;tres et des Is&eacute;p&icirc;phanes</em>. Paris: Gauthier-Villars, pp 59-61, 1929.<br />
[4] Bonnesen, T. and Fenchel, W., <em>Theorie der Convexen K&ouml;rper</em>.  Chelsea Publishing, New York, S.111-112, 1948.<br />
[5] Magnani, C. &#8220;The Problem of Dido&#8221; Internet: <a title="The Problem of Dido by C. Magnani" href="http://mathematicalgarden.wordpress.com/2008/12/21/the-problem-of-dido/">http://mathematicalgarden.wordpress.com/2008/12/21/the-problem-of-dido/</a>, December 2008 [September 5, 2011].<br />
[6] Luthy, P. &#8220;Two Cute Proofs of the Isoperimetric Inequality&#8221; Internet: <a href="http://cornellmath.wordpress.com/2008/05/16/two-cute-proofs-of-the-isoperimetric-inequality/" title="Two Cute Proofs of the Isoperimetric Inequality by Peter Luthy">http://cornellmath.wordpress.com/2008/05/16/two-cute-proofs-of-the-isoperimetric-inequality/</a>, May 16, 2008 [September 5, 2011].<br />
[7]Hoory, S., Linial, N., and Wigderson, A. &#8220;Expander Graphs and Their Applications&#8221; <em>Bulletin of the American Mathematical Society</em> 43 (4): 439-561, 2006.  dpi: 10.1090/S0273-0979-06-01126-8.</p>
]]></content:encoded>
			<wfw:commentRss>http://kreese.net/blog/2011/09/05/isoperimetric-problem-didos-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lagrangian Optimization</title>
		<link>http://kreese.net/blog/2011/09/05/lagrangian-optimization/</link>
		<comments>http://kreese.net/blog/2011/09/05/lagrangian-optimization/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 02:56:25 +0000</pubDate>
		<dc:creator>Kristopher Reese</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kreese.net/?p=1054</guid>
		<description><![CDATA[The Lagrangian multiplier is a method of mathematical optimization that provides a means of finding the maxima and minima of a mathematical function, f, subject to constraints, g. The contour map below shows an optimization problem. We want to consider &#8230; <a href="http://kreese.net/blog/2011/09/05/lagrangian-optimization/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>The Lagrangian multiplier is a method of mathematical optimization that provides a means of finding the maxima and minima of a mathematical function, <em>f</em>, subject to constraints, <em>g</em>.  The contour map below shows an optimization problem.  We want to consider the problem where we maximize <img src='http://s.wordpress.com/latex.php?latex=f%20%5Cleft%20%28%20x%2C%20y%20%5Cright%20%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='f \left ( x, y \right )' title='f \left ( x, y \right )' class='latex' /> subject to the constraints <img src='http://s.wordpress.com/latex.php?latex=g%20%5Cleft%20%28%20x%2C%20y%20%5Cright%20%29%20%3D%20c&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='g \left ( x, y \right ) = c' title='g \left ( x, y \right ) = c' class='latex' />.  The solution to this problem then is the point where the red line tangentially touches a blue contour.  This answer can be found using Lagrangian multipliers.</p>
<div id="attachment_1055" class="wp-caption aligncenter" style="width: 310px"><a href="http://kreese.net/wordpress/wp-content/uploads/2011/09/svg2raster.png"><img class="size-medium wp-image-1055" style="background-color:white;" title="Lagrangian Multiplier" src="http://kreese.net/wordpress/wp-content/uploads/2011/09/svg2raster-300x216.png" alt="Lagrangian Optimization" width="300" height="216" /></a><p class="wp-caption-text">Contour Map where the red line shows the constraint g(x,y) = c and the blue times are contours of f(x,y).</p></div>
<p>We can construct a Lagrangian function as shown below. [1] We assume that function, <em>f</em>, is the problem for which an extremum of a function is desired:<br />
<img src='http://s.wordpress.com/latex.php?latex=f%20%5Cleft%20%28%20x_1%2C%20x_2%2C%20%5Cldots%2C%20x_n%20%5Cright%20%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='f \left ( x_1, x_2, \ldots, x_n \right )' title='f \left ( x_1, x_2, \ldots, x_n \right )' class='latex' /></p>
<p>with a set of any number of constraint functions:<br />
<img src='http://s.wordpress.com/latex.php?latex=%5Cbegin%7Bmatrix%7D%20%20g_i%20%5Cleft%20%28x_1%2C%20x_2%2C%20%5Cldots%2C%20x_n%20%5Cright%20%29%20%3D%20b_i%2C%20%26%20i%20%3D%201%2C2%2C%5Cldots%2C%20m%2C%20%26%20m%20%3C%20n%20%20%5Cend%7Bmatrix%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\begin{matrix}  g_i \left (x_1, x_2, \ldots, x_n \right ) = b_i, &amp; i = 1,2,\ldots, m, &amp; m &lt; n  \end{matrix}' title='\begin{matrix}  g_i \left (x_1, x_2, \ldots, x_n \right ) = b_i, &amp; i = 1,2,\ldots, m, &amp; m &lt; n  \end{matrix}' class='latex' /></p>
<p>Then our Lagrangian function is defined as:<br />
<img src='http://s.wordpress.com/latex.php?latex=%5CLambda%20%5Cleft%20%28%20x_1%2C%20x_2%2C%20%5Cldots%2C%20x_n%2C%5Clambda%20%20%5Cright%20%29%20%3D%20f%5Cleft%20%28%20x_1%2C%20x_2%2C%20%5Cldots%2C%20x_n%20%5Cright%20%29%20%2B%20%5Csum_%7Bi%3D1%7D%5E%7Bm%7D%5Clambda%20_i%5Ccdot%20%5Cleft%20%28%20b_i%20-%20g_i%28x_1%2C%20x_2%2C%20%5Cldots%2C%20x_n%29%20%5Cright%20%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\Lambda \left ( x_1, x_2, \ldots, x_n,\lambda  \right ) = f\left ( x_1, x_2, \ldots, x_n \right ) + \sum_{i=1}^{m}\lambda _i\cdot \left ( b_i - g_i(x_1, x_2, \ldots, x_n) \right )' title='\Lambda \left ( x_1, x_2, \ldots, x_n,\lambda  \right ) = f\left ( x_1, x_2, \ldots, x_n \right ) + \sum_{i=1}^{m}\lambda _i\cdot \left ( b_i - g_i(x_1, x_2, \ldots, x_n) \right )' class='latex' /></p>
<p>The Lagrangian multiplier can be used in any multidimensional space.  The example below is given in [2].</p>
<blockquote><p>A box is to be constructed out of various materials. The material to be used for the front and back sides costs $1 per square meter. The material to be used for the left and right sides costs $2 per square meter. The material to be used for the top and bottom costs $4 per square meter. What is the maximum volume that can be enclosed for a total material cost of $192?</p></blockquote>
<p>We know the constraint, C.  We should also know that the Volume of a cube (box) is <img src='http://s.wordpress.com/latex.php?latex=V%20%3D%20LWH&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='V = LWH' title='V = LWH' class='latex' />.  We must find the constraint function&#8230;</p>
<p>Since we have a front and back side that cost $1 each, we have 2 planes that are length times width.  We have a left and right side that cost $2 each at width time height.  And lastly, we have a top and bottom side that cost $4 each at length times width:</p>
<img src='http://s.wordpress.com/latex.php?latex=2%5Cleft%20%28%201%20L%20H%20%5Cright%20%29%20%2B%202%5Cleft%20%28%202WH%20%5Cright%20%29%20%2B%202%5Cleft%20%28%204LW%20%5Cright%20%29%20%3D%20C%20%5C%5C%20%202LH%2B4WH%2B8LW%20-%20C%20%3D%200&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='2\left ( 1 L H \right ) + 2\left ( 2WH \right ) + 2\left ( 4LW \right ) = C \\  2LH+4WH+8LW - C = 0' title='2\left ( 1 L H \right ) + 2\left ( 2WH \right ) + 2\left ( 4LW \right ) = C \\  2LH+4WH+8LW - C = 0' class='latex' />
<p>We can then write our Lagrangian function:<br />
<img src='http://s.wordpress.com/latex.php?latex=%5CLambda%20%28L%2CW%2CH%2C%5Clambda%20%29%20%3D%20LWH%20%2B%20%5Clambda%20%5Cleft%20%28%202LH%2B4WH%2B8LW%20-%20C%20%5Cright%20%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\Lambda (L,W,H,\lambda ) = LWH + \lambda \left ( 2LH+4WH+8LW - C \right )' title='\Lambda (L,W,H,\lambda ) = LWH + \lambda \left ( 2LH+4WH+8LW - C \right )' class='latex' /></p>
<p>If we continue solving this out using partial derivatives and some algebraic techniques, as shown in [2], we eventually determine the maximal volume for the cost, C, of $192, is a Volume of 64 meters<sup>3</sup>.  This is an example of how we can use the Lagrangian multiplier in a 3-dimensional space.  The multiplier can work in any higher dimensional space as well. </p>
<p>What does the Lagrangian multiplier, &lambda;, represent then?  Well in the problems above, the Lagrangian multiplier represents a scalar value that allows us to change the size of the values used to scale the shape (circle in the contour map, and cube in the Volume problem).  However, the Lagrangian multiplier can be used to represent actual values as well.  [3] shares some examples of what the &lambda; functions can represent in both physics and economics.</p>
<p>In the case of the physics of forces and potential energy, we might think of the constraint functions as representing forces that are helping to bring a point to it minimum or maximum.  The Multiplier can then be thought of as a measure of how hard the constraint has to pull in order to balance our the forces on the constraint surface.  In economics, we might think of maximizing profits based on some limited number of resources.  The multiplier then represents the marginal value of the resource, or the rate at which the function changes if we change the constraints.</p>
<p>Therefore, understanding what the multiplier means takes an understanding of the problem that it is being applied.  In some cases, it may only be a scaling value; in others, it may have a more significant meaning. </p>
<h2>Impact on Computers</h2>
<p>The Lagrangian Multiplier is useful in the field of optimal control theory.  The multipliers can be interpreted as a cost ate variables.  In Pontryagin&#8217;s maximum principle (or minimum principle), applied to deterministic problems, yields the same solution as many of the Bellman principles of Dynamic Programming, without the curse of dimensionality.  The multiplier then works as a means of deriving the optimal minimum or maximum in control theory in a quicker means than Bellman&#8217;s equations for dynamic programming in higher dimensional spaces. [4]</p>
<h2>Works Cited:</h2>
<p>[1] Vapnyarkii, I.B., &#8220;Lagrange multipliers&#8221; in <em>Encyclopaedia of Mathematics</em>, New York: Springer, 2001.<br />
[2] Hahn, K. <em>Lagrange Multiplier Method for finding Optimums</em> Internet: <a href="http://www.karlscalculus.org/pdf/lagrange.pdf" title="Lagrange Multiplier Method for finding Optimums">http://www.karlscalculus.org/pdf/lagrange.pdf</a> [September 5, 2011]<br />
[3] Jensen, S. <em>An Introduction to Lagrange Multipliers</em> Internet: <a href="http://www.slimy.com/~steuard/teaching/tutorials/Lagrange.html" title="An Introduction to Lagrange Multipliers">http://www.slimy.com/~steuard/teaching/tutorials/Lagrange.html</a> [September 5, 2011]<br />
[4] Todorov, E. &#8220;Optimal Control Theory&#8221; in Bayesian Brain, Boston: MIT Press, 2006.</p>
]]></content:encoded>
			<wfw:commentRss>http://kreese.net/blog/2011/09/05/lagrangian-optimization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generating Musical Rhythms</title>
		<link>http://kreese.net/blog/2010/03/27/generating-musical-rhythms/</link>
		<comments>http://kreese.net/blog/2010/03/27/generating-musical-rhythms/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 07:04:42 +0000</pubDate>
		<dc:creator>Kristopher Reese</dc:creator>
				<category><![CDATA[Music Generation]]></category>
		<category><![CDATA[Music]]></category>
		<category><![CDATA[Rhythm]]></category>

		<guid isPermaLink="false">http://kreese.net/?p=454</guid>
		<description><![CDATA[During my research for my thesis on Algorithmically Generated Tonal Music, I was fortunate to run across the research from McGill University, called &#8220;The Euclidean Algorithm Generates Traditional Musical Rhythms&#8220;, that uses a modified version of the Euclidean Algorithm called &#8230; <a href="http://kreese.net/blog/2010/03/27/generating-musical-rhythms/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>During my research for my thesis on Algorithmically Generated Tonal Music, I was fortunate to run across the research from McGill University, called &#8220;<a title="The Euclidean Algorithm Generates Traditional Musical Rhythms" href="http://cgm.cs.mcgill.ca/~godfried/publications/banff.pdf">The Euclidean Algorithm Generates Traditional Musical Rhythms</a>&#8220;, that uses a modified version of the Euclidean Algorithm called the Bjorklund algorithm.  The Euclidean Algorithm is one of the oldest algorithms in existence and was proposed by Euclid in his &#8220;<em>Elements</em>&#8221; Books VII and X.  This algorithm is used to find the Greatest Common Divisor of two numbers.</p>
<p>The Euclidean Algorithm is relatively simple to program. Below is an implementation of the Euclidean Algorithm in Java.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> Euclid<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> m, <span style="color: #000066; font-weight: bold;">int</span> k<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>k<span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">return</span> m<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">Euclid</span><span style="color: #009900;">&#40;</span>k, m<span style="color: #339933;">%</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The Bjorklund algorithm uses a similar concept to the Euclidean algorithm, but is used to distribute the zeros in a binary set evenly.  The simplest, visual way of thinking about the Bjorklund can be described as thinking of a binary set as columns.  The example below shows a Euclidean set of (4, 6)  which we can describe as four 1s and six 0s.  We therefore can create the initial binary set of &#8220;1111000000&#8243;.  From here we choose the smallest of the two numbers and move that number of zeros at the end of the set to the first 1 to n columns.  We update the numbers using the Euclidean Algorithm and continue this step until we are left with 0 or 1 column left, then we concatenate the remaining columns into a new set.  A visual example of the (4,6) version of the Bjorklund algorithm is found below.</p>
<pre>1111000000

111100
0000

1111
0000
00

11
00
00
11
00

1001010010</pre>
<p>This algorithm was originally used with the operation of the components on the spallation neutron source (SNS) accelerators in nuclear physics.  However, if we say that every one is associated with an accented note and the zeroes are unaccented, or rests, we can use this to generate the general rhythms and various world music rhythms.  For this reason, the Bjorklund algorithm is a unique and powerful algorithm in any music generator.  Below is an example Bjorklund algorithm implemented in Java (Special Thanks to Douglas Ferguson for catching some issues in the code and helping to resolve those issues):</p>
<div class="attachments"><dl class="attachments attachments-large"><dt class="icon"><a title="Bjorklund" href="http://kreese.net/blog/2010/03/27/generating-musical-rhythms/?aid=1099&amp;sa=0"><img src="http://kreese.net/wordpress/wp-content/plugins/eg-attachments/img/flags/zip.png" width="48" height="48" alt="Java Implementation of the Bjorklund Algorithm for generating Rhythms" /></a></dt><dd class="caption"><strong>Caption</strong> : <a title="Bjorklund" href="http://kreese.net/blog/2010/03/27/generating-musical-rhythms/?aid=1099&amp;sa=0">Generating Rhythms: Bjorklund</a><br /><strong>Description</strong> : Java Implementation of the Bjorklund Algorithm for generating Rhythms</dd></dl></div>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Bjorklund
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> ArrayList<span style="color: #339933;">&lt;</span>Boolean<span style="color: #339933;">&gt;</span> rhythm <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Boolean<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> pauses, per_pulse, remainder, steps, pulses, noskip, skipXTime<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">boolean</span> switcher<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Bjorklund<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> pulses, <span style="color: #000066; font-weight: bold;">int</span> steps<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">steps</span> <span style="color: #339933;">=</span> steps<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span> <span style="color: #339933;">=</span> pulses<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pauses</span> <span style="color: #339933;">=</span> steps <span style="color: #339933;">-</span> pulses<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">switcher</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span> <span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pauses</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">switcher</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	    <span style="color: #666666; font-style: italic;">// XOR swap pauses and pulses</span>
	    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pauses</span> <span style="color: #339933;">^=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span><span style="color: #339933;">;</span>
	    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span> <span style="color: #339933;">^=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pauses</span><span style="color: #339933;">;</span>
	    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pauses</span> <span style="color: #339933;">^=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">per_pulse</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pauses</span> <span style="color: #339933;">/</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">remainder</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pauses</span> <span style="color: #339933;">%</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">noskip</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">remainder</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span> <span style="color: #339933;">/</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">remainder</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">skipXTime</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">noskip</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span> <span style="color: #339933;">-</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">remainder</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">noskip</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">buildRhythm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">switcher</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// XOR swap pauses and pulses</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pauses</span> <span style="color: #339933;">^=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span> <span style="color: #339933;">^=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pauses</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pauses</span> <span style="color: #339933;">^=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Bjorklund<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> pulses, <span style="color: #000066; font-weight: bold;">int</span> steps, <span style="color: #003399;">String</span> expected<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#40;</span>pulses, steps<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        autorotate<span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> buildRhythm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">int</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> skipper <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">steps</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>count <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rhythm</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">switcher</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                count <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">per_pulse</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">remainder</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> skipper <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	            count<span style="color: #339933;">++;</span>
	            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">remainder</span><span style="color: #339933;">--;</span>
                    skipper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">skipXTime</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">noskip</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">skipXTime</span><span style="color: #339933;">--;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                    skipper<span style="color: #339933;">--;</span>
                <span style="color: #009900;">&#125;</span>
	    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rhythm</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">switcher</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		count<span style="color: #339933;">--;</span>
	    <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> ArrayList<span style="color: #339933;">&lt;</span>Boolean<span style="color: #339933;">&gt;</span> getRhythm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rhythm</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getRhythmSize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rhythm</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> autorotate<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> expected<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">boolean</span> verified <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> size <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rhythm</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> rotate <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rotateRightByPulses</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> found <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getRhythmString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>found.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> rotate <span style="color: #339933;">&lt;</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rotateRightByPulses</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            found <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getRhythmString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>found.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                verified <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>verified<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rhythmic string passed cannot be generated from E(&quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">steps</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> rotateRightByBits<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> numBits<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">Collections</span>.<span style="color: #006633;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rhythm</span>, numBits<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> rotateRightByPulses<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> numPulses<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> numPulses<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000066; font-weight: bold;">int</span> rotater <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rhythm</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	    <span style="color: #000066; font-weight: bold;">int</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rhythm</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>rotater<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		rotater<span style="color: #339933;">--;</span>
		count<span style="color: #339933;">++;</span>
	    <span style="color: #009900;">&#125;</span>
	    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rotateRightByBits</span><span style="color: #009900;">&#40;</span>count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> getRhythmString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    	Iterator<span style="color: #339933;">&lt;</span>Boolean<span style="color: #339933;">&gt;</span> iterator <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rhythm</span>.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #003399;">StringBuffer</span> buffer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>iterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    		buffer.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>iterator.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;x&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>iterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    			buffer.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    		<span style="color: #009900;">&#125;</span>
    	<span style="color: #009900;">&#125;</span>
    	<span style="color: #000000; font-weight: bold;">return</span> buffer.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> print<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;:&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">steps</span> <span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; -&gt; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getRhythmString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> autoverify<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> expected<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">boolean</span> verified <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> size <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rhythm</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> rotate <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rotateRightByBits</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> found <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getRhythmString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>found.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> rotate <span style="color: #339933;">&lt;</span> size<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rotateRightByBits</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            found <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getRhythmString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>found.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;E(&quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">steps</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;) verified for &lt;&lt;&quot;</span> <span style="color: #339933;">+</span> found <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&gt;&gt; by rotating bits right &quot;</span><span style="color: #339933;">+</span>rotate<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; times&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                verified <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            rotate<span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>verified <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;missed E(&quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pulses</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">steps</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;) expected: &lt;&lt;&quot;</span><span style="color: #339933;">+</span> expected <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&gt;&gt; but found: &lt;&lt;&quot;</span><span style="color: #339933;">+</span>found<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;&gt;&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>An example of how to call this class:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> test
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        Bjorklund b <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bjorklund<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        b.<span style="color: #006633;">rotateRightByPulses</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">ArrayList</span> r <span style="color: #339933;">=</span> b.<span style="color: #006633;">getRhythm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Arrays</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>r.<span style="color: #006633;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://kreese.net/blog/2010/03/27/generating-musical-rhythms/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
