<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 46: Manipulating Matrix Data using JavaScript">
<setdocpatch name="46jJavaScriptOperators" patch="46jJavaScriptOperators.maxpat"/>

<previous name="jitterchapter45">Introduction to using Jitter within JavaScript</previous>
<next name="jitterchapter47">Using Jitter Object Callbacks in JavaScript</next>
<parent name="jitindex">Jitter Tutorials</parent>


<h1>Tutorial 46: Manipulating Matrix Data using JavaScript</h1>
<p>As we saw in the last tutorial, we can use the Max <o>js</o> object to design a pipeline of Jitter objects within procedural JavaScript code. The JitterObject and JitterMatrix objects within JavaScript allow us to create new Jitter objects and matrices and work with them more or less as we would within a Max patcher. In many situations, we need to manipulate data stored in a Jitter matrix in a manner that would be awkward or difficult to do using a Max patcher. This tutorial looks at a variety of solutions for how to manipulate matrix data within <o>js</o> using methods and properties of the JitterMatrix object, as well as using the <o>jit.expr</o> object within JavaScript.</p>


<p>This tutorial assumes you've read through the previous <i><link type="tutorial" module="jit" name="jitterchapter45">Tutorial 45:</link> Introduction to using Jitter within JavaScript</i>. In addition, this tutorial works with Jitter OpenGL objects as well as <o>jit.expr</o>, so you may want to review <i><link type="tutorial" module="jit" name="jitterchapter30">Tutorial 30:</link> Drawing 3D Text</i>, <i><link type="tutorial" module="jit" name="jitterchapter31">Tutorial 31:</link> Rendering Destinations</i>, and <i><link type="tutorial" module="jit" name="jitterchapter39">Tutorial 39:</link> Spatial Mapping</i> before we begin.</p>

<bullet>Open the tutorial patch.</bullet>
<p>This tutorial patch uses a <o>js</o> object loading a file called <i>46jParticles.js</i>. The JavaScript code generates a Jitter matrix in response to a <m>bang</m> that we then send to the <o>jit.gl.render</o> object in the patcher.  The file contains a number of functions to respond to various settings we can send as messages from our patch.</p>


<illustration><img src="images/jitterchapter46a.png"/><i>Our patcher containing the JavaScript file.</i></illustration>
<bullet>Click the <o>toggle</o> box above the <o>qmetro</o> object on the left hand side of the patch. Observe the results in the <o>jit.window</o> named <m>parts</m>.</bullet>
<p>Our JavaScript code generates a set of points that represent a simple <i>particle system</i>. A particle system is essentially an algorithm that operates on a (often very large) number of spatial points called <i>particles</i>. These particles have rules that determine how they move over time in relation to each other or in relation to other actors within the space. At their basic level, particles contain only their spatial coordinates. Particle systems may also contain other information and can be used to simulate a wide variety of natural processes such as running water or smoke.  Particle systems are widely used in computer simulations of our environment, and as such are a staple technology in many computer-generated imagery (CGI) applications.</p>


<illustration><img src="images/jitterchapter46b.png"/><i>A particle system generated as a Jitter matrix.</i></illustration>
<h2>The Wide World of Particles</h2>
<p>The particle system used in our JavaScript simulation works by generating two sets of random points representing the positions and velocities of the particles and a number of spatial positions in the 3D space that contain gravity. These gravity points, called <i>attractors</i>, act upon the particles in each frame to gradually pull particles towards them. As we can see in the <o>jit.window</o>, the points in our particle system gradually collapse towards one or more points of singularity. Alternately, the attractor points may be such that the particles oscillate between them, caught in a conflicting gravity field that gradually stabilizes.</p>


<bullet>With the patcher as the frontmost window, press the <i>spacebar</i> on your computer keyboard or click on the <o>message</o> box labeled <m>init</m> attached to the <o>js</o> object. Try this a few times to observe the different behaviors of our particle system.</bullet>
<p>The <m>init</m> message to our <o>js</o> object reboots the particle system. It randomly scatters the particles and generates new attractor points.</p>


<illustration><img src="images/jitterchapter46c.png"/><i>A variety of behaviors from a simple set of rules.</i></illustration>
<bullet>Click in the <o>jit.window</o>. A set of colored red, green, and blue axes will appear around the world. Rotate the world with your mouse. Try zooming out (by holding down the ALT/Option key and dragging in the window). Restart the particle system from different vantage points.</bullet>
<p>A <o>jit.gl.handle</o> object controls our <o>jit.gl.render</o> object, allowing us to see that our particle system inhabits a three-dimensional world. By rotating the space around to different perspectives we can see how the attractors pull in particles from all around them.</p>

<h2>Under the Hood</h2>
<p>Now that we've seen a good part of the functionality of the patch (we'll return to it later), let's look at the JavaScript code to see how the algorithm is constructed.</p>

<bullet>Double-click the <o>js</o> object in our Tutorial patch. A text editor will appear, containing the source code for the <o>js</o> object in the patch. The code is saved as a file called "46jParticles.js" in the same folder as the Tutorial patch.</bullet>
<p>Our JavaScript code works by manipulating our particles and attractors as Jitter matrices. The particle system is updated by a generation every time our <o>js</o> object receives a <m>bang</m>. This is accomplished by performing a series of operations on the data in the Jitter matrices. We can take advantage of the Jitter architecture to perform mathematical operations on <i>entire</i> matrices at once since we've encoded our system as matrices. This provides a significant advantage in speed, clarity, and efficiency over working with our data as individual values that must be adjusted one at a time, in many cases (as we would were we to encode our particles as an Array, for example).</p>

<div>
<techdetail>Our JavaScript code actually contains three ways of updating our particle system from generation to generation, each of which uses different techniques for manipulating the matrix data representing the particles. We can process the particle system as a single entity using a series of op() methods to the JitterMatrix object, by using a <o>jit.expr</o> object, or by iterating through the particle system point-by-point (or cell-by-cell). We'll look at each in turn once we investigate the code common to them all. </techdetail>
</div>
<bullet>Look at the global block of code that begins the JavaScript file. </bullet>
<p>After the initial comment block and inlet/outlet declarations, we can see a number of variables declared and initialized, including a few JitterObject and JitterMatrix objects. If we examine this code in detail, we can see the outline of how we'll perform our particle system.</p>


