TooN::Vector< Size, Precision, Base > Class Template Reference
[Linear Algebra]

A vector. More...

#include <TooN/toon.h>

List of all members.

Public Member Functions

Constructors
 Vector ()
 Vector (int size_in)
 Vector (Precision *data)
 Vector (Precision *data, int size_in)
 Vector (Precision *data_in, int size_in, int stride_in, Internal::Slicing)
template<class Op>
 Vector (const Operator< Op > &op)
template<int Size2, typename Precision2, typename Base2>
 Vector (const Vector< Size2, Precision2, Base2 > &from)
Accessing elements
Precision & operator[] (int i)
const Precision & operator[] (int i) const
Assignment
Vectoroperator= (const Vector &from)
template<int Size2, typename Precision2, typename Base2>
Vector< Size,
Precision, Base > & 
operator= (const Vector< Size2, Precision2, Base2 > &from)
template<class Op>
Vectoroperator= (const Operator< Op > &op)
Operators on the vector
Vectoroperator/= (const Precision &rhs)
Vectoroperator *= (const Precision &rhs)
template<int Size2, class Precision2, class Base2>
Vectoroperator+= (const Vector< Size2, Precision2, Base2 > &rhs)
template<class Op>
Vectoroperator+= (const Operator< Op > &op)
template<class Op>
Vectoroperator-= (const Operator< Op > &op)
template<int Size2, class Precision2, class Base2>
Vectoroperator-= (const Vector< Size2, Precision2, Base2 > &rhs)
Comparison
template<int Size2, class Precision2, class Base2>
bool operator== (const Vector< Size2, Precision2, Base2 > &rhs)
template<int Size2, class Precision2, class Base2>
bool operator!= (const Vector< Size2, Precision2, Base2 > &rhs)
Misc
Vectorref ()
int size () const
Reshaping, sub-vectors and matrices
Matrix< 1, Size,
Precision > 
as_row ()
Matrix< Size,
1, Precision > 
as_col ()
DiagonalMatrix< Size,
Precision > 
as_diagonal ()
template<Start , Length >
const Vector< Length,
Precision > & 
slice () const
template<Start , Length >
Vector< Length,
Precision > & 
slice ()
template<Start , Length >
const Vector< Length,
Precision > & 
slice () const
template<Start , Length >
Vector< Length,
Precision > & 
slice ()

Related Functions

(Note that these are not member functions.)

Arithmetic operations
template<int Size>
Vector< Size > operator- (const Vector< Size > &v)
template<int Size>
Vector< Size > operator+ (const Vector< Size > &lhs, const Vector< Size > &rhs)
template<int Size>
Vector< Size > & operator+= (Vector< Size > &lhs, const Vector< Size > &rhs)
template<int Size>
Vector< Size > operator- (const Vector< Size > &lhs, const Vector< Size > &rhs)
template<int Size>
Vector< Size > & operator-= (Vector< Size > &lhs, const Vector< Size > &rhs)
template<int Size>
Vector< Size > operator * (const Vector< Size > &lhs, double rhs)
template<int Size>
Vector< Size > operator * (double lhs, const Vector< Size > &rhs)
template<int Size>
Vector< Size > & operator *= (const Vector< Size > &lhs, const double &rhs)
template<int Size>
Vector< Size > operator/ (const Vector< Size > &lhs, double rhs)
template<int Size>
Vector< Size > & operator/= (const Vector< Size > &lhs, const double &rhs)
template<int Size>
double operator * (const Vector< Size > &lhs, const Vector< Size > &rhs)
Vector< 3 > operator^ (const Vector< 3 > &lhs, const Vector< 3 > &rhs)
template<int Size>
Vector< Size > diagmult (const Vector< Size > &lhs, const Vector< Size > &rhs)


Detailed Description

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
class TooN::Vector< Size, Precision, Base >

A vector.

Support is provided for all the usual vector operations:

See individual member function documentation for examples of usage.

Statically- and dynamically-sized vectors
The library provides classes for both statically- and dynamically-sized vectors. If you know what dimension of vector you're going to use (e.g. 3 to represent a point in 3D space), it's more efficient to statically sized vectors. The size of static vectors is determined at compile time; that of dynamically-sized vectors at run-time.

To create a 3-dimensional vector, use:

Vector<3> v;

and to create a vector of some other dimensionality just replace 3 with the positive integer of your choice, or some expression which the compiler can evaluate to an integer at compile time.

