Matrix: Difference between revisions

From Graal Bible
m (→‎matrixcreate(): Clarification)
No edit summary
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
A '''matrix''' (''pl.'' matricies) is a list of numbers, generally who have a relationship between them, that are ordered in a type of mathematical array. A matrix, m, may be written as either |m|, [m], or sometimes even (m) in mathematics, where m is a placeholder for the numbers in the matrix.
[[Category:Scripting Reference]]
 
A '''matrix''' (''pl.'' matrices) is a list of numbers, generally who have a relationship between them, that are ordered in a type of mathematical array. A matrix, m, may be written as either [m], or (m) in mathematics, where m is a placeholder for the numbers in the matrix.
''Any matrix examples below will use the |m| notation for simplicity.''


== Reading a Matrix ==
== Reading a Matrix ==
A matrix is ordered in rows and colums, which are counted by increasing i and j respectively. One can think of a matrix as a 2-dimensional array where each object in the 1st dimension holds an array of numbers in that row.
Typically in math, a matrix is ordered in rows and columns, which are counted by increasing i and j respectively. One can think of a matrix as a 2-dimensional array where each object in the 1st dimension holds an array of numbers in that row.


An entry at i=2 and j=3 is noted by either M[2,3] or m<sub>2,3</sub>. These numbers are important for some modifications involving these matricies.
An entry at i=2 and j=3 is noted by either M[2,3] or m<sub>2,3</sub>. These numbers are important for some modifications involving these matrices.


== Demystifying Matricies in GraalScript ==
In GraalScript, however, matrices are used to perform calculations involving objects that rotate, move, or are scaled. The first three elements in such an array are for position:
If you have ever tested any of the matrix functions built into GraalScript, you may be lost as to where the return value comes from. Well, luckily there are some adept enough with math to understand these functions. These are their findings.
 
{{code|1=
x = m[0]; y = m[1]; z = m[2];
}}
The following three elements are for rotation:
{{code|1=
eulerrotation = m.subarray(3,3);
}}
The final element is angle in degrees:
{{code|1=
temp.angle = degtorad(m[6]);
}}
== Demystifying Matrices in GraalScript ==
If you have ever tested any of the matrix functions built into GraalScript, you may be lost as to where the return value comes from. Well, luckily there are some adept enough with math to understand these fu {{code|1=nctions. These are their findings.


=== matrixcreate() ===
=== matrixcreate() ===
Line 18: Line 30:
strMatrix = matrixcreate(v,a);
strMatrix = matrixcreate(v,a);
}}
}}
Where 'a' describes the rotation of the vector.
Where v describes the translation (position) and 'a' describes the rotation (rotation axis and angle).


==== Return Value ====
==== Return Value ====
Line 46: Line 58:


<hr width="33%" noshade="noshade">
<hr width="33%" noshade="noshade">
(1) The returned values are accurate within &plusmn;10<sup>-8</sup>
(1) The returned values are accurate within &plusmn;10<sup>-5</sup>


So far, we have 6 of 7 figured out. The last is the simplest of them all. Plugging the new vector into vectorlen() or using &radic;(x<sup>2</sup> + y<sup>2</sup> + z<sup>2</sup>) returns it's magnitude. This, multiplied by the fourth number of the second parameter, is the last of the returned values (and as far as anyone can tell the vector passed to the function has no bearing on the return value).
So far, we have 6 of 7 figured out. The last is the simplest of them all. Plugging the new vector into vectorlen() or using &radic;(x<sup>2</sup> + y<sup>2</sup> + z<sup>2</sup>) returns it's magnitude. This, multiplied by the fourth number of the second parameter, is the last of the returned values (and as far as anyone can tell the vector passed to the function has no bearing on the return value).


== Matricies in Mathematics ==
=== matrixcreatefromeuler() ===
There are several matrix-specific operations in mathematics that are helpful in calculations of objects in space.
Let '''r''' be an array describing rotation, {&alpha;,&beta;,&gamma;}.