<p>
<pre><code language="javascript">var PARTICLE_COUNT = 1000; // initial number of particle vertices
var ATTRACTOR_COUNT = 3; // initial number of points of gravity</code></pre>
</p>

<p>These two global variables (<i>PARTICLE_COUNT</i> and <i>ATTRACTOR_COUNT</i>) are used to decide how many particles and how many points of attraction we'd like to work with in our simulation. These will determine the <m>dim</m> of the Jitter matrices containing the particle and attractor information.</p>

<p>
<pre><code language="javascript">// create a [jit.noise] object for particle and velocity generation
var noisegen = new JitterObject("jit.noise");
noisegen.dim = PARTICLE_COUNT;
noisegen.planecount = 3;
noisegen.type = "float32";

// create a [jit.noise] object for attractor generation
var attgen = new JitterObject("jit.noise");
attgen.dim = ATTRACTOR_COUNT;
attgen.planecount = 3;
attgen.type = "float32";</code></pre>
</p>


<p>Our particle systems are generated randomly by the init() function, which we will investigate presently. The <o>jit.noise</o> objects created here as JitterObject objects will perform that function for us, by generating one-dimensional matrices of <m>float32</m> values of a size (<m>dim</m>) corresponding to the number of particles and attractors specified for our system. The matrices generated by our <o>jit.noise</o> objects have a <m>planecount</m> of <m>3</m>, corresponding to x, y, and z spatial data.</p>

<p>
<pre><code language="javascript">// create two [jit.expr] objects for the bang_expr() function
// first expression: sum all the planes in the input matrix
var myexpr = new JitterObject("jit.expr");
myexpr.expr = "in[0].p[0]+in[0].p[1]+in[0].p[2]";
// second expression: evaluate a+((b-c)*d/e)
var myexpr2 = new JitterObject("jit.expr");
myexpr2.expr = "in[0]+((in[1]-in[2])*in[3]/in[4])";
</code></pre>
</p>

<p>One of the ways we will update our particle system is to use two <o>jit.expr</o> objects within our JavaScript code. This part of the code creates the JitterObject objects and defines the mathematical expression to be used by them (the <m>expr</m> attribute). We'll step through this when we investigate the code that uses them later on.</p>


<p>
<pre><code language="javascript">// create the Jitter matrices we need to store our data
// matrix of x,y,z particle vertices
var particlemat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// matrix of x,y,z particle velocities
var velomat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// matrix of x,y,z points of attraction (gravity centers)
var attmat = new JitterMatrix(3, "float32", ATTRACTOR_COUNT);
// matrix for aggregate distances
var distmat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// temporary matrix for the bang_op() function
var tempmat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// temporary summing matrix for the bang_op() function
var summat = new JitterMatrix(1, "float32", PARTICLE_COUNT);
// another temporary summing matrix for the bang_op() function
var summat2 = new JitterMatrix(1, "float32", PARTICLE_COUNT);
// a scalar matrix to store the current gravity point
var scalarmat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// a scalar matrix to store acceleration (expr_op() function only)
var amat = new JitterMatrix(1, "float32", PARTICLE_COUNT);
</code></pre>
</p>


<p>Our algorithm calls for a number of JitterMatrix objects to store information about our particle system and to be used as intermediate storage during the processing of each generation of the system. The first three matrices (bound to the variables <i>particlemat</i>, <i>velomat</i>, and <i>attmat</i>) store the x,y,z positions of our particles, the x,y,z velocities of our particles, and the x,y,z positions of our attractors, respectively. The other six matrices are used in computing each generation of the system.</p>


<p><pre><code language="javascript">var a = 0.001; // acceleration factor
var d = 0.01; // decay factor</code></pre></p>


<p>These two variables control two important aspects of the behavior of our particle system: the variable <i>a</i> controls how rapidly the particles accelerate toward an attractor, while the variable <i>d</i> controls how much the particles' current velocity decays with each generation. This second variable influences how easy it is for a particle to change direction and be drawn to other attractors.</p>


<p><pre><code language="javascript">var perform_mode="op"; // default perform function</code></pre></p>

<p>This variable defines which of the three techniques we'll use to process our particle system (the <i>perform_mode</i>).</p>

<h2>The Initialization Phase</h2>
<p>The first step in creating a particle system is to generate an <i>initial state</i> for the particles and any factors that will act upon them (in our case, the attractor points). For example, were we to attempt to simulate a waterfall, we would start all our particles at the top of the space, with a fixed gravity field at the bottom of the space acting upon the particles with each generation. Our system is slightly less ambitious in terms of real-world accuracy&#x2014;the particles and attractors will simply be in random positions throughout the 3D scene.</p>

<bullet>Look at the code for the loadbang() and init() functions in the JavaScript code.</bullet>

<p><pre><code language="javascript">function loadbang() // execute this code when our Max patch opens
{
  init(); // initialize our matrices
  post("particles initialized.\n");
}

function init()
// initialization routine... call at load, as well as
// when we change the number of particles or attractors
{
  // generate a matrix of random particles spread between -1 and 1
  noisegen.matrixcalc(particlemat, particlemat);
  particlemat.op("*", 2.0);
  particlemat.op("-", 1.0);
  // generate a matrix of random velocities spread between -1 and 1
  noisegen.matrixcalc(velomat, velomat);
  velomat.op("*", 2.0);
  velomat.op("-", 1.0);
  // generate a matrix of random attractors spread between -1 and 1
  attgen.matrixcalc(attmat, attmat);
  attmat.op("*", 2.0);
  attmat.op("-", 1.0);
}</code></pre></p>

<p>The loadbang() function in a <o>js</o> object runs whenever the Max patcher containing the <o>js</o> file is loaded. This happens <i>after</i> the object is instantiated with the rest of the patch, and is triggered at the same time as messages triggered by <o>loadbang</o> and <o>loadmess</o> objects in a Max patch would be. Our loadbang() function simply calls the init() function and then prints a friendly message to the Max Console telling us that all is well.</p>

