Builders

In this section, the user will find all the possible builders other than the constructors for the matrix<> object. For the latter, please refer to constructors list in the class matrix description. For example, eye will return the idendity matrix while ones will return a matrix filled with ones.

colon

template<typename U, typename V>
auto castor::colon(U j, V k)
template<typename U, typename V, typename W>
auto castor::colon(U j, V i, W k)

Colon operator (:).

colon(J,K) is the same as [J, J+1, …, J+m], where J+m <= K. In the case where both J and K are integers, this is simply [J, J+1, …, K]. This syntax returns an empty matrix if J > K.

matrix<int> I = colon(1,4);
disp(I);
matrix<> X = colon(0.5,4);
disp(X);

colon(J,I,K) is the same as [J, J+I, …, J+m*I], where J+m*I <= K. This syntax returns an empty matrix when I == 0, I > 0 and J > K, or I < 0 and J < K.

matrix<int> I = colon(1,2,9);
disp(I);
matrix<> X = colon(0.5,0.5,2.1);
disp(X);

See linspace.

diag

template<typename T>
matrix<T> castor::diag(matrix<T> const &A, long k = 0)

Diagonal matrices and diagonals of a matrix.

A = diag(V) is the same as A = diag(V,0) and puts V on the main diagonal.

matrix<> V = {0,1,2,3};
matrix<> A = diag(V);
disp(A);

diag(V,K) when V is a vector with N components is a square matrix of order N+ABS(K) with the elements of V on the K-th diagonal. K = 0 is the main diagonal, K > 0 is above the main diagonal and K < 0 is below the main diagonal.

matrix<> V  = {0,1,2,3};
matrix<> Al = diag(V,-1);
matrix<> Au = diag(V,2);
disp(Al);
disp(Au);

diag(A) is the main diagonal of A. diag(diag(A)) is a diagonal matrix.

matrix<> A = reshape(colon(1,9),3,3);
matrix<> V = diag(A);
disp(V);

diag(A,K) when A is a matrix is a row vector formed from the elements of the K-th diagonal of A.

matrix<> A  = reshape(colon(1,9),3,3);
matrix<> Vl = diag(A,-1);
matrix<> Vu = diag(A,2);
disp(Vl);
disp(Vu);

See eye.

eye

template<typename T = double>
matrix<T> castor::eye(matrix<std::size_t> const &S)
template<typename T = double>
matrix<T> castor::eye(std::size_t m, long n = -1)

Identity matrix.

eye(N) is the N-by-N identity matrix.

eye(M,N) or eye({M,N}) is an M-by-N matrix with 1’s on the diagonal and zeros elsewhere.

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

See ones, zeros, rand.

linspace

template<typename T = double, typename U, typename V>
matrix<T> castor::linspace(U x1, V x2, std::size_t n = 100)

Linearly spaced vector.

linspace(X1, X2) generates a row vector of 100 linearly equally spaced points between X1 and X2.

linspace(X1, X2, N) generates N points. For N=1, linspace returns X2.

matrix<> A = linspace(1,2);
matrix<> B = linspace(1,2,5);
disp(A);
disp(B);

See logspace, colon.

logspace

template<typename T = double, typename U, typename V>
matrix<T> castor::logspace(U x1, V x2, std::size_t n = 50)

Logarithmically spaced vector.

logspace(X1, X2) generates a row vector of 50 logarithmically equally spaced points between decades 10^X1 and 10^X2.

logspace(X1, X2, N) generates N points. For N=1, logspace returns 10^X2.

matrix<> A = logspace(0,3);
matrix<> B = logspace(0,3,4);
disp(A);
disp(B);

See linspace, colon, log.

rand

template<typename T = double>
matrix<T> castor::rand(matrix<std::size_t> const &S, bool seed = false)
template<typename T = double>
matrix<T> castor::rand(std::size_t m, long n = -1, bool seed = false)

matrix of uniformly distributed pseudorandom numbers.

rand(N) returns an N-by-N matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval (0,1).

rand(M,N) or rand({M,N}) returns an M-by-N matrix.

rand(M,N,true) use standard time to initialize seed.

matrix<> A = rand(2);
matrix<> B = rand(2,3);
matrix<> C = rand(size(B));
matrix<> D = rand(2,3,true);
disp(A);
disp(B);
disp(C);
disp(D);

See zeros, eye, ones.

ones

template<typename T = double>
matrix<T> castor::ones(matrix<std::size_t> const &S)
template<typename T = double>
matrix<T> castor::ones(std::size_t m, long n = -1)

Ones matrix.

ones(N) is an N-by-N matrix of ones.

ones(M,N) or ones({M,N}) is an M-by-N matrix of ones.

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

See zeros, eye, rand.

zeros

template<typename T = double>
matrix<T> castor::zeros(matrix<std::size_t> const &S)
template<typename T = double>
matrix<T> castor::zeros(std::size_t m, long n = -1)

Zeros matrix.

zeros(N) is an N-by-N matrix of zeros.

zeros(M,N) or zeros({M,N}) is an M-by-N matrix of zeros.

matrix<> A = zeros(2);
matrix<> B = zeros(2,3);
matrix<> C = zeros(size(B));
disp(A);
disp(B);
disp(C);

See ones, eye, rand.