Algorithms

Standard algorithms over data stored in matrices may be found here. Among many others, it is possible to sort a matrix, find the unique elements, the median of the data, perform the dot product between two matrices, find the maximum value with max, etc.

argintersect

template<typename R, typename S>
auto castor::argintersect(matrix<R> const &A, matrix<S> const &B)

Index of intersection.

[IA,IB] = intersect(A,B) returns index vectors IA and IB such that C = A(IA) and C = B(IB). If there are repeated common values in A or B then the index of the first occurrence of each repeated value is returned.

matrix<> A = {1,2,3};
matrix<> B = {2,3,4};
matrix<std::size_t> IA, IB;
std::tie(IA,IB) = argintersect(A,B);
disp(eval(A(IA)));
disp(eval(B(IB)));

See intersect, argsetdiff, argunique.

argmax

template<typename T>
inline std::size_t castor::argmax(matrix<T> const &A)
template<typename T>
matrix<std::size_t> castor::argmax(matrix<T> const &A, int dim)

Index of max.

I = argmax(A) returns the index corresponding to the maximum value.

matrix<>    A = {{1,2,3},{4,5,6}};
std::size_t I = argmax(A);
disp(I);

I = argmax(A,DIM) returns a vector of indices corresponding to the maximum values into operating dimension.

matrix<>            A = {{1,2,3},{4,5,6}};
matrix<std::size_t> I = argmax(A,1);
matrix<std::size_t> J = argmax(A,2);
disp(I);
disp(J);

See max, maximum, argmin, argsort.

argmin

template<typename T>
inline std::size_t castor::argmin(matrix<T> const &A)
template<typename T>
matrix<std::size_t> castor::argmin(matrix<T> const &A, int dim)

Index of min.

I = argmin(A) returns the index corresponding to the minimum value.

matrix<>    A = {{1,2,3},{4,5,6}};
std::size_t I = argmin(A);
disp(I);

I = argmin(A,DIM) returns a vector of indices corresponding to the minimum values into operating dimension.

matrix<>            A = {{1,2,3},{4,5,6}};
matrix<std::size_t> I = argmin(A,1);
matrix<std::size_t> J = argmin(A,2);
disp(I);
disp(J);

See min, minimum, argmax, argsort.

argsetdiff

template<typename R, typename S>
matrix<std::size_t> castor::argsetdiff(matrix<R> const &A, matrix<S> const &B)

Index of difference.

IA = argsetdiff(A,B) returns index vectors IA such that C = A(IA). If there are repeated values in A that are not in B, then the index of the first occurrence of each repeated value is returned

matrix<> A = {1,2,3,0};
matrix<> B = {2,3,4};
matrix<std::size_t> IA, IB;
IA = argsetdiff(A,B);
disp(eval(A(IA)));

See setdiff, argintersect, argunique.

argsort

template<typename T>
matrix<std::size_t> castor::argsort(matrix<T> const &A, int dim = 0)

Index of sort.

I = argsort(A) returns a sort index I which specifies how the elements of A were rearranged to obtain the sorted output B = sort(A).

matrix<>            A = {{6,5,4},{3,2,1}};
matrix<std::size_t> I = argsort(A);
disp(I);

I = argsort(A,DIM) returns a sort index I which specifies how the elements of A were rearranged to obtain the sorted output B = sort(A,DIM) into operating dimension.

matrix<>            A = {{6,5,4},{3,2,1}};
matrix<std::size_t> I = argsort(A,1);
matrix<std::size_t> J = argsort(A,2);
disp(I);
disp(J);

The sort ordering is stable. Namely, when more than one element has the same value, the order of the equal elements is preserved in the sorted output B and the indices I relating to equal elements are ascending.

See sort, argmin, argmax.

argunique

template<typename T>
auto castor::argunique(matrix<T> const &A)

Index of unique.

[IA,IB] = unique(A) returns index vectors IA and IB such that B = A(IA) and A = B(IB).