The preferred way of initialising a vector is to use makeVector. The makeVector function constructs a static vector initialised to the size and the contents of the comma-separated list of argments. The makeVector vectors are real Vectors and so can be used anywhere where a vector is needed, not just in initialisations. For example

// Create a vector initialised to [1 2 3];
Vector<3> v = makeVector(1, 2, 3);
// Calculate the dot product with the vector [4 0 6]
double dot = v * makeVector(4, 0, 6);

Because the make_Vector syntax creates actual vectors, compile-time checking is done to ensure that all vectors defined in this way have the correct number of elements.

Dynamically-sized vectors
To create a dynamically sized vector, use:
Vector<> v(size);
where size is an integer which will be evaluated at run time.

Vector<> is actually a synonym for Vector<Dynamic> which is Vector<-1> being a template specialisation of Vector<N> with a special implementation that allows the size to be determined at runtime.

Row vectors and column vectors
This library makes no distinction between row vectors and column vectors. Vectors that appear on the left of a multiplication are treated as row vectors while those that appear on the right are treated as column vectors (thus v1*v2 means the dot product). This means that sometimes you have to be careful to include prarentheses since it is possible to write obscure stuff like

Vector<4> v4 = v1 * v2 * v3;

which in the absence of any extra parentheses means 'compute the dot product between v1 and v2 and then multiply v3 by this scalar and assign to v4'.

If the row-column distinction is important, then vectors can be turned into matrices with one row or column by using as_row() or as_col():

double d[3] = {1,2,3};
Vector<3> v(d);
Matrix<3,3> M = v.as_col() * v.as_row(); // creates a symmetric rank 1 matrix from v 

Constructor & Destructor Documentation

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
TooN::Vector< Size, Precision, Base >::Vector (  ) 

Default constructor for vectors.

For fixed-sized vectors, this does nothing, i.e. does not guarantee to initialise the vector to any particular values. For dynamically sized vectors, this sets the vector to have a length of 0 which renders the vector useless because vectors can't be resized

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
TooN::Vector< Size, Precision, Base >::Vector ( int  size_in  ) 

Constructor for dynamically-size vectors.

This can also be used for statically sized vectors in which case the argument is ignored. The values of the vector are uninitialised

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
TooN::Vector< Size, Precision, Base >::Vector ( Precision *  data  ) 

Constructor used when constructing a vector which references other data, e.g.

 double[] d = {1,2,3};
 Vector<3,double,Reference> v(d);

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
TooN::Vector< Size, Precision, Base >::Vector ( Precision *  data,
int  size_in 
)

Constructor used when constructing a dynamic vector which references other data, e.g.

 double[] d = {1,2,3};
 Vector<Dynamic,double,Reference> v(d,3);

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
TooN::Vector< Size, Precision, Base >::Vector ( Precision *  data_in,
int  size_in,
int  stride_in,
Internal::Slicing   
)

internal constructor

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<class Op>
TooN::Vector< Size, Precision, Base >::Vector ( const Operator< Op > &  op  ) 

construction from Operator object

This is used to implement return value optimisation for vectors created from the product of a matrix and a vector, or another object like Ones

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<int Size2, typename Precision2, typename Base2>
TooN::Vector< Size, Precision, Base >::Vector ( const Vector< Size2, Precision2, Base2 > &  from  ) 

constructor from arbitrary vector


Member Function Documentation

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
Precision& TooN::Vector< Size, Precision, Base >::operator[] ( int  i  ) 

access an element of the vector

can be used as an l-value ie

 Vector<3> v;
 v[0] = 10;

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
const Precision& TooN::Vector< Size, Precision, Base >::operator[] ( int  i  )  const

access an element of a constant vector

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
Vector& TooN::Vector< Size, Precision, Base >::operator= ( const Vector< Size, Precision, Base > &  from  ) 

operator = from copy

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<int Size2, typename Precision2, typename Base2>
Vector<Size,Precision,Base >& TooN::Vector< Size, Precision, Base >::operator= ( const Vector< Size2, Precision2, Base2 > &  from  ) 

operator = another Vector

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<class Op>
Vector& TooN::Vector< Size, Precision, Base >::operator= ( const Operator< Op > &  op  ) 

assignment from an Operator object

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
Vector& TooN::Vector< Size, Precision, Base >::operator/= ( const Precision &  rhs  ) 

divide this vector by a constant

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
Vector& TooN::Vector< Size, Precision, Base >::operator *= ( const Precision &  rhs  ) 

