Matrices
51 entries. What each one takes, what it returns, and a working example.
A new matrix with the cells at each position added together.
The two matrices are added cell by cell. The result is a new matrix; the inputs are unchanged.
a = matrixNew(2, 2, 1) b = matrixNew(2, 2, 2) s = matrixAdd(a, b) plot(matrixGet(s, 0, 0))
1+2 = 3 (every cell).
Returns nothing (na); it grows the matrix by one column in place.
Inserts the array as a column at the given position and increases the column count. If the matrix is empty (0 rows), the added column's length sets the row count.
m = matrixNew(2, 1, 1) matrixAddCol(m, 1, arrayFrom(7, 8)) plot(matrixGet(m, 0, 1))
First cell of the appended column is 7.
Returns nothing (na); it grows the matrix by one row in place.
Inserts the array as a row at the given position and increases the row count. If the matrix is empty (0 columns), the first added row's length sets the column count.
m = matrixNew(1, 2, 1) matrixAddRow(m, 1, arrayFrom(7, 8)) plot(matrixGet(m, 1, 0))
First cell of the appended row is 7.
The arithmetic mean of all cells; na if the matrix is empty or not a matrix.
Divides the sum by the cell count. Returns na (not 0) on an empty matrix; guard the result with na().
m = matrixNew(2, 2, 4) plot(matrixAvg(m))
All cells 4 → mean 4.
An array holding the values of column j; an empty array if the index is out of range.
Collects the column top to bottom into an array. The returned array is a copy; it does not affect the matrix.
m = matrixNew(3, 2, 5) c = matrixCol(m, 1) plot(arraySize(c))
Column 1 has 3 elements.
The number of columns in the matrix; 0 if it is not a matrix.
Used as the loop bound over columns. matrixColumns is a second name for the same thing.
m = matrixNew(3, 2, 0) plot(matrixCols(m))
2 columns.
The number of columns in the matrix; 0 if it is not a matrix.
Returns exactly the same result as matrixCols — an alternative name provided for readability.
m = matrixNew(3, 4, 0) plot(matrixColumns(m))
4 columns.
A new matrix with m2's rows appended below m1's rows.
Performs a vertical stack (appends rows below). The result is a new matrix; the inputs are unchanged.
a = matrixNew(1, 2, 1) b = matrixNew(2, 2, 2) c = matrixConcat(a, b) plot(matrixRows(c))
1 row + 2 rows → 3 rows.
An independent (deep) copy of the source; an empty 0×0 matrix if it is not a matrix.
The copy is fully separate from the original; changing one does not affect the other. Used to experiment on a matrix without corrupting it.
m = matrixNew(2, 2, 1) c = matrixCopy(m) matrixSet(c, 0, 0, 9) plot(matrixGet(m, 0, 0))
Even after the copy changes, the original [0,0] stays 1.
The determinant of the matrix (a single number); na if it is not square or not a matrix. Returns 0 for a singular (non-invertible) matrix.
Computed by elimination with partial pivoting. A determinant of 0 means the matrix has no inverse (its rows/columns are dependent).
m = matrixNew(2, 2, 0) matrixSet(m, 0, 0, 2) matrixSet(m, 1, 1, 3) plot(matrixDet(m))
Diagonal 2,3 → determinant 6.
A new matrix with m2 subtracted from m1.
If the second argument is a matrix, subtraction is done cell by cell; if it is a single number, that number is subtracted from all cells. The result is a new matrix.
a = matrixNew(2, 2, 5) b = matrixNew(2, 2, 2) d = matrixDiff(a, b) plot(matrixGet(d, 0, 0))
5-2 = 3 (every cell).
An array of eigenvalues sorted from largest to smallest; an empty array if it is not square or is empty.
Computed by the Jacobi method for symmetric matrices. Eigenvalues give the matrix's diagonal magnitudes (its scale along the principal axes).
m = matrixNew(2, 2, 0) matrixSet(m, 0, 0, 2) matrixSet(m, 1, 1, 3) ev = matrixEigenvalues(m) plot(arrayGet(ev, 0))
Diagonal 2,3 → largest eigenvalue 3.
A new matrix whose columns are the eigenvectors; an empty matrix if it is not square or is empty.
Computed by the Jacobi method for symmetric matrices; each column of the result matrix is an eigenvector, matching the order of matrixEigenvalues.
m = matrixNew(2, 2, 0) matrixSet(m, 0, 0, 2) matrixSet(m, 1, 1, 3) v = matrixEigenvectors(m) plot(matrixRows(v))
2×2 symmetric → eigenvector matrix has 2 rows.
The total cell count (rows × columns); 0 if it is not a matrix.
Useful to check whether a matrix is non-empty before feeding statistics functions (matrixAvg, matrixMedian).
m = matrixNew(2, 3, 0) plot(matrixElementsCount(m))
2×3 = 6 cells.
Returns nothing (na); it sets every cell of the matrix to the value in place.
Does not change the size, it just overwrites the contents with a single value. A quick way to reset a matrix before reusing it.
m = matrixNew(2, 2, 0) matrixFill(m, 3) plot(matrixSum(m))
All four cells become 3 → sum 12.
The cell value at row i, column j; returns na if the index is out of range (no error).
Rows and columns start at 0. An out-of-range index does not crash, it yields na; you can check the result with na().
m = matrixNew(2, 2, 0) matrixSet(m, 0, 1, 9) plot(matrixGet(m, 0, 1))
Reads the 9 written at row 0, column 1.
The inverse of the matrix (a new matrix); na if it is not square or is singular (has no inverse).
Computed by Gauss-Jordan elimination. Multiplying by the inverse yields the identity. A matrix with determinant 0 has no inverse; na is returned then.
m = matrixNew(2, 2, 0) matrixSet(m, 0, 0, 4) matrixSet(m, 1, 1, 4) inv = matrixInv(m) plot(matrixGet(inv, 0, 0))
Diagonal 4 → inverse diagonal 0.25.
true if it is square and every cell outside the anti-diagonal (i+j = n-1) is 0; otherwise false.
The anti-diagonal runs from top-right to bottom-left; this check verifies that only that diagonal may be filled.
m = matrixNew(2, 2, 0) matrixSet(m, 0, 1, 3) matrixSet(m, 1, 0, 4) plot(matrixIsAntidiagonal(m) ? 1 : 0)
Only the anti-diagonal is filled → 1 (yes).
true if it is square and each cell equals the negative of its mirror (m[i][j] = -m[j][i]); otherwise false.
In an antisymmetric matrix the diagonal is necessarily 0 (since m[i][i] = -m[i][i] holds only for 0).
m = matrixNew(2, 2, 0) matrixSet(m, 0, 1, 3) matrixSet(m, 1, 0, -3) plot(matrixIsAntisymmetric(m) ? 1 : 0)
Diagonal 0, mirrors opposite-signed → 1.
true if every cell is only 0 or 1, otherwise false.
Used to validate structures that should contain only 0-1, such as an adjacency matrix.
m = matrixNew(2, 2, 1) plot(matrixIsBinary(m) ? 1 : 0)
All cells 1 → 1 (yes).
true if it is square and every off-diagonal cell is 0; otherwise false.
The diagonal values may be any number; the condition is only that the off-diagonal is 0.
m = matrixNew(2, 2, 0) matrixSet(m, 0, 0, 5) matrixSet(m, 1, 1, 7) plot(matrixIsDiagonal(m) ? 1 : 0)
Only the diagonal is filled → 1 (yes).
true if it is square with a diagonal of 1 and off-diagonal cells of 0; otherwise false.
The identity is the neutral element in multiplication (m × identity = m). This check tells whether a matrix is the identity.
m = matrixNew(2, 2, 0) matrixSet(m, 0, 0, 1) matrixSet(m, 1, 1, 1) plot(matrixIsIdentity(m) ? 1 : 0)
Diagonal 1, rest 0 → 1 (yes).
true if the row count equals the column count, otherwise false.
Used as a guard before functions that require a square matrix (determinant, inverse, power, trace).
m = matrixNew(3, 3, 0) plot(matrixIsSquare(m) ? 1 : 0)
3×3 is square → 1 (yes).
true if every row sums to 1 (within a small rounding tolerance); otherwise false.
This is a row-stochastic check; being square is not required, only that each row sums to 1. Used to validate transition (Markov) matrices.
m = matrixNew(2, 2, 0.5) plot(matrixIsStochastic(m) ? 1 : 0)
Each row 0.5+0.5 = 1 → 1 (yes).
true if it is square and equals its transpose (m[i][j] = m[j][i]); otherwise false.
A symmetric matrix is a mirror across the diagonal. Covariance and correlation matrices are symmetric; the eigenvalue functions are designed for such matrices.
m = matrixNew(2, 2, 5) plot(matrixIsSymmetric(m) ? 1 : 0)
All cells equal → symmetric → 1.
true if it is square and either lower- or upper-triangular (one side of the diagonal is all 0); otherwise false.
In an upper-triangular matrix the area below the diagonal is zero, in a lower-triangular one the area above. Either case qualifies as triangular.
m = matrixNew(2, 2, 0) matrixSet(m, 0, 0, 1) matrixSet(m, 0, 1, 2) matrixSet(m, 1, 1, 3) plot(matrixIsTriangular(m) ? 1 : 0)
Below-diagonal is 0 → upper-triangular → 1.
true if every cell is 0, false if even one is non-zero.
Can be used to check whether a computation wrote any value into the matrix.
m = matrixNew(2, 2, 0) plot(matrixIsZero(m) ? 1 : 0)
All cells 0 → 1 (yes).
The Kronecker product of the two matrices; a new matrix of size (m1.rows×m2.rows) × (m1.cols×m2.cols).
Each cell of m1 is multiplied by all of m2 and placed block by block. The result grows to the product of both sizes.
a = matrixNew(2, 2, 1) b = matrixNew(2, 2, 1) k = matrixKron(a, b) plot(matrixRows(k))
2×2 ⊗ 2×2 → 4×4, row count 4.
The largest value across all cells; na if the matrix is empty or not a matrix.
Compares all cells without distinguishing rows or columns.
m = matrixNew(2, 2, 0) matrixSet(m, 1, 1, 8) plot(matrixMax(m))
One cell is 8 → maximum 8.
The median of all cells; if the cell count is even, the mean of the two middle values. na if empty.
All cells are sorted and the middle value is taken. The median is less sensitive to outliers than the mean.
m = matrixNew(2, 2, 0) matrixSet(m, 0, 0, 1) matrixSet(m, 0, 1, 2) matrixSet(m, 1, 0, 3) matrixSet(m, 1, 1, 4) plot(matrixMedian(m))
1,2,3,4 → mean of the two middle values is 2.5.
The smallest value across all cells; na if the matrix is empty or not a matrix.
Compares all cells without distinguishing rows or columns.
m = matrixNew(2, 2, 0) matrixSet(m, 0, 0, -5) plot(matrixMin(m))
One cell is -5 → minimum -5.
The most frequently repeated cell value; the smallest one if several share the top frequency. na if empty.
When several values share the highest frequency, the smallest value is chosen (a stable, deterministic result).
m = matrixNew(2, 2, 1) matrixSet(m, 0, 0, 5) plot(matrixMode(m))
Three 1s and one 5 → mode 1.
The a×b matrix product; a new matrix of size a.rows × b.cols.
This is true matrix multiplication (not element-wise): result[i][j] is the dot product of a's row i with b's column j.
a = matrixNew(2, 2, 0) matrixSet(a, 0, 0, 1) matrixSet(a, 1, 1, 1) b = matrixNew(2, 2, 5) p = matrixMult(a, b) plot(matrixGet(p, 0, 0))
Identity × b = b → [0,0] = 5.
matrixNew(satır, sütun, başlangıç?)
A new satır×sütun matrix with every cell set to the initial value.
A matrix is a two-dimensional grid of numbers made of rows and columns; every other matrix function expects an object built this way.
m = matrixNew(2, 3, 0) plot(matrixElementsCount(m))
A 2×3 matrix with 6 cells.
m = matrixNew(2, 2, 5) plot(matrixSum(m))
All four cells are 5 → sum 20.
The pseudo-inverse of the matrix (the Moore-Penrose left inverse); na if (mᵀm) cannot be inverted.
Computed as (mᵀm)⁻¹mᵀ; it gives the least-squares solution for non-square or non-invertible matrices. For a square invertible matrix the result equals the ordinary inverse.
m = matrixNew(2, 2, 0) matrixSet(m, 0, 0, 2) matrixSet(m, 1, 1, 4) p = matrixPinv(m) plot(matrixGet(p, 0, 0))
Diagonal 2 → pseudo-inverse diagonal 0.5.
The matrix raised to the n-th power (matrix×matrix multiplied n times); a new matrix.
Multiplies the matrix by itself n times (not an ordinary numeric power). At n=0 the identity, at n=1 the matrix itself is returned.
m = matrixNew(2, 2, 0) matrixSet(m, 0, 0, 2) matrixSet(m, 1, 1, 3) p = matrixPow(m, 2) plot(matrixGet(p, 0, 0))
Diagonal 2 → squared 4.
The rank of the matrix (the number of independent rows/columns); 0 if it is not a matrix.
Computed by elimination. If the rank is less than the row or column count, the matrix has dependency (redundancy) and no inverse.
m = matrixNew(2, 2, 5) plot(matrixRank(m))
All rows identical → rank 1.
An array of the removed column's values; an empty array if the index is out of range.
Deletes the column from the matrix (the column count drops by one) and returns that column as an array. Modifies in place.
m = matrixNew(2, 3, 0) matrixSet(m, 0, 1, 5) c = matrixRemoveCol(m, 1) plot(arrayGet(c, 0))
First value of the removed column 1 is 5.
An array of the removed row's values; an empty array if the index is out of range.
Deletes the row from the matrix (the row count drops by one) and returns that row as an array. Modifies in place.
m = matrixNew(3, 2, 0) matrixSet(m, 1, 0, 9) r = matrixRemoveRow(m, 1) plot(arrayGet(r, 0))
First value of the removed row 1 is 9.
matrixReshape(m, satır, sütun)
A new matrix that places the same cells into the new rows×columns layout.
Cells are read row-major and poured into the new shape. If the new size is larger, missing cells become 0; if smaller, extra cells are dropped. Does not change the source.
m = matrixNew(2, 3, 4) r = matrixReshape(m, 3, 2) plot(matrixRows(r))
2×3 (6 cells) → 3×2, row count 3.
Returns nothing (na); it reverses the row and column order in place.
Reverses both the order of rows and the cells within each row; the first cell swaps with the last (a 180° rotation effect). Modifies in place.
m = matrixNew(1, 3, 0) matrixSet(m, 0, 0, 1) matrixSet(m, 0, 2, 3) matrixReverse(m) plot(matrixGet(m, 0, 0))
[1,0,3] → reversed [3,0,1], first cell 3.
An array holding a copy of row i; an empty array if the index is out of range.
The returned array is a copy; modifying it does not affect the source matrix. An empty result reads as size 0 with arraySize.
m = matrixNew(2, 3, 4) r = matrixRow(m, 0) plot(arraySum(r))
Row 0 is [4,4,4] → sum 12.
The number of rows in the matrix; 0 if it is not a matrix.
Used to know the bound before looping over rows. For the column count use matrixCols.
m = matrixNew(3, 2, 0) plot(matrixRows(m))
3 rows.
matrixSet(matris, i, j, değer)
Returns nothing (na); it modifies the matrix in place.
Overwrites an existing cell; it does not grow the matrix. If the index is out of range it silently does nothing. To add a new row/column use matrixAddRow/matrixAddCol.
m = matrixNew(2, 2, 0) matrixSet(m, 1, 1, 7) plot(matrixGet(m, 1, 1))
The bottom-right cell is set to 7.
Returns nothing (na); it sorts the matrix rows in place.
Rows are reordered as whole units by the value in the chosen column (each row stays intact internally). Does nothing if the column index is invalid.
m = matrixNew(2, 1, 0) matrixSet(m, 0, 0, 5) matrixSet(m, 1, 0, 1) matrixSort(m, 0, true) plot(matrixGet(m, 0, 0))
Ascending sort → the smallest (1) comes first.
matrixSubmatrix(m, r1, r2, c1, c2)
A new matrix made of rows [r1, r2) and columns [c1, c2).
The end indices are exclusive (r2 and c2 are not included). Does not change the source; returns a copy of the sliced region.
m = matrixNew(3, 3, 0) matrixSet(m, 1, 1, 5) s = matrixSubmatrix(m, 1, 2, 1, 2) plot(matrixGet(s, 0, 0))
The single middle cell (5) is taken.
Sums all values without distinguishing rows or columns. Yields 0 for an empty matrix.
m = matrixNew(2, 2, 2.5) plot(matrixSum(m))
Four cells of 2.5 → sum 10.
Returns nothing (na); it swaps columns i and j in place.
Swaps two whole columns. Does nothing if either index is out of range.
m = matrixNew(1, 2, 0) matrixSet(m, 0, 0, 1) matrixSet(m, 0, 1, 2) matrixSwapColumns(m, 0, 1) plot(matrixGet(m, 0, 0))
Columns swapped → [0,0] is now 2.
Returns nothing (na); it swaps rows i and j in place.
Swaps two whole rows. Does nothing if either index is out of range.
m = matrixNew(2, 1, 0) matrixSet(m, 0, 0, 1) matrixSet(m, 1, 0, 2) matrixSwapRows(m, 0, 1) plot(matrixGet(m, 0, 0))
Rows swapped → [0,0] is now 2.
The sum of the main-diagonal (i==j) cells; 0 if it is not a matrix.
For a non-square matrix it sums the diagonal up to the shorter side. The trace reduces the diagonal energy to a single number.
m = matrixNew(3, 3, 0) matrixSet(m, 0, 0, 1) matrixSet(m, 1, 1, 2) matrixSet(m, 2, 2, 3) plot(matrixTrace(m))
Diagonal 1+2+3 = 6.
A new matrix with rows and columns swapped (of size cols×rows).
Does not change the source, it returns a new matrix. Cell at row i, column j becomes row j, column i in the output.
m = matrixNew(2, 3, 0) t = matrixTranspose(m) plot(matrixRows(t))
2×3 → transpose is 3×2, row count 3.