cisstVector: Vectors and matrices quick sheet (part 1 of 4) Revision: 1.18 - Date: 2006/11/21 21:21:53

Code[2] Description
Classes
and typedefs
vctFixedSizeVector<_elementType, _size>
- vctDouble1 vctDouble2 vctDouble3 vctDouble4 ...
- vct1 vct2 vct3 vct4 vct5 vct6 ...
- vctFloat1 vctFloat2 vctFloat3 vctFloat4 vctFloat5 ...
- vctInt1 vctInt2 vctInt3 vctInt4 vctInt5 vctInt6 ...
- vctChar1 vctChar2 vctChar3 vctChar4 vctChar5 ...
Fixed size vector
...of doubles
...of doubles (implicit)
...of floats
...of ints
...of chars
vctFixedSizeMatrix<_elementType, _rows, _cols>
- vctDouble2x2 vctDouble3x3 vctDouble2x3 ...
- vct2x2 vct3x3 vct4x4 vct2x3 vct2x4 vct3x4 ...
- vctFloat2x2 vctFloat3x3 vctFloat4x4 vctFloat3x2 ...
- vctInt2x2 vctInt3x3 vctInt4x4 vctInt3x4 ...
vctFixedSizeMatrix<_elementType, _rows, _cols, _order>
- vctFixedSizeMatrix<double, 3, 3, VCT_ROW_MAJOR> m1;
- vctFixedSizeMatrix<double, 3, 3, VCT_COL_MAJOR> m2;
Fixed size matrix
...of doubles
...of doubles (implicit)
...of floats
...of ints
With specific storage order
- row major[4] (default, $\simeq$ vct3x3)
- col major (Fortran like)
vctDynamicVector<_elementType>
- vctDoubleVec, vctVec
- vctFloatVec, vctIntVec
Dynamic vector
...of doubles, doubles (implicit)
...of floats, ints
vctDynamicMatrix<_elementType>
- vctDoubleMat, vctMat
- vctFloatMat, vctIntMat
Dynamic matrix
...of doubles, doubles (implicit)
...of floats, ints
Constructors vct4 v1;
vct4 v2(0.0); vct4 v3(0.1, 0.2, 0.3, 0.4);
vct4 v4(v1);
Default constructor (undefined content)
Assign values, all or individually
Copy constructor (copy elements)
vct2x2 m1;
vct2x2 m2(0.0); vct2x2 m3(0.1, 0.2, 0.3, 0.4);
vct2x2 m4(m1);
Default constructor (undefined content)
Assign values, all or individually (by rows)
Copy constructor (copy elements)
vctVec v1;
vctVec v2(12);
vctVec v3(2, 0.0); vctVec v4(2, 0.1, 0.2);
vctVec v5(v1);
Default constructor, size is zero
Set size (undefined content)
Set size & assign values, all or individually
Copy constructor (copy elements)
vctMat m1;
vctMat m2(12, 7);
vctMat m3(6, 4, VCT_ROW_MAJOR);
vctMat m4(5, 7, VCT_COL_MAJOR);
vctMat m5(2, 3, 0.0);
vctMat m6(3, 5, 0.0, VCT_ROW_MAJOR);
vctMat m7(2, 3, 0.0, VCT_COL_MAJOR);
vctMat m8(2, 2, 0.1, 0.2, 0.3, 0.4);
vctMat m9(m4);
Default constructor, size is zero
Set size; rows and colums (undefined content)
Set size and row major[4] (undefined content)
Set size and column major (undefined content)
Set size & assign value to all
Set size, row major[4] & assign value to all
Set size, column major & assign value to all
Set size and assign values by rows
Copy constructor (copy elements)
Assignments c1.Assign(c2); c1 = c2 Assign from similar container
c.Assign(s0, s1, s2, ...)
e.g.: v.Assign((double)0, 1.3, .34, 0.0);
Assign from multiple variables using va_arg
Input must be explicitly cast to element type!
c.SetAll(value); c = value;
vctRandom(c, s_min, s_max;)
Assign value to all elements
Fill with random values in $[s_{min}, s_{max}]$
bool succeeded = c1.FastCopyOf(c2);
bool succeeded = c1.FastCopyOf(c2, false);
Shallow copy using memcpy[7]
Shallow copy without safety checks
Identity
(Eye for $I$)
MatrixType::Eye()
- vct4x4 m = vct4x4::Eye();
- vctInt3x3 m = vctFixedSizeMatrix<int, 3, 3>::Eye();
- vctMat m = vctMat::Eye(9);
- vctIntMat = vctDynamicMatrix<int>::Eye(6);
Eye for dynamic & fixed size matrices using:
- fixed size matrix typedef
- fixed size matrix class definition
- dynamic matrix typedef and dimension ($9\times9$)
- dynamic matrix class definition ($6\times6$)

