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

<channel>
	<title>Polymath Programmer &#187; Mathematics</title>
	<atom:link href="http://polymathprogrammer.com/category/mathematics/feed/" rel="self" type="application/rss+xml" />
	<link>http://polymathprogrammer.com</link>
	<description>Mathematics. Programming. Entrepreneurship.</description>
	<lastBuildDate>Fri, 03 Feb 2012 13:34:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Quadratic Bezier curve control point calculation demo</title>
		<link>http://polymathprogrammer.com/2012/01/16/quadratic-bezier-curve-control-point-calculation-demo/</link>
		<comments>http://polymathprogrammer.com/2012/01/16/quadratic-bezier-curve-control-point-calculation-demo/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 11:07:55 +0000</pubDate>
		<dc:creator>Vincent</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[bezier]]></category>
		<category><![CDATA[control point]]></category>
		<category><![CDATA[curve]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[quadratic]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://polymathprogrammer.com/?p=3319</guid>
		<description><![CDATA[MacGile made a video to demonstrate the calculation of control points of a quadratic Bezier curve. The algorithm is based on what I wrote here. That looks awesome! Thanks MacGile!]]></description>
			<content:encoded><![CDATA[<p>MacGile made a video to demonstrate the calculation of control points of a quadratic Bezier curve. The algorithm is based on <a href="http://polymathprogrammer.com/2010/05/03/reverse-engineering-quadratic-bezier-curves/">what I wrote here</a>.</p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/s1ALgNnRi6Q" frameborder="0" allowfullscreen></iframe></p>
<p>That looks awesome! Thanks MacGile!</p>
]]></content:encoded>
			<wfw:commentRss>http://polymathprogrammer.com/2012/01/16/quadratic-bezier-curve-control-point-calculation-demo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bezier curves prefer tea</title>
		<link>http://polymathprogrammer.com/2011/12/11/bezier-curves-prefer-tea/</link>
		<comments>http://polymathprogrammer.com/2011/12/11/bezier-curves-prefer-tea/#comments</comments>
		<pubDate>Sun, 11 Dec 2011 12:03:12 +0000</pubDate>
		<dc:creator>Vincent</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[bezier]]></category>
		<category><![CDATA[calculation]]></category>
		<category><![CDATA[cartesian]]></category>
		<category><![CDATA[curve]]></category>

		<guid isPermaLink="false">http://polymathprogrammer.com/?p=3196</guid>
		<description><![CDATA[My maths professor was hammering on the fact that Citroen used Bezier curves to make sure their cars have aesthetically pleasing curves. Again. (This is not a sponsored post from the automaker). While I appreciate his effort in trying to make what I&#8217;m learning relevant to the real world, I kinda got the idea that [...]]]></description>
			<content:encoded><![CDATA[<p>My maths professor was hammering on the fact that Citroen used Bezier curves to make sure their cars have aesthetically pleasing curves. Again. (This is not a sponsored post from the automaker).</p>
<p>While I appreciate his effort in trying to make what I&#8217;m learning relevant to the real world, I kinda got the idea that Citroen used Bezier curves in their design process. Right about the 3rd tutorial lesson.</p>
<p>My professor then went on to give us homework. &#8220;Us&#8221; meaning 5 of us. It was an honours degree course. It wasn&#8217;t like there was a stampede to take post-graduate advanced maths lessons, you know.</p>
<p>Oh yes, homework. My professor, with wisdom acquired over years of teaching, gave a blend of theoretical and calculation-based questions. Any question that had the words &#8220;prove&#8221;, &#8220;justify&#8221;, &#8220;show&#8221; are probably theoretical questions. Calculation-based questions are like &#8220;What is 1 + 1?&#8221;. Everyone, at least theoretically (haha!), should be able to do the calculation-based questions. The theoretical questions would require more thinking (&#8220;Prove that such and such Bezier curve is actually equal to such and such.&#8221;).</p>
<p>My friend, who took the course with me, loved calculation-based questions. She&#8217;d sit patiently and hammer at the numbers and the calculator. I can&#8217;t say I love them. My professor once gave a question that amounted to solving a system of 5 linear equations with 5 unknowns, which amounted to solving a 5 by 5 matrix. <strong>By hand.</strong> (It involves 15 divisions, 50 multiplications and 50 subtractions. There&#8217;s a reason why linear algebra and numerical methods were pre-requisites) I wanted to scream in frustration, throw my foolscap paper at him, and strangle him. Not necessarily in that order.</p>
<p>This coming from someone who is fine with writing a C program doing memory allocations (using the malloc function. And then manually freeing the pointer with the memory allocation. We didn&#8217;t have garbage collection, ok?) to simulate an N-sized matrix, and then perform Gauss-Jordan elimination on the matrix. I used that program to solve a 100 by 100 matrix. But I dreaded solving a 5 by 5 matrix by hand.</p>
<p>It probably explains why I remember Bezier curves so much.</p>
<p>Anyway, a while ago, someone sent me a question (through Facebook, of all channels). He asked, <strong>for a given &#8220;y&#8221; value of a Bezier curve, how do you find the &#8220;x&#8221; value?</strong></p>
<p>That is a question without a simple answer. The answer is, there&#8217;s no guarantee there&#8217;s only <em>one</em> &#8220;x&#8221; value. A cubic Bezier curve has a possibility of having 1, 2 or 3 &#8220;x&#8221; values (given a &#8220;y&#8221;). Here&#8217;s the &#8220;worst&#8221; case scenario:</p>
<p><img src="http://polymathprogrammer.com/images/blog/201112/multixvalues.png" alt="Multi x values" /></p>
<p>So you can have at most 3 &#8220;x&#8221; values. In the case of the person who asked the question, this is not just wrong, but actually <em>dangerous</em>. The person was an engineer, working on software that cuts metal (or wood). The software had a Bezier curve in it, which it used to calculate (x,y) coordinate values to direct the laser beam (or whatever cutting tool) to the next point (and thus cut the material).</p>
<p>If a &#8220;y&#8221; value has multiple &#8220;x&#8221; values, the software won&#8217;t know which &#8220;x&#8221; value you want. And thus cut the material wrongly.</p>
<p><strong>The only way a Bezier curve has only 1 value, is if it&#8217;s monotonically increasing/decreasing.</strong> That means for all values of x and y such that x <= y [or x >= y], that f(x) <= f(y) [or f(x) >= f(y)].</p>
<p>Bezier curves don&#8217;t work well in the Cartesian plane. They work fine after you&#8217;ve used them to calculate values, and then transfer onto the Cartesian plane. Bezier curves prefer to work with values of t.</p>
]]></content:encoded>
			<wfw:commentRss>http://polymathprogrammer.com/2011/12/11/bezier-curves-prefer-tea/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Negative sales targets and percentage commissions</title>
		<link>http://polymathprogrammer.com/2011/08/24/negative-sales-targets-percentage-commissions/</link>
		<comments>http://polymathprogrammer.com/2011/08/24/negative-sales-targets-percentage-commissions/#comments</comments>
		<pubDate>Wed, 24 Aug 2011 13:06:06 +0000</pubDate>
		<dc:creator>Vincent</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[business]]></category>
		<category><![CDATA[calculation]]></category>
		<category><![CDATA[commission]]></category>
		<category><![CDATA[formula]]></category>
		<category><![CDATA[maths]]></category>
		<category><![CDATA[negative]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[sales]]></category>
		<category><![CDATA[target]]></category>

		<guid isPermaLink="false">http://polymathprogrammer.com/?p=3014</guid>
		<description><![CDATA[A while ago, I received an email from a distraught salesman. He believed his sales commissions were wrongly calculated, and asked me to shed some light. Note that I&#8217;m not using the exact numbers he gave in his email. The story goes that Michael (as I&#8217;ll call him) and his colleagues were given sales targets [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, I received an email from a distraught salesman. He believed his sales commissions were wrongly calculated, and asked me to shed some light.</p>
<p>Note that I&#8217;m not using the exact numbers he gave in his email.</p>
<p>The story goes that Michael (as I&#8217;ll call him) and his colleagues were given sales targets that were negative. <strong>How could sales targets be negative?</strong> Shouldn&#8217;t you be trying to sell something? The reason given was that the current economy was disastrous, and basically each sales person was <em>trying to not lose sales</em>.</p>
<p>You&#8217;re gonna bleed. It&#8217;s how much you bled.</p>
<p>Anyway, given Michael&#8217;s negative sales target, he managed to exceed it. He didn&#8217;t manage to bring in sales (positive sales numbers), <strong>but</strong> he didn&#8217;t lose <em>too</em> much money (slight negative sales numbers). But his sales commissions didn&#8217;t reflect that.</p>
<p>Now I&#8217;m not going to discuss how that works out. I can&#8217;t presume to understand the business logic behind the sales commission in this case, but I&#8217;ll discuss the mathematics behind the numbers.</p>
<h3>The normal sales targets and commission</h3>
<p>Let&#8217;s say your sales target for this month is $1000. This means you&#8217;re expected to sell about $1000 worth of products or services. We&#8217;ll ignore the condition that you will get <em>some</em> commission based on what you sell, regardless of how much you sold (my brother&#8217;s a sales person), as well as other types of commissions.</p>
<p>Let&#8217;s say the sales commission is based on how much <em>extra</em> you sold beyond your sales target. Makes sense, right? Let&#8217;s use simple percentages.</p>
<p>If you sold $1100 worth of products or services, then your percentage commission might be calculated as follows:<br />
(Difference between Your Sales and Your Sales Target) / (Your Sales Target)</p>
<p>Or ($1100 &#8211; $1000) / ($1000) = 10% commission.</p>
<p>This is assuming that your sales amount exceeded the sales target, of course.</p>
<h3>The case of negative sales targets</h3>
<p>Now if the sales target is negative, as in Michael&#8217;s case, the mathematical formula still applies. But you have to note the negative sign. For some reason, &#8220;business&#8221; people (no offense to business people) tend to see -4567 as <em>larger</em> than 12, even though 12 > -4567. They see the magnitude first, not the value itself. (It&#8217;s also why I get emails about calculations involving negative numbers&#8230; anyway&#8230;)</p>
<p>Let&#8217;s say the sales target is -$1000. Everyone&#8217;s expected to lose money, but you try not to lose more than $1000. At least that&#8217;s what I&#8217;m interpreting it as.</p>
<p>Let&#8217;s say Michael managed to lose only $50. Or -$50 to be clear. The formula<br />
(Difference between Your Sales and Your Sales Target) / (Your Sales Target)</p>
<p>have to be modified to this<br />
(Difference between Your Sales and Your Sales Target) / (Magnitude of Your Sales Target)</p>
<p>In maths and programming terms, the &#8220;magnitude&#8221; part refers to the absolute function. Meaning you ignore any negative signs. Actually, the modified version works for the normal case too (which is why you should use it for the normal version anyway to take care of weird cases like this but I digress&#8230;).</p>
<p>So, we get (-$50 &#8211; [-$1000]) / abs(-$1000) = $950 / $1000<br />
= 95%</p>
<p>Actually, you should use this:<br />
abs( [Your Sales] &#8211; [Your Sales Target] ) / abs(Your Sales Target)</p>
<p>That&#8217;s the &#8220;foolproof&#8221; version. Consider it a bonus for reading this far. Frankly speaking, any competent programmer should be able to come up with that formula, even without much maths background. You just need to think about the situation a little carefully (ask &#8220;what if?&#8221; more often).</p>
<h3>Michael&#8217;s calculated commission</h3>
<p>When Michael wrote to me, he said his commission was calculated as follows (given that he only lost $50):<br />
-$50 / -$1000 = 5%</p>
<p>Let&#8217;s say someone else lost -$900 that month. With the above calculation, that person gets:<br />
-$900 / -$1000 = 90%</p>
<p>Clearly it makes more sense to lose more money! This was why Michael wrote to me.</p>
<p>I don&#8217;t propose the method I gave is correct, business-logic-wise. Michael didn&#8217;t give me any details on what he&#8217;s selling, or what his company is (or even why it&#8217;s acceptable to have negative sales targets, regardless of the economy). So I cannot give any help other than from a pure mathematical point of view. But I hope it&#8217;ll at least give Michael a fairer commission amount.</p>
<h3>Questions</h3>
<p>Given Michael&#8217;s situation, what do you think is an appropriate calculation formula?</p>
<p>Can you think of (or know of) a realistic situation where a negative sales target is acceptable? I say &#8220;acceptable&#8221;, but seriously, no company should &#8220;accept&#8221; that they lose money every month.</p>
]]></content:encoded>
			<wfw:commentRss>http://polymathprogrammer.com/2011/08/24/negative-sales-targets-percentage-commissions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cantor sets and Invisibility cloaks</title>
		<link>http://polymathprogrammer.com/2011/07/25/cantor-set/</link>
		<comments>http://polymathprogrammer.com/2011/07/25/cantor-set/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 12:54:59 +0000</pubDate>
		<dc:creator>Vincent</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[cantor set]]></category>
		<category><![CDATA[cantor ternary set]]></category>
		<category><![CDATA[chaos theory]]></category>
		<category><![CDATA[clopen set]]></category>
		<category><![CDATA[closed set]]></category>
		<category><![CDATA[fractal]]></category>
		<category><![CDATA[georg cantor]]></category>
		<category><![CDATA[mathematics]]></category>
		<category><![CDATA[maths]]></category>
		<category><![CDATA[open set]]></category>
		<category><![CDATA[real line]]></category>

		<guid isPermaLink="false">http://polymathprogrammer.com/?p=2976</guid>
		<description><![CDATA[More specifically, it&#8217;s the Cantor ternary set.]]></description>
			<content:encoded><![CDATA[<p><iframe width="560" height="349" src="http://www.youtube.com/embed/ZkgXuE4NIfw" frameborder="0" allowfullscreen></iframe></p>
<p>More specifically, it&#8217;s the Cantor ternary set.</p>
]]></content:encoded>
			<wfw:commentRss>http://polymathprogrammer.com/2011/07/25/cantor-set/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Revenue sharing and operations research – part 2</title>
		<link>http://polymathprogrammer.com/2011/07/16/revenue-sharing-operations-research-part2/</link>
		<comments>http://polymathprogrammer.com/2011/07/16/revenue-sharing-operations-research-part2/#comments</comments>
		<pubDate>Sat, 16 Jul 2011 12:51:35 +0000</pubDate>
		<dc:creator>Vincent</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[linear inequalities]]></category>
		<category><![CDATA[maths]]></category>
		<category><![CDATA[operations research]]></category>
		<category><![CDATA[revenue sharing]]></category>

		<guid isPermaLink="false">http://polymathprogrammer.com/?p=2957</guid>
		<description><![CDATA[This is a mini-series on how revenue sharing and operations research are linked. You might want to read part 1 on the specific business problem I was solving. In this part, I&#8217;ll be telling you about the maths behind the business solution. But first, I have to tell you about a couple of maths concepts. [...]]]></description>
			<content:encoded><![CDATA[<p>This is a mini-series on how revenue sharing and operations research are linked. You might want to read <a href="http://polymathprogrammer.com/2011/07/15/revenue-sharing-operations-research-part1/">part 1</a> on the specific business problem I was solving. In this part, I&#8217;ll be telling you about the maths behind the business solution.</p>
<p>But first, I have to tell you about a couple of maths concepts.</p>
<h3>Converging from opposite directions</h3>
<p>Let&#8217;s say someone asked you how tall you are. Instead of giving a straight answer, you decide to give a simple maths puzzle. You say you are at least 6 feet tall. Then you also say you are at most 6 feet tall. So what&#8217;s your height? Let&#8217;s put those 2 conditions into mathematical form:</p>
<ul>
<li>H >= 6</li>
<li>H <= 6</li>
</ul>
<p>To satisfy those 2 conditions, there&#8217;s only 1 answer: You must be 6 feet tall.</p>
<p>It looks elementary, but it will come into play later on. Just keep in mind that an equality can be split into 2 inequalities. This actually reminds me of the <a href="http://en.wikipedia.org/wiki/Squeeze_theorem">squeeze theorem</a>, but I digress.</p>
<h3>Reversing the direction of an inequality</h3>
<p>The next maths concept is how to reverse the direction of an inequality. For example, we have</p>
<p>x >= 5</p>
<p>This can also be expressed as</p>
<p>-x <= -5</p>
<p>This will also come in handy later on.</p>
<h3>System of linear inequalities</h3>
<p>My experience in operations research had been confined to course work in an academic semester. Operations research mainly is about maximising or minimising some objective. You are given a series of conditions to fulfill. Then you&#8217;re given an objective to maximise or minimise. And you&#8217;re to translate those conditions and objective into a mathematical model formulation. Let&#8217;s look at an example.</p>
<p>Jake&#8217;s mom gave him $15 to buy some sweets (because he did awesomely in a maths test. Parental note: I don&#8217;t think this is a good reward, but hey, it&#8217;s your kid). But mom told Jake that he cannot buy more than 3 lollipops (although Jake already decided he&#8217;s not buying more than 4). There&#8217;s this contest where if you submit wrappers from peppermints and lollipops, you win an iPad (I&#8217;m totally making this up!). Jake needed just 4 more wrappers, and he didn&#8217;t want more than 4 because he wanted to buy more chocolates. Also, Jake had secretly decided to buy at least $10 worth of sweets.</p>
<p>Now, chocolates cost $2 each. Peppermints cost $1 each. And lollipops cost $3 each.</p>
<p>Let C be the number of chocolates bought.<br />
Let P be the number of peppermints bought.<br />
Let L be the number of lollipops bought.</p>
<p>Let the objective be to maximise the number of sweets bought. So in maths form, we typically write it as:<br />
max C + P + L</p>
<p>Let&#8217;s formulate the conditions:<br />
2C + P + 3L <= 15 (mom only gave Jake $15)<br />
L <= 3 (mom said Jake can't buy more than 3 lollipops)<br />
L <= 4 (Jake independently decided he's not buying more than 4)<br />
P + L = 4 (Jake just wanted 4 more wrappers)<br />
2C + P + 3L >= 10 (Jake wanted to waste at least $10)</p>
<p>Now, logically speaking the condition for the number of wrappers doesn&#8217;t make sense. We could say the number of wrappers for peppermints and lollipops be at least 4, which makes better sense. But this is a hypothetical example. More to the point, it&#8217;s <em>my</em> hypothetical example, and I needed an equality condition. So there.</p>
<p>You might also note that there are redundant conditions.<br />
L <= 3<br />
L <= 4</p>
<p>can be represented by just<br />
L <= 3<br />
because if it's satisfied, then L is definitely <= 4. This is to show you that in your mathematical formulation, you can get redundant conditions. It's up to you to eliminate them so you work with less conditions. Real life problems are messy, don't you know?</p>
<p>Let's clean up the formulation.</p>
<p>Objective: max C + P + L<br />
2C + P + 3L <= 15<br />
-2C - P - 3L <= -10<br />
P + L <= 4<br />
-P - L <= -4<br />
L <= 3</p>
<p>I've rearranged the conditions a little for clarity. Note the inequality reversal for the "more than $10" condition. Note the split of the equality to 2 inequalities. Why are we doing this? So we get the general form Ax <= B where<br />
A is the coefficient matrix<br />
x is the variable vector<br />
B is the value vector</p>
<p>We need to do the inequality reversal so the "direction" for all the inequalities is the same. Otherwise we cannot have Ax <= B.</p>
<p>In this case, A is<br />
| 2  1  3 |<br />
|-2 -1 -3 |<br />
| 0  1  1 |<br />
| 0 -1 -1 |<br />
| 0  0  1 |</p>
<p>x is<br />
| C |<br />
| P |<br />
| L |</p>
<p>B is<br />
| 15 |<br />
|-10 |<br />
|  4 |<br />
| -4 |<br />
|  3 |</p>
<p>With this Ax <= B form, we're dealing with matrices and vectors. That means we can use all the mathematical techniques for solving such formulations, such as <a href="http://en.wikipedia.org/wiki/Gaussian_elimination">Gaussian elimination</a> or even <a href="http://en.wikipedia.org/wiki/Gauss%E2%80%93Jordan_elimination">Gauss-Jordan elimination</a>. The idea is to solve all of our unknowns at one go.</p>
<p>You might notice that we have more inequalities than unknowns. This means we have multiple solutions.</p>
<p>For example, a possible solution for (C, P, L) is (2, 3, 1). That solution satisfies all the conditions. It doesn&#8217;t mean it&#8217;s optimal, but it&#8217;s <em>a</em> solution. This means solutions exist for the problem (sometimes just knowing this is reassuring&#8230;).</p>
<h3>The dual</h3>
<p>I want you to know that the objective can always be stated in the opposite manner. If the objective is to &#8220;maximise X&#8221;, it is equivalent to &#8220;minimising -X&#8221;. This is known as the <a href="http://en.wikipedia.org/wiki/Dual_problem">dual</a> of the problem. For example, maximising profit is equivalent to minimising the negative value of profit. <strong>NOTE</strong>: Maximising profit is NOT equivalent to minimising cost. Profit and cost are generally not the opposite of each other (they&#8217;re different business terms), even if they sound like they are (and sometimes give the same solutions). The mathematical formulation and solution is different for both of them. Know what you&#8217;re solving.</p>
<p>This duality property is useful if your program is optimised to solve only minimisation problems. So you just convert a maximisation problem to a minimisation problem, and your program works just fine!</p>
<h3>Linearity</h3>
<p>And you might also note that the objective and the conditions are all linear, meaning unknowns are up to power of 1 (as opposed to quadratic or cubic). Sometimes you get a non-linear condition, and depending on the situation, you might be able to form linear conditions that are equivalent to that non-linear condition.</p>
<p>For example, CP = 2 (number of chocolates multiply by number of peppermints equal to 2).<br />
This set of conditions might be equivalent:</p>
<ul>
<li>C >= 1</li>
<li>P >= 1</li>
<li>C <= 2</li>
<li>P <= 2</li>
<li>C + P = 3</li>
</ul>
<p>The first 2 conditions are inferred from the fact that C and P cannot be zero. If they are, then CP = 2 is never satisfied. If they&#8217;re never zero, then they must be at least 1.</p>
<p>The 3rd and 4th conditions are inferred from the fact that C and P cannot be more than 2. If they are, then the other unknown is fractional. For example, if C is 4, then P must be 1/2. But these are number of items, so they must be integer. Therefore, C and P must be at most 2.</p>
<p>Based on the first 4 conditions, C and P can only take on either 1 or 2 as values. The only combination of permutations that still satisfies the &#8220;CP = 2&#8243; condition is one unknown must be 1 and the other unknown be 2. Hence the sum of the two unknowns must be equal to 3.</p>
<p>Not all non-linear conditions can be translated into linear conditions. Go ask an operations research professor for more.</p>
<h3>Integral conditions</h3>
<p>And we come to the worst part. All the unknowns must be integer (you can&#8217;t buy 2.37 chocolates. Well, at least not without the candy store owner going crazy). This integral condition makes the problem much harder to solve. If you solve the Ax <= B form, you generally get fractional values for the unknowns. This is the optimum solution, but doesn't satisfy the integral condition.</p>
<p>If I remember correctly, you solve Ax <= B as usual. Then you take one of the unknowns and work with the 2 integers that are the floor and ceiling values of that unknown. Then you solve iteratively again, with reduced and reformulated conditions for Py <= Q.</p>
<p>For example, if you get C = 2.37, then you split off 2 scenarios with C = 2 and C = 3 as the optimum solutions. Scenario 1 is</p>
<p>Objective: max P + L<br />
P + 3L <= 11<br />
-P - 3L <= -6<br />
P + L <= 4<br />
-P - L <= -4<br />
L <= 3</p>
<p>Scenario 2 is</p>
<p>Objective: max P + L<br />
P + 3L <= 9<br />
-P - 3L <= -4<br />
P + L <= 4<br />
-P - L <= -4<br />
L <= 3</p>
<p>Basically, you just substitute C = 2 and C = 3. For each scenario, you continue splitting with the other unknowns. There are 3 unknowns, so there are eventually 8 scenarios (2^3 = 8). It's a binary tree. Well, it's at most 8 scenarios, because you might get lucky and hit an original optimised solution with one or more of the unknowns having an original value that's already an integer (now that's a long sentence...).</p>
<p>For each of the scenarios, you either get a solution or you get a contradiction somewhere (meaning it's unsolvable). The contradiction can come from the splitting, because when you substitute a possible value into one of the unknowns, the resulting set of conditions contain a contradiction. For example, out of the set of conditions, you get 2 conditions as follows:</p>
<p>x <= 4<br />
-x <= -8 ( meaning x >= 8 )</p>
<p>Since an unknown variable cannot be both (less than or equal to 4) AND be (more than or equal to 8), the scenario becomes unsolvable.</p>
<p>Then you examine all those scenarios with solutions, substitute the solutions to the unknowns, and compare the result. If it&#8217;s a maximising problem, you check which scenario gave a solution that&#8217;s the largest. If it&#8217;s a minimisation problem, you check which scenario gave a solution that&#8217;s the smallest. And that particular solution becomes the optimum integral solution.</p>
<p>Let me tell you, you do <em>not </em>want to do this by hand. This is why software is written to handle the Gaussian eliminations and checking all the possible scenarios due to the integral condition. With just 3 unknowns, you&#8217;re expected to do 9 Gaussian eliminations (1 for the original, 8 for the branch scenarios), and check through all 8 scenarios. Imagine having hundreds of variables and conditions&#8230;</p>
<p>Mathematics and programming in one. Having skills in one but not the other makes writing the software quite difficult. Imagine a mathematician who cannot express his ideas in programming code, or a programmer who cannot understand the maths involved.</p>
<h3>Back to the business problem</h3>
<p>After that extremely long mathematical discussion, we can now turn back to the original business problem. If you haven&#8217;t read <a href="http://polymathprogrammer.com/2011/07/15/revenue-sharing-operations-research-part1/">part 1</a>, you should do so now, otherwise you&#8217;d be lost.</p>
<p>Let R be the total revenue for a content provider for that particular month<br />
Let R1 be the total revenue for ProductA<br />
Let R2 be the total revenue for ProductB<br />
Let R3 be the total revenue for ProductC<br />
(so R1 + R2 + R3 = R)<br />
Let X1 be the revenue share for the telecommunications company for ProductA<br />
Let X2 be the revenue share for the telecommunications company for ProductB<br />
Let X3 be the revenue share for the telecommunications company for ProductC<br />
Let Y1 be the revenue share for the content provider for ProductA<br />
Let Y2 be the revenue share for the content provider for ProductB<br />
Let Y3 be the revenue share for the content provider for ProductC</p>
<p>Let the revenue sharing split for the telecommunications company be 30%<br />
Let the revenue sharing split for the content provider be 70%</p>
<p>The objective is to minimise rounding errors. Actually, there should be no errors, but we can live with a possible solution first. If it&#8217;s non-zero, <em>then</em> we panic&#8230;</p>
<p>The unknowns in this case are X1, X2, X3, Y1, Y2 and Y3.</p>
<p>Objective: minimise ABS(R &#8211; X1 &#8211; X2 &#8211; X3 &#8211; Y1 &#8211; Y2 &#8211; Y3)<br />
X1 + Y1 = R1<br />
X2 + Y2 = R2<br />
X3 + Y3 = R3<br />
X1 = 0.30 * R1<br />
X2 = 0.30 * R2<br />
X3 = 0.30 * R3<br />
Y1 = 0.70 * R1<br />
Y2 = 0.70 * R2<br />
Y3 = 0.70 * R3</p>
<p>And Xi&#8217;s and Yi&#8217;s must be integers, for i = 1, 2, 3</p>
<p>And ABS stands for the absolute function.</p>
<p>As you can see, there&#8217;s a lot of equal conditions. And it&#8217;s a problem with integer conditions. Frankly speaking, I don&#8217;t want to program a solution either. Just looking at what I&#8217;ve written is already giving me a headache&#8230;</p>
<p>I&#8217;m glad my ex-colleague said no. *whew* This was too complicated to implement. And the content provider might have more products, so the number of unknowns is unknown (no pun intended).</p>
<p>There&#8217;s actually a little bit more to the maths formulation, but I&#8217;ll tell you about it in the 3rd part. I had to come up with a solution that made mathematical sense and could be understood by other people. Mostly, the users (that is, &#8220;normal&#8221; people) must be able to understand the logic behind it.</p>
<p>Leave a comment if you have any questions about the maths involved (or even tell me I&#8217;m wrong), and I&#8217;ll do my best to answer them.</p>
]]></content:encoded>
			<wfw:commentRss>http://polymathprogrammer.com/2011/07/16/revenue-sharing-operations-research-part2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Absolutes and almost every</title>
		<link>http://polymathprogrammer.com/2011/05/30/absolutes-and-almost-every/</link>
		<comments>http://polymathprogrammer.com/2011/05/30/absolutes-and-almost-every/#comments</comments>
		<pubDate>Mon, 30 May 2011 05:00:08 +0000</pubDate>
		<dc:creator>Vincent</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[absolute]]></category>
		<category><![CDATA[ae]]></category>
		<category><![CDATA[almost every]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[maths]]></category>

		<guid isPermaLink="false">http://polymathprogrammer.com/?p=2844</guid>
		<description><![CDATA[Back when I was writing my thesis, I came across this abbreviation: a.e. It took me a while, but I eventually found that it meant almost every. This might have been almost everywhere in measure theory, but I don&#8217;t think so. I seem to recall just &#8220;almost every&#8221;. Almost every what, you ask? Well, I [...]]]></description>
			<content:encoded><![CDATA[<p>Back when I was writing my <a href="http://polymathprogrammer.com/2008/03/05/computer-virus-behaviour-thesis/">thesis</a>, I came across this abbreviation: a.e. It took me a while, but I eventually found that it meant <em>almost every</em>. This might have been <em><a href="http://en.wikipedia.org/wiki/Almost_everywhere">almost everywhere</a></em> in <a href="http://en.wikipedia.org/wiki/Measure_theory">measure theory</a>, but I don&#8217;t think so. I seem to recall just &#8220;almost every&#8221;.</p>
<p>Almost every <em>what</em>, you ask?</p>
<p>Well, I was doing research on computer virus behaviour, so I had books from computer security, graph theory, biological viruses, mathematical models (with exponents and ordinary differential equations and such). I think it was in graph theory. An author was talking about a result or theorem and the proof included <em>almost every</em> type of graph, which was good enough.</p>
<p>I thought that was interesting, because I&#8217;ve thought of mathematics as absolute. Maybe this was why I suck at statistics&#8230; The idea of some event having a probability of happening, instead of just <em>be</em> or <em>be not</em>, shakes my world somewhat. Of course, I&#8217;m less shaken now since life isn&#8217;t really absolute&#8230;</p>
<p>My greatest accomplishment came when I was in the computer lab, and a Masters student was around. She&#8217;s from China, and you know those people are wicked clever. She held up a book, looked at me, then walked over to me.</p>
<p>&#8220;Do you know what a.e. means?&#8221;</p>
<p>AHA! Me, honours student, knew something a Masters student didn&#8217;t!</p>
<p>&#8220;I think it means <em>almost every</em>.&#8221;<br />
&#8220;Oh. OHHHH! Thanks!&#8221;</p>
<p>I suspect she asked me because she believed my English was better than hers, and not because I was more knowledgeable in whatever topic that book of hers was about. Have you seen programming books translated to Chinese? I can read the words, but that doesn&#8217;t mean I know what the hashbrown that meant&#8230; She approached me probably (yay statistics) because she believed a.e. stood for something that someone moderately versed in English will know.</p>
<p>Still, it was an accomplishment. I could tell you I went on to brag about it to all my friends, but truth be told, I continued working on my program code&#8230; I was in the computer lab for <em>something</em>, you know&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://polymathprogrammer.com/2011/05/30/absolutes-and-almost-every/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Is there an equation to describe regular polygons?</title>
		<link>http://polymathprogrammer.com/2011/05/29/regular-polygons-equation/</link>
		<comments>http://polymathprogrammer.com/2011/05/29/regular-polygons-equation/#comments</comments>
		<pubDate>Sun, 29 May 2011 12:12:36 +0000</pubDate>
		<dc:creator>Vincent</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[equation]]></category>
		<category><![CDATA[polygon]]></category>
		<category><![CDATA[question]]></category>
		<category><![CDATA[regular]]></category>

		<guid isPermaLink="false">http://polymathprogrammer.com/?p=2840</guid>
		<description><![CDATA[So a blog reader, Michael Gmirkin, sent me an in-depth email about the possibility of the existence of a super equation that can describe any regular polygon. I wasn&#8217;t sure. For reference, you might want to check out these 2 blog posts about the equation for a square: question, answer. I was going to just [...]]]></description>
			<content:encoded><![CDATA[<p>So a blog reader, Michael Gmirkin, sent me an in-depth email about the possibility of the existence of a super equation that can describe any regular polygon. I wasn&#8217;t sure. For reference, you might want to check out these 2 blog posts about the equation for a square: <a href="http://polymathprogrammer.com/2010/02/22/quiz-can-you-describe-square-with-1-equation/">question</a>, <a href="http://polymathprogrammer.com/2010/03/01/answered-can-you-describe-a-square-with-1-equation/">answer</a>.</p>
<p>I was going to just ask you here. Then I remembered there&#8217;s a Stack Overflow equivalent for maths. <a href="http://math.stackexchange.com/questions/41940/is-there-an-equation-to-describe-regular-polygons">So I went there and asked the question</a>. So if you know the answer, you can comment here, or go to the maths StackExchange site and earn yourself some points.</p>
<p>You can assume that the centre of the regular polygon in at the origin (0,0). Researching a little on the topic, I also learnt about the <a href="http://en.wikipedia.org/wiki/Apothem">apothem</a>, which is also the shortest distance from the centre to a polygon&#8217;s side. The &#8220;normal&#8221; radius is the distance from the centre to one of the regular polygon&#8217;s vertex.</p>
<p>If you trace 2 circles, one with the apothem and one with the radius, you get an inscribed circle and circumscribed circle respectively.</p>
]]></content:encoded>
			<wfw:commentRss>http://polymathprogrammer.com/2011/05/29/regular-polygons-equation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t rely on just the average!</title>
		<link>http://polymathprogrammer.com/2011/04/18/dont-rely-on-just-the-average/</link>
		<comments>http://polymathprogrammer.com/2011/04/18/dont-rely-on-just-the-average/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 13:08:45 +0000</pubDate>
		<dc:creator>Vincent</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[arithmetic]]></category>
		<category><![CDATA[average]]></category>
		<category><![CDATA[height]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[mathematics]]></category>
		<category><![CDATA[mean]]></category>
		<category><![CDATA[median]]></category>
		<category><![CDATA[metric]]></category>
		<category><![CDATA[mode]]></category>
		<category><![CDATA[sample]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://polymathprogrammer.com/?p=2760</guid>
		<description><![CDATA[I made this video because I&#8217;ve always been skeptical about reports (such as population census and surveys) with the average as the only statistical measurement.]]></description>
			<content:encoded><![CDATA[<p><iframe title="YouTube video player" width="560" height="349" src="http://www.youtube.com/embed/-Rw7lhj-yfk" frameborder="0" allowfullscreen></iframe></p>
<p>I made this video because I&#8217;ve always been skeptical about reports (such as population census and surveys) with the average as the only statistical measurement.</p>
]]></content:encoded>
			<wfw:commentRss>http://polymathprogrammer.com/2011/04/18/dont-rely-on-just-the-average/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Bezier curve inflection points</title>
		<link>http://polymathprogrammer.com/2011/02/06/bezier-curve-inflection-points/</link>
		<comments>http://polymathprogrammer.com/2011/02/06/bezier-curve-inflection-points/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 05:39:59 +0000</pubDate>
		<dc:creator>Vincent</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[adrian colomitchi]]></category>
		<category><![CDATA[bezier-curve]]></category>
		<category><![CDATA[inflection point]]></category>

		<guid isPermaLink="false">http://polymathprogrammer.com/?p=2541</guid>
		<description><![CDATA[Adrian Colomitchi wrote me an email about an article I wrote. It turns out that I was wrong about the inflection point section. I was trying to figure out what Timo meant by &#8220;loop tips&#8221;, and I figured it could be an inflection point. So here&#8217;s how Adrian describes inflection points: If you drive or [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.caffeineowl.com/personal/index.html">Adrian Colomitchi</a> wrote me an email about <a href="http://polymathprogrammer.com/2010/07/01/nicely-shaped-reverse-engineered-bezier-curve/">an article I wrote</a>. It turns out that I was wrong about the inflection point section. I was trying to figure out what Timo meant by &#8220;loop tips&#8221;, and I figured it could be an inflection point.</p>
<p>So here&#8217;s how Adrian describes inflection points:</p>
<blockquote><p>If you drive or ride a bicycle: inflection points will happen when you switch your direction from left-to-right or vice-versa.<br />
The inflexion points have 0-curvature (and infinite radius)&#8230; for an instant you travel straight (because you are switching the direction).<br />
By contrast, the tip points will have maximum curvature.</p></blockquote>
<p>Here&#8217;s how the inflection points should actually look like (screenshots taken from Adrian&#8217;s site):</p>
<p>A Bezier curve with no inflection points:<br />
<img src="http://polymathprogrammer.com/images/blog/201102/beziernoinflection.png" alt="Bezier curve with no inflection points" /></p>
<p>A Bezier curve with one inflection point:<br />
<img src="http://polymathprogrammer.com/images/blog/201102/bezieroneinflection.png" alt="Bezier curve with one inflection point" /></p>
<p>A Bezier curve with two inflection points:<br />
<img src="http://polymathprogrammer.com/images/blog/201102/beziertwoinflection.png" alt="Bezier curve with two inflection points" /></p>
<p>You can find out more about <a href="http://www.caffeineowl.com/graphics/2d/vectorial/cubic-inflexion.html">inflection points on a cubic Bezier curve</a> on Adrian&#8217;s website.</p>
<p>If you have anything to add about inflection points (or Bezier curves), comment below.</p>
]]></content:encoded>
			<wfw:commentRss>http://polymathprogrammer.com/2011/02/06/bezier-curve-inflection-points/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elevator waiting maths</title>
		<link>http://polymathprogrammer.com/2011/01/10/elevator-waiting-maths/</link>
		<comments>http://polymathprogrammer.com/2011/01/10/elevator-waiting-maths/#comments</comments>
		<pubDate>Mon, 10 Jan 2011 04:16:41 +0000</pubDate>
		<dc:creator>Vincent</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[elevator]]></category>
		<category><![CDATA[james handley]]></category>
		<category><![CDATA[john cook]]></category>
		<category><![CDATA[lift]]></category>
		<category><![CDATA[maths]]></category>
		<category><![CDATA[minimum distance]]></category>
		<category><![CDATA[wait]]></category>

		<guid isPermaLink="false">http://polymathprogrammer.com/?p=2338</guid>
		<description><![CDATA[John Cook wrote an article on where to wait for an elevator. Where do you wait so you walk the minimum distance to the elevator? Read his article for the full explanation and solution. Here&#8217;s a pictorial summary of the solution: The problem was from a paper written by James Handley (and collaborated with others). [...]]]></description>
			<content:encoded><![CDATA[<p>John Cook wrote an article on <a href="http://www.johndcook.com/blog/2010/11/29/where-to-wait-for-an-elevator/">where to wait for an elevator</a>. Where do you wait so you walk the minimum distance to the elevator? Read his article for the full explanation and solution. Here&#8217;s a pictorial summary of the solution:</p>
<p><img src="http://polymathprogrammer.com/images/blog/201101/elevatorwaitingpositions.png" alt="Elevator optimum waiting position" /></p>
<p>The problem was from a <a href="http://www.medicine.mcgill.ca/epidemiology/hanley/Reprints/median-elevator.pdf">paper written by James Handley</a> (and collaborated with others). Basically, you don&#8217;t wait at the average position among all the elevators. You wait at the elevator with the median position.</p>
<p>In reality, however, you still have to enter the space where the elevators are. Therefore, the best position with the minimum distance to walk is</p>
<blockquote><p>to remain at the point of entry, and to not move at all!</p></blockquote>
<p>That of course, assumes that the elevators had been activated to arrive at your floor. You know, you need to push the button. <strong>How do you push that button if you&#8217;re 5 metres away?</strong> With a long pole? Other people? Telekinesis?</p>
<p>I have a more in-depth discussion on this in the <a href="http://polymathprogrammer.com/2011/01/01/singularity-january-2011/">January 2011 issue of Singularity</a>. Go download it and read it. It&#8217;s free.</p>
<p>Here&#8217;s something to think about. James Handley is a mathematician. What&#8217;s he doing in the faculty of medicine? Because he&#8217;s helping with epidemiology. If you&#8217;re not familiar with the word, &#8220;epidemiology&#8221; means the study of epidemics. The spread of viruses, rates of infection and the like.</p>
<p>This reminds me of the time when my thesis mentor (back in my university days) suggested I work in the field of epidemiology. Because <a href="http://polymathprogrammer.com/2008/03/05/computer-virus-behaviour-thesis/">my thesis dealt with computer virus behaviour</a>. Oh my god, I just looked at my thesis again&#8230; oh dear, ordinary differential equations! *closes thesis hurriedly*</p>
]]></content:encoded>
			<wfw:commentRss>http://polymathprogrammer.com/2011/01/10/elevator-waiting-maths/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

