<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Basic Tutorial 22: Designing Equations">

<setdocpatch name="22mDesigningEquations" patch="22mDesigningEquations.maxpat"/>
<previous name="basicchapter21">Controlling Data Flow</previous>
<next name="midichapter01">Basics</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Basics" title="Designing Equations">Creating complex mathematical statements</indexinfo>

<h1>Tutorial 22: Designing Equations</h1>

<h2>Introduction</h2>

<p>This tutorial covers the creation of equations: mathematical statements that evaluate an expression based on input values.  The key to equations in Max is the <o>expr</o> object, which provides a wealth of mathematical functions and a “write your own” mechanism for developing complex statements.</p>

<p>To this point, we have created somewhat complex mathematical processes using individual math objects (<o>+</o>, <o>*</o>, etc.).  However, in many cases, we can collapse a number of objects into a single mathematical equation, eliminating a lot of unnecessary complexity in our patches. The tool used for this simplification is the <o>expr</o> object, which uses a specific syntax for creating an equation that accepts multiple inputs and can produce very complex results.</p>

<p>To open the tutorial patch, click on the <b>Open Tutorial</b> button in the upper right-hand corner of the documentation window.</p>

<h2>Overview of the drawing patch</h2>

<p>Take a look at the tutorial patch to see the work we have ahead of us.  This patch is broken into two encapsulated portions of patcher logic and a drawing routine foran <o>lcd</o> object, also encapsulated. Communication between the parts is accomplished using <o>value</o> objects and pairs of <o>send</o> and <o>receive</o> objects. To see the patch in action, turn on the <o>metro</o> at the left of the patcher using the green <o>toggle</o> box, then hit the space bar to trigger the events at the top of the patch.  The result will vary from small scribbles to large-format abstract line-and-circle drawings.  Each time you hit the space bar, the <o>lcd</o> will clear and a new, related drawing will begin to appear.</p>

<p>The <o>comment</o> box at the top of the patch shows three lines of mathematical equations:<br/>
<m>x = sin(A*y)-z*cos(B*x);</m><br/>
<m>y = z*sin(C*x)-cos(D*y);</m><br/>
<m>z = E*sin(x);</m><br/>
These expressions compute three values (<m>x</m>, <m>y</m>, and <m>z</m>) based on applying some simple trigonometric functions to their <i>previous</i> states as well as five fixed values (<m>A</m>, <m>B</m>, <m>C</m>, <m>D</m>, and <m>E</m>).  When plotted onto the <o>lcd</o> object (with <m>z</m> visualized as a circle of variable size), we can see that a wide variety of patterns can be created.  This equation generates something called a <i>strange attractor</i> - in essence an iterated function where the previous outputs of the equation combine with five variables (called <i>coefficients</i>) to generate a variety of shapes that exhibit chaotic behavior.</p>

<bullet>Double-click the <m>Do_the_math</m> patcher object to open it.</bullet>

<p>Let’s examine each section of this patch.  The lower-left corner of the main patch is obviously the key to making things draw – it generates <m>bang</m> messages from a <o>metro</o> that are sent to the <m>compute</m> and <m>draw</m> <o>receive</o> objects, as well as into a subpatch that slowly varies the color of the drawing (more on this later). After the <m>Changecolor</m> subpatch is evaluated, the <m>Do_the_math</m> subpatch receives a <m>bang</m> via its <o>receive</o> <m>compute</m> object.  This  updates the equation for the next round of drawing by triggering a number of <o>value</o> objects, sending their output through equations encoded in the <o>expr</o> objects.  These then update the <m>x</m>, <m>y</m>, and <m>z</m> values for drawing.  We'll examine how these <o>expr</o> objects work in a moment.</p>

<bullet>Double-click the <m>Draw_shape</m> patcher object to open it.</bullet>

<p>The <o>send</o> object named <m>draw</m> sends <m>bang</m> messages to the drawing routine in the <m>Draw_shape</m> subpatch. This triggers the newly calculated <m>x</m>, <m>y</m> and <m>z</m> values which are scaled, packed into command messages, and then put through a <o>send</o> object to the <o>receive</o> object attached to the <o>lcd</o>, which performs the drawing commands that were generated.</p>

<bullet>Double-click the <m>Generate_coefficients</m> patcher object to open it.</bullet>
<p>Look at the upper left of the main patch. When the <o>key</o> object outputs a <m>32</m> (meaning that the space bar has been depressed), a <o>button</o> is used to generate a <m>bang</m> message that is sent to the <m>Generate_coefficients</m> subpatch  This, in turn, performs seven actions in order from right to left: the generation of 5 <o>random</o> values (<m>E</m> to <m>A</m>) that are scaled from <m>-4.0</m> to <m>4.0</m>, the transmission of a <m>clear</m> message to the <o>lcd</o> object, and a reset of the value contained in the <m>x</m>, <m>y</m> and <m>z</m> <o>value</o> objects. In essence, the space bar is the overall <i>reset</i> key for this patch, in that virtually every segment of the patch is affected when that key is hit.</p>

