Vector summary

Below, Element refers to one of the following:

Correspondingly, Vector refers to one of the following:


Defining vectors:

// not initialized Vector(const int size); // init to scalar Vector(const int size, const Element x) // init with array Vector(const int size, const Element *d) // init with function Vector(const int size, Element (*f)(const int index)) // init with another vector Vector(const Vector& V);


Elementwise operations:

For two vectors 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 Vector& V), that is, apply(function_name, A) Only active elements are included.


Subscripting:

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


Vector products:

For two vectors A and B of the same size and type, and a scalar x of the same type, x = inner(A, B); // dot product of A and B Only active elements are included.

For two vectors A(N) and B(M), and a matrix C(N,M),

C = outer(A, B); // cross product of A and B The context should be one whose shape matches the resulting matrix. Only active elements of C are computed.


Reductions:

For a vector A (with N elements), a scalar x of the same type, an integer n, and a boolean b, x = sum(A); // x = A[0] + A[1] + ... + A[N-1] b = any(A); // b = true iff ANY of A[i] != 0 for i in 0..N-1 b = all(A); // b = true iff ALL of A[i] != 0 for i in 0..N-1 n = n_nonzeros(A); // n = number of nonzero elements in A n = n_active(A); // n = number of active elements in A x = first(A); // x = A[first_index(A)] n = first_index(A); // n = i if i is the smallest-numbered active virtual processor x = max_value(A); // x = max of A[i] for i in 0..N-1 x = min_value(A); // x = min of A[i] for i in 0..N-1 n = max_index(A); // n = i if A[i] is max of A[j] for j in 0..N-1 n = min_index(A); // n = i if A[i] is min of A[j] for j in 0..N-1 Only the active elements contribute to the reduction. sum(), max_value(), min_value(), max_index(), and min_index() do not apply to boolean vectors or matrices.


Scans:

For two vectors A and B of the same size and type, A = plus_scan(B); // A[i] = B[0] + B[1] + ... + B[i-1] A = max_scan(B); // A[i] = max of B[j], for j in 0..i-1 A = min_scan(B); // A[i] = min of B[j], for j in 0..i-1 Only active elements are involved.

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

C = or_scan(D); // C[i] = D[0] || D[1] || ... || D[i-1] C = and_scan(D); // C[i] = D[0] && D[1] && ... && D[i-1] Only active elements are involved.

Note that the value of the first element is independent of the contents of the vector. For plus_scan, the value is 0. For max_scan, the value is the smallest which that type can hold (for integers, -2147483647). For min_scan, the value is the largest which that type can hold (for integers, 2147483647). For or_scan, the value is false , and for and_scan, the value is true.


Shift, rotate, permute:

For two vectors A and B of the same size and type, and an integer vector P, A = shift(B, 3); // shift right: A[i+3] = B[i], except A[0..2] = 0 A = shift(B, -2); // shift left: A[i-2] = B[i], except A[N-2..N-1] = 0 A = pack(B); // active non-zeros go left, rest are zeros A = rotate(B, 3); // rotate right: A[i+3 mod N] = B[i] A = rotate(B, -2); // rotate left: A[i-2+N mod N] = B[i] A = permute(B, P); // permute B by P shift() and rotate() are not affected by the active set. Only active elements remain in the result of pack().

Only active elements are moved in permute(). The result vector is initialized to be the same as the input vector, and then overwritten by copying the active elements from the input vector to the appropriate destination element in the result vector. Thus, active elements may move to inactive positions.


Load and store:

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

Input and output:

Vectors can be used with iostreams for input and output. For a vector A(N), cin >> A; // expects N whitespace-separated integers cout << A; // prints N tab-separated integers (no newlines) Only the active elements are written, so it may actually print fewer than N elements. Similarly, for reading, only the active elements are read from input, and thus only the active elements should be provided as input.

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


Back to DAPPLE