Matrix - What the Crazy Node is this ?

Topics concerning ICE & Math
User avatar
gustavoeb
Moderator
Posts: 587
Joined: 21 Jul 2010, 00:33
Skype: gustavoboehs

Re: Matrix - What the Crazy Node is this ?

Post by gustavoeb » 28 Oct 2013, 06:20

the matrix is a spreadsheet, you can feel it when you go to work, when you go to church, when you pay your taxes
Gustavo Eggert Boehs
Blog: http://www.gustavoeb.com.br/

nautilus
Posts: 29
Joined: 08 Mar 2011, 22:32

Re: Matrix - What the Crazy Node is this ?

Post by nautilus » 28 Oct 2013, 14:52

here you go - all explained very well (it is 3x3 matrices, but the 4th row only represents the translate aka the position, also explained in the help manual):
http://www.andynicholas.com/?p=861

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 18:32

What is a matrix?
----------------------

A matrix is a two dimensional array of numeric data, where each row or column consists of one or more numeric values.

The size of a matrix is defined in terms of the number of rows and columns. A matrix with M rows and N columns is defined as a M x N matrix.

Individual elements of the matrix are referenced using two index values. Using mathematical notation these are usually assigned the variables 'i' and 'j'. The order is row first, column second.

Matrix in Softimage are Zero (0) based indices which means that you start your count from Zero to M-1 (not from one). So the first row is row zero and the first column is column zero.

For example, if a matrix M with order 4x4 exists, then the elements of the matrix are indexed by the following row x column pairs:

| 00 01 02 03 |
| 10 11 12 13 |
| 20 21 22 23 |
| 30 31 32 33 |

The element at the top right of the matrix has i=0 and j=3

This is referenced as follows:

Code: Select all

 M   =  M
  i,j   0,3
In computer animation, the most commonly used matrices have either 2, 3 or 4 rows and columns. These are referred to as 2x2, 3x3 and 4x4 matrices respectively.

2x2 matrices are used to perform rotations, shears and other types of image processing.

3x3 matrices are used to perform low-budget 3D animation. Operations such as rotation and multiplication can be performed using matrix operations, but perspective depth projection is performed using standard optimized into pure divide operations.

4x4 matrices are used to perform high-end 3D animation. Operations such as multiplication and perspective depth projection can be performed using matrix mathematics.

General purpose NxN matrices can be used to perform image processing functions such as convolution.

Arithmetic operations which can be performed with matrices include addition, subtraction, multiplication, division and transpose.

Warnings


Softimage is row vector based therefore the order of operation with matrices is important: scale, rotate then translate. The order is easy to remember: it's the "SRT" order on the right side of your XSI screen.

Softimage is also a right hand system, Y up, Z toward the viewer. Remember this when reading other books or documentations as their system may be different.
Last edited by Daniel Brassard on 28 Oct 2013, 21:29, edited 4 times in total.
$ifndef "Softimage"
set "Softimage" "true"
$endif

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 19:18

What is the "order" of a matrix?
-------------------------------------

The "order" of a matrix is another name for the size of the matrix. A matrix with M rows and N columns is said to have order MxN.

How do matrices relate to coordinate systems?
--------------------------------------------------

With either 3x3 or 4x4 rotation, translation or shearing matrices, there is a simple relationship between each matrix and the resulting coordinate system.

The first three rows of the matrix define the direction vectors in the X, Y and Z axii respectively.

If a 4x4 matrix is defined as follows:

| A B C D |
| E F G H |
| I J K L |
| M N O P |

Then the direction vector for each axis is as follows:

X-axis = [ A B C ]

Y-axis = [ E F G ]

Z-axis = [ I J K ]


Edit: Thanks Graham for correcting me on this one. Cheers!
Last edited by Daniel Brassard on 28 Oct 2013, 21:32, edited 5 times in total.
$ifndef "Softimage"
set "Softimage" "true"
$endif

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 19:39

What is the identity matrix?
---------------------------------

The identity matrix is a square matrix in which has an identical number of rows and columns. Also, all the elements in which i=j are set to one. All others are set to zero. The identity matrix is also called the unit matrix and often noted in mathematics as I (for Identity).

For example a 4x4 identity matrix is as follows:

Code: Select all

| 1 0 0 0 |
| 0 1 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |
Note the location of the Ones .... they are in the major diagonal of the matrix.

What is the major diagonal of a matrix?
--------------------------------------------

The major diagonal of a matrix is the set of elements where the row number is equal to the column number ie.

M11, M22, M33 ....Mij where i=j

In the case of the identity matrix, only the elements on the major diagonal are set to 1, while all others are set to 0.

What is the use of the identity matrix?
---------------------------------------