[2]v is for vector, m for matrix, c for a container (either vector or matrix) and s is for scalar.

[4]VCT_ROW_MAJOR is the default for fixed size and dynamic matrices. Use VCT_COL_MAJOR to store by column.

[7]FastCopyOf fails and returns false if the containers are not compact or don't have the same memory layout (storage order).

cisstVector: Vectors and matrices quick sheet (part 2 of 4) Revision: 1.18 - Date: 2006/11/21 21:21:53

Code Description
Member
typedefs
(STL like)
ContainerType::size_type size;
ContainerType::index_type index;
ContainerType::stride_type stride;
ContainerType::value_type element;
ContainerType::reference ref;
ContainerType::const_reference constRef;
ContainerType::pointer ptr;
ContainerType::const_pointer constPtr;
ContainerType::NormType norm;
ContainerType::AngleType angle;
Size type (unsigned int)
Index type (unsigned int)
Stride type (int)
Type of contained elements
Reference on element (value_type &)
Const reference (const value_type &)
Pointer on element (value_type *)
Const pointer (const value_type *)
Norm type (double)
Angle type (double)
Iterators
(STL like)
ContainerType::iterator iter;
ContainerType::const_iterator iter;
ContainerType::reverse_iterator iter;
ContainerType::const_reverse_iterator iter;
iter = c.begin(); iter = c.end();
iter = c.rbegin(); iter = c.rend();
Iterator type
Const iterator type
Reverse iterator type
Const reverse iterator type
Iterator on first/last element
Reverse iterator on last/first element
Memory layout size = c.size();
nbRows = m.rows(); nbCols = m.cols();
b = c.empty();
b = m.IsSquare();
b = m.IsSquare(5);
Length for vectors, rows $\times$ cols for matrices
Number of rows/columns
Test if size is zero
Test if matrix is square
Test if square matrix of a given size (e.g. 5)
stride = v.stride();
rowStride = m.row_stride();
colStride = m.col_stride();
Stride between elements
Stride between rows
Stride between columns
ptr = c.Pointer();
ptr = v.Pointer(i);
ptr = m.Pointer(row, col);
Pointer on first element
Pointer on $i^{th}$ vector element
Pointer on $[row,col]$ matrix element
Test indices bool b = c.ValidIndex(index);
bool b = m.ValidIndex(row, col);
bool b = m.ValidRowIndex(row);
bool b = m.ValidColIndex(col);
Index is lesser or equal to size
Both row and column indices are correct
Row index is valid
Column index is valid
Storage bool b = m.IsRowMajor(); Stored row first (C/C++ like - default)
bool b = m.IsColMajor(); Stored column first (Fortran like)
bool b = m.IsCompact(); Uses a compact block of memory , i.e., all elements are in one contiguous block.
bool b = m.IsFortran(); Both compact and column major
bool storageOrder = m.StorageOrder(); Returns storage order (true for row major, false for column major)
Dynamic vector
resizing
v.SetSize(newSize);
v.resize(newSize);
Destructive size change
Non destructive size change
Dynamic matrix
resizing
m.SetSize(rows, cols, storageOrder);
m.resize(rows, cols);
Destructive size change
Non destructive size change
Access vector parts v.X() = x; y = v.Y(); v.Z() = z; w = v.W(); Access elements by name
v.XY() = v2; v3 = v.XYZ(); v4 = v.XYZW(); Access subvectors by name
v.XZ(); v.XW(); v.YZ(); v.YW(); v.ZW(); v.YZW(); Access subvectors other than ``head''
Access matrix parts v = m.Row(index); v = m.Column(index);
v = m.Diagonal(); mt = m.TransposeRef();
Reference to a given row/column
Reference to the diagonal/transpose
Matrix
permutations
m.ExchangeRows(r1, r2);
m.ExchangeColumns(c1, c2);
Swap the values between two rows/columns
Elementwise tests b = c.IsPositive();
b = c.IsNonNegative();
b = c.IsNegative();
b = c.IsNonPositive();
b = c.All();
b = c.Any();
All elements are greater than zero
All elements are greater than or equal to zero
All elements are lesser than zero
All elements are lesser than or equal to zero
All elements are different from zero
At least one element is different from zero
Formatted output std::string s = c.ToString();
std::cout « c; c.ToStream(std::cout);
Output to std::string
Output to a C++ stream

cisstVector: Vectors and matrices quick sheet (part 3 of 4) Revision: 1.18 - Date: 2006/11/21 21:21:53