matrix<> A = {1,2,1,3,2,3};
matrix<> B = unique(A);
matrix<std::size_t> IA, IB;
std::tie(IA,IB) = argunique(A);
disp(eval(A(IA)));
disp(eval(B(IB)));

See unique, argsetdiff, argintersect.

conv

template<typename T>
matrix<T> castor::conv(matrix<T> const &A, matrix<T> const &B, int dim = 0)

Convolution product and polynomial multiplication.

C = conv(A,B) convolves array A and B. The resulting array is vector of length NUMEL(A)+NUMEL(B)-1. If A and B are vectors of polynomial coefficients, convolving them is equivalent to multiplying the two polynomials.

C = conv(A,B,DIM) convolve along dimension DIM if A and B have compatible size.

matrix<float> A = eye(3,4);
matrix<float> B = rand(3,10);
disp(conv(A,B,2));

See fftconv.

cross

template<typename R, typename S>
auto castor::cross(matrix<R> const &A, matrix<S> const &B)

Matrix cross product.

C = cross(A,B) returns the cross product of the matrix A and B, C=AxB. A and B must have same number of row with 3 columns.

matrix<> A = {{1,0,0},{0,1,0},{0,0,1}};
matrix<> B = {{0,1,0},{0,0,1},{1,0,0}};
matrix<> C = cross(A,B);
disp(C);

See dot.

cumprod

template<typename T>
inline matrix<T> castor::cumprod(matrix<T> const &A)
template<typename T>
matrix<T> castor::cumprod(matrix<T> const &A, int dim)

Cumulative product of elements.

B = cumprod(A) computes the cumulative product of all elements of A. B is the same size as A.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> B = cumprod(A);
disp(B);

B = cumprod(A,DIM) computes the cumulative product along the dimension DIM. if DIM==0, B is the cumulative product. if DIM==1, B is a vector containing the cumulative product from each column. if DIM==2, B is a vector containing the cumulative product from each row.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> R = cumprod(A,1);
matrix<> C = cumprod(A,2);
disp(R);
disp(C);

See cumsum, prod.

cumsum

template<typename T>
inline matrix<T> castor::cumsum(matrix<T> const &A)
template<typename T>
matrix<T> castor::cumsum(matrix<T> const &A, int dim)

Cumulative sum of elements.

B = cumsum(A) computes the cumulative sum of all elements of A. B is the same size as A.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> B = cumsum(A);
disp(B);

B = cumsum(A,DIM) computes the cumulative sum along the dimension DIM. if DIM==0, B is the cumulative sum. if DIM==1, B is a vector containing the cumulative sum from each column. if DIM==2, B is a vector containing the cumulative sum from each row.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> R = cumsum(A,1);
matrix<> C = cumsum(A,2);
disp(R);
disp(C);

See cumprod, sum.

diff

template<typename T>
inline matrix<T> castor::diff(matrix<T> const &A)
template<typename T>
matrix<T> castor::diff(matrix<T> const &A, int dim)

Difference of elements.

B = diff(A) computes the difference of all elements of A. B is a row vector containing numel(A)-1 elements.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> B = diff(A);
disp(B);

B = cumsum(A,DIM) computes the cumulative sum along the dimension DIM. if DIM==0, B is the difference of all elements. if DIM==1, B is a vector containing the difference from each column. if DIM==2, B is a vector containing the difference from each row.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> R = diff(A,1);
matrix<> C = diff(A,2);
disp(R);
disp(C);

See sum, prod.

dot

template<typename R, typename S>
auto castor::dot(matrix<R> const &A, matrix<S> const &B)

Matrix dot product.

C = dot(A,B) returns the dot product of the matrix A and B, C = A.B. A and B must have same number of rows with 3 columns.

matrix<> A = {{1,0,0},{0,1,0},{0,0,1}};
matrix<> B = {{0,1,0},{0,0,1},{1,0,0}};
matrix<> C = dot(A,B);
disp(C);

