# Defining Matrices

Page Contents

To use you must include MathymaWrite.js and MathymaMatrix.js as scripts in the head section of the page. All matrix prints should be in a mathyma class < div > :

```< head >
...
< script type="text/javascript" src="MathymaWrite.js" > < /script >
< script type="text/javascript" src="MathymaMatrix.js" > < /script >
...
< /head > < body >
...
< div class="mathyma" >
...
< script > document.write(wMatA.McPrint()); < /script >
...
< /div >
...
```

## Defining a Matrix

The easiest way to define a matrix is to use the "constructor" funtion with a string of values of matrix elements. The rows of the matrix are separated by slash (/) and the elements within a row are separated by a comma (,). For example:

```< script >
var wMatA = new mathyma.Matrix ("1,2/4,5,6/7,8,9");
< /script >
```

defines a 3 x 3 matrix:

Note that there are only two elements in the first row in the input string. defines the number of columns in a matrix as being the maximum number of elements in any row in the input string. Any row which has fewer elements will be "padded out" with zeroes.

## Special Matrices

### Identity Matrix

To define the n x n identity matrix, the first letter of the input string must be "I", followed immediately by the dimension:

```< script >
var wI5 = new mathyma.Matrix ("I5");
< /script >
```

### Diagonal Matrix

To define a diagonal matrix, the first parameter in input string must be "D", the following parameters will be the values on the diagonal:

```< script >
var wDiag = new mathyma.Matrix ("D,-1,2,-3,4");
< /script >
```

## Displaying a Matrix

To display a matrix, provides a mathyma.Matrix method, McPrint(), which outputs the matrix in format, so that it can be used as a parameter in document.write(). Remember to include MathymaDom.js as a script in the head section of the page.

For example to display the matrix defined in the previous section we use these lines

```< script >
document.write(wMatA.McPrint());
< /script >
```

And this is the output:

which when translated by , becomes:

So remember that all matrix prints should be in a mathyma class < div > :

```...
< div class="mathyma" >
...
< script >
document.write(wMatA.McPrint());
< /script >
...
< /div >
...
```

However as McPrint() just returns a string, this can be combined with other strings to provide more sofisticated output:

```< script >
document.write("#A _ _ = _ _ " + wMatA.McPrint());
< /script >
```

which gives:

The output so far has all had two decimal places showing. This is because defaults to a precision of two. To change this you must change the value of the variable mathyma.Matrix_Precision. For example if we wanted to display the above matrix as an integer matrix, we use the following lines

```< script >
mathyma.Matrix_Precision = 0;
document.write("#A _ _ = _ _ " + wMatA.McPrint());
< /script >
```

giving:

Remember that once you've changed the precision, this holds for all the following matrix prints on the page, until you change it again. For the moment we will leave the precision as 0 on this page.

## Matrix from Array

You can also define a matrix based on a two-dimensional array. This will usually only be of interest to those using mathyma.Matrix objects in a wider application. Here's an example:

```< script >
var wArr = new Array ();
wArr = new Array(11,12);
wArr = new Array(13,14);
wArr = new Array(15,20,30);
var wMatB = new mathyma.Matrix (wArr);
document.write("#B _ _ = _ _ " + wMatB.McPrint());
< /script >
```

## Matrix Concatenation

It is often useful to be able to build a matrix up using other matrices as blocks or sub-matrices. provides two methods to achieve this: vertical concatenation, VConcat ( ), and horizontal concatenation, HConcat ( ). The resulting matrices can be further combined to build even larger matrices.

### Vertical Concatenation

```< script >
var wMatTop = new mathyma.Matrix("11,12,13/21,22,23");
var wMatBot = new mathyma.Matrix("31,32,33/41,42,43");
document.write("Concatenating _ " + wMatTop.McPrint() + " _ and _ " + wMatBot.McPrint());
document.write(" _ vertically, _ giving _ " + wMatTop.VConcat(wMatBot).McPrint() );
< /script >
```

### Horizontal Concatenation

```< script >
var wMatLeft = new mathyma.Matrix("11,12,13/21,22,23");
var wMatRight = new mathyma.Matrix("14,15/24,25");
document.write("Concatenating _ " + wMatLeft.McPrint() + " _ and _ " + wMatRight.McPrint());
document.write(" _ horizontally, _ giving _ " + wMatLeft.HConcat(wMatRight).McPrint() );
< /script >
```