The identity matrix can be particularly useful in simplifying mathematical equations, which in turn can make for more efficient and robust code. A trivial scalar example:

x + ax + bx = x ( 1 + a + b)

So instead of performing two multiplies and two addition, we can perform one multiply and two additions. And the more complex the equation, the more potential for benefit. Now a similar matrix example (N and M have the same dimensions ;->):

A + NA + MA = (I + N + M) A

(note the A is after the operation, thus preserving the matrix order of operation)

Matrix additions are much faster than matrix multiplications.

Another use for the identity matrix is when you want to invert a matrix. When inverting you are in fact solving the following equation:

A(Inverse)A = AA(Inverse)= I (the Identity matrix)
Last edited by Daniel Brassard on 28 Oct 2013, 22:04, edited 4 times in total.
$ifndef "Softimage"
set "Softimage" "true"
$endif

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 19:51

What is the inverse of a matrix?
-------------------------------------

Given a matrix M, then the inverse of that matrix, denoted as M-1 , is the matrix which satisfies the following expression:

Code: Select all

-1
M . M   = I
where I is the identity matrix.

Thus, multiplying a matrix with its inverse will generate the identity matrix. However, several requirements must be satisfied before the inverse of a matrix can be calculated.

These include that the width and height of the matrix are identical (equal number of rows and columns) and that the determinant of the matrix is non-zero.

Calculating the inverse of a matrix is a task often performed in order to implement inverse kinematics using spline curves.

How do I calculate the inverse of an arbitary matrix?
----------------------------------------------------------

Depending upon the size of the matrix, the calculation of the inverse can be trivial or extremely complicated.

For example, the inverse of a 1x1 matrix is simply the reciprocal (1/x) of the single element:

ie. M = | x |

Then the inverse is defined as:

Code: Select all

     -1   | 1 |
    M   = | - |
          | x |
Solving 2x2 matrices and larger can be achieved by using Kramer's Rule or by solving as a set of simultaneous equations.

However, in certain cases, such as identity or rotation matrices, the inverse is already known or can be determined from taking the transpose of the matrix.

How do I calculate the inverse of an identity matrix?
----------------------------------------------------------

Don't even bother. The inverse of an identity matrix is the identity matrix. ie.

Code: Select all

         -1
    I . I   = I
Any identity matrix will always have a determinant of +1.
Last edited by Daniel Brassard on 28 Oct 2013, 19:57, edited 3 times in total.
$ifndef "Softimage"
set "Softimage" "true"
$endif

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 19:55

What is the transpose of a matrix?
---------------------------------------

The transpose of matrix is the matrix generated when every element in the matrix is swapped with the opposite relative to the major diagonal

This can be expressed as the mathematical operation:

Code: Select all

    M'   = M
      ij    ji
However, this can only be performed if a matrix has an equal number of rows and columns.

If the matrix M is defined as:

Code: Select all

        |  0.707 -0.900 |
    M = |               |
        |  0.866  0.707 |

Then the transpose is equal to:

Code: Select all

        |  0.707  0.866 |
    T = |               |
        | -0.900  0.707 |
If the matrix is a rotation matrix, then the transpose is guaranteed to be the inverse of the matrix.
$ifndef "Softimage"
set "Softimage" "true"
$endif

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 20:06

How do I add two matrices together?
----------------------------------------

The rule of thumb with adding two matrices together is:

"add row and column to row and column"

This can be expressed mathematically as:

Code: Select all

    R   = M   + L
     ij    ij    ij
However, both matrices must be identical in size (same number of rows and same number of columns).

For example, if the 3x3 matrix M is added with the 3x3 matrix L then the result R is a 3x3 matrix as follow:

Code: Select all

    R = M + L

        | A B C |   | J K L |
        |       |   |       |
      = | D E F | + | M N O |
        |       |   |       |
        | G H I |   | P Q R |


        | A+J B+K C+L |
        |             |
      = | D+M E+N F+O |
        |             |
        | G+P H+Q I+R |
Last edited by Daniel Brassard on 28 Oct 2013, 21:38, edited 3 times in total.
$ifndef "Softimage"
set "Softimage" "true"
$endif

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 20:08

How do I subtract two matrices?
-------------------------------------

The rule of thumb with subtracting two matrices is similar to the addition of matrices:

"subtract row and column from row and column"

This can be expressed mathematically as:

Code: Select all

    R   = M   - L
     ij    ij    ij
However, both matrices must be identical in size.

For example, if the 3x3 matrix L is subtracted from the 3x3 matrix M then the result R is as follows:

Code: Select all

    R = M - L

        | A B C |   | J K L |
        |       |   |       |
      = | D E F | - | M N O |
        |       |   |       |
        | G H I |   | P Q R |


        | A-J B-K C-L |
        |             |
      = | D-M E-N F-O |
        |             |
        | G-P H-Q I-R |