<div>
<techdetail>The loadbang() function of JavaScript code only executes when the patcher containing the <o>js</o> object is opened.  This function does <i>not</i> execute when you change and recompile the JavaScript code. </techdetail>
</div>
<p>Our init() function runs when we open our patch as well as whenever we call it from our Max patcher (through the <o>message</o> box triggered by the <i>spacebar</i>). The init() function also gets called whenever we change the number of attractors and particles in our simulation. The matrixcalc() method of <o>jit.noise</o> fills the output matrix (the second argument to the method) with random values between <m>0</m> and <m>1</m>. This is the same as sending a <m>bang</m> to a <o>jit.noise</o> object in a patcher. In our init() function we fill three
matrices with random values in 3 planes. These matrices represent the initial position of
our particles (<i>particlemat,</i>) the initial velocity of our particles (<i>velomat,</i>) and the position of our attractors (<i>attmat</i>). Using the op() method to our JitterMatrix objects, we then scale these random values to be between <m>&#x2013;1</m> and <m>1</m>. We do this by multiplying the matrix values by <m>2</m> and then subtracting <m>1</m>.</p>

<p>Now that we have our initial state set up for our particle system, we need to look at how we process the particles with each generation. This is accomplished through one of three different methods in our JavaScript code determined by the <i>perform_mode</i> variable.</p>

<bullet>In the Tutorial patcher, restart the particle system and switch the <o>umenu</o> object labeled <i>Perform routine</i> from &#x201C;op&#x201D; to &#x201C;expr&#x201D;. The particle system should continue to behave exactly as before. Switch the <o>umenu</o> again to &#x201C;iter&#x201D;. The particle system will still run, but very slowly (note the frame rate in the <o>jit.fpsgui</o> object attached to the <o>jit.gl.render</o> object in the lower left part of the patch). Switch the <o>umenu</o> back to &#x201C;op&#x201D;.</bullet>
<p>The <o>umenu</o> changes the value of the <i>perform_mode</i> variable via the mode() function in our JavaScript code. We will look at later in this Tutorial, but it's important to note that one of the methods used (&#x201C;iter&#x201D;) runs much more slowly than the other two. This is largely due to the technique used to update the particle system. We"ll look at why this is when we investigate the function that performs that task.</p>

<bullet>Look at the bang() function in the JavaScript code.</bullet>

<p><pre><code language="javascript">function bang() // perform one iteration of our particle system
{
  switch(perform_mode) { // choose from the following...
   case "op": // use Jitter matrix operators
      bang_op();
      break;
   case "expr": // use [jit.expr] for the bulk of the algorithm
      bang_expr();
      break;
   case "iter": // iterate cell-by-cell through the matrices
      bang_iter();
      break;
   default: // use bang_op() as our default
      bang_op();
      break;
  }
  // output our new matrix of particle vertices
  outlet(0, "jit_matrix", particlemat.name);
}
</code></pre></p>

<p>Our bang() function uses a JavaScript switch() statement to decide what function to call from within it to do the actual processing of our particle system. Depending on the <i>perform_mode</i> we choose in the Max patcher, we select from one of three different functions (bang_op(), bang_expr(), or bang_iter()). Assuming all goes well, we then output the message <m>jit_matrix</m>, followed by the <m>name</m> of our <i>particlemat</i> matrix (which contains the current coordinates of the simulation"s particles) back into Max.</p>

<p>In the grand tradition of <i>Choose Your Own Adventure</i> and <i>Let's Make a Deal</i>, we'll now investigate the three different perform routines represented by the different functions mentioned above.</p>

<h2>Door #1: The op() route</h2>
<bullet>Look at the JavaScript source for the bang_op() function.</bullet>
<p>Our bang_op() function updates our particle system by using, whenever possible, the op() method to the JitterMatrix object to mathematically alter the contents of matrices <i>all at once</i>. Whenever possible, we do this processing <i>in place</i> to limit the number of separate Jitter matrices we need to get through the algorithm. We perform the bulk of the processing multiple times within a for() loop, once for each attractor in our particle system. Once this loop completes, we get an updated version of the velocity matrix (<i>velomat</i>), which we then add to the particle matrix (<i>particlemat</i>) to define the new positions of the particles.</p>

<p>In a nutshell, we do the following:</p>


<p><pre><code language="javascript">function bang_op() // create our particle matrix using Matrix operators
{
  for(var i = 0; i &lt; ATTRACTOR_COUNT; i++)
  // do one iteration per gravity point
  {</code></pre></p>


<p>We perform the code until the closing brace (}) once for every attractor, setting the attractor we're currently working on to the variable <i>i</i>.</p>


        <p><pre><code language="javascript">// create a scalar matrix out of the current attractor:
scalarmat.setall(attmat.getcell(i));</code></pre></p>

<p>The getcell() method of a JitterMatrix object returns the values of the numbers in the cell specified as its argument. The setall() method sets <i>all</i> the cells of a matrix to a value (or array of values). These methods work the same as the corresponding messages to the <o>jit.matrix</o> object in a Max patcher. This line tells our <o>js</o> object to copy the current attractors coordinates out of the attractor matrix (<i>attmat</i>) and set every single cell in the JitterMatrix <i>scalarmat</i> to those values. The <i>scalarmat</i> matrix has the same <m>dim</m> as the <i>particlemat</i> matrix (equal to the number of particles in our system). This allows us to use it as a scalar operand in our op() methods.</p>


<p><pre><code language="javascript">// subtract our particle positions from the current attractor
// and store in a temporary matrix (x,y,z):
tempmat.op("-", scalarmat, particlemat);</code></pre></p>

<p>This code subtracts our particle positions (<i>particlemat</i>) from the position of the attractor we"re currently working with (<i>scalarmat</i>). The result is then stored in a temporary matrix with the same <m>dim</m> as two used in the op() function. This matrix represents the distances from each particle to the current attractor.</p>


<p><pre><code language="javascript">// square to create a cartesian distance matrix (x*x, y*y, z*z):
distmat.op("*", tempmat, tempmat);</code></pre></p>

<p>This code multiplies the <i>tempmat</i> matrix <i>by itself</i>, as a simple way of squaring it. The result is then stored in the <i>distmat</i> matrix.</p>


<p><pre><code language="javascript">// sum the planes of the distance matrix (x*x+y*y+z*z)
summat.planemap = 0;
summat.frommatrix(distmat);
summat2.planemap = 1;
summat2.frommatrix(distmat);
summat.op("+", summat, summat2);
summat2.planemap = 2;
summat2.frommatrix(distmat);
summat.op("+", summat, summat2);</code></pre></p>


<p>In this block of code, we take the separate <i>planes</i> of the <i>distmat</i> matrix and add them together into a single-plane matrix called <i>summat</i>. In order to do this, we use the <m>planemap</m> property of JitterMatrix to specify which plane of the source matrix to use when copying from using the frommatrix() method. To sum everything we need a second temporary matrix (<i>summat2</i>) to help with the operation. First we copy plane <m>0</m> of <i>distmat</i> (the squared <i>x</i> distances) into <i>z</i> distances) into <i>summat2</i>. We then add <i>summat</i> and <i>summat2</i> again, keeping in mind that <i>summat</i> at this point <i>already</i> contains the sum of the first two planes of <i>distmat</i>. The result of this second sum is stored back into <i>summat</i>, which now contains the sum of all three planes of <i>distmat</i>.</p>


<p><pre><code language="javascript">// scale our distances by the acceleration value:
tempmat.op("*", a);
// divide our distances by the sum of the distances
// to derive gravity for this frame:
tempmat.op("/", summat);
// add to the current velocity bearings to get the
// amount of motion for this frame:
velomat.op("+", tempmat);
}</code></pre></p>

