All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Vector Operations

Table of Contents

Previous: Vector Types     Next: Matrix Types


Constructors


Instantiating and setting up a vector is very easy and intuitive. However, there are a few rules to take care of:


Default Construction

// All vectors can be default constructed. Whereas the size
// of StaticVectors is fixed via the second template parameter,
// the initial size of a default constructed DynamicVector or
// CompressedVector is 0.
StaticVector<int,2UL> v1; // Instantiation of a 2D integer column vector.
// All elements are initialized to 0.
StaticVector<long,3UL,columnVector> v2; // Instantiation of a 3D long integer column vector.
// Again, all elements are initialized to 0L.
DynamicVector<float> v3; // Instantiation of a dynamic single precision column
// vector of size 0.
DynamicVector<double,rowVector> v4; // Instantiation of a dynamic double precision row
// vector of size 0.
CompressedVector<int> v5; // Instantiation of a compressed integer column
// vector of size 0.
CompressedVector<double,rowVector> v6; // Instantiation of a compressed double precision row
// vector of size 0.


Construction with Specific Size

The DynamicVector and CompressedVector classes offer a constructor that allows to immediately give the vector the required size. Whereas DynamicVector uses this information to allocate memory for all vector elements, CompressedVector merely acquires the size but remains empty.

DynamicVector<int,columnVector> v7( 9UL ); // Instantiation of an integer dynamic column vector
// of size 9. The elements are NOT initialized!
DynamicVector< complex<float> > v8( 2UL ); // Instantiation of a column vector with two single
// precision complex values. The elements are
// default constructed.
CompressedVector<int,rowVector> v9( 10UL ); // Instantiation of a compressed row vector with
// size 10. Initially, the vector provides no
// capacity for non-zero elements.


Initialization Constructors

All dense vector classes offer a constructor that allows for a direct, homogeneous initialization of all vector elements. In contrast, for sparse vectors the predicted number of non-zero elements can be specified

StaticVector<int,3UL,rowVector> v10( 2 ); // Instantiation of a 3D integer row vector.
// All elements are initialized to 2.
DynamicVector<float> v11( 3UL, 7.0F ); // Instantiation of a dynamic single precision
// column vector of size 3. All elements are
// set to 7.0F.
CompressedVector<float,rowVector> v12( 15UL, 3UL ); // Instantiation of a single precision column
// vector of size 15, which provides enough
// space for at least 3 non-zero elements.

The StaticVector class offers a special initialization constructor. For StaticVectors of up to 6 elements (i.e. 6D vectors) the vector elements can be individually specified in the constructor:

StaticVector<int,1UL> v13( 4 );
StaticVector<long,2UL> v14( 1L, -2L );
StaticVector<float,3UL,columnVector> v15( -0.1F, 4.2F, -7.1F );
StaticVector<double,4UL,rowVector> v16( 1.3, -0.4, 8.3, -1.2 );
StaticVector<size_t,5UL> v17( 3UL, 4UL, 1UL, 9UL, 4UL );
StaticVector<long,6UL> v18( 1L, 3L, -2L, 9L, 4L, -3L );


Copy Construction

All dense and sparse vectors can be created as the copy of any other dense or sparse vector with the same transpose flag (i.e. blaze::rowVector or blaze::columnVector).

StaticVector<int,9UL,columnVector> v19( v7 ); // Instantiation of the dense column vector v19
// as copy of the dense column vector v7.
DynamicVector<int,rowVector> v20( v9 ); // Instantiation of the dense row vector v20 as
// copy of the sparse row vector v9.
CompressedVector<int,columnVector> v21( v1 ); // Instantiation of the sparse column vector v21
// as copy of the dense column vector v1.
CompressedVector<float,rowVector> v22( v12 ); // Instantiation of the sparse row vector v22 as
// copy of the row vector v12.

Note that it is not possible to create a StaticVector as a copy of a vector with a different size:

StaticVector<int,5UL,columnVector> v23( v7 ); // Runtime error: Size does not match!
StaticVector<int,4UL,rowVector> v24( v10 ); // Compile time error: Size does not match!


Assignment


There are several types of assignment to dense and sparse vectors: Homogeneous Assignment, Array Assignment, Copy Assignment, and Compound Assignment.


Homogeneous Assignment

Sometimes it may be necessary to assign the same value to all elements of a dense vector. For this purpose, the assignment operator can be used:

