<?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>Planetmarshall &#187; Mathematics</title>
	<atom:link href="http://www.planetmarshall.co.uk/tag/mathematics/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.planetmarshall.co.uk</link>
	<description>Andrew Marshall&#039;s blog</description>
	<lastBuildDate>Thu, 10 Nov 2011 17:33:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Solving Listener Crossword 4151 with Mathematica</title>
		<link>http://www.planetmarshall.co.uk/2011/10/solving-listener-crossword-4151-with-mathematica/</link>
		<comments>http://www.planetmarshall.co.uk/2011/10/solving-listener-crossword-4151-with-mathematica/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 09:29:25 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[Mathematica]]></category>

		<guid isPermaLink="false">http://www.planetmarshall.co.uk/?p=1126</guid>
		<description><![CDATA[MathsJam is a monthly meeting for people interested in recreational mathematics, and drinking of beer. At the Edinburgh meeting in August, we were presented with this Listener Crossword, from The Times Crossword Club. At the time of writing, there are &#8230; <a href="http://www.planetmarshall.co.uk/2011/10/solving-listener-crossword-4151-with-mathematica/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/crossword/puzzle.png" title="Listener Crossword 4151" class="shutterset_singlepic181" >
	<img class="ngg-singlepic ngg-right" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/181__150x_puzzle.png" alt="Listener Crossword 4151" title="Listener Crossword 4151" />
</a>

<p class="pm_first"><a href="http://mathsjam.com/" target="_blank" title="MathsJam">MathsJam </a>is a monthly meeting for people interested in recreational mathematics, and drinking of beer. At the <a href="http://mathsjam.com/index.php?content=edinburgh" title="MathsJam Edinburgh" target="_blank">Edinburgh meeting</a> in August, we were presented with this <a href="http://www.listenercrossword.com/" title="The Listener Crossword" target="_blank">Listener Crossword</a>, from <a href="https://www.crosswordclub.co.uk/" title="The Times Crossword Club" target="_blank">The Times Crossword Club</a>. At the time of writing, there are plenty of solutions on the web from those who tackled the puzzle in the traditional manner of brain, pen and paper. I opted to cheat.<br />
<span id="more-1126"></span><br />
The description of the puzzle itself is quite complex, but in a nutshell the task is to substitute each of the given letters for one of the first ten prime numbers ( with no duplication ). The expression for each clue will then give a numerical solution. Substitute each digit in the solution with the first letter of its name and write the result in the crossword. There are two ways of substituting digits for letters, either in English or in German, but a solution to a given clue must be consistently one language or the other.</p>
<h2>An Algorithmic Solution</h2>
<p>I have decided to use <a href="http://www.wolfram.com/mathematica/" title="Wolfram Mathematica" target="_blank">Mathematica</a> due to its built in symbolic evaluation capabilities, but in principle any sufficiently powerful programming language would do. If you wish you can <a href="#Download">download</a> a copy of the mathematica notebook.</p>
<p>The goal of the algorithm will be to find two sets of rules that map the first 10 primes to the given letters, which give valid solutions. One straightforward way to achieve this is to enumerate every single possibility until we find two such sets. So how many possibilities are there?</p>
<p>There are <img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/a466372541e515c191b8f1f549969e84.png"title="10! = 3,628,800" alt="10! = 3,628,800" style="vertical-align:top ;"/> possible sets of rules to examine. However, for each of the 27 clues, the solution generated by each rule set can be one of two options: English or German. That means that the total number of candidate solutions is<br />
<img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/44a695f6b2ea985a651430e5605b725c.png"title="&#10;10!\times 2^{27}=487,049,291,366,400&#10;" alt="&#10;10!\times 2^{27}=487,049,291,366,400&#10;" style="vertical-align:top ;"/></p>
<p>This is what&#8217;s called the &#8216;solution space&#8217;, and at a rate of 1000 candidates per second, it would take over 15,000 years to examine them all. Clearly this kind of brute force enumeration is impractical.</p>
<p>However, most of these solutions will be invalid in some way. In fact, if we trust the crossword compilers, all but two of them will be invalid. Some rules will produce solutions of the wrong size, others may conflict with other solutions in the crossword, and some may be invalid from a mathematical point of view ( such as a division that does not produce a whole number ). If instead of trying to solve the puzzle in one go, we construct &#8216;partial&#8217; solutions in some systematic fashion, we could avoid exploring parts of the solution space that cannot possibly lead to a correct solution. This is the principle behind the <a href="http://en.wikipedia.org/wiki/Backtracking" title="Wikipedia article on Backtracking" target="_blank">backtracking search</a>, one of the cornerstones of artificial intelligence. </p>
<h2>Implementation of a Backtracking Search</h2>
<p>The notion of a partial solution comes quite easily when considering a crossword puzzle, as it is simply a puzzle that is incomplete &#8211; ie, the solutions to at least one clue has been filled in. As an example, consider clue 19 Down : <img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/372c8c0974114a670f97d71577e77b98.png"title="A^2" alt="A^2" style="vertical-align:top ;"/>.</p>
<p>There are ten ways of mapping the single letter in 19 down to each prime</p>
<pre class="brush: mathematica; title: ; notranslate">
In[1]  := rules = {{a -&gt; 2}, {a -&gt; 3}, {a -&gt; 5}, {a -&gt; 7}, {a -&gt; 11}, {a -&gt; 13}, {a -&gt;
   17}, {a -&gt; 19}, {a -&gt; 23}, {a -&gt; 29}}

In[2]  := a^2/.rules
Out[2] = {4, 9, 25, 49, 121, 169, 289, 361, 529, 841}
</pre>
<p>Since we know 19 Down must be three letters long, immediately we can discard the first four candidates. The remainder are all possibilities, since at this point we have no more information. Lets take the first matching possibility, <img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/129e6f0f7e0fd7ed8bd23d9eee7930c6.png"title="A^2=121" alt="A^2=121" style="vertical-align:top ;"/> given by the rule <img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/ce5e35c9cebf11ae5974aa56145381cb.png"title="A=11" alt="A=11" style="vertical-align:top ;"/>.</p>
<p>In English, this gives</p>
<pre class="brush: mathematica; title: ; notranslate">
In[3]  := IntegerDigits[121] /. {1 -&gt; &quot;O&quot;, 2 -&gt; &quot;T&quot;}
Out[3] = {&quot;O&quot;, &quot;T&quot;, &quot;O&quot;}
</pre>
<p>If we now look at 21 Across &#8211; <img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/8cdcc93f429c0e2dbee6d8a1b257f8b1.png"title="ABI" alt="ABI" style="vertical-align:top ;"/> &#8211; There are 2 symbols to map to the remaining 9 primes ( &#8220;A&#8221; has already been mapped to 11 by 19 Down ). The number of <em>ordered</em> k-subsets of n is given by,<br />
<img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/6d7a125c468b365be35d6b8195f692f0.png"title="&#10;_nP^k = \frac{n!}{(n-k)!}&#10;" alt="&#10;_nP^k = \frac{n!}{(n-k)!}&#10;" style="vertical-align:top ;"/></p>
<p>So we have <img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/0949100eaa2bca627621bb76f08199cd.png"title="9!/7!=90" alt="9!/7!=90" style="vertical-align:top ;"/> new sets of rules to examine</p>
<pre class="brush: mathematica; title: ; notranslate">
In[4] := rules = {a -&gt; 11, b -&gt; #[[1]], i -&gt; #[[2]]} &amp; /@
 Permutations[{2, 3, 5, 7, 13, 17, 19, 23, 29}, {2}]
Out[4] = {{a -&gt; 11, b -&gt; 2, i -&gt; 3}, {a -&gt; 11, b -&gt; 2, i -&gt; 5},...

In[5] := a b i/. rules
Out[5] = {66, 110, 154, 286, 374, 418, 506, 638, 66,...
</pre>
<p>Again, many of the partial solutions produced by these rules are invalid. Only 36 are the required 4 digits long, and we also now have information about the form of this solution. We know that the first letter is &#8220;T&#8221;, given to us by the puzzle setter, and also that the last letter is &#8220;T&#8221;, because of our solution to 19 Down : &#8220;OTO&#8221;.</p>
<pre class="brush: mathematica; title: ; notranslate">
In[6] := candidates = Select[a b i /. rules, Length[IntegerDigits[#]] == 4 &amp;]
Out[6] = {1045, 1265, 1595, 1001, 1309, 1463, 1771, 2233,...

In[7] := englishRules = {0 -&gt; &quot;Z&quot;, 1 -&gt; &quot;O&quot;, 2 -&gt; &quot;T&quot;, 3 -&gt; &quot;T&quot;,...
In[8] := germanRules = {0 -&gt; &quot;N&quot;, 1 -&gt; &quot;E&quot;, 2 -&gt; &quot;Z&quot;, 3 -&gt; &quot;D&quot;,...
In[9] := candidates = StringJoin @@@ ((IntegerDigits /@ candidates)
   /. Join[english, german])
Out[9] = {&quot;OZFF&quot;, &quot;OTSF&quot;, &quot;OFNF&quot;, &quot;OZZO&quot;, &quot;OTZN&quot;, &quot;OFST&quot;,...

In[10] := Select[candidates, StringMatchQ[#, RegularExpression[&quot;T..T&quot;]] &amp;]
Out[10] = {&quot;TTTT&quot;, &quot;TFFT&quot;, &quot;TTTT&quot;, &quot;TFFT&quot;}
</pre>
<p>Note the duplicates. This is because <img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/234da07d44fe9b1f1a2d23844f177e16.png"title="B I = I B " alt="B I = I B " style="vertical-align:top ;"/>, but we cannot eliminate them because the rules used to produce them are different, and so will lead to different partial solutions. By applying the constraints systematically, we have reduced the number of potential solutions for 21 Across from 90 to just 4.</p>
<p>
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/crossword/alg.png" title="A portion of the backtracking search algorithm." class="shutterset_singlepic178" >
	<img class="ngg-singlepic ngg-left" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/178__256x_alg.png" alt="Algorithm" title="Algorithm" />
</a>
We then repeat this process in a recursive manner, using one of the valid solutions to 21 across, and the rules that  were used to generate it, to examine the next clue. We continue in this fashion until either the crossword is filled, or we reach a dead end ( which is more likely ). If we reach a dead end, we backtrack ( hence the name ) to one of the possibilities we haven&#8217;t tried yet. </p>
<p>Why is this approach any more efficient than the brute force enumeration described above? The key is the generation of partial solutions in a tree like fashion, and the ability to eliminate them when we know that they could not possibly form part of a full solution. When we eliminate such a partial solution because it didn&#8217;t satisfy the constraints, we eliminate all the possible solutions leading from that branch. For example, when we eliminated the rule <img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/1e1a0e85efabbd0e97200a1aada32fa1.png"title="A=2" alt="A=2" style="vertical-align:top ;"/> because it produced a solution to 19 Down that was too short, it eliminates not just one solution but all <img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/a28fcb488e01635362c81adeeb069a7f.png"title=" 9!\times 2^{27}=48,704,929,136,640 " alt=" 9!\times 2^{27}=48,704,929,136,640 " style="vertical-align:top ;"/> solutions incorporating that rule. Not bad for a single calculation.</p>
<h2>The Solution</h2>
<p>In abbreviated form, the algorithm looks something like this.</p>
<pre class="brush: mathematica; title: ; notranslate">
ExamineNextCandidate[{solved_, unsolved_},
  rules_] := {solved, rules} /; Length[unsolved] == 0

ExamineNextCandidate[{solved_, unsolved_}, rules_] := Module[{...},
   {clueIdx, cluePattern} = First[unsolved];
  nextRuleSet =
   GenerateRules[GetSymbolsInClue[ClueExpression[clueIdx]], rules];
  solution = Null;
  While[Length[nextRuleSet] &gt; 0 &amp;&amp; solution == Null,
   nextRule = First[nextRuleSet];
   nextRuleSet = Rest[nextRuleSet];
   candidates =
    StringJoin @@@ (IntegerDigits[
        ClueExpression[clueIdx] /. nextRule] /. {english, german});
   candidates = FilterCandidatesByConstraints[candidates];
   If[Length[candidates] &gt; 0,
     nextPatterns =
      GenerateNewPatterns[unsolved, {#, clueIdx}] &amp; /@ candidates;
     nextArgs = {Append[solved, First[#]], Rest[#]} &amp; /@ nextPatterns;
     While[solution == Null &amp;&amp; Length[nextArgs] &gt; 0,
      solution = ExamineNextCandidate[First[nextArgs], nextRule];
      nextArgs = Rest[nextArgs]
      ];
     ]
    ];
  solution
</pre>
<p>Note that the function <code>ExamineNextCandidate</code> calls itself until the pattern on the list of unsolved solutions is met, ie the list is empty. We call this function using an initially empty rules list, and the patterns provided by the puzzle setter.</p>
<pre class="brush: mathematica; title: ; notranslate">
In[1]:=ExamineNextCandidate[{{}, {...,{20, &quot;..&quot;}, {21, &quot;T...&quot;}, {23, &quot;....&quot;},...}, {}]
Out[1]={...,{20, &quot;FE&quot;}, {21, &quot;TTTT&quot;}, {23, &quot;FZFO&quot;},...}
</pre>
<p>
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/crossword/clue1.png" title="The solution for the first grid" class="shutterset_singlepic179" >
	<img class="ngg-singlepic ngg-left" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/179__256x_clue1.png" alt="Solution 1" title="Solution 1" />
</a>
 On my dual core netbook ( A 2010 Alienware M11xR2 ), this took just over 2 seconds to find a solution.</p>
<p>For the second puzzle, we alter the pattern to specify that the first letter of 21 Across ( and last of 9 Down ) can be any letter <em>except</em> &#8216;T&#8217;, using the regular expression <code>"[^T]..."</code>. In the code snippets, I have abbreviated the output somewhat. See the attached Mathematica notebook file for full details.</p>
<pre class="brush: mathematica; title: ; notranslate">
In[2]:=ExamineNextCandidate[{{}, {...,{20, &quot;..&quot;}, {21, &quot;[^T]...&quot;}, {23, &quot;....&quot;},...}, {}]
Out[2]={...,{20, &quot;EE&quot;}, {21, &quot;FFTT&quot;}, {23, &quot;FZFO&quot;},...}
</pre>
<p>
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/crossword/clue2.png" title="The solution for the second grid" class="shutterset_singlepic180" >
	<img class="ngg-singlepic ngg-right" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/180__256x_clue2.png" alt="Solution 2" title="Solution 2" />
</a>
 Finally, the last part of the puzzle is to complete the following task:</p>
<blockquote><p>&#8220;Solvers must identify the English transcriptions of the answers entered using German in one of the grids, which, in clue order, form two words of equal length that are to be entered below the grids, one on each side of the colon; this will relate to a claim made above.&#8221;
</p></blockquote>
<p>To identify the answers entered in German, we apply the rules returned by the solver to the original expressions and use the German digit replacement rules. We then compare these to the completed grid.</p>
<pre class="brush: mathematica; title: ; notranslate">
In[1]:={solution,rules}=ExamineNextCandidate[{{}, {...,{20, &quot;..&quot;}, {21, &quot;T...&quot;}, {23, &quot;....&quot;},...}, {}]
Out[1]={...,{a -&gt; 11, b -&gt; 17, d -&gt; 19, e -&gt; 7,...}}

In[2]:=Intersection[(clues /. rules1) /. {a_, b_} :&gt; {a,
    IntegerDigits[b] /. german}, solution][[;;,1]]
Out[2]={10, 11, 13, 16, 25, 29, 30}

In[3]:=(Select[clues, MemberQ[%2, First[#]] &amp;] /. rules) /. {a_, b_} :&gt; {a,
    StringJoin @@ (IntegerDigits[b] /. english)}
Out[3]={{10, &quot;SF&quot;}, {11, &quot;NF&quot;}, {13, &quot;FOO&quot;}, {16, &quot;TNOTE&quot;}, {25, &quot;NON&quot;}, {29,
   &quot;SENSE&quot;}, {30, &quot;SSF&quot;}}
</pre>
<p>Having identified those clues, we reevaluate the expressions this time using the English rules. There are 4 clues that are the same in both German and English, but it is pretty clear that the required words are &#8220;<strong>Footnote</strong>&#8221; and &#8220;<strong>Nonsense</strong>&#8221;</p>
<h2>Download</h2>
<p><a name="Download"/><a href="http://www.planetmarshall.co.uk/code/xword/xword.nb" title="Mathematica Notebook" target="_blank">Mathematica Notebook (111K)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.planetmarshall.co.uk/2011/10/solving-listener-crossword-4151-with-mathematica/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drawing geodesic curves using the Bing maps Silverlight control</title>
		<link>http://www.planetmarshall.co.uk/2010/06/drawing-geodesic-curves-using-the-bing-maps-silverlight-control/</link>
		<comments>http://www.planetmarshall.co.uk/2010/06/drawing-geodesic-curves-using-the-bing-maps-silverlight-control/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 14:52:05 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[bing maps]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.planetmarshall.co.uk/?p=624</guid>
		<description><![CDATA[For an upcoming post I wanted to be able to plot the shortest routes between various positions on the Earth using the Bing Maps Silverlight control. Although since I started working on the problem Bing have provided a similar feature &#8230; <a href="http://www.planetmarshall.co.uk/2010/06/drawing-geodesic-curves-using-the-bing-maps-silverlight-control/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p class="pm_first">For an upcoming post I wanted to be able to plot the shortest routes between various positions on the Earth using the <a title="Bing Maps Silverlight Control" href="http://msdn.microsoft.com/en-us/library/ee681884.aspx" target="_blank">Bing Maps Silverlight control</a>. Although since I started working on the problem Bing have provided a similar feature with their <a title="New Bing Map Apps: Gas Prices, Distance Calculator and Parking Finder" href="http://www.bing.com/community/blogs/maps/archive/2010/05/19/new-bing-map-apps-gas-prices-distance-calculator-and-parking-finder.aspx" target="_blank">Distance Calculator App</a>, the functions are not available for reuse via the public API. Interested developers may just want to skip the maths and just download the code.</p>
<p><a title="Source code in zip file for Silverlight 4.0" href="http://www.planetmarshall.co.uk/code/geodesic.zip">Geodesic source code for Silverlight 4.0</a></p>
<h3>Geodesics</h3>

<a href="http://www.planetmarshall.co.uk/wp-content/gallery/geodesic/geo.png" title="Geodesic showing the shortest distance between Cambridge and New York" class="shutterset_singlepic118" >
	<img class="ngg-singlepic ngg-right" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/118__150x_geo.png" alt="Geodesic " title="Geodesic " />
</a>

<p>The shortest path between two points on an arbitrary surface is called a <a title="Geodesic from MathWorld" href="http://mathworld.wolfram.com/Geodesic.html" target="_blank">Geodesic</a>, and on a sphere, it is a <a title="Great Circle from MathWorld" href="http://mathworld.wolfram.com/GreatCircle.html" target="_blank">Great Circle</a>. Modelling the surface of the earth as a perfect sphere, the shortest distance between any two locations on the surface is then described by a section of a Great Circle, ie an arc that lies on the plane that is described by the vectors between its start and end points and the Earth&#8217;s centre ( see figure 1 ).</p>
<p>With this information, one way ( and the way I have adopted ) to plot such a curve is as follows:</p>
<ol>
<li>Generate the points of the curve in two dimensions using the parametric equation of a circle.</li>
<li>Transform the plane of the 2d curve into 3D space such that it intersects the end points on the sphere, and the sphere&#8217;s centre.</li>
<li>Project the transformed points back into 2D space using the Mercator projection equations.</li>
</ol>
<p><span id="more-624"></span><br />
[silverlight: Geodesic.xap,520,384,false]</p>
<h3>A Parametric Representation of a Great Circle</h3>
<p>The <a title="Mercator Projection from MathWorld" href="http://mathworld.wolfram.com/MercatorProjection.html">Mercator projection</a> gives the 2D rectilinear coordinates (x,y) as a function of the latitude and longitude of a point on a sphere.  However, it is easier to draw the point using a typical drawing API, if we have a representation that gives each point of the curve in terms of a single parameter. To derive such a function, we observe that the parametric equation for a circle is given by</p>
<p>$$!begin{pmatrix} x \ y end{pmatrix} = begin{pmatrix} r cos t \ r sin t end{pmatrix} $$</p>
<p>Since a great circle is a rigid transformation of a circle in 3D space, it can also be represented as a function of a single parameter</p>
<p>$$!begin{pmatrix} x \ y \ z end{pmatrix} = mathbf{R}cdotbegin{pmatrix} r cos t \ r sin t end{pmatrix} $$</p>
<p>where $$mathbf{R}$$ is a 2&#215;3 matrix that transforms the plane circle to a location on a sphere. Using <a title="Spherical Coordinates from MathWorld" href="http://mathworld.wolfram.com/SphericalCoordinates.html">spherical coordinates</a>, the Mercator projection of the curve specified above is then<br />
$$!mathbf{C}(t) =begin{pmatrix} lambda \ tanh^{-1} left(sin phi right)end{pmatrix} = begin{pmatrix} tan^{-1}left(y/xright)\ tanh^{-1}z end{pmatrix}$$<br />
Where $$lambda$$ and $$phi$$ are the longitude and latitude of the point to be projected, respectively. Writing the equation out in full gives,</p>
<p>$$!mathbf{C}(t) =begin{pmatrix}</p>
<p>tan^{-1}left(frac{R_{2,1}cos t + R_{2,2}sin t}{R_{1,1}cos t + R_{1,2}sin t}right)\<br />
tanh^{-1}(R_{3,1}cos t +R_{3,2}sin t)<br />
end{pmatrix}<br />
$$</p>
<p>Now that we have a suitable parametric equation, we can draw the geodesic with a series of connected line segments by varying the parameter, t.</p>
<blockquote><p>Mathematical note &#8211; the parameterization given by this expression is highly non-uniform, meaning that there are many more points generated in some parts of the curve than in others. The mathematics of generating uniform ( or natural ) parameterizations belongs to the field of differential curve geometry and is beyond the scope of this article ( and my brain ).</p></blockquote>
<h3>Implementation notes</h3>
<p>
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/geodesic/maps.jpg" title="The upper image shows what happens when aa curve wraps around the map boundary. The lower image shows the effect of splitting the curve at the boundary." class="shutterset_singlepic141" >
	<img class="ngg-singlepic ngg-left" src="http://www.planetmarshall.co.uk/index.php?callback=image&amp;pid=141&amp;width=150&amp;height=&amp;mode=" alt="Geodesic with Split" title="Geodesic with Split" />
</a>
 Inevitably, the mathematics alone is not sufficient to produce an implementation of a reuseable class for the Bing Silverlight control. There are two main issues to resolve; firstly, it is not immediately obvious how to derive from the provided <a title="Documentation on MapShapeBase from MSDN" href="http://msdn.microsoft.com/en-us/library/microsoft.maps.mapcontrol.core.mapshapebase.aspx" target="_blank"><code>MapShapeBase</code></a> class to create new shape overlays and secondly, how to handle drawing the curves when they &#8216;wrap&#8217; beyond the map&#8217;s viewable area ( this is easier to illustrate than to describe &#8211; see the figure opposite ).</p>
<h4>Inheriting from <code>MapShapeBase</code></h4>
<p>I must confess that I cheated slightly in implementing the <code>MapGeodesicPath</code> class, in that I used <a title="Reflector from Red-Gate. Stop sending them hate mail!" href="http://www.red-gate.com/products/reflector/" target="_blank">Reflector</a> to peer into the implementation of the base class. The existing derivations of this class simply defer to <code>MapShapeBase</code> for most of the work, which they can do since for a <code>MapPolygon</code> and <code>MapPolyline</code> there is a one-to-one relationship between <code>Locations</code>, latitude and longitude points on the map and <code>Points</code>, the actual 2D cartesian coordinates used to draw the shape. For the Geodesic, this is not the case, because we only want to specify the start and ending points of the curve, not every point in between. A solution is to delegate the point generation code to a secondary class, one that can be independently tested.</p>
<h4>Splitting the curves at the map boundary</h4>
<p>When the curves wrap around the map projection, they need to be split at the boundary. This is done by finding the parameter t for the longitude value of the boundary. Where the longitude value is +/- 180, this is straightforward as the equation above reduces to,<br />
$$!t = -tan^{-1}frac{R_{1,2}}{R_{2,2}} $$<br />
For other longitude values, we simply offset the longitude values by the required amount, and calculate the value of t for the new matrix.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.planetmarshall.co.uk/2010/06/drawing-geodesic-curves-using-the-bing-maps-silverlight-control/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Reaction-Diffusion Models</title>
		<link>http://www.planetmarshall.co.uk/2009/03/reaction-diffusion-models/</link>
		<comments>http://www.planetmarshall.co.uk/2009/03/reaction-diffusion-models/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 00:27:03 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Differential Equations]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.planetmarshall.co.uk/?p=124</guid>
		<description><![CDATA[One of Alan Turing&#8216;s many contributions to mathematics and science during the 20th century was his 1952 paper on &#8220;The Chemical Basis of Morphogenesis&#8221; in which he suggested that a simple model of coupled differential equations could account for pattern &#8230; <a href="http://www.planetmarshall.co.uk/2009/03/reaction-diffusion-models/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/maze.png" title="" class="shutterset_singlepic68" >
	<img class="ngg-singlepic ngg-right" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/68__x96_maze.png" alt="maze.png" title="maze.png" />
</a>

<p class="pm_first"><span>O</span>ne of <a target="_new" href="http://en.wikipedia.org/wiki/Alan_Turing">Alan Turing</a>&#8216;s many contributions to mathematics and science during the 20th century was his <a href="#1_turing">1952 paper</a> on &#8220;The Chemical Basis of Morphogenesis&#8221; in which he suggested that a simple model of coupled differential equations could account for pattern formation in natural processes such as those found on animal coats. Such models are known as Reaction-Diffusion models, and take the following general form<br />
<img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/a7d70e02c93cb96e6b0012ecebb75b57.png"title="&#10;\frac{\partial}{\partial t}\mathbf{q}=\mathbf{D}\nabla^2\mathbf{q}+\mathbf{R}(\mathbf{q})&#10;" alt="&#10;\frac{\partial}{\partial t}\mathbf{q}=\mathbf{D}\nabla^2\mathbf{q}+\mathbf{R}(\mathbf{q})&#10;" style="vertical-align:top ;"/></p>
<p><span id="more-124"></span></p>
<p>The equation describes how the concentration of each chemical components in q evolves as a function of the other components. I have chosen to illustrate the Gray-Scott model; the physical derivation of the reactant term is described in detail in <a href="#2_grayscott">[2]</a>, the addition of the diffusion term and the resultant behaviour in <a href="#3_pearson">[3]</a>. The model has two chemical components, U and V and is described as follows.<br />
<img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/0066159d03a0b2aa45c47b46c63ac571.png"title="&#10;\begin{align*}&#10;\frac{\partial}{\partial t}U&amp;#038;=D_u\nabla^2U-UV^2+F(1-U) \\&#10;\frac{\partial}{\partial t}V&amp;#038;=D_v\nabla^2V+UV^2-(F+k)V&#10;\end{align*}&#10;" alt="&#10;\begin{align*}&#10;\frac{\partial}{\partial t}U&amp;#038;=D_u\nabla^2U-UV^2+F(1-U) \\&#10;\frac{\partial}{\partial t}V&amp;#038;=D_v\nabla^2V+UV^2-(F+k)V&#10;\end{align*}&#10;" style="vertical-align:top ;"/></p>
<p>The model is what is knows as an activator-inhibitor model, in which one chemical acts to inhibit the growth of the other.</p>
<h3>Simulation</h3>
<p>The simulation is implemented with <a href="http://www.silverlight.net" target="_new">Silverlight 2.0</a>. For full details of the implementation see the separate <a href="http://www.planetmarshall.co.uk/index.php/2009/04/implentation-of-the-reaction-diffusion-simulation/">post</a>. For guidance in parameter selection, see <a href="#3_pearson">[3]</a>, or just select a preset and modify it. Note that the Png image generation feature currently requires the <a target="_new" href="http://www.mozilla.com/firefox/">Firefox</a> browser.</p>
<p>[silverlight: ReactionDiffusion.xap, 450, 300]</p>
<h3>Gallery</h3>
<div class="ngg-galleryoverview" id="ngg-gallery-5-124">

	<!-- Slideshow link -->
	<div class="slideshowlink">
		<a class="slideshowlink" href="http://www.planetmarshall.co.uk/2009/03/reaction-diffusion-models/?show=slide">
			[Show as slideshow]		</a>
	</div>

	
	<!-- Thumbnails -->
		
	<div id="ngg-image-65" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/blood.png" title=" " class="shutterset_set_5" >
								<img title="blood.png" alt="blood.png" src="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/thumbs/thumbs_blood.png"  />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-66" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/cells.png" title=" " class="shutterset_set_5" >
								<img title="cells.png" alt="cells.png" src="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/thumbs/thumbs_cells.png"  />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-67" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/clouds.png" title=" " class="shutterset_set_5" >
								<img title="clouds.png" alt="clouds.png" src="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/thumbs/thumbs_clouds.png"  />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-68" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/maze.png" title=" " class="shutterset_set_5" >
								<img title="maze.png" alt="maze.png" src="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/thumbs/thumbs_maze.png"  />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-69" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/waves.png" title=" " class="shutterset_set_5" >
								<img title="waves.png" alt="waves.png" src="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/thumbs/thumbs_waves.png"  />
							</a>
		</div>
	</div>
	
		
 	 	
	<!-- Pagination -->
 	<div class="ngg-clear"></div> 	
</div>

<h3>References</h3>
<ol>
<li><a name="1_turing"></a>Turing,A.,1952. <a target="_new" href="http://www.dna.caltech.edu/courses/cs191/paperscs191/turing.pdf">The chemical basis of morphogenesis [pdf]</a>. <i>Philosophical Transactions of the Royal Society</i>, 237 pp.37-72
</li>
<li><a name="2_grayscott"></a>Gray,P. and Scott,S.K. 1985. <a href="http://www.jstor.org/stable/2881810" target="_new">Sustained Oscillations and Other Exotic Patterns of Behavior in Isothermal Reactions.</a> <i>Journal of Physical Chemistry</i>, 89 pp.22-32
</li>
<li><a name="3_pearson"></a>Pearson,J.,1993. <a href="http://www.jstor.org/stable/2881810" target="_new">Complex patterns in a simple system.</a> <i>Science</i>, 261(5118) pp.189-192
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.planetmarshall.co.uk/2009/03/reaction-diffusion-models/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>URM Emulator</title>
		<link>http://www.planetmarshall.co.uk/2009/01/urm-emulator/</link>
		<comments>http://www.planetmarshall.co.uk/2009/01/urm-emulator/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 23:22:34 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[Open University]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.planetmarshall.co.uk/?p=24</guid>
		<description><![CDATA[The Unlimited Register Machine (URM), designed by Nigel Cutland, is an abstraction of a computer similar to the Turing Machine &#8211; but somewhat easier to get to grips with and more in tune with the operation of a modern computer &#8230; <a href="http://www.planetmarshall.co.uk/2009/01/urm-emulator/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p class="pm_first"><span>T</span>he Unlimited Register Machine (URM), designed by <a href="http://www.cambridge.org/us/catalogue/catalogue.asp?isbn=9780521294652" target="_new">Nigel Cutland</a>, is an abstraction of a computer similar to the <a href="http://en.wikipedia.org/wiki/Turing_machine" target="_new">Turing Machine</a> &#8211; but somewhat easier to get to grips with and more in tune with the operation of a modern computer processor.</p>
<p>It also happens to be the model used by the Open University&#8217;s course on <a href="http://www3.open.ac.uk/courses/bin/p12.dll?C01M381" target="_new">Mathematical Logic and Number Theory</a>, and is the reason for this article since I studied the course in 2007.</p>
<p><span id="more-24"></span></p>
<h3>Emulator</h3>
<p>The implementation details of the emulator are quite interesting, using a mixture of Microsoft technologies including F# and Silverlight, however I will save that for another article for those interested in such things. To use the Emulator just hit Reset, enter the program and initial registers and click Step to trace through the program.<br />
[silverlight: Urm.App.xap,400,300,false] </p>
]]></content:encoded>
			<wfw:commentRss>http://www.planetmarshall.co.uk/2009/01/urm-emulator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