=== Matrix Addition and Subtraction ===
==== Usage ====
These operations are generally between two equal-sized matricies. This is a rather simple operation. however, we can assume that the matricies can zero-fill until they are of equal size. If we have two matricies, m and n, the sum of these two is merely m<sub>i,j</sub>&plusmn;n<sub>i,j</sub> for each i and j where 0 &le; i &lt; # of rows and 0 &le; j &lt; # of columns.
{{code|1=
strMatrix = matrixcreatefromeuler(r);
}}
Where r describes the rotation (rotation axis and angle).
 
==== Return Value ====
Returns a matrix array of length 7.


<table>
==== Example ====
  <tr>
{{code|1=
    <td>
temp.matrix = matrixcreatefromeuler({pi/2,0,pi/2});
<table frame="vsides" style="border: 1px solid black;" cellspacing=3>
// This will return 0,0,0,-0.577350258,-0.577350258,-0.577350258,119.999976018
  <tr>
}}
    <td>a</td>
    <td>b</td>
  </tr>
  <tr>
    <td>c</td>
    <td>d</td>
  </tr>
  <tr>
    <td>e</td>
    <td>f</td>
  </tr>
</table>
    </td>
    <td>+</td>
    <td>
<table frame="vsides" style="border: 1px solid black;" cellspacing=3>
  <tr>
    <td>g</td>
    <td>h</td>
  </tr>
  <tr>
    <td>i</td>
    <td>j</td>
  </tr>
  <tr>
    <td>k</td>
    <td>l</td>
  </tr>
</table>
    </td>
    <td>=</td>
    <td>
<table frame="vsides" style="border: 1px solid black;" cellspacing=3>
  <tr>
    <td>a+g</td>
    <td>b+h</td>
  </tr>
  <tr>
    <td>c+i</td>
    <td>d+j</td>
  </tr>
  <tr>
    <td>e+k</td>
    <td>f+l</td>
  </tr>
</table>
    </td>
  </tr>
</table>
=== Matrix Multiplication ===
In mathematics, two matricies can be multiplied together to create a third matrix. There is only one rule to multiplying matricies. That is that the number of columns of the first must be equal to the number of rows of the second.
A matrix that is ''a'' high and ''b'' wide can multiply with another matrix that is ''b'' high and ''c'' wide, but not the reverse. Their products will be a matrix with height ''a'' and width ''c''.
<table>
  <tr>
    <td>
<table frame="vsides" style="border: 1px solid black;" cellspacing=3>
  <tr>
    <td>a</td>
    <td>b</td>
    <td>c</td>
  </tr>
  <tr>
    <td>d</td>
    <td>e</td>
    <td>f</td>
  </tr>
  <tr>
    <td>g</td>
    <td>h</td>
    <td>i</td>
  </tr>
</table>
    </td>
    <td>
<table frame="vsides" style="border: 1px solid black;" cellspacing=3>
  <tr>
    <td>j</td>
  </tr>
  <tr>
    <td>k</td>
  </tr>
  <tr>
    <td>l</td>
  </tr>
</table>
    </td>
    <td>=</td>
    <td>
<table frame="vsides" style="border: 1px solid black;" cellspacing=3>
  <tr>
    <td>aj+bk+cl</td>
  </tr>
  <tr>
    <td>dj+ek+fl</td>
  </tr>
  <tr>
    <td>gj+hk+ij</td>
  </tr>
</table>
    </td>
  </tr>
  <tr>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;3x3</td>
    <td>3x1</td>
    <td>&nbsp;</td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3x1</td>
  </tr>
</table>


The above example could be used to simply describe the equations of three planes and find the point at which three planes in three dimensions meet (three planes meet at exactly one point, two planes meet at a unique line).
==== The Math Behind the Function ====
'''Coming soon!'''

Latest revision as of 19:40, 5 April 2010

