Classes (extension) |

# Matrix : Array : ArrayedCollection : SequenceableCollection : Collection : ObjectExtension

an ordered 2-dimensional array of numbers
Source: Matrix.sc

## Description

Matrices are a 2-dimensional Array whose slots may only contain (at the moment 'real' ) numbers. Their shape is fully described by the number of rows and columns(cols). Each element can be adressed by 2 indices (row,col), where row (col) ranges between 0 and rows-1 (cols-1). For a lesson on matrices, read your math books from school. Or this reference if you like it the hard way: http://www.ee.ic.ac.uk/hp/staff/dmb/matrix/intro.html#Intro

Array2DMatrix uses an array-of-rowsâ€“notation. This is the same concept as found in Array2D. As a subclass of Array, Matrix responds to far more methods than given in this helpfile. Be aware of strange results when using them. This is meant to support only the basic matrix manipulations. Most of this is not designed for realtime action. Too slow, not optimized.

Part of MathLib, a diverse library of mathematical functions.

## Class Methods

### Matrix.newClear(rows: 1, cols: 1)

Create a new Matrix of shape (rows, cols) filled with zeros. Matrix.newClear(3,3).postln;

#### Arguments:

 rows rows cols cols

### Matrix.with(array)

Create a new Matrix whose rows are filled with the given subarrays.

Matrix.with([[1,2,3],[4,5,6],[7,8,9]]).postln;

#### Arguments:

 array array

### Matrix.withFlatArray(rows, cols, array)

Create a new Matrix from a 1-dimensional array of shape (rows , cols). Matrix.withFlatArray(3,3,[1,2,3,4,5,6,7,8,9]).postln;

#### Arguments:

 rows rows cols cols array array

### Matrix.newIdentity(n)

Create a new identity matrix of shape (n,n). Matrix.newIdentity(3).postln;

 n n

### Matrix.newDiagonal(diagonal)

Create a new square diagonal matrix.

#### Arguments:

 diagonal An array of values with which to fill the diagonal.

### Matrix.newDFT(n)

Create a new discrete Fourier transform matrix of shape (n,n). Matrix.newDFT(3).postln;

 n n

### Matrix.newIDFT(n)

Create a new inverse discrete Foutier transform matrix of shape (n,n). Matrix.newIDFT(3).postln;

 n n

### Matrix.fill(rows, cols, func)

fill the matrix by evaluating function. function is passed two arguments: row, col

#### Arguments:

 rows rows cols cols func function

### Matrix.mul(m1: 0, m2: 0)

#### Arguments:

 m1 ArrayA m2 ArrayB

## Instance Methods

### .rows

#### Returns:

the number of rows

### .cols

#### Returns:

the number of columns

### .shape

#### Returns:

the number of rows and columns as array [rows, cols]

### .postmln

post the matrix as 2D representation.

### .doRow(row, func)

evaluate function for each element of row; function is passed two arguments: item, col

#### Arguments:

 row row func function

### .doCol(col, func)

evaluate function for each element of col; function is passed two arguments: item, row

#### Arguments:

 col col func function

### .doMatrix(function)

evaluate function for each element ; function is passed three arguments: item, row,col;

#### Arguments:

 function function

### .colsDo(func)

Iterate over the columns. Each column will be passed to func in turn.

### .rowsDo(func)

Iterate over the rows. Each row will be passed to func in turn.

### .put(row, col, val)

put a single element at (row, col)

#### Arguments:

 row row col col val value

### .putRow(row, vals)

put a row of elements at (row)

Matrix.newClear(3,3).putRow(0,[2,4,6]).postln

#### Arguments:

 row row vals values

### .putCol(col, vals)

put a column of elements at (col)

Matrix.newClear(3,3).putCol(1,[2,4,6]).postln

#### Arguments:

 col col vals values

### .fillRow(row, func)

fill a row by evaluating function for each element; function is passed two arguments: row, col.

#### Arguments:

 row row func function

### .fillCol(col, func)

fill a column by evaluating function for each element; function is passed two arguments: row, col.

#### Arguments:

 col col func function

### .exchangeRow(posA, posB)

exchange two rows

#### Arguments:

 posA rowA posB rowB

### .exchangeCol(posA, posB)

exchange two cols

#### Arguments:

 posA colA posB colB

### .at(row, col)

#### Arguments:

 row row col col

### .get(row, col)

#### Arguments:

 row row col col

#### Returns:

element at (row,col)

### .getRow(row)

#### Arguments:

 row row

#### Returns:

an array from row

### .getCol(col)

#### Arguments:

 col col

#### Returns:

an array from column

### .getDiagonal

#### Returns:

an array from the diagonal elements

an array of rows

### .flat

#### Returns:

a one slot array of all elements

### .fromRow(row)

#### Arguments:

 row row

#### Returns:

a new matrix from row

### .fromCol(col)

#### Arguments:

 col col

#### Returns:

a new matrix from column

### .getSub(rowStart: 0, colStart: 0, rowLength, colHeight)

get a sub-matrix from within matrix.

#### Arguments:

 rowStart Row index to begin copying. colStart Column index to begin copying. rowLength The number of elements to copy from each row. colHeight The number of elements to copy from each column.

#### Returns:

a new matrix

add a row (values) to the matrix and return. receiver is unchanged.

#### Arguments:

 rowVals values

add a column (values) to the matrix and return. receiver is unchanged.

#### Arguments:

 colVals values

