<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Antimatroid, The</title>
	<atom:link href="http://antimatroid.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://antimatroid.wordpress.com</link>
	<description>thoughts on mathematics, computer science and business</description>
	<lastBuildDate>Sun, 01 Jan 2012 14:16:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='antimatroid.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Antimatroid, The</title>
		<link>http://antimatroid.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://antimatroid.wordpress.com/osd.xml" title="Antimatroid, The" />
	<atom:link rel='hub' href='http://antimatroid.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Thoughts on delivering software</title>
		<link>http://antimatroid.wordpress.com/2012/01/01/source-code-organization/</link>
		<comments>http://antimatroid.wordpress.com/2012/01/01/source-code-organization/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 14:00:16 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=1581</guid>
		<description><![CDATA[Preface A lot can happen in four years. We get used to its passing as a milestone marking a transition from one stage of life to another. I was thinking about the last four some odd years since I&#8217;d graduated from college and came to realize that while I&#8217;ve learned a lot since then, I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1581&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>Preface</h3>
<p>A lot can happen in four years. We get used to its passing as a milestone marking a transition from one stage of life to another. I was thinking about the last four some odd years since I&#8217;d graduated from college and came to realize that while I&#8217;ve learned a lot since then, I still have a ways to go. One recurring question I have floating around my head all this time is how to deliver a complete product. At the end of college I had the theoretical side of the story, but I only had limited exposure doing internships and consulting.</p>
<p>My first job after college was working for a small company focusing on healthcare. Our development group was just a couple months old and I was the second developer brought on board. We started out as a group of cowboys doing what was necessary to retain customers and bring on new ones. Despite the chaos, lack of requirements and ever shifting priorities, we managed to make the company successful and a leader in its industry. During that time, I feel that I have learned a lot about how to deliver software in the face of uncertainty. </p>
<p>More importantly, I&#8217;ve learned how be more than just a  developer and grow into someone trusted to autonomously improve the business with my vision and execution. I&#8217;m going to cover a few choice topics that I feel have had the biggest impact on how I deliver software. Things that I feel answer that burning question of how to deliver a complete product. This isn&#8217;t intended to be comprehensive, but it should give someone where I was four years ago a little insight into how to get a quality product out the door even when it feels like it&#8217;ll never happen.</p>
<h3>Communication</h3>
<p>When you are sitting in front of a computer, it&#8217;s easy to get excited and starting writing whatever happens to pop into mind and race its way down your fingers. Few things are more satisfying as a developer than falling into flow and building something to completion- going through the motions of refactoring, testing and becoming convinced that you&#8217;ve written something solid. You eagerly present your product to the stakeholders. They look back at you and say, &#8220;this isn&#8217;t at all what we asked for&#8221;. </p>
<p>It is easy to make this mistake. Not taking the time to fully understand the stakeholder&#8217;s needs and more importantly, making sure they do too. After all, the biggest reason why software fails to meet expectations is because expectations weren&#8217;t established and requirements weren&#8217;t well defined. The source of these failures lies in failures in communication. It should seem obvious, but any software endeavor that involves more than one person relies on thorough communication to execute a shared goal- to bring a solution to market in a timely manner.</p>
<p>To make sure that you avoid this pitfall, take the time to work with your stakeholders. An hour of talking and planning can prevent you from wasting a day of misdirected development time. Focus on the domain problem and its solution. How the solution is implemented, is largely irrelevant compared to its completeness of requirements. To ensure that you and your stakeholder have the same view of the product, produce a dossier similar to a user&#8217;s guide that outlines how the solution behaves. Once defined, produce a quick prototype and present it to the stakeholder. Upon their approval, implement the product.</p>
<h3>Simplicity</h3>
<p>Now, simplicity doesn&#8217;t imply mediocre no more than complexity implies extraordinary. As Antoine de Saint-Exupéry said in <i>Wind, Sand and Stars</i>, &#8220;<i>Il semble que la perfection soit atteinte non quand il n&#8217;y a plus rien à ajouter, mais quand il n&#8217;y a plus rien à retrancher</i>&#8220;. Simplicity is simply the path to perfection and removing that which is unnecessary. Much of what can be said about delivering software can be said about managing complexity. Markets change and so too do the demands on products. As a consequence, so too do the demands on code. How much that code has to change depends largely on how well it was written.</p>
<p>Complexity encroaches on software in one of two ways. A product that grows organically without any direction and second through poor coding practices and design. This lack of direction comes from stakeholders committing to one-off functionality without thinking how it contributes to a uniform product. In the second example, an unorthodox solution results in a product that can never be changed and a bad set of abstractions make it difficult to change anything.</p>
<p>What can be done to avoid complexity and enable simplicity? Focus on established principles of the field. Design components that are modular, capable only of doing one thing and doing it well. Interfaces between systems should be flexible in what they accept and resistant to change. Code should assert its assumptions and not be <i>ad hoc</i> in its solutions. Above all, code should be written with meaningful abstractions that represent the problem and take into consideration how that problem can mutate or be extended to other problems. Doing this intelligently, results in maintainable, reliable and predictable code -but more importantly- code that ships.</p>
<h3>Introspection</h3>
<p>Being a software professional requires a certain degree of introspection. Solving problems requires us to build complex models in our minds and walk around them to discover insights that lead to solutions. The ability to maintain these mental models is an important factor to our ability to deliver a product. Despite the amount of time spent in one&#8217;s mind, there is often a failure to think about how one carries out his or her job. Lucid requirements, minimal designs and all the theory in the world are useless if you don&#8217;t know how to keep up with the demands placed upon you.</p>
<p>What happens when you fail to keep up? You might think to spend an extra hour a day trying to catch up. This works for a bit, but not for long. You end up giving something up for that extra hour and usually it means sacrificing sleep, relationships, interests or your health. Doing this for prolonged periods leads to falling further and further behind until deadlines are racing past you, pounds piling up on your body and work invading every aspect of your life. Ultimately, it all leads to burnout.</p>
<p>To keep up with demands, and to get that product out the door, it is important to know yourself and set limitations. Figure out if you are most effective in the morning or night. Understand if you like switching between projects or like focusing on one project at a time. At the end of each day, write down what you&#8217;ve accomplished and what you hope to accomplish the next day. A list makes it easy for you to see that things are getting done and what you need to be working on. If you find yourself behind due to reasons beyond your control, negotiate with the stakeholders to extend the timeline. Nothing is ever set in stone.</p>
<h3>Epilogue</h3>
<p>I doubt there is an all encompassing answer to how to deliver a complete product. Each year I have a different take on the question and no doubt I will continue to in the years to come. I am certain however, that building a product takes more than just a few clever software professionals, it takes a broad set of skills and abilities from people of different backgrounds. Sales, management, marketing, accounting, information technology and many other disciplines contribute to the delivery of a complete product. Building a product with these groups in mind makes it easier to deliver a complete product but also one that is successful.</p>
<p>Do each of these groups have the tools they need to provide a quality service to customers? Is there a well defined process for supporting the product and escalating issues to development? Does sales and marketing have access to usage data to know what customers are doing with the product? Does accounting and payroll have the information they need to send out bills and payments for services rendered?  A product can really only be called complete if there is a set of processes, services and tools supporting it.</p>
<p>Take the time to really learn your company and industry. It&#8217;s easy enough to just be someone who spends all day writing code, but its just as easy to spend time learning what the business is really all about and what problems it aims to solve in its industry. Spend the time to learn what other groups in the company are up to and how their efforts are contributing to the success of your product. The technical mind is a great platform for spotting opportunities that can result in better products and ultimately a better company. When you have the big picture, there really isn&#8217;t anything holding you back from delivering a complete product.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/1581/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/1581/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/antimatroid.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/antimatroid.wordpress.com/1581/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/antimatroid.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/antimatroid.wordpress.com/1581/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/1581/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/1581/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/1581/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1581&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2012/01/01/source-code-organization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>
	</item>
		<item>
		<title>Space Cowboy: A Shoot’em up game in C#: Part 3</title>
		<link>http://antimatroid.wordpress.com/2011/05/01/space-cowboy-a-shoot%e2%80%99em-up-game-in-c-part-3/</link>
		<comments>http://antimatroid.wordpress.com/2011/05/01/space-cowboy-a-shoot%e2%80%99em-up-game-in-c-part-3/#comments</comments>
		<pubDate>Sun, 01 May 2011 14:00:10 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[C-Sharp]]></category>
		<category><![CDATA[Shoot'em up]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=2199</guid>
		<description><![CDATA[Introduction I&#8217;ve been working on a small video game the past few months and recent finished its development. You can read up on the original vision of the game and then check out how the prototype went. In this final installment of the series, I am going to present two sides of the application: the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=2199&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>I&#8217;ve been working on a small video game the past few months and recent finished its development. You can read up on the original <a href="http://antimatroid.wordpress.com/2011/01/01/space-cowboy-a-shootem-up-game-in-haskell-part-1/" target="blank">vision</a> of the game and then check out how the <a href="http://antimatroid.wordpress.com/2011/04/01/space-cowboy-a-shoot%e2%80%99em-up-game-in-haskell-part-2/" target="blank">prototype</a> went. In this final installment of the series, I am going to present two sides of the application: the first is the layout of the application and how some of the high-level organizational aspects were implemented. The second side of the application is the gameplay and how some of the more interesting aspects were implemented. Here is a full demo of the game:</p>
<span style="text-align:center; display: block;"><a href="http://antimatroid.wordpress.com/2011/05/01/space-cowboy-a-shoot%e2%80%99em-up-game-in-c-part-3/"><img src="http://img.youtube.com/vi/82F0qnR6Ee8/2.jpg" alt="" /></a></span>
<p></p>
<h3>User Interface</h3>
<p></p>
<h4>Design</h4>
<p>The majority of the application is based around the idea of stories and storyboards. Stories are analogous to tasks and storyboards to work flows. The transition between stories on a storyboard is initiated by the end user or by an automatic timer. The application has three main storyboards:</p>
<ul>
<li>Main Storyboard &#8211; describes the stories that constitute the application oriented stories</li>
<li>Game Storyboard &#8211; defines the stories that make up the gameplay</li>
<li>High Scores Storyboard &#8211; how the end user interacts with the high score stories</li>
</ul>
<p></p>
<h5>Main Storyboard</h5>
<p></p>
<p><a href="http://antimatroid.files.wordpress.com/2011/04/storyboard-main1.png"><img src="http://antimatroid.files.wordpress.com/2011/04/storyboard-main1.png?w=700&#038;h=148" alt="" title="storyboard-main" width="700" height="148" class="aligncenter size-full wp-image-2291" /></a></p>
<p>When the user launches the game, they will be presented the Title Story. The Title Story displays the application logo and will transition to the Menu Story after a few seconds. The Menu Story displays a list of actions that the user can initiate. The user may start a new game, view the high scores, review the settings or exit the application. When the user starts a new game, they will be presented the Game Storyboard. When the user views the high scores, they will be presented the High Scores Storyboard. When the user reviews the settings, they will be presented the Settings Story. The Settings will list out all the keyboard commands. When the user exits the application, the End Title Story is displayed. The End Title Story will display the application tagline for a few seconds and then the application will terminate.</p>
<p></p>
<h5>Game Storyboard</h5>
<p></p>
<p><a href="http://antimatroid.files.wordpress.com/2011/04/storyboard-game1.png"><img src="http://antimatroid.files.wordpress.com/2011/04/storyboard-game1.png?w=700&#038;h=148" alt="" title="storyboard-game" width="700" height="148" class="aligncenter size-full wp-image-2292" /></a></p>
<p>When the user elects to start a new game, the Countdown Story is displayed. The Countdown Story will display 3&#8230; 2&#8230; 1&#8230; prior to transitioning to the Gameplay Story. The Gameplay Story allows the user to play the actual game. When a user completes a level in the game, the user will be shown the Level Completed Story. The user will be able to review their performance and decide if they are done playing or want to play the next level. If they elect to quit playing, they will be taken to the High Scores Storyboard, otherwise they will be presented the Countdown Story prior to beginning the next level. If the user is destroyed and has no more lives, the user is presented the Game Over Story and after a few seconds elapses, the user will be presented the High Scores Storyboard. If a user completes all of the levels in the game, they will be presented the Game Completed Story. Similar to the Game Over Story, the Game Completed Story will transition to the High Scores Storyboard after a few seconds.</p>
<p></p>
<h5>High Scores Storyboard</h5>
<p></p>
<p><a href="http://antimatroid.files.wordpress.com/2011/04/storyboard-high-scores1.png"><img src="http://antimatroid.files.wordpress.com/2011/04/storyboard-high-scores1.png?w=700&#038;h=148" alt="" title="storyboard-high-scores" width="700" height="148" class="aligncenter size-full wp-image-2293" /></a></p>
<p>If a user had received a new high score during their game, they will be presented with the New High Score Story. They will enter in their name and then be transitioned to the High Scores Story. If user did not receive a new high score during their game, they will be presented the High Scores Story. The High Scores Story will display the top highest scores recorded in the game. Once the user is done reviewing the scores, they may return to the Main Storyboard. If the user had transitioned from the Main Storyboard to the High Scores Storyboard, then they will only be presented the High Scores Story.</p>
<p></p>
<h4>Implementation</h4>
<p>Each of the of storyboards is responsible for the displaying stories on the screen and transitioning between stories and storyboards (in the following I use each interchangeably). This functionality is achieved by keeping references to each story and instantiating each story on demand. When a new story is to be displayed, the storyboard will unload the current story and load up the new story. Unloading a story consists of removing the instance from the Controls collection, unregistering all event handlers and disposing of the story. Loading a story creates a new instance of the story, registers all relevant event handlers and then displays the story by adding it to the Controls collection.</p>
<p>To illustrate a concrete example of this practice, here is the implementation of the Main Storyboard.</p>
<p><pre class="brush: csharp;">
using System;
using System.Windows.Forms;
using UserInterface.GameScreen.Presentation;
using UserInterface.HighScoreScreen.Presentation;
using UserInterface.HomeScreen.Presentation;
using UserInterface.Resources;
using UserInterface.SettingsScreen.Presentation;
using UserInterface.ShutdownScreen.Presentation;
using UserInterface.StartupScreen.Presentation;

namespace UserInterface {
	public class MainWindow : Form {
		private DisplayStartupStory displayStartupStory;
		private MainMenuUserControl mainMenuUserControl;
		private HighScoreStoryBoard highScoreStoryBoard;
		private DisplaySettingsStory displaySettingsStory;
		private GameStoryBoard gameStoryBoard;
		private DisplayShutdownStory displayShutdownStory;

		public MainWindow() {
			base.Width = 320;
			base.Height = 480;
			base.FormBorderStyle = FormBorderStyle.FixedSingle;
			base.MaximizeBox = false;
			base.SizeGripStyle = SizeGripStyle.Hide;
			base.BackColor = InMemoryResources.BackgroundColor;
			base.Text = &quot;Space Cowboy&quot;;
			base.Icon = InMemoryResources.LogoIcon;
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);
			this.Location = this.DesktopLocation = new System.Drawing.Point(400, 100);
			loadStartup();
		}

		private void handleStartupDisplayed() {
			unloadStartup();
			loadMenu();
		}

		private void handleNewGame() {
			unloadMenu();
			loadGame();
		}

		private void handleGameOver(int score) {
			unloadGame();
			loadHighScores(score);
		}

		private void handleShowHighScores() {
			unloadMenu();
			loadHighScores(null);
		}

		private void handleHighscoreShowMenu() {
			unloadHighScores();
			loadMenu();
		}

		private void handleShowSettings() {
			unloadMenu();
			loadSettings();
		}

		private void handleSettingsShowMenu() {
			unloadSettings();
			loadMenu();
		}

		private void handleExit() {
			unloadMenu();
			loadShutdown();
		}

		private void handleShutdownDisplayed() {
			unloadShutdown();
			Application.Exit();
		}

		private void loadStartup() {
			displayStartupStory = new DisplayStartupStory();
			displayStartupStory.Dock = DockStyle.Fill;
			displayStartupStory.Displayed += new Action(handleStartupDisplayed);
			Controls.Add(displayStartupStory);
		}

		private void unloadStartup() {
			Controls.Clear();
			displayStartupStory.Displayed -= handleStartupDisplayed;
			displayStartupStory.Dispose();
			displayStartupStory = null;
		}

		
		private void loadMenu() {
			mainMenuUserControl = new MainMenuUserControl();
			mainMenuUserControl.Dock = DockStyle.Fill;
			mainMenuUserControl.NewGame += new Action(handleNewGame);
			mainMenuUserControl.ShowHighScores += new Action(handleShowHighScores);
			mainMenuUserControl.ShowSettings += new Action(handleShowSettings);
			mainMenuUserControl.Exit += new Action(handleExit);
			Controls.Add(mainMenuUserControl);
		}
	
		private void unloadMenu() {
			Controls.Clear();
			mainMenuUserControl.NewGame -= handleNewGame;
			mainMenuUserControl.ShowHighScores -= handleShowHighScores;
			mainMenuUserControl.ShowSettings -= handleShowSettings;
			mainMenuUserControl.Exit -= handleExit;
			mainMenuUserControl.Dispose();
			mainMenuUserControl = null;
		}


		private void loadGame() {
			gameStoryBoard = new GameStoryBoard();
			gameStoryBoard.Dock = DockStyle.Fill;
			gameStoryBoard.GameOver += new Action&lt;int&gt;(handleGameOver);
			Controls.Add(gameStoryBoard);
		}

		private void unloadGame() {
			Controls.Clear();
			gameStoryBoard.GameOver -= handleGameOver;
			gameStoryBoard.Dispose();
			gameStoryBoard = null;
		}


		private void loadHighScores(int? score) {
			highScoreStoryBoard = (score == null) ? new HighScoreStoryBoard() : new HighScoreStoryBoard(score.Value);
			highScoreStoryBoard.ShowMenu += new Action(handleHighscoreShowMenu);
			highScoreStoryBoard.Dock = DockStyle.Fill;
			Controls.Add(highScoreStoryBoard);
		}

		private void unloadHighScores() {
			Controls.Clear();
			highScoreStoryBoard.ShowMenu -= handleHighscoreShowMenu;
			highScoreStoryBoard.Dispose();
			highScoreStoryBoard = null;
		}


		private void loadSettings() {

			displaySettingsStory = new DisplaySettingsStory();
			displaySettingsStory.ShowMenu += new Action(handleSettingsShowMenu);
			displaySettingsStory.Dock = DockStyle.Fill;
			Controls.Add(displaySettingsStory);
		}

		private void unloadSettings() {
			Controls.Clear();
			displaySettingsStory.ShowMenu -= handleSettingsShowMenu;
			displaySettingsStory.Dispose();
			displaySettingsStory = null;
		}


		private void loadShutdown() {
			displayShutdownStory = new DisplayShutdownStory();
			displayShutdownStory.Dock = DockStyle.Fill;
			displayShutdownStory.Displayed += new Action(handleShutdownDisplayed);
			Controls.Add(displayShutdownStory);
		}

		private void unloadShutdown() {
			Controls.Clear();
			displayShutdownStory.Displayed -= handleShutdownDisplayed;
			displayShutdownStory.Dispose();
			displayShutdownStory = null;
		}
	}
}
</pre></p>
<p>A typical story contains a few controls and a sparse amount of logic. The following is the Game Story and a custom control called the LevelCanvas which is responsible for drawing the actors of the universe on the screen. The LevelCanvas derives from a custom control that uses a manual double buffering scheme that I&#8217;ve written about in some of my previous posts.</p>
<p><pre class="brush: csharp;">
using System.Windows.Forms;
using UserInterface.GameScreen.Gameplay;

namespace UserInterface.GameScreen.Presentation {
	public class GameStory : UserControl {
		private TableLayoutPanel panel;
		private GameStatisticsView headsUpDisplay;
		private LevelCanvas levelUserControl;

		public GameStory(Game game) {
			headsUpDisplay = new GameStatisticsView(game.GameStatistics);
			headsUpDisplay.Dock = DockStyle.Fill;

			levelUserControl = new LevelCanvas(game);
			levelUserControl.Dock = DockStyle.Fill;

			panel = new TableLayoutPanel();
			panel.ColumnStyles.Add(new ColumnStyle() { SizeType = SizeType.Percent, Width = 100.0f });
			panel.RowStyles.Add(new RowStyle() { SizeType = SizeType.Absolute, Height = 48.0f });
			panel.RowStyles.Add(new RowStyle() { SizeType = SizeType.Percent, Height = 100.0f });
			panel.Dock = DockStyle.Fill;
			panel.Controls.Add(headsUpDisplay, 0, 0);
			panel.Controls.Add(levelUserControl, 0, 1);

			Controls.Add(panel);
		}
	}

	public class LevelCanvas : DoubleBufferedUserControl {
		private Game game;
		private Level currentLevel;

		public LevelCanvas(Game game) {
			this.game = game;

			this.BorderStyle = BorderStyle.FixedSingle;

			game.StartLevel += new Action&lt;Level&gt;(handleStartLevel);
			game.LevelCompleted += new Action&lt;SessionStatistics&gt;(handleLevelCompleted);
			game.LevelOver += new Action(handleLevelOver);
		}

		override protected void Dispose(bool disposing) {
			base.Dispose(disposing);

			if (disposing) {
				game.StartLevel -= handleStartLevel;
				game.LevelCompleted -= handleLevelCompleted;
				game.LevelOver -= handleLevelOver;
			}
		}

		override protected void Draw(Graphics graphics) {
			if (currentLevel == null)
				return;

			graphics.InterpolationMode = InterpolationMode.Bicubic;
			graphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;
			graphics.SmoothingMode = SmoothingMode.AntiAlias;

			foreach (Actor actor in currentLevel.Actors) {
				try {
					actor.View.Draw(graphics, ClientRectangle);
				} catch (Exception E) {
					System.Diagnostics.Trace.WriteLine(E);
				}
			}
		}

		private void handleLevelChanged() {
			if (InvokeRequired) {
				Invoke(new Action(handleLevelChanged));
				return;
			}

			Draw();
		}

		private void handleLevelCompleted(SessionStatistics statistics) {
			if (InvokeRequired) {
				Invoke(new Action&lt;SessionStatistics&gt;(handleLevelCompleted), statistics);
				return;
			}

			currentLevel.Stop();
			currentLevel.LevelChanged -= handleLevelChanged;
			currentLevel = null;

			Invalidate();
		}

		private void handleLevelOver() {
			if (InvokeRequired) {
				Invoke(new Action(handleLevelOver));
				return;
			}

			currentLevel.Stop();
			currentLevel.LevelChanged -= handleLevelChanged;
			currentLevel = null;

			Invalidate();
		}

		private void handleStartLevel(Level level) {
			if (InvokeRequired) {
				Invoke(new Action&lt;Level&gt;(handleStartLevel));
				return;
			}

			currentLevel = level;
			currentLevel.LevelChanged += new Action(handleLevelChanged);
			currentLevel.User.Behavior = new KeyboardActorBehavior(currentLevel, this);

			currentLevel.Start();

			Invalidate();
		}
	}
}
</pre></p>
<p></p>
<h3>Gameplay</h3>
<p></p>
<h4>Design</h4>
<p>Much of the gameplay design was focused on providing a positive end user experience. The end user experience revolves around making the gameplay predictable, so that the end user could learn how to play quickly, then shifts to introduce new dynamics keeping the experience fresh. The core concepts that constitute the end user experience can be summarized as:</p>
<ul>
<li>Mechanics &#8211; how things in the game universe behave</li>
<li>Incentives &#8211; making the end user want to play the game again and again</li>
<li>Extras &#8211; making the game more interesting and requiring new strategies</li>
</ul>
<p></p>
<h5>Mechanics</h5>
<ul>
<li>The end user is given the ability to maneuver about the universe and to fire weapons. The user is able to issue commands to move the ship north, east, south or west. Each command results in a small amount of thrust being produced</li>
<li>A user can rotate the ship&#8217;s weapons to the target an object in the universe and engage the weapons to emit projectiles. Projectiles follow the same rules as every other object in the universe</li>
<li>The user is given a fixed number of health points. Each time a ship collides with another object in the universe, both objects have their health depleted by a fixed amount. If the total points goes to zero, the user has two additional lives to use. Once all the lives have been used up, the game is over</li>
<li>All enemies in the universe will attempt to destroy the user at all costs</li>
</ul>
<p></p>
<h5>Incentives</h5>
<ul>
<li>Each time a user destroys an object in the universe, they may receive a variable amount of points that contributes to their overall score</li>
</ul>
<p></p>
<h5>Extras</h5>
<ul>
<li>The head-up display will flash and then fade back to normal whenever a value changes</li>
<li>An object&#8217;s overall health can be determined by the object&#8217;s opacity on the screen. A completely healthy object will be full opaque and the closer an object is to be destroyed, the more transparent it will appear</li>
<li>When an object is destroyed, it may reveal power-ups that were hidden inside or breakup into smaller pieces of debris. Power-ups come in the following flavors:
<ul>
<li>Health &#8211; Set&#8217;s the object&#8217;s health to 100%</li>
<li>Engine &#8211; Increases the propulsion of the object&#8217;s engine</li>
<li>Weapon &#8211; Replaces the standard weapon with three standard weapons</li>
</ul>
</li>
</ul>
<p></p>
<h4>Implementation</h4>
<p></p>
<h5>Game state</h5>
<p>The Level class is responsible for driving the game in terms of notifying the UI to redraw the universe and for evolving the universe according to the design rules. The main method of interest is the handleTick method, which is responsible for running through the objects in the universe and then firing off different events based on the state of the universe. The class also takes care of the process of breaking an object into debris and applying non-physical behavior to collisions.</p>
<p><pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using Library.Mathematics;
using UserInterface.GameScreen.Gameplay.Behavior;
using UserInterface.GameScreen.Physics;

namespace UserInterface.GameScreen.Gameplay {
	public class Level {
		static private Random RNG;

		static Level() {
			RNG = new Random((int)(DateTime.Now.Ticks &amp; 0xffffff));
		}

		public event Action LevelChanged;
		public event Action&lt;SessionStatistics&gt; LevelCompleted;
		public event Action UserDestroyed;
		public event Action&lt;int&gt; UserScored;

		private LevelStatistics levelStatistics;
		private TimePiece timePiece;
		private PhysicsEngine physicsEngine;
		
		public List&lt;Actor&gt; Actors { get; protected set; }
		public UserActor User { get; protected set; }

		public Level(double width, double height) {
			physicsEngine = new PhysicsEngine(width, height);
			physicsEngine.Collided += new Action&lt;Actor, Actor&gt;(handleCollided);

			levelStatistics = new LevelStatistics();

			timePiece = new TimePiece();
			timePiece.Tick += new Action(handleTick);

			User = new UserActor();
			Actors = new List&lt;Actor&gt;() { User };
		}

		public void Start() {
			timePiece.Start();
		}

		public void Stop() {
			timePiece.Stop();
		}

		private List&lt;Actor&gt; breakUp(Actor actor) {
			List&lt;Actor&gt; actors = new List&lt;Actor&gt;();
			if (actor.Body.Radius &lt;= 6)
				return actors;

			if (actor.GetType().Equals(typeof(PowerUpNeutralActor)))
				return actors;

			for (int n = 0; n &lt; 3; n++) {
				Actor fragment = null;

				if (0.4 * actor.Body.Radius == 6.0) {
					int x = RNG.Next(0, 10);
					if (x == 0) {
						fragment = new HealthPowerUpNeutralActor();
					} else if (x == 1) {
						fragment = new EnginePowerUpNeutralActor();
					} else if (x == 2) {
						fragment = new WeaponPowerUpNeutralActor();
					} else {
						fragment = new DebrisNeutralActor();
					}
				} else {
					fragment = new DebrisNeutralActor();
				}

				fragment.Body = new Body() {
					CurrentMass = 0.1 * actor.Body.StartingMass,
					Radius = 0.4 * actor.Body.Radius
				};

				fragment.AngularMovement = new AngularMovement() {
					Acceleration = actor.AngularMovement.Acceleration,
					Heading = actor.AngularMovement.Heading,
					Velocity = actor.AngularMovement.Velocity
				};


				fragment.LinearMovement = new LinearMovement() {
					Acceleration = new Vector(2, (i) =&gt; actor.LinearMovement.Acceleration[i]),
					Location = new Vector(2, (i) =&gt; actor.LinearMovement.Location[i]),
					Velocity = new Vector(2, (i) =&gt; actor.LinearMovement.Velocity[i])
				};

				fragment.Points = 0;

				fragment.Behavior = new MeanderActorBehavior(fragment);

				Vector direction = new Vector(2);
				direction[0] = Math.Cos((n * 120.0) * (Math.PI / 180.0));
				direction[1] = Math.Sin((n * 120.0) * (Math.PI / 180.0));

				fragment.LinearMovement.Location += (actor.Body.Radius * 0.5) * direction;
				fragment.LinearMovement.Velocity = (0.3 * actor.LinearMovement.Velocity.Length()) * direction;

				actors.Add(fragment);
			}

			return actors;
		}

		private void handleCollided(Actor A, Actor B) {
			bool aIsPowerUp = A is PowerUpNeutralActor;
			bool bIsPowerUp = B is PowerUpNeutralActor;
			if (!(aIsPowerUp ^ bIsPowerUp))
				return;

			PowerUpNeutralActor powerUpActor = null;
			Actor receipentActor = null;
			if (aIsPowerUp) {
				powerUpActor = A as PowerUpNeutralActor;
				receipentActor = B;
			} else {
				powerUpActor = B as PowerUpNeutralActor;
				receipentActor = A;
			}

			if (receipentActor is BulletUserActor || receipentActor is BulletEnemyActor)
				return;

			powerUpActor.Apply(receipentActor);
		}

		private void handleTick() {
			physicsEngine.Step(0.1, Actors);

			List&lt;Actor&gt; actorsToAdd = new List&lt;Actor&gt;();
			for (int n = 0; n &lt; Actors.Count; n++) {

				if (Actors[n].Body.CurrentMass &lt;= 0.0) {
					if (Actors[n].Equals(User)) {
						userDestroyed();
					} else {
						if (Actors[n] is BulletUserActor || Actors[n] is BulletEnemyActor) {

						} else {
							userScored(Actors[n].Points);
							actorsToAdd.AddRange(breakUp(Actors[n]));
						}

						Actors.RemoveAt(n--);
					}
				}
			}
			Actors.AddRange(actorsToAdd);

			if (isLevelComplete())
				levelCompleted();

			int actorCount = Actors.Count;
			for (int n = 0; n &lt; actorCount; n++)
				Actors[n].Step(0.1);

			levelChanged();
		}

		private bool isLevelComplete() {
			for (int n = 0; n &lt; Actors.Count; n++) {
				if (Actors[n] is BulletEnemyActor || Actors[n] is BulletUserActor)
					continue;

				if (Actors[n] is EnemyActor || Actors[n] is NeutralActor)
					return false;
			}
			return true;
		}

		private void levelChanged() {
			if (LevelChanged != null)
				LevelChanged();
		}

		private void levelCompleted() {
			if (LevelCompleted != null) {
				levelStatistics.TimeTaken = TimeSpan.FromMilliseconds(timePiece.ElapsedTimeMilliseconds);
				LevelCompleted(new SessionStatistics() {
					LevelStatistics = levelStatistics
				});
			}
		}

		private void userDestroyed() {
			levelStatistics.LivesLost++;

			if (UserDestroyed != null)
				UserDestroyed();
		}

		private void userScored(int points) {
			levelStatistics.Scored += points;

			if (UserScored != null)
				UserScored(points);
		}
	}
}
</pre></p>
<h5>Physics</h5>
<p>One of the key features of the game is providing a realistic enough perception of a simulated universe and that the user is able to interact with objects. This is done by providing a handful of basic physical attributes that are implemented by the PhysicsEngine class. The core responsibilities of the class is to apply physical rules over the course of a slice of time. Objects in the universe can be destroyed and created during this process.</p>
<p>Collisions between actors is handled through the typical conservation of linear momentum approach. Two objects, <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='A' title='A' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=B&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='B' title='B' class='latex' />, with momentums, <img src='http://s0.wp.com/latex.php?latex=p_%7BA%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='p_{A}' title='p_{A}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=p_%7BB%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='p_{B}' title='p_{B}' class='latex' />, must have the same amount of momentum before and after the collision. Assuming a totally elastic collision, we end up with <img src='http://s0.wp.com/latex.php?latex=p_%7BA%7D%5E%7B%28b%29%7D+%2B+p_%7BB%7D%5E%7B%28b%29%7D+%3D+p_%7BA%7D%5E%7B%28a%29%7D+%2B+p_%7BB%7D%5E%7B%28a%29%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='p_{A}^{(b)} + p_{B}^{(b)} = p_{A}^{(a)} + p_{B}^{(a)}' title='p_{A}^{(b)} + p_{B}^{(b)} = p_{A}^{(a)} + p_{B}^{(a)}' class='latex' />. Momentum is defined as <img src='http://s0.wp.com/latex.php?latex=p+%3D+%5Cfrac%7B1%7D%7B2%7D+m+v%5E%7B2%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='p = &#92;frac{1}{2} m v^{2}' title='p = &#92;frac{1}{2} m v^{2}' class='latex' />, where <img src='http://s0.wp.com/latex.php?latex=m&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='m' title='m' class='latex' /> is the mass of an object and <img src='http://s0.wp.com/latex.php?latex=v&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='v' title='v' class='latex' /> is its velocity. Since no mass is being lost in the collision, the velocities must change as a result. Solving for the <img src='http://s0.wp.com/latex.php?latex=v%5E%7B%28a%29%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='v^{(a)}' title='v^{(a)}' class='latex' /> velocities yields the trajectories that the objects will follow after the collision.</p>
<p>Collisions between actors and walls is once again handled through the typical conservation of momentum. Since the wall is of infinite mass and has zero velocity, the colliding object&#8217;s velocity is reflected about the wall&#8217;s normal vector.</p>
<p>Any object that escapes or has invalid numerical data is removed from the universe.</p>
<p><pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using Library.Mathematics;

namespace UserInterface.GameScreen.Physics {
	public class PhysicsEngine {
		public event Action&lt;Actor, Actor&gt; Collided;

		private double Width, Height;

		public PhysicsEngine(double width, double height) {
			Width = width;
			Height = height;
		}

		public void Step(double dt, List&lt;Actor&gt; actors) {
			// Actor-Actor interactions
			for (int n = 0; n &lt; actors.Count; n++)
				for (int m = n + 1; m &lt; actors.Count; m++)
					if (intersects(actors[n], actors[m])) {
						collide(actors[n], actors[m]);
					}

			// Actor-Wall interactions
			for (int n = 0; n &lt; actors.Count; n++) {
				Vector l = actors[n].LinearMovement.Location;
				Vector v = actors[n].LinearMovement.Velocity;
				double r = actors[n].Body.Radius;

				if (l[0] - r &lt; 0)
					l[0] = r;

				if (l[0] + r &gt; Width)
					l[0] = Width - r;

				if (l[1] - r &lt; 0)
					l[1] = r;

				if (l[1] + r &gt; Height)
					l[1] = Height - r;

				Vector N = getWallNormal(l, r);
				if (N != null)
					actors[n].LinearMovement.Velocity = v - (2 * N.dot(v)) * N;
			}

			// get rid of anything that escaped the universe.
			for (int n = 0; n &lt; actors.Count; n++) {
				Vector l = actors[n].LinearMovement.Location;
				double r = actors[n].Body.Radius;

				if (double.IsInfinity(l[0]) || double.IsInfinity(l[1])) {
					actors.RemoveAt(n--);
					continue;
				}

				if (double.IsNaN(l[0]) || double.IsNaN(l[1])) {
					actors.RemoveAt(n--);
					continue;
				}

				if (l[0] - r &lt; -5 || l[0] + r &gt; Width + 5 || l[1] - r &lt; -5 || l[1] + r &gt; Height + 5)
					actors.RemoveAt(n--);
			}
		}

		private bool intersects(Actor A, Actor B) {
			Vector pointDistance = (A.LinearMovement.Location - B.LinearMovement.Location);
			double touchingPointDistance = (A.Body.Radius + B.Body.Radius);
			return pointDistance.Length() &lt;= touchingPointDistance;
		}

		private void collide(Actor A, Actor B) {
			Vector a = (1.0 / (A.Body.CurrentMass + B.Body.CurrentMass)) * ((A.Body.CurrentMass - B.Body.CurrentMass) * A.LinearMovement.Velocity + (2 * B.Body.CurrentMass) * B.LinearMovement.Velocity);
			Vector b = (1.0 / (A.Body.CurrentMass + B.Body.CurrentMass)) * ((B.Body.CurrentMass - A.Body.CurrentMass) * B.LinearMovement.Velocity + (2 * A.Body.CurrentMass) * A.LinearMovement.Velocity);

			A.LinearMovement.Velocity = a;
			B.LinearMovement.Velocity = b;

			A.Body.CurrentMass -= 2.5;
			B.Body.CurrentMass -= 2.5;

			if (Collided != null)
				Collided(A, B);
		}

		private Vector getWallNormal(Vector l, double r) {
			Vector N = null;
			if (l[0] - r &lt;= 0.0) {
				// left side of the body is against the left side of the frame
				if (l[1] - r &lt;= 0.0) {
					// top of the body is against the top of the frame
					// =&gt; two point of contact
					N = new Vector(2, (i) =&gt; i == 0 ? 1 : -1);
				} else if (l[1] + r &gt;= Height) {
					// bottom of the body is against the bottom of the frame
					// =&gt; two points of contact
					N = new Vector(2, (i) =&gt; i == 0 ? 1 : 1);
				} else {
					// body is between the top and bottom
					// =&gt; single point of contact
					N = new Vector(2, (i) =&gt; i == 0 ? 1 : 0);
				}
			} else if (l[0] + r &gt;= Width) {
				// right side of the body is against the right side of the frame
				if (l[1] - r &lt;= 0.0) {
					// top of the body is against the top of the frame
					// =&gt; two points of contact
					N = new Vector(2, (i) =&gt; i == 0 ? -1 : -1);
				} else if (l[1] + r &gt;= Height) {
					// bottom of the body is against the bottom of the frame
					// =&gt; two points of contact
					N = new Vector(2, (i) =&gt; i == 0 ? -1 : 1);
				} else {
					// body is between the top and bottom
					// =&gt; single point of contact
					N = new Vector(2, (i) =&gt; i == 0 ? -1 : 0);
				}
			} else {
				// body is between the left and right
				if (l[1] - r &lt;= 0.0) {
					// top of the body is against the top of the frame
					// =&gt; single point of contact
					N = new Vector(2, (i) =&gt; i == 1 ? -1 : 0);
				} else if (l[1] + r &gt;= Height) {
					// bottom of the body is against the bottom of the frame
					// =&gt; single point of contact
					N = new Vector(2, (i) =&gt; i == 1 ? +1 : 0);
				} else {
					// body is between the top and bottom
					// =&gt; zero points of contact
				}
			}
			return N;
		}
	}
}
</pre></p>
<p></p>
<h5>Enemy Targeting</h5>
<p>To make the game more interesting, the enemy ships are able to target the user&#8217;s ship. Since the enemy must adhere to the same mechanics that the user does, it incrementally rotates clockwise and counterclockwise to keep the user in target. When the user is within an acceptable window of error, the enemy will fire its weapons in hopes of hitting the user.</p>
<p>The first approach here was to simply keep track of which direction the enemy is rotating and to rotate next in the direction that minimized the distance in radians between the enemy&#8217;s heading and the direction that the user is traveling. Unfortunately, this will result in overshooting the desired destination.</p>
<p>The second approach is to take into account how long it will take the rotation to slow down given its current angular velocity. If there is enough time then the rotation will increase, otherwise it will slowdown. Given the second approach, the enemies exhibit a reasonable accurate behavior of tracking the user&#8217;s ship.</p>
<p><pre class="brush: csharp;">
using System;
using Library.Mathematics;
using UserInterface.GameScreen.Gameplay.Components;
using UserInterface.GameScreen.Physics;

namespace UserInterface.GameScreen.Gameplay.Behavior {
	public class TargetingActorBehavior : IBehavior {
		private Actor toControl;
		private Actor toTarget;
		private Level level;

		public TargetingActorBehavior(Actor toControl, Actor toTarget, Level level) {
			this.toControl = toControl;
			this.toTarget = toTarget;
			this.level = level;
		}

		public void Step(double dt) {
			Vector d = toTarget.LinearMovement.Location - toControl.LinearMovement.Location;
			Vector h = toControl.AngularMovement.HeadingVector;
			double radsToTarget = d.RadsBetween(h);
			
			double a = Math.PI / 5.0;

			AngularMovement cw = new AngularMovement();
			cw.Heading = toControl.AngularMovement.Heading - a;
			double radsToTargetCW = d.RadsBetween(cw.HeadingVector);

			AngularMovement ccw = new AngularMovement();
			ccw.Heading = toControl.AngularMovement.Heading + a;
			double radsToTargetCCW = d.RadsBetween(ccw.HeadingVector);

			double v = toControl.AngularMovement.Velocity;

			if (v &lt; 0) {
				// rotating cw
				if (radsToTargetCW &lt; radsToTargetCCW) { 
					// continuing to rotate cw will bring us closer to zero
					// first, check to see if we are going to overshoot if we
					// continue to rotate cw.
					double radsToStop = 1.5 * v * (v + -a * dt) / (-a * dt);
					if (radsToTarget &lt;= radsToStop) {
						toControl.AngularEngine.Rotate(Rotation.CounterClockwise);
					} else if (radsToTarget &gt; radsToStop) {
						toControl.AngularEngine.Rotate(Rotation.Clockwise);
					}
				} else if (radsToTargetCW &gt;= radsToTargetCCW) { 
					// continuing to rotate cw will bring us further away from zero
					// =&gt; rotate the opposite direction
					toControl.AngularEngine.Rotate(Rotation.CounterClockwise);
				}
			} else if (v == 0) {
				// not rotating
				// pick which ever direction is closer to zero
				if (radsToTargetCW &lt; radsToTargetCCW) {
					toControl.AngularEngine.Rotate(Rotation.Clockwise);
				} else if(radsToTargetCW &gt;= radsToTargetCCW) {
					toControl.AngularEngine.Rotate(Rotation.CounterClockwise);
				}
			} else if (v &gt; 0) { 
				// rotating ccw
				if (radsToTargetCCW &lt; radsToTargetCW) {
					// continuing to rotate ccw will bring us closer to zero
					// first, check to see if we are going to overshoot if we
					// continue to rotate ccw.
					double radsToStop = 1.5 * v * (v + a * dt) / (a * dt);
					if (radsToTarget &lt;= radsToStop) {
						toControl.AngularEngine.Rotate(Rotation.Clockwise);
					} else if (radsToTarget &gt; radsToStop) {
						toControl.AngularEngine.Rotate(Rotation.CounterClockwise);
					}
				} else if (radsToTargetCCW &gt;= radsToTargetCW) {
					// continuing to rotate ccw will bring us further away from zero
					// =&gt; rotate the opposite direction
					toControl.AngularEngine.Rotate(Rotation.Clockwise);
				}
			}

			// If the angle is less than five degrees (pi/36), then fire.
			if (radsToTarget &lt; Math.PI / 36.0)
				level.Actors.AddRange(toControl.Weapon.Fire());
		}
	}
}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/2199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/2199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/2199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/2199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/antimatroid.wordpress.com/2199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/antimatroid.wordpress.com/2199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/antimatroid.wordpress.com/2199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/antimatroid.wordpress.com/2199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/2199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/2199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/2199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/2199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/2199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/2199/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=2199&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2011/05/01/space-cowboy-a-shoot%e2%80%99em-up-game-in-c-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2011/04/storyboard-main1.png" medium="image">
			<media:title type="html">storyboard-main</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2011/04/storyboard-game1.png" medium="image">
			<media:title type="html">storyboard-game</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2011/04/storyboard-high-scores1.png" medium="image">
			<media:title type="html">storyboard-high-scores</media:title>
		</media:content>
	</item>
		<item>
		<title>Space Cowboy: A Shoot’em up game in Haskell: Part 2</title>
		<link>http://antimatroid.wordpress.com/2011/04/01/space-cowboy-a-shoot%e2%80%99em-up-game-in-haskell-part-2/</link>
		<comments>http://antimatroid.wordpress.com/2011/04/01/space-cowboy-a-shoot%e2%80%99em-up-game-in-haskell-part-2/#comments</comments>
		<pubDate>Fri, 01 Apr 2011 14:00:04 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[HOpenGL]]></category>
		<category><![CDATA[Leksah]]></category>
		<category><![CDATA[Shoot’em up]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=2197</guid>
		<description><![CDATA[Introduction A couple months back I wrote about a shoot&#8217;em up game that I was planning on making in Haskell. My goal was to make something a little more elaborate than my previous games and also take my understanding of Haskell further. Ultimately, I did not use Haskell and instead decided to use C# for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=2197&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>A couple months back I <a href="http://antimatroid.wordpress.com/2011/01/01/space-cowboy-a-shootem-up-game-in-haskell-part-1/">wrote</a> about a shoot&#8217;em up game that I was planning on making in <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" target="blank">Haskell</a>. My goal was to make something a little more elaborate than my previous games and also take my understanding of Haskell further. Ultimately, I did not use Haskell and instead decided to use C# for the final product (main reason was productivity). Nonetheless, I felt it was worthwhile to post the work that was done on the prototype and talk a bit about the development process.</p>
<p>To get started, here is a quick demo of the features that were implemented in the prototype, namely, the user&#8217;s ability to navigate the ship and fire its weapons using the keyboard.</p>
<span style="text-align:center; display: block;"><a href="http://antimatroid.wordpress.com/2011/04/01/space-cowboy-a-shoot%e2%80%99em-up-game-in-haskell-part-2/"><img src="http://img.youtube.com/vi/dLtgBGc1S6w/2.jpg" alt="" /></a></span>
<p></p>
<p>As others have put it, programming in Haskell is like writing a proof, so in similar vein I&#8217;m going to present the core modules of the prototype and then build upon those to present the more complicated ones (which follows more or less the development process). The code that is posted here was authored in <a href="http://leksah.org/" target="blank">Leksah</a>, which replaces a lot of common syntax with &#8220;source candy&#8221;, so hopefully you will be able to deduce the formal syntax.</p>
<h3>Mathematics Module</h3>
<p>Since I didn&#8217;t have a lot experience in designing a game like this in Haskell, I decided I&#8217;d start with the basic mathematical model of the game. I thought about the concepts that were needed to represent bodies in a universe and settled on points, vectors and shapes to represent the ideas I had brewing in my head.</p>
<p></p>
<h4>Point</h4>
<p>The Point data type represents a single coordinate pair on the <a href="http://en.wikipedia.org/wiki/Euclidean_plane" target="blank">Euclidean Plane</a>.</p>
<pre>
type Coordinate = Float

data Point = Point Coordinate Coordinate

pointZero :: Point
pointZero = Point 0.0 0.0

type Distance = Float

pointDistance :: Point →  Point →  Distance
pointDistance (Point x y) (Point u v) = sqrt ((x - u)↑2 + (y - v)↑2)
</pre>
<p></p>
<h4>Vector</h4>
<p>The Vector data type serves two purposes: the first is to describe the translation of points along the plane and the second is to describe the direction in which bodies are moving. The usual set of operations on <a href="http://en.wikipedia.org/wiki/Euclidean_vector" target="blank">Euclidean Vectors</a> were implemented.</p>
<pre>
data Vector = Vector Coordinate Coordinate

instance Eq Vector where
    (Vector x y) ≡ (Vector u v) = (x ≡ u) ∧ (y ≡ v)
    (Vector x y) ≠ (Vector u v) = (x ≠ u) ∨ (y ≠ v)

vectorZero :: Vector
vectorZero = Vector 0.0 0.0

vectorUp :: Vector
vectorUp = Vector 0 1

vectorLeft :: Vector
vectorLeft = Vector (-1) 0

vectorDown :: Vector
vectorDown = Vector 0 (-1)

vectorRight :: Vector
vectorRight = Vector 1 0

vectorIdentity :: Vector →  Vector
vectorIdentity (Vector x y) = Vector x y

vectorAdd :: Vector →  Vector →  Vector
vectorAdd (Vector x y) (Vector x' y') = Vector (x + x') (y + y')

vectorDotProduct :: Vector →  Vector →  Float
vectorDotProduct (Vector x y) (Vector x' y') = (x * x') + (y * y')

vectorScale :: Float →  Vector →  Vector
vectorScale a (Vector x' y') = Vector (a * x') (a * y')

vectorMinus :: Vector →  Vector →  Vector
vectorMinus (Vector x y) (Vector x' y') = Vector (x - x') (y - y')

vectorMagnitude :: Vector →  Float
vectorMagnitude (u) = sqrt $ vectorDotProduct u u

vectorNormalize :: Vector →  Vector
vectorNormalize (u)
    | vectorMagnitude u ≡ 0 = Vector 0 0
    | otherwise = vectorScale (1.0 / (vectorMagnitude u)) u

pointAdd :: Point →  Vector →  Point
pointAdd (Point x y) (Vector u v) = Point (x + u) (y + v)
</pre>
<p></p>
<h4>Shape</h4>
<p>Bodies are represented as simple shapes. In the initial round of design, rectangles and ellipses were considered, but for the purpose of developing a prototype, I settled on circles. The benefit is that determining the minimum distance between two circles is simpler and consequently so is detecting collisions.</p>
<pre>
data Shape =
    Circle {
        center :: Point,
        radius :: Distance
    }

unitCircle :: Shape
unitCircle = Circle {
    center = pointZero,
    radius = 1.0
    }

shapeDistance :: Shape →  Shape →  Distance
shapeDistance (Circle c r) (Circle c' r') = (pointDistance c c') - (r + r')

shapeCollide :: Shape →  Shape →  Bool
shapeCollide a b = distance ≤ 0
    where
        distance = shapeDistance a b
</pre>
<p></p>
<h3>Physics Module</h3>
<p>Now that I had a mathematical model of the objects that would be considered, it made sense to tackle the physics model of the game. Bodies in the game are treated as simple <a href="http://en.wikipedia.org/wiki/Rigid_body" target="blank">Rigid Bodies</a> with non-rotational <a href="http://en.wikipedia.org/wiki/Kinematics" target="blank">Kinematics</a>.</p>
<p></p>
<h4>Movement</h4>
<p>To capture the kinematics of the bodies, the Movement data type captures the location, velocity and acceleration of a body. The heart of the physics engine is captured in movementEvolved- it is responsible for updating the location, velocity and acceleration over a slice of time.</p>
<pre>
type Velocity = Vector

type Acceleration = Vector

data Movement = Movement {
    location :: Point,
    velocity :: Velocity,
    acceleration :: Acceleration
    }

movementZero :: Movement
movementZero = Movement {
    location = pointZero,
    velocity = vectorZero,
    acceleration = vectorZero
    }

type Time = Float

movementEvolve :: Movement →  Time →  Movement
movementEvolve (Movement l v a) t = Movement l' v' a'
    where a' = vectorIdentity a
          v' = vectorAdd (vectorScale t a) v
          l' = pointAdd l (vectorAdd (vectorScale (t * t / 2.0) a) (vectorScale t v))
</pre>
<p></p>
<h4>Body</h4>
<p>Each physical body in the universe has a mass, shape and movement. The second key component of the physics engine is the process of detecting collisions. bodiesCollide is responsible for taking a collection of bodies and for each one, collecting the bodies that collide with it and then supplying that body and its contacts to a function that determines the outcome of the collision. </p>
<pre>
type Mass = Float

data Body a = Body {
        shape :: Shape,
        mass :: Mass,
        movement :: Movement,
        description :: a
    }

bodyAddMass :: Body a →  Mass →  Body a
bodyAddMass (Body s m mo d) amount = Body {
        shape = s,
        mass = m + amount,
        movement = mo,
        description = d
    }

bodiesCollide :: [Body a] →  (Body a →  [Body a] →  [Body a]) →  [Body a]
bodiesCollide xs f = apply [] xs f

apply :: [Body a] →  [Body a] →  (Body a →  [Body a] →  [Body a]) →  [Body a]
apply _ [] _ = []
apply leftList (item:rightList) mapping = processed ⊕ remaining
    where
        processed =
            if null collisions
            then [item]
            else mapping item collisions
        collisions = filter (λx →  bodyCollide (item, x)) (leftList ⊕ rightList)
        remaining = apply (leftList ⊕ [item]) rightList mapping

bodyCollide :: (Body a, Body a) →  Bool
bodyCollide (a, b) = shapeCollide (shape a) (shape b)

bodyEvolve :: Body a →  Time →  Body a
bodyEvolve (Body (Circle c r) mass m d) t = Body {
    shape = Circle (location m') r,
    mass = mass,
    movement = m',
    description = d
    }
    where
        m' = movementEvolve m t
</pre>
<p></p>
<h4>Universe</h4>
<p>The game universe spans the plane, contains a collection of bodies and a sense of time. The universe brings together the two main components of the physics engine and exposes a way to remove items from the universe.</p>
<pre>
data Universe a = Universe {
    bodies :: [Body a],
    time :: Time
    }

universeAddBodies :: Universe a →  [Body a] →  Universe a
universeAddBodies u bs = Universe {
        bodies = (bodies u) ⊕ bs,
        time = time u
    }

universeCollide :: Universe a →  (Body a →  [Body a] →  [Body a]) →  Universe a
universeCollide (Universe bs t) f = Universe {
    bodies = bodiesCollide bs f,
    time = t
    }

universeEvolve :: Universe a →  Time →  Universe a
universeEvolve u t = Universe {
    bodies = map (λb →  bodyEvolve b t) (bodies u),
    time = t + (time u)
    }

universeFilter :: (Universe a) →  (Body a →  Bool) →  (Universe a)
universeFilter u p = Universe {
        bodies = filter p (bodies u),
        time = time u
    }
</pre>
<p></p>
<h3>Game Module</h3>
<p>Now that the physics of the universe have been described, we can start describing specific aspects of the game.</p>
<p></p>
<h4>Weapon</h4>
<p>Each ship has some number of weapons capable of doing some amount of damage and can fire projectiles with a given acceleration.</p>
<pre>
data Weapon =
    Torpedo

type Damage = Int

weaponDamage :: Weapon →  Damage
weaponDamage Torpedo = 2
weaponDamage _ = defined

type Thrust = Float

weaponThrust :: Weapon →  Thrust
weaponThrust Torpedo = 0.5
weaponThrust _ = undefined
</pre>
<p></p>
<h4>Engine</h4>
<p>Each ship has some number of engines capable of providing some amount of acceleration.</p>
<pre>
data Engine =
    Rocket

engineThrust :: Engine →  Thrust
engineThrust Rocket = 0.05
engineThrust _ = undefined
</pre>
<p></p>
<h4>Ship</h4>
<p>A ship is any body in the universe, described here as either a Projectile or a Fighter. It is what will be captured in the parametric type of the Universe data type.</p>
<pre>
data Ship =
    Projectile Thrust Damage
    | Fighter Engine Weapon

shipEngines :: Ship →  [Engine]
shipEngines (Fighter e _) = [e]
shipEngines _ = []

shipThrust :: Ship →  Thrust
shipThrust s = sum $ map engineThrust (shipEngines s)

shipWeapons :: Ship →  [Weapon]
shipWeapons (Fighter _ w) = [w]
shipWeapons _ = []

shipFireWeapons :: Ship →  [Ship]
shipFireWeapons s = map newProjectile $ shipWeapons s
</pre>
<p></p>
<h5>Projectile</h5>
<p>A projectile is any body fired from a weapon. </p>
<pre>
newProjectile :: Weapon →  Ship
newProjectile w = Projectile (weaponThrust w) (weaponDamage w)

projectileToBody :: Ship →  Movement →  Body Ship
projectileToBody p@(Projectile t d) m@(Movement l v a) = Body {
        shape = Circle {
            center = pointAdd l (vectorScale 1.1 vectorUp),
            radius = 0.2
        },
        movement = Movement {
            location = pointAdd l (vectorScale 1.25 vectorUp),
            velocity = vectorScale t vectorUp,
            acceleration = vectorIdentity a
        },
        description = p,
        mass = 1
    }
</pre>
<p></p>
<h5>Fighter</h5>
<p>The Fighter represents the end user and has a number of functions for controlling it. Notably, firing of the weapons and navigating the plane.</p>
<pre>
shipIsFighter :: Ship →  Bool
shipIsFighter (Fighter _ _) = True
shipIsFighter _ = False

fighterDestroyed :: (Universe Ship) →  Bool
fighterDestroyed (Universe bs t) = null $ filter (λb →  shipIsFighter (description b)) bs

fighterMove :: Body Ship →  Vector →  [Body Ship]
fighterMove (Body s mass (Movement l v a) d) direction = [Body {
        movement = Movement {
            location = l,
            velocity = vectorAdd δ v,
            acceleration = a
        },
        mass = mass,
        shape = s,
        description = d
    }]
    where
        δ = vectorScale (shipThrust d) direction

fighterFire :: Body Ship →  [Body Ship]
fighterFire b@(Body s mass m d) = [b] ⊕ bs
   where
        bs = map (λx →  projectileToBody x m) $ projectiles
        projectiles = shipFireWeapons d
        direction = vectorUp

universeActOnFighter :: (Universe Ship) →  (Body Ship →  [Body Ship]) →  (Universe Ship)
universeActOnFighter u f = Universe {
        bodies = bodiesActOnFighter (bodies u) f,
        time = time u
    }

bodiesActOnFighter :: [Body Ship] →  (Body Ship →  [Body Ship]) →  [Body Ship]
bodiesActOnFighter [] _ = []
bodiesActOnFighter (b:bs) f = b&#039; ⊕ bs&#039;
    where
        b&#039; = bodyActOnFighter b f
        bs&#039; = bodiesActOnFighter bs f

bodyActOnFighter :: Body Ship →  (Body Ship →  [Body Ship]) →  [Body Ship]
bodyActOnFighter b f
    | shipIsFighter $ description b = f b
    | otherwise = [b]
</pre>
<p></p>
<h3>Graphics Module</h3>
<p>The Graphics module deals with mapping the above data types into their corresponding <a href="http://www.haskell.org/haskellwiki/Opengl" target="blank">HOpenGL</a> counterparts. (I looked at a number of Haskell&#8217;s graphics libraries and ultimately chose to go with HOpenGL since I was the most familiar with OpenGL.) </p>
<pre>
coordinateToGLfloat :: Coordinate →  GLfloat
coordinateToGLfloat c = realToFrac c

type OpenGLPoint = (GLfloat, GLfloat, GLfloat)

pointToOpenGLPoint :: Geometry.Point →  OpenGLPoint
pointToOpenGLPoint (Geometry.Point x y) = (x', y', 0.0::GLfloat)
    where
        x' = coordinateToGLfloat x
        y' = coordinateToGLfloat y

type OpenGLView = [OpenGLPoint]

shapeToView :: Shape →  OpenGLView
shapeToView (Circle c r) = map pointToOpenGLPoint points
    where
        points = map (λθ →  Geometry.Point (r * (cos θ)) (r * (sin θ))) degrees
        degrees = map (λn →  n * increment ) [0..steps - 1]
        increment = 2.0 * pi / steps
        steps = 16

shipToView :: Ship →  OpenGLView
shipToView (Projectile _ _) = [ ... ]
shipToView (Fighter _ _) = [ ... ]
shipToView _ = undefined

openGLPointTranslate :: OpenGLPoint →  OpenGLPoint →  OpenGLPoint
openGLPointTranslate (x, y, z) (dx, dy, dz) = (x + dx, y + dy, z + dz)

openGLViewTranslate :: OpenGLView →  OpenGLPoint →  OpenGLView
openGLViewTranslate xs d = map (openGLPointTranslate d) xs

openGLPointToIO :: OpenGLPoint →  IO ()
openGLPointToIO (x, y, z) = vertex $ Vertex3 x y z

openGLViewToIO :: OpenGLView →  IO ()
openGLViewToIO ps = mapM_ openGLPointToIO ps

displayBody :: Body Ship →  IO()
displayBody (Body s mass m d) =
    color (Color3 (1.0::GLfloat) 1.0 1.0) &gt;&gt;
    renderPrimitive LineLoop ioShip
    where
        ioBody = openGLViewToIO $ openGLViewTranslate (shapeToView s) dl
        ioShip = openGLViewToIO $ openGLViewTranslate (shipToView d) dl
        dl = pointToOpenGLPoint l
        l = location m

displayUniverse :: Universe Ship →  IO ()
displayUniverse universe = mapM_ displayBody $ bodies universe
</pre>
<p></p>
<h3>Main Module</h3>
<p>The Main Module is the glue that brings together all of the other modules. Much of the functions described here are for gluing together the OpenGL callbacks to the functions described above.</p>
<pre>
theUniverse :: Universe Ship
theUniverse = ...

main :: IO()
main = do
    (programName, _) ←  getArgsAndInitialize
    initialDisplayMode $= [ DoubleBuffered ]
    createWindow "Space Cowboy"
    universe ←  newIORef theUniverse
    displayCallback $= (display universe)
    idleCallback $= Just (idle universe)
    keyboardMouseCallback $= Just (keyboardMouse universe)
    mainLoop

display :: IORef (Universe Ship) →  IO ()
display ioRefUniverse = do
    clear [ ColorBuffer ]
    loadIdentity
    scale 0.2 0.2 (0.2::GLfloat)
    universe ←  get ioRefUniverse
    displayUniverse universe
    swapBuffers
    flush

idle :: IORef (Universe Ship) →  IO ()
idle ioRefUniverse = do
    universe ←  get ioRefUniverse
    ioRefUniverse $= stepUniverse universe game
    threadDelay 10
    postRedisplay Nothing

stepUniverse :: (Universe Ship) →  (Universe Ship)
stepUniverse u = collided
    where
        collided = universeCollide filtered collide
        filtered = universeFilter evolved inBounds
        evolved = universeEvolve u 0.1

collide :: Body Ship →  [Body Ship] →  [Body Ship]
collide b@(Body s mass m (Projectile d t)) xs = []
collide b _ = [b]

inBounds :: Body Ship →  Bool
inBounds b@(Body _ _ (Movement (Geometry.Point x y) _ _) d)
    | shipIsFighter d = True
    | otherwise = and [abs x &lt; 10, abs y &lt; 10]

keyboardMouse ioRefUniverse key state modifiers position =
    keyboard ioRefUniverse key state

keyboard :: IORef (Universe Ship) →  Key →  KeyState →  IO ()
keyboard ioRefUniverse (Char &#039;q&#039;) Down = do exitSuccess
keyboard ioRefUniverse (Char &#039; &#039;) Down = fire ioRefUniverse
keyboard ioRefUniverse (SpecialKey KeyLeft) Down = navigate ioRefUniverse vectorLeft
keyboard ioRefUniverse (SpecialKey KeyRight) Down = navigate ioRefUniverse vectorRight
keyboard ioRefUniverse (SpecialKey KeyUp) Down = navigate ioRefUniverse vectorUp
keyboard ioRefUniverse (SpecialKey KeyDown) Down = navigate ioRefUniverse vectorDown
keyboard _ _ _ = return ()

fire :: IORef (Universe Ship) →  IO()
fire ioRefUniverse = do
    universe ←  get ioRefUniverse
    ioRefUniverse $= universeActOnFighter universe fighterFire

navigate :: IORef (Universe Ship) →  Vector →  IO ()
navigate ioRefUniverse direction = do
    universe ←  get ioRefUniverse
    ioRefUniverse $= universeActOnFighter universe (λf →  fighterMove f direction)
</pre>
<p></p>
<h3>Wrap-up</h3>
<p>For a month of on-again, off-again work, the prototype turned out reasonably well and I got a lot out of it. I think that as I continue to use Haskell, my brain will slowly switch from thinking in terms of structures of data to flows of data and I will ultimately be able to be more productive in Haskell. Until then, I&#8217;m going to stick with my current technology stack and continue to experiment with Haskell. Keep an eye for part three of this series which will go over the completed product.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/2197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/2197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/2197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/2197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/antimatroid.wordpress.com/2197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/antimatroid.wordpress.com/2197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/antimatroid.wordpress.com/2197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/antimatroid.wordpress.com/2197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/2197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/2197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/2197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/2197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/2197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/2197/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=2197&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2011/04/01/space-cowboy-a-shoot%e2%80%99em-up-game-in-haskell-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>
	</item>
		<item>
		<title>Water and Wine Problem</title>
		<link>http://antimatroid.wordpress.com/2011/03/01/water-and-wine-problem/</link>
		<comments>http://antimatroid.wordpress.com/2011/03/01/water-and-wine-problem/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 14:00:17 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Dynamical Systems]]></category>
		<category><![CDATA[Linear Algebra]]></category>
		<category><![CDATA[Difference Equations]]></category>
		<category><![CDATA[Eigen Decomposition]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=1994</guid>
		<description><![CDATA[Take two containers, and . Initially, is full of water and is full of wine. Remove a teaspoon of water from and put it in , then a teaspoon of the mixture in and put it in . In what proportions are the water and wine now mixed in and ? What are the proportions [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1994&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p>
Take two containers, <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='A' title='A' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=B&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='B' title='B' class='latex' />. Initially, <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='A' title='A' class='latex' /> is full of water and <img src='http://s0.wp.com/latex.php?latex=B&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='B' title='B' class='latex' /> is full of wine. Remove a teaspoon of water from <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='A' title='A' class='latex' /> and put it in <img src='http://s0.wp.com/latex.php?latex=B&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='B' title='B' class='latex' />, then a teaspoon of the mixture in <img src='http://s0.wp.com/latex.php?latex=B&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='B' title='B' class='latex' /> and put it in <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='A' title='A' class='latex' />. In what proportions are the water and wine now mixed in <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='A' title='A' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=B&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='B' title='B' class='latex' />? What are the proportions of water and wine in each container after <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n' title='n' class='latex' /> iterations?
</p></blockquote>
<p>First some assumptions: containers <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='A' title='A' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=B&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='B' title='B' class='latex' /> are identical in volume and both capable of containing the combined fluid of each. The initial amount of fluid in each container is identical. When transferring fluids between the containers, none of the fluid is lost. Rather than focusing on what amount a teaspoon represents, we&#8217;ll state that we are going to transfer a percentage, <img src='http://s0.wp.com/latex.php?latex=%5Calpha&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;alpha' title='&#92;alpha' class='latex' />, of <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='A' title='A' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=B&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='B' title='B' class='latex' />. Likewise, when transferring fluid from <img src='http://s0.wp.com/latex.php?latex=B&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='B' title='B' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='A' title='A' class='latex' />, we&#8217;ll use a percentage <img src='http://s0.wp.com/latex.php?latex=%5Cbeta&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;beta' title='&#92;beta' class='latex' />. Note that the amount that is transferred, <img src='http://s0.wp.com/latex.php?latex=%5Calpha+%5Cin+%280%2C+1%5D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;alpha &#92;in (0, 1]' title='&#92;alpha &#92;in (0, 1]' class='latex' />, from <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='A' title='A' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=B&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='B' title='B' class='latex' /> has to be equal to the amount that is later transferred, <img src='http://s0.wp.com/latex.php?latex=%5Cbeta%28%5Calpha+%2B+1%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;beta(&#92;alpha + 1)' title='&#92;beta(&#92;alpha + 1)' class='latex' />, from <img src='http://s0.wp.com/latex.php?latex=B&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='B' title='B' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='A' title='A' class='latex' />. From that fact we can conclude that <img src='http://s0.wp.com/latex.php?latex=%5Cbeta+%3D+%5Cfrac%7B%5Calpha%7D%7B%5Calpha+%2B+1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;beta = &#92;frac{&#92;alpha}{&#92;alpha + 1}' title='&#92;beta = &#92;frac{&#92;alpha}{&#92;alpha + 1}' class='latex' />. Following these conventions, we arrive at the following formulated events in the problem statement:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="33%" />
<td width="33%" colspan="2" align="center">Container A</td>
<td width="33%" colspan="2" align="center">Container B</td>
<tr>
<tr>
<td />
<td width="16%" align="center">Water</td>
<td width="16%" align="center">Wine</td>
<td width="16%" align="center">Water</td>
<td width="16%" align="center">Wine</td>
</tr>
<tr>
<td>Initially, A is full of water and B is full of wine.</td>
<td align="center"><img src='http://s0.wp.com/latex.php?latex=1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='1' title='1' class='latex' /></td>
<td align="center"><img src='http://s0.wp.com/latex.php?latex=0&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='0' title='0' class='latex' /></td>
<td align="center"><img src='http://s0.wp.com/latex.php?latex=0&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='0' title='0' class='latex' /></td>
<td align="center"><img src='http://s0.wp.com/latex.php?latex=1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='1' title='1' class='latex' /></td>
</tr>
<tr>
<td>Remove a teaspoon of water from A and put it in B,</td>
<td align="center"><img src='http://s0.wp.com/latex.php?latex=1-%5Calpha&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='1-&#92;alpha' title='1-&#92;alpha' class='latex' /></td>
<td align="center"><img src='http://s0.wp.com/latex.php?latex=0&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='0' title='0' class='latex' /></td>
<td align="center"><img src='http://s0.wp.com/latex.php?latex=%5Calpha&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;alpha' title='&#92;alpha' class='latex' /></td>
<td align="center"><img src='http://s0.wp.com/latex.php?latex=1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='1' title='1' class='latex' /></td>
</tr>
<tr>
<td>then a teaspoon of the mixture in B and put it in A.</td>
<td align="center"><img src='http://s0.wp.com/latex.php?latex=1+-+%5Calpha+%2B+%5Calpha+%5Cfrac%7B%5Calpha%7D%7B%5Calpha%2B1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='1 - &#92;alpha + &#92;alpha &#92;frac{&#92;alpha}{&#92;alpha+1}' title='1 - &#92;alpha + &#92;alpha &#92;frac{&#92;alpha}{&#92;alpha+1}' class='latex' /></td>
<td align="center"><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Calpha%7D%7B%5Calpha%2B1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;frac{&#92;alpha}{&#92;alpha+1}' title='&#92;frac{&#92;alpha}{&#92;alpha+1}' class='latex' /></td>
<td align="center"><img src='http://s0.wp.com/latex.php?latex=%5Calpha%281-%5Cfrac%7B%5Calpha%7D%7B%5Calpha%2B1%7D%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;alpha(1-&#92;frac{&#92;alpha}{&#92;alpha+1})' title='&#92;alpha(1-&#92;frac{&#92;alpha}{&#92;alpha+1})' class='latex' /></td>
<td align="center"><img src='http://s0.wp.com/latex.php?latex=1-%5Cfrac%7B%5Calpha%7D%7B%5Calpha%2B1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='1-&#92;frac{&#92;alpha}{&#92;alpha+1}' title='1-&#92;frac{&#92;alpha}{&#92;alpha+1}' class='latex' /></td>
</tr>
</table>
<p></br></p>
<p>One way of capturing these events is to model the system, <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BS%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{S}' title='&#92;mathcal{S}' class='latex' />, as a matrix. Each row of a matrix represents a container and each column of the matrix represents the amount of water or wine in the container. The initial state of the system, <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BS%7D_%7B0%7D+%3D+%5Cbegin%7Bpmatrix%7D+1+%26+0+%5C%5C+0+%26+1+%5Cend%7Bpmatrix%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{S}_{0} = &#92;begin{pmatrix} 1 &amp; 0 &#92;&#92; 0 &amp; 1 &#92;end{pmatrix}' title='&#92;mathcal{S}_{0} = &#92;begin{pmatrix} 1 &amp; 0 &#92;&#92; 0 &amp; 1 &#92;end{pmatrix}' class='latex' />, is the amount of water and wine in each container. The act of transferring fluid from one container to another and back again is given by <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BT%7D+%3D+%5Cbegin%7Bpmatrix%7D+1+%26+%5Cfrac%7B%5Calpha%7D%7B%5Calpha%2B1%7D+%5C%5C+0+%26+%5Cfrac%7B1%7D%7B%5Calpha%2B1%7D+%5Cend%7Bpmatrix%7D+%5Cbegin%7Bpmatrix%7D+1-%5Calpha+%26+0+%5C%5C+%5Calpha+%26+1+%5Cend%7Bpmatrix%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{T} = &#92;begin{pmatrix} 1 &amp; &#92;frac{&#92;alpha}{&#92;alpha+1} &#92;&#92; 0 &amp; &#92;frac{1}{&#92;alpha+1} &#92;end{pmatrix} &#92;begin{pmatrix} 1-&#92;alpha &amp; 0 &#92;&#92; &#92;alpha &amp; 1 &#92;end{pmatrix}' title='&#92;mathcal{T} = &#92;begin{pmatrix} 1 &amp; &#92;frac{&#92;alpha}{&#92;alpha+1} &#92;&#92; 0 &amp; &#92;frac{1}{&#92;alpha+1} &#92;end{pmatrix} &#92;begin{pmatrix} 1-&#92;alpha &amp; 0 &#92;&#92; &#92;alpha &amp; 1 &#92;end{pmatrix}' class='latex' /> which simplifies to <img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B%5Calpha%2B1%7D+%5Cbegin%7Bpmatrix%7D+1+%26+%5Calpha+%5C%5C+%5Calpha+%26+1+%5Cend%7Bpmatrix%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;frac{1}{&#92;alpha+1} &#92;begin{pmatrix} 1 &amp; &#92;alpha &#92;&#92; &#92;alpha &amp; 1 &#92;end{pmatrix}' title='&#92;frac{1}{&#92;alpha+1} &#92;begin{pmatrix} 1 &amp; &#92;alpha &#92;&#92; &#92;alpha &amp; 1 &#92;end{pmatrix}' class='latex' />. To determine the quantities of water and wine in each container after <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n' title='n' class='latex' /> iterations, we can looks at the recurrence relation <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BS%7D_%7Bn%7D+%3D+%5Cmathcal%7BT%7D+%5Cmathcal%7BS%7D_%7Bn-1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{S}_{n} = &#92;mathcal{T} &#92;mathcal{S}_{n-1}' title='&#92;mathcal{S}_{n} = &#92;mathcal{T} &#92;mathcal{S}_{n-1}' class='latex' /> which simplifies to <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BS%7D_%7Bn%7D+%3D+%5Cmathcal%7BT%7D%5E%7Bn%7D%5Cmathcal%7BS%7D_%7B0%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{S}_{n} = &#92;mathcal{T}^{n}&#92;mathcal{S}_{0}' title='&#92;mathcal{S}_{n} = &#92;mathcal{T}^{n}&#92;mathcal{S}_{0}' class='latex' />.</p>
<p><img src="http://antimatroid.files.wordpress.com/2010/11/waterwine.png?w=700" alt="" title="waterwine"   class="alignright size-full wp-image-2151" /></p>
<p>To figure out a closed form solution to <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BS%7D_%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{S}_{n}' title='&#92;mathcal{S}_{n}' class='latex' /> we&#8217;ll note that the system is a system of <a href="http://en.wikipedia.org/wiki/Difference_equation" target="blank">difference equations</a>. The general solution being <img src='http://s0.wp.com/latex.php?latex=f%28n%29+%3D+%5Csum%7B+c_%7Bi%7D+%5Cmathcal%7BQ%7D_%7B%28i%29%7D+%5Clambda_%7Bi%7D%5En+%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='f(n) = &#92;sum{ c_{i} &#92;mathcal{Q}_{(i)} &#92;lambda_{i}^n }' title='f(n) = &#92;sum{ c_{i} &#92;mathcal{Q}_{(i)} &#92;lambda_{i}^n }' class='latex' /> where <img src='http://s0.wp.com/latex.php?latex=c_%7Bi%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='c_{i}' title='c_{i}' class='latex' /> is a coefficient based on initial conditions, <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BQ%7D_%7B%28i%29%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{Q}_{(i)}' title='&#92;mathcal{Q}_{(i)}' class='latex' /> is the i&#8217;th eigenvector and <img src='http://s0.wp.com/latex.php?latex=%5Clambda_%7Bi%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;lambda_{i}' title='&#92;lambda_{i}' class='latex' /> is the i&#8217;th eigenvalue, i.e., the i&#8217;th solution to <img src='http://s0.wp.com/latex.php?latex=%5Clvert+%5Cmathcal%7BA%7D+-+%5Clambda+%5Cmathcal%7BI%7D+%5Crvert+%3D+0&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;lvert &#92;mathcal{A} - &#92;lambda &#92;mathcal{I} &#92;rvert = 0' title='&#92;lvert &#92;mathcal{A} - &#92;lambda &#92;mathcal{I} &#92;rvert = 0' class='latex' />.</p>
<p>Following this practice, we arrive at <img src='http://s0.wp.com/latex.php?latex=f%28n%29+%3D+%5Cdisplaystyle+c_%7B1%7D+%5Cbegin%7Bpmatrix%7D+1+%5C%5C+1+%5Cend%7Bpmatrix%7D+1%5E%7Bn%7D+%2B+c_%7B2%7D+%5Cbegin%7Bpmatrix%7D+-1+%5C%5C+1+%5Cend%7Bpmatrix%7D+%5Cfrac%7B1-%5Calpha%7D%7B1%2B%5Calpha%7D%5En&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='f(n) = &#92;displaystyle c_{1} &#92;begin{pmatrix} 1 &#92;&#92; 1 &#92;end{pmatrix} 1^{n} + c_{2} &#92;begin{pmatrix} -1 &#92;&#92; 1 &#92;end{pmatrix} &#92;frac{1-&#92;alpha}{1+&#92;alpha}^n' title='f(n) = &#92;displaystyle c_{1} &#92;begin{pmatrix} 1 &#92;&#92; 1 &#92;end{pmatrix} 1^{n} + c_{2} &#92;begin{pmatrix} -1 &#92;&#92; 1 &#92;end{pmatrix} &#92;frac{1-&#92;alpha}{1+&#92;alpha}^n' class='latex' />. To find <img src='http://s0.wp.com/latex.php?latex=c_%7B1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='c_{1}' title='c_{1}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=c_%7B2%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='c_{2}' title='c_{2}' class='latex' /> we solve for <img src='http://s0.wp.com/latex.php?latex=n+%3D+0&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n = 0' title='n = 0' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+c_%7B1%7D+%5Cbegin%7Bpmatrix%7D+1+%5C%5C+1+%5Cend%7Bpmatrix%7D+%2B+c_%7B2%7D+%5Cbegin%7Bpmatrix%7D+-1+%5C%5C+1+%5Cend%7Bpmatrix%7D+%3D+%5Cbegin%7Bpmatrix%7D+1+%5C%5C+0+%5Cend%7Bpmatrix%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle c_{1} &#92;begin{pmatrix} 1 &#92;&#92; 1 &#92;end{pmatrix} + c_{2} &#92;begin{pmatrix} -1 &#92;&#92; 1 &#92;end{pmatrix} = &#92;begin{pmatrix} 1 &#92;&#92; 0 &#92;end{pmatrix}' title='&#92;displaystyle c_{1} &#92;begin{pmatrix} 1 &#92;&#92; 1 &#92;end{pmatrix} + c_{2} &#92;begin{pmatrix} -1 &#92;&#92; 1 &#92;end{pmatrix} = &#92;begin{pmatrix} 1 &#92;&#92; 0 &#92;end{pmatrix}' class='latex' />. We go with <img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Bpmatrix%7D+1+%5C%5C+0+%5Cend%7Bpmatrix%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;begin{pmatrix} 1 &#92;&#92; 0 &#92;end{pmatrix}' title='&#92;begin{pmatrix} 1 &#92;&#92; 0 &#92;end{pmatrix}' class='latex' /> because it represents a container starting with one type of fluid and none of the other. The solution is <img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28n%29+%3D+%5Cfrac%7B1%7D%7B2%7D+%5Cbegin%7Bpmatrix%7D+1+%5C%5C+1+%5Cend%7Bpmatrix%7D+-+%5Cfrac%7B1%7D%7B2%7D+%5Cbegin%7Bpmatrix%7D+-1+%5C%5C+1+%5Cend%7Bpmatrix%7D+%5Cfrac%7B1-%5Calpha%7D%7B1%2B%5Calpha%7D%5En&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(n) = &#92;frac{1}{2} &#92;begin{pmatrix} 1 &#92;&#92; 1 &#92;end{pmatrix} - &#92;frac{1}{2} &#92;begin{pmatrix} -1 &#92;&#92; 1 &#92;end{pmatrix} &#92;frac{1-&#92;alpha}{1+&#92;alpha}^n' title='&#92;displaystyle f(n) = &#92;frac{1}{2} &#92;begin{pmatrix} 1 &#92;&#92; 1 &#92;end{pmatrix} - &#92;frac{1}{2} &#92;begin{pmatrix} -1 &#92;&#92; 1 &#92;end{pmatrix} &#92;frac{1-&#92;alpha}{1+&#92;alpha}^n' class='latex' />. Important to note that the solution represents both containers and one can swap the water and wine labels. </p>
<p>One last thing of interest is to look at the limiting behavior of <img src='http://s0.wp.com/latex.php?latex=f%28n%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='f(n)' title='f(n)' class='latex' />. As <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n' title='n' class='latex' /> increases, the first term remains constant and the second term tends towards zero given the domain of <img src='http://s0.wp.com/latex.php?latex=%5Calpha&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;alpha' title='&#92;alpha' class='latex' />. Thus, <img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Clim_%7Bn+%5Cto+%5Cinfty%7D+f%28n%29+%3D+%5Cfrac%7B1%7D%7B2%7D+%5Cbegin%7Bpmatrix%7D+1+%5C%5C+1+%5Cend%7Bpmatrix%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle &#92;lim_{n &#92;to &#92;infty} f(n) = &#92;frac{1}{2} &#92;begin{pmatrix} 1 &#92;&#92; 1 &#92;end{pmatrix}' title='&#92;displaystyle &#92;lim_{n &#92;to &#92;infty} f(n) = &#92;frac{1}{2} &#92;begin{pmatrix} 1 &#92;&#92; 1 &#92;end{pmatrix}' class='latex' />. Thus, no mater what size scope we take, we&#8217;ll always arrive at a container containing equal parts water and wine.</p>
<p>An alternative approach to figuring out the limiting behavior is to look directly at <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BS%7D_%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{S}_{n}' title='&#92;mathcal{S}_{n}' class='latex' />. Matrix computations are time consuming, so we&#8217;ll want to have a way to compute <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BS%7D_%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{S}_{n}' title='&#92;mathcal{S}_{n}' class='latex' /> that minimizes the number of computations. One way to do this is to apply an <a href="http://en.wikipedia.org/wiki/Eigendecomposition" target="blank">Eigen Decomposition</a> of a matrix. The idea is that we can decompose a matrix into a product of three matrices: first is the eigenvectors of the matrix, the second has the matrix&#8217;s eigenvalues along the diagonal and the third is an inverse of the first. The decomposition looks like the following: <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BA%7D+%3D+%5Cmathcal%7BQ%7D+%5CLambda+%5Cmathcal%7BQ%7D%5E%7B-%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{A} = &#92;mathcal{Q} &#92;Lambda &#92;mathcal{Q}^{-}' title='&#92;mathcal{A} = &#92;mathcal{Q} &#92;Lambda &#92;mathcal{Q}^{-}' class='latex' /> with <img src='http://s0.wp.com/latex.php?latex=%5CLambda_%7Bi%2Ci%7D+%3D+%5Clambda_%7Bi%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;Lambda_{i,i} = &#92;lambda_{i}' title='&#92;Lambda_{i,i} = &#92;lambda_{i}' class='latex' />. One important consequence is that we can compute <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BA%7D%5E%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{A}^{n}' title='&#92;mathcal{A}^{n}' class='latex' /> easily now by <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BQ%7D+%5CLambda+%5Cmathcal%7BQ%7D%5E%7B-%7D+%5Cmathcal%7BQ%7D+%5CLambda+%5Cmathcal%7BQ%7D%5E%7B-%7D+%5Ccdots+%5Cmathcal%7BQ%7D+%5CLambda+%5Cmathcal%7BQ%7D%5E%7B-%7D+%3D+%5Cmathcal%7BQ%7D+%5CLambda%5En+%5Cmathcal%7BQ%7D%5E%7B-%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{Q} &#92;Lambda &#92;mathcal{Q}^{-} &#92;mathcal{Q} &#92;Lambda &#92;mathcal{Q}^{-} &#92;cdots &#92;mathcal{Q} &#92;Lambda &#92;mathcal{Q}^{-} = &#92;mathcal{Q} &#92;Lambda^n &#92;mathcal{Q}^{-}' title='&#92;mathcal{Q} &#92;Lambda &#92;mathcal{Q}^{-} &#92;mathcal{Q} &#92;Lambda &#92;mathcal{Q}^{-} &#92;cdots &#92;mathcal{Q} &#92;Lambda &#92;mathcal{Q}^{-} = &#92;mathcal{Q} &#92;Lambda^n &#92;mathcal{Q}^{-}' class='latex' /> and as a direct consequence of how <img src='http://s0.wp.com/latex.php?latex=%5CLambda&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;Lambda' title='&#92;Lambda' class='latex' /> is defined, <img src='http://s0.wp.com/latex.php?latex=%5CLambda%5E%7Bn%7D_%7Bi%2Ci%7D+%3D+%5Clambda_%7Bi%7D%5E%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;Lambda^{n}_{i,i} = &#92;lambda_{i}^{n}' title='&#92;Lambda^{n}_{i,i} = &#92;lambda_{i}^{n}' class='latex' />.</p>
<p>Given that knowledge, we can decompose <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BT%7D%5E%7Bn%7D+%3D+%5Cmathcal%7BQ%7D%5CLambda%5En%5Cmathcal%7BQ%7D%5E%7B-%7D+%3D+%5Cfrac%7B1%7D%7B2%7D+%5Cbegin%7Bpmatrix%7D+1+%26+-1+%5C%5C+1+%26+1+%5Cend%7Bpmatrix%7D+%5Cbegin%7Bpmatrix%7D+1+%26+0+%5C%5C+0+%26+%5Cfrac%7B1-%5Calpha%7D%7B1%2B%5Calpha%7D%5E%7Bn%7D+%5Cend%7Bpmatrix%7D+%5Cbegin%7Bpmatrix%7D+1+%26+1+%5C%5C+-1+%26+1+%5Cend%7Bpmatrix%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{T}^{n} = &#92;mathcal{Q}&#92;Lambda^n&#92;mathcal{Q}^{-} = &#92;frac{1}{2} &#92;begin{pmatrix} 1 &amp; -1 &#92;&#92; 1 &amp; 1 &#92;end{pmatrix} &#92;begin{pmatrix} 1 &amp; 0 &#92;&#92; 0 &amp; &#92;frac{1-&#92;alpha}{1+&#92;alpha}^{n} &#92;end{pmatrix} &#92;begin{pmatrix} 1 &amp; 1 &#92;&#92; -1 &amp; 1 &#92;end{pmatrix}' title='&#92;mathcal{T}^{n} = &#92;mathcal{Q}&#92;Lambda^n&#92;mathcal{Q}^{-} = &#92;frac{1}{2} &#92;begin{pmatrix} 1 &amp; -1 &#92;&#92; 1 &amp; 1 &#92;end{pmatrix} &#92;begin{pmatrix} 1 &amp; 0 &#92;&#92; 0 &amp; &#92;frac{1-&#92;alpha}{1+&#92;alpha}^{n} &#92;end{pmatrix} &#92;begin{pmatrix} 1 &amp; 1 &#92;&#92; -1 &amp; 1 &#92;end{pmatrix}' class='latex' />.</p>
<p>Taking the limit <img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Clim_%7Bn+%5Crightarrow+%5Cinfty%7D+%5Cmathcal%7BT%7D%5En+%3D+%5Cfrac%7B1%7D%7B2%7D+%5Cbegin%7Bpmatrix%7D+1+%26+1+%5C%5C+1+%26+1+%5Cend%7Bpmatrix%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle &#92;lim_{n &#92;rightarrow &#92;infty} &#92;mathcal{T}^n = &#92;frac{1}{2} &#92;begin{pmatrix} 1 &amp; 1 &#92;&#92; 1 &amp; 1 &#92;end{pmatrix}' title='&#92;displaystyle &#92;lim_{n &#92;rightarrow &#92;infty} &#92;mathcal{T}^n = &#92;frac{1}{2} &#92;begin{pmatrix} 1 &amp; 1 &#92;&#92; 1 &amp; 1 &#92;end{pmatrix}' class='latex' /> leads to the <a href="http://en.wikipedia.org/wiki/Steady_state" target="blank">steady state</a> solution of <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BS%7D_%7B%5Cinfty%7D+%3D+%5Cfrac%7B1%7D%7B2%7D+%5Cbegin%7Bpmatrix%7D+1+%26+1+%5C%5C+1+%26+1+%5Cend%7Bpmatrix%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{S}_{&#92;infty} = &#92;frac{1}{2} &#92;begin{pmatrix} 1 &amp; 1 &#92;&#92; 1 &amp; 1 &#92;end{pmatrix}' title='&#92;mathcal{S}_{&#92;infty} = &#92;frac{1}{2} &#92;begin{pmatrix} 1 &amp; 1 &#92;&#92; 1 &amp; 1 &#92;end{pmatrix}' class='latex' />.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/1994/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/1994/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/antimatroid.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/antimatroid.wordpress.com/1994/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/antimatroid.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/antimatroid.wordpress.com/1994/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/1994/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/1994/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/1994/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1994&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2011/03/01/water-and-wine-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2010/11/waterwine.png" medium="image">
			<media:title type="html">waterwine</media:title>
		</media:content>
	</item>
		<item>
		<title>One tree many possibilities: Part 3</title>
		<link>http://antimatroid.wordpress.com/2011/02/01/one-tree-many-possibilities-part-3/</link>
		<comments>http://antimatroid.wordpress.com/2011/02/01/one-tree-many-possibilities-part-3/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 14:00:11 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[k-ary Cartesian product]]></category>
		<category><![CDATA[k-Combinations]]></category>
		<category><![CDATA[k-Permutations]]></category>
		<category><![CDATA[Permutations]]></category>
		<category><![CDATA[Power Set]]></category>
		<category><![CDATA[Proofs]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=1756</guid>
		<description><![CDATA[Introduction When I first began publishing content on this blog, I wrote a couple of posts entitled One tree many possibilities that covered how to enumerate the sets produced by different counting techniques. You can catch up on the idea in part one and the C# implementation in part two. In this post I&#8217;m going [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1756&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>When I first began publishing content on this blog, I wrote a couple of posts entitled <i>One tree many possibilities</i> that covered how to enumerate the sets produced by different counting techniques. You can catch up on the idea in <a href="http://antimatroid.wordpress.com/2008/06/29/one-tree-many-possibilities/" target="blank">part one</a> and the C# implementation in <a href="http://antimatroid.wordpress.com/2008/07/06/one-tree-many-possibilities-part-2/" target="blank">part two</a>. In this post I&#8217;m going to cover how to take the knowledge from the first two posts and derive closed form solutions to each of the counting techniques through analysis of their respective <a href="http://en.wikipedia.org/wiki/Recurrence_relation" target="blank">recurrence relations</a>.</p>
<h3>Preliminaries</h3>
<p>The basic idea is that that the set <img src='http://s0.wp.com/latex.php?latex=S&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='S' title='S' class='latex' /> and each of its elements, <img src='http://s0.wp.com/latex.php?latex=e&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='e' title='e' class='latex' />, are supplied to a binary operation, <img src='http://s0.wp.com/latex.php?latex=%5Coplus&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;oplus' title='&#92;oplus' class='latex' />, that maps to a subset <img src='http://s0.wp.com/latex.php?latex=S%5E%7B%5Cprime%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='S^{&#92;prime}' title='S^{&#92;prime}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=S&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='S' title='S' class='latex' />. This process is performed iteratively on <img src='http://s0.wp.com/latex.php?latex=S%5E%7B%5Cprime%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='S^{&#92;prime}' title='S^{&#92;prime}' class='latex' /> until the process has been applied to a depth of <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k' title='k' class='latex' /> or the resulting subset becomes the empty set. </p>
<p>Since we can have a variety of conceivable sets, we need to map sets of a given cardinality to a common set of the same cardinality. Let <img src='http://s0.wp.com/latex.php?latex=b%28S%29+%3D+P_%7B%5Clvert+S+%5Crvert%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='b(S) = P_{&#92;lvert S &#92;rvert}' title='b(S) = P_{&#92;lvert S &#92;rvert}' class='latex' /> be a <a href="http://en.wikipedia.org/wiki/Bijection" target="blank">bijective function</a> that maps a set to the set <img src='http://s0.wp.com/latex.php?latex=P_%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{n}' title='P_{n}' class='latex' /> of positive integers from 1 to <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n' title='n' class='latex' />.</p>
<h3>k-ary Cartesian Product</h3>
<p>The <a href="http://en.wikipedia.org/wiki/Cartesian_product#n-ary_product" target="blank">k-ary cartesian product</a> is the way of selecting <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k' title='k' class='latex' /> items from a set where the order of the selection is important and items can be placed back into the set after selection. </p>
<p>The process uses <img src='http://s0.wp.com/latex.php?latex=%5Coplus+%3D+id&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;oplus = id' title='&#92;oplus = id' class='latex' /> where <img src='http://s0.wp.com/latex.php?latex=id%28S%2C+e%29+%3D+S&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='id(S, e) = S' title='id(S, e) = S' class='latex' /> is an identity function. As an example, here is a graph of the process applied to <img src='http://s0.wp.com/latex.php?latex=P_%7B3%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{3}' title='P_{3}' class='latex' /> with <img src='http://s0.wp.com/latex.php?latex=k+%3D+3&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k = 3' title='k = 3' class='latex' />:</p>
<p><a href="http://antimatroid.files.wordpress.com/2010/09/cartesian-product.png"><img src="http://antimatroid.files.wordpress.com/2010/09/cartesian-product.png?w=700&#038;h=67" alt="" title="cartesian-product" width="700" height="67" class="aligncenter size-full wp-image-1838" /></a></p>
<p>To count the number of elements, we need to count the number of nodes at depth <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k' title='k' class='latex' />. We will write this using the following recurrence relation:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7Bn%7D%2C+0%29+%3D+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{n}, 0) = 1' title='&#92;displaystyle f(P_{n}, 0) = 1' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7Bn%7D%2C+k%29+%3D+%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+%7Bf%28P_%7Bn%7D%2C+k+-+1%29+%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{n}, k) = &#92;sum_{i=1}^{n} {f(P_{n}, k - 1) }' title='&#92;displaystyle f(P_{n}, k) = &#92;sum_{i=1}^{n} {f(P_{n}, k - 1) }' class='latex' /></p>
<p>If we think of the tree having a height of <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k' title='k' class='latex' />, then we can label each layer from <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k' title='k' class='latex' /> (at the root) to zero. As a consequence, we can interpret the first statement as a node at depth zero will be considered a leaf node and should be counted once. The second statement says that for each element in <img src='http://s0.wp.com/latex.php?latex=P_%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{n}' title='P_{n}' class='latex' /> we will add up whatever number of leaf nodes were counted in the layer beneath the current layer <img src='http://s0.wp.com/latex.php?latex=k+-+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k - 1' title='k - 1' class='latex' />.</p>
<p>Rearranging the second statement leads to <img src='http://s0.wp.com/latex.php?latex=f%28P_%7Bn%7D%2C+k%29+%3D+n+f%28P_%7Bn%7D%2C+k+-+1%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='f(P_{n}, k) = n f(P_{n}, k - 1)' title='f(P_{n}, k) = n f(P_{n}, k - 1)' class='latex' />. Reducing further <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k' title='k' class='latex' /> times leads to <img src='http://s0.wp.com/latex.php?latex=f%28P_%7Bn%7D%2C+k%29+%3D+n+%28n+%5Ccdots+%28n%281%29%29%29+%3D+n%5E%7Bk%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='f(P_{n}, k) = n (n &#92;cdots (n(1))) = n^{k}' title='f(P_{n}, k) = n (n &#92;cdots (n(1))) = n^{k}' class='latex' />.</p>
<h3>k-Permutations</h3>
<p>The <a href="http://en.wikipedia.org/wiki/Permutation#In_combinatorics" target="blank">permutation and k-permutation</a> is a way of selecting <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k' title='k' class='latex' /> items (<img src='http://s0.wp.com/latex.php?latex=k+%5Cle+n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k &#92;le n' title='k &#92;le n' class='latex' />) from a list where the order of the selection is important but items cannot be placed back into the set after selection.</p>
<p>The process uses <img src='http://s0.wp.com/latex.php?latex=%5Coplus+%3D+%5Csetminus&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;oplus = &#92;setminus' title='&#92;oplus = &#92;setminus' class='latex' /> where <img src='http://s0.wp.com/latex.php?latex=%5Csetminus%28S%2C+e%29+%3D+%5Clbrace+s+%5Ccolon+s+%5Cne+e%2C+s+%5Cin+S+%5Crbrace&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;setminus(S, e) = &#92;lbrace s &#92;colon s &#92;ne e, s &#92;in S &#92;rbrace' title='&#92;setminus(S, e) = &#92;lbrace s &#92;colon s &#92;ne e, s &#92;in S &#92;rbrace' class='latex' /> is the set difference operator. We use the set difference because the item is removed from the set upon each selection. As before, here is a graph of the process applied to <img src='http://s0.wp.com/latex.php?latex=P_%7B3%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{3}' title='P_{3}' class='latex' /> with <img src='http://s0.wp.com/latex.php?latex=k%3D3&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k=3' title='k=3' class='latex' />:</p>
<p><a href="http://antimatroid.files.wordpress.com/2010/09/permutation.png"><img src="http://antimatroid.files.wordpress.com/2010/09/permutation.png?w=700&#038;h=67" alt="" title="permutation" width="700" height="67" class="aligncenter size-full wp-image-1840" /></a></p>
<p>To count the number of elements, we need to count the number of nodes at depth <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k' title='k' class='latex' />. (The regular permutation can be counted when <img src='http://s0.wp.com/latex.php?latex=k+%3D+n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k = n' title='k = n' class='latex' />.) We will write this using the following recurrence relation:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7Bn%7D%2C+0%29+%3D+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{n}, 0) = 1' title='&#92;displaystyle f(P_{n}, 0) = 1' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7Bn%7D%2C+k%29+%3D+%5Csum_%7Bi+%3D+1%7D%5E%7Bn%7D+%7Bf%28P_%7Bn-1%7D%2C+k+-+1%29%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{n}, k) = &#92;sum_{i = 1}^{n} {f(P_{n-1}, k - 1)}' title='&#92;displaystyle f(P_{n}, k) = &#92;sum_{i = 1}^{n} {f(P_{n-1}, k - 1)}' class='latex' /></p>
<p>Using the same labeling scheme as in the k-ary cartesian product, we can interpret the first statement as any node at depth zero will be considered a leaf node and counted once. The second statement says that for each element in <img src='http://s0.wp.com/latex.php?latex=P_%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{n}' title='P_{n}' class='latex' /> we will add up whatever number of leaf nodes were counted in the layer beneath the current layer <img src='http://s0.wp.com/latex.php?latex=k+-+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k - 1' title='k - 1' class='latex' /> for the set <img src='http://s0.wp.com/latex.php?latex=P_%7Bn-1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{n-1}' title='P_{n-1}' class='latex' />. We use <img src='http://s0.wp.com/latex.php?latex=P_%7Bn-1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{n-1}' title='P_{n-1}' class='latex' /> since we removed an element from <img src='http://s0.wp.com/latex.php?latex=P_%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{n}' title='P_{n}' class='latex' />.</p>
<p>Rearranging the second statement leads to <img src='http://s0.wp.com/latex.php?latex=f%28P_%7Bn%7D%2C+k%29+%3D+n+f%28P_%7Bn-1%7D%2C+k+-+1%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='f(P_{n}, k) = n f(P_{n-1}, k - 1)' title='f(P_{n}, k) = n f(P_{n-1}, k - 1)' class='latex' />. Taking <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k' title='k' class='latex' /> times leads to <img src='http://s0.wp.com/latex.php?latex=f%28P_%7Bn%7D%2C+k%29+%3D+n+%28n+-+1+%5Ccdots+%28n+-+k+%2B+1%281%29%29%29+%3D+%5Cfrac%7Bn%21%7D%7B%28n-k%29%21%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='f(P_{n}, k) = n (n - 1 &#92;cdots (n - k + 1(1))) = &#92;frac{n!}{(n-k)!}' title='f(P_{n}, k) = n (n - 1 &#92;cdots (n - k + 1(1))) = &#92;frac{n!}{(n-k)!}' class='latex' />.</p>
<h3>Power Set</h3>
<p>The <a href="http://en.wikipedia.org/wiki/Power_set" target="blank">power set</a> is the way of selecting zero items to the cardinality of the set items from a set where the order of the selection is not important and items are not placed back into the set after selection. </p>
<p>The process uses <img src='http://s0.wp.com/latex.php?latex=%5Coplus+%3D+%3C&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;oplus = &lt;' title='&#92;oplus = &lt;' class='latex' /> where <img src='http://s0.wp.com/latex.php?latex=%3C%28S%2C+e%29+%3D+%5Clbrace+s+%5Ccolon+s+%3C+e%2C+s+%5Cin+S+%5Crbrace&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&lt;(S, e) = &#92;lbrace s &#92;colon s &lt; e, s &#92;in S &#92;rbrace' title='&lt;(S, e) = &#92;lbrace s &#92;colon s &lt; e, s &#92;in S &#92;rbrace' class='latex' />. As before, here is a graph of the process applied to <img src='http://s0.wp.com/latex.php?latex=P_%7B3%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{3}' title='P_{3}' class='latex' />:</p>
<p><a href="http://antimatroid.files.wordpress.com/2010/09/power-set1.png"><img src="http://antimatroid.files.wordpress.com/2010/09/power-set1.png?w=700&#038;h=67" alt="" title="power-set" width="700" height="67" class="aligncenter size-full wp-image-1842" /></a></p>
<p>To count the number of elements, we need to count the number of nodes in the tree. We will write this using the following recurrence relation:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7B0%7D%29+%3D+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{0}) = 1' title='&#92;displaystyle f(P_{0}) = 1' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7Bn%7D%29+%3D+%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+%7Bf%28P_%7Bi-1%7D%29%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{n}) = &#92;sum_{i=1}^{n} {f(P_{i-1})}' title='&#92;displaystyle f(P_{n}) = &#92;sum_{i=1}^{n} {f(P_{i-1})}' class='latex' /></p>
<p>The first statement says for any node a depth zero we will count it once. The second statement states that for each element in <img src='http://s0.wp.com/latex.php?latex=P_%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{n}' title='P_{n}' class='latex' /> we will add up the result from <img src='http://s0.wp.com/latex.php?latex=P_%7B0%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{0}' title='P_{0}' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=P_%7Bn-1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{n-1}' title='P_{n-1}' class='latex' />. Starting at <img src='http://s0.wp.com/latex.php?latex=i+%3D+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='i = 1' title='i = 1' class='latex' /> is a way of encoding that all nodes should be counted. We go up to <img src='http://s0.wp.com/latex.php?latex=P_%7Bn-1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{n-1}' title='P_{n-1}' class='latex' /> because there are <img src='http://s0.wp.com/latex.php?latex=n+-+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n - 1' title='n - 1' class='latex' /> elements in <img src='http://s0.wp.com/latex.php?latex=P_%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{n}' title='P_{n}' class='latex' /> less than <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n' title='n' class='latex' />.</p>
<p>Rearranging the second statement leads to <img src='http://s0.wp.com/latex.php?latex=f%28P_%7Bn%7D%29+%3D+%5Csum_%7Bi%3D1%7D%5E%7Bn-1%7D+%7Bf%28P_%7Bi-1%7D%29%7D+%2B+f%28P_%7Bn-1%7D%29+%3D+2+f%28P_%7Bn-1%7D%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='f(P_{n}) = &#92;sum_{i=1}^{n-1} {f(P_{i-1})} + f(P_{n-1}) = 2 f(P_{n-1})' title='f(P_{n}) = &#92;sum_{i=1}^{n-1} {f(P_{i-1})} + f(P_{n-1}) = 2 f(P_{n-1})' class='latex' />. Taking the expression to zero leads to <img src='http://s0.wp.com/latex.php?latex=f%28P_%7Bn%7D%29+%3D+2%282+%5Ccdots%282%281%29%29%29+%3D+2%5E%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='f(P_{n}) = 2(2 &#92;cdots(2(1))) = 2^{n}' title='f(P_{n}) = 2(2 &#92;cdots(2(1))) = 2^{n}' class='latex' />.</p>
<h3>k-Combinations</h3>
<p>The <a href="http://en.wikipedia.org/wiki/Combination#Number_of_k-combinations" target="blank">k-combination</a> is the way of selecting a fixed number of items from a set where the order of the selection is not important and items are not placed back into the set after selection. </p>
<p>The process is identical to the power set. </p>
<p>To count the number of elements, we need to count the number of nodes at depth <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k' title='k' class='latex' /> in the tree. We will write this using the following recurrence relation:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7Bn%7D%2C+0%29+%3D+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{n}, 0) = 1' title='&#92;displaystyle f(P_{n}, 0) = 1' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7Bn%7D%2C+k%29+%3D+%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+%7Bf%28P_%7Bi-1%7D%2C+k-1%29+%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{n}, k) = &#92;sum_{i=1}^{n} {f(P_{i-1}, k-1) }' title='&#92;displaystyle f(P_{n}, k) = &#92;sum_{i=1}^{n} {f(P_{i-1}, k-1) }' class='latex' /></p>
<p>The first statement says for depth zero we will count once, the second statement states that we will add up whatever sum was produced by iterating over <img src='http://s0.wp.com/latex.php?latex=P_%7Bi-1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{i-1}' title='P_{i-1}' class='latex' /> from one to <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n' title='n' class='latex' /> and reducing the depth <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k' title='k' class='latex' /> by one. To find the closed form solution, we&#8217;ll approach the recurrence relation inductively:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7Bn%7D%2C+0%29+%3D+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{n}, 0) = 1' title='&#92;displaystyle f(P_{n}, 0) = 1' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7Bn%7D%2C+1%29+%3D+%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+%7Bf%28P_%7Bi-1%7D%2C+0%29+%7D+%3D+%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+%7B1%7D+%3D+n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{n}, 1) = &#92;sum_{i=1}^{n} {f(P_{i-1}, 0) } = &#92;sum_{i=1}^{n} {1} = n' title='&#92;displaystyle f(P_{n}, 1) = &#92;sum_{i=1}^{n} {f(P_{i-1}, 0) } = &#92;sum_{i=1}^{n} {1} = n' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7Bn%7D%2C+2%29+%3D+%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+%7Bf%28P_%7Bi-1%7D%2C+1%29+%7D+%3D+%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+%7Bi-1%7D+%3D+%5Cfrac%7Bn%28n-1%29%7D%7B2%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{n}, 2) = &#92;sum_{i=1}^{n} {f(P_{i-1}, 1) } = &#92;sum_{i=1}^{n} {i-1} = &#92;frac{n(n-1)}{2}' title='&#92;displaystyle f(P_{n}, 2) = &#92;sum_{i=1}^{n} {f(P_{i-1}, 1) } = &#92;sum_{i=1}^{n} {i-1} = &#92;frac{n(n-1)}{2}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7Bn%7D%2C+3%29+%3D+%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+%7Bf%28P_%7Bi-1%7D%2C+2%29+%7D+%3D+%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+%7B%5Cfrac%7B%28i-1%29%28i-2%29%7D%7B2%7D%7D+%3D+%5Cfrac%7Bn%28n-1%29%28n-2%29%7D%7B6%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{n}, 3) = &#92;sum_{i=1}^{n} {f(P_{i-1}, 2) } = &#92;sum_{i=1}^{n} {&#92;frac{(i-1)(i-2)}{2}} = &#92;frac{n(n-1)(n-2)}{6}' title='&#92;displaystyle f(P_{n}, 3) = &#92;sum_{i=1}^{n} {f(P_{i-1}, 2) } = &#92;sum_{i=1}^{n} {&#92;frac{(i-1)(i-2)}{2}} = &#92;frac{n(n-1)(n-2)}{6}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+f%28P_%7Bn%7D%2C+k%29+%3D+%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+%7Bf%28P_%7Bi-1%7D%2C+k-1%29+%7D+%3D+%5Cfrac%7Bn%28n-1%29%28n-2%29%5Ccdots%28n-k%2B1%29%7D%7Bk%28k-1%29%28k-2%29%5Ccdots1%7D+%3D+%5Cfrac%7Bn%21%7D%7Bk%21%28n-k%29%21%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle f(P_{n}, k) = &#92;sum_{i=1}^{n} {f(P_{i-1}, k-1) } = &#92;frac{n(n-1)(n-2)&#92;cdots(n-k+1)}{k(k-1)(k-2)&#92;cdots1} = &#92;frac{n!}{k!(n-k)!}' title='&#92;displaystyle f(P_{n}, k) = &#92;sum_{i=1}^{n} {f(P_{i-1}, k-1) } = &#92;frac{n(n-1)(n-2)&#92;cdots(n-k+1)}{k(k-1)(k-2)&#92;cdots1} = &#92;frac{n!}{k!(n-k)!}' class='latex' /></p>
<p>Important to notice that for <img src='http://s0.wp.com/latex.php?latex=k+%3D+0&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k = 0' title='k = 0' class='latex' /> that we have the unit, for <img src='http://s0.wp.com/latex.php?latex=k+%3D+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k = 1' title='k = 1' class='latex' /> we have the natural numbers, for <img src='http://s0.wp.com/latex.php?latex=k+%3D+2&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k = 2' title='k = 2' class='latex' /> we have the triangle numbers, for <img src='http://s0.wp.com/latex.php?latex=k+%3D+3&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='k = 3' title='k = 3' class='latex' /> we have the tetrahedral numbers and so on. The generalized sequence is the <a href="http://en.wikipedia.org/wiki/Figurate_number" target="blank">Figurate number</a>- these numbers constitute <a href="http://en.wikipedia.org/wiki/Pascal%27s_triangle" target="blank">Pascal&#8217;s Triangle</a> which is one method of calculating combinations.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/1756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/1756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/1756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/1756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/antimatroid.wordpress.com/1756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/antimatroid.wordpress.com/1756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/antimatroid.wordpress.com/1756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/antimatroid.wordpress.com/1756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/1756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/1756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/1756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/1756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/1756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/1756/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1756&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2011/02/01/one-tree-many-possibilities-part-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2010/09/cartesian-product.png" medium="image">
			<media:title type="html">cartesian-product</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2010/09/permutation.png" medium="image">
			<media:title type="html">permutation</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2010/09/power-set1.png" medium="image">
			<media:title type="html">power-set</media:title>
		</media:content>
	</item>
		<item>
		<title>Space Cowboy: A Shoot&#8217;em up game in Haskell: Part 1</title>
		<link>http://antimatroid.wordpress.com/2011/01/01/space-cowboy-a-shootem-up-game-in-haskell-part-1/</link>
		<comments>http://antimatroid.wordpress.com/2011/01/01/space-cowboy-a-shootem-up-game-in-haskell-part-1/#comments</comments>
		<pubDate>Sat, 01 Jan 2011 14:00:16 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Shoot'em up]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=1608</guid>
		<description><![CDATA[It&#8217;s the start of a new year and January always marks the return of Antimatroid, The. The past few months I&#8217;ve been busy working on a number of project around the house and spending some time working on a few projects for around here. This article serves as an introduction to a shoot&#8217;em up game [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1608&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><i>It&#8217;s the start of a new year and January always marks the return of Antimatroid, The. The past few months I&#8217;ve been busy working on a number of project around the house and spending some time working on a few projects for around here. This article serves as an introduction to a shoot&#8217;em up game that I&#8217;ve been working on since July. Future installments will cover the implementation and design aspects of the project.</i></p>
<h3>Introduction</h3>
<p>The past few months I&#8217;ve been working on a <a href="http://en.wikipedia.org/wiki/Shoot%27em_up" target="blank">Shoot&#8217;em up</a> game in Haskell and decided that I&#8217;d gotten to a point where a writeup was in order. I&#8217;ve always enjoyed the genre and I wanted to create something with a degree of complexity greater than my previous arcade games. Along the same lines, I decided to go with Haskell so that I could get a better hang of the language and build something tangible and practical beyond the utilities and solvers I&#8217;ve written. The title of the game is a nod to various <a href="http://en.wikipedia.org/wiki/Space_western" target="blank">Space Westerns</a> that inspired me to get started. The following writeup goes over the software development process I applied to the project from specification to implementation.</p>
<h3>Specification</h3>
<p>Space Cowboy is a single player game consisting of a sequence of levels. The user starts on the first level and plays until he or she has completed the level and progresses onto the next level and so on. Once there are no more levels left, the user has won the game. If a user fails to complete a level, then the game is over. Each level is of a fixed length and contains a fixed number of opponents. To complete a level, the user either gets to the end of the level or destroys all of the opponents. If an opponent destroys the user, then the user does not complete the level and the game is over. </p>
<p>The user and the opponents are represented by ships. Ships start with a fixed number of hit points and lose hit points any time the ship is attacked or it collides with another ship. Once a ship has lost enough hit points to match its initial hit points, then it is considered destroyed. Ships can attack one another using weapons that come in a variety of rates of fire and magnitude of damage. Each ship can can maneuver around the level using engines that come in a variety of thrusts and rates of propulsion. The user&#8217;s ship can be maneuvered left, right, up and down and instructed to fire its weapons. Opponents ships are maneuvered by the game. Ships can improve their abilities by colliding with power-ups which upgrade weapons, engines and hit points. Power-ups are produced randomly whenever a ship is destroyed. </p>
<p>A score system keeps track of the user&#8217;s progress. When the user starts the game, his or her score is set to zero and increases as he or she plays each level. Whenever a user gets a power-up or destroys a ship, then their score increments proportionally to the magnitude of the power-up or ship destroyed. A user&#8217;s score is never decremented. The user is incentivised to play again to obtain a higher score than his or her previous trial. </p>
<h3>Requirements</h3>
<h4>User Interface</h4>
<p>The user interface will be a portfolio view consisting of four screens:</p>
<ul>
<li>Logo Screen: displays the developer&#8217;s logo and copyright information</li>
<li>Menu Screen: allows the user to start a new game, view high scores and exit the application</li>
<li>Game Screen: contains information about what level the user is on, their score, the number of lives they have left and the view of the game universe</li>
<li>High Scores Screen: contains a list of the top five high scores achieved in the game</li>
</ul>
<p>When the application starts, the user will see the Logo Screen which will transition to the Main Menu Screen after a few seconds. When the user presses the New Game Button, the application will go to the Game Screen. When the user presses the High Scores Button, the application will go to the High Scores Screen. When a user completes their game, they will transition to the High Scores Screen. If the user has a new high score, they will be asked to enter the name they want to associate with the score. The user may go back to the Main Menu Screen by clicking on the Menu Button.</p>
<p><a href="http://antimatroid.files.wordpress.com/2010/11/revised_sketch.png"><img src="http://antimatroid.files.wordpress.com/2010/11/revised_sketch.png?w=700&#038;h=326" alt="" title="revised_sketch" width="700" height="326" class="aligncenter size-full wp-image-1969" /></a></p>
<h4>Gameplay</h4>
<p>The game universe is a simplified model of the physical universe. The game universe has two spatial dimensions and one temporal dimension. Both spatial dimensions are bounded and residents of the game universe may not exist outside of those boundaries. When the game starts, the user will be at one extreme of these boundaries and must reach the other extreme to complete the level.</p>
<p>Every resident in the game universe has a dimension, heading and location. No two residents of the game universe may occupy the same space at the same time. When two residents collide, their resulting motions will follow Newton&#8217;s laws of motion. If the damage done on either body during the collision is material, then the body will be reduced to debris and power-ups.</p>
<p>When a ship fires its engines, the thrust of the engine will advance the ship in the direction indicated by the user with respect to the ship&#8217;s heading. When the ship fires its weapons, the ship will experience kickback in accordance with Newton&#8217;s third law of motion. As the user&#8217;s ship moves through the game universe, the view of the game universe will be centered on the user&#8217;s ship.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/1608/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/1608/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/1608/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/1608/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/antimatroid.wordpress.com/1608/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/antimatroid.wordpress.com/1608/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/antimatroid.wordpress.com/1608/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/antimatroid.wordpress.com/1608/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/1608/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/1608/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/1608/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/1608/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/1608/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/1608/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1608&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2011/01/01/space-cowboy-a-shootem-up-game-in-haskell-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2010/11/revised_sketch.png" medium="image">
			<media:title type="html">revised_sketch</media:title>
		</media:content>
	</item>
		<item>
		<title>Another busy month</title>
		<link>http://antimatroid.wordpress.com/2010/09/01/another-busy-month/</link>
		<comments>http://antimatroid.wordpress.com/2010/09/01/another-busy-month/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 14:00:02 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Buffalo Bicycle Classic]]></category>
		<category><![CDATA[Vacation]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=1732</guid>
		<description><![CDATA[I have had a busy August and it is looking like September will be equally busy. In August I took a vacation out to San Francisco, CA and enjoyed checking out Union Square, Chinatown, Fisherman&#8217;s Wharf, the Golden Gate Bridge and Alcatraz. I especially enjoyed riding from San Francisco up to Tiburon and catching a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1732&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have had a busy August and it is looking like September will be equally busy. In August I took a vacation out to San Francisco, CA and enjoyed checking out Union Square, Chinatown, Fisherman&#8217;s Wharf, the Golden Gate Bridge and Alcatraz. I especially enjoyed riding from San Francisco up to Tiburon and catching a ferry back. In September I will be participating in the <a href="http://www.buffalobicycleclassic.com/" target="blank">Buffalo Bicycle Classic</a> that takes place here in Boulder, CO. I&#8217;ve done the event in the past and am looking forward to closing out my bicycle season with a ride along the front range to support the College of Arts and Sciences at the University of Colorado at Boulder. </p>
<p>As a consequence of how I want to be spending my time, I am finding it difficult to work on side projects and to post write ups. I am going to postpone posting any new content until later this year or around the start of next year. I enjoy taking my time working on interesting projects to produce interesting content, but feel that I would only be posting ho-hum content about ho-hum projects if I can&#8217;t commit the proper amount of time. I am planning on returning with posts covering a game I&#8217;ve been working on in Haskell, exploring the relationship between source code and team organization and developing some new tree drawing algorithms (which seem to bring a lot of you to this website.) I am also planning on writing on topics in business, finance and architecture as it relates to software engineering.</p>
<p>If you enjoy the content on this site, I recommend <a href="http://antimatroid.wordpress.com/feed/" target="blank">adding it to your RSS reader</a> and keeping an eye out for future posts. In the mean time, take a look through the archives and find something interesting to read.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/1732/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/1732/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/1732/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/1732/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/antimatroid.wordpress.com/1732/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/antimatroid.wordpress.com/1732/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/antimatroid.wordpress.com/1732/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/antimatroid.wordpress.com/1732/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/1732/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/1732/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/1732/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/1732/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/1732/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/1732/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1732&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2010/09/01/another-busy-month/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>
	</item>
		<item>
		<title>Haskell ecosystem on Windows XP</title>
		<link>http://antimatroid.wordpress.com/2010/08/01/haskell-ecosystem-on-windows-xp/</link>
		<comments>http://antimatroid.wordpress.com/2010/08/01/haskell-ecosystem-on-windows-xp/#comments</comments>
		<pubDate>Sun, 01 Aug 2010 14:00:36 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Gtk2hs]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Haskell Platform]]></category>
		<category><![CDATA[HOpenGL]]></category>
		<category><![CDATA[Leksah]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=1616</guid>
		<description><![CDATA[It&#8217;s been fun watching the Haskell ecosystem evolve into a mature system over the years. Seems that about every three months it takes a leap forward and I usually find myself uninstalling what I previously had and putting the latest and greatest on my laptop. To save myself some time in the future, I&#8217;ve compiled [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1616&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been fun watching the Haskell ecosystem evolve into a mature system over the years. Seems that about every three months it takes a leap forward and I usually find myself uninstalling what I previously had and putting the latest and greatest on my laptop. To save myself some time in the future, I&#8217;ve compiled this post as a reference of basic &#8220;stuff&#8221; that is good to have for doing Haskell development on a Windows XP machine.</p>
<h3>Haskell Platform</h3>
<p>It used to be that you had to go find a number of different applications to get packages, compile source code and generate documents (among a handful of other things), then a group of folks identified an opportunity and put together a platform for getting new users a place to start. The result is the <a href="http://hackage.haskell.org/platform/" target="blank">Haskell Platform</a>.</p>
<ul>
<li><a href="http://www.haskell.org/ghc/" target="blank">Glasgow Haskell Compiler</a> (GHC) &#8211; <i>The</i> Haskell compiler that also comes with a command line interpreter (GHCi). Alternatives are the <a href="http://yhc06.blogspot.com/" target="blank">York Haskell Compiler</a> (yhc) and <a href="http://www.haskell.org/haskellwiki/Hugs" target="blank">Hugs</a></li>
<li><a href="http://haskell.org/cabal/" target="blank">Cabal</a> &#8211; Basic package management system</li>
<li><a href="http://www.haskell.org/haddock/" target="blank">Haddock</a> &#8211; used for generating documentation based on comments in source code</li>
</ul>
<p>After installing, you&#8217;ll want to go to the command line and run the following commands to make sure that you&#8217;ve got the latest version of Cabal and to make sure that it has the latest package list:</p>
<pre style="color:grey;background:black;margin:5px;padding:5px;">
C:\cabal install cabal-install
C:\cabal update
</pre>
<h3>Leksah</h3>
<p><a href="http://antimatroid.files.wordpress.com/2010/07/leksah.png"><img src="http://antimatroid.files.wordpress.com/2010/07/leksah.png?w=300&#038;h=181" alt="" title="leksah" width="300" height="181" class="alignright size-medium wp-image-1633" /></a></p>
<p>Many developers are probably used to having a quality Integrated Development Environment (IDE) to work with and the Haskell Community&#8217;s answer is <a href="http://leksah.org/" target="blank">Leksah</a>. Leksah is still fairly green and has a ways to go before being comparable to Eclipse or Visual Studio, but nonetheless, Leksah is packed with plenty of goodies that will make for easy development of packages and modules for distribution on Cabal.</p>
<p>It is best to use the installer from the Leksah website. Once you&#8217;ve installed the latest, you&#8217;ll need to run the following from the command-line</p>
<pre style="color:grey;background:black;margin:5px;padding:5px;">
C:\ghc-pkg recache
</pre>
<p>So that the packages on the system (those provided by GHC) will show up when you have the browse pane open.</p>
<h3>Gtk2hs</h3>
<p>If you plan on doing any Graphical User Interfaces (GUIs), then you&#8217;ll want to get the Haskell binding to the <a href="http://www.gtk.org/download-windows.html" target="blank">GTK+</a> library. On the page there should be an &#8220;All-in-one bundle&#8221;- for the purpose of the following, I went with version 2.20.</p>
<p>After extracting the bundle on the machine, make sure that the path you extracted the bundle at along with the child bin directory is added to the PATH environment variable.</p>
<p>Then from the command-line run the following and you should be able to include GTK in your project:</p>
<pre style="color:grey;background:black;margin:5px;padding:5px;">
C:\cabal install gtk
</pre>
<h3>HOpenGL</h3>
<p>I&#8217;ve been working on some basic game programming and I&#8217;ve done some stuff in the past with <a href="http://www.opengl.org/" target="blank">OpenGL</a> so I decided to give the Haskell bindings a try. Windows doesn&#8217;t natively ship with the OpenGL library, so you&#8217;ll need to get it from <a href="http://www.xmission.com/~nate/glut.html" target="blank">here</a>.</p>
<p>Then get the following packages off of Cabal:</p>
<pre style="color:grey;background:black;margin:5px;padding:5px;">
c:\cabal install glut
C:\cabal install opengl
</pre>
<h3>Wrap-up</h3>
<p>I haven&#8217;t done a dry run to test all of the above, so if you follow all of the above and come across a problem, post the solution in the comments. I&#8217;ll continue to update this post as I identify any problems or come across additional &#8220;stuff&#8221; that falls into the must-have category.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/1616/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/1616/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/1616/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/1616/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/antimatroid.wordpress.com/1616/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/antimatroid.wordpress.com/1616/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/antimatroid.wordpress.com/1616/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/antimatroid.wordpress.com/1616/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/1616/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/1616/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/1616/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/1616/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/1616/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/1616/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1616&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2010/08/01/haskell-ecosystem-on-windows-xp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2010/07/leksah.png?w=300" medium="image">
			<media:title type="html">leksah</media:title>
		</media:content>
	</item>
		<item>
		<title>Mathematics Test Practice Questions</title>
		<link>http://antimatroid.wordpress.com/2010/07/01/mathematics-test-practice-questions/</link>
		<comments>http://antimatroid.wordpress.com/2010/07/01/mathematics-test-practice-questions/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 14:00:01 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[GRE]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=1456</guid>
		<description><![CDATA[I&#8217;ve had a fairly busy month, so I haven&#8217;t had a lot of time to work on any projects. I&#8217;ve got a few in the pipeline, but none really mature enough yet to get a write-up. I&#8217;ve been playing with the idea of graduate school for a couple years now and have always felt that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1456&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve had a fairly busy month, so I haven&#8217;t had a lot of time to work on any projects. I&#8217;ve got a few in the pipeline, but none really mature enough yet to get a write-up. I&#8217;ve been playing with the idea of graduate school for a couple years now and have always felt that a strong mathematics background is a good basis for pursuing more interesting (computer science) topics. So, this past week I&#8217;ve been working my way through the <a href="http://www.ets.org/Media/Tests/GRE/pdf/gre_0809_math_practice_book.pdf" target="blank">Mathematics Test Practice Book</a> to see how well I&#8217;d fare. The examination seems fairly aggressive allowing for roughly 2.5 minutes per problem for 66 problems. I&#8217;m a little more methodical in my ways, so I would certainly take more time than what&#8217;s allotted. Nonetheless, I thought I&#8217;d share my take on a handful of  problems from different subject matters that I found of particular interest:</p>
<blockquote><p>
Let <img src='http://s0.wp.com/latex.php?latex=P_%7B1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{1}' title='P_{1}' class='latex' /> be the set of all primes, <img src='http://s0.wp.com/latex.php?latex=%5C%7B2%2C+3%2C+5%2C+7%2C%5Cldots+%5C%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;{2, 3, 5, 7,&#92;ldots &#92;}' title='&#92;{2, 3, 5, 7,&#92;ldots &#92;}' class='latex' />, and for each integer <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n' title='n' class='latex' />, let <img src='http://s0.wp.com/latex.php?latex=P_%7Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{n}' title='P_{n}' class='latex' /> be the set of all prime multiples of n, <img src='http://s0.wp.com/latex.php?latex=%5C%7B2n%2C+3n%2C+5n%2C+7n%2C+%5Cldots+%5C%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;{2n, 3n, 5n, 7n, &#92;ldots &#92;}' title='&#92;{2n, 3n, 5n, 7n, &#92;ldots &#92;}' class='latex' />. Which of the following intersections is nonempty?</p>
<ol type="A">
<li><img src='http://s0.wp.com/latex.php?latex=P_%7B1%7D+%5Ccap+P_%7B23%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{1} &#92;cap P_{23}' title='P_{1} &#92;cap P_{23}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=P_%7B7%7D+%5Ccap+P_%7B21%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{7} &#92;cap P_{21}' title='P_{7} &#92;cap P_{21}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=P_%7B12%7D+%5Ccap+P_%7B20%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{12} &#92;cap P_{20}' title='P_{12} &#92;cap P_{20}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=P_%7B20%7D+%5Ccap+P_%7B24%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{20} &#92;cap P_{24}' title='P_{20} &#92;cap P_{24}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=P_%7B5%7D+%5Ccap+P_%7B25%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{5} &#92;cap P_{25}' title='P_{5} &#92;cap P_{25}' class='latex' /></li>
</ol>
</blockquote>
<p>Given <img src='http://s0.wp.com/latex.php?latex=P_%7Bn%7D+%5Ccap+P_%7Bm%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P_{n} &#92;cap P_{m}' title='P_{n} &#92;cap P_{m}' class='latex' />, we&#8217;ll pick two elements (one from each set) and solve for <img src='http://s0.wp.com/latex.php?latex=n+p_%7B1%7D%5E%7B%28i%29%7D+%3D+m+p_%7B1%7D%5E%7B%28j%29%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n p_{1}^{(i)} = m p_{1}^{(j)}' title='n p_{1}^{(i)} = m p_{1}^{(j)}' class='latex' />. We then assume that <img src='http://s0.wp.com/latex.php?latex=n%2C+m&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n, m' title='n, m' class='latex' /> are of the form <img src='http://s0.wp.com/latex.php?latex=a+p_%7B1%7D%5E%7B%28k%29%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='a p_{1}^{(k)}' title='a p_{1}^{(k)}' class='latex' /> where <img src='http://s0.wp.com/latex.php?latex=a&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='a' title='a' class='latex' /> is a composite. Thus, <img src='http://s0.wp.com/latex.php?latex=a+p_%7B1%7D%5E%7B%28j%29%7D+p_%7B1%7D%5E%7B%28i%29%7D+%3D+a+p_%7B1%7D%5E%7B%28i%29%7D+p_%7B1%7D%5E%7B%28j%29%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='a p_{1}^{(j)} p_{1}^{(i)} = a p_{1}^{(i)} p_{1}^{(j)}' title='a p_{1}^{(j)} p_{1}^{(i)} = a p_{1}^{(i)} p_{1}^{(j)}' class='latex' />, we can conclude that <img src='http://s0.wp.com/latex.php?latex=p_%7B1%7D%5E%7B%28j%29%7Dp_%7B1%7D%5E%7B%28i%29%7D+%5Cin+P_%7Bn%7D+%5Ccap+P_%7Bm%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='p_{1}^{(j)}p_{1}^{(i)} &#92;in P_{n} &#92;cap P_{m}' title='p_{1}^{(j)}p_{1}^{(i)} &#92;in P_{n} &#92;cap P_{m}' class='latex' />. Thus, D is the correct answer.</p>
<hr />
<blockquote><p>
For what value of <img src='http://s0.wp.com/latex.php?latex=b&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='b' title='b' class='latex' /> is the line <img src='http://s0.wp.com/latex.php?latex=g%28x%29+%3D+10x&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='g(x) = 10x' title='g(x) = 10x' class='latex' /> tangent to the curve <img src='http://s0.wp.com/latex.php?latex=h%28x%29+%3D+e%5E%7Bbx%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='h(x) = e^{bx}' title='h(x) = e^{bx}' class='latex' /> at some point in the xy-plane?
</p></blockquote>
<p>Using operator notation, a line tangent to a curve is given by <img src='http://s0.wp.com/latex.php?latex=T_%7Bx_%7B0%7D%7D+f+%3D+%5Cfrac%7Bdf%7D%7Bdx%7D%28x_%7B0%7D%29%28x+-+x_%7B0%7D%29+%2B+f%28x_%7B0%7D%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='T_{x_{0}} f = &#92;frac{df}{dx}(x_{0})(x - x_{0}) + f(x_{0})' title='T_{x_{0}} f = &#92;frac{df}{dx}(x_{0})(x - x_{0}) + f(x_{0})' class='latex' />. To solve for <img src='http://s0.wp.com/latex.php?latex=b&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='b' title='b' class='latex' /> we solve for <img src='http://s0.wp.com/latex.php?latex=T_%7Bx_%7B0%7D%7D+h+%3D+g+%5Cimplies+10x+%3D+be%5E%7Bb+x_0%7D%28x+-+x_0%29+%2B+e%5E%7Bb+x_0%7D+%5Cimplies+10x+%3D+be%5E%7Bb+x_0%7Dx+%2B+e%5E%7Bb+x_0%7D%281+-+bx_0%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='T_{x_{0}} h = g &#92;implies 10x = be^{b x_0}(x - x_0) + e^{b x_0} &#92;implies 10x = be^{b x_0}x + e^{b x_0}(1 - bx_0)' title='T_{x_{0}} h = g &#92;implies 10x = be^{b x_0}(x - x_0) + e^{b x_0} &#92;implies 10x = be^{b x_0}x + e^{b x_0}(1 - bx_0)' class='latex' /></p>
<p>Which yields the following two equations: <img src='http://s0.wp.com/latex.php?latex=10+%3D+be%5E%7Bb+x_0%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='10 = be^{b x_0}' title='10 = be^{b x_0}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=0+%3D+e%5E%7Bb+x_0%7D%281+-+bx_0%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='0 = e^{b x_0}(1 - bx_0)' title='0 = e^{b x_0}(1 - bx_0)' class='latex' /> since <img src='http://s0.wp.com/latex.php?latex=e%5E%7Bb+x_0%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='e^{b x_0}' title='e^{b x_0}' class='latex' /> will never equal zero, we can conclude <img src='http://s0.wp.com/latex.php?latex=0+%3D+1+-+b+x_0+%5Cimplies+1+%3D+bx_0+%5Cimplies+x_0+%3D+%5Cfrac%7B1%7D%7Bb%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='0 = 1 - b x_0 &#92;implies 1 = bx_0 &#92;implies x_0 = &#92;frac{1}{b}' title='0 = 1 - b x_0 &#92;implies 1 = bx_0 &#92;implies x_0 = &#92;frac{1}{b}' class='latex' />. Substituting for <img src='http://s0.wp.com/latex.php?latex=x_%7B0%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x_{0}' title='x_{0}' class='latex' /> yields <img src='http://s0.wp.com/latex.php?latex=10+%3D+b+e%5E%7Bb+%5Cfrac%7B1%7D%7Bb%7D%7D+%5Cimplies+10+%3D+be+%5Cimplies+b+%3D+%5Cfrac%7B10%7D%7Be%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='10 = b e^{b &#92;frac{1}{b}} &#92;implies 10 = be &#92;implies b = &#92;frac{10}{e}' title='10 = b e^{b &#92;frac{1}{b}} &#92;implies 10 = be &#92;implies b = &#92;frac{10}{e}' class='latex' />.</p>
<hr />
<blockquote><p>
Suppose that two binary operations, denoted by <img src='http://s0.wp.com/latex.php?latex=%5Coplus&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;oplus' title='&#92;oplus' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Codot&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;odot' title='&#92;odot' class='latex' />, are defined on a nonempty set <img src='http://s0.wp.com/latex.php?latex=S&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='S' title='S' class='latex' />, and that the following conditions are satisfied for all <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x' title='x' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=y&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='y' title='y' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=z&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='z' title='z' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=S&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='S' title='S' class='latex' />:</p>
<p>(1) (Closure) <img src='http://s0.wp.com/latex.php?latex=x+%5Coplus+y%2C+x+%5Codot+y+%5Cin+S&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x &#92;oplus y, x &#92;odot y &#92;in S' title='x &#92;oplus y, x &#92;odot y &#92;in S' class='latex' /><br />
(2) (Associativity) <img src='http://s0.wp.com/latex.php?latex=x+%5Coplus+%28y+%5Coplus+z%29+%3D+%28x+%5Coplus+y%29+%5Coplus+z&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x &#92;oplus (y &#92;oplus z) = (x &#92;oplus y) &#92;oplus z' title='x &#92;oplus (y &#92;oplus z) = (x &#92;oplus y) &#92;oplus z' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=x+%5Codot+%28y+%5Codot+z%29+%3D+%28x+%5Codot+y%29+%5Codot+z&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x &#92;odot (y &#92;odot z) = (x &#92;odot y) &#92;odot z' title='x &#92;odot (y &#92;odot z) = (x &#92;odot y) &#92;odot z' class='latex' /><br />
(3) (Commutativity) <img src='http://s0.wp.com/latex.php?latex=x+%5Coplus+y+%3D+y+%5Coplus+x&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x &#92;oplus y = y &#92;oplus x' title='x &#92;oplus y = y &#92;oplus x' class='latex' /></p>
<p>Also, for each <img src='http://s0.wp.com/latex.php?latex=x+%5Cin+S&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x &#92;in S' title='x &#92;in S' class='latex' /> and for each positive integer <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n' title='n' class='latex' />, the elements <img src='http://s0.wp.com/latex.php?latex=nx&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='nx' title='nx' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=x%5En&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x^n' title='x^n' class='latex' /> are defined recursively as follows:</p>
<p>(4) (Identity Element) <img src='http://s0.wp.com/latex.php?latex=1x+%3D+x%5E1+%3D+x&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='1x = x^1 = x' title='1x = x^1 = x' class='latex' /><br />
(5) and if <img src='http://s0.wp.com/latex.php?latex=kx&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='kx' title='kx' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=x%5Ek&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x^k' title='x^k' class='latex' /> have been defined, then <img src='http://s0.wp.com/latex.php?latex=%28k+%2B+1%29x+%3D+kx+%5Coplus+x&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='(k + 1)x = kx &#92;oplus x' title='(k + 1)x = kx &#92;oplus x' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=x%5E%7Bk+%2B+1%7D+%3D+x%5Ek+%5Codot+x&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x^{k + 1} = x^k &#92;odot x' title='x^{k + 1} = x^k &#92;odot x' class='latex' /> </p>
<p>Which of the following must be true?</p>
<ol type="A">
<li>I only</li>
<li>II only</li>
<li>III only</li>
<li>II and III only</li>
<li>I, II and III</li>
</ol>
</blockquote>
<p>I.) <img src='http://s0.wp.com/latex.php?latex=%28x+%5Codot+y%29%5En+%3D+x%5En+%5Codot+y%5En&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='(x &#92;odot y)^n = x^n &#92;odot y^n' title='(x &#92;odot y)^n = x^n &#92;odot y^n' class='latex' /> s.t. <img src='http://s0.wp.com/latex.php?latex=x%2C+y+%5Cin+S%2C+n+%5Cin+%5Cmathbb%7BZ%7D%5E%7B%2B%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x, y &#92;in S, n &#92;in &#92;mathbb{Z}^{+}' title='x, y &#92;in S, n &#92;in &#92;mathbb{Z}^{+}' class='latex' /></p>
<p>Assume <img src='http://s0.wp.com/latex.php?latex=x+%5Codot+y+%5Cin+S&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x &#92;odot y &#92;in S' title='x &#92;odot y &#92;in S' class='latex' />, from which we assume <img src='http://s0.wp.com/latex.php?latex=%28x+%5Codot+y%29+%5Codot+%28x+%5Codot+y%29+%5Cin+S&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='(x &#92;odot y) &#92;odot (x &#92;odot y) &#92;in S' title='(x &#92;odot y) &#92;odot (x &#92;odot y) &#92;in S' class='latex' />. If (3) were defined for <img src='http://s0.wp.com/latex.php?latex=%5Codot&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;odot' title='&#92;odot' class='latex' />, we could swap either side then apply (2) reducing it down to the recursive definition (5) and have a proof. Since that isn&#8217;t the case, we can&#8217;t conclude that <img src='http://s0.wp.com/latex.php?latex=%28x+%5Codot+y%29%5En+%3D+x%5En+%5Codot+y%5En&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='(x &#92;odot y)^n = x^n &#92;odot y^n' title='(x &#92;odot y)^n = x^n &#92;odot y^n' class='latex' />.</p>
<p>II.) <img src='http://s0.wp.com/latex.php?latex=n+%28x+%5Coplus+y%29+%3D+nx+%5Coplus+ny&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n (x &#92;oplus y) = nx &#92;oplus ny' title='n (x &#92;oplus y) = nx &#92;oplus ny' class='latex' /> s.t. <img src='http://s0.wp.com/latex.php?latex=x%2Cy+%5Cin+S%2C+n+%5Cin+%5Cmathbb%7BZ%7D%5E%7B%2B%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x,y &#92;in S, n &#92;in &#92;mathbb{Z}^{+}' title='x,y &#92;in S, n &#92;in &#92;mathbb{Z}^{+}' class='latex' /></p>
<p>Given (1) we can construct <img src='http://s0.wp.com/latex.php?latex=%28x+%5Coplus+y%29+%3D+1+%28x+%5Coplus+y%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='(x &#92;oplus y) = 1 (x &#92;oplus y)' title='(x &#92;oplus y) = 1 (x &#92;oplus y)' class='latex' /> by using (4). We can state <img src='http://s0.wp.com/latex.php?latex=2+%28x+%5Coplus+y%29+%3D+1+%28x+%5Coplus+y%29+%5Coplus+%28x+%5Coplus+y%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='2 (x &#92;oplus y) = 1 (x &#92;oplus y) &#92;oplus (x &#92;oplus y)' title='2 (x &#92;oplus y) = 1 (x &#92;oplus y) &#92;oplus (x &#92;oplus y)' class='latex' /> from (5), leading to <img src='http://s0.wp.com/latex.php?latex=2+%28x+%5Coplus+y%29+%3D+%28x+%5Coplus+y%29+%5Coplus+%28y+%5Coplus+x%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='2 (x &#92;oplus y) = (x &#92;oplus y) &#92;oplus (y &#92;oplus x)' title='2 (x &#92;oplus y) = (x &#92;oplus y) &#92;oplus (y &#92;oplus x)' class='latex' /> by way of (3). <img src='http://s0.wp.com/latex.php?latex=2+%28x+%5Coplus+y%29+%3D+x+%5Coplus+%28y+%5Coplus+%28y+%5Coplus+x%29%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='2 (x &#92;oplus y) = x &#92;oplus (y &#92;oplus (y &#92;oplus x))' title='2 (x &#92;oplus y) = x &#92;oplus (y &#92;oplus (y &#92;oplus x))' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=2+%28x+%5Coplus+y%29+%3D+x+%5Coplus+%28%28y+%5Coplus+y%29+%5Coplus+x%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='2 (x &#92;oplus y) = x &#92;oplus ((y &#92;oplus y) &#92;oplus x)' title='2 (x &#92;oplus y) = x &#92;oplus ((y &#92;oplus y) &#92;oplus x)' class='latex' /> by (2). From (5) <img src='http://s0.wp.com/latex.php?latex=2+%28x+%5Coplus+y%29+%3D+x+%5Coplus+%282y+%5Coplus+x%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='2 (x &#92;oplus y) = x &#92;oplus (2y &#92;oplus x)' title='2 (x &#92;oplus y) = x &#92;oplus (2y &#92;oplus x)' class='latex' />. Unfolding that set of steps leads to the following: <img src='http://s0.wp.com/latex.php?latex=2+%28x+%5Coplus+y%29+%3D+x+%5Coplus+%28x+%5Coplus+2y%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='2 (x &#92;oplus y) = x &#92;oplus (x &#92;oplus 2y)' title='2 (x &#92;oplus y) = x &#92;oplus (x &#92;oplus 2y)' class='latex' />,  <img src='http://s0.wp.com/latex.php?latex=2+%28x+%5Coplus+y%29+%3D+%28x+%5Coplus+x%29+%5Coplus+2y&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='2 (x &#92;oplus y) = (x &#92;oplus x) &#92;oplus 2y' title='2 (x &#92;oplus y) = (x &#92;oplus x) &#92;oplus 2y' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=2+%28x+%5Coplus+y%29+%3D+2x+%5Coplus+2y&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='2 (x &#92;oplus y) = 2x &#92;oplus 2y' title='2 (x &#92;oplus y) = 2x &#92;oplus 2y' class='latex' />. If we were to continue down this path for <img src='http://s0.wp.com/latex.php?latex=n+%3D+%5C%7B3%2C+4%2C+5%2C%5Cdotsc%5C%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n = &#92;{3, 4, 5,&#92;dotsc&#92;}' title='n = &#92;{3, 4, 5,&#92;dotsc&#92;}' class='latex' /> then we would find that <img src='http://s0.wp.com/latex.php?latex=n+%28x+%5Coplus+y%29+%3D+nx+%5Coplus+ny&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n (x &#92;oplus y) = nx &#92;oplus ny' title='n (x &#92;oplus y) = nx &#92;oplus ny' class='latex' />. </p>
<p>III.) <img src='http://s0.wp.com/latex.php?latex=x%5Em+%5Codot+x%5En+%3D+x%5E%7Bm%2Bn%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x^m &#92;odot x^n = x^{m+n}' title='x^m &#92;odot x^n = x^{m+n}' class='latex' /> s.t. <img src='http://s0.wp.com/latex.php?latex=x+%5Cin+S%2C+m%2Cn+%5Cin+%5Cmathbb%7BZ%7D%5E%7B%2B%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x &#92;in S, m,n &#92;in &#92;mathbb{Z}^{+}' title='x &#92;in S, m,n &#92;in &#92;mathbb{Z}^{+}' class='latex' />.</p>
<p>Starting with (5): <img src='http://s0.wp.com/latex.php?latex=x%5E%7Bm+%2B+1%7D+%3D+x%5Em+%5Codot+x&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x^{m + 1} = x^m &#92;odot x' title='x^{m + 1} = x^m &#92;odot x' class='latex' /> we introduce an additional <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x' title='x' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=x%5E%7Bm+%2B+1%7D+%5Codot+x+%3D+%28x%5Em+%5Codot+x%29+%5Codot+x&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x^{m + 1} &#92;odot x = (x^m &#92;odot x) &#92;odot x' title='x^{m + 1} &#92;odot x = (x^m &#92;odot x) &#92;odot x' class='latex' /> and then apply (2) <img src='http://s0.wp.com/latex.php?latex=x%5E%7Bm+%2B+2%7D%3D+x%5Em+%5Codot+%28x+%5Codot+x%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x^{m + 2}= x^m &#92;odot (x &#92;odot x)' title='x^{m + 2}= x^m &#92;odot (x &#92;odot x)' class='latex' /> which leads to <img src='http://s0.wp.com/latex.php?latex=x%5E%7Bm+%2B+2%7D+%3D+x%5Em+%5Codot+x%5E2&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x^{m + 2} = x^m &#92;odot x^2' title='x^{m + 2} = x^m &#92;odot x^2' class='latex' />. The same set of steps once again leads to <img src='http://s0.wp.com/latex.php?latex=x%5E%7Bm+%2B+2%7D+%5Codot+x+%3D+%28x%5Em+%5Codot+x%5E2%29+%5Codot+x&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x^{m + 2} &#92;odot x = (x^m &#92;odot x^2) &#92;odot x' title='x^{m + 2} &#92;odot x = (x^m &#92;odot x^2) &#92;odot x' class='latex' /> <img src='http://s0.wp.com/latex.php?latex=x%5E%7Bm+%2B+3%7D+%3D+x%5Em+%5Codot+%28x%5E2+%5Codot+x%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x^{m + 3} = x^m &#92;odot (x^2 &#92;odot x)' title='x^{m + 3} = x^m &#92;odot (x^2 &#92;odot x)' class='latex' /> <img src='http://s0.wp.com/latex.php?latex=x%5E%7Bm+%2B+3%7D+%3D+x%5Em+%5Codot+x%5E3&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x^{m + 3} = x^m &#92;odot x^3' title='x^{m + 3} = x^m &#92;odot x^3' class='latex' />. By induction, we can conclude <img src='http://s0.wp.com/latex.php?latex=x%5E%7Bm+%2B+n%7D+%3D+x%5Em+%5Codot+x%5En&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x^{m + n} = x^m &#92;odot x^n' title='x^{m + n} = x^m &#92;odot x^n' class='latex' />.</p>
<hr />
<blockquote><p>
At a banquet, 9 women and 6 men are to be seated in a row of 15 chairs. If the entire seating arrangement is to be chosen at random, what is the probability that all of the men will be seated next to each other in 6 consecutive positions?
</p></blockquote>
<p>I&#8217;m going to use the notation <img src='http://s0.wp.com/latex.php?latex=y_%7BM%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='y_{M}' title='y_{M}' class='latex' /> to denote a number of men (<img src='http://s0.wp.com/latex.php?latex=Y_%7BM%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='Y_{M}' title='Y_{M}' class='latex' /> for the total number of men), <img src='http://s0.wp.com/latex.php?latex=x_%7BW%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x_{W}' title='x_{W}' class='latex' /> to denote a number of women (<img src='http://s0.wp.com/latex.php?latex=X_%7BW%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='X_{W}' title='X_{W}' class='latex' /> for the total) and <img src='http://s0.wp.com/latex.php?latex=S&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='S' title='S' class='latex' /> to denote the number of seats. A sequence of this notation such as <img src='http://s0.wp.com/latex.php?latex=3_%7BW%7D+6_%7BM%7D+6_%7BW%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='3_{W} 6_{M} 6_{W}' title='3_{W} 6_{M} 6_{W}' class='latex' /> indicates a sequence of three women, six men and six women seated amongst the fifteen seats. Given the definition, we are interested in the probability of <img src='http://s0.wp.com/latex.php?latex=P%5B+%5Cbigcup_%7Bi+%3D+0%7D%5E%7BX_%7BW%7D%7D+%7Bi_%7BW%7DY_%7BM%7D%28X_%7BW%7D+-+i%29_%7BW%7D%7D%5D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='P[ &#92;bigcup_{i = 0}^{X_{W}} {i_{W}Y_{M}(X_{W} - i)_{W}}]' title='P[ &#92;bigcup_{i = 0}^{X_{W}} {i_{W}Y_{M}(X_{W} - i)_{W}}]' class='latex' />. Since there are a fixed number of seats and no two people can occupy the same seat, we know that the number of permutations of <img src='http://s0.wp.com/latex.php?latex=S+%3D+S%21&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='S = S!' title='S = S!' class='latex' /> is the total number of possible arrangements (our sample space). There are <img src='http://s0.wp.com/latex.php?latex=Y_%7BM%7D%21&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='Y_{M}!' title='Y_{M}!' class='latex' /> ways in which the men can be arranged and there are <img src='http://s0.wp.com/latex.php?latex=X_%7BW%7D%21&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='X_{W}!' title='X_{W}!' class='latex' /> ways in which the women can be arranged in the remaining seats. Since there are <img src='http://s0.wp.com/latex.php?latex=X_%7BW%7D+%2B+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='X_{W} + 1' title='X_{W} + 1' class='latex' /> ways to &#8220;slide&#8221; the group of men along the seats we can conclude that:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+P%5B+%5Cbigcup_%7Bi+%3D+0%7D%5E%7BX_%7BW%7D%7D+%7Bi_%7BW%7DY_%7BM%7D%28X_%7BW%7D+-+i%29_%7BW%7D%7D%5D+%3D+%5Cfrac%7B%28X_%7BW%7D%2B1%29+X_%7BW%7D%21+Y_%7BM%7D%21%7D%7BS%21%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle P[ &#92;bigcup_{i = 0}^{X_{W}} {i_{W}Y_{M}(X_{W} - i)_{W}}] = &#92;frac{(X_{W}+1) X_{W}! Y_{M}!}{S!}' title='&#92;displaystyle P[ &#92;bigcup_{i = 0}^{X_{W}} {i_{W}Y_{M}(X_{W} - i)_{W}}] = &#92;frac{(X_{W}+1) X_{W}! Y_{M}!}{S!}' class='latex' /></p>
<p>Given the question, the solution is </p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+P%5B+0_%7BW%7D6_%7BM%7D9_W+%5Ccup+1_%7BW%7D6_%7BM%7D8_W+%5Ccup+2_%7BW%7D6_%7BM%7D7_W+%5Ccdot+9_%7BW%7D6_%7BM%7D0_W%5D+%3D+%5Cfrac%7B10+%2A+9%21+6%21%7D%7B15%21%7D+%3D+%5Cfrac%7B10%21+6%21%7D%7B15%21%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle P[ 0_{W}6_{M}9_W &#92;cup 1_{W}6_{M}8_W &#92;cup 2_{W}6_{M}7_W &#92;cdot 9_{W}6_{M}0_W] = &#92;frac{10 * 9! 6!}{15!} = &#92;frac{10! 6!}{15!}' title='&#92;displaystyle P[ 0_{W}6_{M}9_W &#92;cup 1_{W}6_{M}8_W &#92;cup 2_{W}6_{M}7_W &#92;cdot 9_{W}6_{M}0_W] = &#92;frac{10 * 9! 6!}{15!} = &#92;frac{10! 6!}{15!}' class='latex' /></p>
<hr />
<blockquote><p>
If <img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+z+%3D+e%5E%7B%5Cfrac%7B2+%5Cpi+i%7D%7B5%7D%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle z = e^{&#92;frac{2 &#92;pi i}{5}}' title='&#92;displaystyle z = e^{&#92;frac{2 &#92;pi i}{5}}' class='latex' />, then <img src='http://s0.wp.com/latex.php?latex=w+%3D+1+%2B+z+%2B+z%5E2+%2B+z%5E3+%2B+5z%5E4+%2B+4z%5E5+%2B+4z%5E6+%2B+4z%5E7+%2B+4z%5E8+%2B+5z%5E9+%3D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='w = 1 + z + z^2 + z^3 + 5z^4 + 4z^5 + 4z^6 + 4z^7 + 4z^8 + 5z^9 =' title='w = 1 + z + z^2 + z^3 + 5z^4 + 4z^5 + 4z^6 + 4z^7 + 4z^8 + 5z^9 =' class='latex' />?</p>
<ol type="A">
<li><img src='http://s0.wp.com/latex.php?latex=0&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='0' title='0' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=4+e%5E%7B%5Cfrac%7B3+%5Cpi+i%7D%7B5%7D%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='4 e^{&#92;frac{3 &#92;pi i}{5}}' title='4 e^{&#92;frac{3 &#92;pi i}{5}}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=5+e%5E%7B%5Cfrac%7B4+%5Cpi+i%7D%7B5%7D%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='5 e^{&#92;frac{4 &#92;pi i}{5}}' title='5 e^{&#92;frac{4 &#92;pi i}{5}}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=-4+e%5E%7B%5Cfrac%7B2+%5Cpi+i%7D%7B5%7D%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='-4 e^{&#92;frac{2 &#92;pi i}{5}}' title='-4 e^{&#92;frac{2 &#92;pi i}{5}}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=-5+e%5E%7B%5Cfrac%7B3+%5Cpi+i%7D%7B5%7D%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='-5 e^{&#92;frac{3 &#92;pi i}{5}}' title='-5 e^{&#92;frac{3 &#92;pi i}{5}}' class='latex' /></li>
</ol>
</blockquote>
<p>First thing to observe is that <img src='http://s0.wp.com/latex.php?latex=z&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='z' title='z' class='latex' /> is the 5&#8242;th root of unity which carries with it two properties: </p>
<ol>
<li><img src='http://s0.wp.com/latex.php?latex=z%5E5+%3D+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='z^5 = 1' title='z^5 = 1' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csum_%7Bk+%3D+0%7D%5E%7B4%7D+z%5Ek+%3D+0&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle &#92;sum_{k = 0}^{4} z^k = 0' title='&#92;displaystyle &#92;sum_{k = 0}^{4} z^k = 0' class='latex' /></li>
</ol>
<p>Proof of property (1) is trivial, proof of property (2) is basic proof of the geometric series:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csum_%7Bk+%3D+0%7D%5E%7Bn%7D+z%5Ek+%3D+a+%5Cimplies+%5Csum_%7Bk+%3D+1%7D%5E%7Bn%2B1%7D+z%5Ek+%3D+z+a+%5Cimplies%5Cdisplaystyle+%5Csum_%7Bk+%3D+1%7D%5E%7Bn%2B1%7D%7Bz%5Ek%7D+-+%5Csum_%7Bk+%3D+0%7D%5E%7Bn%7D%7Bz%5Ek%7D+%3D+z+a+-+a+%5Cimplies&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle &#92;sum_{k = 0}^{n} z^k = a &#92;implies &#92;sum_{k = 1}^{n+1} z^k = z a &#92;implies&#92;displaystyle &#92;sum_{k = 1}^{n+1}{z^k} - &#92;sum_{k = 0}^{n}{z^k} = z a - a &#92;implies' title='&#92;displaystyle &#92;sum_{k = 0}^{n} z^k = a &#92;implies &#92;sum_{k = 1}^{n+1} z^k = z a &#92;implies&#92;displaystyle &#92;sum_{k = 1}^{n+1}{z^k} - &#92;sum_{k = 0}^{n}{z^k} = z a - a &#92;implies' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+z%5E%7Bn%2B1%7D+-+1+%3D+a+%28z+-+1%29+%5Cimplies+a+%3D+%5Cfrac%7Bz%5E%7Bn%2B1%7D+-+1%7D%7Bz+-+1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle z^{n+1} - 1 = a (z - 1) &#92;implies a = &#92;frac{z^{n+1} - 1}{z - 1}' title='&#92;displaystyle z^{n+1} - 1 = a (z - 1) &#92;implies a = &#92;frac{z^{n+1} - 1}{z - 1}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Ctherefore+%5Csum_%7Bk+%3D+0%7D%5E%7Bn%7D+z%5Ek+%3D+%5Cfrac%7Bz%5E%7Bn%2B1%7D+-+1%7D%7Bz+-+1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle &#92;therefore &#92;sum_{k = 0}^{n} z^k = &#92;frac{z^{n+1} - 1}{z - 1}' title='&#92;displaystyle &#92;therefore &#92;sum_{k = 0}^{n} z^k = &#92;frac{z^{n+1} - 1}{z - 1}' class='latex' /></p>
<p>Given these properties, we begin to evaluate <img src='http://s0.wp.com/latex.php?latex=w&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='w' title='w' class='latex' /> by separating the terms into reducible portions:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+w+%3D+%281+%2B+z+%2B+z%5E2+%2B+z%5E3+%2B+z%5E4%29+%2B+4z%5E4%281+%2B+z+%2B+z%5E2+%2B+z%5E3+%2B+z%5E4%29+%2B+5%28z%5E4+z%5E5%29+%5Cimplies&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle w = (1 + z + z^2 + z^3 + z^4) + 4z^4(1 + z + z^2 + z^3 + z^4) + 5(z^4 z^5) &#92;implies' title='&#92;displaystyle w = (1 + z + z^2 + z^3 + z^4) + 4z^4(1 + z + z^2 + z^3 + z^4) + 5(z^4 z^5) &#92;implies' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+w+%3D+%281+%2B+4z%5E4%29%5Csum_%7Bk+%3D+0%7D%5E%7B4%7D%7Bz%5Ek%7D+%2B+5%28z%5E4+1%29+%5Cimplies+w+%3D+5z%5E4+%5Cimplies+w+%3D+5+e%5E%7B%5Cfrac%7B8+%5Cpi+i%7D%7B5%7D%7D+%5Cimplies&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle w = (1 + 4z^4)&#92;sum_{k = 0}^{4}{z^k} + 5(z^4 1) &#92;implies w = 5z^4 &#92;implies w = 5 e^{&#92;frac{8 &#92;pi i}{5}} &#92;implies' title='&#92;displaystyle w = (1 + 4z^4)&#92;sum_{k = 0}^{4}{z^k} + 5(z^4 1) &#92;implies w = 5z^4 &#92;implies w = 5 e^{&#92;frac{8 &#92;pi i}{5}} &#92;implies' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+w+%3D+5+e%5E%7B%5Cpi+i%7D+e%5E%7B%5Cfrac%7B3+%5Cpi+i%7D%7B5%7D%7D+%5Cimplies+w+%3D+-5+e%5E%7B%5Cfrac%7B3+%5Cpi+i%7D%7B5%7D%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle w = 5 e^{&#92;pi i} e^{&#92;frac{3 &#92;pi i}{5}} &#92;implies w = -5 e^{&#92;frac{3 &#92;pi i}{5}}' title='&#92;displaystyle w = 5 e^{&#92;pi i} e^{&#92;frac{3 &#92;pi i}{5}} &#92;implies w = -5 e^{&#92;frac{3 &#92;pi i}{5}}' class='latex' /></p>
<hr />
<blockquote><p>
If <img src='http://s0.wp.com/latex.php?latex=%5Clfloor%7Bx%7D%5Crfloor&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;lfloor{x}&#92;rfloor' title='&#92;lfloor{x}&#92;rfloor' class='latex' /> denotes the greatest integer not exceeding x, then <img src='http://s0.wp.com/latex.php?latex=%5Cint_%7B0%7D%5E%7B%5Cinfty%7D%7B%5Clfloor%7Bx%7D%5Crfloor+e%5E%7B-x%7D+dx%7D+%3D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;int_{0}^{&#92;infty}{&#92;lfloor{x}&#92;rfloor e^{-x} dx} =' title='&#92;int_{0}^{&#92;infty}{&#92;lfloor{x}&#92;rfloor e^{-x} dx} =' class='latex' /></p>
<ol type="A">
<li><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7Be%7D%7Be%5E2+-+1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;frac{e}{e^2 - 1}' title='&#92;frac{e}{e^2 - 1}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7Be+-+1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;frac{1}{e - 1}' title='&#92;frac{1}{e - 1}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7Be+-+1%7D%7Be%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;frac{e - 1}{e}' title='&#92;frac{e - 1}{e}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='1' title='1' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=%2B%5Cinfty&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='+&#92;infty' title='+&#92;infty' class='latex' /></li>
</ol>
</blockquote>
<p>When <img src='http://s0.wp.com/latex.php?latex=x+%5Cin+%5Cmathbb%7BZ%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x &#92;in &#92;mathbb{Z}' title='x &#92;in &#92;mathbb{Z}' class='latex' />, we have a discontinuity in the function that we are integrating because of the floor function. To resolve this issue, we need to break up the integral into a sum of integrals as follows: <img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cint_%7B0%7D%5E%7B%5Cinfty%7D%7B%5Clfloor%7Bx%7D%5Crfloor+e%5E%7B-x%7D+dx%7D+%3D+%5Csum_%7Bn+%3D+0%7D%5E%7B%5Cinfty%7D+n+%5Cint_%7Bn%7D%5E%7Bn%2B1%7D%7Be%5E%7B-x%7D+dx%7D+%3D+%5Csum_%7Bn+%3D+0%7D%5E%7B%5Cinfty%7D+n+%28-e%5E%7B-x%7D%29%5Crvert_%7Bn%7D%5E%7Bn%2B1%7D+%3D+&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle &#92;int_{0}^{&#92;infty}{&#92;lfloor{x}&#92;rfloor e^{-x} dx} = &#92;sum_{n = 0}^{&#92;infty} n &#92;int_{n}^{n+1}{e^{-x} dx} = &#92;sum_{n = 0}^{&#92;infty} n (-e^{-x})&#92;rvert_{n}^{n+1} = ' title='&#92;displaystyle &#92;int_{0}^{&#92;infty}{&#92;lfloor{x}&#92;rfloor e^{-x} dx} = &#92;sum_{n = 0}^{&#92;infty} n &#92;int_{n}^{n+1}{e^{-x} dx} = &#92;sum_{n = 0}^{&#92;infty} n (-e^{-x})&#92;rvert_{n}^{n+1} = ' class='latex' /> <img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csum_%7Bn+%3D+0%7D%5E%7B%5Cinfty%7D+n+%28-e%5E%7B-%28n%2B1%29%7D+%2B+e%5E%7B-n%7D%29+%3D+%5Csum_%7Bn+%3D+0%7D%5E%7B%5Cinfty%7D+n+%5Cleft%28+%5Cfrac%7B1%7D%7Be%5En%7D+-+%5Cfrac%7B1%7D%7Be+e%5En%7D+%5Cright%29+%3D+%5Cleft%28+1+-+%5Cfrac%7B1%7D%7Be%7D+%5Cright%29+%5Csum_%7Bn+%3D+0%7D%5E%7B%5Cinfty%7D+n+%5Cfrac%7B1%7D%7Be%7D%5En&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle &#92;sum_{n = 0}^{&#92;infty} n (-e^{-(n+1)} + e^{-n}) = &#92;sum_{n = 0}^{&#92;infty} n &#92;left( &#92;frac{1}{e^n} - &#92;frac{1}{e e^n} &#92;right) = &#92;left( 1 - &#92;frac{1}{e} &#92;right) &#92;sum_{n = 0}^{&#92;infty} n &#92;frac{1}{e}^n' title='&#92;displaystyle &#92;sum_{n = 0}^{&#92;infty} n (-e^{-(n+1)} + e^{-n}) = &#92;sum_{n = 0}^{&#92;infty} n &#92;left( &#92;frac{1}{e^n} - &#92;frac{1}{e e^n} &#92;right) = &#92;left( 1 - &#92;frac{1}{e} &#92;right) &#92;sum_{n = 0}^{&#92;infty} n &#92;frac{1}{e}^n' class='latex' />. </p>
<p>Now we are left with a series that looks similar to a geometric series, so we evaluate it using a similar proof, then return to evaluating the integral.</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csum_%7Bn+%3D+0%7D%5E%7Bm%7D+n+x%5En+%3D+a+%5Cimplies+x+%5Csum_%7Bn+%3D+0%7D%5E%7Bm%7D+n+x%5En+%3D+x+a+%5Cimplies+%5Csum_%7Bn+%3D+0%7D%5E%7Bm%7D+n+x%5E%7Bn%2B1%7D+%3D+x+a+%5Cimplies+&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle &#92;sum_{n = 0}^{m} n x^n = a &#92;implies x &#92;sum_{n = 0}^{m} n x^n = x a &#92;implies &#92;sum_{n = 0}^{m} n x^{n+1} = x a &#92;implies ' title='&#92;displaystyle &#92;sum_{n = 0}^{m} n x^n = a &#92;implies x &#92;sum_{n = 0}^{m} n x^n = x a &#92;implies &#92;sum_{n = 0}^{m} n x^{n+1} = x a &#92;implies ' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csum_%7Bn+%3D+0%7D%5E%7Bm%7D+n+x%5E%7Bn%2B1%7D+-+%5Csum_%7Bn+%3D+0%7D%5E%7Bm%7D+n+x%5En+%3D+%28x+-+1%29+a+%5Cimplies+-+%5Csum_%7Bn+%3D+1%7D%5E%7Bm%7D%7Bx%5En%7D+%2B+m+x%5E%7Bm%2B1%7D+%3D+a%28x-1%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle &#92;sum_{n = 0}^{m} n x^{n+1} - &#92;sum_{n = 0}^{m} n x^n = (x - 1) a &#92;implies - &#92;sum_{n = 1}^{m}{x^n} + m x^{m+1} = a(x-1)' title='&#92;displaystyle &#92;sum_{n = 0}^{m} n x^{n+1} - &#92;sum_{n = 0}^{m} n x^n = (x - 1) a &#92;implies - &#92;sum_{n = 1}^{m}{x^n} + m x^{m+1} = a(x-1)' class='latex' />. </p>
<p>(This last step can be produced by expanding the series.) We see that we have a geometric series (without the first term) which was proved earlier. From these facts we can conclude:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+a+%3D+%5Cfrac%7B-+%28%5Cfrac%7Bx%5En+-+1%7D%7Bx+-+1%7D+-+1%29+%2B+m+x%5E%7Bm%2B1%7D%7D%7Bx-1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle a = &#92;frac{- (&#92;frac{x^n - 1}{x - 1} - 1) + m x^{m+1}}{x-1}' title='&#92;displaystyle a = &#92;frac{- (&#92;frac{x^n - 1}{x - 1} - 1) + m x^{m+1}}{x-1}' class='latex' />. </p>
<p>As we take the limit of <img src='http://s0.wp.com/latex.php?latex=m+%5Cto+%5Cinfty&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='m &#92;to &#92;infty' title='m &#92;to &#92;infty' class='latex' /> of this expression (assuming <img src='http://s0.wp.com/latex.php?latex=x+%3C+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='x &lt; 1' title='x &lt; 1' class='latex' /> for convergence) we find:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+a+%3D+%5Cfrac%7B-+%28%5Cfrac%7B-+1%7D%7Bx+-+1%7D+-+1%29%7D%7Bx-1%7D+%5Cimplies+a+%3D+%5Cfrac%7B%5Cfrac%7B1%7D%7Bx+-+1%7D%2B1%7D%7Bx-1%7D+%5Cimplies+a+%3D+%5Cfrac%7B%5Cfrac%7Bx%7D%7Bx+-+1%7D%7D%7Bx-1%7D+%5Cimplies+a+%3D+%5Cfrac%7Bx%7D%7B%28x-1%29%5E2%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle a = &#92;frac{- (&#92;frac{- 1}{x - 1} - 1)}{x-1} &#92;implies a = &#92;frac{&#92;frac{1}{x - 1}+1}{x-1} &#92;implies a = &#92;frac{&#92;frac{x}{x - 1}}{x-1} &#92;implies a = &#92;frac{x}{(x-1)^2}' title='&#92;displaystyle a = &#92;frac{- (&#92;frac{- 1}{x - 1} - 1)}{x-1} &#92;implies a = &#92;frac{&#92;frac{1}{x - 1}+1}{x-1} &#92;implies a = &#92;frac{&#92;frac{x}{x - 1}}{x-1} &#92;implies a = &#92;frac{x}{(x-1)^2}' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Ctherefore+%5Csum_%7Bn+%3D+0%7D%5E%7B%5Cinfty%7D+n+x%5En+%3D+%5Cfrac%7Bx%7D%7B%28x-1%29%5E2%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle &#92;therefore &#92;sum_{n = 0}^{&#92;infty} n x^n = &#92;frac{x}{(x-1)^2}' title='&#92;displaystyle &#92;therefore &#92;sum_{n = 0}^{&#92;infty} n x^n = &#92;frac{x}{(x-1)^2}' class='latex' />.</p>
<p>Returning to evaluating the integral, we now find:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cint_%7B0%7D%5E%7B%5Cinfty%7D%7B%5Clfloor%7Bx%7D%5Crfloor+e%5E%7B-x%7D+dx%7D+%3D+%5Cfrac%7Be+-+1%7D%7Be%7D+%5Cfrac%7B%5Cfrac%7B1%7D%7Be%7D%7D%7B%28%5Cfrac%7B1%7D%7Be%7D-1%29%5E2%7D+%3D+%5Cfrac%7Be+-+1%7D%7Be%7D+%5Cfrac%7Be%7D%7B%281-e%29%5E2%7D+%3D+%5Cfrac%7Be+-+1%7D%7B%28-1%29%5E2%28e-1%29%5E2%7D+%3D+%5Cfrac%7B1%7D%7Be-1%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle &#92;int_{0}^{&#92;infty}{&#92;lfloor{x}&#92;rfloor e^{-x} dx} = &#92;frac{e - 1}{e} &#92;frac{&#92;frac{1}{e}}{(&#92;frac{1}{e}-1)^2} = &#92;frac{e - 1}{e} &#92;frac{e}{(1-e)^2} = &#92;frac{e - 1}{(-1)^2(e-1)^2} = &#92;frac{1}{e-1}' title='&#92;displaystyle &#92;int_{0}^{&#92;infty}{&#92;lfloor{x}&#92;rfloor e^{-x} dx} = &#92;frac{e - 1}{e} &#92;frac{&#92;frac{1}{e}}{(&#92;frac{1}{e}-1)^2} = &#92;frac{e - 1}{e} &#92;frac{e}{(1-e)^2} = &#92;frac{e - 1}{(-1)^2(e-1)^2} = &#92;frac{1}{e-1}' class='latex' />.</p>
<hr />
<blockquote><p>
<img src="http://antimatroid.files.wordpress.com/2010/06/inscription.png?w=700" alt="" title="inscription"   class="aligncenter size-full wp-image-1532" /></p>
<p>The four shaded circles in Figure 1 above are congruent and each is tangent to the large circle and to two of the other shaded circles. Figure 2 is the result of replacing each of the shaded circles in Figure 1 by a figure that is geometrically similar to Figure 1. What is the ratio of the area of the shaded portion of Figure 2 to the area of the shaded portion of Figure 1?</p>
<ol type="A">
<li><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2+%5Csqrt%7B2%7D%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;frac{1}{2 &#92;sqrt{2}}' title='&#92;frac{1}{2 &#92;sqrt{2}}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B1+%2B+%5Csqrt%7B2%7D%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;frac{1}{1 + &#92;sqrt{2}}' title='&#92;frac{1}{1 + &#92;sqrt{2}}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B4%7D%7B1+%2B+%5Csqrt%7B2%7D%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;frac{4}{1 + &#92;sqrt{2}}' title='&#92;frac{4}{1 + &#92;sqrt{2}}' class='latex' /></li>
<li><img src='http://s0.wp.com/latex.php?latex=%5Cleft%28+%5Cfrac%7B%5Csqrt%7B2%7D%7D%7B1%2B%5Csqrt%7B2%7D%7D+%5Cright%29%5E2&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;left( &#92;frac{&#92;sqrt{2}}{1+&#92;sqrt{2}} &#92;right)^2' title='&#92;left( &#92;frac{&#92;sqrt{2}}{1+&#92;sqrt{2}} &#92;right)^2' class='latex' />
<li><img src='http://s0.wp.com/latex.php?latex=%5Cleft%28+%5Cfrac%7B2%7D%7B1%2B%5Csqrt%7B2%7D%7D+%5Cright%29%5E2&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;left( &#92;frac{2}{1+&#92;sqrt{2}} &#92;right)^2' title='&#92;left( &#92;frac{2}{1+&#92;sqrt{2}} &#92;right)^2' class='latex' />
</ol>
</blockquote>
<p><a href="http://antimatroid.files.wordpress.com/2010/06/inscription-proof.png"><img src="http://antimatroid.files.wordpress.com/2010/06/inscription-proof.png?w=300&#038;h=300" alt="" title="inscription-proof" width="300" height="300" class="alignright size-medium wp-image-1535" /></a></p>
<p>The first goal is to construct one of the shaded circles in Figure 1. Once the circle has been constructed, we know that the scaling factor of Figure 1 onto Figure 2 will be the radius of the shaded circle (assuming the bounding circle is the unit circle).</p>
<p>Starting with the unit circle, <img src='http://s0.wp.com/latex.php?latex=U&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='U' title='U' class='latex' />, centered at the origin, <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='A' title='A' class='latex' /> select points <img src='http://s0.wp.com/latex.php?latex=B+%3D+%281%2C+0%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='B = (1, 0)' title='B = (1, 0)' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=C+%3D+%280%2C+1%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='C = (0, 1)' title='C = (0, 1)' class='latex' /> on <img src='http://s0.wp.com/latex.php?latex=U&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='U' title='U' class='latex' />. Construct the midpoint <img src='http://s0.wp.com/latex.php?latex=D+%3D+%28%5Cfrac%7B1%7D%7B2%7D%2C+%5Cfrac%7B1%7D%7B2%7D%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='D = (&#92;frac{1}{2}, &#92;frac{1}{2})' title='D = (&#92;frac{1}{2}, &#92;frac{1}{2})' class='latex' /> to construct the line <img src='http://s0.wp.com/latex.php?latex=%5Coverleftarrow%7BA%7D%5Coverrightarrow%7BD%7D+%3D+x&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;overleftarrow{A}&#92;overrightarrow{D} = x' title='&#92;overleftarrow{A}&#92;overrightarrow{D} = x' class='latex' />. The intersection of <img src='http://s0.wp.com/latex.php?latex=%5Coverleftarrow%7BA%7D%5Coverrightarrow%7BD%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;overleftarrow{A}&#92;overrightarrow{D}' title='&#92;overleftarrow{A}&#92;overrightarrow{D}' class='latex' /> with <img src='http://s0.wp.com/latex.php?latex=U&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='U' title='U' class='latex' /> leads to the point <img src='http://s0.wp.com/latex.php?latex=E+%3D+%28%5Cfrac%7B%5Csqrt%7B2%7D%7D%7B2%7D%2C%5Cfrac%7B%5Csqrt%7B2%7D%7D%7B2%7D%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='E = (&#92;frac{&#92;sqrt{2}}{2},&#92;frac{&#92;sqrt{2}}{2})' title='E = (&#92;frac{&#92;sqrt{2}}{2},&#92;frac{&#92;sqrt{2}}{2})' class='latex' />. Let <img src='http://s0.wp.com/latex.php?latex=%5Coverleftarrow%7BF%7D%5Coverrightarrow%7BG%7D+%3D+-x+%2B+%5Csqrt%7B2%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;overleftarrow{F}&#92;overrightarrow{G} = -x + &#92;sqrt{2}' title='&#92;overleftarrow{F}&#92;overrightarrow{G} = -x + &#92;sqrt{2}' class='latex' /> be perpendicular to <img src='http://s0.wp.com/latex.php?latex=%5Coverleftarrow%7BA%7D%5Coverrightarrow%7BD%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;overleftarrow{A}&#92;overrightarrow{D}' title='&#92;overleftarrow{A}&#92;overrightarrow{D}' class='latex' /> at <img src='http://s0.wp.com/latex.php?latex=E&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='E' title='E' class='latex' />. Points <img src='http://s0.wp.com/latex.php?latex=F+%3D+%280%2C+%5Csqrt%7B2%7D%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='F = (0, &#92;sqrt{2})' title='F = (0, &#92;sqrt{2})' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=G+%28%5Csqrt%7B2%7D%2C+0%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='G (&#92;sqrt{2}, 0)' title='G (&#92;sqrt{2}, 0)' class='latex' />. We can now construct two angle bisectors: <img src='http://s0.wp.com/latex.php?latex=%5Coverleftarrow%7BF%7D%5Coverrightarrow%7BJ%7D+%3D+-%281%2B%5Csqrt%7B2%7D%29x+%2B+%5Csqrt%7B2%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;overleftarrow{F}&#92;overrightarrow{J} = -(1+&#92;sqrt{2})x + &#92;sqrt{2}' title='&#92;overleftarrow{F}&#92;overrightarrow{J} = -(1+&#92;sqrt{2})x + &#92;sqrt{2}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Coverleftarrow%7BI%7D%5Coverrightarrow%7BG%7D+%3D+%281+-+%5Csqrt%7B2%7D%29x+%2B+2+-+%5Csqrt%7B2%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;overleftarrow{I}&#92;overrightarrow{G} = (1 - &#92;sqrt{2})x + 2 - &#92;sqrt{2}' title='&#92;overleftarrow{I}&#92;overrightarrow{G} = (1 - &#92;sqrt{2})x + 2 - &#92;sqrt{2}' class='latex' />. The intersection of these two lines is <img src='http://s0.wp.com/latex.php?latex=H+%3D+%28+%5Csqrt%7B2%7D+-+1%2C+%5Csqrt%7B2%7D+-+1%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='H = ( &#92;sqrt{2} - 1, &#92;sqrt{2} - 1)' title='H = ( &#92;sqrt{2} - 1, &#92;sqrt{2} - 1)' class='latex' />. Which implies the inscribed circle <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BI%7D+%3D+%28x+-+%5Csqrt%7B2%7D+%2B+1%29%5E2+%2B+%28y+-+%5Csqrt%7B2%7D+%2B+1%29%5E2+%3D+%28%5Csqrt%7B2%7D+-+1%29%5E2&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{I} = (x - &#92;sqrt{2} + 1)^2 + (y - &#92;sqrt{2} + 1)^2 = (&#92;sqrt{2} - 1)^2' title='&#92;mathcal{I} = (x - &#92;sqrt{2} + 1)^2 + (y - &#92;sqrt{2} + 1)^2 = (&#92;sqrt{2} - 1)^2' class='latex' />.</p>
<p>Thus, the area of <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BI%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;mathcal{I}' title='&#92;mathcal{I}' class='latex' /> is <img src='http://s0.wp.com/latex.php?latex=%5Cpi+%28%5Csqrt%7B2%7D+-+1%29%5E2&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;pi (&#92;sqrt{2} - 1)^2' title='&#92;pi (&#92;sqrt{2} - 1)^2' class='latex' /> and the total shaded area in Figure 1 is <img src='http://s0.wp.com/latex.php?latex=4+%5Cpi+%28%5Csqrt%7B2%7D+-+1%29%5E2&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='4 &#92;pi (&#92;sqrt{2} - 1)^2' title='4 &#92;pi (&#92;sqrt{2} - 1)^2' class='latex' />. To figure out the shaded area of Figure 2, we&#8217;ll scale Figure 1 down by a factor of <img src='http://s0.wp.com/latex.php?latex=%5Csqrt%7B2%7D+-+1&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;sqrt{2} - 1' title='&#92;sqrt{2} - 1' class='latex' /> and multiply by 4 yielding <img src='http://s0.wp.com/latex.php?latex=16+%5Cpi+%28%5Csqrt%7B2%7D+-+1%29%5E4&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='16 &#92;pi (&#92;sqrt{2} - 1)^4' title='16 &#92;pi (&#92;sqrt{2} - 1)^4' class='latex' />. These two areas give a ratio of: <img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7B16+%5Cpi+%28%5Csqrt%7B2%7D+-+1%29%5E4%7D%7B4+%5Cpi+%28%5Csqrt%7B2%7D+-+1%29%5E2%7D+%3D+%5Cfrac%7B4+%28%5Csqrt%7B2%7D+-+1%29%5E2%7D%7B1%7D+%5Cfrac%7B%28%5Csqrt%7B2%7D%2B1%29%5E2%7D%7B%28%5Csqrt%7B2%7D%2B1%29%5E2%7D+%3D+%5Cfrac%7B%282%28%5Csqrt%7B2%7D+-+1%29%28%5Csqrt%7B2%7D%2B1%29%29%5E2%7D%7B%28%5Csqrt%7B2%7D%2B1%29%5E2%7D+%3D+%5Cleft%28+%5Cfrac%7B2%7D%7B%5Csqrt%7B2%7D%2B1%7D+%5Cright%29%5E2&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle &#92;frac{16 &#92;pi (&#92;sqrt{2} - 1)^4}{4 &#92;pi (&#92;sqrt{2} - 1)^2} = &#92;frac{4 (&#92;sqrt{2} - 1)^2}{1} &#92;frac{(&#92;sqrt{2}+1)^2}{(&#92;sqrt{2}+1)^2} = &#92;frac{(2(&#92;sqrt{2} - 1)(&#92;sqrt{2}+1))^2}{(&#92;sqrt{2}+1)^2} = &#92;left( &#92;frac{2}{&#92;sqrt{2}+1} &#92;right)^2' title='&#92;displaystyle &#92;frac{16 &#92;pi (&#92;sqrt{2} - 1)^4}{4 &#92;pi (&#92;sqrt{2} - 1)^2} = &#92;frac{4 (&#92;sqrt{2} - 1)^2}{1} &#92;frac{(&#92;sqrt{2}+1)^2}{(&#92;sqrt{2}+1)^2} = &#92;frac{(2(&#92;sqrt{2} - 1)(&#92;sqrt{2}+1))^2}{(&#92;sqrt{2}+1)^2} = &#92;left( &#92;frac{2}{&#92;sqrt{2}+1} &#92;right)^2' class='latex' />.</p>
<p>(Images in this section were generated by <a href="http://www.geogebra.org" target="blank">GeoGebra</a>.)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/1456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/1456/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/1456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/1456/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/antimatroid.wordpress.com/1456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/antimatroid.wordpress.com/1456/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/antimatroid.wordpress.com/1456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/antimatroid.wordpress.com/1456/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/1456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/1456/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/1456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/1456/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/1456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/1456/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1456&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2010/07/01/mathematics-test-practice-questions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2010/06/inscription.png" medium="image">
			<media:title type="html">inscription</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2010/06/inscription-proof.png?w=300" medium="image">
			<media:title type="html">inscription-proof</media:title>
		</media:content>
	</item>
		<item>
		<title>An Experiment in Optical Character Recognition</title>
		<link>http://antimatroid.wordpress.com/2010/06/01/an-experiment-in-optical-character-recognition/</link>
		<comments>http://antimatroid.wordpress.com/2010/06/01/an-experiment-in-optical-character-recognition/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 14:00:50 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[Backpropagation]]></category>
		<category><![CDATA[C-Sharp]]></category>
		<category><![CDATA[Machine Learning]]></category>
		<category><![CDATA[Neural Networks]]></category>
		<category><![CDATA[Optical Character Recognition]]></category>
		<category><![CDATA[Segmentation]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=1418</guid>
		<description><![CDATA[Introduction I&#8217;ve always been interested in machine learning and how it can be applied to a number of different problems. I spent some time during college learning some of the techniques used in machine learning, but since then I&#8217;ve gotten a bit rusty. So, I revisited the subject by doing some additional research and decided [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1418&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>I&#8217;ve always been interested in machine learning and how it can be applied to a number of different problems. I spent some time during college learning some of the techniques used in machine learning, but since then I&#8217;ve gotten a bit rusty. So, I revisited the subject by doing some additional research and decided to try my hand at <a href="http://en.wikipedia.org/wiki/Optical_character_recognition" target="blank">Optical Character Recognition</a> (OCR)- the process of identifying characters within an image and producing a text output. There are a handful of traditional aspects to this process: image acquisition, segmentation, recognition and correction. Acquisition is about correcting an input image so that a segmentation process can be readily applied; segmentation identifies the characters in the image, recognition maps those visual characters to text characters, finally correction takes the text output and rectifies any errors. The following outlines my approach to segmentation and recognition. </p>
<h3>Segmentation</h3>
<p>Consider the following body of text from one of my earlier posts:</p>
<p><img src="http://antimatroid.files.wordpress.com/2010/05/input.png?w=700" alt="" title="input"   class="aligncenter size-full wp-image-1420" /></p>
<p>The goal is to extract each of the characters in the image. The simplest way to accomplish this is to implement an algorithm that reads the image much in the same way one might read a block of text:  start at the top of the text and scan downward identifying all of the rows of text, then for each row, read all the characters from left to right, then identify words based on white space. Figuring out the word boundaries is done via a simple clustering process. Assuming we have an ordered set of rectangles produced by the first two steps, we can calculate the average distance between consecutive rectangles. Then, once this average has been produced, to then iterate over the list once more adding rectangles to words when the distance between consecutive rectangles is less than the average distance, then creating new words when the distance is exceeded.</p>
<p><img src="http://antimatroid.files.wordpress.com/2010/05/segmented-problems.png?w=700" alt="" title="segmented-problems"   class="aligncenter size-full wp-image-1426" /></p>
<p>This segmentation approach isn&#8217;t perfect as it assumes that we are dealing with evenly spaced characters of the same size from the same font. Of course, this isn&#8217;t always the case and we have things like <a href="http://en.wikipedia.org/wiki/Kerning" target="blank">kerning</a> and <a href="http://en.wikipedia.org/wiki/Typographic_ligature" target="blank">ligatures</a> to deal with. In this example two categories of problems arise: character combinations such as ay, ey and ly that are separable then combinations such as yw, rt and ct that are not separable end up being interpreted as a single character using this method. For the first category, I chose to divide rectangles whenever a line of characters has a single black pixel that does not have a black pixel neighboring ((x-1, y + a) | a &lt;- [-1, 1]) it to the left. The second case isn&#039;t as clear cut as all the pixels neighbor one another, in principal one could implement a <a href="http://en.wikipedia.org/wiki/K-means_clustering" target="blank">k-means clustering algorithm</a>, but that assumes you know how many characters (k) you have in the image. I decided to allow the error to propagate through the system.</p>
<h3>Recognition</h3>
<p>Starting out, I knew that I wanted to use an <a href="http://en.wikipedia.org/wiki/Artificial_neural_network" target="blank">Artificial neural network</a> (ANN), so I spent some time reading Stuart&#8217;s and Norvig&#8217;s &#8220;<a href="http://www.amazon.com/Artificial-Intelligence-Modern-Approach-2nd/dp/0137903952/ref=sr_1_2?ie=UTF8&amp;s=books&amp;qid=1274054835&amp;sr=8-2-spell" target="blank">Artificial Intelligence: A Modern Approach</a>&#8220;, but felt that it wasn&#8217;t as comprehensive as I wanted, so I also read MacKay&#8217;s &#8220;<a href="http://www.amazon.com/Information-Theory-Inference-Learning-Algorithms/dp/0521642981/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1274054854&amp;sr=1-1" target="blank">Information Theory, Inference and Learning Algorithms</a>,&#8221; which was more in tune with what I had in mind. I also came across a series (<a href="http://www4.rgu.ac.uk/files/chapter1%20-%20intro.pdf" target="blank">1</a>, <a href="http://www4.rgu.ac.uk/files/chapter2%20-%20intro%20to%20ANNs.pdf" target="blank">2</a>, <a href="http://www4.rgu.ac.uk/files/chapter3%20-%20bp.pdf" target="blank">3</a>) of pdf files hosted at Aberdeen&#8217;s Robert Gordon University that provided a more practical view of applying neural networks to pattern recognition.</p>
<p>A little background on ANNs: The general idea is that an individual neuron aggregates the weighted inputs from other neurons then outputs a signal. The magnitude of the signal is determined as a function of the aggregation called the activation function. Neurons are organized into layers, typically an input layer, one or more hidden layers and finally an output layer. Values from the input layer and feed into the hidden layer, then those outputs feed into the next and so on until all the values have gone through the output layer. The process of getting the network to map an input to an output is accomplished through training, in this case, a method known as <a href="http://en.wikipedia.org/wiki/Backpropagation" target="blank">Backpropagation</a>. Given an input and an expected output, the input is feed through the network and produces an output. The difference between the two output vectors is the error that then needs to be feed backward through the network updating each node&#8217;s input weights such that the net error of the system is reduced. The method is effectively a gradient descent algorithm. I recommend reading the aforementioned references for details on how it all works. Following is my implementation of the Backpropagation algorithm:</p>
<p><pre class="brush: csharp;">
using System;
using System.Linq;
using Library.Mathematics;

namespace OCRProject.Recognizers.NeuralNetworks {
    public class Neuron {
        Func&lt;double, double&gt; activationFunction;

        public Vector InputWeights { get; set; }

        public Neuron(Func&lt;double, double&gt; activationFunction) {
            this.activationFunction = activationFunction;
        }

        public double Output(Vector input) {
            return activationFunction(InputWeights.dot(input));
        }
    }

    public class NeuralNetwork {
        private Neuron[] hiddenLayer, outputLayer;

	...

        public Vector Output(Vector input) {
            Vector hiddenLayerOutput = new Vector(hiddenLayer.Length, (i) =&gt; hiddenLayer[i].Output(input));
            return new Vector(outputLayer.Length, (i) =&gt; outputLayer[i].Output(hiddenLayerOutput));
        }

        public Vector Train(Vector input, Vector desiredOutput, double learningRate) {
            Vector hOutput = new Vector(hiddenLayer.Length, (i) =&gt; hiddenLayer[i].Output(input));
            Vector oOutput = new Vector(outputLayer.Length, (i) =&gt; outputLayer[i].Output(hOutput));

            Vector oError = new Vector(
                oOutput.Dimension,
                (i) =&gt; oOutput[i] * (1 - oOutput[i]) * (desiredOutput[i] - oOutput[i])
            );

            for (int n = 0; n &lt; outputLayer.Length; n++) {
                outputLayer[n].InputWeights = new Vector(
                    hiddenLayer.Length,
                    (i) =&gt; outputLayer[n].InputWeights[i] + learningRate * oError[n] * hOutput[i]
                );
            }

            Vector hError = new Vector(
                hiddenLayer.Length,
                (i) =&gt; hOutput[i] * (1 - hOutput[i]) * (oError.dot(new Vector(oError.Dimension, (j) =&gt; outputLayer[j].InputWeights[i])))
            );

            for (int n = 0; n &lt; hiddenLayer.Length; n++) {
                hiddenLayer[n].InputWeights = new Vector(
                    input.Dimension,
                    (i) =&gt; hiddenLayer[n].InputWeights[i] + learningRate * hError[n] * input[i]
                );
            }

            return oError;
        }
    }
}
</pre></p>
<p>In terms of how all of this applies to OCR, I started out with all visible characters and produced a collection of 16&#215;16 images. Each image contained a single character centered in the image. This image was then mapped to a 256 dimensional vector with its corresponding character mapped to an 8 dimensional vector representing the expected output. The question that remains is how many hidden layer units should be used. To figure this out, I conducted a small experiment:</p>
<p><img src="http://antimatroid.files.wordpress.com/2010/06/chart1.png?w=700" alt="" title="chart"   class="aligncenter size-full wp-image-1441" /></p>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<th>Hidden Units</th>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
<th>11</th>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
<td>16</td>
</tr>
<tr>
<th>Minimum (%)</th>
<td>0</td>
<td>4</td>
<td>7</td>
<td>21</td>
<td>36</td>
<td>48</td>
<td>69</td>
<td>80</td>
<td>87</td>
<td>85</td>
<td>92</td>
<td>88</td>
<td>92</td>
<td>89</td>
<td>93</td>
<td>93</td>
</tr>
<tr>
<th>Average (%)</th>
<td>1</td>
<td>4</td>
<td>12</td>
<td>28</td>
<td>44</td>
<td>56</td>
<td>72</td>
<td>83</td>
<td>89</td>
<td>90</td>
<td>94</td>
<td>93</td>
<td>94</td>
<td>93</td>
<td>95</td>
<td>95</td>
</tr>
<tr>
<th>Maximum (%)</th>
<td>1</td>
<td>6</td>
<td>15</td>
<td>31</td>
<td>51</td>
<td>62</td>
<td>75</td>
<td>87</td>
<td>91</td>
<td>93</td>
<td>99</td>
<td>96</td>
<td>98</td>
<td>95</td>
<td>98</td>
<td>99</td>
</tr>
</table>
<p></p>
<p>Each run consisted of 10 trials with each trial taking 5000 iterations to train the network. (Normally, one can measure the <a href="http://en.wikipedia.org/wiki/Mean_squared_error" target="blank">Mean squared error</a> of the network and halt the training process once it is sufficiently small.) After doing this testing, I found that ANNs with 11 hidden units looked to give the highest accuracy with the fewest number of units. Given the original text that was used, the following text was produced:</p>
<p><img src="http://antimatroid.files.wordpress.com/2010/06/output1.png?w=700" alt="" title="output"   class="aligncenter size-full wp-image-1440" /></p>
<p>As expected, the category of errors that were identified earlier (character combinations ff, rt, ct) were not segmented correctly and consequently not recognized correctly.</p>
<h3>Wrap-up</h3>
<p>There is a lot of additional work that could be thrown at the project. In the future, it&#8217;d be good to modify the solution to accept any sized font as well as any font, adding support for images containing scanned or photographed text rather than computer generated images and some error correction on the output to flag parts of the text that require review. I suspect that I will continue down this road and start investigating how these methods can be applied to general computer vision for some planned robotics projects.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/1418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/1418/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/1418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/1418/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/antimatroid.wordpress.com/1418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/antimatroid.wordpress.com/1418/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/antimatroid.wordpress.com/1418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/antimatroid.wordpress.com/1418/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/1418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/1418/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/1418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/1418/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/1418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/1418/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&amp;blog=4448583&amp;post=1418&amp;subd=antimatroid&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2010/06/01/an-experiment-in-optical-character-recognition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2010/05/input.png" medium="image">
			<media:title type="html">input</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2010/05/segmented-problems.png" medium="image">
			<media:title type="html">segmented-problems</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2010/06/chart1.png" medium="image">
			<media:title type="html">chart</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2010/06/output1.png" medium="image">
			<media:title type="html">output</media:title>
		</media:content>
	</item>
	</channel>
</rss>