multiply this vector by a constant

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<int Size2, class Precision2, class Base2>
Vector& TooN::Vector< Size, Precision, Base >::operator+= ( const Vector< Size2, Precision2, Base2 > &  rhs  ) 

add another vector onto this one

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<class Op>
Vector& TooN::Vector< Size, Precision, Base >::operator+= ( const Operator< Op > &  op  ) 

add an Operator object onto this vector

this is used to handle cases such as:

 Vector<3> v;
 v+=Ones

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<int Size2, class Precision2, class Base2>
Vector& TooN::Vector< Size, Precision, Base >::operator-= ( const Vector< Size2, Precision2, Base2 > &  rhs  ) 

subtract another vector from this one

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<int Size2, class Precision2, class Base2>
bool TooN::Vector< Size, Precision, Base >::operator== ( const Vector< Size2, Precision2, Base2 > &  rhs  ) 

Test for equality with another vector.

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<int Size2, class Precision2, class Base2>
bool TooN::Vector< Size, Precision, Base >::operator!= ( const Vector< Size2, Precision2, Base2 > &  rhs  ) 

Test for inequality with another vector.

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
Vector& TooN::Vector< Size, Precision, Base >::ref (  ) 

return me as a non const reference - useful for temporaries

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
int TooN::Vector< Size, Precision, Base >::size (  )  const

What is the size of this vector?

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
Matrix<1, Size, Precision> TooN::Vector< Size, Precision, Base >::as_row (  ) 

Convert this vector into a 1-by-Size matrix, i.e.

a matrix which has this vector as its only row.

       Vector<3> a = makeVector(1,2,3);
       Matrix<1,3> m = a.as_row();  // now m = [1 2 3]

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
Matrix<Size, 1, Precision> TooN::Vector< Size, Precision, Base >::as_col (  ) 

Convert this vector into a Size-by-1 matrix, i.e.

a matrix which has this vector as its only column.

       Vector<3> a = makeVector(1,2,3);
       Matrix<3,1> m = a.as_col();   // now m = [1 2 3]'

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
DiagonalMatrix<Size,Precision> TooN::Vector< Size, Precision, Base >::as_diagonal (  ) 

Convert this vector into a Diagonal Size-by-Size matrix, i.e.

a matrix which is zero everywhere except on the diagonal and the diagonal contains the values from this vector

       Vector<3> v = makeVector(1,2,3);
       Vector<3> v2 = makeVector(2,3,4);
       Vector<3> v3 = v.as_diagonal() * v2; // now v3 = (2,6,12)

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<Start , Length >
const Vector<Length,Precision>& TooN::Vector< Size, Precision, Base >::slice (  )  const

Extract a sub-vector.

The vector extracted will be begin at element Start and will contain the next Length elements.

       Vector<5> a = makeVector(1,2,3,4,5);
       Extract the three elements starting from element 2
       Vector<3> b = a.slice<2,3>();  /// b = [3 4 5]

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<Start , Length >
Vector<Length,Precision>& TooN::Vector< Size, Precision, Base >::slice (  ) 

Extract a sub-vector.

The vector extracted will be begin at element Start and will contain the next Length elements. This version can be used as an l-value as well as an r-value

       Vector<5> a = makeVector(1,2,3,4,5);
       Vector<2> b = makeVector(8,9);
       // replace the two elements starting from element 1 with b
       a.slice<1, 2>() = b;       /// now a = [1 8 9 4 5]

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<Start , Length >
const Vector<Length,Precision>& TooN::Vector< Size, Precision, Base >::slice (  )  const

Extract a sub-vector with runtime parameters.

The vector extracted will be begin at element start and will contain the next length elements.

       Vector<5> a = makeVector(1,2,3,4,5);
       Extract the three elements starting from element 2
       Vector<> b = a.slice(2,3);  /// b = [3 4 5]

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
template<Start , Length >
Vector<Length,Precision>& TooN::Vector< Size, Precision, Base >::slice (  ) 

Extract a sub-vector with runtime parameters, which can be used as an l-value.

The vector extracted will be begin at element start and will contain the next length elements.

       Vector<5> a = makeVector(1,2,3,4,5);
       Extract the three elements starting from element 2
       a.slice(2,3)[0] = 17;  /// a -> [1 2 17 4 5]


Friends And Related Function Documentation

template<int Size>
Vector< Size > operator- ( const Vector< Size > &  v  )  [related]