### .insertRow(col, rowVals)

insert a row (values) in matrix and return. receiver is unchanged.

#### Arguments:

 col col rowVals values

### .insertCol(row, colVals)

insert a column (values) in matrix and return. receiver is unchanged.

#### Arguments:

 row row colVals values

### .removeRow(row)

#### Arguments:

 row row

#### Returns:

a new matrix without row

### .removeCol(col)

#### Arguments:

 col col

#### Returns:

a new matrix without column

### .collect(func)

#### Arguments:

 func function

#### Returns:

a new matrix by evaluating function for each element. function is passed three arguments: item, row, col.

### .sub(row, col)

#### Arguments:

 row row col col

#### Returns:

a submatrix that results from matrix by crossing out row and col

### .flop

#### Returns:

the transpose of matrix

### .inverse

#### Returns:

the inverse of a square matrix

### .gram

#### Returns:

the gram matrix (the transpose of matrix multiplied with matrix) T^t * T

### .pseudoInverse

#### Returns:

the pseudoInverse of a matrix

### *(that)

#### Returns:

the result of matrix multiplication: matrix * matrix2 matrix.cols must equal matrix2.rows

multiplication with aNumber for each element

### +(summand2)

#### Returns:

(matrix + matrix2) matrix must have the same shape as matrix2

summation with aNumber for each element

### -(summand2)

#### Returns:

(matrix - matrix2) matrix must have the same shape as matrix2

subtraction with aNumber for each element

### .center(mean)

#### Arguments:

 mean mean

#### Returns:

a matrix centred around the mean vector (which will be calculated for you if not supplied)

From superclass: SequenceableCollection

Bilateral thresholding.

#### Arguments:

 thresh When the input.abs < thresh, the output is forced to 0. Should be a positive value. adverb Optional, for processing Collections. See Adverbs for Binary Operators.

### .sum

#### Returns:

the sum of all elements

### .sumRow(row)

#### Arguments:

 row row

#### Returns:

the sum of all elements of desired row

### .sumCol(col)

#### Arguments:

 col col

#### Returns:

the sum of all elements of desired column

### .sumRows(function)

#### Arguments:

 function function

#### Returns:

an array giving the sum for every row

### .sumCols(function)

#### Arguments:

 function function

#### Returns:

an array giving the sum for every column

### .grammian

#### Returns:

the grammian of a matrix (determinant of the gram matrix)

### .mean

#### Returns:

the mean array, calculated over the rows

### .cov(mean)

#### Arguments:

 mean mean

#### Returns:

the sample covariance, if rows represent observations

### .covML(mean)

#### Arguments:

 mean mean

#### Returns:

the Maximum-Likelihood covariance estimate, if rows represent observations and the distribution is assumed to be Gaussian

the determinant

### .cofactor(row, col)

#### Arguments:

 row row col col

#### Returns:

the cofactor to element (row, col) this is the determinant of the matrix.sub(row, col) mutiplied with (-1)**(row+col)

### .trace

#### Returns:

the trace of matrix: (sum of the diagonal elements)

### .norm

#### Returns:

the euclidean norm of matrix ( sqrt( tr [ A*A(T) ] ) )

### .isSquare

#### Returns:

true for (n x n) - matrices

### .isSingular

#### Returns:

true if determiant is zero

### .isRegular

#### Returns:

true if determiant is Non-zero

### .isSymmetric

#### Returns:

true if matrix is symmetric

### .isAntiSymmetric

#### Returns:

true if matrix is antisymmetric

### .isPositive

#### Returns:

true if matrix is strictly positive

### .isNonNegative

#### Returns:

true if matrix is positive / non negative (zeros allowed)

### .isNormal

#### Returns:

true if matrix is normal

### .isZero

#### Returns:

true for a zero matrix

### .isIntegral

#### Returns:

true if matrix is integral An Integral matrix is one whose elements are all integers.

### .isIdentity

#### Returns:

true if matrix is an identity matrix (the diagonal elements are all 1; the nondigonal elements are all zero)

### .isDiagonal

#### Returns:

true if matrix is diagonal a(i,j)=0 unless i=j.

### .isOrthogonal

#### Returns:

true if matrix is orthogonal (the matrix multiplied with its transpose is an identity matrix)

### .isIdempotent

#### Returns:

true if matrix is idempotent (the squared matrix equals itself)

### ==(matrix2)

#### Returns:

true if matrix equals matrix2

### .neg

From superclass: SequenceableCollection

### .bitNot

From superclass: SequenceableCollection

### .abs

From superclass: SequenceableCollection

### .ceil

From superclass: SequenceableCollection

### .floor

From superclass: SequenceableCollection

### .frac

From superclass: SequenceableCollection

### .sign

From superclass: SequenceableCollection

### .squared

From superclass: SequenceableCollection

### .cubed

From superclass: SequenceableCollection

### .sqrt

From superclass: SequenceableCollection

### .exp

From superclass: SequenceableCollection

### .reciprocal

From superclass: SequenceableCollection

new matrices.

### Binary Operators

usage: matrix bop aNumber or: aNumber bop matrix

From superclass: SequenceableCollection

From superclass: SequenceableCollection

### %(that)

From superclass: Object

### **(that)

From superclass: Object

From superclass: SequenceableCollection

From superclass: SequenceableCollection

new matrices

## Authors

• sc.solar(at)studiobeige.de (Original author)
• Till Bovermann