<p>This is the last block of code in our per-attractor loop. We multiply the <i>tempmat</i> matrix (which contains the distances of our particles from the current attractor) by the value stored in the variable a, representing acceleration. We then divide that result by the <i>summat</i> matrix (the sum of the squared distances), and add those results to the current velocities of each particle as stored in the <i>velomat</i> matrix. The result of the addition is stored in <i>velomat</i>.</p>

<p>This <i>entire</i> process is repeated again for each attractor. As a result, the <i>velomat</i> matrix is added to each time based on how far our particles are from each attractor. By the time the loop finishes (when <i>i</i> reaches the last attractor index), <i>velomat</i> contains a matrix of velocities corresponding to the aggregate pull of all our attractors on all our particles.</p>


<p><pre><code language="javascript">   // offset our current positions by the amount of motion:
   particlemat.op("+", velomat);
   // reduce our velocities by the decay factor for the next frame:
   velomat.op("*", d);
}</code></pre></p>


<p>Finally, we add these velocities to our matrix of particles (<i>particlemat</i> + <i>velomat</i>). Our particle matrix is now updated to a new set of particle positions. We then decay the velocity matrix by the amount stored in the variable <i>d</i>, so that the simulation retains a remnant of this generation"s velocity for the next generation of the particle system.</p>

<p>The use of a cascading series of op() methods to perform our algorithm on entire matrices gives us a big advantage in terms for speed, as Jitter can perform a simple mathematical operation on a large set of data very quickly. However, there are a few points (particularly in the generation of the summing matrix <i>summat</i>) where the code may have seemed more awkward than necessary. We can use <o>jit.expr</o> to define a more complex mathematical expression to perform much of this work in a single operation.</p>

<h2>Door #2: The expr() route</h2>
<bullet>Back in the global block of our JavaScript file, revisit the code that instantiates the <o>jit.expr</o> objects.</bullet>
<p><pre><code language="javascript">// create two [jit.expr] objects for the bang_expr() function

// first expression: sum all the planes in the input matrix
var myexpr = new JitterObject("jit.expr");
myexpr.expr = "in[0].p[0]+in[0].p[1]+in[0].p[2]";
// second expression: evaluate a+((b-c)*d/e)
var myexpr2 = new JitterObject("jit.expr");
myexpr2.expr = "in[0]+((in[1]-in[2])*in[3]/in[4])";</code></pre></p>