<h2>Using <o>expr</o> for equation creation</h2>

<bullet>Double-click the <m>Do_the_math</m> patcher object to open it.</bullet>

<p>Now let’s look into the <o>expr</o> objects themselves.  While they look complicated, the argument for each of the <o>expr</o> objects is a relatively simple equation.  The important thing to realize is that the <m>$</m> references simply refer to an incoming message, along with its message <i>type</i> and <i>inlet</i> number.  So, for instance, using a reference named <m>$f1</m> means that the equation should use an incoming message (<m>$</m>) that is a floating-point number (<m>f</m>) retrieved from inlet number 1 (<m>1</m>). Once you realize that the apparent complexity of the equation is simply its reference to incoming messages, the format of the <o>expr</o> statement is a little more comprehensible.</p>

<p>The operations used in our <o>expr</o> statements are limited to the <o>sin</o> (sine function), <o>cos</o> (cosine) and <o>*</o> (multiply) functions; however, there are dozens of operations available.  You can use the common math operators (<o>+</o>, <link name="-" type="refpage">-</link>, <o>*</o> and <o>/</o>), logic (<o>&amp;&amp;</o>, <o>||</o>), bitwise comparison (<o>&amp;</o>, <o>|</o>) and other C language math functions such as <o>abs</o>, <o>sin</o>, <o>cos</o>, and <o>pow</o>.  The documentation provides a complete list of the available functions.</p>

<p>In the case of our tutorial patch, the three <o>expr</o> objects use eight different variables (<m>x</m>, <m>y</m>, <m>z</m>, <m>A</m>, <m>B</m>, <m>C</m>, <m>D</m>, <m>E</m>) to create the three target values (<m>x</m>, <m>y</m>, <m>z</m>).  There is an interesting bit of feedback here: the target variables are not only used for the drawing routine, but are also used for the next round of computation. The five variables <m>A</m> through <m>E</m> only change when the space bar is hit, so they provide an anchor to the drawing routine while the ever-changing <m>x</m>, <m>y</m> and <m>z</m> variables determine the individual line and circle drawings for each round of computation.</p>

<h2>Adding your own equation</h2>

<p>In order to make the drawing a little more interesting, let’s do some creative equation-making and use it to generate a constantly shifting foreground color.  We are going to tap into the <m>x</m>, <m>y</m> and <m>z</m> <o>value</o> objects that are updated on each <m>bang</m> of the <o>metro</o>.</p>

<p>Double-click the <o>patcher</o> object labeled <m>changecolor</m> at the bottom of the patch.  We can see that the foreground color of the <o>lcd</o> is being altered by a trio of <o>drunk</o> objects that are <o>pack</o>ed into an <m>frgb</m> message sent to the <o>lcd</o>.  In here, we are going to use an <o>expr</o> object compute the <m>exp</m> (<b><i>e</i></b> to the power of <i>x</i>) mathematical function.  If you aren’t familiar with the properties of <b><i>e</i></b>, that’s OK – it’s sufficient to know that it is exponential in nature, and that the value will rapidly increase as the input value increases.</p>

<p>Delete the <o>drunk</o> objects, or disconnect them from the <o>inlet</o> and <o>pack</o> objects and move them out of the way.  Create three new <o>value</o> objects with <m>x</m>, <m>y</m>, and <m>z</m> as their respective arguments and connect the <o>inlet</o> object to their inlets.</p>

<p>Now create a new object and type in the following:<br/>
<o>expr </o> <m>int(exp($f1)*5.)</m><br/>
Examining the argument/equation from the inside-out, we see that we are taking an incoming floating-point value (<m>$f1</m>), using the <m>exp</m> function to create a new value (<m>exp($f1)</m>), multiplying it by 5.0 (<m>*5.</m>) then turning it into an integer (<m>int</m>).  The result will be a number that is approximately in the range of color values (0-255), but is predominantly low in that number range.  Copy this <o>expr</o> object two times, then connect the output of the three <o>value</o> objects (<m>x</m>, <m>y</m> and <m>z</m>) to each of the <o>expr</o> objects.  Connect the outlets of the <o>expr</o> objects into the <o>pack</o>, and see what happens.</p>

<p>When we start the <o>metro</o> and <m>clear</m> the <o>lcd</o>, we will see that the drawing is colored, but tends toward the darker colors.  Since the coloring is influenced by the calculated <m>x</m>, <m>y</m> and <m>z</m> coordinates, the colors will tend to be brighter toward the extreme edges of the drawing window.  This is an example of calculating drawing commands that might be difficult using traditional object-based programming, but is made quite easy using <o>expr</o>.</p>

<h2>Summary</h2>

<p>Using the <o>expr</o> object gives us the opportunity to create complex mathematical and logical equations without having to resort to large object programming segments. The huge number of operations that can be performed, in combination with the ability to define and use multiple inputs, allows for interesting results that might otherwise be cumbersome to create.</p>

<seealsolist>
<seealso name="expr">Evaluate a C-like expression</seealso>
</seealsolist>

</chapter>
