<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Basic Tutorial 17: Data Structures And Probability">

<setdocpatch name="17mDataStructuresAndProbability" patch="17mDataStructuresAndProbability.maxpat"/>
<previous name="basicchapter16">Remote Messaging</previous>
<next name="basicchapter18">Data Collections</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Basics" title="Data Structures and Probability">Working with histograms and lookup tables</indexinfo>

<h1>Tutorial 17: Data Structures And Probability</h1>

<h2>Introduction</h2>

<p>In this tutorial, we look at using the <o>itable</o> object for data storage, data transformation and histogram tracking. The <o>itable</o> object can be drawn into, or can be set using various formulae.  We will also look at using the <o>uzi</o> object to quickly generate many <m>bang</m> messages or streams of ascending numbers, both of which can be used to quickly create lots of algorithmically generated data.</p>

<p>A two-dimensional data structure (like <o>itable</o>) gives us the opportunity to create data transformations that are easy to set up.  These <i>transfer functions</i> allow you to create irregular curves, probability tables that weigh the output based on the input, and random patterns that are easy to visualize.  Additionally, the introduction of the <o>uzi</o> object for the quick generation of procedural messages helps us find a way to create the functions necessary to load these <o>itable</o> objects in interesting ways.</p>

<p>The <o>itable</o> object used in this tutorial is virtually equivalent to the <o>table</o> object, which has its own window and can be typed into a regular object box.  The <o>itable</o> is used here simply because it appears embedded visually in a patcher, rather than in a separate editor window.</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>Basic data structures with <o>itable</o></h2>

<p>In this tutorial's patcher, you will see a few display/drawing patches (labeled with numbers) and several small calculations (labeled with letters).  We will start working with the patch area labeled <b>1</b>.  This patch features an <o>itable</o> object; an object that provides a visual interface to a two-dimensional data structure called a <o>table</o>.  The <o>table</o>/<o>itable</o> structure gives us an X/Y combination and a simple interface to this data: send in an number that represents an index on the X axis, and you will receive the corresponding Y axis value.</p>

<p>When you first open the patch, the <o>itable</o> contains a random distribution of values.  If we move the <o>slider</o> at the top of the patch, it scans the <o>itable</o> values, and outputs the Y value from its left outlet for the X position represented by the <o>slider</o>.  We have this connected to a <o>number</o> box and <o>slider</o>, so we can see the results of the transfer function described by the <o>itable</o>.  Next to the input <o>slider</o> are a pair of <o>message</o> boxes with two-integer lists; these will set the location of the first integer (X) to the value of the second integer (Y).  Thus, with the first <o>message</o> box, the value at location <m>50</m> will be set to <m>80</m>.  If we click on this <o>message</o> box, then move the <o>slider</o> (or <o>number</o> box) to <m>50</m>, we can see that the output of the <o>itable</o> is <m>80</m>.  Likewise, after we click on the second <o>message</o> box (<m>100 20</m>), we can see that an input value of <m>100</m> outputs a value of <m>20</m>.</p>

<p>The <m>clear</m> message “erases” the <o>itable</o> view, but values are still intrinsic to the <o>itable</o>; when cleared, the <o>itable</o> simply assigns a <m>0</m> value to every possible data point.  Another way to change the values of the <o>itable</o> is to draw within the <o>itable</o> itself.  If you place the mouse inside the <o>itable</o> and click-drag the mouse, you can draw in the function contents you desire.  If you need to make straight lines, you can hold down the shift key while you move the mouse, and click the mouse to commit the new line.  Using these drawing commands gives you an easy way to manually create interesting transfer functions. (Note however, that there is only one Y value for any X. You cannot draw a line that overlaps itself vertically.)</p>

<p>Below patch 1 are three small patches that utilize <o>uzi</o> objects to generate all of the points in the <o>itable</o>. If you click on the <b>A</b> <o>button</o>, a function is created where the input value is the same as the output value.  Clicking on <b>B</b> produces the opposite: input values ranging from <m>0-127</m> are converted into a <m>127-0</m> range.  The <b>C</b> <o>button</o> produces a <o>random</o> distribution similar to the starting contents of the <o>itable</o> object.</p>

<h2>Creating formulas with <o>uzi</o></h2>

<p>Let’s look more closely at these function-generating routines, and particularly at the <o>uzi</o> object. The <o>uzi</o> object, as its name implies, is made to rapidly fire off <m>bang</m> messages. A single <m>bang</m> to an <o>uzi</o> object’s input will generate as many <m>bang</m> messages as are specified in the object's argument; therefore, <o>uzi</o> <m>128</m> will send out <m>128</m> <m>bang</m> messages from the left outlet.  In order to keep track of the current <m>bang</m> number, the <i>rightmost</i> outlet produces a count (starting with <m>1</m>) – this output is what we are using for the index to the <o>itable</o> object.  In patch segment <b>A</b>, this count is reduced by <m>1</m> (so that it begins with <m>0</m>, the start of the <o>itable</o> range), and used for both the X and Y values, giving us the straight line output for a function.</p>

<p>The <b>B</b> segment is similar, except that the Y value uses the <o>!-</o> object to subtract the incoming value from <m>127</m>.  This results in a similar ramp, but heading in the opposite direction – the lowest X values have the highest Y values. The <b>C</b> segment uses a <o>random</o> object to create random values as part of the output list.  It has to use the <o>swap</o> object (which simply "swaps" the left and right values it receives) to reverse the order of the list contents so that the <o>random</o> object's output is used for the Y value instead of the X value.</p>