<p>At the beginning of our JavaScript code we created two JitterObject objects (<i>myexpr</i> and <i>myexpr2</i>) that instantiated <o>jit.expr</o> objects. The expression for the first object takes a single matrix (<i>in[0]</i>) and sums its planes (the <i>.p[n]</i> notation refers to the data stored in plane <i>n</i> of that matrix). The second expression takes five matrices (<i>in[0]</i> &#x2013; <i>in[4]</i>) and adds the first matrix (<i>A</i>) to the result of the second subtracted from the third (<i>B-C</i>) multiplied by a fourth (<i>D</i>) and divided by a fifth (<i>E</i>). Our <i>myexpr2</i> JitterObject therefore evaluates the expression:</p>

<p>            A+((B-C)*D/E) </p>

<bullet>Look at the code for the bang_expr() function. Compare it to what we"ve used in the bang_op() function.</bullet>
<p>The basic outline of our bang_expr() function is equivalent to the bang_op() function, i.e. we iterate through a loop based on the number of attractors in our simulation, eventually ending up with an aggregate velocity matrix (<i>velomat</i>) that we than use to offset our particle matrix (<i>particlemat</i>). The key difference lies in where we insert the calls to <o>jit.expr</o>:</p>


<p>
<pre><code language="javascript">function bang_expr() // create our particle matrix using [jit.expr]
{
  // create a scalar matrix out of our acceleration value:
  amat.setall(a);</code></pre></p>


<p>The above line fills every cell in the <i>amat</i> matrix with the value of the variable <i>a</i> (the acceleration factor). This allows us to use it as an operand in one of the <o>jit.expr</o> expressions later on.</p>

<p>
<pre><code language="javascript">for(var i = 0; i &lt; ATTRACTOR_COUNT; i++)
// do one iteration per gravity point
{
   // create a scalar matrix out of the current attractor:
   scalarmat.setall(attmat.getcell(i));
   // subtract our particle positions from the current attractor
   // and store in a temporary matrix (x,y,z):
   tempmat.op("-", scalarmat, particlemat);
   // square to create a cartesian distance matrix (x*x, y*y, z*z):
   distmat.op("*", tempmat, tempmat);</code></pre></p>

<p>This is all the same as in bang_op(). We derive a squared distance matrix based on the difference between the current attractor and our particle positions.</p>

<p><pre><code language="javascript">// sum the planes of the distance matrix (x*x+y*y+z*z) :
// "in[0].p[0]+in[0].p[1]+in[0].p[2]" :
myexpr.matrixcalc(distmat, summat);</code></pre></p>

<p>Instead of summing the <i>distmat</i> matrix plane-by-plane using op() and frommatrix() methods, we simply evaluate our first mathematical expression using <i>distmat</i> as a 3-plane input matrix and <i>summat</i> as a 1-plane output matrix.</p>

<p><pre><code language="javascript">// derive amount of motion for this frame :
// "in[0]+((in[1]-in[2])*in[3]/in[4])" :
myexpr2.matrixcalc([velomat,scalarmat,particlemat,amat,summat],velomat);</code></pre></p>

<p>Similarly, at the end of our attractor loop we can derive the velocity matrix <i>velomat</i> in one compound expression based on the previous velocity matrix (<i>velomat</i>), the scalar matrix containing the current attractor point (<i>scalarmat</i>), the current particle positions (<i>particlemat</i>), the scalar matrix containing the acceleration (<i>amat</i>), and the matrix containing the distance sums (<i>summat</i>). This is much simpler (and a cleaner read) than using a whole sequence of op() functions working with intermediary matrices. Note that we use brackets ([ and ]) to establish an array of input matrices in the matrixcalc() method to the <i>myexpr2</i> object.</p>

<p><pre><code language="javascript">   // offset our current positions by the amount of motion:
   particlemat.op("+", velomat);
   // reduce our velocities by the decay factor for the next frame:
   velomat.op("*", d);
}</code></pre></p>

<p>This is the same as in bang_op(). We generate the new particle positions and decay the new velocities for use as initial velocities in the next generation of the system. </p>

<h2>Door #3: Cell-by-cell </h2>
<bullet>Look at the code for the bang_iter() function.</bullet>
<p>The bang_iter() function works in a different way from the other two perform routines we're using in our JavaScript code. Rather than working on the matrices as single entities, we work on everything on a cell-by-cell basis, iterating through not only the matrix of attractor positions (<i>attmat</i>), but also through the matrices of particles and velocities. We do this through a pair of nested for() loops, temporarily storing each cell value in different Array objects. We use the getcell() and setcell1d() methods to the JitterMatrix object to retrieve and store values from these Arrays.</p>

<p><pre><code language="javascript">function bang_iter() // create our particle matrix cell-by-cell
{
  var p_array = new Array(3); // array for a single particle
  var v_array = new Array(3); // array for a single velocity
  var a_array = new Array(3); // array for a single attractor

  for(var j = 0; j &lt; PARTICLE_COUNT; j++)
  // do one iteration per particle
  {
   // fill an array with the current particle:
   p_array = particlemat.getcell(j);
   // fill an array with the current particle's velocity:
   v_array = velomat.getcell(j);

   for(var i = 0; i &lt; ATTRACTOR_COUNT; i++)
   // do one iteration per gravity point
   {
      // fill an array with the current attractor:
      a_array = attmat.getcell(i);

      // find the distance from this particle to the
      // current attractor:
      var distsum = (a_array[0]-p_array[0])*(a_array[0]-p_array[0]);
      distsum+= (a_array[1]-p_array[1])*(a_array[1]-p_array[1]);
      distsum+= (a_array[2]-p_array[2])*(a_array[2]-p_array[2]);

      // derive the amount of motion for this frame:
      v_array[0]+= (a_array[0]-p_array[0])*a/distsum; // x
      v_array[1]+= (a_array[1]-p_array[1])*a/distsum; // y
      v_array[2]+= (a_array[2]-p_array[2])*a/distsum; // z
   }

   // offset our current positions by the amount of motion
   p_array[0]+=v_array[0]; // x
   p_array[1]+=v_array[1]; // y
   p_array[2]+=v_array[2]; // z

   // reduce our velocities by the decay factor for the next frame:
   v_array[0]*=d; // x
   v_array[1]*=d; // y
   v_array[2]*=d; // z

   // set the position for this particle in the Jitter matrix:
   particlemat.setcell1d(j, p_array[0],p_array[1],p_array[2]);
   // set the velocity for this particle in the Jitter matrix:
   velomat.setcell1d(j, v_array[0],v_array[1],v_array[2]);
  }
} </code></pre></p>

<p>Note that by updating our particle system bit-by-bit (and using intermediary Array objects to store data for each cell) we're essentially replicating the same operation, as many times as there are particles in our system! While this may not be noticeably inefficient with a small number of particles, once you begin to work with thousands of points it will become noticeably slower.</p>

<h2>Other functions </h2>
<bullet>Back in the Max patcher, change the <o>number</o> box objects attached to the <o>message</o> boxes labeled <m>particles $1</m>, <m>attractors $1</m>, <m>accel $1</m>, and <m>decay $1</m>. Try setting the number of particles to very large and very small numbers. Try to work out how the <m>accel</m> and <m>decay</m> attributes alter the responsiveness of the system. Look at the code for these functions in the JavaScript file.</bullet>
<p>The bulk of these functions simply change variables, sometimes scaling them first (e.g. accel() and decay() simply change the values of <i>a</i> and <i>d</i>, respectively). Similarly, the mode() function changes the value of the <i>perform_mode</i> variable to a string that we use to decide the perform routine:</p>

<p><pre><code language="javascript">function mode(v) // change perform mode
{
  perform_mode = v;
}</code></pre></p>

<p>The particles() and attractors() functions, however, need to not only change the value of a variable (<i>PARTICLE_COUNT</i> and <i>ATTRACTOR_COUNT</i>, respectively), but they need to change the <m>dim</m> of the matrices that depend on those values as well as reboot the particle simulation (by calling the init() function).</p>

<p><pre><code language="javascript">function particles(v) // change the number of particles we're working with
{
  PARTICLE_COUNT = v;

  // resize matrices
  noisegen.dim = PARTICLE_COUNT;
  particlemat.dim = PARTICLE_COUNT;
  velomat.dim = PARTICLE_COUNT;
  distmat.dim = PARTICLE_COUNT;
  attmat.dim = PARTICLE_COUNT;
  tempmat.dim = PARTICLE_COUNT;
  summat.dim = PARTICLE_COUNT;
  summat2.dim = PARTICLE_COUNT;
  scalarmat.dim = PARTICLE_COUNT;
  amat.dim = PARTICLE_COUNT;

  init(); // re-initialize particle system
}

function attractors(v)
// change the number of gravity points we're working with
{
  ATTRACTOR_COUNT = v;

  // resize attractor matrix
  attgen.dim = ATTRACTOR_COUNT;

  init(); // re-initialize particle system
}</code></pre></p>

<bullet>In the Max patcher, change the <o>umenu</o> object labeled <i>Drawing primitive</i>. Try different settings and notice how it changes the way the particle system is drawn. The primitive() function in our JavaScript code changes the value of the variable <i>draw_primitive</i>.</bullet>
<p>Our particle system is visualized by sending the matrix of particle positions (referred to as <i>particlemat</i> in our JavaScript code) to the <o>jit.gl.render</o> object. The matrix contains <i>3</i> planes of <i>float32</i> data, which <o>jit.gl.render</o> interprets as x,y,z vertices in a geometry. The <i>drawing primitive</i>, which is set using the <m>@draw_mode</m> attribute of the <o>jit.gl.mesh</o> object, defines the way in which our OpenGL drawing context visualizes the data. </p>

<p>For more information on the specifics of these drawing primitives and the OpenGL matrix format, consult <i><link type="tutorial" module="jit" name="jitterchapter99_appendixb">Appendix B:</link> The OpenGL Matrix Format</i> or the OpenGL "Redbook". </p>

<illustration><img src="images/jitterchapter46d.png"/>Different ways of visualizing our particles using different drawing primitives. </illustration>

<bullet>Play around with the patch some more, looking at the different ways we can generate and visualize our particles. A wide variety of interesting systems can be created simply by passing unadorned x,y,z values to the <o>jit.gl.render</o> object as 3-plane matrices.</bullet>

<h2>Conclusion </h2>
<p>JavaScript can be a powerful language to use when designing algorithms that manipulate matrix data in Jitter. The ability to perform mathematical operations directly on matrices using a variety of techniques (op() methods, <o>jit.expr</o> objects, and cell-by-cell iteration) within procedural code lets you take advantage of Jitter as a tool to process large sets of data at once.</p>

<p>In the next tutorial, we"ll look at ways to trigger callback functions in JavaScript based on the actions of JitterObject objects themselves. </p>

<h2>Code Listing</h2>
<p><pre><code language="javascript">// 46jParticles.js
//
// a 3-D particle generator with simple gravity simulation
// demonstrating different techniques for mathematical
// matrix manipulation using Jitter objects in [js].
//
// rld, 7.05
//

inlets = 1;
outlets = 1;

var PARTICLE_COUNT = 1000; // initial number of particle vertices
var ATTRACTOR_COUNT = 3; // initial number of points of gravity

// create a [jit.noise] object for particle and velocity generation
var noisegen = new JitterObject("jit.noise");
noisegen.dim = PARTICLE_COUNT;
noisegen.planecount = 3;
noisegen.type = "float32";

// create a [jit.noise] object for attractor generation
var attgen = new JitterObject("jit.noise");
attgen.dim = ATTRACTOR_COUNT;
attgen.planecount = 3;
attgen.type = "float32";

// create two [jit.expr] objects for the bang_expr() function

// first expression: sum all the planes in the input matrix
var myexpr = new JitterObject("jit.expr");
myexpr.expr = "in[0].p[0]+in[0].p[1]+in[0].p[2]";
// second expression: evaluate a+((b-c)*d/e)
var myexpr2 = new JitterObject("jit.expr");
myexpr2.expr = "in[0]+((in[1]-in[2])*in[3]/in[4])";

// create the Jitter matrices we need to store our data

// matrix of x,y,z particle vertices
var particlemat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// matrix of x,y,z particle velocities
var velomat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// matrix of x,y,z points of attraction (gravity centers)
var attmat = new JitterMatrix(3, "float32", ATTRACTOR_COUNT);
// matrix for aggregate distances
var distmat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// temporary matrix for the bang_op() function
var tempmat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// temporary summing matrix for the bang_op() function
var summat = new JitterMatrix(1, "float32", PARTICLE_COUNT);
// another temporary summing matrix for the bang_op() function
var summat2 = new JitterMatrix(1, "float32", PARTICLE_COUNT);
// a scalar matrix to store the current gravity point
var scalarmat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// a scalar matrix to store acceleration (expr_op() function only)
var amat = new JitterMatrix(1, "float32", PARTICLE_COUNT);

var a = 0.001; // acceleration factor
var d = 0.01; // decay factor

var perform_mode=""op";" // default perform function

function loadbang() // execute this code when our Max patch opens
{
  init(); // initialize our matrices
  post("particles initialized.\n");
}

function init()
// initialization routine... call at load, as well as
// when we change the number of particles or attractors
{
  // generate a matrix of random particles spread between -1 and 1
  noisegen.matrixcalc(particlemat, particlemat);
  particlemat.op("*", 2.0);
  particlemat.op("-", 1.0);
  // generate a matrix of random velocities spread between -1 and 1
  noisegen.matrixcalc(velomat, velomat);
  velomat.op("*", 2.0);
  velomat.op("-", 1.0);
  // generate a matrix of random attractors spread between -1 and 1
  attgen.matrixcalc(attmat, attmat);
  attmat.op("*", 2.0);
  attmat.op("-", 1.0);
}

function bang() // perform one iteration of our particle system
{
  switch(perform_mode) { // choose from the following...
   case "op": // use Jitter matrix operators
      bang_op();
      break;
   case "expr": // use [jit.expr] for the bulk of the algorithm
      bang_expr();
      break;
   case "iter": // iterate cell-by-cell through the matrices
      bang_iter();
      break;
   default: // use bang_op() as our default
      bang_op();
      break;
  }

  // output our new matrix of particle vertices
  outlet(0, "jit_matrix", particlemat.name);

}

function bang_op() // create our particle matrix using Matrix operators
{
  for(var i = 0; i &lt; ATTRACTOR_COUNT; i++)
  // do one iteration per gravity point
  {
   // create a scalar matrix out of the current attractor:
   scalarmat.setall(attmat.getcell(i));

   // subtract our particle positions from the current attractor
   // and store in a temporary matrix (x,y,z):
   tempmat.op("-", scalarmat, particlemat);

   // square to create a cartesian distance matrix (x*x, y*y, z*z):
   distmat.op("*", tempmat, tempmat);

   // sum the planes of the distance matrix (x*x+y*y+z*z)
   summat.planemap = 0;
   summat.frommatrix(distmat);
   summat2.planemap = 1;
   summat.frommatrix(distmat);
   summat.op("+", summat, summat2);
   summat2.planemap = 2;
   summat2.frommatrix(distmat);
   summat.op("+", summat, summat2);

   // scale our distances by the acceleration value:
   tempmat.op("*", a);
   // divide our distances by the sum of the distances
   // to derive gravity for this frame:
   tempmat.op("/", summat);
   // add to the current velocity bearings to get the
   // amount of motion for this frame:
   velomat.op("+", tempmat);
  }

   // offset our current positions by the amount of motion:
   particlemat.op("+", velomat);
   // reduce our velocities by the decay factor for the next frame:
   velomat.op("*", d);
}

function bang_expr() // create our particle matrix using [jit.expr]
{
  // create a scalar matrix out of our acceleration value:
  amat.setall(a);

  for(var i = 0; i &lt; ATTRACTOR_COUNT; i++)
  // do one iteration per gravity point
  {
   // create a scalar matrix out of the current attractor:
   scalarmat.setall(attmat.getcell(i));
   // subtract our particle positions from the current attractor
   // and store in a temporary matrix (x,y,z):
   tempmat.op("-", scalarmat, particlemat);
   // square to create a cartesian distance matrix (x*x, y*y, z*z):
   distmat.op("*", tempmat, tempmat);

// sum the planes of the distance matrix (x*x+y*y+z*z) :
// "in[0].p[0]+in[0].p[1]+in[0].p[2]" :
   myexpr.matrixcalc(distmat, summat);

   // derive amount of motion for this frame :
   // "in[0]+((in[1]-in[2])*in[3]/in[4])" :
   myexpr2.matrixcalc([velomat,scalarmat,particlemat,amat,summat],
   velomat);

   // offset our current positions by the amount of motion:
   particlemat.op("+", velomat);
   // reduce our velocities by the decay factor for the next frame:
   velomat.op("*", d);
}

function bang_iter() // create our particle matrix cell-by-cell
{
  var p_array = new Array(3); // array for a single particle
  var v_array = new Array(3); // array for a single velocity
  var a_array = new Array(3); // array for a single attractor

  for(var j = 0; j &lt; PARTICLE_COUNT; j++)
  // do one iteration per particle
  {
   // fill an array with the current particle:
   p_array = particlemat.getcell(j);
   // fill an array with the current particle's velocity:
   v_array = velomat.getcell(j);

   for(var i = 0; i &lt; ATTRACTOR_COUNT; i++)
   // do one iteration per gravity point
   {
      // fill an array with the current attractor:
      a_array = attmat.getcell(i);

      // find the distance from this particle to the
      // current attractor:
      var distsum = (a_array[0]-p_array[0])*(a_array[0]-p_array[0]);
      distsum+= (a_array[1]-p_array[1])*(a_array[1]-p_array[1]);
      distsum+= (a_array[2]-p_array[2])*(a_array[2]-p_array[2]);

      // derive the amount of motion for this frame:
      v_array[0]+= (a_array[0]-p_array[0])*a/distsum; // x
      v_array[1]+= (a_array[1]-p_array[1])*a/distsum; // y
      v_array[2]+= (a_array[2]-p_array[2])*a/distsum; // z
   }

   // offset our current positions by the amount of motion
   p_array[0]+=v_array[0]; // x
   p_array[1]+=v_array[1]; // y
   p_array[2]+=v_array[2]; // z

   // reduce our velocities by the decay factor for the next frame:
   v_array[0]*=d; // x
   v_array[1]*=d; // y
   v_array[2]*=d; // z

   // set the position for this particle in the Jitter matrix:
   particlemat.setcell1d(j, p_array[0],p_array[1],p_array[2]);
   // set the velocity for this particle in the Jitter matrix:
   velomat.setcell1d(j, v_array[0],v_array[1],v_array[2]);
  }
}


function particles(v) // change the number of particles we're working with
{
  PARTICLE_COUNT = v;

  // resize matrices
  noisegen.dim = PARTICLE_COUNT;
  particlemat.dim = PARTICLE_COUNT;
  velomat.dim = PARTICLE_COUNT;
  distmat.dim = PARTICLE_COUNT;
  attmat.dim = PARTICLE_COUNT;
  tempmat.dim = PARTICLE_COUNT;
  summat.dim = PARTICLE_COUNT;
  summat2.dim = PARTICLE_COUNT;
  scalarmat.dim = PARTICLE_COUNT;
  amat.dim = PARTICLE_COUNT;

  init(); // re-initialize particle system
}

function attractors(v)
// change the number of gravity points we're working with
{
  ATTRACTOR_COUNT = v;

  // resize attractor matrix
  attgen.dim = ATTRACTOR_COUNT;

  init(); // re-initialize particle system
}

function accel(v) // set acceleration
{
  a = v*0.001;
}

function decay(v) // set decay
{
  d = v*0.001;
}

function mode(v) // change perform mode
{
  perform_mode = v;
}

function bang() // perform one iteration of our particle system
{
  switch(perform_mode) { // choose from the following...
   case "op": // use Jitter matrix operators
      bang_op();
      break;
   case "expr": // use [jit.expr] for the bulk of the algorithm
      bang_expr();
      break;
   case "iter": // iterate cell-by-cell through the matrices
      bang_iter();
      break;
   default: // use bang_op() as our default
      bang_op();
      break;
  }

  // output our new matrix of particle vertices
  outlet(0, "jit_matrix", particlemat.name);

}

function bang_op() // create our particle matrix using Matrix operators
{
  for(var i = 0; i &lt; ATTRACTOR_COUNT; i++) // do one iteration per gravity point
  {
   scalarmat.setall(attmat.getcell(i)); // create a scalar matrix out of the current attractor

   tempmat.op("-", scalarmat, particlemat); // subtract our particle positions from the current attractor and store in a temporary matrix (x,y,z)

   distmat.op("*", tempmat, tempmat); // square to create our cartesian distance matrix (x*x, y*y, z*z)

   // sum the planes of the distance matrix (x*x+y*y+z*z)
   summat.planemap = 0;
   summat.frommatrix(distmat);
   summat2.planemap = 1;
   summat.frommatrix(distmat);
   summat.op("+", summat, summat2);
   summat2.planemap = 2;
   summat2.frommatrix(distmat);
   summat.op("+", summat, summat2);

   tempmat.op("*", a); // scale our distances by the acceleration value
   tempmat.op("/", summat); // divide our distances by the sum of the distances to derive gravity for this frame
   velomat.op("+", tempmat); // add to the current velocity bearings to get the amount of motion for this frame
  }

   particlemat.op("+", velomat); // offset our current positions by the amount of motion
   velomat.op("*", d); // reduce our velocities by the decay factor for the next frame
}

function bang_expr() // create our particle matrix using [jit.expr]
{
  amat.setall(a); // create a scalar matrix out of our acceleration value

  for(var i = 0; i &lt; ATTRACTOR_COUNT; i++) // do one iteration per gravity point
  {
   scalarmat.setall(attmat.getcell(i)); // create a scalar matrix out of the current attractor

   tempmat.op("-", scalarmat, particlemat); // subtract our particle positions from the current attractor and store in a temporary matrix (x,y,z)

   distmat.op("*", tempmat, tempmat); // square to create our cartesian distance matrix (x*x, y*y, z*z)

   myexpr.matrixcalc(distmat, summat); // sum the planes of the distance matrix (x*x+y*y+z*z) : "in[0].p[0]+in[0].p[1]+in[0].p[2]"

   myexpr2.matrixcalc([velomat,scalarmat,particlemat,amat,summat], velomat); // derive amount of motion for this frame : "in[0]+((in[1]-in[2])*in[3]/in[4])"
  }

   particlemat.op("+", velomat); // offset our current positions by the amount of motion
   velomat.op("*", d); // reduce our velocities by the decay factor for the next frame
}

function bang_iter() // create our particle matrix cell-by-cell
{
  var p_array = new Array(3); // create an array for a single particle (x,y,z)
  var v_array = new Array(3); // create an array for a single velocity (x,y,z)
  var a_array = new Array(3); // create an array for a single attractor (x,y,z)


  for(var j = 0; j &lt; PARTICLE_COUNT; j++) // do one iteration per particle
  {
   p_array = particlemat.getcell(j); // fill an array with the current particle
   v_array = velomat.getcell(j); // fill an array with the current particle's velocity

   for(var i = 0; i &lt; ATTRACTOR_COUNT; i++) // do one iteration per gravity point
   {
      a_array = attmat.getcell(i); // fill an array with the current attractor

      // find the distance from this particle to the current attractor
      var distsum = (a_array[0]-p_array[0])*(a_array[0]-p_array[0]);
      distsum+= (a_array[1]-p_array[1])*(a_array[1]-p_array[1]);
      distsum+= (a_array[2]-p_array[2])*(a_array[2]-p_array[2]);

      v_array[0]+= (a_array[0]-p_array[0])*a/distsum; // derive the amount of motion for this frame (x)
      v_array[1]+= (a_array[1]-p_array[1])*a/distsum; // derive the amount of motion for this frame (y)
      v_array[2]+= (a_array[2]-p_array[2])*a/distsum; // derive the amount of motion for this frame (z)
   }

   p_array[0]+=v_array[0]; // offset our current positions by the amount of motion (x)
   p_array[1]+=v_array[1]; // offset our current positions by the amount of motion (y)
   p_array[2]+=v_array[2]; // offset our current positions by the amount of motion (z)

   v_array[0]*=d; // reduce our velocities by the decay factor for the next frame (x)
   v_array[1]*=d; // reduce our velocities by the decay factor for the next frame (y)
   v_array[2]*=d; // reduce our velocities by the decay factor for the next frame (z)

   particlemat.setcell1d(j, p_array[0],p_array[1],p_array[2]); // set the position for this particle in the Jitter matrix
   velomat.setcell1d(j, v_array[0],v_array[1],v_array[2]); // set the velocity for this particle in the Jitter matrix
  }
}

function particles(v) // change the number of particles we're working with
{
  PARTICLE_COUNT = v;

  // resize matrices
  noisegen.dim = PARTICLE_COUNT;
  particlemat.dim = PARTICLE_COUNT;
  velomat.dim = PARTICLE_COUNT;
  distmat.dim = PARTICLE_COUNT;
  attmat.dim = PARTICLE_COUNT;
  tempmat.dim = PARTICLE_COUNT;
  summat.dim = PARTICLE_COUNT;
  summat2.dim = PARTICLE_COUNT;
  scalarmat.dim = PARTICLE_COUNT;
  amat.dim = PARTICLE_COUNT;

  init(); // re-initialize particle system
}

function attractors(v) // change the number of gravity points we're working with
{
  ATTRACTOR_COUNT = v;

  // resize attractor matrix
  attgen.dim = ATTRACTOR_COUNT;

  init(); // re-initialize particle system
}

function accel(v) // set acceleration
{
  a = v*0.001;
}

function decay(v) // set decay
{
  d = v*0.001;
}

function mode(v) // change perform mode
{
  perform_mode = v;
}

</code></pre></p>

   <seealsolist>
      <seealso name="jit.expr">Evaluate expressions</seealso>
      <seealso name="jit.gl.handle">Use mouse movement to control position/rotation</seealso>
      <seealso name="jit.gl.render">Render Open GL</seealso>
      <seealso name="jit.window">Display data in a Window</seealso>
      <seealso name="js">Javascript in Max</seealso>
      <seealso name="qmetro">Queue-based metronome</seealso>
   </seealsolist>

   </chapter>
