Matrix:
Filter:
MathLib/Classes (extension) | Math | Linear Algebra | Libraries > MathLib > Matrix

Matrix
ExtensionExtension

an ordered 2-dimensional array of numbers

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

.newClear

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

Arguments:

rows

cols

.with

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

.withFlatArray

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

cols

array

.newIdentity

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

Arguments:

n

.newDiagonal

Create a new square diagonal matrix.

Arguments:

diagonal

An array of values with which to fill the diagonal.

.newDFT

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

Arguments:

n

.newIDFT

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

Arguments:

n

.fill

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

Arguments:

rows

cols

function

.mul

Arguments:

ArrayA

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

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

Arguments:

row

function

.doCol

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

Arguments:

col

function

.doMatrix

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

Arguments:

function

.colsDo

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

.rowsDo

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

Changing Matrices

.put

put a single element at (row, col)

Arguments:

row

col

value

.putRow

put a row of elements at (row)

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

Arguments:

row

values

.putCol

put a column of elements at (col)

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

Arguments:

col

values

.fillRow

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

Arguments:

row

function

.fillCol

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

Arguments:

col

function

.exchangeRow

exchange two rows

Arguments:

rowA

rowB

.exchangeCol

exchange two cols

Arguments:

colA

colB

Return Arrays

.at

Arguments:

row

col

.get

Arguments:

row

col

Returns:

element at (row,col)

.getRow

Arguments:

row

Returns:

an array from row

.getCol

Arguments:

col

Returns:

an array from column

.getDiagonal

Returns:

an array from the diagonal elements

.asArray

Returns:

an array of rows

.flat

Returns:

a one slot array of all elements

Return Matrices

.fromRow

Arguments:

row

Returns:

a new matrix from row

.fromCol

Arguments:

col

Returns:

a new matrix from column

.getSub

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

.addRow

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

Arguments:

values

.addCol

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

Arguments:

values

.insertRow

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

Arguments:

col

values

.insertCol

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

Arguments:

row

values

.removeRow

Arguments:

row

Returns:

a new matrix without row

.removeCol

Arguments:

col

Returns:

a new matrix without column

.collect

Arguments:

function

Returns:

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

.sub

Arguments:

row

col

Returns:

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

.flop

Returns:

the transpose of matrix

.adjoint

Returns:

the adjoint or adjugate of a square 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

*

Returns:

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

multiplication with aNumber for each element

+

Returns:

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

summation with aNumber for each element

-

Returns:

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

subtraction with aNumber for each element

.center

Arguments:

mean

Returns:

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

.thresh2

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.

Discussion:

Return Characteristic Values

.sum

Returns:

the sum of all elements

.sumRow

Arguments:

row

Returns:

the sum of all elements of desired row

.sumCol

Arguments:

col

Returns:

the sum of all elements of desired column

.sumRows

Arguments:

function

Returns:

an array giving the sum for every row

.sumCols

Arguments:

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

Arguments:

mean

Returns:

the sample covariance, if rows represent observations

.covML

Arguments:

mean

Returns:

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

Return Characteristic Values of Square Matrices

.det

Returns:

the determinant

.cofactor

Arguments:

row

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) ] ) )

Testing

.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)

==

Returns:

true if matrix equals matrix2

Unary Operators: matrix.uop

.neg

.bitNot

.abs

.ceil

.floor

.frac

.sign

.squared

.cubed

.sqrt

.exp

.reciprocal

Returns:

new matrices.

Binary Operators

usage: matrix bop aNumber or: aNumber bop matrix

/

.div

%

**

.min

.max

Returns:

new matrices

Authors