<p>Let’s alter the <b>B</b> segment a little to see how different functions could be created. If we change the <o>!-</o> object to be a division (<o>/</o>) object with an argument of <m>4</m>, we will reduce the range of the Y values to only 1/4th of the vertical range. Likewise, if we multiply (<o>*</o>) the value by <m>2</m>, we will hit the Y range maximum halfway through the X values.  Obviously, with a little manipulation, you can use <o>uzi</o> and a little math to create many complex functions.</p>

<h2>Histograms with <o>table</o></h2>

<p>The right-hand patch area (labeled <b>2</b>) provides an interesting use of the <o>itable</o> system, as well as several formula patches and a new object called <o>histo</o>.  The <o>histo</o> object performs a histogram function: it keeps a count of how many times a number has come into its input.  Whenever a value comes into the input, the object produces the number of times it has been received out its <i>right</i> outlet followed immediately by the value itself out its <i>left</i> outlet - it can then be fed directly into the <o>itable</o> object, where the left inlet becomes the X and the right inlet becomes the Y.  If you use the <o>slider</o> at the top of the patch to “scratch” in a lot of numbers, you will see the <o>itable</o> object show the relative distribution of the values you have sent.  If you want to clear the <o>itable</o> and the <o>histo</o> objects, you can hit the return key on your keyboard (ASCII <m>13</m>).</p>

<h2>Create probability distributions</h2>

<p>As with our first drawing patch, we have a number of smaller patch areas that can be used to generate functions into the <o>itable</o> object.  However, instead of drawing discrete functions, these generate fuzzy distributions by sending random and semi-random values into the <o>histo</o> object; this histogram is then fed to the <o>itable</o> to generate a probability map. Segments <b>D</b> and <b>E</b> use <o>random</o> and <o>drunk</o>, respectively, to generate the values.  Clicking on the attached <o>button</o> objects repeatedly will show how each affects the distribution curve – <o>random</o> hits all of the values fairly evenly, but <o>drunk</o> is much more lumpy in its distribution.  Again, we can clear the histogram at any time with the return key.</p>

<bullet>Open the <m>More_ops</m> subpatch</bullet>

<p>Patch segments <b>F</b> and <b>G</b> take a somewhat different approach.  Segment <b>F</b> generates two <b>random</b> numbers, then uses the <o>minimum</o> object to choose the <i>lower</i> of the two numbers.  This will tend to emphasize the lower numbers in the range; repeatedly clicking on the connected <o>button</o> will show the lower numbers being used more often than the higher values.  Segment <b>G</b> does the opposite: it uses the higher of two <o>random</o> numbers by putting them into the <o>maximum</o> object.  Thus, repeatedly clicking on the connected <o>button</o> will tend to generate more high numbers.</p>

<p>Finally, segment <b>H</b> creates an average of 3 <o>random</o> numbers by generating the numbers, adding them together, then dividing the sum by <m>3</m>.  If you clear the <o>itable</o> and repeatedly hit the <b>H</b> <o>button</o>, you will see a “bell-shaped” curve being generated, since the average of three random numbers will tend to be in the middle of the range.  This is a simple type of <i>Gaussian</i> (or normal) distribution, and is commonly found in statistical research when most subjects fall within the middle of a range of possible values.</p>

<h2>Using the probability distribution</h2>

<p>Turn on the <o>metro</o> object with the <o>toggle</o> at the top of patch 2; this will cause <m>bang</m> messages to be sent to the <o>itable</o> object.  This produces a random “quantile” output to be generated – a random but weighted output value from the <o>itable</o> object. The result is that the <o>lcd</o> area below the <o>itable</o> will begin drawing small circles where the density is roughly equivalent to the distribution curve shown in the <o>itable</o> routine.  If you want to clear the <o>lcd</o>, you can hit the space bar (ASCII <m>32</m>) to restart drawing. (The <m>Y</m> location of every dort is random, so the proability curve shows in the left to right density.)</p>

<p>Leaving the <o>metro</o> running while using the right side uzi function generators should cause the drawing to simulate the distribution curves in the <o>itable</o> object. To test more radical curves, you can draw sharp lines within the <o>itable</o> object to force certain output curves; you can also use the <o>slider</o> to “shade” in parts of the equation, causing the drawing output to shade in segments in response to the new distribution curve.</p>

<h2>Summary</h2>

<p>Many different processes depend on the ability to create functions that transform an input range into an output range.  In previous tutorials, we saw how the <o>scale</o> object could do this for us. However, if we want to manually change the distribution of values, or if we want to use a formula for data transformation, we need to use a more complex system.  As we’ve seen in this tutorial, the <o>table</o>/<o>itable</o> objects serve this purpose perfectly, giving us the ability to either manufacture or manually draw a transfer function.</p>

<p>Using the <o>histo</o> object, we also saw how we can generate the sort of distribution curves that serve as common random techniques in generative art and stochastic music.  Whether the fuzziness of true random generation, or the tendencies found with more discrete math, these curves can be used to both generate and guide results to a desired outcome.</p>

<seealsolist>
<seealso name="table">Store and graphically edit an array of numbers</seealso>
<seealso name="itable">A table in a patcher window</seealso>
<seealso name="uzi">Sends out n bangs</seealso>
<seealso name="swap">Swap left and right inlets to outlets</seealso>
<seealso name="histo">A simple histogram</seealso>
<seealso name="minimum">Determines the minimum of two numbers or a list of numbers</seealso>
<seealso name="maximum">determines the maximum of two numbers or a list of numbers</seealso>
</seealsolist>

</chapter>