// Setting all integer elements of the StaticVector to 2
v1 = 2;
// Setting all double precision elements of the DynamicVector to 5.0
v2 = 5.0;


Array Assignment

Dense vectors can also be assigned a static array:

float array1[2] = { 1.0F, 2.0F };
double array2[5] = { 2.1, 4.0, -1.7, 8.6, -7.2 };
v1 = array1;
v2 = array2;


Copy Assignment

For all vector types it is generally possible to assign another vector with the same transpose flag (i.e. blaze::columnVector or blaze::rowVector). Note that in case of StaticVectors, the assigned vector is required to have the same size as the StaticVector since the size of a StaticVector cannot be adapted!

// ... Initialization of the vectors
v1 = v2; // OK: Assignment of a 3D dense column vector to another 3D dense column vector
v1 = v4; // OK: Assignment of a 3D sparse column vector to a 3D dense column vector
v1 = v3; // Runtime error: Cannot assign a 5D vector to a 3D static vector
v1 = v5; // Compilation error: Cannot assign a row vector to a column vector


Compound Assignment

Next to plain assignment, it is also possible to use addition assignment, subtraction assignment, and multiplication assignment. Note however, that in contrast to plain assignment the size and the transpose flag of the vectors has be to equal in order to able to perform a compound assignment.

// ... Initialization of the vectors
v1 += v2; // OK: Addition assignment between two column vectors of the same size
v1 += v3; // Runtime error: No compound assignment between vectors of different size
v1 -= v4; // Compilation error: No compound assignment between vectors of different transpose flag
v4 *= v5; // OK: Multiplication assignment between two row vectors of the same size


Common Vector Operations


Size of a Vector

Via the size() function, the current size of a vector can be queried:

// Instantiating a dynamic vector with size 10
v1.size(); // Returns 10
// Instantiating a compressed vector with size 12 and capacity for 3 non-zero elements
v2.size(); // Returns 12


Capacity of a Vector

Via the capacity() function the internal capacity of a DynamicVector or CompressedVector can be queried. Note that the capacity of a vector doesn't have to be equal to the size of a vector. In case of a dense vector the capacity will always be greater or equal than the size of the vector, in case of a sparse vector the capacity may even be less than the size.

v1.capacity(); // returns at least 10


Number of Non-Zero Elements

For both dense and sparse vectors the number of non-zero elements can be determined via the nonZeros() function. Sparse vectors directly return their number of non-zero elements, dense vectors traverse their elements and count the number of non-zero elements.

// ... Initializing the vectors
v1.nonZeros(); // Returns the number of non-zero elements in the dense vector
v2.nonZeros(); // Returns the number of non-zero elements in the sparse vector


Resize/Reserve


The size of a StaticVector is fixed by the second template parameter. In contrast, the size of DynamicVectors as well as CompressedVectors can be changed via the resize() function:

DynamicVector<int,columnVector> v1;
CompressedVector<int,rowVector> v2( 4 );
v2[1] = -2;
v2[3] = 11;
// Adapting the size of the dynamic and compressed vectors. The (optional) second parameter
// specifies whether the existing elements should be preserved. Per default, the existing
// elements are not preserved.
v1.resize( 5UL ); // Resizing vector v1 to 5 elements. Elements of built-in type remain
// uninitialized, elements of class type are default constructed.
v1.resize( 3UL, false ); // Resizing vector v1 to 3 elements. The old elements are lost, the
// new elements are NOT initialized!
v2.resize( 8UL, true ); // Resizing vector v2 to 8 elements. The old elements are preserved.
v2.resize( 5UL, false ); // Resizing vector v2 to 5 elements. The old elements are lost.

When the internal capacity of a vector is no longer sufficient, the allocation of a larger junk of memory is triggered. In order to avoid frequent reallocations, the reserve() function can be used up front to set the internal capacity:

v1.reserve( 100 );
v1.size(); // Returns 0
v1.capacity(); // Returns at least 100

Note that the size of the vector remains unchanged, but only the internal capacity is set according to the specified value!


Element Access


The easiest and most intuitive way to access a dense or sparse vector is via the subscript operator. The indices to access a vector are zero-based:

v1[0] = 1;
v1[1] = 3;
// ...
v2[2] = 7.3F;
v2[4] = -1.4F;

Whereas using the subscript operator on a dense vector only accesses the already existing element, accessing an element of a sparse vector via the subscript operator potentially inserts the element into the vector and may therefore be more expensive. Consider the following example:

