Skip to content

Matrix Math

ikaros-project edited this page Aug 3, 2024 · 15 revisions

This page describes some of the basic mathematical functions that are included in the ikaros matrix class. For basic matrix operations see Matrices

Element-wise functions with scalar

Each element of a matrix is modified with the constant c.

matrix & add(float c);
matrix & subtract(float c);
matrix & scale(float c);
matrix & divide(float c);

For example, to add 5 to each element of a matrix:

matrix m = {{1, 2}, {3, 4}};
m.add(5);

Element-wise functions with two matrices

The following functions modifies the value of each element with a value from another matrix. Both matrices must have the same dimensions.

matrix & add(matrix A);
matrix & subtract(matrix A);
matrix & multiply(matrix A);
matrix & divide(matrix A);

For example, here the matrix B is added to matrix A:

matrix A = {{1, 2}, {3, 4}};
matrix B = {{0, -1}, {7, 2}};
A.add(B);

Similarly, two matrices can be combined into a third matrix:

matrix A = {{1, 2}, {3, 4}};
matrix B = {{0, -1}, {7, 2}};
matrix C;
C.add(A, B);

In this case, C must be either empty or have the same dimensionality as A and B.

Matrix reduce functions

These are functions that operate on all the elements of a matrix and produce a scalar result:

float sum();
float product();
float min();
float max();
float average();
float median();

// Example use

matrix m = {1, 2, 3};
float s  = m.average();

Matrix multiplication

Two matrices can be multiplied with the matmul function:

C.matmul(A,B); Here, C must be either empty or have the correct size for the resulting multiplication.

Correlation and Convolution

Correlation and convolution have similar interfaces and both applies a kernel matix to an image. Note that for correlation the indices order is the same for image and kernel, but for actual convolution the indexing of the kernel is reversed. (In the image processing literature, correlation is often called convolution although this is not technically correct). Make sure you use the correct function.

Given an image I and a kernel K and a result matrix R, the functions operates as in the example below:

R.corr(I,K);
R.conv(I,K);

Clone this wiki locally