Unary minus.

Returns the negative of the argument.

 Vector<4> a = 1,2,3,4;
 Vector<4> b = -a; // b will now be [-1 -2 -3 -4]

template<int Size>
Vector< Size > operator+ ( const Vector< Size > &  lhs,
const Vector< Size > &  rhs 
) [related]

Add two vectors.

Returns the sum of the two vectors.

 Vector<4> a = 1,2,3,4;
 Vector<4> b = 5,5,5,5;
 Vector<4> c = a + b;  // c will now be [6 7 8 9]

template<int Size>
Vector< Size > & operator+= ( Vector< Size > &  lhs,
const Vector< Size > &  rhs 
) [related]

Add a vector to another one.

Returns the modified vector.

 Vector<4> a = 1,2,3,4;
 Vector<4> b = 5,5,5,5;
 a += b;   // a will now be [6 7 8 9]

template<int Size>
Vector< Size > operator- ( const Vector< Size > &  lhs,
const Vector< Size > &  rhs 
) [related]

Subtract two vectors.

Returns the dfference of the two vectors.

 Vector<4> a = 1,2,3,4;
 Vector<4> b = 5,5,5,5;
 Vector<4> c = b + a;  // c will now be [4 3 2 1]

template<int Size>
Vector< Size > & operator-= ( Vector< Size > &  lhs,
const Vector< Size > &  rhs 
) [related]

Subtract one vector from another one.

Returns the modified vector.

 Vector<4> a = 1,2,3,4;
 Vector<4> b = 5,5,5,5;
 a -= b;   // a will now be [-4 -3 -2 -1]

template<int Size>
Vector< Size > operator * ( const Vector< Size > &  lhs,
double  rhs 
) [related]

Multiply a vector by a scalar.

Both left- and right- multiply are allowed.

 Vector<4> a = 1,2,3,4;
 Vector<4> b = a * 2.0;  // now b = [2 4 6 8]

template<int Size>
Vector< Size > operator * ( double  lhs,
const Vector< Size > &  rhs 
) [related]

Multiply a vector by a scalar.

Both left- and right- multiply are allowed.

 Vector<4> a = 1,2,3,4;
 Vector<4> b = 3.0 * a;  // now b = [3 6 9 12]

template<int Size>
Vector< Size > & operator *= ( const Vector< Size > &  lhs,
const double &  rhs 
) [related]

Multiply a vector by a scalar.

Returns the modified vector.

 Vector<4> a = 1,2,3,4;
 a *= 2.0;     // now a = [2 4 6 8]

template<int Size>
Vector< Size > operator/ ( const Vector< Size > &  lhs,
double  rhs 
) [related]

Divide a vector by a scalar.

 Vector<4> a = 1,2,3,4;
 Vector<4> b = a / 2.0;  // now b = [0.5 1.0 1.5 2.0]

template<int Size>
Vector< Size > & operator/= ( const Vector< Size > &  lhs,
const double &  rhs 
) [related]

Divide vector by a scalar.

Returns the modified vector.

 Vector<4> a = 1,2,3,4;
 a /= 2.0;     // now a = [0.5 1.0 1.5 2.0]

template<int Size>
double operator * ( const Vector< Size > &  lhs,
const Vector< Size > &  rhs 
) [related]

The dot product of two vectors.

 Vector<4> a = 1,2,3,4;
 Vector<4> b = 5,6,7,8;
 double d = a*b;  // now d = 1*5 + 2*6 + 3*7 + 4*8 = 70

template<int Size = Dynamic, typename Precision = DefaultPrecision, typename Base = Internal::VBase>
Vector< 3 > operator^ ( const Vector< 3 > &  lhs,
const Vector< 3 > &  rhs 
) [related]

The cross product of two 3-vectors.

The vectors must be fixed-size.

 Vector<3> a = 1,2,3;
 Vector<3> b = 5,6,7;
 double d = a^b;  // now d = [-4 8 4]

template<int Size>
Vector< Size > diagmult ( const Vector< Size > &  lhs,
const Vector< Size > &  rhs 
) [related]

Multiply two vectors, treating the one vector as representing a diagonal matricx.

This has the effect of an element-by-element multiplication of two vectors, i.e. if $a$, $b$ and $c$ are three (equally-sized) vectors, $c_i = a_i b_i$.


Generated on Thu May 7 20:28:42 2009 for TooN by  doxygen 1.5.3