Last edited by Daniel Brassard on 28 Oct 2013, 21:39, edited 1 time in total.
$ifndef "Softimage"
set "Softimage" "true"
$endif

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 20:10

How do I multiply two matrices together?
---------------------------------------------

The rule of thumb with multiplying two matrices together is:

"multiply row into column and sum the result".

This can be expressed mathematically as:

Code: Select all

          n
          --
    R   = \   M   x L
     ij   /    ik    kj
          --
          k=1
If the two matrices to be multiplied together have orders:

M = AxB and L = CxD

then the two values B and C must be identical.

Also, the resulting matrix has an order of AxD

Thus, it is possible to multiply a Nx4 matrix with a 4x4 matrix but not the other way around.

For example, if the 4x4 matrix M is defined as:

Code: Select all

        | A B C D |
    M = | E F G H |
        | I J K L |
        | M N O P |
and a 4x2 matrix L is defined as:

Code: Select all

    L = | Q R |
        | S T |
        | U V |
        | W X |
then the size of the resulting matrix is 4x2. The resulting matrix is defined as:

Code: Select all

    R = M x L

        | A B C D |   | Q R |
      = | E F G H | x | S T |
        | I J K L |   | U V |
        | M N O P |   | W X |

        | AQ+BS+CU+DW  AR+BT+CV+DX |
      = | EQ+FS+GU+HW  ER+FT+GV+HX |
        | IQ+JS+KU+LW  IR+JT+KV+LX |
        | MQ+NS+OU+PW  MR+NT+OV+PX |
$ifndef "Softimage"
set "Softimage" "true"
$endif

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 20:13

How do I square or raise a matrix to a power?
--------------------------------------------------

A matrix may be squared or even raised to an integer power. However there are several restrictions. For all powers, the matrix must be orthogonal ie. have the same width and height (same number of rows and columns)

For example,

Code: Select all

     -1
    M   
is the inverse of the matrix

Code: Select all

     0
    M   
generates the identity matrix

Code: Select all

     1
    M


leaves the matrix unchanged.

Code: Select all

     2
    M  
squares the matrix and

Code: Select all

    3
   M  
generates the cube of the matrix


Raising a matrix to a power greater than one involves multiplying a matrix by itself a specific number of times.

For example,

Code: Select all

     2
    M  = M . M
and so on.


Raising the identity matrix to any power always generates the identity matrix ie.

Code: Select all

     n
    I  = I
Last edited by Daniel Brassard on 28 Oct 2013, 21:44, edited 1 time in total.
$ifndef "Softimage"
set "Softimage" "true"
$endif

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 20:17

How do I multiply one or more vectors by a matrix?
-------------------------------------------------------

The best way to perform this task is to treat the list of vectors as a single matrix, with each vector represented as a row vector.

If N vectors are to be multiplied by a 4x4 matrix, then they can be treated as a single Nx4 matrix:

If the matrix is defined as:

Code: Select all

        | A B C D |
    M = | E F G H |
        | I J K L |
        | M N O P |
and the list of vectors is defined as:

Code: Select all

        | x1 y1 z1 1|
    V = | x2 y2 z2 1|
        | x3 y3 z3 1|
        | x4 y4 z4 1|
        | x5 y5 z5 1|
Note that an additional column of constant terms is added to the vector list, all of which are set to 1.0. This row does not exist. It is simply used to make the orders of the vector list V and the matrix M match.

Then the multiplication is performed as follows (remember Softimage is row vector based therefore the order is vector multiply by the matrix):

Code: Select all

           V . M = V'

| x1 y1 z1 1 |    | A B C D |    | x1.A+y1.E+z1.I+1.M      x1.B+y1.F+z1.J+1.N    x1.C+y1.G+z1.K+1.O  ... |
| x2 y2 z2 1 | .  | E F G H |=   | x2.A+y2.E+z2.I+1.M      x2.B+y2.F+z2.J+1.N    x2.C+y2.G+z2.K+1.O  ... |
| x3 y3 z3 1 |    | I J K L |    | x3.A+y3.E+z3.I+1.M      x3.B+y3.F+z3.J+1.N    x3.C+y3.G+z3.K+1.O  ... | 
| x4 y4 z4 1 |    | M N O P |    | x4.A+y4.E+z4.I+1.M      x4.B+y4.F+z4.J+1.N    x4.C+y4.G+z4.K+1.O  ... |
| x5 y5 z5 1 |                   | x5.A+y5.E+z5.I+1.M      x5.B+y5.F+z5.J+1.N    x5.C+y5.G+z5.K+1.O  ... |
Quick Check: a 5x4 vertors multiply by a 4x4 matrix will give you a 5x4 vectors, each row corresponding to the new vector.
Last edited by Daniel Brassard on 28 Oct 2013, 21:22, edited 7 times in total.
$ifndef "Softimage"
set "Softimage" "true"
$endif