A matrix (pl. matrices) is a list of numbers, generally who have a relationship between them, that are ordered in a type of mathematical array. A matrix, m, may be written as either [m], or (m) in mathematics, where m is a placeholder for the numbers in the matrix.

Reading a Matrix

Typically in math, a matrix is ordered in rows and columns, which are counted by increasing i and j respectively. One can think of a matrix as a 2-dimensional array where each object in the 1st dimension holds an array of numbers in that row.

An entry at i=2 and j=3 is noted by either M[2,3] or m2,3. These numbers are important for some modifications involving these matrices.

In GraalScript, however, matrices are used to perform calculations involving objects that rotate, move, or are scaled. The first three elements in such an array are for position:


x = m[0]; y = m[1]; z = m[2];

The following three elements are for rotation:

eulerrotation = m.subarray(3,3);

The final element is angle in degrees:

temp.angle = degtorad(m[6]);

Demystifying Matrices in GraalScript

If you have ever tested any of the matrix functions built into GraalScript, you may be lost as to where the return value comes from. Well, luckily there are some adept enough with math to understand these fu {{code|1=nctions. These are their findings.

matrixcreate()

Let v be a vector and a consisting of a vector and a scalar.

Usage

strMatrix = matrixcreate(v,a);

Where v describes the translation (position) and 'a' describes the rotation (rotation axis and angle).

Return Value

Returns a matrix array of length 7 (this will be explained later).

Example

temp.matrix = matrixcreate({1,1,1},{1,7,1,1});

// This will return 1,1,1,0.140028,0.980196058,0.140028,7.14595223

The Math Behind the Function

The first three numbers of the return value should be rather familiar. It is the vector you passed to the function. The other four seem to come out of nowhere. However, there is a method to this madness. The first of these three are merely a property of angles. Because of the Dot Product of vectors, we are able to calculate the cosine of the angle between two vectors. Well, we have actually created a new vector in this code!

As defined in Vectors, we know that there are three unit vectors that lay on the x, y, and z axes. These vectors have been given the letters i, j, and k, respectively. The first value defined in the "rotation" is multiplied to i, the second to j, and so forth, and this makes a new vector. However, this still does not tell us why we have these numbers returned to us.

From the definition of the dot product, we know that

cos(θ) = u&#0149;v/(|u||v|)

The first three numbers returned to us are the cosine of the angles between the new vector (in this case, <1,7,1>) and each of our unit vectors. For example:

cos(α) = <1,7,1>&#0149;<1,0,0>/(|<1,7,1>||<1,0,0>|) = 1/√51 = 0.1400280084
cos(β) = <1,7,1>&#0149;<0,1,0>/(|<1,7,1>||<0,1,0>|) = 7/√51 = 0.9801960588
cos(γ) = <1,7,1>&#0149;<0,0,1>/(|<1,7,1>||<0,0,1>|) = 1/√51 = 0.1400280084

There is an evident difference between the values given here and the return value. A computer cannot calculate cosine on its own. It merely uses a method of estimation.

cos(α)2 + cos(β)2 + cos(γ)2 = 1 (1)

(1) The returned values are accurate within ±10-5

So far, we have 6 of 7 figured out. The last is the simplest of them all. Plugging the new vector into vectorlen() or using √(x2 + y2 + z2) returns it's magnitude. This, multiplied by the fourth number of the second parameter, is the last of the returned values (and as far as anyone can tell the vector passed to the function has no bearing on the return value).

matrixcreatefromeuler()

Let r be an array describing rotation, {α,β,γ}.

Usage

strMatrix = matrixcreatefromeuler(r);

Where r describes the rotation (rotation axis and angle).

Return Value

Returns a matrix array of length 7.

Example

temp.matrix = matrixcreatefromeuler({pi/2,0,pi/2});

// This will return 0,0,0,-0.577350258,-0.577350258,-0.577350258,119.999976018

The Math Behind the Function

Coming soon!