Scripting Guide > Data Structures > Matrices > Numeric Operations
Publication date: 04/12/2021

## Numeric Operations

You can perform numeric operations (such as subtraction, addition, and multiplication) on matrices. Most statistical methods are expressed in compact matrix notation and can be implemented in JSL.

For example, the following expression uses matrix multiplication and inversion to illustrate least squares regression:

b = (XX)-1Xy

Implement this equation through the following JSL expression:

`b = Inv( X′*X )*X′*y;`

### Basic Arithmetic

You can perform the following basic arithmetic on matrices:

addition

subtraction

multiplication

division (multiplying by the inverse)

Note: The standard multiply operator is a matrix multiplier, not an elementwise multiplier.

To perform matrix multiplication, use one of the following methods:

* operator

Multiply() function

Matrix Mult() function

To perform matrix division, use one of the following methods:

/ operator

Divide() function

Note the following about matrix multiplication and division:

Remember that while multiplication or division of scalars is commutative (ab = ba), multiplication or division of matrices is not commutative.

When one of the two elements is a scalar, elementwise multiplication or division is performed.

To use elementwise multiplication, use :* or the EMult() function.

To use elementwise division, use :/, or the equivalent EDiv() function.

#### Examples

`A = [1 2 3, 4 5 6, 7 8 9, 10 11 12];`
`B = [0 1 2, 2 1 0, 0 1 1, 2 0 0];`
`C = [1 2 3 4, 4 3 2 1, 0 1 0 1];`
`D = [0 1 2, 2 1 0, 1 2 0];`

Matrix addition:

`R = A + B;`

[1 3 5,

6 6 6,

7 9 10,

12 11 12]

Matrix subtraction:

`R = A - B;`

[1 1 1,

2 4 6,

7 7 8,

8 11 12]

Matrix multiplication (inner product of rows of A with columns of C):

`R = A * C;`

[9 11 7 9,

24 29 22 27,

39 47 37 45,

54 65 52 63]

Matrix division (equivalent to A*Inverse(D)):

`R = A / D;`

[1.5 0.5 0,

3 2 0,

4.5 3.5 0,

6 5 0]

Matrix elementwise multiplication:

`R = A :* B;`

[0 2 6,

8 5 0,

0 8 9,

20 0 0]

Matrix scalar multiplication:

`R = C * 2;`

[2 4 6 8,

8 6 4 2,

0 2 0 2]

Matrix scalar division:

`R = C / 2;`

[0.5 1 1.5 2,

2 1.5 1 0.5,

0 0.5 0 0.5]

Matrix elementwise division (division by zero results in missing values):

`R = A :/ B;`

[. 2 1.5,

2 5 .,

. 8 9,

5 . .]

### Numeric (Scalar) Functions on Matrices

Numeric functions work elementwise on matrices. Most of the pure numeric functions can be applied to matrices, resulting in a matrix of results. You can mix conformable matrix and scalar arguments.

Examples of numeric functions include the following:

Sqrt(), Root(), Log(), Exp(), ^ Power(), Log10()

Abs(), Mod(), Floor(), Ceiling(), Round(), Modulo()

Sine,(), Cosine(), Tangent(), ArcSine(), and other trigonometry functions.

Normal Distribution(), and other probability functions.

#### Example

`A = [1 2 3, 4 5 6, 7 8 9, 10 11 12];`
`B = Sqrt( A ); // elementwise square root`

[1 1.414213562373095 1.732050807568877,

2 2.23606797749979 2.449489742783178,

2.645751311064591 2.82842712474619 3,

3.16227766016838 3.3166247903554 3.464101615137754]

Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).