grahamef
Posts: 281
Joined: 23 Jun 2009, 21:01

Re: Matrix - What the Crazy Node is this ?

Post by grahamef » 28 Oct 2013, 20:18

Daniel Brassard wrote: If a 4x4 matrix is defined as follows:

| A B C D |
| E F G H |
| I J K L |
| M N O P |

Then the direction vector for each axis is as follows:

X-axis = [ A E I ]

Y-axis = [ B F J ]

Z-axis = [ C G K ]
That's if you are using column vectors, like most of the world does (e.g. most math, physics, and engineering references).

However the CG world, including Softimage, tends to use row vectors. So in Softimage the X-axis is [A B C], the Y-axis is [E F G], and the Z-axis is .

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 20:22

What is the determinant of a matrix?
-----------------------------------------

The determinant of a matrix is a floating point value which is used to indicate whether the matrix has an inverse or not. If zero, then no inverse exists. If non-zero, then a determinant exists.

As an example, consider a matrix consisting of a single element:

Code: Select all

    M = [ 1 ].
For a matrix of this size, the determinant is simply the value of the single element.

Also, the inverse is simply the reciprocal (1/x) of this single element:

Code: Select all

   -1
  M   = [ 1 / M[0][0] ] 
If this single value is non-zero, then an inverse exists. In the case of the identity matrix, this happens to be 1 / 1 or 1.0

However, if the value of this single element is zero, then the determinant is also zero.

Attempting to calculate the reciprocal of zero (1/0), generates a value of infinity. This isn't permitted as far a matrices are concerned, so no inverse of the matrix exists.

For an identity matrix, the determinant is always equal to one. Any matrix with a determinant of 1.0 is said to be isotropic.

Thus all rotation matrices are said to be isotropic, since the determinant is always equal to 1.0.
Last edited by Daniel Brassard on 28 Oct 2013, 21:46, edited 1 time in total.
$ifndef "Softimage"
set "Softimage" "true"
$endif

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 20:24

How do I calculate the determinant of a matrix?
----------------------------------------------------

The determinant of a matrix is calculated using the Kramer's rule, where the value can be calculated by breaking the matrix into smaller matrices.

For a 2x2 matrix M, the determinant D is calculated as follows:

Code: Select all

        | A B |
    M = |     |
        | C D |

Code: Select all

    D = AD - CB
For 3x3 and 4x4 matrices, this is more complicated, but can be solved by methods such as the Kramer's Rule.

Determinant of a 3x3 matrix

Given the matrix M as follows:

Code: Select all

|A B C|
|D E F|
|G H I|
The determinant D is

Code: Select all

D = A (E.I-H.F) – B (D.I-G.F) + C (D.H-G.E)
Last edited by Daniel Brassard on 28 Oct 2013, 21:58, edited 2 times in total.
$ifndef "Softimage"
set "Softimage" "true"
$endif

User avatar
Daniel Brassard
Posts: 827
Joined: 18 Mar 2010, 23:38
Location: St. Thomas, Ontario

Re: Matrix - What the Crazy Node is this ?

Post by Daniel Brassard » 28 Oct 2013, 20:27

What are Isotropic and Anisotropic matrices?
-------------------------------------------------

An Isotropic matrix is one in which the sum of the squares of all three rows or columns add up to one. A matrix in which this is not the case, is said to be Anisotropic.

When 3x3 or 4x4 matrices are used to rotate and scale an object, it is sometimes necessary to enlarge or shrink one axis more than the others.

Another example is the implementation of "squash" and "stretch" with character animation. When a character is hit by a heavy object eg. an anvil, the desired effect is for the character to stretched out sideways and squashed vertically:

A suitable matrix would be as follows:

Code: Select all

        |  2   0   0    0  |
    M = |  0   2   0    0  |
        |  0   0   0.5  0  |
        |  0   0   0    1  |
However, there is problem looming ahead. While this matrix will cause no problems with the transformation of vertex data, it will cause problems with Gouraud shading using outward Normals.

Because the transformation stage is implemented using matrix multiplication, both vertex data and outward normal data will be multiplied with this matrix.

While this is not a problem with vertex data (it is the desired effect) it causes a major headache with the outward normal data.

After raw multiplication, each outward normal will no longer be normalized and consequently will affect other calculations such as shading and back-face culling.
$ifndef "Softimage"
set "Softimage" "true"
$endif