    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Chaos Theory</title>
        <link>https://members.accu.org/index.php/articles/331</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #57 - Oct 2003</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c155/">57</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-155/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+155/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Chaos Theory</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 02 October 2003 22:56:14 +01:00 or Thu, 02 October 2003 22:56:14 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a></h2>
<h3>Part 2</h3>
</div>
<p>In a couple of ways this article represents a return to my past:
both to C++ and to the &quot;Chaos Theory&quot; theme. For the last couple of
years my professional interest has been diverted from C++ to other
areas (specifically to Java, J2EE and development methods). As a
result I've accumulated a backlog of C++ related material waiting
to be read. In particular, I've finally found time to read &quot;Modern
C++ Design&quot; which demonstrates the ability to use the language to
do things at compile time. Other books (like &quot;Generative
Programming&quot;) that I've read during my diversion have also used
these ideas and there are libraries (boost has a fine example) to
support these uses. But I wanted to do more than read and admire
these novel ideas. I wanted to try them out - but I was in search
of a problem.</p>
<p>The problem I chose is one that I wrote about once before - in
the &quot;first&quot; article in a series of articles on &quot;Chaos Theory&quot;.This
was a long time ago, I can't remember why but the rest of the
series never materialised (in fact I can't find a copy of thefirst
article either - but I think it was published in C Vu about ten
years ago.)</p>
<p>Chaos theory is a branch of mathematics that was developed in
the nineteenth century by Poincar&eacute; in an attempt to solve
the problem &quot;Is the Solar System stable?&quot; Although he failed to
solve the problem he made a sufficient dent to be awarded a
significant prize for this work. Towards the end of the last
millennium work on the stability of mathematical systems grew in
importance with theincreasing use of computers to do numerical
modelling.</p>
<p>The types of mathematical model to which chaos theory applies
are those that develop over time and whose current state
dependsupon the past. It gets interesting when this change is
complicated enough that exact solution is infeasible and numerical
modelling is the only approach to getting results. What the
mathematicians showed was that even when it isn't possible to write
down an exact description of the evolution of the model it is still
possible to make useful predictions about the type of
behaviour.</p>
<p>This sounded fun, so I decided to try it for myself and started
writing a series of articles for C Vu. At least I think I did - I
never wrote the second article and I can't find the first! The
first article introduced an easy to understand non-linear system
and demonstrated the application of these predictions. The system
in question takes a pair of numbers and generates a new pair of
numbers - and what chaos theory predicts is that one of three
things will happen:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>There is an infinite non-repeating sequence of number pairs.</p>
</li>
<li>
<p>Eventually the sequence of number pairs settles into a limited
range of values - an &quot;attractor&quot;.</p>
</li>
<li>
<p>From some point in the sequence all the number pairs have the
same value. (This is really a special case of 2)</p>
</li>
</ol>
</div>
<p>(In the particular system I'm writing about the first of these
is extremely implausible and it turns out that case 3 is what
happens.) My thoughts turned to finding these fixed values at
compile time.</p>
<pre class="programlisting">
/*
  &quot;This sentence has eight vowels and
  twenty consonants&quot;

  The above sentence is false because the
  numbers eight and twenty are arbitary
  (and wrong). But we can create a
  sequence of number pairs (Vn, Cn) by
  substituting the numbers into a sentence
  of this form and counting the vowels and
  consonants to get the next pair of
  numbers.

  Continuing to substitute these values
  back into the sentence then one of three
  things must happen:
  1/ The series of pairs (Vn, Cn)
     diverges
  2/ The series of pairs (Vn, Cn)
     loops though a sequence of values
  3/ The series of pairs (Vn, Cn)
     converges to constant values

  The following program executes this
  algorithm *at compile time* to find
  values of (Vn, Cn) which make the
  sentence true and outputs the
  result.
*/