See cross.

fftconv

template<typename S>
auto castor::fftconv(matrix<S> const &A, matrix<S> const &B, int dim = 0)

Convolution product and polynomial multiplication using FFT.

C = fftconv(A,B) convolves array A and B using Fast Fourier Transform that guarantee efficient execution. The resulting array is vector of length NUMEL(A)+NUMEL(B)-1. If A and B are vectors of polynomial coefficients, convolving them is equivalent to multiplying the two polynomials.

C = fftconv(A,B,DIM) convolve along dimension DIM if A and B have compatible size.

matrix<float> A = eye(3,4);
matrix<float> B = rand(3,10);
disp(fftconv(A,B,2);

See conv, fft.

gmres

Warning

doxygenfunction: Unable to resolve function “gmres” with arguments (matrix<T> const&, matrix<T> const&, double, std::size_t, std::function<matrix<T>(matrix<T> const&)> const&, matrix<T> const&) in doxygen xml output for project “castor” from directory: ../xml. Potential matches:

- template<typename T> matrix<T> gmres(hmatrix<T> const &Ah, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, std::function<matrix<T>(matrix<T> const&)> const &Am1 = std::function<matrix<T>(matrix<T> const&)>(), matrix<T> const &X0 = matrix<T>())
- template<typename T> matrix<T> gmres(hmatrix<T> const &Ah, matrix<T> const &B, double tol, std::size_t maxit, hmatrix<T> const &Ahm1, matrix<T> const &X0 = matrix<T>())
- template<typename T> matrix<T> gmres(hmatrix<T> const &Ah, matrix<T> const &B, double tol, std::size_t maxit, hmatrix<T> const &Lh, hmatrix<T> const &Uh, matrix<T> const &X0 = matrix<T>())
- template<typename T> matrix<T> gmres(matrix<T> const &A, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, std::function<matrix<T>(matrix<T> const&)> const &Am1 = std::function<matrix<T>(matrix<T> const&)>(), matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(matrix<T> const &A, matrix<T> const &B, double tol, std::size_t maxit, matrix<T> const &Am1, matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(smatrix<T> const &As, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, std::function<matrix<T>(matrix<T> const&)> const &Am1 = std::function<matrix<T>(matrix<T> const&)>(), matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(smatrix<T> const &As, matrix<T> const &B, double tol, std::size_t maxit, smatrix<T> const &Asm1, matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(std::function<matrix<T>(matrix<T> const&)> const &A, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, matrix<T> const &Am1 = matrix<T>(), matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(std::function<matrix<T>(matrix<T> const&)> const &A, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, std::function<matrix<T>(matrix<T> const&)> const &Am1 = std::function<matrix<T>(matrix<T> const&)>(), matrix<T> const &X0 = matrix<T>(), int info = 1)

Warning

doxygenfunction: Unable to resolve function “gmres” with arguments (matrix<T> const&, matrix<T> const&, double, std::size_t, matrix<T> const&, matrix<T> const&) in doxygen xml output for project “castor” from directory: ../xml. Potential matches:

- template<typename T> matrix<T> gmres(hmatrix<T> const &Ah, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, std::function<matrix<T>(matrix<T> const&)> const &Am1 = std::function<matrix<T>(matrix<T> const&)>(), matrix<T> const &X0 = matrix<T>())
- template<typename T> matrix<T> gmres(hmatrix<T> const &Ah, matrix<T> const &B, double tol, std::size_t maxit, hmatrix<T> const &Ahm1, matrix<T> const &X0 = matrix<T>())
- template<typename T> matrix<T> gmres(hmatrix<T> const &Ah, matrix<T> const &B, double tol, std::size_t maxit, hmatrix<T> const &Lh, hmatrix<T> const &Uh, matrix<T> const &X0 = matrix<T>())
- template<typename T> matrix<T> gmres(matrix<T> const &A, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, std::function<matrix<T>(matrix<T> const&)> const &Am1 = std::function<matrix<T>(matrix<T> const&)>(), matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(matrix<T> const &A, matrix<T> const &B, double tol, std::size_t maxit, matrix<T> const &Am1, matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(smatrix<T> const &As, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, std::function<matrix<T>(matrix<T> const&)> const &Am1 = std::function<matrix<T>(matrix<T> const&)>(), matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(smatrix<T> const &As, matrix<T> const &B, double tol, std::size_t maxit, smatrix<T> const &Asm1, matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(std::function<matrix<T>(matrix<T> const&)> const &A, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, matrix<T> const &Am1 = matrix<T>(), matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(std::function<matrix<T>(matrix<T> const&)> const &A, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, std::function<matrix<T>(matrix<T> const&)> const &Am1 = std::function<matrix<T>(matrix<T> const&)>(), matrix<T> const &X0 = matrix<T>(), int info = 1)

Warning

doxygenfunction: Unable to resolve function “gmres” with arguments (std::function<matrix<T>(matrix<T> const&)> const&, matrix<T> const&, double, std::size_t, std::function<matrix<T>(matrix<T> const&)> const&, matrix<T> const&) in doxygen xml output for project “castor” from directory: ../xml. Potential matches:

- template<typename T> matrix<T> gmres(hmatrix<T> const &Ah, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, std::function<matrix<T>(matrix<T> const&)> const &Am1 = std::function<matrix<T>(matrix<T> const&)>(), matrix<T> const &X0 = matrix<T>())
- template<typename T> matrix<T> gmres(hmatrix<T> const &Ah, matrix<T> const &B, double tol, std::size_t maxit, hmatrix<T> const &Ahm1, matrix<T> const &X0 = matrix<T>())
- template<typename T> matrix<T> gmres(hmatrix<T> const &Ah, matrix<T> const &B, double tol, std::size_t maxit, hmatrix<T> const &Lh, hmatrix<T> const &Uh, matrix<T> const &X0 = matrix<T>())
- template<typename T> matrix<T> gmres(matrix<T> const &A, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, std::function<matrix<T>(matrix<T> const&)> const &Am1 = std::function<matrix<T>(matrix<T> const&)>(), matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(matrix<T> const &A, matrix<T> const &B, double tol, std::size_t maxit, matrix<T> const &Am1, matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(smatrix<T> const &As, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, std::function<matrix<T>(matrix<T> const&)> const &Am1 = std::function<matrix<T>(matrix<T> const&)>(), matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(smatrix<T> const &As, matrix<T> const &B, double tol, std::size_t maxit, smatrix<T> const &Asm1, matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(std::function<matrix<T>(matrix<T> const&)> const &A, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, matrix<T> const &Am1 = matrix<T>(), matrix<T> const &X0 = matrix<T>(), int info = 1)
- template<typename T> matrix<T> gmres(std::function<matrix<T>(matrix<T> const&)> const &A, matrix<T> const &B, double tol = 1e-6, std::size_t maxit = 10, std::function<matrix<T>(matrix<T> const&)> const &Am1 = std::function<matrix<T>(matrix<T> const&)>(), matrix<T> const &X0 = matrix<T>(), int info = 1)

See linsolve.

intersect

template<typename R, typename S>
auto castor::intersect(matrix<R> const &A, matrix<S> const &B)

Set intersection of two arrays. C = intersect(A,B) for matrix A and B returns a vector with the values common to the two matrix, with no repetitions. C will be sorted.

matrix<> A = {1,2,3};
matrix<> B = {2,3,4};
matrix<> C = intersect(A,B);
disp(C);

See argintersect, setdiff, unique, union2.

kron

template<typename R, typename S>
inline auto castor::kron(R A, matrix<S> const &B)
template<typename R, typename S>
inline auto castor::kron(matrix<R> const &A, S B)
template<typename R, typename S>
auto castor::kron(matrix<R> const &A, matrix<S> const &B)

Kronecker tensor product.

kron(A,B) is the Kronecker tensor product of A and B. The result is a large matrix formed by taking all possible products between the elements of A and those of B. For example, if A is 2 by 3, then kron(A,B) is [ A(1,1)*B A(1,2)*B A(1,3)*B A(2,1)*B A(2,2)*B A(2,3)*B ]

matrix<> A = eye(2,3);
matrix<> B = ones(3,2);
matrix<> C = kron(A,B);
disp(C);

See mtimes.

max

template<typename T>
inline T castor::max(matrix<T> const &A)
template<typename T>
matrix<T> castor::max(matrix<T> const &A, int dim)

Maximum elements of an array.

M = max(A) is the largest element in the array A.

matrix<> A = {{1,2,3},{4,5,6}};
double   m = max(A);
disp(m);

M = max(A,DIM) operates along the dimension DIM: if DIM==0, M is the maximum element of the array. if DIM==1, M is a vector containing the maximum element from each column. if DIM==2, M is a vector containing the maximum element from each row.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> R = max(A,1);
matrix<> C = max(A,2);
disp(R);
disp(C);

See argmax, maximum, min, sort.

min

template<typename T>
T castor::min(matrix<T> const &A)
template<typename T>
matrix<T> castor::min(matrix<T> const &A, int dim)

Minimum elements of an array.

M = min(A) is the smallest element in the array A.

matrix<> A = {{1,2,3},{4,5,6}};
double   m = min(A);
disp(m);

M = min(A,DIM) operates along the dimension DIM: if DIM==0, M is the minimum element. if DIM==1, M is a vector containing the minimum element from each column. if DIM==2, M is a vector containing the minimum element from each row.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> R = min(A,1);
matrix<> C = min(A,2);
disp(R);
disp(C);

See argmin, minimum, max, sort.

maximum

template<typename R, typename S>
inline auto castor::maximum(R A, matrix<S> const &B)
template<typename R, typename S>
inline auto castor::maximum(matrix<R> const &A, S B)
template<typename R, typename S>
auto castor::maximum(matrix<R> const &A, matrix<S> const &B)

Maximum elements between two arrays.

C = maximum(A,B) returns an array with the largest elements taken from A or B. A and B must have compatible sizes. In the simplest cases, they can be the same size or one can be a scalar. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the inputs are the same.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> B = {{6,5,4},{3,2,1}};
matrix<> C = maximum(A,B);
matrix<> D = maximum(A,3);
disp(C);
disp(D);

See minimum, max, min, sort.

mean

template<typename T>
inline T castor::mean(matrix<T> const &A)
template<typename T>
matrix<T> castor::mean(matrix<T> const &A, int dim)

Average or mean value.

M = mean(A) is the mean value of all the elements in the array A.

matrix<> A = {{1,2,3},{4,5,6}};
double   m = mean(A);
disp(m);

M = mean(A,DIM) takes the mean along the dimension DIM of A. if DIM==0, M is the mean value of the array. if DIM==1, M is a vector containing the mean value from each column. if DIM==2, M is a vector containing the mean value from each row.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> R = mean(A,1);
matrix<> C = mean(A,2);
disp(R);
disp(C);

See max, min, median, variance, stddev.

median

template<typename T>
inline T castor::median(matrix<T> const &A)
template<typename T>
matrix<T> castor::median(matrix<T> const &A, int dim)

Median value.

M = median(A) is the median value of the elements in A.

matrix<> A = {{1,2,3},{4,5,6}};
double   m = median(A);
disp(m);

M = median(A,DIM) takes the median along the dimension DIM of A. if DIM==0, M is the mediane value of the array. if DIM==1, M is a vector containing the mediane value from each column. if DIM==2, M is a vector containing the mediane value from each row.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> R = median(A,1);
matrix<> C = median(A,2);
disp(R);
disp(C);

See max, min, variance, stddev.

minimum

template<typename R, typename S>
inline auto castor::minimum(R A, matrix<S> const &B)
template<typename R, typename S>
inline auto castor::minimum(matrix<R> const &A, S B)
template<typename R, typename S>
auto castor::minimum(matrix<R> const &A, matrix<S> const &B)

Minimum elements between two arrays.

C = minimum(A,B) returns an array with the smallest elements taken from A or B. A and B must have compatible sizes. In the simplest cases, they can be the same size or one can be a scalar. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the inputs are the same.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> B = {{6,5,4},{3,2,1}};
matrix<> C = minimum(A,B);
matrix<> D = minimum(A,3);
disp(C);
disp(D);

See maximum, min, max sort.

mtimes

template<typename R, typename S>
inline auto castor::mtimes(R A, matrix<S> const &B)
template<typename R, typename S>
inline auto castor::mtimes(matrix<R> const &A, S B)
template<typename R, typename S>
auto castor::mtimes(matrix<R> const &A, matrix<S> const &B)

Matrix multiply.

mtimes(A,B) is the matrix product of A and B, the number of columns of A must equal the number of rows of B.

matrix<> A = ones(3,1);
matrix<> B = ones(1,2);
matrix<> C = mtimes(A,B);
disp(C);

See tgemm, kron.

norm

template<typename S>
auto castor::norm(matrix<S> const &A, std::string typ = "2")
template<typename S>
auto castor::norm(matrix<S> const &A, std::string typ, int dim)

Vectorial norm applied to all values of a matrix. Vectorial norms are not matrix norm (e.g. Frobenius, SVD, etc.), but norms applied to all element of a matrix, using linear indexing.

norm(X) is the euclidian norm.

matrix<> A   = eye(3,4);
double   nrm = norm(A);
disp(nrm);

norm(X,TYP) is the TYP-norm of X : TYP=1 returns the 1-norm of X, TYP=2 returns the 2-norm of X, TYP=”inf” returns the infinite norm of X.

matrix<> A    = eye(3,4);
double   nrm1 = norm(A,"1");
double   nrm2 = norm(A,"2");
double   nrmI = norm(A,"inf");
disp(nrm1);
disp(nrm2);
disp(nrmI);

norm(X,TYP,DIM) returns the TYP-norm of X along the dimension DIM.

matrix<> A = eye(3,4);
matrix<> R = norm(A,"inf",1);
matrix<> C = norm(A,"inf",2);
disp(R);
disp(C);

See max, sum.

prod

template<typename T>
inline T castor::prod(matrix<T> const &A)
template<typename T>
matrix<T> castor::prod(matrix<T> const &A, int dim)

Product of elements.

P = prod(A) is the product of all the elements of the array A.

matrix<> A = {{1,2,3},{4,5,6}};
double   p = prod(A);
disp(p);

prod(A,DIM) operates along the dimension DIM: if DIM==0, M is the product of all elements. if DIM==1, M is a vector containing the prpduct from each column. if DIM==2, M is a vector containing the product from each row.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> R = prod(A,1);
matrix<> C = prod(A,2);
disp(R);
disp(C);

See sum, diff.

setdiff

template<typename R, typename S>
auto castor::setdiff(matrix<R> const &A, matrix<S> const &B)

Set difference of two arrays. C = setdiff(A,B) for matrix A and B returns a vector with the values in A that are not in B, with no repetitions. C will be sorted.

matrix<> A = {1,2,3};
matrix<> B = {2,3,4};
matrix<> C = setdiff(A,B);
disp(C);

See argsetdiff, intersect, unique, union2.

sort

template<typename T>
matrix<T> castor::sort(matrix<T> const &A, int dim = 0)

Sort in ascending order.

B = sort(A) sorts the elements of A in ascending order. The sorted output B has the same type and size as A.

matrix<> A = {{6,5,4},{3,2,1}};
matrix<> B = sort(A);
disp(B);

B = sort(A,DIM) also specifies a dimension DIM to sort along. if DIM==0, M is the sorted element of the array. if DIM==1, M is a vector containing the sorted elements from each column. if DIM==2, M is a vector containing the sorted elements from each row.

matrix<> A = {{6,5,4},{3,2,1}};
matrix<> R = sort(A,1);
matrix<> C = sort(A,2);
disp(R);
disp(C);

See argsort, min, max.

stddev

template<typename T>
inline T castor::stddev(matrix<T> const &A)
template<typename T>
matrix<T> castor::stddev(matrix<T> const &A, int dim)

Standard deviation of elements.

S = stddev(A) is the standard deviation of all elements of the array A.

matrix<> A = {{1,2,3},{4,5,6}};
double   m = stddev(A);
disp(m);

S = stddev(A,DIM) is the standard deviation along the dimension DIM. if DIM==0, M is the standard deviation of the array. if DIM==1, M is a vector containing the standard deviation from each column. if DIM==2, M is a vector containing the standard deviation from each row.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> R = stddev(A,1);
matrix<> C = stddev(A,2);
disp(R);
disp(C);

See max, min, mean, median, variance.

sum

template<typename T>
inline T castor::sum(matrix<T> const &A)
template<typename T>
matrix<T> castor::sum(matrix<T> const &A, int dim)

Sum of elements.

S = sum(A) is the sum of all the elements of the array A.

matrix<> A = {{1,2,3},{4,5,6}};
double   s = sum(A);
disp(s);

S = sum(A,DIM) sums along the dimension DIM. if DIM==0, M is the sum of all element of the array. if DIM==1, M is a vector containing the sum from each column. if DIM==2, M is a vector containing the sum from each row.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> R = sum(A,1);
matrix<> C = sum(A,2);
disp(R);
disp(C);

See prod, diff, cumsum.

tgemm

template<typename P, typename Q, typename R, typename S, typename T>
void castor::tgemm(P alpha, matrix<Q> const &A, matrix<R> const &B, S beta, matrix<T> &C)

In-place matrix product.

tgemm(alpha,A,B,beta,C) performs the in-place matrix-matrix operations C = alpha*A*B + beta*C, where alpha, beta are scalars and A, B, C are matrices with compatible size.

NOTE : This is a naive implementation, you can use the cblas interface proposed in “linalg.hpp” to get better performance.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> B = eye(3,3);
matrix<> C = zeros(2,3);
tgemm(1,A,B,1,C);
disp(C);

See mtimes, tgemm.

union2

template<typename R, typename S>
auto castor::union2(matrix<R> const &A, matrix<S> const &B)

Set union of two arrays. C = union2(A,B) for matrix A and B, returns a vector with the combined values of the two matrix, with no repetitions. C will be sorted.

matrix<> A = {1,2,3};
matrix<> B = {2,3,4};
matrix<> C = union2(A,B);
disp(C);

See intersect, setdiff, unique.

unique

template<typename T>
matrix<T> castor::unique(matrix<T> const &A)

Set unique of an array. B = unique(A) for the matrix A returns a vector with the same values as in A, but with no repetitions. B will be sorted.

matrix<> A = {1,2,1,3,2,3};
matrix<> B = unique(A);
disp(B);

See argunique, intersect, setdiff, union2.

variance

template<typename T>
inline T castor::variance(matrix<T> const &A)
template<typename T>
matrix<T> castor::variance(matrix<T> const &A, int dim)

Variance of elements.

S = variance(X) is the variance of all elements of the array X. If N is the sample size, variance normalizes by N and produces the second moment of the sample about its mean.

matrix<> A = {{1,2,3},{4,5,6}};
double   v = variance(A);
disp(v);

S = variance(X,DIM) is the variance along the dimension DIM: if DIM==0, M is the variance of the array. if DIM==1, M is a vector containing the variance from each column. if DIM==2, M is a vector containing the variance from each row.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> R = variance(A,1);
matrix<> C = variance(A,2);
disp(R);
disp(C);

See max, min, median, mean, stddev.