5.03 Matrix Operations

What is a matrix?

A Matrix is a specialised type of multi-dimension array where all sub-arrays at the same level are of the same size. The advantage of using a matrix is that mathematical functions can be performed on all elements of the matrix, or on one sub-array, with a single command.

Creating a matrix?

You can create a matrix using a normal assignment.

The simplest matrix is a one-dimensional array.

mtxa = { -6, 2, -7}
mtxb = {  5, 4, -6}
mtxc = { -6.0, 2.0, -7.0}

To create a 3×2 matrix:

mtx = {{11, 12}, {21, 22}, {31, 32}}

That creates the array

| 11  12 |
| 21  22 | = mtx
| 31  32 |

The comma is used to separate column items. Rows are specified as sub-arrays within the array.

range mtxa(0 to 2, 0 to 2)
mtxa = {{}, {}, {1, 2}, {3, 4, 5}}

This creates the array

| 0  0  0 |
| 1  2  0 | = mtxa
| 3  4  5 |

Supported operators:

Add/sub:

mtxb = {{1, 2}, {3, 4}}
mtxc = {{5, 6}, {7, 8}}
mtxa = mtxb + mtxc
mtxc = mtxa - mtxb

Equal:

bool = (mtxa = mtxb)

Unary:

mtxa2 = -mtxa

Multiplication:

mtxa = {{1, 2}, {3, 4}}
mtxb = {{5}, {6}}
mtxc = mtxa * mtxb
mtxd = 0.8 * mtxa

Inverse:

mtxa = {{1, -1, 1}, {2, -1, 2}, {3, 2, -1}}
put inverse(mtxa)

Gauss-Jordan:

put "Solve this:"
put "  5x - 2y + 3z = -2"
put " -2x + 7y + 5z =  7"
put "  3x + 5y + 6z =  9"
put
mtxa = {{5, -2, 3}, {-2, 7, 5}, {3, 5, 6}}
mtxb = {{-2}, {7}, {9}}
mtxc = LinEqn(mtxa, mtxb)
put "{{x},{y},{z}} =" mtxc

By default arrays consist of a single row.

range mtxa(3)
mtxa = {1, 2, 3}
| 1 2 3 | = mtxa

To create an array of single column rows use:

range mtxa(3, 1)
mtxa = {{1}, {2}, {3}}
| 1 |
| 2 | = mtxa
| 3 |

???? Default operation a. = b. + 1 or a() = b() + 1


lynplex/lp0503.txt · Last modified: 2011/09/08 16:07 (external edit)