<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" gd:etag="W/&quot;Ak8MRn04eip7ImA9WhBWEkk.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962</id><updated>2013-04-06T13:48:07.332+02:00</updated><title>Bannalia: trivial notes on themes diverse</title><subtitle type="html" /><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>102</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/BannaliaTrivialNotesOnThemesDiverse" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="bannaliatrivialnotesonthemesdiverse" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">BannaliaTrivialNotesOnThemesDiverse</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><entry gd:etag="W/&quot;A0IHRXc_fSp7ImA9WxJWEEQ.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-604070549426781380</id><published>2009-06-15T22:20:00.002+02:00</published><updated>2009-06-15T22:58:54.945+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-06-15T22:58:54.945+02:00</app:edited><title>60/40 split</title><content type="html">&lt;p style="text-align: justify;"&gt;Tim Harford recounts in his book &lt;a style="font-style: italic;" href="http://timharford.com/logicoflife/"&gt;The Logic of Life&lt;/a&gt; an anecdote about the authors of &lt;a style="font-style: italic;" href="http://www.freakonomicsbook.com/"&gt;Freakonomics&lt;/a&gt;, Steven D. Levitt and Stephen J. Dubner: when discussing the terms of their collaboration for writing the book, Levitt stated that he wouldn't go for less than a 60/40 split on revenues; Dubner, on the other hand, would settle for no less than 60/40 either. The apparent confrontation vanished when they realized that each was meaning &lt;span style="font-style: italic;"&gt;the other&lt;/span&gt; to get 60%, so they decided to join the venture.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Charming as it may sound, this story cannot be true: if I'm settling for 40% of the revenues and my partner proposes a 60/40 split, the proposal immediately fits my goals no matter whom I assume will get each part of the split.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/604070549426781380/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=604070549426781380" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/604070549426781380?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/604070549426781380?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2009/06/6040-split.html" title="60/40 split" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>1</thr:total></entry><entry gd:etag="W/&quot;CkYAQXk9eip7ImA9WxVUF0U.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-408856239886034984</id><published>2009-03-23T04:49:00.000+01:00</published><updated>2009-03-23T04:49:00.762+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-03-23T04:49:00.762+01:00</app:edited><title>About break statements</title><content type="html">&lt;p style="text-align: justify;"&gt;In a recent &lt;a href="http://dobbscodetalk.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=1095&amp;amp;Itemid="&gt;article&lt;/a&gt;, Andrew Koenig argues that &lt;code&gt;break&lt;/code&gt; statements are harmful because they make it harder to deduce the program context at loop termination. For instance, given:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;while (n != 0) { /* &lt;em&gt;do something&lt;/em&gt; */ }&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;code&gt;n&lt;/code&gt; can be assumed to be zero at loop termination &lt;span style="font-style: italic;"&gt;unless&lt;/span&gt; there is a &lt;code&gt;break &lt;/code&gt; within it. Koenig proposes two different approaches to alleviating this problem:&lt;p&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Include the program context at the &lt;code&gt;break&lt;/code&gt; statement as a possible value for the program context at loop termination.&lt;/li&gt;&lt;li&gt;Force the loop termination condition just before &lt;code&gt;break&lt;/code&gt;ing.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="text-align: justify;"&gt;There is another alternative: exclude the loop termination condition from the evaluable program context. Here is a way to do that:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;for (int local_n = n; local_n != 0;) { /* do something with local_n */ }&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;As the scope of &lt;code&gt;local_n&lt;/code&gt; does not extend outside the loop, program context after loop termination does not depend on the termination procedure. A cruder, but also more flexible way to do the same is the following:&lt;blockquote&gt;&lt;pre&gt;‎&lt;span style="font-size:85%;"&gt;{&lt;br /&gt;‎  int local_n = n;&lt;br /&gt;‎  while (local_n != 0) { /* do something with local_n */ }&lt;br /&gt;‎}&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;The somewhat unexpected surrounding braces are a conspicuous reminder that &lt;code&gt;local_n&lt;/code&gt; does not form part of the global program state.&lt;br /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/408856239886034984/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=408856239886034984" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/408856239886034984?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/408856239886034984?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2009/03/about-break-statements.html" title="About &lt;code&gt;break&lt;/code&gt; statements" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>1</thr:total></entry><entry gd:etag="W/&quot;A0UDSX08cSp7ImA9WxVUE00.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-3170605158255851237</id><published>2009-03-17T18:01:00.000+01:00</published><updated>2009-03-17T18:01:18.379+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-03-17T18:01:18.379+01:00</app:edited><title>A curious syntactic transformation</title><content type="html">&lt;p style="text-align: justify;"&gt;Consider the sentence&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;Rationalism has many followers.&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;What in principle looks like a rather dull N+V+O statement does not however allow for seeminlgy innocuous variations on the object:&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;*&lt;/span&gt;&lt;span style="font-style: italic;"&gt;Rationalism has many people.&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The reason why the former is valid while the latter is not is that followers are followers of something, in this particular case rationalism:&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;Rationalism has many followers [of rationalism].&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;In fact, we can view our sentence as a mere rewording of:&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;There are many followers of rationalism,&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;which leads us to hypothetize the existence of the following syntactic transformation:&lt;/p&gt;&lt;p style="text-align: center;"&gt;There is/are [Det] N of NP → NP has/have [Det] N.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;For instance:&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;There were lots of fans of the Beatles&lt;/span&gt;&lt;span style="font-style: italic;"&gt; → &lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;The Beatles had lots of fans&lt;/span&gt;&lt;span style="font-style: italic;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;There are no enemies of Rome&lt;/span&gt;&lt;span style="font-style: italic;"&gt; → &lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;Rome has no enemies&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;This transformational rule explains from a purely syntactical perspective why superficially similar sentences like &lt;span style="font-style: italic;"&gt;*&lt;/span&gt;&lt;span style="font-style: italic;"&gt;Rationalism has many people&lt;/span&gt; are invalid --they have no "There is..." equivalent.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Interestingly, the rule seems to operate in other languages apart from English (I presume that at least in most modern European languages):&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;El racionalismo tiene muchos seguidores.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Le rationalisme a beaucoup d'adeptes.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Rationalismus hat viele Anhänger.&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;This points to some general (maybe universal?) mechanism of reification of the &lt;a href="http://en.wikipedia.org/wiki/Possession_%28linguistics%29"&gt;possesion relationship&lt;/a&gt; between nouns.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/3170605158255851237/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=3170605158255851237" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/3170605158255851237?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/3170605158255851237?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2009/03/curious-syntactic-transformation.html" title="A curious syntactic transformation" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DU8DR3c4cCp7ImA9WxVbF0U.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-5448027529294301476</id><published>2009-03-10T19:50:00.002+01:00</published><updated>2009-04-03T21:44:36.938+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-04-03T21:44:36.938+02:00</app:edited><title>The mythical Bell Curve in Human Resources</title><content type="html">&lt;div style="text-align: justify;"&gt;Suppose a company's HR department wishes to establish a statistical model for their personnel performance in order to set up outcome predictions for the company's bonus system. It is all too easy to assume that performance will follow a normal distribution such as this:&lt;/div&gt;&lt;p style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_rqhF_8E1nlA/SbY35otybJI/AAAAAAAAAZ0/MVDcMQs0fM4/s1600-h/fig1.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 281px;" src="http://1.bp.blogspot.com/_rqhF_8E1nlA/SbY35otybJI/AAAAAAAAAZ0/MVDcMQs0fM4/s400/fig1.PNG" alt="" id="BLOGGER_PHOTO_ID_5311494273851616402" border="0" /&gt;&lt;/a&gt;&lt;span style="font-weight: bold;"&gt;Fig. 1: Performance distribution under the Bell Curve assumption.&lt;/span&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The assumption stems from the deeply held custom in Psychology and  Sociology of using the Bell Curve to model any a priori unknown human trait. Actually, if the company's hiring process is indeed efficient in selecting better than average people, this assumption is a complete contradiction.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Consider a simplistic hiring process in which performance is assesed by means of a test, so that applicants scoring some fixed minimum at the test are hired, and let us concede for the sake of the argument that the test is a perfect predictor of performance. The resulting performance distribution is depicted at the figure.&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_rqhF_8E1nlA/SbY3_yZvctI/AAAAAAAAAZ8/ZUsSqxlOSIU/s1600-h/fig2.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 281px;" src="http://3.bp.blogspot.com/_rqhF_8E1nlA/SbY3_yZvctI/AAAAAAAAAZ8/ZUsSqxlOSIU/s400/fig2.PNG" alt="" id="BLOGGER_PHOTO_ID_5311494379531104978" border="0" /&gt;&lt;/a&gt;&lt;span style="font-weight: bold;"&gt;Fig. 2: Performance distribution with a test-based selection process.&lt;/span&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The distribution has positive &lt;a href="http://mathworld.wolfram.com/Skewness.html"&gt;skewness&lt;/a&gt;, i.e. its right tail is longer than the left tail. So, the normal approximation with the same mean and variance (shown in dotted line) both overestimates low performers and underestimates high performers. It also underestimates low-to-normal performers and overestimates normal-to-high performers.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;In other hiring process scenario, the best among &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; candidates is selected. The resulting distribution is depicted at the following figure for &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; = 10.&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_rqhF_8E1nlA/SbY4DH1WC7I/AAAAAAAAAaE/qn5wmgqiuTk/s1600-h/fig3.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 281px;" src="http://3.bp.blogspot.com/_rqhF_8E1nlA/SbY4DH1WC7I/AAAAAAAAAaE/qn5wmgqiuTk/s400/fig3.PNG" alt="" id="BLOGGER_PHOTO_ID_5311494436823632818" border="0" /&gt;&lt;/a&gt;&lt;span style="font-weight: bold;"&gt;Fig. 3: Performance distribution with best-of-10 selection process.&lt;/span&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;We have positive skewness again, though not as marked as in the prvious case. Skewness grows as &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; does. Again, the normal approximation results in overestimation of low performers and underestimation of high performers.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Finally, we consider a two-stage hiring process where applicants are first filtered by a test and then the best candidate out of &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; is selected.&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_rqhF_8E1nlA/SbY4G515rmI/AAAAAAAAAaM/DaQ3ph_txkY/s1600-h/fig4.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 281px;" src="http://1.bp.blogspot.com/_rqhF_8E1nlA/SbY4G515rmI/AAAAAAAAAaM/DaQ3ph_txkY/s400/fig4.PNG" alt="" id="BLOGGER_PHOTO_ID_5311494501787348578" border="0" /&gt;&lt;/a&gt;&lt;span style="font-weight: bold;"&gt;Fig. 4: Performance distribution with test prefiltering and best-of-10 selection process.&lt;/span&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The test filter results in a slightly larger positive skewness. As in previous cases, normal approximation predicts more low performers and less high performers than the real case.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;To summarize: hiring process not only results in a personnel performance distribution with a higher than average mean (which is the primary purpose of any hiring process); the distribution will also have positive skewness, with more excellent and less deficient people than predicted by the Bell Curve.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/5448027529294301476/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=5448027529294301476" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/5448027529294301476?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/5448027529294301476?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2009/03/mythical-bell-curve-in-human-resources.html" title="The mythical Bell Curve in Human Resources" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/_rqhF_8E1nlA/SbY35otybJI/AAAAAAAAAZ0/MVDcMQs0fM4/s72-c/fig1.PNG" height="72" width="72" /><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;DUEGQH46eSp7ImA9WxRUGEk.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-1142458292063675512</id><published>2008-11-28T05:47:00.006+01:00</published><updated>2008-11-28T05:47:01.011+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-11-28T05:47:01.011+01:00</app:edited><title>More on price gas hysteresis</title><content type="html">&lt;div style="text-align: justify;"&gt;We redo the &lt;a href="http://bannalia.blogspot.com/2008/11/gas-price-hysteresis.html"&gt;hysteresis analysis&lt;/a&gt; we conducted on gas prices, now for a longer period of time and using much more data than before:&lt;/div&gt;&lt;ul style="text-align: justify;"&gt;&lt;li&gt;Average weekly retail prices without taxes of 95 octane gasoline and gasoil in Spain have been obtained from the &lt;a href="http://ec.europa.eu/energy/observatory/oil/bulletin_en.htm"&gt;European Commision Oil Bulletin&lt;/a&gt; (thanks to &lt;a href="http://wonkapistas.blogspot.com/"&gt;Wonka&lt;/a&gt; for the pointer).&lt;/li&gt;&lt;li&gt;The US Energy Information Administration provides daily &lt;a href="http://tonto.eia.doe.gov/dnav/pet/hist/rbrted.htm"&gt;spot prices of Brent oil&lt;/a&gt; in dollars per barrel.&lt;/li&gt;&lt;li&gt;Historical data on euro to dollar exchange rates can be retrieved from &lt;a href="http://www.oanda.com/convert/fxhistory"&gt;FXHistory&lt;/a&gt;.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="text-align: justify;"&gt;Using this information we have constructed the following figure showing the evolution of Brent prices and gasoline and gasoil Spanish retail prices without taxes, all in c€ per liter, between January 2006 and October 2008.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_rqhF_8E1nlA/SS1cZxbMNAI/AAAAAAAAAXE/jSRTnGpmd74/s1600-h/gas_prices.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 271px;" src="http://3.bp.blogspot.com/_rqhF_8E1nlA/SS1cZxbMNAI/AAAAAAAAAXE/jSRTnGpmd74/s400/gas_prices.png" alt="" id="BLOGGER_PHOTO_ID_5272972336554587138" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;And this is the scatter plot of Δ(gasoline price before taxes) against Δ(Brent price):&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_rqhF_8E1nlA/SS1eUh9pv5I/AAAAAAAAAXM/lXhCZADEfik/s1600-h/dgasoline_dbrent.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 395px; height: 400px;" src="http://3.bp.blogspot.com/_rqhF_8E1nlA/SS1eUh9pv5I/AAAAAAAAAXM/lXhCZADEfik/s400/dgasoline_dbrent.png" alt="" id="BLOGGER_PHOTO_ID_5272974445528072082" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The linear regressions of this plot for the semiplanes Δ(Brent price) ≥ 0 and ≤ 0 are:&lt;/p&gt;&lt;p style="text-align: center;"&gt;ΔBrent ≥ 0 → &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt; + &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; = 0.1854 + 0.1691&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;,&lt;br /&gt;ΔBrent ≤ 0 → &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt; + &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; = 0.1273 + 0.3636&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;On one hand, &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt; is more than twice as large as &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt;, which indicates that gasoline price follows oil price reductions much more strongly than oil price increases &lt;span style="font-style: italic;"&gt;when these variations are large&lt;/span&gt;; when oil price variations are small, it is the fact that both &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt; and &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt; are positive that dominates the situation: gasoline prices have a very clear bias towards increasing then. The "consumer unfavorable" region in which &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) &gt; −&lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;(−&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) is&lt;/p&gt;&lt;p style="text-align: center;"&gt;−1.61 c€/l &amp;lt; ΔBrent &amp;lt; 1.61 c€/l.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;As for gasoil, the plot look like this:&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_rqhF_8E1nlA/SS1ib8vdC_I/AAAAAAAAAXU/Ijr21OYetgg/s1600-h/dgasoil_dbrent.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 395px; height: 400px;" src="http://4.bp.blogspot.com/_rqhF_8E1nlA/SS1ib8vdC_I/AAAAAAAAAXU/Ijr21OYetgg/s400/dgasoil_dbrent.PNG" alt="" id="BLOGGER_PHOTO_ID_5272978971021872114" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;with regression lines&lt;/p&gt;&lt;p style="text-align: center;"&gt;ΔBrent ≥ 0 → &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt; + &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; = 0.2669 + 0.1269&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;,&lt;br /&gt;ΔBrent ≤ 0 → &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt; + &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; = 0.2391 + 0.4134&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Again, &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt; is much greater than &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt; (~3.25 times), but this is masked for small variations of Brent price by the constants &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt; and &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt; being greater than zero. The consumer unfavorable region is&lt;/p&gt;&lt;p style="text-align: center;"&gt;−1.77 c€/l &amp;lt; ΔBrent &amp;lt; 1.77 c€/l,&lt;/p&gt;&lt;p style="text-align: justify;"&gt;which is very similar to the consumer unfavorable region for gasoline.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;So, we have a somewhat mixed scenario: when oil prices do not vary much, there is a tendency for retail gas prices to increase, whereas when oil price variations are high, reductions are tracked much  more strongly than increases. We propose two explanations, both highly speculative, for this phenomenon:&lt;/p&gt;&lt;ul&gt;&lt;li style="text-align: justify;"&gt;Small variations of oil prices do not reach public attention, which allows retailers to silently increase gas prices; high variations in oil price, however, generally make it to the news, so that retailers are under greater public scrutiny when transferring these variations to the pump.&lt;/li&gt;&lt;li style="text-align: justify;"&gt;So as to soften the public negative reaction to price increments (and associated contractions in consumption), retailers do not reflect high increases in oil price as abruptly as oil price reductions, but opt to distribute the increment over a longer period of time; this would account both for the fact that &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt; &amp;lt; &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt; and for the positive bias in small price variations.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/1142458292063675512/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=1142458292063675512" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/1142458292063675512?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/1142458292063675512?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/11/more-on-price-gas-hysteresis.html" title="More on price gas hysteresis" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/_rqhF_8E1nlA/SS1cZxbMNAI/AAAAAAAAAXE/jSRTnGpmd74/s72-c/gas_prices.png" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;AkEMSH46eyp7ImA9WxRUFUg.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-3708517055057796571</id><published>2008-11-24T21:31:00.000+01:00</published><updated>2008-11-24T21:31:29.013+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-11-24T21:31:29.013+01:00</app:edited><title>Optimizing red-black tree color bits</title><content type="html">&lt;p style="text-align: justify;"&gt;C++ associative containers are almost universally implemented on top of a structure known as  a &lt;a href="http://en.wikipedia.org/wiki/Red-black_tree"&gt;&lt;span style="font-style: italic;"&gt;red-black tree&lt;/span&gt;&lt;/a&gt;. An rb tree node has approximately the following look:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎enum color_type{red=false,black=true};&lt;br /&gt;‎&lt;br /&gt;‎template&amp;lt;typename T&amp;gt;&lt;br /&gt;‎struct rb_node&lt;br /&gt;‎{&lt;br /&gt;‎  color_type color;&lt;br /&gt;‎  rb_node*   parent;&lt;br /&gt;‎  rb_node*   left;&lt;br /&gt;‎  rb_node*   right;&lt;br /&gt;‎  T          value;&lt;br /&gt;‎};&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;The color of a node, from which the data structure receives its name, is used to implement some algorithms to keep the tree balanced. Note that the information conveyed by &lt;code&gt;color&lt;/code&gt; is exactly one bit; yet, this member takes at least one &lt;code&gt;char&lt;/code&gt; of storage and, in most platforms, much more than that because of alignment issues. Increasingly more libraries implement an optimization technique that is able to get rid of the extra storage demanded by &lt;code&gt;color&lt;/code&gt; by embedding the information bit into one of the additional node fields. As this technique does not appear to be much documented on the Internet, this entry describes it in detail. Much of the material draws from the source code of &lt;a href="http://www.boost.org/libs/multi_index/doc/index.html"&gt;Boost.MultiIndex&lt;/a&gt;. Some acquantaince with &lt;a href="http://www.boost.org/libs/mpl/doc/index.html"&gt;Boost.MPL&lt;/a&gt; is welcome to understand what follows.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;To embed a bit into the &lt;span style="font-style: italic;"&gt;representation&lt;/span&gt; of a pointer we need to use in place of the pointer an unsigned integer type with the same size. In most 32bit platforms this type is &lt;code&gt;unsigned int&lt;/code&gt;, but the standard does not guarantee this, or even that such an integral type exists. We assume  that we have the following utilities at our disposal:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎typedef ... has_uintptr_type;&lt;br /&gt;‎typedef ... uintptr_type;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;where &lt;code&gt;has_uintptr_type&lt;/code&gt; is a &lt;a href="http://www.boost.org/libs/mpl/doc/refmanual/bool.html"&gt;Boost.MPL boolean&lt;/a&gt; indicating whether an unsigned integral type exists with exactly the same size as a pointer, and if so &lt;code&gt;uintptr_type&lt;/code&gt; is such type. We will provide later an implementation for these.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Alignment considerations dictate that many types must lie in memory addresses that are multiple of some small integer greater than 1; in particular, it is extremely usual that the alignment of a type &lt;code&gt;T&lt;/code&gt; is even (i.e. a multiple of 2), in which case the least significant bit of the representation of pointers to &lt;code&gt;T&lt;/code&gt; is always zero. And this is precisely the situation in which we can reuse this bit to embed the color information:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎template&amp;lt;typename T,bool ColorBitCompression=true&amp;gt;&lt;br /&gt;‎class rb_node:&lt;br /&gt;‎  public boost::mpl::if_c&amp;lt;&lt;br /&gt;‎    ColorBitCompression&amp;amp;&amp;amp;&lt;br /&gt;‎    has_uintptr_type::value&amp;amp;&amp;amp;&lt;br /&gt;‎    (boost::alignment_of&amp;lt;compressed_rb_node_header&amp;gt;::value%2==0),&lt;br /&gt;‎    compressed_rb_node_header,&lt;br /&gt;‎    regular_rb_node_header&lt;br /&gt;‎  &amp;gt;::type&lt;br /&gt;‎{&lt;br /&gt;‎  T value_;&lt;br /&gt;‎&lt;br /&gt;‎public:&lt;br /&gt;‎  T&amp;amp;       value(){return value_;}&lt;br /&gt;‎  const T&amp;amp; value()const{return value_;}&lt;br /&gt;‎};&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;&lt;code&gt;rb_node&lt;/code&gt; uses a header with the color bit impression technique when:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;This is requested by the user via &lt;code&gt;ColorBitCompression&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;There is an unsigned integer with which pointers can be represented.&lt;/li&gt;&lt;li&gt;The alignment of headers is even.&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p style="text-align: justify;"&gt;Otherwise a regular header is used. The implementation of the latter is entirely obvious:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎class regular_rb_node_header&lt;br /&gt;‎{&lt;br /&gt;‎  typedef regular_rb_node_header* pointer;&lt;br /&gt;‎&lt;br /&gt;‎  color_type                color_;&lt;br /&gt;‎  regular_rb_node_header*   parent_;&lt;br /&gt;‎  regular_rb_node_header*   left_;&lt;br /&gt;‎  regular_rb_node_header*   right_;&lt;br /&gt;‎&lt;br /&gt;‎public:&lt;br /&gt;‎  color_type&amp;amp; color(){return color_;}&lt;br /&gt;‎  color_type  color()const{return color_;}&lt;br /&gt;‎  pointer&amp;amp;    parent(){return parent_;}&lt;br /&gt;‎  pointer     parent()const{return parent_;}&lt;br /&gt;‎  pointer&amp;amp;    left(){return left_;}&lt;br /&gt;‎  pointer     left()const{return left_;}&lt;br /&gt;‎  pointer&amp;amp;    right(){return right_;}&lt;br /&gt;‎  pointer     right()const{return right_;}&lt;br /&gt;‎};&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;As for &lt;code&gt;compressed_rb_node_header&lt;/code&gt;, the most interesting part lie in the use of proxy classes &lt;code&gt;color_ref&lt;/code&gt; and &lt;code&gt;parent_ref&lt;/code&gt; to isolate the user from the representation of the color information and the parent pointer as a merged &lt;code&gt;uintptr_type&lt;/code&gt;:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎class compressed_rb_node_header&lt;br /&gt;‎{&lt;br /&gt;‎  typedef compressed_rb_node_header* pointer;&lt;br /&gt;‎  struct color_ref&lt;br /&gt;‎  {&lt;br /&gt;‎    color_ref(uintptr_type* r_):r(r_){}&lt;br /&gt;‎ &lt;br /&gt;‎    operator color_type()const&lt;br /&gt;‎    {&lt;br /&gt;‎      return color_type(*r&amp;amp;uintptr_type(1));&lt;br /&gt;‎    }&lt;br /&gt;‎ &lt;br /&gt;‎    color_ref&amp;amp; operator=(color_type c)&lt;br /&gt;‎    {&lt;br /&gt;‎      *r&amp;amp;=~uintptr_type(1);&lt;br /&gt;‎      *r|=uintptr_type(c);&lt;br /&gt;‎      return *this;&lt;br /&gt;‎    }&lt;br /&gt;‎ &lt;br /&gt;‎    color_ref&amp;amp; operator=(const color_ref&amp;amp; x)&lt;br /&gt;‎    {&lt;br /&gt;‎      return operator=(x.operator color_type());&lt;br /&gt;‎    }&lt;br /&gt;‎ &lt;br /&gt;‎  private:&lt;br /&gt;‎    uintptr_type* r;&lt;br /&gt;‎  };&lt;br /&gt;‎  struct parent_ref&lt;br /&gt;‎  {&lt;br /&gt;‎    parent_ref(uintptr_type* r_):r(r_){}&lt;br /&gt;‎ &lt;br /&gt;‎    operator pointer()const&lt;br /&gt;‎    {&lt;br /&gt;‎      return (pointer)(void*)(*r&amp;amp;~uintptr_type(1));&lt;br /&gt;‎    }&lt;br /&gt;‎ &lt;br /&gt;‎    parent_ref&amp;amp; operator=(pointer p)&lt;br /&gt;‎    {&lt;br /&gt;‎      *r=((uintptr_type)(void*)p)|(*r&amp;amp;uintptr_type(1));&lt;br /&gt;‎      return *this;&lt;br /&gt;‎    }&lt;br /&gt;‎ &lt;br /&gt;‎    parent_ref&amp;amp; operator=(const parent_ref&amp;amp; x)&lt;br /&gt;‎    {&lt;br /&gt;‎      return operator=(x.operator pointer());&lt;br /&gt;‎    }&lt;br /&gt;‎&lt;br /&gt;‎    pointer operator-&amp;gt;()const&lt;br /&gt;‎    {&lt;br /&gt;‎      return operator pointer();&lt;br /&gt;‎    }&lt;br /&gt;‎&lt;br /&gt;‎  private:&lt;br /&gt;‎    uintptr_type* r;&lt;br /&gt;‎  };&lt;br /&gt;‎&lt;br /&gt;‎  uintptr_type parentcolor_;&lt;br /&gt;‎  pointer      left_;&lt;br /&gt;‎  pointer      right_;&lt;br /&gt;‎&lt;br /&gt;‎public:&lt;br /&gt;‎  color_ref  color(){return color_ref(&amp;amp;parentcolor_);}&lt;br /&gt;‎  color_type color()const{return color_type(parentcolor_&amp;amp;std::size_t(1ul));}&lt;br /&gt;‎  parent_ref parent(){return parent_ref(&amp;amp;parentcolor_);}&lt;br /&gt;‎  pointer    parent()const&lt;br /&gt;‎    {return (pointer)(void*)(parentcolor_&amp;amp;~uintptr_type(1));}&lt;br /&gt;‎  pointer&amp;amp; left(){return left_;}&lt;br /&gt;‎  pointer  left()const{return left_;}&lt;br /&gt;‎  pointer&amp;amp; right(){return right_;}&lt;br /&gt;‎  pointer  right()const{return right_;}&lt;br /&gt;‎};&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;A complete &lt;a href="http://joaquinlopezmunoz.googlepages.com/cbitopt.cpp"&gt;example program&lt;/a&gt; is provided. In typical 32bit architectures, color bit compression makes the size of &lt;code&gt;rb_node&amp;lt;T&amp;gt;&lt;/code&gt; reduce by 4 bytes.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;This color bit compression technique introduces some penalty when accessing and modifying the color and parent information. This penalty, however, is entirely negligible. What is more, the reduction of size brought about by color bit compression usually results in a performance &lt;span style="font-style: italic;"&gt;speedup&lt;/span&gt; for associative containers taking advantage of this trick: as nodes are smaller, more of them can be stored in L1 cache, which accelerates execution noticeably.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;One final remark about this technique: as the parent pointer is no longer declared as a regular pointer, but rather represented by a masked integer, most C++ garbage collectors will not recognize the implicit pointer, which in principle can cause problems. All is fine, however, as it is only the parent that is masked: the right and left pointers are stored as genuine pointers, and as every node of a red/black tree is reachable only through left and right garbage collectors will not fail.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Thank you to Thorsten Ottosen for discussing this color bit compression technique with me.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/3708517055057796571/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=3708517055057796571" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/3708517055057796571?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/3708517055057796571?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/11/optimizing-red-black-tree-color-bits.html" title="Optimizing red-black tree color bits" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;CUMNSHs7eCp7ImA9WxRUEk0.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-5479685694739474641</id><published>2008-11-20T18:51:00.000+01:00</published><updated>2008-11-20T18:51:39.500+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-11-20T18:51:39.500+01:00</app:edited><title>Infinitely many voters</title><content type="html">&lt;p style="text-align: justify;"&gt;As we have discussed &lt;a href="http://bannalia.blogspot.com/2007/11/all-kings-philosophers-ultrafilters-and.html"&gt;before&lt;/a&gt;, the famous &lt;a href="http://en.wikipedia.org/wiki/Arrow%27s_impossibility_theorem"&gt;Arrow's Theorem&lt;/a&gt; is equivalent to this basic theorem on &lt;a href="http://planetmath.org/encyclopedia/NonPrincipalUltrafilter2.html"&gt;ultrafilters&lt;/a&gt;:&lt;/p&gt;&lt;p style="text-align: center;"&gt;Every ultrafilter on a finite set is principal.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;(See for instance Joel W. Robbin's &lt;a href="http://www.math.wisc.edu/%7Erobbin/ARROW.pdf"&gt;paper&lt;/a&gt; for a proof of this equivalence.) Basically, the set on which the ultrafilter is built represents the voters, and the member sets of an ultrafilter can be equated with &lt;span style="font-style: italic;"&gt;forcing coalitions&lt;/span&gt;, groups of voters which, if unanimous on a given preference, determine the overall result with respect to that preference. The theorem above says that every ultrafilter on a finite set of voters has a forcing coalition of size 1, i.e. a dictator.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;If we have an inifite set, however, the Axiom of Choice implies that there are non-principal ultrafilters: so if there were infinitely many voters we could devise (alas, nonconstructively) a fair voting system free of dictatorships. It is interesting to try to take advantage of this result to somehow circumvent the limitations for the finite case, and see what fails (something must fail after all):&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Let our voters be represented by the set &lt;span style="font-style: italic;"&gt;V&lt;/span&gt;={&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt;,...,&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;&lt;/sub&gt;}: we "extend" them by assigning to each &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt; an enumerable set of fictitious voters &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;1&lt;/sub&gt;, &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;2&lt;/sub&gt;,... Thus the set &lt;span style="font-style: italic;"&gt;V'&lt;/span&gt; of fictitious voters is infinite and we can have a non-principal ultrafilter &lt;span style="font-style: italic;"&gt;F&lt;/span&gt; on it. We design the following voting system: for any given preference the vote of each &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt; is obtained and assigned to the associated fictitious voters &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;ij&lt;/span&gt;&lt;/sub&gt;; if the overall resulting set of fictitious voters supporting the preference is a forcing coalition according to &lt;span style="font-style: italic;"&gt;F&lt;/span&gt;, the preference is approved, otherwise rejected.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;We already know that this voting system cannot be fair, and in fact it is easy to uncover the underlying flaw: the range of our mapping &lt;span style="font-style: italic;"&gt;f&lt;/span&gt; : &lt;span style="font-style: italic;"&gt;V&lt;/span&gt; → &lt;span style="font-style: italic;"&gt;V'&lt;/span&gt; is not the entire powerset of &lt;span style="font-style: italic;"&gt;V'&lt;/span&gt;, but only a finite subset of it (with cardinality 2&lt;sup&gt;&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;&lt;/sup&gt;); although &lt;span style="font-style: italic;"&gt;F &lt;/span&gt;is non-principal, it becomes principal (streching somewhat the meaning of the concept) when restricted to range(&lt;span style="font-style: italic;"&gt;f&lt;/span&gt;): &lt;span style="font-style: italic;"&gt;F&lt;/span&gt; necessarily contains then a pseudodictatorial forcing coalition comprising exactly all the fictitious voters associated to a single &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt;, who is the resulting dictator for the so devised voting system.&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/5479685694739474641/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=5479685694739474641" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/5479685694739474641?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/5479685694739474641?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/11/infinitely-many-voters.html" title="Infinitely many voters" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>3</thr:total></entry><entry gd:etag="W/&quot;CU4HSHg9eCp7ImA9WxRVGEs.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-4878615013986977817</id><published>2008-11-16T20:32:00.000+01:00</published><updated>2008-11-16T20:32:19.660+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-11-16T20:32:19.660+01:00</app:edited><title>State semantics</title><content type="html">&lt;p style="text-align: justify;"&gt;The following is a proposal for the classification of types in an imperative language according to the way the state of their objects changes over time.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Immutable.&lt;/span&gt; Objects of an immutable type cannot change state during their lifetime.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Stateful.&lt;/span&gt; On the other side of the spectrum, an object of a stateful type publish some methods that cause the object's internal state to change when executed.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Bound.&lt;/span&gt; (For lack of a better name.) The state of an object &lt;code&gt;x&lt;/code&gt; of a bound type remains immutable unless &lt;code&gt;x&lt;/code&gt; is explicitly given the same state as some &lt;code&gt;y&lt;/code&gt; through explicit assignment &lt;code&gt;x = y&lt;/code&gt;. A very popular example of a bound type is &lt;code&gt;java.lang.String&lt;/code&gt;. Although Java strings are commonly characterized as immutable, it is actually their associated states that are immutable, since an object of type &lt;code&gt;java.lang.String&lt;/code&gt; can be rebound through assignment. In C++, bound semantics are approximated by pointers to constant objects—C++ references, on the other hand, do not have bound semantics since they cannot be reassigned, that is, rebound.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;It turns out that bound types are sufficiently powerful to emulate stateful semantics using the following mapping technique: let &lt;code&gt;T&lt;/code&gt; be a stateful type and an associated mutable method&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;void T::f(args);&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;We construct an associated bound type &lt;code&gt;B&lt;sub&gt;T&lt;/sub&gt;&lt;/code&gt; whose internal state is represented by values of &lt;code&gt;T&lt;/code&gt;. We replace &lt;code&gt;T::f&lt;/code&gt; with the following immutable method:&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎B&lt;sub&gt;T&lt;/sub&gt; B&lt;sub&gt;T&lt;/sub&gt;::f(args)&lt;br /&gt;‎{&lt;br /&gt;‎  T state=this-&amp;gt;state;&lt;br /&gt;‎  state.f(args);&lt;br /&gt;‎  return B&lt;sub&gt;T&lt;/sub&gt;(state);&lt;br /&gt;‎} &lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;so that the expression&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎T x;&lt;br /&gt;‎...&lt;br /&gt;‎x.f(args);&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;can be rewritten with &lt;code&gt;B&lt;sub&gt;T&lt;/sub&gt;&lt;/code&gt; as&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎B&lt;sub&gt;T&lt;/sub&gt; x;&lt;br /&gt;‎...&lt;br /&gt;‎x=x.f(args);&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;And similarly we can provide a stateful façade to a bound type: let &lt;code&gt;T&lt;/code&gt; be a bound type with some method&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎T T::f(args);&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;We define a stateful type &lt;code&gt;S&lt;sub&gt;T&lt;/sub&gt;&lt;/code&gt; whose internal state is represented by values of &lt;code&gt;T&lt;/code&gt;, and implement the associated method &lt;code&gt;f&lt;/code&gt; as follows:&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎void S&lt;sub&gt;T&lt;/sub&gt;::f(args)&lt;br /&gt;‎{&lt;br /&gt;‎  this-&amp;gt;state=this-&amp;gt;state.f(args);&lt;br /&gt;‎}&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;In summary, bound and stateful types are to some extent equivalent, though practical considerations might favor one kind over the other depending on the use context. For instance, bound types are more amenable to &lt;a href="http://en.wikipedia.org/wiki/String_intern_pool"&gt;interning&lt;/a&gt; techniques and can be more easily equipped with locking mechanisms for use in multithreaded environments.&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/4878615013986977817/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=4878615013986977817" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/4878615013986977817?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/4878615013986977817?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/11/state-semantics.html" title="State semantics" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CUIFSXo-fSp7ImA9WxRVFEo.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-5894778975383878919</id><published>2008-11-12T08:04:00.001+01:00</published><updated>2008-11-12T08:05:18.455+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-11-12T08:05:18.455+01:00</app:edited><title>Gas price hysteresis</title><content type="html">&lt;p style="text-align: justify;"&gt;Due to the extreme rise of gas prices during the last couple of years, car users have become very sensitive (if not specially elastic) to the translation of oil price variations to retail gasoline prices. In particular, some have expressed the suspicion that increases in oil price are rapidly transferred to gas stations, whereas when the oil price decreases, this reflects less directly into a reduction of retail gas prices. Is this a case of actual price hysteresis or just consumer hysteria?&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The figure shows the evolution of Brent prices and average retail prices of 95 octane gasoline and gasoil (diesel) in Spain, all in c€/l (source: &lt;a href="http://www.cores.es/"&gt;Cores&lt;/a&gt;, via &lt;a href="http://wonkapistas.blogspot.com/2008/09/precios-de-las-gasolinas-y-del-petrleo.html"&gt;WonkaPistas&lt;/a&gt;).&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_rqhF_8E1nlA/SRgPEyb9r7I/AAAAAAAAAWA/jmL-NsLhV2I/s1600-h/gas_prices.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 323px;" src="http://3.bp.blogspot.com/_rqhF_8E1nlA/SRgPEyb9r7I/AAAAAAAAAWA/jmL-NsLhV2I/s400/gas_prices.png" alt="" id="BLOGGER_PHOTO_ID_5266976339142750130" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The final price of a liter of gasoline or gasoil in Spain can be expressed as&lt;/p&gt;&lt;p style="text-align: center;"&gt;final price = (1 + VAT) × (price before taxes + special tax),&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;where the applicable VAT is currently 16% and the special tax, known as the &lt;span style="font-style: italic;"&gt;impuesto especial sobre hidrocarburos&lt;/span&gt;, is approximately fixed. So, the variations on this price do not depend on this special tax:&lt;/p&gt;&lt;p style="text-align: center;"&gt;Δ(final price) = (1 + VAT)  × Δ(price before taxes).&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;These variations can be then attributed to the volatile part of the product cost, the most important of which is oil price. This is how Δ(gasoline price before taxes) plots against Δ(Brent price) monthly between February 2007 and August 2008:&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_rqhF_8E1nlA/SRmOjg8GMVI/AAAAAAAAAWo/84TwGttYjDg/s1600-h/dgasoline_dbrent.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 395px; height: 400px;" src="http://4.bp.blogspot.com/_rqhF_8E1nlA/SRmOjg8GMVI/AAAAAAAAAWo/84TwGttYjDg/s400/dgasoline_dbrent.png" alt="" id="BLOGGER_PHOTO_ID_5267397979975070034" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The dotted lines are the linear regressions of the points for Δ(Brent price) ≥ 0 and ≤ 0, respectively:&lt;/p&gt;&lt;p style="text-align: center;"&gt;ΔBrent ≥ 0 → &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt; + &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; = −0.5378 + 0.9885&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;,&lt;br /&gt;ΔBrent ≤ 0 → &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt; + &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; = 2.4610 + 1.4865&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;,&lt;/p&gt;&lt;p style="text-align: justify;"&gt;How can we interpret this? If the variations of gasoline price were symmetrical with respect to the sign of Δ(Brent price), we would have&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) = −&lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;(−&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;),&lt;br /&gt;&lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt; = &lt;span&gt;−&lt;/span&gt;&lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;,&lt;br /&gt;&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt; = &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;,&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;which is clearly not the case. In fact, we have that&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) &amp;gt; −&lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;(−&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) for &lt;span style="font-style: italic;"&gt;x&lt;/span&gt; in [0,3.86),&lt;/p&gt;&lt;p style="text-align: justify;"&gt;that is, variations in the price of the gasoline when 0 ≤ ΔBrent &amp;lt; 3.86 are larger than the corresponding reductions when − 3.86 &amp;lt; ΔBrent ≤ 0. In this interval, gasoline price can be said to present hysteresis unfavorable to the consumer. The situation is reversed when ΔBrent &amp;gt; 3.86, though this phenomenom is mainly accounted for by the presence of the outlier point at (−5.64,−5.41).&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;As for the variations on the price of gasoil before taxes, we have the following:&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_rqhF_8E1nlA/SRmPT2sdleI/AAAAAAAAAW4/U5hSEf_Cijk/s1600-h/dgasoil_dbrent.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 395px; height: 400px;" src="http://4.bp.blogspot.com/_rqhF_8E1nlA/SRmPT2sdleI/AAAAAAAAAW4/U5hSEf_Cijk/s400/dgasoil_dbrent.png" alt="" id="BLOGGER_PHOTO_ID_5267398810448795106" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;with regression lines&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;ΔBrent ≥ 0 → &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt; + &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;+&lt;/sub&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; = −0.7877 + 1.3841&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;,&lt;br /&gt;ΔBrent ≤ 0 → &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt; + &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;−&lt;/sub&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; = 2.4676 + 1.6557&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;,&lt;/p&gt;&lt;p style="text-align: justify;"&gt;resunting in an user unfavorable hysteresis interval −6.19 &amp;lt; ΔBrent &amp;lt; 6.19, which spans the entire dataset.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;In conclusion, only to some extent does the analysis corroborate the feeling many users share that oil companies do not reflect oil price reductions as faithfully as they do reflect increases.  These results are not very trustworthy since negative variations of oil price are underrepresented. Given the recent drop in the international oil market, it will be interesting to repeat the calculations when the data for the last two months become available.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/5894778975383878919/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=5894778975383878919" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/5894778975383878919?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/5894778975383878919?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/11/gas-price-hysteresis.html" title="Gas price hysteresis" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/_rqhF_8E1nlA/SRgPEyb9r7I/AAAAAAAAAWA/jmL-NsLhV2I/s72-c/gas_prices.png" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DkcBQHg4eip7ImA9WxRVF0k.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-4041594334148306472</id><published>2008-11-08T04:55:00.002+01:00</published><updated>2008-11-15T11:14:11.632+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-11-15T11:14:11.632+01:00</app:edited><title>Multiattribute querying with Boost.MultiIndex</title><content type="html">&lt;p style="text-align: justify;"&gt;We saw in a &lt;a href="http://bannalia.blogspot.com/2008/10/multicolumn-querying.html"&gt;prior entry&lt;/a&gt; how to define database indexes on a set of columns so that all possible queries involving clauses of the form &lt;span style="font-style: italic;"&gt;column&lt;sub&gt;i&lt;/sub&gt;&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;value&lt;sub&gt;i&lt;/sub&gt;&lt;/span&gt; are accelerated by some index. We see now how to model this situation for an in-memory C++ container, which gives us the opportunity to use &lt;a href="http://www.boost.org/libs/multi_index/"&gt;Boost.MultiIndex&lt;/a&gt; and show some template and preprocessor metaprogramming in action.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;So, suppose we have the following struct:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎struct element&lt;br /&gt;‎{&lt;br /&gt;‎  int x1,x2,x3,x4;&lt;br /&gt;‎};&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;and we are assigned the task of designing a container of &lt;code&gt;elements&lt;/code&gt;s along with efficient querying functions of the form:&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎typedef ... container;&lt;br /&gt;‎&lt;br /&gt;‎template&amp;lt;int N&amp;gt;&lt;br /&gt;‎struct field&lt;br /&gt;‎{&lt;br /&gt;‎  field(int x):x(x){}&lt;br /&gt;‎  int x;&lt;br /&gt;‎};&lt;br /&gt;‎&lt;br /&gt;‎template&amp;lt;int N0&amp;gt;&lt;br /&gt;‎const element* find(const container&amp;amp; c,const field&amp;lt;N0&amp;gt;&amp;amp; f0);&lt;br /&gt;‎&lt;br /&gt;‎...&lt;br /&gt;‎&lt;br /&gt;‎template&amp;lt;int N0,int N1,int N2,int N3&amp;gt;&lt;br /&gt;‎const element* find(&lt;br /&gt;‎  const container&amp;amp; c,&lt;br /&gt;‎  const field&amp;lt;N0&amp;gt;&amp;amp; f0,const field&amp;lt;N1&amp;gt;&amp;amp; f1,&lt;br /&gt;‎  const field&amp;lt;N2&amp;gt;&amp;amp; f2,const field&amp;lt;N3&amp;gt;&amp;amp; f2);&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;so that operations like the following are possible:&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎container c;&lt;br /&gt;‎...&lt;br /&gt;‎// x3==10 &amp;amp;&amp;amp; x1==20&lt;br /&gt;‎find(c,field&amp;lt;3&amp;gt;(10),field&amp;lt;1&amp;gt;(20));&lt;br /&gt;‎&lt;br /&gt;‎// x1==5 &amp;amp;&amp;amp; x4==40 &amp;amp;&amp;amp; x2==11&lt;br /&gt;‎find(c,field&amp;lt;1&amp;gt;(5),field&amp;lt;4&amp;gt;(40),field&amp;lt;2&amp;gt;(11));&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;Boost.MultiIndex is a natural choice to implement the container. As we already know, we need as many as six indices to &lt;a href="http://bannalia.blogspot.com/2008/10/generating-permutation-covers-part-ii.html"&gt;cover all the possible variations&lt;/a&gt;:&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎typedef member&amp;lt;element,int,&amp;amp;element::x1&amp;gt; key1;&lt;br /&gt;‎typedef member&amp;lt;element,int,&amp;amp;element::x2&amp;gt; key2;&lt;br /&gt;‎typedef member&amp;lt;element,int,&amp;amp;element::x3&amp;gt; key3;&lt;br /&gt;‎typedef member&amp;lt;element,int,&amp;amp;element::x4&amp;gt; key4;&lt;br /&gt;‎&lt;br /&gt;‎‎typedef multi_index_container&amp;lt;&lt;br /&gt;‎  element,&lt;br /&gt;‎  indexed_by&amp;lt;&lt;br /&gt;‎    ordered_non_unique&amp;lt;composite_key&amp;lt;element,key1,key3,key2,key4&amp;gt; &amp;gt;,&lt;br /&gt;‎    ordered_non_unique&amp;lt;composite_key&amp;lt;element,key4,key1,key3,key2&amp;gt; &amp;gt;,&lt;br /&gt;‎    ordered_non_unique&amp;lt;composite_key&amp;lt;element,key2,key1,key3,key4&amp;gt; &amp;gt;,&lt;br /&gt;‎    ordered_non_unique&amp;lt;composite_key&amp;lt;element,key4,key2,key1,key3&amp;gt; &amp;gt;,&lt;br /&gt;‎    ordered_non_unique&amp;lt;composite_key&amp;lt;element,key3,key2,key1,key4&amp;gt; &amp;gt;,&lt;br /&gt;‎    ordered_non_unique&amp;lt;composite_key&amp;lt;element,key4,key3,key2,key1&amp;gt; &amp;gt;&lt;br /&gt;‎  &amp;gt;&lt;br /&gt;‎&amp;gt; container;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;Actually, the combined &lt;a href="http://www.boost.org/libs/multi_index/doc/tutorial/key_extraction.html#composite_keys"&gt;composite keys&lt;/a&gt; are redundant, and we can shorten some of them. Also, we add a little instrumentation to the type for future use, so the final definition of &lt;code&gt;container&lt;/code&gt; is:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎struct key1:member&amp;lt;element,int,&amp;amp;element::x1&amp;gt;,boost::mpl::int_&amp;lt;1&amp;gt;{};&lt;br /&gt;‎struct key2:member&amp;lt;element,int,&amp;amp;element::x2&amp;gt;,boost::mpl::int_&amp;lt;2&amp;gt;{};&lt;br /&gt;‎struct key3:member&amp;lt;element,int,&amp;amp;element::x3&amp;gt;,boost::mpl::int_&amp;lt;3&amp;gt;{};&lt;br /&gt;‎struct key4:member&amp;lt;element,int,&amp;amp;element::x4&amp;gt;,boost::mpl::int_&amp;lt;4&amp;gt;{};&lt;br /&gt;‎&lt;br /&gt;‎template&amp;lt;int&amp;gt;struct cover;&lt;br /&gt;‎&lt;br /&gt;‎typedef multi_index_container&amp;lt;&lt;br /&gt;‎  element,&lt;br /&gt;‎  indexed_by&amp;lt;&lt;br /&gt;‎    ordered_non_unique&amp;lt;&lt;br /&gt;‎      tag&amp;lt;cover&amp;lt;1&amp;gt;,cover&amp;lt;13&amp;gt;,cover&amp;lt;123&amp;gt;,cover&amp;lt;1234&amp;gt; &amp;gt;,&lt;br /&gt;‎      composite_key&amp;lt;element,key1,key3,key2,key4&amp;gt;&lt;br /&gt;‎    &amp;gt;,&lt;br /&gt;‎    ordered_non_unique&amp;lt;&lt;br /&gt;‎      tag&amp;lt;cover&amp;lt;4&amp;gt;,cover&amp;lt;14&amp;gt;,cover&amp;lt;134&amp;gt; &amp;gt;,&lt;br /&gt;‎      composite_key&amp;lt;element,key4,key1,key3&amp;gt;&lt;br /&gt;‎    &amp;gt;,&lt;br /&gt;‎    ordered_non_unique&amp;lt;&lt;br /&gt;‎      tag&amp;lt;cover&amp;lt;2&amp;gt;,cover&amp;lt;12&amp;gt; &amp;gt;,&lt;br /&gt;‎      composite_key&amp;lt;element,key2,key1&amp;gt;&lt;br /&gt;‎    &amp;gt;,&lt;br /&gt;‎    ordered_non_unique&amp;lt;&lt;br /&gt;‎      tag&amp;lt;cover&amp;lt;24&amp;gt;,cover&amp;lt;124&amp;gt; &amp;gt;,&lt;br /&gt;‎      composite_key&amp;lt;element,key4,key2,key1&amp;gt;&lt;br /&gt;‎    &amp;gt;,&lt;br /&gt;‎    ordered_non_unique&amp;lt;&lt;br /&gt;‎      tag&amp;lt;cover&amp;lt;3&amp;gt;,cover&amp;lt;23&amp;gt; &amp;gt;,&lt;br /&gt;‎      composite_key&amp;lt;element,key3,key2&amp;gt;&lt;br /&gt;‎    &amp;gt;,&lt;br /&gt;‎    ordered_non_unique&amp;lt;&lt;br /&gt;‎      tag&amp;lt;cover&amp;lt;34&amp;gt;,cover&amp;lt;234&amp;gt; &amp;gt;,&lt;br /&gt;‎      composite_key&amp;lt;element,key4,key3,key2&amp;gt;&lt;br /&gt;‎    &amp;gt;&lt;br /&gt;‎  &amp;gt;&lt;br /&gt;‎&amp;gt; container;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;The &lt;code&gt;key&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/code&gt; &lt;a href="http://www.boost.org/libs/multi_index/doc/reference/key_extraction.html#key_extractors"&gt;extractor&lt;/a&gt; derives from &lt;code&gt;&lt;a href="http://www.boost.org/libs/mpl/doc/refmanual/int.html"&gt;boost::mpl::int_&lt;/a&gt;&amp;lt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&amp;gt;&lt;/code&gt;, which makes these types easily indexable. As for the &lt;code&gt;cover&lt;/code&gt; &lt;a href="http://www.boost.org/libs/multi_index/doc/tutorial/basics.html#tagging"&gt;tags&lt;/a&gt;, they simply indicate the different query formulas supported by each index using a straightforward coding technique: for instance, the second index covers the querying formulas&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎x4==v4 --&amp;gt; cover&amp;lt;4&amp;gt;&lt;br /&gt;‎x1==v1 &amp;amp;&amp;amp; x4==v4 --&amp;gt; &lt;/span&gt;&lt;span&gt;&lt;span style="font-size:85%;"&gt;cover&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;14&amp;gt;&lt;br /&gt;‎x1==v1 &amp;amp;&amp;amp; x3==v3 &amp;amp;&amp;amp; x4==v4 --&amp;gt; &lt;/span&gt;&lt;span&gt;&lt;span style="font-size:85%;"&gt;cover&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;134&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;To avoid ambiguities, cover numbers are coded assuming the attribute names appear in the order &lt;code&gt;x1&lt;/code&gt;, &lt;code&gt;x2&lt;/code&gt;, &lt;code&gt;x3&lt;/code&gt;, &lt;code&gt;x4&lt;/code&gt;, even if the tagged index does not respect this order in the specification of the composite key; also, when two different types cover the same formula, we assign the tag to only one of them. So, there are a total of 15 different tags, exactly as many as the number of different query formulas with up to 4 attributes. &lt;code&gt;cover_number&lt;/code&gt; does the metaprogrammatic work of calculating a cover number from an &lt;a href="http://www.boost.org/libs/mpl/doc/refmanual/forward-sequence.html"&gt;MPL sequence&lt;/a&gt; of &lt;code&gt;field&amp;lt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&amp;gt;&lt;/code&gt; types:&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎template&amp;lt;typename FieldSequence&amp;gt;&lt;br /&gt;‎struct cover_number:&lt;br /&gt;‎  boost::mpl::fold&amp;lt;&lt;br /&gt;‎    typename boost::mpl::sort&amp;lt;FieldSequence&amp;gt;::type,&lt;br /&gt;‎    boost::mpl::int_&amp;lt;0&amp;gt;,&lt;br /&gt;‎    boost::mpl::plus&amp;lt;&lt;br /&gt;‎      boost::mpl::times&amp;lt;boost::mpl::_1,boost::mpl::int_&amp;lt;10&amp;gt; &amp;gt;,&lt;br /&gt;‎      boost::mpl::_2&lt;br /&gt;‎    &amp;gt;&lt;br /&gt;‎  &amp;gt;::type&lt;br /&gt;‎{};&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;The following is a possible realization of the overload of &lt;code&gt;find&lt;/code&gt; with three fields:&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎template&amp;lt;int N0,int N1,int N2&amp;gt;&lt;br /&gt;‎const element* find(&lt;br /&gt;‎  const container&amp;amp; c,&lt;br /&gt;‎  const field&amp;lt;N0&amp;gt;&amp;amp; f0,const field&amp;lt;N1&amp;gt;&amp;amp; f1,const field&amp;lt;N2&amp;gt;&amp;amp; f2)&lt;br /&gt;‎{&lt;br /&gt;‎  // find the tag associated to the index conering our fields&lt;br /&gt;‎  typedef cover&amp;lt;&lt;br /&gt;‎    cover_number&amp;lt;&lt;br /&gt;‎      boost::mpl::vector_c&amp;lt;int,N0,N1,N2&amp;gt;&lt;br /&gt;‎    &amp;gt;::value&lt;br /&gt;‎  &amp;gt;                                                tag;&lt;br /&gt;‎&lt;br /&gt;‎  // retrieve the index type&lt;br /&gt;‎  typedef typename container::index&amp;lt;tag&amp;gt;::type     index_type;&lt;br /&gt;‎&lt;br /&gt;‎  // retrieve the type of the associated composite key extractor&lt;br /&gt;‎  typedef typename index_type::key_from_value      composite_key_type;&lt;br /&gt;‎&lt;br /&gt;‎  // an MPL sequence containing the field types passed&lt;br /&gt;‎  typedef boost::mpl::vector&amp;lt;&lt;br /&gt;‎    field&amp;lt;N0&amp;gt;,field&amp;lt;N1&amp;gt;,field&amp;lt;N2&amp;gt;&lt;br /&gt;‎  &amp;gt;                                                fields_type;&lt;br /&gt;‎&lt;br /&gt;‎  // get the index&lt;br /&gt;‎  const index_type&amp;amp;         i=c.get&amp;lt;tag&amp;gt;();&lt;br /&gt;‎&lt;br /&gt;‎  // pack the field values into a tuple...&lt;br /&gt;‎  boost::tuple&amp;lt;int,int,int&amp;gt; t=boost::make_tuple(f0.x,f1.x,f2.x);&lt;br /&gt;‎&lt;br /&gt;‎  // ...and use it with i.find(), rearranging the tuple&lt;br /&gt;‎  // so that each field&amp;lt;n&amp;gt; matches its key&amp;lt;n&amp;gt; in the composite key&lt;br /&gt;‎  typename index_type::iterator it=i.find(&lt;br /&gt;‎    boost::make_tuple(&lt;br /&gt;‎      get&amp;lt;composite_key_type,fields_type,boost::mpl::int_&amp;lt;0&amp;gt; &amp;gt;(t),&lt;br /&gt;‎      get&amp;lt;composite_key_type,fields_type,boost::mpl::int_&amp;lt;1&amp;gt; &amp;gt;(t),&lt;br /&gt;‎      get&amp;lt;composite_key_type,fields_type,boost::mpl::int_&amp;lt;2&amp;gt; &amp;gt;(t)&lt;br /&gt;‎    )&lt;br /&gt;‎  );&lt;br /&gt;‎  if(it!=i.end())return &amp;amp;*it;&lt;br /&gt;‎  else return 0;&lt;br /&gt;‎}&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;The only missing part is the &lt;code&gt;get&lt;/code&gt; template function, that accepts a composite key extractor type, an MPL sequence of fields, a position and a tuple of field values and returns the value associated to the &lt;code&gt;field&amp;lt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&amp;gt;&lt;/code&gt; with the same &lt;code&gt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/code&gt; as the &lt;code&gt;key&amp;lt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&amp;gt;&lt;/code&gt; at the indicated position in the composite key extractor:&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎template&amp;lt;&lt;br /&gt;‎  typename CompositeKey,typename FieldSequence,&lt;br /&gt;‎  typename Pos,typename Tuple&lt;br /&gt;‎&amp;gt;&lt;br /&gt;‎int get(const Tuple&amp;amp; t)&lt;br /&gt;‎{&lt;br /&gt;‎  typedef typename boost::tuples::element&amp;lt;&lt;br /&gt;‎    Pos::value,&lt;br /&gt;‎    typename CompositeKey::key_extractor_tuple&lt;br /&gt;‎  &amp;gt;::type key_at_pos;&lt;br /&gt;‎  const int M=&lt;br /&gt;‎    boost::mpl::distance&amp;lt;&lt;br /&gt;‎      typename boost::mpl::begin&amp;lt;FieldSequence&amp;gt;::type,&lt;br /&gt;‎      typename boost::mpl::find&amp;lt;&lt;br /&gt;‎        FieldSequence,&lt;br /&gt;‎        field&amp;lt;key_at_pos::value&amp;gt;&lt;br /&gt;‎      &amp;gt;::type&lt;br /&gt;‎    &amp;gt;::value;&lt;br /&gt;‎  return t.template get&amp;lt;M&amp;gt;();&lt;br /&gt;‎}&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;The code is simpler than it might appear at first glance: if the key at the position &lt;code&gt;Pos&lt;/code&gt; of &lt;code&gt;CompositeKey&lt;/code&gt; is of the form &lt;code&gt;key&amp;lt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;M&lt;/code&gt; is simply the distance from the beginning of &lt;code&gt;FieldSequence&lt;/code&gt; to the type &lt;code&gt;key&amp;lt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&amp;gt;&lt;/code&gt;. The expression &lt;code&gt;key_at_pos::value&lt;/code&gt; takes advantage of the fact that each &lt;code&gt;key&amp;lt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&amp;gt;&lt;/code&gt; is derived from &lt;span style="font-family:monospace;"&gt;boost::mpl::int_&lt;/span&gt;&lt;code&gt;&amp;lt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The different overloads of &lt;code&gt;find&lt;/code&gt; are entirely similar to the one we have just seen. &lt;a href="http://www.boost.org/libs/preprocessor/doc/index.html"&gt;Boost.Preprocessor&lt;/a&gt; allows us to generate the overloads without code repetition:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎#define FIND_PARAM_MACRO(z,n,data) \&lt;br /&gt;‎const field&amp;lt;BOOST_PP_CAT(N,n)&amp;gt;&amp;amp; BOOST_PP_CAT(f,n)&lt;br /&gt;‎&lt;br /&gt;‎#define FIND_FIELD_MACRO(z,n,data) field&amp;lt;BOOST_PP_CAT(N,n)&amp;gt;&lt;br /&gt;‎&lt;br /&gt;‎#define FIND_VALUE_MACRO(z,n,data) BOOST_PP_CAT(f,n).x&lt;br /&gt;‎&lt;br /&gt;‎#define FIND_GET_MACRO(z,n,data) \&lt;br /&gt;‎get&amp;lt;composite_key_type,fields_type,boost::mpl::int_&amp;lt;n&amp;gt; &amp;gt;(t)&lt;br /&gt;‎&lt;br /&gt;‎#define DEFINE_FIND(num_fields) \&lt;br /&gt;‎template&amp;lt;BOOST_PP_ENUM_PARAMS(num_fields,int N)&amp;gt; \&lt;br /&gt;‎const element* find( \&lt;br /&gt;‎  const container&amp;amp; c, \&lt;br /&gt;‎  BOOST_PP_ENUM(num_fields,FIND_PARAM_MACRO,~)) \&lt;br /&gt;‎{ \&lt;br /&gt;‎  typedef cover&amp;lt; \&lt;br /&gt;‎    cover_number&amp;lt; \&lt;br /&gt;‎      boost::mpl::vector_c&amp;lt;int, \&lt;br /&gt;‎        BOOST_PP_ENUM_PARAMS(num_fields,N) \&lt;br /&gt;‎      &amp;gt; \&lt;br /&gt;‎    &amp;gt;::value \&lt;br /&gt;‎  &amp;gt;                                                tag; \&lt;br /&gt;‎  typedef typename container::index&amp;lt;tag&amp;gt;::type     index_type; \&lt;br /&gt;‎  typedef typename index_type::key_from_value      composite_key_type; \&lt;br /&gt;‎  typedef boost::mpl::vector&amp;lt; \&lt;br /&gt;‎    BOOST_PP_ENUM(num_fields,FIND_FIELD_MACRO,~) \&lt;br /&gt;‎  &amp;gt;                                                fields_type; \&lt;br /&gt;‎ \&lt;br /&gt;‎  const index_type&amp;amp; i=c.get&amp;lt;tag&amp;gt;(); \&lt;br /&gt;‎  boost::tuple&amp;lt; \&lt;br /&gt;‎    BOOST_PP_ENUM_PARAMS(num_fields,int BOOST_PP_INTERCEPT) \&lt;br /&gt;‎  &amp;gt; t=boost::make_tuple( \&lt;br /&gt;‎    BOOST_PP_ENUM(num_fields,FIND_VALUE_MACRO,~) \&lt;br /&gt;‎  ); \&lt;br /&gt;‎  typename index_type::iterator it=i.find( \&lt;br /&gt;‎    boost::make_tuple( \&lt;br /&gt;‎      BOOST_PP_ENUM(num_fields,FIND_GET_MACRO,~) \&lt;br /&gt;‎    ) \&lt;br /&gt;‎  ); \&lt;br /&gt;‎  if(it!=i.end())return &amp;amp;*it; \&lt;br /&gt;‎  else return 0; \&lt;br /&gt;‎}&lt;br /&gt;‎&lt;br /&gt;‎DEFINE_FIND(1)&lt;br /&gt;‎DEFINE_FIND(2)&lt;br /&gt;‎DEFINE_FIND(3)&lt;br /&gt;‎DEFINE_FIND(4)&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;A complete &lt;a href="http://joaquinlopezmunoz.googlepages.com/attrquery.cpp"&gt;implementation program&lt;/a&gt; is provided.&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Although we have metaprogrammed the code for selecting and using the indices of an &lt;span style="font-style: italic;"&gt;n&lt;/span&gt;-attribute container, still the definition of &lt;code&gt;container&lt;/code&gt; was done manually. The following is a very tough challenge for the reader: program a &lt;a href="http://www.boost.org/libs/mpl/doc/refmanual/metafunction.html"&gt;metafunction&lt;/a&gt; that accepts an integer &lt;span style="font-style: italic;"&gt;n&lt;/span&gt; and produces a suitable &lt;code&gt;multi_index_container&lt;/code&gt; instantiation providing full query coverage for a struct with attributes &lt;code&gt;x1&lt;/code&gt;,...,&lt;code&gt;x&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/code&gt;. Note that solving this challenge implies metaprogramming the &lt;a href="http://bannalia.blogspot.com/2008/10/generating-permutation-covers-part-ii.html"&gt;permutation cover algorithm&lt;/a&gt; we devised some entries ago.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/4041594334148306472/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=4041594334148306472" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/4041594334148306472?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/4041594334148306472?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/11/multiattribute-querying-with.html" title="Multiattribute querying with Boost.MultiIndex" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CEUNQXw8eip7ImA9WxRVEE0.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-3647522428515129719</id><published>2008-11-04T01:01:00.002+01:00</published><updated>2008-11-06T21:11:30.272+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-11-06T21:11:30.272+01:00</app:edited><title>Filling up at dawn</title><content type="html">&lt;p style="text-align: justify;"&gt;I recently read in a free daily paper that it is advisable to fill up the car tank early in the morning rather than at noon because the gasoline will be usually cooler at that time of the day and thus denser, so we are pumping more grams of gas per liter paid. Does this really make a difference?&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The volumetric &lt;a href="http://en.wikipedia.org/wiki/Coefficient_of_thermal_expansion#Thermal_expansion_coefficients_for_some_common_materials"&gt;thermal expansion coefficient&lt;/a&gt; of gasoline is 950·10&lt;sup&gt;-6&lt;/sup&gt;/°C, which means that when the gasoline is colder we obtain an excess mass of&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;cold&lt;/sub&gt;&lt;span style="font-style: italic;"&gt; &lt;/span&gt;&lt;span&gt;&amp;minus;&lt;/span&gt;&lt;span style="font-style: italic;"&gt; &lt;/span&gt;&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;hot&lt;/sub&gt;&lt;span&gt;)&lt;/span&gt;/&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;hot&lt;/sub&gt; = 950·10&lt;sup&gt;-6&lt;/sup&gt;Δ&lt;span style="font-style: italic;"&gt;T&lt;/span&gt;.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;For a rather large Δ&lt;span style="font-style: italic;"&gt;T&lt;/span&gt; of 20 °C the gain would be around 2%. Take into account, however, that station tanks are buried a couple of meters underground, which damps the temperature oscillations experienced at the surface. This damping effect can be &lt;a href="http://soil.scijournals.org/cgi/content/full/68/3/784#BDY"&gt;estimated&lt;/a&gt; as:&lt;/p&gt;&lt;p style="text-align: center;"&gt;Δ&lt;span style="font-style: italic;"&gt;T&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;) = Δ&lt;span style="font-style: italic;"&gt;T&lt;/span&gt;&lt;sub&gt;surface&lt;/sub&gt;·&lt;span style="font-style: italic;"&gt;e&lt;/span&gt;&lt;sup&gt;−&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;/&lt;span style="font-style: italic;"&gt;D&lt;/span&gt;&lt;/sup&gt;,&lt;br /&gt;&lt;span style="font-style: italic;"&gt;D&lt;/span&gt; = √(2&lt;span style="font-style: italic;"&gt;K&lt;/span&gt;/&lt;span style="font-style: italic;"&gt;ω&lt;/span&gt;),&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;where &lt;span style="font-style: italic;"&gt;K&lt;/span&gt; is the soil &lt;a href="http://en.wikipedia.org/wiki/Thermal_diffusivity"&gt;thermal diffusivity&lt;/a&gt;, with &lt;a href="http://soil.scijournals.org/cgi/content/full/65/6/1641/FIG2"&gt;typical values&lt;/a&gt; for natural soils ranging from 0.2 to 0.8 10&lt;sup&gt;-6&lt;/sup&gt;m&lt;sup&gt;2&lt;/sup&gt;/s. Setting &lt;span style="font-style: italic;"&gt;z&lt;/span&gt; = 2 m and &lt;span style="font-style: italic;"&gt;ω&lt;/span&gt; = 2&lt;span style="font-style: italic;"&gt;π&lt;/span&gt; rad/day, we have&lt;/p&gt;&lt;p style="text-align: center;"&gt;1.9·10&lt;sup&gt;-12&lt;/sup&gt;Δ&lt;span style="font-style: italic;"&gt;T&lt;/span&gt;&lt;sub&gt;surface&lt;/sub&gt; ≤ Δ&lt;span style="font-style: italic;"&gt;T&lt;/span&gt;&lt;sub&gt;station tank&lt;/sub&gt; ≤ 1.4·10&lt;sup&gt;-6&lt;/sup&gt;Δ&lt;span style="font-style: italic;"&gt;T&lt;/span&gt;&lt;sub&gt;surface&lt;/sub&gt;, &lt;/p&gt;&lt;p style="text-align: justify;"&gt;that is, the temperature of the station tank is basically constant across the day. So, filling up the car tank in the cool hours of the day does not seem to make any difference.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;There is a measurable gain, though, when we compare car tank filling during different seasons of the year. Here, the temperature oscillations are higher and, most importantly, the damping effect of the soil is much lesser. Taking&lt;/p&gt;&lt;p style="text-align: center;"&gt;Δ&lt;span style="font-style: italic;"&gt;T&lt;/span&gt;&lt;sub&gt;surface&lt;/sub&gt; = 30 °C,&lt;br /&gt;&lt;span style="font-style: italic;"&gt;z&lt;/span&gt; = 2 m,&lt;br /&gt;&lt;span style="font-style: italic;"&gt;ω&lt;/span&gt; = 2&lt;span style="font-style: italic;"&gt;π&lt;/span&gt; rad/year,&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;we have&lt;/p&gt;&lt;p style="text-align: center;"&gt;7.3 °C ≤ Δ&lt;span style="font-style: italic;"&gt;T&lt;/span&gt;&lt;sub&gt;station tank&lt;/sub&gt; ≤ 14.8 °C, &lt;/p&gt;&lt;p style="text-align: justify;"&gt;which translates to&lt;/p&gt;&lt;p style="text-align: center;"&gt; 0.7% ≤ &lt;span&gt;(&lt;/span&gt;&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;cold&lt;/sub&gt;&lt;span style="font-style: italic;"&gt; &lt;/span&gt;&lt;span&gt;&amp;minus;&lt;/span&gt;&lt;span style="font-style: italic;"&gt; &lt;/span&gt;&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;hot&lt;/sub&gt;&lt;span&gt;)&lt;/span&gt;/&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;hot&lt;/sub&gt; &lt;sub&gt;t&lt;/sub&gt; ≤ 1.4%.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The potential impact as a cost-saving measure is, in any case, very unspectacular.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/3647522428515129719/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=3647522428515129719" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/3647522428515129719?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/3647522428515129719?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/11/filling-up-at-dawn.html" title="Filling up at dawn" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DUYBRXY4fip7ImA9WxNRFEo.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-6379480381353752288</id><published>2008-10-31T07:35:00.001+01:00</published><updated>2009-09-09T08:19:14.836+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-09T08:19:14.836+02:00</app:edited><title>C preprocessor tail recursion</title><content type="html">&lt;p style="text-align: justify;"&gt;The C preprocessor gives the false impression of being functional in nature, but its capacities are more limited than expected; for instance, function reentrancy does not work: consider the following attempt at recursively defining the factorial function using &lt;a href="http://www.boost.org/libs/preprocessor/doc/index.html"&gt;Boost.Preprocessor&lt;/a&gt;:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎#define FACTORIAL(n) \&lt;br /&gt;‎BOOST_PP_IF( \&lt;br /&gt;‎  n, \&lt;br /&gt;‎  BOOST_PP_MUL(n,FACTORIAL(BOOST_PP_DEC(n))), \&lt;br /&gt;‎  1)&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;The expression &lt;code&gt;FACTORIAL(5)&lt;/code&gt; does not expand to &lt;code&gt;120&lt;/code&gt;, however, but to&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎BOOST_PP_TUPLE_ELEM_3_0 BOOST_PP_IIF_BOOST_PP_BOOL_FACTORIAL(4)(&lt;br /&gt;‎BOOST_PP_WHILE_2, (0, 5, FACTORIAL(4)) BOOST_PP_TUPLE_EAT_3)(BOO&lt;br /&gt;‎ST_PP_MUL_P, BOOST_PP_MUL_O, (5, 5, BOOST_PP_DEC_FACTORIAL(4)))&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;The rules of the C preprocessor mandate that during the expansion of the function macro &lt;code&gt;FACTORIAL&lt;/code&gt; internal occurrences of this token are not further expanded, thus ruling out the very core of recursion. A simpler example illustrates this:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎#define A B&lt;br /&gt;‎#define B A&lt;br /&gt;‎&lt;br /&gt;‎A&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;The last line of the snippet above does not enter into infinite recursion, but simply causes the expansion rules &lt;code&gt;A&lt;/code&gt;→&lt;code&gt;B&lt;/code&gt;→&lt;code&gt;A&lt;/code&gt; to be applied, stopping here because &lt;code&gt;A&lt;/code&gt; is met inside the expansion of &lt;code&gt;A&lt;/code&gt; itself.&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Despite these serious limitations, Boost.Preprocessor is able to provide constructs for bounded iteration implemented with very sophisticated techniques. Let us see how to use &lt;a href="http://www.boost.org/libs/preprocessor/doc/ref/while.html"&gt;&lt;code&gt;BOOST_PP_WHILE&lt;/code&gt;&lt;/a&gt; to simulate &lt;a href="http://en.wikipedia.org/wiki/Tail_recursion"&gt;&lt;span style="font-style: italic;"&gt;tail recursion&lt;/span&gt;&lt;/a&gt;, a special form of recursion in which recursivity is applied exactly at the end of the recursive function body. The usual formulation of the factorial function is not tail recursive:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎int factorial(int n)&lt;br /&gt;‎{&lt;br /&gt;‎  if(n&amp;gt;0)return n*factorial(n-1);&lt;br /&gt;‎  else   return 1;&lt;br /&gt;‎}&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;But it can be rewritten to be so as follows:&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎int factorial(int n,int res=1)&lt;br /&gt;‎{&lt;br /&gt;‎  if(n&amp;gt;0)return factorial(n-1,n*res);&lt;br /&gt;‎  else   return res;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;p&gt;&lt;/p&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;The idea is that the recursive function passes all the relevant contextual information as part of the recursive call, thus delegating the work that otherwise should be done after the recursion returns; in this way, recursing (except when terminating recursion) is exactly the last thing that the function does. Let us know apply the following trasformation &lt;span style="font-style: italic;"&gt;f&lt;/span&gt; → &lt;span style="font-style: italic;"&gt;f'&lt;/span&gt; to a tail recursive function &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;:&lt;br /&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;If &lt;span style="font-style: italic;"&gt;f&lt;/span&gt; stops and returns res, &lt;span style="font-style: italic;"&gt;f'&lt;/span&gt; returns the sequence (&lt;span style="font-weight: bold;"&gt;stop&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;res&lt;/span&gt;).&lt;br /&gt;&lt;/li&gt;&lt;li&gt;If &lt;span style="font-style: italic;"&gt;f&lt;/span&gt; recursively calls &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;args&lt;/span&gt;), &lt;span style="font-style: italic;"&gt;f'&lt;/span&gt; returns the sequence (&lt;span style="font-weight: bold;"&gt;iterate&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;args&lt;/span&gt;).&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="text-align: justify;"&gt;That is, &lt;span style="font-style: italic;"&gt;f'&lt;/span&gt; does not recurse but instead returns the computation state if recursion is to occur, or the final return value otherwise. The following pseudocode illustrates the transformation process for &lt;code&gt;factorial&lt;/code&gt;:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎factorial_body(int n,int res=1)&lt;br /&gt;‎{&lt;br /&gt;‎  if(n&amp;gt;0)return (iterate,n-1,n*res);&lt;br /&gt;‎  else   return (stop,res);&lt;br /&gt;‎}&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;span style="font-style: italic;"&gt;f'&lt;/span&gt; is no longer recursive, but it can be used to compute &lt;span style="font-style: italic;"&gt;f&lt;/span&gt; as follows:&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎recurse(body,args)&lt;br /&gt;‎{&lt;br /&gt;‎  state=(iterate,args);&lt;br /&gt;‎  while(state[0]!=stop){&lt;br /&gt;‎    state=body(state.args);&lt;br /&gt;‎  }&lt;br /&gt;‎}&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;The interesting fact about this process is that recursion (and so reentrancy)  has been eliminated in favor of iteration: &lt;span style="font-style: italic;"&gt;f'&lt;/span&gt; is called repeatedly until it signals termination. We can very easily implement this idea using Boost.Preprocessor:&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎#define RECURSE(body,args) \&lt;br /&gt;‎RECURSE_RES(BOOST_PP_WHILE(RECURSE_PRED,RECURSE_OP,(body)ITERATE args))&lt;br /&gt;‎&lt;br /&gt;‎#define RECURSE_PRED(d,state) \&lt;br /&gt;‎BOOST_PP_SEQ_ELEM(1,state)&lt;br /&gt;‎&lt;br /&gt;‎#define RECURSE_OP(d,state) \&lt;br /&gt;‎(RECURSE_BODY(state))RECURSE_BODY(state)(BOOST_PP_SEQ_REST_N(2,state))&lt;br /&gt;‎&lt;br /&gt;‎#define RECURSE_BODY(state) \&lt;br /&gt;‎BOOST_PP_SEQ_ELEM(0,state)&lt;br /&gt;‎&lt;br /&gt;‎#define RECURSE_RES(state) \&lt;br /&gt;‎BOOST_PP_SEQ_ELEM(2,state)&lt;br /&gt;‎&lt;br /&gt;‎#define STOP (0)&lt;br /&gt;‎&lt;br /&gt;‎#define ITERATE (1)&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;which allows us to implement our factorial macro in a reasonably straightforward manner:&lt;/div&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎‎#define FACTORIAL(n) RECURSE(FACTORIAL_BODY,(n)(1))&lt;br /&gt;‎&lt;br /&gt;‎#define FACTORIAL_BODY(args) \&lt;br /&gt;‎FACTORIAL_BODY_IMPL( \&lt;br /&gt;‎  BOOST_PP_SEQ_ELEM(0,args),BOOST_PP_SEQ_ELEM(1,args))&lt;br /&gt;‎&lt;br /&gt;‎#define FACTORIAL_BODY_IMPL(n,res) \&lt;br /&gt;‎BOOST_PP_IF( \&lt;br /&gt;‎  n, \&lt;br /&gt;‎  ITERATE(BOOST_PP_DEC(n))(BOOST_PP_MUL(n,res)), \&lt;br /&gt;‎  STOP(res))&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;Arguments are packed using &lt;a href="http://www.boost.org/libs/preprocessor/doc/data/sequences.html"&gt;Boost.Preprocessor sequences&lt;/a&gt;. A &lt;a href="http://joaquinlopezmunoz.googlepages.com/cpptailrec.c"&gt;complete implementation&lt;/a&gt; program is provided. Unfortunately, the process seems to be very onerous for common implementations of the C preprocessor, as internal limits are generally reached with arguments greater than 5. This problem could deserve further investigation in a later entry.&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/6379480381353752288/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=6379480381353752288" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/6379480381353752288?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/6379480381353752288?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/10/c-preprocessor-tail-recursion.html" title="C preprocessor tail recursion" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;CEQFSX4zfip7ImA9WxRWEEQ.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-3806746697072195731</id><published>2008-10-27T03:58:00.001+01:00</published><updated>2008-10-27T08:25:18.086+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-27T08:25:18.086+01:00</app:edited><title>Law-abiding miracles</title><content type="html">&lt;p style="text-align: justify;"&gt;Newton believed that, if left to the action of gravitational forces alone, the Solar System must be unstable, and only the continuous action of God, counteracting the small perturbations here and there, &lt;a href="http://books.google.es/books?id=KaAIAAAAIAAJ&amp;amp;pg=PA504&amp;amp;dq=%22The+six+primary+planets+are+revolved+about+the+sun%22+%22This+most+beautiful+system%22+%22could+only+proceed+from+the+counsel+and+dominion+of+an+intelligent+and+powerful+Being.%22&amp;amp;ei=fQ4CSczfD4OQsgPJ29XVDA"&gt;keeps the system together&lt;/a&gt; and wonderfully regular:&lt;/p&gt;&lt;blockquote style="text-align: justify;"&gt;The six primary planets are revolved about the sun in circles concentric with the sun, and with motions directed towards the same parts, and almost in the same plane. Ten moons are revolved about the earth, Jupiter and Saturn, in circles concentric with them, with the same direction of motion, and nearly in the planes of the orbits of those planets; but it is not to be conceived that mere mechanical causes could give birth to so many regular motions[...] This most beautiful system of the sun, planets, and comets could only proceed from the counsel and dominion of an intelligent and powerful Being.&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;This curious picture of God as a careful supervisor of heavenly motions would only be compatible with the same physical laws that Newton crafted if God Himself would possess a physical body with mass and energy (and large quantities of them, for that matter): classical (and relativistic) mechanics is &lt;span style="font-style: italic;"&gt;deterministic&lt;/span&gt; and does not allow for any degree of freedom that a deity could take advantage of to direct the evolution of a physical system one way or another. In this sense, the action of God as a stabilizer of the Solar System is a sustained &lt;span style="font-style: italic;"&gt;miracle&lt;/span&gt;, a physical phenomenon incompatible with the laws of physics. This very definition of miracle is logically contradictory to begin with, at least given the premise of a deterministic universe.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;On the other hand, if we consider non-deterministic physical theories, there is room for a concept that we could aptly name "law-abiding miracle": for instance, &lt;a href="http://en.wikipedia.org/wiki/Quantum_tunneling"&gt;quantum mechanics tunneling&lt;/a&gt; allows for the occurence of phenomena that we generally deem impossible, such as wall crossing and other ghostly effects, albeit the probability of their occurrence at a macroscopic level is exceedingly small. So small in fact that it is not reasonable to expect that a macroscopic tunneling event takes place during the entire lifetime of the Universe, let alone in the presence of humans. But unlikely is not impossible: a quantum god can in principle miraculously interfere with the business of the physical world without violating the rules that govern it.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;There is, nonetheless, some degree of circularity with this notion of law-abiding miracles within a probabilistic physical theory. If the frequency of such miracles is too high (as it would be the case with the scenario envisioned by Newton of a solar system being constantly tweaked to keep it stable), such divine interventions would show up in the statistical calculations made by physicists when experimentally testing the theory, rendering results highly inconsistent with the theoretical predictions; and this can in the end &lt;a href="http://en.wikipedia.org/wiki/Falsifiability"&gt;falsify&lt;/a&gt; the theory. The insistence of a deity to meddle in the normal course of events would block humans from acquiring knowledge of the laws of Nature.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/3806746697072195731/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=3806746697072195731" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/3806746697072195731?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/3806746697072195731?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/10/law-abiding-miracles.html" title="Law-abiding miracles" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DU8MSH84cSp7ImA9WxRXGE8.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-1444009073107517750</id><published>2008-10-23T22:30:00.002+02:00</published><updated>2008-10-24T07:58:09.139+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-24T07:58:09.139+02:00</app:edited><title>Circles, ellipses, assistants and employees</title><content type="html">&lt;p style="text-align: justify;"&gt;The well-known &lt;a href="http://en.wikipedia.org/wiki/Circle-ellipse_problem"&gt;Circle-ellipse Problem&lt;/a&gt; states that, in the world of OO, a circle is not an ellipse because both entities do not behave the same: for instance, one can arbitrarily stretch an ellipse, but stretching a circle would break the essential property of this shape, namely that all its points are at the same distance from the center. On the other hand, other subtype relationships perfectly match the real-life scenarios they model: for instance, an assistant is certainly a kind of employee, and so an associated &lt;code&gt;Assistant&lt;/code&gt; type can be modeled as a subtype of &lt;code&gt;Employee&lt;/code&gt; without any problem whatsoever. What is the difference between these two modelizations that make the latter succeed where the former fails?&lt;/p&gt;&lt;p style="text-align: justify;"&gt;We introduce notation to talk about some concepts around the notion of "object" in OO. An object &lt;span style="font-style: italic;"&gt;x&lt;/span&gt; has a definite lifetime &lt;span style="font-style: italic;"&gt;T&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;) = [&lt;span style="font-style: italic;"&gt;t&lt;/span&gt;&lt;sub&gt;0&lt;/sub&gt;,&lt;span style="font-style: italic;"&gt;t&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt;] during which the internal state of the object might (or might not) &lt;span style="font-style: italic;"&gt;&lt;/span&gt;change according to the operations performed on it. We denote by &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;t&lt;/span&gt;) the state of &lt;span style="font-style: italic;"&gt;x&lt;/span&gt; at time &lt;span style="font-style: italic;"&gt;t&lt;/span&gt;. Assume that we have a type &lt;code&gt;Ellipse&lt;/code&gt; modelling the mathematical concept of ellipse, which we can identify by the set &lt;span style="font-style: italic;"&gt;E&lt;/span&gt; containing all the ellipses in &lt;span style="font-weight: bold;font-family:arial;" &gt;R&lt;/span&gt;&lt;sup&gt;2&lt;/sup&gt;: we intuitively expect from &lt;code&gt;Ellipse&lt;/code&gt; that the states of objects of this type can be univocally mapped to elements of &lt;span style="font-style: italic;"&gt;E&lt;/span&gt;:&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;for all &lt;span style="font-style: italic;"&gt;x&lt;/span&gt; of type &lt;code&gt;Ellipse&lt;/code&gt;, &lt;span style="font-style: italic;"&gt;t&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;T&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;), there is an ellipse &lt;span style="font-style: italic;"&gt;e&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;E&lt;/span&gt; such that &lt;span style="font-style: italic;"&gt;e&lt;/span&gt; ~ &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;x&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;t&lt;/span&gt;),&lt;/p&gt;&lt;p style="text-align: justify;"&gt;or, put more succintly, we can say that&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;the states of &lt;code&gt;Ellipse&lt;/code&gt; objects are ellipses (modulo  isomorphism).&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Similarly, the states of &lt;code&gt;Circle&lt;/code&gt; objects are circles. A &lt;code&gt;Circle&lt;/code&gt; cannot be a subtype of &lt;code&gt;Ellipse&lt;/code&gt; because, according to &lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;Liskov Principle&lt;/a&gt;, a &lt;code&gt;Circle&lt;/code&gt; does not behave as an &lt;code&gt;Ellipse&lt;/code&gt;: there are mutable operations on &lt;code&gt;Ellipse&lt;/code&gt; (like the streching example mentioned at the beginning) such that the final state of the modifed object can never be a circle, so we cannot provide an equivalent operation for &lt;code&gt;Circle&lt;/code&gt;.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Now, suppose we have types &lt;code&gt;Employee&lt;/code&gt; and &lt;code&gt;Assistant&lt;/code&gt;: here the subtyping relationship works because all the state changes of an employee (salary raise, relocation, etc.)  are compatible with the state changes of an assistant. And this points right to the crux of the matter: the real-life "employee" concept refer to a mutable entity whose state can change as part of the interactions of the employee with the rest of the company; &lt;code&gt;Employee&lt;/code&gt; models this concept by making the object state changes reflect the changes of the modelled employee:&lt;/p&gt;&lt;p style="text-align: center;"&gt;the states of &lt;code&gt;Employee&lt;/code&gt; objects are employee states.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Confront this with&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;the states of &lt;code&gt;Ellipse&lt;/code&gt; objects are ellipses.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;code&gt;Ellipse&lt;/code&gt; states are ellipses, not ellipse &lt;span style="font-style: italic;"&gt;states&lt;/span&gt; (ellipses do not have any mutable state). This is why the the subtypying relationship &lt;code&gt;Ellipse&lt;/code&gt;/&lt;code&gt;Circle&lt;/code&gt; does not work while &lt;code&gt;Employee&lt;/code&gt;/&lt;code&gt;Assistant&lt;/code&gt; does. In general, for a concept &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; &amp;lt; &lt;span style="font-style: italic;"&gt;T'&lt;/span&gt; relationship to be representable by types &lt;code&gt;T&lt;/code&gt; &amp;lt; &lt;code&gt;T'&lt;/code&gt;, the states of &lt;code&gt;T&lt;/code&gt; and &lt;code&gt;T'&lt;/code&gt; objects must model states of &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;T'&lt;/span&gt; entities, respectively.&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/1444009073107517750/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=1444009073107517750" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/1444009073107517750?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/1444009073107517750?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/10/circles-ellipses-assistants-and.html" title="Circles, ellipses, assistants and employees" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DkcMQX07eyp7ImA9WxRXFEg.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-826223857484393655</id><published>2008-10-20T00:08:00.002+02:00</published><updated>2008-10-20T00:08:00.303+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-20T00:08:00.303+02:00</app:edited><title>Multicolumn querying</title><content type="html">&lt;p style="text-align: justify;"&gt;Suppose we have a database table equipped with a multicolumn (also called concatenated or complex) index like this:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎CREATE TABLE user (last_name varchar, first_name varchar)&lt;br /&gt;‎CREATE INDEX user_index ON user (last_name, first_name)&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;Most platforms take advantage of &lt;code&gt;user_index&lt;/code&gt; in queries involving both &lt;code&gt;user&lt;span style="font-family:Georgia,serif;"&gt;&lt;/span&gt;&lt;/code&gt; attributes or just &lt;code&gt;last_name&lt;/code&gt;:&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎SELECT * from user WHERE first_name="Robert" AND last_name="Grant";&lt;br /&gt;‎SELECT * from user WHERE last_name="Smith";&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;while, on the other hand, queries like the following:&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎SELECT * from user WHERE first_name="Mary";&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;trigger a full table scan or use the index in a suboptimal fashion. The simple reason behind this is that database indexes are usually implemented with &lt;a href="http://en.wikipedia.org/wiki/B-tree"&gt;B-trees&lt;/a&gt; or similar structures that sort the data by the values of the indexed column: for a multicolumn index on (&lt;span style="font-style: italic;"&gt;c&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt;,...,&lt;span style="font-style: italic;"&gt;c&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/sub&gt;), sorting is performed according to the lexicographical order associated to the &lt;span style="font-style: italic;"&gt;n&lt;/span&gt; columns, with &lt;span style="font-style: italic;"&gt;c&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt; taking precedence over &lt;span style="font-style: italic;"&gt;c&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;j&lt;/span&gt;&lt;/sub&gt; if &lt;span style="font-style: italic;"&gt;i&lt;/span&gt; &amp;lt; &lt;span style="font-style: italic;"&gt;j&lt;/span&gt;. So, in our example the index sorts the rows by &lt;code&gt;last_name&lt;/code&gt; and, within clusters with equal &lt;code&gt;last_name&lt;/code&gt;, data is further sorted by &lt;code&gt;first_name&lt;/code&gt;. This explains why querying on &lt;code&gt;last_name&lt;/code&gt; benefits from the index (rows are sorted by the first attribute to begin with) while querying on  &lt;code&gt;first_name&lt;/code&gt; does not. Database designers learn to carefully consider the column specification order when creating multicolumn indexes.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;So, in general, an index on (&lt;span style="font-style: italic;"&gt;c&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt;,...,&lt;span style="font-style: italic;"&gt;c&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/sub&gt;) serves to accelerate queries of the form&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;c&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt; = &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt; &lt;span style="font-size:85%;"&gt;&lt;code&gt;AND&lt;/code&gt;&lt;/span&gt; &lt;span style="font-style: italic;"&gt;c&lt;/span&gt;&lt;sub&gt;2&lt;/sub&gt; = &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;sub&gt;2&lt;/sub&gt; &lt;span style="font-size:85%;"&gt;&lt;code&gt;AND&lt;/code&gt;&lt;/span&gt; ··· &lt;span style="font-size:85%;"&gt;&lt;code&gt;AND&lt;/code&gt;&lt;/span&gt; &lt;span style="font-style: italic;"&gt;c&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt; = &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;for all &lt;span style="font-style: italic;"&gt;i&lt;/span&gt; ≤ &lt;span style="font-style: italic;"&gt;n&lt;/span&gt;. The question arises: how many multicolumn indexes, and how, must be created so that all possible attribute combinations are covered by some index? For instance, if we have three attributes:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎CREATE TABLE user (last_name varchar, first_name varchar, age integer)&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;three indexes suffice:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎CREATE INDEX user_index1 ON user (last_name, first_name, age)&lt;br /&gt;‎CREATE INDEX user_index2 ON user (first_name, last_name, age)&lt;br /&gt;‎CREATE INDEX user_index3 ON user (age, last_name, first_name)&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;(You might find it entertaining to check out manually that all the 7 different attribute combinations are covered.) It turns out that for &lt;span style="font-style: italic;"&gt;n&lt;/span&gt; attributes, which generate 2&lt;sup&gt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/sup&gt; − 1 combinations, the minimum number of indexes needed is&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;,floor(&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;/2)) = &lt;span style="font-style: italic;"&gt;n&lt;/span&gt;!/&lt;span style="font-size:130%;"&gt;(&lt;/span&gt;ceil(&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;/2)!·floor(&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;/2)!&lt;span style="font-size:130%;"&gt;)&lt;/span&gt;,&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;where &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;a&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;b&lt;/span&gt;) denotes the binomial coefficient &lt;a href="http://mathworld.wolfram.com/BinomialCoefficient.html"&gt;&lt;span style="font-style: italic;"&gt;a&lt;/span&gt; choose &lt;span style="font-style: italic;"&gt;b&lt;/span&gt;&lt;/a&gt;. The values of &lt;a href="http://www.research.att.com/%7Enjas/sequences/A001405"&gt;&lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;,floor(&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;/2))&lt;/a&gt; for &lt;span style="font-style: italic;"&gt;n&lt;/span&gt; =  1,2,... are: 1, 2, 3, 6, 10, 20, 35, 70, 126,... Using &lt;a href="http://mathworld.wolfram.com/StirlingsApproximation.html"&gt;Stirling's approximation&lt;/a&gt;, the sequence can be proved to be O(2&lt;sup&gt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/sup&gt;/√&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;). The mathematical formulation of this problem has been studied in a &lt;a href="http://bannalia.blogspot.com/2008/09/combinatory-theorem.html"&gt;previous entry&lt;/a&gt;. Also, an algorithm for generating the index set &lt;a href="http://bannalia.blogspot.com/2008/10/generating-permutation-covers-part-ii.html"&gt;is provided&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/826223857484393655/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=826223857484393655" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/826223857484393655?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/826223857484393655?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/10/multicolumn-querying.html" title="Multicolumn querying" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;Ck4NSHk5cCp7ImA9WxRXEUk.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-3676732140489116391</id><published>2008-10-16T05:58:00.005+02:00</published><updated>2008-10-16T09:09:59.728+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-16T09:09:59.728+02:00</app:edited><title>On necessity in McGinn</title><content type="html">&lt;p style="text-align: justify;"&gt;&lt;a href="http://colinmcginnblog.com/"&gt;Colin McGinn&lt;/a&gt; challenges in his book &lt;a href="http://www.oup.com/uk/catalogue/?ci=9780199262632"&gt;&lt;span style="font-style: italic;"&gt;Logical Properties&lt;/span&gt;&lt;/a&gt; the standard treatment of modality and proposes some rather provocative and unorthodox ideas on the nature of necessity and associated modal notions. There is much to argue about McGinn's positions, but I would like to focus on three particular issues:&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The main objection of McGinn to the possible world semantics is that this approach reduces modal notions to quantifications over universes of &lt;span style="font-style: italic;"&gt;possible&lt;/span&gt; worlds, and whether a world is possible or not is already dependent on the modal concept of possibility, thus engendering a circular reasoning. As I see it, the flaw in this argument stems from the wrong assumption that characterizing a world &lt;span style="font-style: italic;"&gt;W&lt;/span&gt; as possible is a modal business; although McGinn does not put it explicitly, he seems to think that the statement "&lt;span style="font-style: italic;"&gt;W&lt;/span&gt; is a possible world" is equivalent to something like "possibly &lt;span style="font-style: italic;"&gt;W&lt;/span&gt; exists". But actually there is nothing modal about the concept of "possible world": a possible world is just a &lt;span style="font-style: italic;"&gt;consistent&lt;/span&gt; (i.e. logically non-contradictory) state of affairs, of which the actual world we live in is just an example. Rephrase the whole possible worlds semantics formulation replacing "possible world" with "consistent world" and all the apparent circularities vanish away.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Later on, McGinn proposes a &lt;span style="font-style: italic;"&gt;copula modifier theory&lt;/span&gt; to explain modality: basically, this theory contends that modal notions qualify the way that objects hold properties. So, "Socrates must be a man" means that Socrates holds the property of being a man in the mode of necessity. McGinn immediately realizes that adscribing modality to the copula of statements cannot cover uses of modality qualifying entire statements, as in "it is impossible that 2 + 2 = 5" (which, incidentally, is how modality operators work in formal modal logic). He solves the problem by postulating that in these cases the missing copula to qualify is that adscribing the truth property to the proposition at hand: so "it is impossible that 2 + 2 = 5" can be rephrased as "the proposition '2 + 2 = 5' cannot be true", which, according to the copula modifier theory, means that "2 + 2 = 5" holds the property of being true in the mode of impossibility. In the process, McGinn has forced us to accept propositions as first-class entities to talk about, an ontological extension that some (notably Quine) reject commiting to. Even admitting this unexpected guest, it is not clear to me how this theory sheds any light on the nature of modality: one can as well go the reverse way and formulate predicate-qualifying modal sentences as sentence-level modal utterances: for instance, "Socrates must be a man" is just a way of saying "necessarily Socrates is a man", which process does not involve the introduction of propositions and  the "truth predicate". Why McGinn prefers one rewriting rule to the converse is unknown to me --furthermore, McGinn's formulation poses some perplexities when iterated modality is considered, as he readily acknowledges.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Finally, having stated that modality affects the way objects hold properties, McGinn renounces to further analyze what the exact nature of this relationship between objects and properties is. He gives in passing a Tarski-style semantic formulation of modality along the lines&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; satisfies 'is necessarily &lt;span style="font-style: italic;"&gt;F&lt;/span&gt;' iff &lt;span style="font-style: italic;"&gt;x&lt;/span&gt; necessarily satisfies '&lt;span style="font-style: italic;"&gt;F&lt;/span&gt;',&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;only to admit that this does not add anything to our understanding of modality. Modal truth is left as a sort of primitive, epiphenomenal concept for which even a special entity status is set up within the confines of McGinn's exuberant ontology. It is difficult to see how this situation represents an improvement over the standard modal theory.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/3676732140489116391/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=3676732140489116391" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/3676732140489116391?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/3676732140489116391?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/10/on-necessity-in-mcginn.html" title="On necessity in McGinn" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CkECQH8_eCp7ImA9WxRQF0Q.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-4668384364805547671</id><published>2008-10-12T07:51:00.001+02:00</published><updated>2008-10-12T07:51:01.140+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-12T07:51:01.140+02:00</app:edited><title>Points inside a polygon and the Residue Theorem</title><content type="html">&lt;p style="text-align: justify;"&gt;A usual way to determine whether a 2D point &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; lies inside a &lt;a href="http://mathworld.wolfram.com/SimplePolygon.html"&gt;&lt;span style="font-style: italic;"&gt;simple polygon&lt;/span&gt;&lt;/a&gt; &lt;span style="font-style: italic;"&gt;P&lt;/span&gt; consists in tracing a ray from &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; to infinity and computing how many times this ray intersects &lt;span style="font-style: italic;"&gt;P&lt;/span&gt;: &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; is inside &lt;span style="font-style: italic;"&gt;P&lt;/span&gt; iff the number of intersections is odd. We investigate a different technique for solving the problem based on complex analysis, only to finally see that this new method is really equivalent to the classical ray algorithm.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;We consider &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;P&lt;/span&gt; to be represented in the complex plane and assume, without loss of generality, that &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; = 0. The &lt;a href="http://mathworld.wolfram.com/ResidueTheorem.html"&gt;Residue Theorem&lt;/a&gt; tells us that&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-size:130%;"&gt;∫&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;P&lt;/span&gt;&lt;/sub&gt; (1/&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;) &lt;span style="font-style: italic;"&gt;dz&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;is null if the point 0 (the only pole of 1/&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;) is outside &lt;span style="font-style: italic;"&gt;P&lt;/span&gt;, and&lt;/p&gt;&lt;p style="text-align: center;"&gt;2&lt;span style="font-style: italic;"&gt;πi&lt;/span&gt; Res&lt;sub&gt;&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;=0&lt;/sub&gt;(1/&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;) = 2&lt;span style="font-style: italic;"&gt;πi&lt;/span&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;if 0 lies inside &lt;span style="font-style: italic;"&gt;P&lt;/span&gt;. So we need only evaluate the integral &lt;span style="font-size:130%;"&gt;∫&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;P&lt;/span&gt;&lt;/sub&gt; (1/&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;) &lt;span style="font-style: italic;"&gt;dz&lt;/span&gt;; as &lt;span style="font-style: italic;"&gt;P&lt;/span&gt; is a loop sequence of segments going counterclockwise like this&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt; &lt;span style="font-style: italic;"&gt;P = &lt;/span&gt;(&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt;,&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;2&lt;/sub&gt;), (&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;2&lt;/sub&gt;,&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;3&lt;/sub&gt;), ... ,  (&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;−1&lt;/sub&gt;,&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;&lt;/sub&gt;), (&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;&lt;/sub&gt;,&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt;),&lt;/p&gt;&lt;p style="text-align: justify;"&gt;and &lt;a href="http://mathworld.wolfram.com/NaturalLogarithm.html"&gt;ln &lt;span style="font-style: italic;"&gt;z&lt;/span&gt;&lt;/a&gt; is an antiderivative of 1/&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;, we have that&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-size:130%;"&gt;∫&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;P&lt;/span&gt;&lt;/sub&gt; (1/&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;) &lt;span style="font-style: italic;"&gt;dz&lt;/span&gt; = &lt;span style="font-size:130%;"&gt;∑&lt;/span&gt; &lt;span style="font-size:130%;"&gt;∫&lt;/span&gt;&lt;sub&gt;(&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt;,&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;+1&lt;/sub&gt;)&lt;/sub&gt; (1/&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;) &lt;span style="font-style: italic;"&gt;dz&lt;/span&gt; =&lt;br /&gt;= &lt;span style="font-size:130%;"&gt;∑&lt;/span&gt; (ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;+1&lt;/sub&gt; − ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt;).&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;But now, this sum seems to be unconditionally null:&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-size:130%;"&gt;∑&lt;/span&gt; (ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;+1&lt;/sub&gt; − ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt;) =&lt;br /&gt;= ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;2&lt;/sub&gt; − ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt; + ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;3&lt;/sub&gt; − ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;2&lt;/sub&gt; + ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;4&lt;/sub&gt; − ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;3&lt;/sub&gt; + ... + ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span&gt;1&lt;/span&gt;&lt;/sub&gt; − ln &lt;span style="font-style: italic;"&gt;p&lt;sub&gt;N&lt;/sub&gt;&lt;/span&gt;&lt;sub&gt;&lt;/sub&gt; = 0.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Each term is cancelled out by some other with different sign, so the total sum is, or seems to be, zero! Obviously we have made some mistake during the process, and in fact the wrong assumption is this: the equality&lt;/p&gt;&lt;p style="text-align: center;"&gt; &lt;span style="font-size:130%;"&gt;∫&lt;/span&gt;&lt;sub&gt;(&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt;,&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;+1&lt;/sub&gt;)&lt;/sub&gt; (1/&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;) &lt;span style="font-style: italic;"&gt;dz&lt;/span&gt; = ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;+1&lt;/sub&gt; − ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;only holds if  the derivative of ln &lt;span style="font-style: italic;"&gt;z&lt;/span&gt; is 1/&lt;span style="font-style: italic;"&gt;z&lt;/span&gt; along all the segment (&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;i&lt;/sub&gt;,&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;+1&lt;/sub&gt;) (more precisely, at a region containing the segment): but ln &lt;span style="font-style: italic;"&gt;z&lt;/span&gt;, or, strictly speaking, the &lt;a href="http://mathworld.wolfram.com/PrincipalBranch.html"&gt;&lt;span style="font-style: italic;"&gt;principal branch&lt;/span&gt;&lt;/a&gt; of ln &lt;span style="font-style: italic;"&gt;z&lt;/span&gt;, has a &lt;a href="http://mathworld.wolfram.com/BranchCut.html"&gt;&lt;span style="font-style: italic;"&gt;cut&lt;/span&gt;&lt;/a&gt; at the negative real axis, so the equality above fails to hold when (&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;i&lt;/sub&gt;,&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;+1&lt;/sub&gt;) intersects this cut. When this is the case, we can evaluate the integral by using a different branch of the complex logarithm so that (&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;i&lt;/sub&gt;,&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;+1&lt;/sub&gt;) does not intersect the new cut. It is not hard to see that doing so yields&lt;/p&gt;&lt;p style="text-align: center;"&gt; &lt;span style="font-size:130%;"&gt;∫&lt;/span&gt;&lt;sub&gt;(&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt;,&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;+1&lt;/sub&gt;)&lt;/sub&gt; (1/&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;) &lt;span style="font-style: italic;"&gt;dz&lt;/span&gt; = ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;+1&lt;/sub&gt; − ln &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt; + &lt;span style="font-style: italic;"&gt;σ&lt;/span&gt;2&lt;span style="font-style: italic;"&gt;πi&lt;/span&gt;,&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;where ln denotes again the principal branch of the complex logarithm and &lt;span style="font-style: italic;"&gt;σ&lt;/span&gt; is +1 or −1 depending on whether (&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;i&lt;/sub&gt;,&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;+1&lt;/sub&gt;) crosses the negative real axis southwards or northwards. Having the situation with the negative real axis covered, we can easily deduce that&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-size:130%;"&gt;∫&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;P&lt;/span&gt;&lt;/sub&gt; (1/&lt;span style="font-style: italic;"&gt;z&lt;/span&gt;) &lt;span style="font-style: italic;"&gt;dz&lt;/span&gt; =(&lt;span style="font-style: italic;"&gt;s&lt;/span&gt; − &lt;span style="font-style: italic;"&gt;n&lt;/span&gt;)&lt;span style="font-style: italic;"&gt;&lt;/span&gt;2&lt;span style="font-style: italic;"&gt;πi&lt;/span&gt;,&lt;/p&gt;&lt;p style="text-align: justify;"&gt;where &lt;span style="font-style: italic;"&gt;s&lt;/span&gt; is the number of segments of &lt;span style="font-style: italic;"&gt;P&lt;/span&gt; crossing the negative real axis southwards, and &lt;span style="font-style: italic;"&gt;n&lt;/span&gt; corresponds to those crossing northwards. And, in the end, this is but a simple reformulation of the old ray-based algorithm.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/4668384364805547671/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=4668384364805547671" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/4668384364805547671?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/4668384364805547671?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/10/points-inside-polygon-and-residue.html" title="Points inside a polygon and the Residue Theorem" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DkMCSX87fCp7ImA9WxRQFks.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-3912989751140533392</id><published>2008-10-08T04:17:00.001+02:00</published><updated>2008-10-10T20:47:48.104+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-10T20:47:48.104+02:00</app:edited><title>Generating permutation covers: part II</title><content type="html">&lt;div style="text-align: justify;"&gt;In a &lt;a href="http://bannalia.blogspot.com/2008/09/generating-permutation-covers-part-i.html"&gt;prior entry&lt;/a&gt;, we reduced the problem of generating a &lt;a href="http://bannalia.blogspot.com/2008/09/combinatory-theorem.html"&gt;minimal permutation cover&lt;/a&gt; of a set &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; of &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; elements to that of constructing a minimal tuple cover of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;0&lt;/sub&gt; &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; ... &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;M&lt;/sub&gt;, where &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;i&lt;/sub&gt; is the set of all subsets of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; with cardinality &lt;span style="font-style: italic;"&gt;i&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;M&lt;/span&gt; = (&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1)/2 and &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; is odd. We do this construction recursively on &lt;span style="font-style: italic;"&gt;m&lt;/span&gt; = 0,...,&lt;span style="font-style: italic;"&gt;M&lt;/span&gt;.&lt;/div&gt;&lt;p style="text-align: justify;"&gt;Suppose then that we have a minimal tuple cover &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;0&lt;/sub&gt; &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; ... &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;, i.e. a minimal set of &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;-tuples of different elements jointly covering all subsets of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; with cardinality ≤ &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;, and we want to extend &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; to a minimal set &lt;span style="font-style: italic;"&gt;T'&lt;/span&gt; of (&lt;span style="font-style: italic;"&gt;m&lt;/span&gt; + 1)-tuples jointly covering &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;0&lt;/sub&gt;  &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; ... &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt; &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;&lt;sub&gt; + 1&lt;/sub&gt;. We state without proof the following&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Lemma.&lt;/span&gt; If &lt;span style="font-style: italic;"&gt;m&lt;/span&gt; ≤ &lt;span style="font-style: italic;"&gt;N&lt;/span&gt;/2, a miminal tuple cover of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;0&lt;/sub&gt; &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; ... &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt; has cardinality &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;).&lt;/p&gt;&lt;p style="text-align: justify;"&gt;So, |&lt;span style="font-style: italic;"&gt;T&lt;/span&gt;| = &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;) and we want to extend &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; to a set &lt;span style="font-style: italic;"&gt;T'&lt;/span&gt; by assigning to each tuple &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt; of &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; one or more tuples of the form &lt;span style="font-style: italic;"&gt;τa&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;a&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; − range(&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;), in such a way that all subsets of  &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;&lt;sub&gt; + 1&lt;/sub&gt; are covered and the final number of elements of the cover |&lt;span style="font-style: italic;"&gt;T'&lt;/span&gt;| is &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;m&lt;/span&gt; + 1).&lt;/p&gt;&lt;p style="text-align: justify;"&gt;When extending a tuple &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt; of &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; we will disregard the order of its elements, so basically we are identifying &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt; with range(&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;); this reduction still yields &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;) different elements, since every element of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;  must be covered by some tuple of &lt;span style="font-style: italic;"&gt;T&lt;/span&gt;. So the extension mapping &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; →&lt;span style="font-style: italic;"&gt; &lt;/span&gt;&lt;span style="font-style: italic;"&gt;T'&lt;/span&gt; relates elements of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt; to elements of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;&lt;sub&gt; + 1&lt;/sub&gt;, and in fact can be regarded as a subset of the graph induced by inclusion between elements of these two sets.&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_rqhF_8E1nlA/SNnYpv4KZPI/AAAAAAAAAVo/Vsa6RWa9VWU/s1600-h/Sm_Sm1.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://4.bp.blogspot.com/_rqhF_8E1nlA/SNnYpv4KZPI/AAAAAAAAAVo/Vsa6RWa9VWU/s400/Sm_Sm1.png" alt="" id="BLOGGER_PHOTO_ID_5249465052415288562" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;It is easy to see that each element of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt; is the source of (&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;) arrows, whereas &lt;span style="font-style: italic;"&gt;m&lt;/span&gt; + 1  arrows arrive at each element of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;&lt;sub&gt; + 1&lt;/sub&gt;. Our job is then to select &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;m&lt;/span&gt; + 1) arrows from the diagram above so that all the elements from the source and the destination set are met by at least one arrow. This selection process has to maintain some balance so that no source or destination element is left unattended: for instance, if we select for each element of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;&lt;sub&gt; + 1&lt;/sub&gt; an arbitrary arrow arriving at it there is the possibility that some elements of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;  are not visited by any of the selected arrows. The following is a balanced selection criterion: given a fixed injection  &lt;span style="font-style: italic;"&gt;f&lt;/span&gt; : &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt; → &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;&lt;sub&gt; + 1&lt;/sub&gt; and function &lt;span style="font-style: italic;"&gt;g&lt;/span&gt; : &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;&lt;sub&gt; + 1&lt;/sub&gt; → &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;, both compatible with the inclusion relationship between &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt; and &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;&lt;sub&gt; + 1&lt;/sub&gt; (that is, &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; is a subset of &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;X&lt;/span&gt;) and &lt;span style="font-style: italic;"&gt;Y&lt;/span&gt; a superset of &lt;span style="font-style: italic;"&gt;g&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;Y&lt;/span&gt;)), we select an arrow &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; → &lt;span style="font-style: italic;"&gt;Y&lt;/span&gt; iff&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;Y&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;X&lt;/span&gt;) or &lt;span style="font-size:130%;"&gt;(&lt;/span&gt;&lt;span style="font-style: italic;"&gt;X&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;g&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;Y&lt;/span&gt;) and &lt;span style="font-style: italic;"&gt;Y&lt;/span&gt; is not in &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;)&lt;span style="font-size:130%;"&gt;)&lt;/span&gt;.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;It is obvious that this criterion does not leave any &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; or &lt;span style="font-style: italic;"&gt;Y&lt;/span&gt; unvisited. The number of selected arrows coincides with the number of elements in &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;&lt;sub&gt; + 1&lt;/sub&gt;, which is &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;m&lt;/span&gt; + 1) as required. In the following we suppose, without loss of generality, that &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; is the set {0,...,&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1}. Constructively defining an inclusion compatible injection from &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt; to &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;&lt;sub&gt; + 1&lt;/sub&gt; is not a trivial task, but fortunately for us  &lt;a href="http://dml.cz/handle/10338.dmlcz/106156"&gt;a paper&lt;/a&gt; from Pudlák, Turzík and Poljak provides the definition of such an injection &lt;span style="font-style: italic;"&gt;f&lt;/span&gt; along with an algorithm &lt;span style="font-style: italic;"&gt;χ&lt;/span&gt; : &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;&lt;sub&gt; + 1&lt;/sub&gt; → {true,false} that checks whether a given &lt;span style="font-style: italic;"&gt;Y&lt;/span&gt; belongs to &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;m&lt;/sub&gt;). We adopt the following definition for &lt;span style="font-style: italic;"&gt;g&lt;/span&gt;:&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;g&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;Y&lt;/span&gt;) := &lt;span style="font-style: italic;"&gt;Y&lt;/span&gt; − max(&lt;span style="font-style: italic;"&gt;Y&lt;/span&gt;),&lt;/p&gt;&lt;p style="text-align: justify;"&gt;which leads to this algorithm for generating &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;'&lt;/span&gt; from &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;:&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;extend&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;)&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;'&lt;/span&gt; ← Ø&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;for every&lt;/span&gt; &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;in&lt;/span&gt; &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;br /&gt;····&lt;span style="font-style: italic;"&gt;X&lt;/span&gt; ← range(&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;)&lt;br /&gt;····&lt;span style="font-style: italic;"&gt;a&lt;/span&gt; ←  &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;X&lt;/span&gt;) − &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; {the one element added to &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; by &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;}&lt;br /&gt;····&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;'&lt;/span&gt; ← &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;'&lt;/span&gt; &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; {&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;·&lt;span style="font-style: italic;"&gt;a&lt;/span&gt;}&lt;br /&gt;····&lt;span style="font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="font-style: italic;"&gt;i&lt;/span&gt; = max(&lt;span style="font-style: italic;"&gt;X&lt;/span&gt;) + 1 &lt;span style="font-weight: bold;"&gt;to&lt;/span&gt; &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1&lt;br /&gt;········&lt;span style="font-style: italic;"&gt;Y&lt;/span&gt; ← &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; {&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;}&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;br /&gt;········&lt;span style="font-weight: bold;"&gt;if &lt;/span&gt;&lt;span style="font-weight: bold;"&gt;not&lt;/span&gt; &lt;span style="font-style: italic;"&gt;χ&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;Y&lt;/span&gt;) &lt;span style="font-weight: bold;"&gt;&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;then&lt;/span&gt;&lt;br /&gt;············&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;'&lt;/span&gt; ← &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;'&lt;/span&gt; &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; {&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;·&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;}&lt;br /&gt;········&lt;span style="font-weight: bold;"&gt;end if&lt;/span&gt;&lt;br /&gt;····&lt;span style="font-weight: bold;"&gt;end for&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;end for&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Note that the double loop over (&lt;span style="font-style: italic;"&gt;X&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;) is designed in such a way that it only visits the &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; → &lt;span style="font-style: italic;"&gt;Y&lt;/span&gt; arrows where &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; =  &lt;span style="font-style: italic;"&gt;g&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;Y&lt;/span&gt;), which is maximally efficient and saves us the need to explicity compute &lt;span style="font-style: italic;"&gt;g&lt;/span&gt;. The complete &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;sub&gt;0&lt;/sub&gt; = Ø → &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt; → ··· → &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;M&lt;/span&gt;&lt;/sub&gt; process can be inlined to avoid generating the intermediate &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt; covers.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;tuple-cover&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;) {initial call: &lt;span style="font-weight: bold;"&gt;tuple-&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;cover&lt;/span&gt;(0,&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;Ø,&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;) with &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt; empty}&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="font-style: italic;"&gt;m&lt;/span&gt; = (&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1)/2 &lt;span style="font-weight: bold;"&gt;then&lt;/span&gt;&lt;br /&gt;····&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt; ← &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt; &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; {&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;}&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;else&lt;/span&gt;&lt;br /&gt;····&lt;span style="font-style: italic;"&gt;X&lt;/span&gt; ← range(&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;)&lt;br /&gt;····&lt;span style="font-style: italic;"&gt;a&lt;/span&gt; ←  &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;X&lt;/span&gt;) − &lt;span style="font-style: italic;"&gt;X&lt;/span&gt;&lt;br /&gt;····&lt;span style="font-weight: bold;"&gt;tuple-&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;cover&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;m&lt;/span&gt; + 1,&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;·&lt;span style="font-style: italic;"&gt;a&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;)&lt;br /&gt;····&lt;span style="font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="font-style: italic;"&gt;i&lt;/span&gt; = max(&lt;span style="font-style: italic;"&gt;X&lt;/span&gt;) + 1 &lt;span style="font-weight: bold;"&gt;to&lt;/span&gt; &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1&lt;br /&gt;········&lt;span style="font-style: italic;"&gt;Y&lt;/span&gt; ← &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; {&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;}&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;br /&gt;········&lt;span style="font-weight: bold;"&gt;if &lt;/span&gt;&lt;span style="font-weight: bold;"&gt;not&lt;/span&gt; &lt;span style="font-style: italic;"&gt;χ&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;Y&lt;/span&gt;) &lt;span style="font-weight: bold;"&gt;&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;then&lt;/span&gt;&lt;br /&gt;············&lt;span style="font-weight: bold;"&gt;cover&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;m&lt;/span&gt; + 1,&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;·&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;)&lt;br /&gt;········&lt;span style="font-weight: bold;"&gt;end if&lt;/span&gt;&lt;br /&gt;····&lt;span style="font-weight: bold;"&gt;end for&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;end if&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;In order to leverage the tuple cover algorithm to construct a minimal permutation cover, the only missing piece is finding a bijection &lt;span style="font-style: italic;"&gt;d&lt;/span&gt; : &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;M&lt;/span&gt;&lt;/sub&gt; → &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;M&lt;/span&gt;&lt;/sub&gt; such that &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; ∩ &lt;span style="font-style: italic;"&gt;d&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;X&lt;/span&gt;) = Ø for all &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;M&lt;/span&gt;&lt;/sub&gt;, as we &lt;a href="http://bannalia.blogspot.com/2008/09/generating-permutation-covers-part-i.html"&gt;already saw&lt;/a&gt;. The aforementioned paper from Pudlák et al. provides also such a function (which, in fact, is used for the construction of &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;). Having all the necessary components, the following is the full algorithm for constructing a minimal permutation cover on &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; = {0,...,&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1}.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;cover&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;)&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; is even &lt;span style="font-weight: bold;"&gt;then&lt;/span&gt;&lt;br /&gt;····&lt;span style="font-style: italic;"&gt;N'&lt;/span&gt; ← &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;else&lt;/span&gt;&lt;br /&gt;····&lt;span style="font-style: italic;"&gt;N'&lt;/span&gt; ← &lt;span style="font-style: italic;"&gt;N&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;end if&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt; ← Ø&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt; ← Ø&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;tuple-&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;cover&lt;/span&gt;(0,&lt;span style="font-style: italic;"&gt;N'&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;Ø,&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;)&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;for every&lt;/span&gt; &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;in&lt;/span&gt; &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;&lt;br /&gt;····find &lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt; with range(&lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;d&lt;/span&gt;(range(&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;))&lt;br /&gt;····&lt;span style="font-style: italic;"&gt;a&lt;/span&gt; ← &lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;S&lt;/span&gt; − range(&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;)  − range(&lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt;)&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;br /&gt;····&lt;span style="font-style: italic;"&gt;σ&lt;/span&gt; &lt;span style="font-style: italic;"&gt;&lt;/span&gt; ← &lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;·&lt;span style="font-style: italic;"&gt;a&lt;/span&gt;·reverse(&lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt;)&lt;br /&gt;····&lt;span style="font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; is even &lt;span style="font-weight: bold;"&gt;then&lt;/span&gt;&lt;br /&gt;········&lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt; ← &lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt;  &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; {&lt;span style="font-style: italic;"&gt;N'&lt;/span&gt;·&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;σ&lt;/span&gt;}&lt;br /&gt;········&lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt; ← &lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt;  &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; {&lt;span style="font-style: italic;"&gt;σ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;·&lt;span style="font-style: italic;"&gt;N'&lt;/span&gt;}&lt;br /&gt;···· &lt;span style="font-weight: bold;"&gt;else&lt;/span&gt;&lt;br /&gt;········&lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt; ← &lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt;  &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; {&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;σ&lt;/span&gt;}&lt;br /&gt;····&lt;span style="font-weight: bold;"&gt;end if&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;end for&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;A &lt;a href="http://joaquinlopezmunoz.googlepages.com/permcover.cpp"&gt;C++ implementation&lt;/a&gt; of the algorithm is available (&lt;a href="http://www.boost.org/"&gt;Boost&lt;/a&gt; used). The following are the different covers generated for |&lt;span style="font-style: italic;"&gt;S&lt;/span&gt;| = 1,...,8.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;table style="padding: 0pt; margin-left: auto; margin-right: auto; text-align: center; height: 201px;" border="1"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="font-style: italic;"&gt;&lt;span&gt;|&lt;/span&gt;&lt;span&gt;S|&lt;/span&gt;&lt;/td&gt;&lt;td&gt;permutation cover&lt;code&gt;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;1&lt;/td&gt;&lt;td style="text-align: left;"&gt;&lt;span style="font-size:85%;"&gt;(&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;a&lt;/span&gt;&lt;span style="font-size:85%;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;2&lt;/td&gt;&lt;td style="text-align: left;"&gt;&lt;span style="font-size:85%;"&gt;(&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;a&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;b&lt;/span&gt;&lt;span style="font-size:85%;"&gt;), (&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;b&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;a&lt;/span&gt;&lt;span style="font-size:85%;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;3&lt;/td&gt;&lt;td style="text-align: left;"&gt;&lt;span style="font-size:85%;"&gt;(&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;acb&lt;/span&gt;&lt;span style="font-size:85%;"&gt;), (&lt;span style="font-style: italic;"&gt;bac&lt;/span&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;), (&lt;span style="font-style: italic;"&gt;cba&lt;/span&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:85%;" &gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;4&lt;/td&gt;&lt;td style="text-align: left;"&gt;&lt;span style="font-size:85%;"&gt;(&lt;span style="font-style: italic;"&gt;acbd&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;dacb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bacd&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;dbac&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cbad&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;dcba&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;5&lt;/td&gt;&lt;td style="text-align: left;"&gt;&lt;span style="font-size:85%;"&gt;(&lt;span style="font-style: italic;"&gt;acedb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;baedc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bdaec&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bedca&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cbaed&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cdbae&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cebad&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;daceb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;decab&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;eadbc&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;6&lt;/td&gt;&lt;td style="text-align: left;"&gt;&lt;span style="font-size:85%;"&gt;(&lt;span style="font-style: italic;"&gt;acedbf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;facedb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;baedcf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;fbaedc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bdaecf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;fbdaec&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bedcaf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;fbedca&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cbaedf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;fcbaed&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;cdbaef&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;fcdbae&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cebadf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;fcebad&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;dacebf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;fdaceb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;decabf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;fdecab&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;eadbcf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;feadbc&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;7&lt;/td&gt;&lt;td style="text-align: left;"&gt;&lt;span style="font-size:85%;"&gt;(&lt;span style="font-style: italic;"&gt;acegfdb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;baegfdc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bdagfec&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bdfagec&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bdgfeca&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bedagfc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;befdcag&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;begdcaf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bfaegdc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bfgecad&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bgafced&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cbagfed&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cdbagfe&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cdfbage&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;cdgbafe&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cebagfd&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cefbagd&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cegbafd&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cfbaged&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cfgeadb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cgbfdae&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;dacgfeb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;decbagf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;defcagb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;degcafb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;dfacgeb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;dfgceab&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;dgafbec&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;eadcgfb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;efadbgc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;efgdabc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;egadbfc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;facegdb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;fgaebdc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;gacfdeb&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;8&lt;/td&gt;&lt;td style="text-align: left;"&gt;&lt;span style="font-size:85%;"&gt;(&lt;span style="font-style: italic;"&gt;acegfdbh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hacegfdb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;baegfdch&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hbaegfdc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bdagfech&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hbdagfec&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bdfagech&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;hbdfagec&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bdgfecah&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hbdgfeca&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bedagfch&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hbedagfc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;befdcagh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hbefdcag&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;begdcafh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hbegdcaf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bfaegdch&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hbfaegdc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bfgecadh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hbfgecad&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;bgafcedh&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;hbgafced&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cbagfedh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hcbagfed&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cdbagfeh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hcdbagfe&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cdfbageh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hcdfbage&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;cdgbafeh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hcdgbafe&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cebagfdh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hcebagfd&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cefbagdh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hcefbagd&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cegbafdh&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;hcegbafd&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cfbagedh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hcfbaged&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cfgeadbh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hcfgeadb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;cgbfdaeh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hcgbfdae&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;dacgfebh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hdacgfeb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;decbagfh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hdecbagf&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;defcagbh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hdefcagb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;degcafbh&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;hdegcafb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;dfacgebh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hdfacgeb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;dfgceabh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hdfgceab&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;dgafbech&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hdgafbec&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;eadcgfbh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;headcgfb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;efadbgch&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hefadbgc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;efgdabch&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hefgdabc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;egadbfch&lt;/span&gt;),&lt;br /&gt;(&lt;span style="font-style: italic;"&gt;hegadbfc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;facegdbh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hfacegdb&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;fgaebdch&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hfgaebdc&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;gacfdebh&lt;/span&gt;),(&lt;span style="font-style: italic;"&gt;hgacfdeb&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;In a later entry we will see a practical application of permutation covers in the context of database querying.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/3912989751140533392/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=3912989751140533392" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/3912989751140533392?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/3912989751140533392?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/10/generating-permutation-covers-part-ii.html" title="Generating permutation covers: part II" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_rqhF_8E1nlA/SNnYpv4KZPI/AAAAAAAAAVo/Vsa6RWa9VWU/s72-c/Sm_Sm1.png" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;AkYGQX0ycSp7ImA9WxRQEUk.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-2192561258773982808</id><published>2008-10-04T21:22:00.004+02:00</published><updated>2008-10-04T21:22:00.399+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-04T21:22:00.399+02:00</app:edited><title>User attention sharing</title><content type="html">&lt;p style="text-align: justify;"&gt;CPU is a precious resource that must be shared between the processes concurrently running on a machine at a given time. Some operating systems of the past implemented this sharing using a technique called &lt;span style="font-style: italic;"&gt;collaborative multitasking&lt;/span&gt;, which is based on the processes' explicitly relinquishing CPU control to the OS so that this control be passed on to other tasks. This can be very inefficient, as sharing cannot be controlled by the operating system, but depends on the good judgement of third party application programmers; furthermore, a faulty process that fails to yield CPU or hangs will collapse the whole system. So, modern OSs implement &lt;span style="font-style: italic;"&gt;preemptive multitasking&lt;/span&gt;, in which the system scheduler can give or take CPU control without any special help from the processes. Usual implementations of preemptive multitasking group processes into two categories: IO-bound processes, which consume most of the time waiting for user input or dealing with secondary storage, and CPU-bound processes, which take up as much computing power as they are given. Some scheduling algorithms reward the least CPU-hungry processes by giving them higher priority to regain CPU.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;In a desktop machine, user attention is as much of a precious shared resource as CPU is: concurrently living applications try to attract the user focus by displaying some visual effects or directly putting themselves on the forefront. In the early, more naïve days, applications could freely jump into the foreground of their own accord, which could quickly degrade into an all-out fight (specially if malware was involved) for the user's attention, or "attention thrashing", and render the computer unusable. This situation is reminiscent of the first candid collaborative multitasking systems; incidentally, attention thrashing led some OS vendors to change the behavior of &lt;a href="http://msdn.microsoft.com/en-us/library/ms633539%28VS.85%29.aspx"&gt;some of their APIs&lt;/a&gt; to try to restrict the applications' freedom to steal focus from each other, though &lt;a href="http://www.ddj.com/windows/184405755"&gt;workarounds abound&lt;/a&gt;. In the web browser arena, attention thrashing can be even more daunting, and most users end up entirely blocking pop-ups and other forms of focus stealing.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;As I see it, user attention should then be explicitly taken care of by the operating system in the same way as CPU control is; in fact, we can learn from the history of multitasking and move from the current "collaborative" situation to system-controlled scenarios where applications request attention and are given it based on attention availability and some measure of application fairness. So, applications that request attention very rarely and do so for justified motives (where this last attribute can be estimated indirectly by, say, recording the amount of time spent by the user on the application when this pops up) are ranked higher and can be given more leeway to call on the user. On the other hand, noisy applications are rapidly ranked low and will not be able to steal focus unless the user explicitly clicks on them.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/2192561258773982808/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=2192561258773982808" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/2192561258773982808?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/2192561258773982808?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/10/user-attention-sharing.html" title="User attention sharing" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;A0IEQXYzcSp7ImA9WxRRF08.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-8145803263322451890</id><published>2008-09-30T01:05:00.002+02:00</published><updated>2008-09-30T01:05:00.889+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-09-30T01:05:00.889+02:00</app:edited><title>Mass exhalation</title><content type="html">&lt;p style="text-align: justify;"&gt;How much bodily mass do we give out through breathing? The chemical reaction for aerobic respiration is&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;C&lt;sub&gt;6&lt;/sub&gt;H&lt;sub&gt;12&lt;/sub&gt;O&lt;sub&gt;6 &lt;/sub&gt; + 6O&lt;sub&gt;2 &lt;/sub&gt; → 6CO&lt;sub&gt;2&lt;/sub&gt; + 6H&lt;sub&gt;2&lt;/sub&gt;O&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The amount of carbon dioxide we exhale is then&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;6 mol CO&lt;sub&gt;2&lt;/sub&gt; / mol glucose = 1.47 g CO&lt;sub&gt;2&lt;/sub&gt; / g glucose.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;Processing of fatty acids yields a different amount of exhaled CO&lt;sub&gt;2&lt;/sub&gt;. For instance, the oxidation of a saturated fatty acid is expressed as&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;C&lt;sub style="font-style: italic;"&gt;n&lt;/sub&gt;H&lt;sub&gt;2&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/sub&gt;O&lt;sub&gt;2&lt;/sub&gt; + (3&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;/2 − 1) O&lt;sub&gt;2&lt;/sub&gt; → &lt;span style="font-style: italic;"&gt;n&lt;/span&gt; CO&lt;sub&gt;2&lt;/sub&gt; + &lt;span style="font-style: italic;"&gt;n&lt;/span&gt; H&lt;sub&gt;2&lt;/sub&gt;O&lt;/p&gt;&lt;p style="text-align: justify;"&gt;yielding&lt;/p&gt;&lt;p style="text-align: center;"&gt;44&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;/(14&lt;span style="font-style: italic;"&gt;n&lt;/span&gt; + 32) g CO&lt;sub&gt;2&lt;/sub&gt; / g C&lt;sub style="font-style: italic;"&gt;n&lt;/sub&gt;H&lt;sub&gt;2&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/sub&gt;O&lt;sub&gt;2&lt;/sub&gt;.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;This last expression also serves approximately for unsaturated fatty acids, since these are missing only a few lightweight hydrogen atoms with respect to the saturated acid with the same amount of carbon. Assuming most consumed fat is in the form of palmitate, we have a carbon dioxide yield of&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;2.75 g CO&lt;sub&gt;2&lt;/sub&gt; / g fat;&lt;br /&gt;&lt;/p&gt;&lt;div style="text-align: justify;"&gt;Carbohydrates produce &lt;a href="http://www.fao.org/DOCREP/006/y5022e/y5022e05.htm#bm5.3"&gt;4 kcal per gram&lt;/a&gt;, whereas fatty acid processing yields &lt;a href="http://www.fao.org/DOCREP/006/y5022e/y5022e05.htm#bm5.2"&gt;9 kcal per gram&lt;/a&gt; of fat.  While at rest, the human body takes 30% of the energy from carbohydrates and 70% from fat; so, this results in a combined production of carbon dioxide of&lt;/div&gt;&lt;p style="text-align: center;"&gt;0.324 g CO&lt;sub&gt;2&lt;/sub&gt; / kcal.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;For a daily energy consumption rate around 2,500 kcal, 810 grams of carbon dioxide are then expelled through breathing. There is a experimental study by the &lt;a href="http://www.usda.gov/"&gt;USDA&lt;/a&gt; on the subject that unfortunately I haven't been able to find, but some sources cite it as reporting a value beween  &lt;a href="http://www.faithscience.org/Articles/Articles%20Pdfs/HANNA002.pdf"&gt;445&lt;/a&gt; and &lt;a href="http://www.firetop.co.uk/2006/11/15/cut-co2-emissions-stop-breathing/"&gt;450&lt;/a&gt; liters of CO&lt;sub&gt;2&lt;/sub&gt; (881-891 g) per day,  which is in good agreement with our calculations.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The value calculated is not the answer to our original question about exhaled &lt;span style="font-style: italic;"&gt;bodily&lt;/span&gt; mass: much of the oxygen in the CO&lt;sub&gt;2&lt;/sub&gt; produced comes from the air rather than catabolized substances, specially in the case of fat. We will assume that all the oxygen coming from the burned substance goes to CO&lt;sub&gt;2&lt;/sub&gt;: under these conditions, the amount of bodily mass expelled through breathing is&lt;/p&gt;&lt;p style="text-align: center;"&gt;0.933 g / g glucose,&lt;br /&gt;0.875 g / g fat,&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;or, using the assumed ratios of carbohydrate and fat consumption,&lt;/p&gt;&lt;p style="text-align: center;"&gt;0.138 g / kcal,&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;which yields 345 g for a typical daily energy consumption of 2,500 calories.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/8145803263322451890/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=8145803263322451890" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/8145803263322451890?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/8145803263322451890?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/09/mass-exhalation.html" title="Mass exhalation" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>4</thr:total></entry><entry gd:etag="W/&quot;DUANSXo9cCp7ImA9WxRRFks.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-5161835866730687818</id><published>2008-09-26T05:50:00.003+02:00</published><updated>2008-09-29T07:56:38.468+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-09-29T07:56:38.468+02:00</app:edited><title>Generating permutation covers: part I</title><content type="html">&lt;div style="text-align: justify;"&gt;We investigate ways of efficiently generating &lt;a href="http://bannalia.blogspot.com/2008/09/combinatory-theorem.html"&gt;minimal permutation covers&lt;/a&gt; for a set &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; of &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; elements. As we have already proved, such covers have size &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,floor(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;/2)). The following intermediate results reduce the problem to simpler terms.&lt;/div&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Theorem.&lt;/span&gt; Assume that &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; is even and greater than zero. Let &lt;span style="font-style: italic;"&gt;a&lt;/span&gt; be an an arbitrary element of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt; a minimal permutation cover of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; − {&lt;span style="font-style: italic;"&gt;a&lt;/span&gt;}. The set &lt;span style="font-style: italic;"&gt;Σ'&lt;/span&gt; defined as&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;Σ'&lt;/span&gt; = {&lt;span style="font-style: italic;"&gt;aσ&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;σa&lt;/span&gt; : &lt;span style="font-style: italic;"&gt;σ&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;is a minimal permutation cover of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Proof.&lt;/span&gt; &lt;span style="font-style: italic;"&gt;Σ'&lt;/span&gt; is a permutation cover: let &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; be a subset of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;; if &lt;span style="font-style: italic;"&gt;a&lt;/span&gt; is not in &lt;span style="font-style: italic;"&gt;X&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; is covered by some permutation &lt;span style="font-style: italic;"&gt;σ&lt;/span&gt; of &lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt; and a fortiori by &lt;span style="font-style: italic;"&gt;σa&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;Σ'&lt;/span&gt;; if &lt;span style="font-style: italic;"&gt;a&lt;/span&gt; belongs to &lt;span style="font-style: italic;"&gt;X&lt;/span&gt;, some  &lt;span style="font-style: italic;"&gt;σ&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt; covers &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; − {&lt;span style="font-style: italic;"&gt;a&lt;/span&gt;} and then &lt;span style="font-style: italic;"&gt;aσ&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;Σ'&lt;/span&gt; covers &lt;span style="font-style: italic;"&gt;X&lt;/span&gt;. &lt;span style="font-style: italic;"&gt;Σ'&lt;/span&gt; is minimal as  |&lt;span style="font-style: italic;"&gt;Σ'&lt;/span&gt;| = 2|&lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt;| = 2 &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1, floor((&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1)/2)) = 2 &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1, &lt;span style="font-style: italic;"&gt;N&lt;/span&gt;/2 − 1) = &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;/2) = &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,floor(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;/2)), where the equalities depend on basic properties of binomial coefficients and the fact that &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; is even.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;This result reduces the problem to &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; odd. The &lt;a href="http://mathworld.wolfram.com/HasseDiagram.html"&gt;Hasse diagram&lt;/a&gt; of the powerset of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; when &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; is odd looks like this (the particular case depicted is &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; = 5):&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_rqhF_8E1nlA/SNOWxkOiD4I/AAAAAAAAAVg/BjgiJOGcsPM/s1600-h/hasse_5.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://2.bp.blogspot.com/_rqhF_8E1nlA/SNOWxkOiD4I/AAAAAAAAAVg/BjgiJOGcsPM/s400/hasse_5.png" alt="" id="BLOGGER_PHOTO_ID_5247703769099145090" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;We name the layers of the diagram &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;0&lt;/sub&gt;,...,&lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;&lt;/sub&gt;. &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt; consists of the subsets of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; with cardinality &lt;span style="font-style: italic;"&gt;i&lt;/span&gt;. We have |&lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt;| = &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;). A permutation covers exactly one element of each &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;i&lt;/span&gt;&lt;/sub&gt;.  Similarly, a tuple &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt; of &lt;span style="font-style: italic;"&gt;n&lt;/span&gt; non-repeating elements covers exactly one element of each of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;0&lt;/sub&gt;,...,&lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/sub&gt;. So, a minimal tuple cover of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;0&lt;/sub&gt;,...,&lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/sub&gt; can be proved to have as many elements as the largest layer covered. The next result shows how to extend a minimal tuple cover of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;0&lt;/sub&gt;,...,&lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1)/2 &lt;/sub&gt;, which has cardinality &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1)/2) = &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,floor(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;/2)) and spreads over the bottom half of the powerset of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;, to a minimal permutation cover of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Theorem.&lt;/span&gt; Assume that &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; is odd and set &lt;span style="font-style: italic;"&gt;M&lt;/span&gt; = (&lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1)/2. Let &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt; be a minimal set of &lt;span style="font-style: italic;"&gt;M&lt;/span&gt;-tuples of non-repeating elements covering &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;0&lt;/sub&gt; &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; ... &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;M&lt;/span&gt;&lt;/sub&gt;, and &lt;span style="font-style: italic;"&gt;d&lt;/span&gt; : &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;M&lt;/span&gt;&lt;/sub&gt; → &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;M&lt;/span&gt;&lt;/sub&gt; a bijection with &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; ∩ &lt;span style="font-style: italic;"&gt;d&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;X&lt;/span&gt;) = Ø for all &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;M&lt;/span&gt;&lt;/sub&gt;. The set &lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt; defined as&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt; = { &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;·&lt;span style="font-style: italic;"&gt;a&lt;/span&gt;·reverse(&lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt;) : &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt; belong to &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;, range(&lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;d&lt;/span&gt;(range(&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;)), &lt;span style="font-style: italic;"&gt;a&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; − range(&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;)  − range(&lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt;)}&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;is a minimal permutation cover of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Proof.&lt;/span&gt; We see first that to each &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt; there corresponds exactly one permutation  &lt;span style="font-style: italic;"&gt;σ&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;&lt;/sub&gt; of the form &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;·&lt;span style="font-style: italic;"&gt;a&lt;/span&gt;·reverse(&lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt;) described above: since &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt; is minimal each element of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;M&lt;/span&gt;&lt;/sub&gt; is covered by one and only one element of &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;; so, for each &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt; there is exactly one &lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt; covering &lt;span style="font-style: italic;"&gt;d&lt;/span&gt;(range(&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;)). We prove now that &lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt; covers &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;: let &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; be a subset of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;; if |&lt;span style="font-style: italic;"&gt;X&lt;/span&gt;| ≤ &lt;span style="font-style: italic;"&gt;M&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; is covered by some &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt; and hence also by the associated &lt;span style="font-style: italic;"&gt;σ&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;&lt;/sub&gt;; if |&lt;span style="font-style: italic;"&gt;X&lt;/span&gt;| &amp;gt; &lt;span style="font-style: italic;"&gt;M&lt;/span&gt;, the set &lt;span style="font-style: italic;"&gt;Y&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; − &lt;span style="font-style: italic;"&gt;X&lt;/span&gt; has |&lt;span style="font-style: italic;"&gt;Y&lt;/span&gt;| ≤ &lt;span style="font-style: italic;"&gt;M&lt;/span&gt; and is thus covered by some &lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;; as we have seen, &lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt; univocally determines a permutation &lt;span style="font-style: italic;"&gt;σ&lt;/span&gt; = &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;·&lt;span style="font-style: italic;"&gt;a&lt;/span&gt;·reverse(&lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt;), and &lt;span style="font-style: italic;"&gt;σ&lt;/span&gt; obviously covers &lt;span style="font-style: italic;"&gt;X&lt;/span&gt;, as all the elements of &lt;span style="font-style: italic;"&gt;Y&lt;/span&gt; are packed at the tail of the permutation. Finally, &lt;span style="font-style: italic;"&gt;Σ &lt;/span&gt;is minimal, since |&lt;span style="font-style: italic;"&gt;Σ&lt;/span&gt;|= |&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;| and we saw before that |&lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt;| = &lt;span style="font-style: italic;"&gt;C&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;,floor(&lt;span style="font-style: italic;"&gt;N&lt;/span&gt;/2)).&lt;/p&gt;&lt;p style="text-align: justify;"&gt;The mapping &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt; → &lt;span style="font-style: italic;"&gt;σ&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;&lt;/sub&gt; just defined can be described in a more algorithmic fashion as follows:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Find the &lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt; in &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt; covering &lt;span style="font-style: italic;"&gt;d&lt;/span&gt;(range(&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;)).&lt;/li&gt;&lt;li&gt;Let &lt;span style="font-style: italic;"&gt;a&lt;/span&gt; be the only element of &lt;span style="font-style: italic;"&gt;S&lt;/span&gt; not present in &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt; nor &lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt;.&lt;/li&gt;&lt;li&gt;&lt;span style="font-style: italic;"&gt;σ&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;&lt;/sub&gt; := &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;·&lt;span style="font-style: italic;"&gt;a&lt;/span&gt;·reverse(&lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt;).&lt;/li&gt;&lt;/ol&gt;&lt;p style="text-align: justify;"&gt;Note that the last theorem extends easily to &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; even: in this case, the permutations have the form &lt;span style="font-style: italic;"&gt;τ&lt;/span&gt;·reverse(&lt;span style="font-style: italic;"&gt;τ'&lt;/span&gt;), i.e. there is no middle element &lt;span style="font-style: italic;"&gt;a&lt;/span&gt;. However, it is more efficient to reduce the case &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; even to &lt;span style="font-style: italic;"&gt;N&lt;/span&gt; − 1 using the first theorem of this entry.&lt;br /&gt;&lt;/p&gt;&lt;div style="text-align: justify;"&gt;Now we are left with the tasks of designing an algorithm to generate a tuple set &lt;span style="font-style: italic;"&gt;Τ&lt;/span&gt; covering &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;0&lt;/sub&gt; &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; ... &lt;span style="font-family:arial;"&gt;U&lt;/span&gt; &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;M&lt;/sub&gt; and finding a suitable bijection &lt;span style="font-style: italic;"&gt;d&lt;/span&gt; : &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;M&lt;/span&gt;&lt;/sub&gt; → &lt;span style="font-style: italic;"&gt;S&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;M&lt;/span&gt;&lt;/sub&gt;.&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/5161835866730687818/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=5161835866730687818" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/5161835866730687818?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/5161835866730687818?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/09/generating-permutation-covers-part-i.html" title="Generating permutation covers: part I" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/_rqhF_8E1nlA/SNOWxkOiD4I/AAAAAAAAAVg/BjgiJOGcsPM/s72-c/hasse_5.png" height="72" width="72" /><thr:total>1</thr:total></entry><entry gd:etag="W/&quot;A0QNSX4_eip7ImA9WxNSFkQ.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-2639703929408012162</id><published>2008-09-22T02:00:00.006+02:00</published><updated>2009-08-31T08:16:38.042+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-31T08:16:38.042+02:00</app:edited><title>Profiling stable_vector</title><content type="html">&lt;p style="text-align: justify;"&gt;The two random access containers of the C++ standard library, &lt;code&gt;std::vector&lt;/code&gt; and &lt;code&gt;std::deque&lt;/code&gt;, have the same big-O complexity as &lt;a href="http://bannalia.blogspot.com/2008/09/introducing-stablevector.html"&gt;&lt;code&gt;stable_vector&lt;/code&gt;&lt;/a&gt; for the following operations:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;push_back&lt;/code&gt;: amortized constant&lt;/li&gt;&lt;li&gt;middle &lt;code&gt;insert&lt;/code&gt;: O(&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;)&lt;/li&gt;&lt;li&gt;&lt;code&gt;operator[]&lt;/code&gt;: constant&lt;/li&gt;&lt;/ul&gt;&lt;p style="text-align: justify;"&gt;Yet the differences in actual performance vary wildly. I have written a &lt;a href="http://joaquinlopezmunoz.googlepages.com/prof_stable_vector.cpp"&gt;profile program&lt;/a&gt; that measures the time taken by the three containers at these operations for two different value types: &lt;code&gt;int&lt;/code&gt; and a "heavy" user defined type &lt;code&gt;bigobj&lt;/code&gt;  holding an &lt;code&gt;int&lt;/code&gt; and an &lt;code&gt;std::string&lt;/code&gt;. The program was built with release mode settings with MSVC++ 8.0 and GNU GCC 3.4.4 for Cygwin/MinGW. Resulting times are summarized in the following table: values are normalized to the minimum among the three containers.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;table style="padding: 0pt; margin-left: auto; margin-right: auto; width: 80%; text-align: center;" border="1"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td colspan="7"&gt;MSVC++ 8.0&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;td colspan="3"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;int&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td colspan="3"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;bigobj&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="width: 14%;"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;std::&lt;br /&gt;vector&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="width: 14%;"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;std::&lt;br /&gt;deque&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="width: 14%;"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;stable_&lt;br /&gt;vector&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="width: 14%;"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;std::&lt;br /&gt;vector&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="width: 14%;"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;std::&lt;br /&gt;deque&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="width: 14%;"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;stable_&lt;br /&gt;vector&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;push_back&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;1.00&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;4.06&lt;/td&gt;&lt;td&gt;21.37&lt;br /&gt;&lt;/td&gt;&lt;td style="font-weight: bold;"&gt;1.00&lt;/td&gt;&lt;td&gt;1.09&lt;br /&gt;&lt;/td&gt;&lt;td&gt;1.26&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;insert&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;1.00&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;4.24&lt;/td&gt;&lt;td&gt;5.29&lt;br /&gt;&lt;/td&gt;&lt;td&gt;1.50&lt;br /&gt;&lt;/td&gt;&lt;td&gt;1.73&lt;br /&gt;&lt;/td&gt;&lt;td style="font-weight: bold;"&gt;1.00&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;operator[]&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;1.00&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;3.35&lt;br /&gt;&lt;/td&gt;&lt;td&gt;1.01&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;1.00&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;3.27&lt;br /&gt;&lt;/td&gt;&lt;td&gt;1.09&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td colspan="7"&gt;GNU GCC 3.4.4&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;td colspan="3"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;int&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td colspan="3"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;bigobj&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="width: 14%;"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;std::&lt;br /&gt;vector&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="width: 14%;"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;std::&lt;br /&gt;deque&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="width: 14%;"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;stable_&lt;br /&gt;vector&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="width: 14%;"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;std::&lt;br /&gt;vector&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="width: 14%;"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;std::&lt;br /&gt;deque&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="width: 14%;"&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;stable_&lt;br /&gt;vector&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;push_back&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;1.09&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;1.00&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;91.57&lt;br /&gt;&lt;/td&gt;&lt;td&gt;1.08&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;1.00&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-family:monospace;"&gt;1.14&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;insert&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;1.00&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;1.30&lt;br /&gt;&lt;/td&gt;&lt;td&gt;5.17&lt;br /&gt;&lt;/td&gt;&lt;td&gt;21.38&lt;br /&gt;&lt;/td&gt;&lt;td&gt;21.36&lt;br /&gt;&lt;/td&gt;&lt;td style="font-weight: bold;"&gt;1.00&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-size:78%;"&gt;&lt;code&gt;operator[]&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;1.00&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;41.04&lt;br /&gt;&lt;/td&gt;&lt;td&gt;1.11&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;1.00&lt;/span&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;81.68&lt;br /&gt;&lt;/td&gt;&lt;td&gt;2.03&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;For small objects like &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;std::vector&lt;/code&gt; is, as expected, the fastest or very close to the fastest at every operation. &lt;code&gt;push_back&lt;/code&gt; takes much longer for &lt;code&gt;stable_vector&lt;/code&gt; than &lt;code&gt;std::deque&lt;/code&gt; presumably because the latter allocates element chunks thus amortizing allocation time whereas &lt;code&gt;stable_vector&lt;/code&gt; performs an allocation for every new node. The poor performance of &lt;code&gt;stable_vector&lt;/code&gt; at &lt;code&gt;&lt;/code&gt;&lt;code&gt;insert&lt;/code&gt; can be attributed to the operation of adjustment of &lt;a href="http://bannalia.blogspot.com/2008/08/stable-vectors.html"&gt;"up" pointers&lt;/a&gt; with each insertion  operation, which is considerably slower than the simple &lt;code&gt;int&lt;/code&gt; copying incurred by the other two containers. Finally, note that random access is very slow for &lt;code&gt;std::deque&lt;/code&gt; because it involves some  non-trivial arithmetical calculations, whereas &lt;code&gt;std::vector&lt;/code&gt; and &lt;code&gt;stable_vector&lt;/code&gt; only perform simple pointer offsets and indirections.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;When storing heavy objects with expensive copy, the situation changes somewhat. The fact that &lt;code&gt;stable_vector&lt;/code&gt; does not internally copy elements upon resizing or shrinking improves its relative performance with respect to the other two containers at &lt;code&gt;push_back&lt;/code&gt;, and specially at &lt;code&gt;insert&lt;/code&gt;, where &lt;code&gt;stable_vector&lt;/code&gt; is the fastest.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;This basic analysis suggests that the main performance handicap of &lt;code&gt;stable_vector&lt;/code&gt; stems from the fact that it is a node-based container, as opposed to the chunk-based nature of &lt;code&gt;std::vector&lt;/code&gt; and &lt;code&gt;std::deque&lt;/code&gt;. This drawback might be alleviated by the use of &lt;a href="http://www.drivehq.com/web/igaztanaga/allocplus/#Chapter2"&gt;&lt;span style="font-style: italic;"&gt;burst-allocation&lt;/span&gt;&lt;/a&gt;, as  &lt;a href="http://lists.boost.org/Archives/boost/2008/09/142371.php"&gt;proposed by Ion Gaztañaga&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/2639703929408012162/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=2639703929408012162" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/2639703929408012162?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/2639703929408012162?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/09/profiling-stablevector.html" title="Profiling &lt;code&gt;stable_vector&lt;/code&gt;" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;A04CRH87eip7ImA9WxRSF0w.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-5038556422816687906</id><published>2008-09-18T07:11:00.000+02:00</published><updated>2008-09-18T08:39:25.102+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-09-18T08:39:25.102+02:00</app:edited><title>Too powerful to evolve</title><content type="html">&lt;p style="text-align: justify;"&gt;Paragraph 1.4.8 of the C++ standard (&lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2723.pdf"&gt;draft copy&lt;/a&gt;) states that&lt;/p&gt;&lt;blockquote&gt;&lt;span style="font-style: italic;"&gt;A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any well-formed program.&lt;/span&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;It turns out that, strictly speaking, this unexpectedly bans many possible extensions an implementation of the C++ standard library might provide; C++ is such a powerful beast that a well-formed program might be made to depend on the non-existence of certain entities.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Function and function templates.&lt;/span&gt; The following program&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;‎&lt;br /&gt;‎using namespace std;&lt;br /&gt;‎&lt;br /&gt;‎int any_of(int count,int n1,...){/*...*/}&lt;br /&gt;‎&lt;br /&gt;‎int main()&lt;br /&gt;‎{&lt;br /&gt;‎  int n=any_of(2,1023,233);&lt;br /&gt;‎}&lt;br /&gt;‎&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;is valid C++03, but will fail with the upcoming C++0x revision of the standard, as this includes a function template &lt;code&gt;any_of&lt;/code&gt; within &lt;code&gt;&amp;lt;algorithm&amp;gt;&lt;/code&gt;:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎template &amp;lt;class InputIterator, class Predicate&amp;gt;&lt;br /&gt;‎bool any_of(InputIterator first, InputIterator last, Predicate pred);&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;that takes precedence over the user defined &lt;code&gt;any_of&lt;/code&gt; due to the &lt;code&gt;using namespace std&lt;/code&gt; directive.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Classes and class templates.&lt;/span&gt; Similarly, this C++03 valid program&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎#include &amp;lt;functional&amp;gt;&lt;br /&gt;‎&lt;br /&gt;‎using namespace std;&lt;br /&gt;‎&lt;br /&gt;‎template&amp;lt;typename T&amp;gt;&lt;br /&gt;‎struct hash&lt;br /&gt;‎{&lt;br /&gt;‎  size_t operator()(const T&amp;amp; t){/*...*/}&lt;br /&gt;‎};&lt;br /&gt;‎&lt;br /&gt;‎int main()&lt;br /&gt;‎{&lt;br /&gt;‎  hash&amp;lt;int&amp;gt;   h;&lt;br /&gt;‎  size_t s=h(983265);&lt;br /&gt;‎}&lt;br /&gt;‎&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;will fail with C++0x due to the presence of an homonym &lt;code&gt;hash&lt;/code&gt; in &lt;code&gt;&amp;lt;functional&amp;gt;&lt;/code&gt;.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Member functions.&lt;/span&gt; The previous situations can be avoided if non-standard extensions are declared in separate headers or belong to a namespace other than &lt;code&gt;std&lt;/code&gt;. These measures cannot be taken, however, when we are adding members to a standard class. The following is a totally artificial yet valid C++03 program:&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="font-size:85%;"&gt;‎#include &amp;lt;vector&amp;gt;&lt;br /&gt;‎&lt;br /&gt;‎template&amp;lt;class Vector&amp;gt;&lt;br /&gt;‎int foo(char x){/*...*/}&lt;br /&gt;‎&lt;br /&gt;‎template &amp;lt;typename T,void (T::*)()&amp;gt; struct mfp{};&lt;br /&gt;‎&lt;br /&gt;‎template&amp;lt;class Vector&amp;gt;&lt;br /&gt;‎void* foo(int x,mfp&amp;lt;Vector,&amp;amp;Vector::shrink_to_fit&amp;gt;* =0){/*...*/}&lt;br /&gt;‎&lt;br /&gt;‎int main()&lt;br /&gt;‎{&lt;br /&gt;‎  int x=foo&amp;lt;std::vector&amp;lt;int&amp;gt; &amp;gt;(0);&lt;br /&gt;‎}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p style="text-align: justify;"&gt;The so-called &lt;a href="http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error"&gt;SFINAE&lt;/a&gt; rule prevents the second overload of &lt;code&gt;foo&lt;/code&gt; to be selected because &lt;code&gt;vector&lt;/code&gt; has no member function called &lt;code&gt;shrink_to_fit&lt;/code&gt;; but it does in C++0x, rendering the program invalid (the second &lt;code&gt;foo&lt;/code&gt; return type is not convertible to &lt;code&gt;int&lt;/code&gt;).&lt;/p&gt;Of course this is not a serious objection to the C++ standard text: 1.4.8 could always be rephrased to restrict it to &lt;i&gt;reasonable&lt;/i&gt; well-formed programs, for some definition of "reasonable". But it can be instructive to reflect on how C++ has become so powerful (some would say complex) that such devious tricks can emerge (and, in &lt;a href="http://www.boost.org/libs/utility/enable_if.html"&gt;some cases&lt;/a&gt;, be benefited from).</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/5038556422816687906/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=5038556422816687906" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/5038556422816687906?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/5038556422816687906?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/09/too-powerful-to-evolve_18.html" title="Too powerful to evolve" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;C0IHRXs6fSp7ImA9WxBVFEU.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-1354686027326212050</id><published>2008-09-14T20:28:00.001+02:00</published><updated>2010-02-18T09:25:34.515+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-18T09:25:34.515+01:00</app:edited><title>A causality calculus</title><content type="html">&lt;p style="text-align: justify;"&gt;Let us design a first-order calculus including causality as non-truth-functional connective for which causality semantics is defined at the same level as the rest of the (truth-functional) connectives, according to the philosophical position stated in a &lt;a href="http://bannalia.blogspot.com/2008/09/causality-as-primitive-concept.html"&gt;previous entry&lt;/a&gt;. We assume we are given an infinite set of variables &lt;span style="font-style: italic;"&gt;V&lt;/span&gt; and a set &lt;span style="font-style: italic; font-weight: bold;font-family:arial;font-size:100%;"  &gt;R&lt;/span&gt; of predicate symbols with associated arities.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Propositional constants:&lt;/span&gt; &lt;span style="font-style: italic;"&gt;F&lt;/span&gt; (falsehood).&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Connectives:&lt;/span&gt; → (only if), &amp;gt; (causal implication).&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Quantifiers:&lt;/span&gt; &lt;span style="font-weight: bold;font-family:arial;" &gt;V&lt;/span&gt; (for all).&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Axioms and rules of inference:&lt;/span&gt; the usual ones for standard &lt;a href="http://en.wikipedia.org/wiki/First-order_logic"&gt;first-order calculus&lt;/a&gt; plus the following axiom schemas dealing with causality:&lt;/p&gt;&lt;p style="text-align: center;"&gt;(C&lt;sub&gt;1&lt;/sub&gt;) &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; &amp;gt; &lt;span style="font-style: italic;"&gt;q&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;q&lt;/span&gt; &amp;gt; &lt;span style="font-style: italic;"&gt;r&lt;/span&gt; → &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; &amp;gt; &lt;span style="font-style: italic;"&gt;r&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;(C&lt;sub&gt;2&lt;/sub&gt;) (&lt;span style="font-style: italic;"&gt;p&lt;/span&gt; &amp;gt; &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;) → &lt;span style="font-style: italic;"&gt;F&lt;/span&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;(C&lt;sub&gt;3&lt;/sub&gt;) &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; &amp;gt; &lt;span style="font-style: italic;"&gt;q&lt;/span&gt;  → (&lt;span style="font-style: italic;"&gt;p&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;q&lt;/span&gt;)&lt;/p&gt;&lt;p style="text-align: center;"&gt;(C&lt;sub&gt;4&lt;/sub&gt;) &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; &amp;gt; &lt;span style="font-style: italic;"&gt;r&lt;/span&gt; → (&lt;span style="font-style: italic;"&gt;p&lt;/span&gt; or &lt;span style="font-style: italic;"&gt;q&lt;/span&gt;) &amp;gt; &lt;span style="font-style: italic;"&gt;r &lt;/span&gt;&lt;/p&gt;&lt;p style="text-align: center;"&gt;(C&lt;sub&gt;5&lt;/sub&gt;) &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; &amp;gt; (&lt;span style="font-style: italic;"&gt;q&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;r&lt;/span&gt;) → &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; &amp;gt; &lt;span style="font-style: italic;"&gt;q&lt;/span&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;These axioms deserve some comment: C&lt;sub&gt;1&lt;/sub&gt; states the usual transitivity of causation, while C&lt;sub&gt;2&lt;/sub&gt; says that no event causes itself; C&lt;sub&gt;3&lt;/sub&gt; allows us to speak about causation only among events actually taking place, which makes this a "realist" semantics. C&lt;sub&gt;4&lt;/sub&gt; and C&lt;sub&gt;5&lt;/sub&gt; translate some usual properties of material implication to causal implication.&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="text-align: justify;"&gt; &lt;/div&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Semantics:&lt;/span&gt; a valuation &lt;span style="font-style: italic;"&gt;v&lt;/span&gt; specifies several mappings:&lt;/p&gt;&lt;ul style="text-align: justify;"&gt;&lt;li&gt;From &lt;span style="font-style: italic;"&gt;V&lt;/span&gt; to some set &lt;span style="font-style: italic;"&gt;U&lt;/span&gt;.&lt;/li&gt;&lt;li&gt;From each &lt;span style="font-style: italic;"&gt;n&lt;/span&gt;-ary predicate in &lt;span style="font-style: italic; font-weight: bold;font-family:arial;font-size:100%;"  &gt;R&lt;/span&gt; to some &lt;span style="font-style: italic;"&gt;n&lt;/span&gt;-ary relation in &lt;span style="font-style: italic;"&gt;U&lt;/span&gt;.&lt;/li&gt;&lt;li&gt;From each formula to the set {&lt;span style="font-style: italic;"&gt;F&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;T&lt;/span&gt;}.&lt;/li&gt;&lt;/ul&gt;&lt;p style="text-align: justify;"&gt;and obeys the following rules:&lt;/p&gt;&lt;ul style="text-align: justify;"&gt;&lt;li&gt;&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;F&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;F&lt;/span&gt;.&lt;/li&gt;&lt;li&gt;&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;φ&lt;/span&gt; → &lt;span style="font-style: italic;"&gt;ψ&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; iff &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;φ&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;F&lt;/span&gt; or &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;ψ&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;T&lt;/span&gt;.&lt;/li&gt;&lt;li&gt;&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;R&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;t&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt;,...,&lt;span style="font-style: italic;"&gt;t&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/sub&gt;)) = &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; iff (&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;t&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt;),...,&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;t&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;n&lt;/span&gt;&lt;/sub&gt;)) is in &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;R&lt;/span&gt;).&lt;/li&gt;&lt;li&gt;&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;(&lt;span style="font-weight: bold;font-family:arial;" &gt;V&lt;/span&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; &lt;span style="font-style: italic;"&gt;φ&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; iff &lt;span style="font-style: italic;"&gt;v'&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;φ&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; for every valuation &lt;span style="font-style: italic;"&gt;v'&lt;/span&gt; identical to &lt;span style="font-style: italic;"&gt;v&lt;/span&gt; except possibly at &lt;span style="font-style: italic;"&gt;x&lt;/span&gt;.&lt;/li&gt;&lt;li&gt;The relation on formulas of the calculus defined by &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;φ&lt;/span&gt; &amp;gt; &lt;span style="font-style: italic;"&gt;ψ&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;T&lt;/span&gt; is consistent with axiom schemas C&lt;sub&gt;1&lt;/sub&gt;,..., C&lt;sub&gt;5&lt;/sub&gt;. In particular, this implies that the relation thus defined is a strict partial order.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="text-align: justify;"&gt;As usual, a statement is valid if it is true for any valuation. It is easy to see that there are valuations with the properties described above: just take any valuation for standard first-order predicate calculus and augment it with &lt;span style="font-style: italic;"&gt;v&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;φ&lt;/span&gt; &amp;gt; &lt;span style="font-style: italic;"&gt;ψ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;F&lt;/span&gt; for all &lt;span style="font-style: italic;"&gt;φ&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;ψ&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt; (we may call this a Humean valuation).&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/1354686027326212050/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=1354686027326212050" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/1354686027326212050?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/1354686027326212050?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/09/causality-calculus.html" title="A causality calculus" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CEMMQXg7fip7ImA9WxRSEEw.&quot;"><id>tag:blogger.com,1999:blog-2715968472735546962.post-8015991744152069094</id><published>2008-09-10T04:08:00.000+02:00</published><updated>2008-09-10T04:08:00.606+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-09-10T04:08:00.606+02:00</app:edited><title>Introducing stable_vector</title><content type="html">&lt;p style="text-align: justify;"&gt;We present &lt;code&gt;stable_vector&lt;/code&gt;, a fully STL-compliant &lt;span style="font-style: italic;"&gt;stable&lt;/span&gt; container that provides most of the features of &lt;code&gt;std::vector&lt;/code&gt; except element contiguity. The underlying data structure has been discussed in a &lt;a href="http://bannalia.blogspot.com/2008/08/stable-vectors.html"&gt;prior entry&lt;/a&gt;.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://joaquinlopezmunoz.googlepages.com/stable_vector.hpp"&gt;&lt;code&gt;stable_vector.hpp&lt;/code&gt;&lt;/a&gt;. The code uses &lt;a href="http://boost.org/"&gt;Boost&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;&lt;a href="http://joaquinlopezmunoz.googlepages.com/test_stable_vector.cpp"&gt;&lt;code&gt;test_stable_vector.cpp&lt;/code&gt;&lt;/a&gt;: Test suite for &lt;code&gt;stable_vector&lt;/code&gt;.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="text-align: justify;"&gt;In the following we are using the terminology of the C++98 standard.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;General properties.&lt;/span&gt; &lt;code&gt;stable_vector&lt;/code&gt; satisfies all the requirements of a &lt;span style="font-style: italic;"&gt;container&lt;/span&gt;, a &lt;span style="font-style: italic;"&gt;reversible container&lt;/span&gt; and a &lt;span style="font-style: italic;"&gt;sequence&lt;/span&gt; and provides all the optional operations present in &lt;code&gt;std::vector&lt;/code&gt;. Like &lt;code&gt;std::vector&lt;/code&gt;, &lt;codestable_vector&gt; iterators are random access. &lt;code&gt;stable_vector&lt;/code&gt; does not provide &lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#69"&gt;element contiguity&lt;/a&gt;; in exchange for this &lt;span class="ital-inline"&gt;absence, the container is stable, i.e. references and iterators to  an element of a &lt;code&gt;stable_vector&lt;/code&gt; &lt;/span&gt;remain valid as long as the element is not erased, and an iterator that has been assigned the return value of &lt;code&gt;end()&lt;/code&gt; always remain valid until the destruction of the associated &lt;span class="ital-inline"&gt; &lt;code&gt;stable_vector&lt;/code&gt;.&lt;/span&gt;&lt;/codestable_vector&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Operation complexity.&lt;/span&gt; The big-&lt;span style="font-style: italic;"&gt;O&lt;/span&gt; complexities of  &lt;code&gt;stable_vector&lt;/code&gt; operations match exactly those of &lt;code&gt;std::vector&lt;/code&gt;. In general, insertion/deletion is constant time at the end of the sequence and linear elsewhere. Unlike &lt;code&gt;std::vector&lt;/code&gt;, &lt;code&gt;stable_vector&lt;/code&gt; does not internally perform any &lt;code&gt;value_type&lt;/code&gt; destruction, copy or assignment operations other than those exactly corresponding to the insertion of new elements or deletion of stored elements, which can sometimes compensate in terms of performance for the extra burden of doing more pointer manipulation and an additional allocation per element.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Exception safety.&lt;/span&gt; (according to &lt;a href="http://www.boost.org/community/exception_safety.html"&gt;Abrahams' terminology&lt;/a&gt;) As &lt;code&gt;stable_vector&lt;/code&gt; does not internally copy elements around, some operations provide stronger exception safety guarantees than in &lt;code&gt;std::vector&lt;/code&gt;:&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;table style="padding: 0pt; margin-left: auto; margin-right: auto; width: 80%; text-align: center; height: 201px;" border="1"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;span&gt;operation&lt;/span&gt;&lt;/td&gt;&lt;td style="width: 40%;"&gt;exception safety for&lt;br /&gt;&lt;span&gt;&lt;code&gt;std::vector&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/span&gt;&lt;/td&gt;&lt;td style="width: 40%;"&gt;exception safety for&lt;br /&gt;&lt;code&gt;stable_vector&lt;span&gt;&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;insert&lt;/code&gt;&lt;/td&gt;&lt;td&gt;strong unless copy or assignment of &lt;code&gt;T&lt;/code&gt; throw (basic)&lt;br /&gt;&lt;/td&gt;&lt;td&gt;strong&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;erase&lt;/code&gt;&lt;/td&gt;&lt;td&gt;no-throw unless copy or assignment of &lt;code&gt;T&lt;/code&gt; throw (basic)&lt;br /&gt;&lt;/td&gt;&lt;td&gt;no-throw &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Memory overhead.&lt;/span&gt; The C++ standard does not specifiy requirements on memory consumption, but virtually any implementation of &lt;code&gt;std::vector&lt;/code&gt; has the same behavior wih respect to memory usage: the memory allocated by a vector &lt;code&gt;v&lt;/code&gt; with &lt;span style="font-style: italic;"&gt;n&lt;/span&gt; elements of type &lt;code&gt;T&lt;/code&gt; is&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;/sub&gt; = &lt;span style="font-style: italic;"&gt;c&lt;/span&gt;·&lt;span style="font-style: italic;"&gt;e&lt;/span&gt;,&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;where &lt;span style="font-style: italic;"&gt;c&lt;/span&gt; is &lt;code&gt;v.capacity()&lt;/code&gt; and &lt;span style="font-style: italic;"&gt;e&lt;/span&gt; is &lt;code&gt;sizeof(T)&lt;/code&gt;. &lt;span style="font-style: italic;"&gt;c&lt;/span&gt; can be as low as &lt;span style="font-style: italic;"&gt;n&lt;/span&gt; if the user has explicitly reserved the exact capacity required; otherwise, the average value &lt;span style="font-style: italic;"&gt;c&lt;/span&gt; for a growing vector oscillates between  1.25·&lt;span style="font-style: italic;"&gt;n&lt;/span&gt; and 1.5·&lt;span style="font-style: italic;"&gt;n&lt;/span&gt; for typical resizing policies. For &lt;code&gt;stable_vector&lt;/code&gt;, the memory usage is&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;s&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;/sub&gt; = (&lt;span style="font-style: italic;"&gt;c&lt;/span&gt; + 1)&lt;span style="font-style: italic;"&gt;p&lt;/span&gt; + (&lt;span style="font-style: italic;"&gt;n &lt;/span&gt;+ 1)(&lt;span style="font-style: italic;"&gt;e&lt;/span&gt; + &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;),&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;where &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; is the size of a pointer. We have &lt;span style="font-style: italic;"&gt;c&lt;/span&gt; + 1 and  &lt;span style="font-style: italic;"&gt;n &lt;/span&gt;+ 1 rather than &lt;span style="font-style: italic;"&gt;c&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;n&lt;/span&gt; because a dummy node  is needed at the end of the sequence. If we call &lt;span style="font-style: italic;"&gt;f&lt;/span&gt; the capacity to size ratio &lt;span style="font-style: italic;"&gt;c&lt;/span&gt;/&lt;span style="font-style: italic;"&gt;n&lt;/span&gt; and assume that &lt;span style="font-style: italic;"&gt;n&lt;/span&gt; is large enough, we have that&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;s&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;/sub&gt;/&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;/sub&gt; ≈ (&lt;span style="font-style: italic;"&gt;fp&lt;/span&gt; + &lt;span style="font-style: italic;"&gt;e&lt;/span&gt; + &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;)/&lt;span style="font-style: italic;"&gt;fe&lt;/span&gt;.&lt;br /&gt;&lt;/p&gt;&lt;p style="text-align: justify;"&gt;So, &lt;code&gt;stable_vector&lt;/code&gt; uses less memory than &lt;code&gt;std::vector&lt;/code&gt; only when &lt;span style="font-style: italic;"&gt;e&lt;/span&gt; &amp;gt; &lt;span style="font-style: italic;"&gt;p&lt;/span&gt; and the capacity to size ratio exceeds a given threshold:&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;s&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;/sub&gt; &amp;lt; &lt;span style="font-style: italic;"&gt;m&lt;/span&gt;&lt;sub&gt;&lt;span style="font-style: italic;"&gt;v&lt;/span&gt;&lt;/sub&gt; ↔ &lt;span style="font-style: italic;"&gt;f&lt;/span&gt; &amp;gt; (&lt;span style="font-style: italic;"&gt;e&lt;/span&gt; + &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;)/(&lt;span style="font-style: italic;"&gt;e&lt;/span&gt; − &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;). (provided &lt;span style="font-style: italic;"&gt;e&lt;/span&gt; &amp;gt; &lt;span style="font-style: italic;"&gt;p&lt;/span&gt;)&lt;/p&gt;&lt;p style="text-align: justify;"&gt;This threshold approaches typical values of &lt;span style="font-style: italic;"&gt;f&lt;/span&gt; below 1.5 when &lt;span style="font-style: italic;"&gt;e&lt;/span&gt; &amp;gt; 5&lt;span style="font-style: italic;"&gt;p&lt;/span&gt;; in a 32-bit architecture, when &lt;span style="font-style: italic;"&gt;e&lt;/span&gt; &amp;gt; 20 bytes.&lt;/p&gt;&lt;p style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Summary.&lt;/span&gt; &lt;code&gt;stable_vector&lt;/code&gt; is a drop-in replacement for &lt;code&gt;std::vector&lt;/code&gt; providing stability of references and iterators, in exchange for missing element contiguity and also some performance and memory overhead. When the element objects are expensive to move around, the performance overhead can turn into a net performance gain for &lt;code&gt;stable_vector&lt;/code&gt; if many middle insertions or deletions are performed or if resizing is very frequent. Similarly, if the elements are large there are situations when the memory used by &lt;code&gt;stable_vector&lt;/code&gt; can actually be less than required by &lt;code&gt;std::vector&lt;/code&gt;.&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://bannalia.blogspot.com/feeds/8015991744152069094/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2715968472735546962&amp;postID=8015991744152069094" title="10 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/8015991744152069094?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2715968472735546962/posts/default/8015991744152069094?v=2" /><link rel="alternate" type="text/html" href="http://bannalia.blogspot.com/2008/09/introducing-stablevector.html" title="Introducing &lt;code&gt;stable_vector&lt;/code&gt;" /><author><name>Joaquín M López Muñoz</name><uri>http://www.blogger.com/profile/08579853272674211100</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp3.blogger.com/_rqhF_8E1nlA/SBhnkQF0HBI/AAAAAAAAAL0/73p4DZW4km0/S220/joaquin_lopez.jpg" /></author><thr:total>10</thr:total></entry></feed>
