Matrix summary

Below, Element refers to one of the following:

Correspondingly, Matrix refers to one of the following:


Defining matrices:

// not initialized Matrix(const int rows, const int cols); // init to scalar Matrix(const int rows, const int cols, const Element x) // init with array Matrix(const int rows, const int cols, const Element **d) // init with function Matrix(const int rows, const int cols, Element (*f)(const int r, const int c)) // init with another matrix Matrix(const Matrix& V);


Elementwise operations:

For two matrices A and B of the same size, and a scalar x, A = B; A = x; A += B; // not for booleans A += x; // not for booleans A -= B; // not for booleans A -= x; // not for booleans A *= B; // not for booleans A *= x; // not for booleans A /= B; // not for booleans A /= x; // not for booleans A %= B; // only for integers A %= x; // only for integers A++ // not for booleans ++A // not for booleans A-- // not for booleans --A // not for booleans +A // not for booleans -A // not for booleans A + B // not for booleans A + x // not for booleans x + A // not for booleans A - B // not for booleans A - x // not for booleans x - A // not for booleans A * B // not for booleans A * x // not for booleans x * A // not for booleans A / B // not for booleans A / x // not for booleans x / A // not for booleans A % B // only for integers A % x // only for integers x % A // only for integers A < B // not for booleans; boolean result A < x // not for booleans; boolean result x < A // not for booleans; boolean result A <= B // not for booleans; boolean result A <= x // not for booleans; boolean result x <= A // not for booleans; boolean result A == B // not for booleans; boolean result A == x // not for booleans; boolean result x == A // not for booleans; boolean result A != B // not for booleans; boolean result A != x // not for booleans; boolean result x != A // not for booleans; boolean result A > B // not for booleans; boolean result A > x // not for booleans; boolean result x > A // not for booleans; boolean result A >= B // not for booleans; boolean result A >= x // not for booleans; boolean result x >= A // not for booleans; boolean result A && B // only for booleans; boolean result A && x // only for booleans; boolean result x && A // only for booleans; boolean result A || B // only for booleans; boolean result A || x // only for booleans; boolean result x || A // only for booleans; boolean result !A // only for booleans; boolean result apply(Element (*f)(const Element), const Matrix& V), that is, apply(function_name, A) Only active elements are included.


Subscripting and slices:

For a matrix A and a scalar x of the same type, and integers i and j, x = A[i][j]; A[i][j] = x; This is not affected by the context or activity of A[i].

For a matrix A(N,M), vectors B(N) and C(M) of the same type, and integers i and j,

B = A[_][j]; // a column slice C = A[i][_]; // a row slice Slices can be used anywhere a vector can be used; in particular, slices may be assigned. Note the subtlety about slices in a context.


Reductions:

For a matrix A (of N by M elements), a scalar x of the same type, an integer n, and a boolean b, x = sum(A); // x = A[0][0] + A[0][1] + ... + A[N-1][M-1] b = any(A); // b = true iff ANY of A[i][j] != 0 for i in 0..N-1 and j in 0..M-1 b = all(A); // b = true iff ALL of A[i][j] != 0 for i in 0..N-1 and j in 0..M-1 n = n_nonzeros(A); // n = number of nonzero elements in A n = n_active(A); // n = number of active elements in A x = max_value(A); // x = max of A[i][j] for i in 0..N-1 and j in 0..M-1 x = min_value(A); // x = min of A[i][j] for i in 0..N-1 and j in 0..M-1 Only active elements are included.


Scans:

For two matrices A and B of the same size and type, A = plus_scan_rows(B); // A[i][j] = B[i][0] + B[i][1] + ... + B[i][j-1] A = plus_scan_cols(B); // A[i][j] = B[0][j] + B[1][j] + ... + B[i-1][j] A = max_scan_rows(B); // A[i][j] = max of B[i][k], for k in 0..j-1 A = max_scan_cols(B); // A[i][j] = max of B[k][j], for k in 0..i-1 A = min_scan_rows(B); // A[i][j] = min of B[i][k], for k in 0..j-1 A = min_scan_cols(B); // A[i][j] = min of B[k][j], for k in 0..i-1 Only active elements are involved.

For two boolean matrices C and D of the same size,

C = or_scan_rows(D); // C[i][j] = D[i][0] || D[i][1] || ... || D[i][j-1] C = or_scan_cols(D); // C[i][j] = D[0][j] || D[1][j] || ... || D[i-1][j] C = and_scan_rows(D); // C[i][j] = D[i][0] && D[i][1] && ... && D[i][j-1] C = and_scan_cols(D); // C[i][j] = D[0][j] && D[1][j] && ... && D[i-1][j] Only active elements are involved.


Shift and rotate:

For two matrices A and B of the same size and type, and an integer matrix P, A = shift(B, 3, 4); // shift each element 3 down and 4 right A = shift_rows(B, S); // shift each row i by S[i] A = shift_cols(B, S); // shift each column i by S[i] A = rotate(B, 3, 4); // rotate each element 3 down and 4 right A = rotate_rows(B, S); // rotate each row i by S[i] A = rotate_cols(B, S); // rotate each column i by S[i] Shifts and rotates are not affected by the active set. There is no pack() for matrices. There is no permute() for matrices.

Load and store:

Matrices can be loaded from an array, or stored into an array: intMatrix A(N,M); int array[N][M]; ... A.load(array); A.store(array); Only the active elements are loaded or stored.

Input and output:

Matrices can be used with iostreams for input and output. For a matrix A(N,M), cin >> A; // it expects N*M whitespace-separated integers, row-major order cout << A; // prints N lines, each with M tab-separated integers Only the active elements are written, so it may actually print fewer than N*M elements. If entire rows of a matrix are inactive, fewer than N lines may be printed. Similarly, for reading, only the active elements are read from input, and thus only the active elements should be provided as input.

Boolean matrices are printed as 0 and 1. Character matrices are printed without tab separators, and read without expecting them.


Back to DAPPLE