![]() |
Blaze 3.9
|
Sometimes it may be desirable to separate two or more distinct groups of vectors and matrices, for instance in order to allow operations only within a group and to prevent operations across groups. This goal can be achieved by means of tags. All vector and matrix classes provide a template parameter to specify a tag (for instance, the fourth template parameter for blaze::DynamicVector and the sixth template parameter for blaze::StaticVector):
By default, all vectors and matrices are associated with blaze::Group0 (i.e. the tag is set to blaze::Group0). However, it is possible to explicitly associate vectors and matrices with different groups:
All vectors or matrices that are associated with the same group can be freely combined with any other vector or matrix from the same group. The attempt to combine vectors and matrices from different groups results in a compilation error.
Blaze provides the tags for the ten predefined groups blaze::Group0 through blaze::Group9. In order to create further groups, all that needs to be done is to create new instances of the blaze::GroupTag class template:
All groups based on the blaze::GroupTag class template will be treated as separate groups just as the ten predefined groups.
Sometimes it is not enough to separate vectors and matrices into different groups, but it is required to define the interaction between different groups. This situation for instance occurs if a vector or matrix is associated with a physical quantity. This problem can be solved by using custom tags. The following example gives an impression on how to define the physics on meters (represented by the Meter
tag) and seconds (represented by the Second
tag):
The Meter
and Second
tags are not associated with the blaze::GroupTag class template. For that reason, by default, it is not possible to perform any operation on an accordingly tagged vector or matrix. All required operations need to be declared explicitly in order to specify the resulting tag of an operation. In the following code example, this happens by declaring both the addition for the Meter
tag and the Second
tag, the multiplication between two Meter
tags and the division between Meter
and Second
. Note that it is enough to declare the operations, it is not necessary to define them!
With these declarations it is now possible to add meters and seconds, but not to subtract them (no subtraction operator was declared). Also, it is possible to multiply meters and to divide meters and seconds:
At this point it is possible to use the pow2()
function for vectors and matrices tagged with Meter
since pow2()
is based on multiplication, which has already been declared. However, it is not possible to use the abs()
function:
In order to enable the abs()
function it also needs to be explicitly declared for the Meter
tag:
Previous: Customization of Vectors and Matrices Next: Customization of the Error Reporting Mechanism