Description Method or Function[2] Operator
$+$ Addition c.SumOf(c1, cs2);
c.Add(cs1);
c = c1 + cs2;
c += cs1;
$-$
Subtraction
c.DifferenceOf(c1, cs2);
c.Subtract(cs1);
c = c1 - cs2;
c -= cs1;
$\times$ Container scalar multiplication c1.ProductOf(c2, s);
c1.ProductOf(s, c2);
c.Multiply(s1);
c1 = c2 * s;
c1 = s * c2;
c *= s1;

Matrix vector multiplication
v2.ProductOf(m, v1);
v2.ProductOf(v1, m);
v2 = m * v1;
v2 = v1 * m;

Matrix matrix multiplication[4] m.ProductOf(m1, m2); m = m1 * m2;

Vector outer product m.OuterProductOf(v1, v2);

Elementwise multiplication
$c_1[i] = c_2[i] * c_3[i]$ and $c_1[i] *= c_2[i]$
c1.ElementwiseProductOf(c2, c3);
c1.ElementwiseMultiply(c2);
$\div$ Container scalar division c1.RatioOf(c2, s);
c.Divide(s);
c1 = c2 / s;
c /= s;

Elementwise division
$c_1[i] = c_2[i] / c_3[i]$ and $c_1[i] /= c_2[i]$
c1.ElementwiseRatioOf(c2, c3);
c1.ElementwiseDivide(c2);
$\times$ $\wedge$ Cross product (size 3 only)
% operator has high precedence
v1.CrossProductOf(v2, v3);
v1 = vctCrossProduct(v2, v3);
v1 = v2 % v3;
$\circ$ $\bullet$ Dot product (inner product) s = v1.DotProduct(v2);
s = vctDotProduct(v1, v2);
s = v1 * v2;
$+\times$ Accumulation c2.AddProductOf(s, c1); c2 += (s * c1);
$=$

$\neq$

$\simeq$
Equal

Not equal

Approximately equal
b = c1.Equal(cs2);
cb = c1.ElementwiseEqual(cs2);
b = c1.NotEqual(c2);
cb = c1.ElementwiseNotEqual(c2);
b = c1.AlmostEqual(c2, tolerance);
b = (c1 == cs2);

b = (c1 != cs2);
$<$

$\leq$
Lesser

Lesser or equal
b = c1.Lesser(cs2);
cb = c1.ElementwiseLesser(cs2);
b = c1.LesserOrEqual(cs2);
cb = c1.ElementwiseLesserOrEqual(cs2);
$>$

$\geq$
Greater

Greater or equal
b = c1.Greater(cs2);
cb = c1.ElementwiseGreater(cs2);
b = c1.GreaterOrEqual(cs2);
cb = c1.ElementwiseGreaterOrEqual(cs2);
$min(c_i, u)$


$max(c_i, l)$
Clip above upper bound


Clip below lower bound
c.ClipAbove(s);
c2.ClippedAboveOf(c1, s);
c2.ClippedAboveOf(s, c1);
c.ClipBelow(s);
c2.ClippedBelowOf(c1, s);
c2.ClippedBelowOf(s, c1);

[2]Operand types: b boolean, s scalar, v vector, m matrix, c container (v or m), cs container or scalar, cb container of booleans.

[4]Verifies that output base pointer is different from input pointers, throws std::runtime_error otherwise.

cisstVector: Vectors and matrices quick sheet (part 4 of 4) Revision: 1.18 - Date: 2006/11/21 21:21:53

Description Method or Function[2] Operator
$\Vert c\Vert _2$
$\Vert c\Vert _2^2$
$\Vert c\Vert _1$
$\Vert c\Vert _\infty$
$\sum_i c[i]$
$\prod_i c[i]$
$\sum_i m[i,i]$
$\min_i c[i]$
$\max_i c[i]$
$\min_i \vert c[i]\vert$
$\max_i \vert c[i]\vert$
$\ell_2$ norm
$\ell_2$ norm square
$\ell_1$ norm
$\ell_\infty$ norm
Sum of elements
Product of elements
Trace
Minimum
Maximum
Minimum of absolute values
Maximum of absolute values
Minimum and maximum
s = c.Norm();
s = c.NormSquare();
s = c.L1Norm();
s = c.LinfNorm();
s = c.SumOfElements();
s = c.ProductOfElements();
s = m.Trace();
s = c.MinElement();
s = c.MaxElement();
s = c.MinAbsElement();
s = c.MaxAbsElement();
c.MinAndMaxElement(s_min, s_max);
$c/\Vert c\Vert$ Normalization c.NormalizedSelf(); b = c.IsNormalized();
c2.NormalizedOf(c1); c2 = c1.Normalized();
c /= c.Norm();
$\vert c_i\vert$ Elementwise absolute values c.AbsSelf(); c2.AbsOf(c1); c2 = c1.Abs();
$\lfloor c_i \rfloor$ Elementwise floor c.FloorSelf(); c2.FloorOf(c1); c2 = c1.Floor();
$\lceil c_i \rceil$ Elementwise ceil c.CeilSelf(); c2.CeilOf(c1); c2 = c1.Ceil();
$-$ Negation c.NegationSelf();
c2.NegationOf(c1); c2 = c1.Negation();