for( size_t i=0UL; i<v1.size(); ++i ) {
... = v1[i];
}

Although the compressed vector is only used for read access within the for loop, using the subscript operator temporarily inserts 10 non-zero elements into the vector. Therefore, all vectors (sparse as well as dense) offer an alternate way via the begin() and end() functions to traverse only the currently contained elements by iterators. In case of non-const vectors, begin() and end() return an Iterator, which allows a manipulation of the non-zero value, in case of a constant vector a ConstIterator is returned:

CompressedVector<int> v1( 10UL );
// ... Initialization of the vector
// Traversing the vector by Iterator
for( CompressedVector<int>::Iterator it=v1.begin(); it!=v1.end(); ++it ) {
it->value() = ...; // OK: Write access to the value of the non-zero element.
... = it->value(); // OK: Read access to the value of the non-zero element.
it->index() = ...; // Compilation error: The index of a non-zero element cannot be changed.
... = it->index(); // OK: Read access to the index of the non-zero element.
}
// Traversing the vector by ConstIterator
for( CompressedVector<int>::ConstIterator it=v1.begin(); it!=v1.end(); ++it ) {
it->value() = ...; // Compilation error: Assignment to the value via a ConstIterator is invalid.
... = it->value(); // OK: Read access to the value of the non-zero element.
it->index() = ...; // Compilation error: The index of a non-zero element cannot be changed.
... = it->index(); // OK: Read access to the index of the non-zero element.
}


Element Insertion


In contrast to dense vectors, that store all elements independent of their value and that offer direct access to all elements, spares vectors only store the non-zero elements contained in the vector. Therefore it is necessary to explicitly add elements to the vector. The first option to add elements to a sparse vector is the subscript operator:

CompressedVector<int> v1( 3UL );
v1[1] = 2;

In case the element at the given index is not yet contained in the vector, it is automatically inserted. Otherwise the old value is replaced by the new value 2. The operator returns a reference to the sparse vector element.
However, insertion of elements can be better controlled via the insert() function. In contrast to the subscript operator it emits an exception in case the element is already contained in the matrix. In order to check for this case, the find() function can be used:

// In case the element at index 4 is not yet contained in the matrix it is inserted
// with a value of 6.
if( v1.find( 4 ) == v1.end() )
v1.insert( 4, 6 );

Although the insert() function is very flexible, due to performance reasons it is not suited for the setup of large sparse vectors. A very efficient, yet also very low-level way to fill a sparse vector is the append() function. It requires the sparse vector to provide enough capacity to insert a new element. Additionally, the index of the new element must be larger than the index of the previous element. Violating these conditions results in undefined behavior!

v1.reserve( 5 ); // Reserving space for 5 non-zero elements
v1.append( 5, -2 ); // Appending the element -2 at index 5
v1.append( 6, 4 ); // Appending the element 4 at index 6
// ...


Reset/Clear


In order to reset all elements of a vector, the reset() function can be used:

// Setup of a single precision column vector, whose elements are initialized with 2.0F.
// Resetting all elements to 0.0F. Only the elements are reset, the size of the vector is unchanged.
reset( v1 ); // Resetting all elements
v1.size(); // Returns 3: size and capacity remain unchanged

In order to return a vector to its default state (i.e. the state of a default constructed vector), the clear() function can be used:

// Setup of a single precision column vector, whose elements are initialized with -1.0F.
// Resetting the entire vector.
clear( v1 ); // Resetting the entire vector
v1.size(); // Returns 0: size is reset, but capacity remains unchanged

Note that resetting or clearing both dense and sparse vectors does not change the capacity of the vectors.


Vector Transpose


As already mentioned, vectors can be either column vectors (blaze::columnVector) or row vectors (blaze::rowVector). A column vector cannot be assigned to a row vector and vice versa. However, vectors can be transposed via the trans() function:

v1 = v2; // Compilation error: Cannot assign a row vector to a column vector
v1 = trans( v2 ); // OK: Transposing the row vector to a column vector and assigning it
// to the column vector v1
v2 = trans( v1 ); // OK: Transposing the column vector v1 and assigning it to the row vector v2
v1 += trans( v2 ); // OK: Addition assignment of two column vectors


Swap

Via the swap() function it is possible to completely swap the contents of two vectors of the same type:

swap( v1, v2 ); // Swapping the contents of v1 and v2


Previous: Vector Types     Next: Matrix Types