#include &lt;string&gt;
#include &lt;iostream&gt;
namespace {

/*
The first issue to address is that it
isn't possible to count vowels or
consonants in a string at compile
time. Compile time processing is
limited to creating types and
constant integral expressions. There
are two approaches to this that
occur to me: create a type for each
character and represent a sentence
as a typelist or break the sentence
into subsentences representing the
fixed and variable portions and
represent these as types. While
the former is clearly a lot more
general it involves more work and
it is the latter approach to the
problem that I adopted.
So for the variable parts I have:
*/

template&lt;int count&gt;
struct number_as_subsentence;

/*
OK, we'll have to define some
specialisations for this before we can
use it, but this is a useful placeholder
for the full sentence template.
*/

template&lt;int vowels, int consonants&gt;
struct sentence {
  enum {
    no_of_vowels = 11
      + number_as_subsentence&lt;vowels&gt;::no_of_vowels
      + number_as_subsentence&lt;consonants&gt;::no_of_vowels,
    no_of_consonants = 23
      + number_as_subsentence&lt;vowels&gt;::no_of_consonants
      + number_as_subsentence&lt;consonants&gt;::no_of_consonants,
    is_true = no_of_vowels == vowels
      &amp;&amp; no_of_consonants == consonants
  };

  static std::string as_string() {
    static const std::string
          beginning(&quot;This sentence has &quot;);
    static const std::string
          middle(&quot; vowels and &quot;);
    static const std::string
          end(&quot; consonants!&quot;);
    return beginning
      + number_as_subsentence&lt;vowels&gt;::as_string()
      + middle
      + number_as_subsentence&lt;consonants&gt;::as_string()
      + end;
  }
};

/*
This template provides a compile time
mechanism to take a number of vowels
and a number of consonants and determine
the effect of placing them into our
template for a sentence. It will also
construct the corresponding sentence
for us.

What's next? In the original
program there was a loop to keep
trying the sequence of sentences
until we find one that is true. But
that is another thing that we cannot
do at compile time: we can't do
iteration, we have to rework the
algorithm as recursion:
*/

template&lt;int vowels,
         int consonants,
         bool finished = false&gt;
struct calculate_sentence
       : private sentence&lt;vowels,
                          consonants&gt; {
  typedef typename calculate_sentence&lt;
          calculate_sentence::no_of_vowels,
          calculate_sentence::no_of_consonants,
          calculate_sentence::is_true&gt;::result result;
};

/*
This keeps trying new sentences all
right, but we need to end the recursion
(which is what the third parameter is
for). Interestingly one cannot use a
&quot;metaprogramming if_&quot; (like that in the
boost library) here because both the true
and false conditions get instantiated.
With the current approach we just need a
specialisation of calculate_sentence as
follows:
*/

template&lt;int vowels, int consonants&gt;
struct calculate_sentence&lt;vowels,
                          consonants, true&gt; {
  typedef sentence&lt;vowels, consonants&gt; result;
};

/*
That is really all the interesting bits
of the program done. The templates for
describing the numbers are tediously
repetitive - but there is a useful preprocessor
to handle that:
*/

#define NUMBER_AS_SUBSENTENCE(number,\
                      text, vowels, consonants)\
  template&lt;&gt;\
  struct number_as_subsentence&lt;number&gt; {\
    static std::string as_string() {\
      return text;\
    }\
\
    enum {\
      no_of_vowels = vowels,\
      no_of_consonants = consonants\
    };\
  }

  NUMBER_AS_SUBSENTENCE(0,&quot;zero&quot;,2,2);
  NUMBER_AS_SUBSENTENCE(1,&quot;one&quot;,2,1);
  NUMBER_AS_SUBSENTENCE(2,&quot;two&quot;,1,2);
  NUMBER_AS_SUBSENTENCE(3,&quot;three&quot;,2,3);
  ...
  NUMBER_AS_SUBSENTENCE(48,&quot;forty eight&quot;,3,7);
  NUMBER_AS_SUBSENTENCE(49,&quot;forty nine&quot;,3,6);
  NUMBER_AS_SUBSENTENCE(50,&quot;fifty&quot;,1,4);
  #undef NUMBER_AS_SUBSENTENCE
}

/*
To run the program we only need
instantiate the template and output the
result...
*/

int main() {
  std::cout &lt;&lt; calculate_sentence&lt;8, 20&gt;
                  ::result::as_string()
            &lt;&lt; std::endl;
}
</pre>
<p>The full program is available on my website at: <a href=
"http://www.octopull.demon.co.uk/C++/ThisSentence/" target=
"_top">http://www.octopull.demon.co.uk/C++/ThisSentence/</a></p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