c2 = -c1;
$v_i$
Access vector elements
s = v.Element(i);
s = v.at(i);[5]
s = v[i];
s = v(i);[5]
$m_{i,j}$ Access matrix elements s = m.Element(row,col);
s = m.at(row, col);[5]
v = m.Row(row);
v = m.Column(col); v = m.Diagonal();
s = m[row][col];
s = m(row, col);[5]
v = m[row];

[2]Operand types: b boolean, s scalar, v vector, m matrix, c container (v or m), cs container or scalar, cb container of booleans.

[5]Performs a bound check and throws std::out_of_range if necessary.

cisstVector: Transformations quick sheet Revision: 1.18 - Date: 2006/11/21 21:21:53

Code Description
Global constants cmnPI, cmnPI_2, cmnPI_4 $\pi$, $\pi / 2$ , $\pi / 4$
Typedefs for
rotations &
frames
vctRot2 (a.k.a. vctMatRot2), vctAnRot2
vctRot3 (a.k.a. vctMatRot3), vctQuatRot3
vctAxAnRot3, vctRodRot3
vctFrm3 (a.k.a. vctMatFrm3)
vctQuatFrm3
Matrix/angle rotation SO(2)
Matrix/quaternion rotation SO(3)
Axis-angle/Rodriguez rotation SO(3)
Frame based on matrix rotation SO(3)
Frame based on quaternion rotation SO(3)
Frame components rotation = frame.Rotation();
translation = frame.Translation();
Rotation reference (& and const &)
Translation reference (& and const &)
Identity constants id = TransformationType::Identity();
e.g.: vctRot3 id = vctRot3::Identity();
Identity (static method per class)
Inverse &
normalization
invTr.InverseOf(tr); invTr = tr.Inverse();
tr.InverseSelf();
Inverse transformation
norTr.NormalizedOf(tr); norTr = tr.Normalized();
tr.NormalizedSelf();
Normalized transformation[5]
b = tr.IsNormalized(tolerance);[8]
Equality
See equivalence for non unique representations
b = tr1.Equal(tr2);
b = (tr1 == tr2);
b = tr1.AlmostEqual(tr2);[8]
b = tr1.AlmostEqual(tr2, tolerance);
Exactly equal (elementwise)

Almost equal (elementwise)
With user specified tolerance
Equivalence across non-unique representations, such as axis-angle b = tr1.AlmostEquivalent(tr2);[8]
b = tr1.AlmostEquivalent(tr2, tolerance);
Equivalent transformations
With user specified tolerance
Conversions
between rotations[2]
TypeA rotA(rotB);
TypeA rotA; rotA.From(rotB);
If rotB is normalized convert to
TypeA otherwise throw an exception
or frames TypeA rotA(rotB, VCT_NORMALIZE);
TypeA rotA; rotA.FromNormalized(rotB);
Convert to TypeA from a normalized
copy of rotB

TypeA rotA(rotB, VCT_DO_NOT_NORMALIZE);
TypeA rotA; rotA.FromRaw(rotB);
Convert to TypeA with neither validation
nor modification of rotB
Apply &
apply inverse[7]
tr.ApplyTo(in, out);
out = tr.ApplyTo(in); out = tr * in
tr.ApplyInverseTo(in, out);
out = tr.ApplyInverseTo(in);
Apply to either a vector, a matrix or a transformation of the same type. Note that the methods with (in, out) avoid the creation of a temporary variable.

[2]TypeA and TypeB can be vctAnRot2 or vctMatRot2 for 2D rotations and vctMatRot3, vctQuatRot3, vctAxAnRot3 or vctRodRot3 in 3D.

[5]The normalization methods do not modify the angle of vctAnRot2 and vctAxAnRot3 nor vctRodRot3.

[7]The rotations vctAnRot2, vctAxAnRot3 and vctRodRot3 do not support the Apply* methods nor the operator *.

[8]Most methods requiring a tolerance use cmnTypeTraits<value_type>::Tolerance() by default.