This is a templatized class for viewing data as a vector.
A vector, in general, does not have its own data. Instead, it views a block of
data in a certain way. It is therefore possible to have multiple vectors that
view the same data, or even to have a mixture of vectors and matrices that all
view the same data. Class T must have a public default
constructor, copy constructor, destructor, and assignment operator. Many of the
operators and member functions will only work properly for types that behave
like standard numeric types.
Vector<T>()This creates an empty vector.
Vector<T>(size_t n)This constructor creates a vector of n elements. If
T is a standard numeric type, pointer, or any type without a
constructor, including std::complex<T>, then the vector will
be initialized to zero. If not, then the elements are initialized with a
default constructed object. This constructor uses the
Zero<T>() function to obtain the value to initialize to. Use
of this contructor is therefore equivelent to Vector<T>
x(Zero<T>(), n).
Vector<T>(const T& a, size_t n)This creates a vector of size n, with each element initialized
to the value a. This initialization is done through the copy
constructor of class T.
Vector<T>(NoInit, size_t n)This creates a vector of size n, with each element initialized
with the default constructor of class T. If class T
has a specialized version of the VMTraits<T> structure, with
the flag is_simple set to true, then the elements
will not be initialized at all. This flag is set for all the standard numeric
types, including std::complex<float> and
std::complex<double>.
Vector<T>(const T* a, size_t n, ptrdiff_t s = 1)This creates a vector of size n from a C-style array given by
the pointer a. The data is copied with the copy constructor of
type T, and it is up to the user to make sure the array actually
has n elements. The optional parameter s allows for
copying from arrays with strides other than one.
Vector<T>(const T& start, const T& incr,
size_t n)This creates a vector of length n, such that the zeroth element
is set to start, and each element after is incremented by
incr. Copying of these values is done with the copy constructor of
type T.
Vector<T>(const Vector<T>& rhs)The copy constructor makes a shallow copy of the vector rhs.
This means that it views the same data as rhs, in exactly the same
way. The primary purpose of this constructor is for making permanent versions
of returned temporary vectors, for example:
Vec_DP x(4.5, 10), y(2.5, 10);
Vec_DP z(x + y);
Vector<complex<T> >(const Vector<T>& re, const
Vector<T>& im)This creates a complex vector from two real vectors. It will only work
properly for the following types: float and
double.
Vector<complex<T> >(const Vector<T>& re, const
T& im)This creates a complex vector from a real vector, and a scalar. It also only works for the types listed above.
Vector<T>& apply(T (*fn)(T x))This function applies the function fn to each element of the
vector it is called for. The function must take one argument of type
T, and return a value that can be cast to type T.
This member function returns itself by reference, allowing for cascading.
Vector<T>& apply(T (*fn)(const T& x))This function applies the function fn to each element of the
vector it is called for. The function must take one argument of type
T&, and return a value that can be cast to type
T.This member function returns itself by reference, allowing for
cascading.
Vector<T> copy()This function returns a deep copy of the vector it is called for. This means
that the returned vector will be the only object referencing its data, and that
its stride will be one. Copying is done with the copy constructor of type
T.
bool isdeap() constReturns true if the following criteria are met:
If any of these criteria are not met, false is returned.
void makedeep()This function causes a vector to become a deep copy of itself. It now
references its own data, and has a stride of one. Its original data is not
changed, although it is deallocated if the vector it is called for is the only
object referencing it. If new memory is allocated, then copying is done with
the copy constructor of type T.
void reference(const Vector<T>& x)This causes the vector it is called for to become a shallow copy of
x. This is very useful for setting a vector to a returned
temporary, for example:
Vec_INT x(2, 10), y;
y.reference(x + 2);
void free()This makes a vector size zero, deallocating any memory it is using if it is the only object referencing its data.
void reshape(size_t n)This changes the size of the vector to n. The vector no longer
views its old data. Instead, it has new data of its own. The new values are
initialized with the Zero<T>() function, using the copy
constructor of type T.
void resize(size_t n)The same as reshape(), except that the data is copied. If the
vector is made shorter, then the copied data is truncated. If it is made
longer, then the new values will be initialized with the
Zero<T>() function. Copying is done with the copy
constructor of type T.
Vector<T> slice(size_t b, size_t n, ptrdiff_t s)
constThis returns a new view of the data that is a slice of the current view.
b is the element that the slice should start with, n
is the number of elements in the new slice, and s is the stride of
the slice, that is, the number of elements that should be incremented by to get
to the next element of the slice. Note that s can be negative. For
example, the following views a vector in reverse order:
Vec_DP x(0.0, 0.5, 10);
Vec_DP y = x.slice(9, 10, -1);
Vector<T> real() constThis returns the real part of a complex<T> vector. Note
that this returns a deep copy. It is not possible to view the real and
imaginary parts of complex vectors as real vectors. This function is only
defined for complex<float> and
complex<double>. Attempting to use it with other complex
types, or with non-complex types, will almost certainly result in compiler
errors.
Vector<T> imag() constThis returns the imaginary part of a complex<T> vector.
The same limitations apply as for real().
size_t size() constThis returns the size of the vector.
ptrdiff_t stride() constThis returns the stride of the vector.
T* data()This returns a pointer to the beginning of the vector's data. It is up to the user to keep track of the stride.
const T* data() constThe constant version of this member function returns a constant pointer.
T sum() constThis returns the sum of the elements of the vector using the +=
operator of type T. The vector must not be empty.
T min() constThis returns the smallest element in the vector, using the <
operator of type T. The vector must not be empty.
T max() constThis returns the largest element in the vector, using the <
operator of type T. The vector must not be empty.
Vector<T> rotate(ptrdiff_t n) constThis returns a copy of the original vector rotated by n points.
This means that R[i] = V[(i + n) % V.size()], where V
is the original vector, and R is the rotated vector.
Vector<T> delta() constThis returns a vector such that D[i] = V[i + 1] - V[i]. The
last point is given by D[n - 1] = V[0] - V[n - 1], as though the
vecter were periodic.
Vector<T> cumsum() constThis returns the cumulitive sum of the vector. Each element of the new vector is the sum of all elements of the original vector up to, and including, that element.
Matrix<T> rowmat() constThis creates a matrix view of the vector's data. The data is represented as
a matrix with one row, and n columns, where n is the
size of the vector.
Matrix<T> colmat() constThis creates a matrix view of the vector's data. The data is represented as
a matrix with one column and n rows, where n is the
size of the vector.
Matrix<T> matrix(size_t b, size_t n, size_t m, ptrdiff_t rs,
ptrdiff_t cs) constThis creates a matrix view of the vector's data. The data is represented as
a matrix with n rows and m columns, where
b is the index of element (0, 0) of the created
matrix, rs is the number of elements between adjacent rows, and
cs is the number of elements between adjacent columns.
void sort()This sorts the elements of the vector using the
Vector<T>::iterator class, and the Standard Template
Library function std::sort(Vector<T>::iterator a,
Vector<T>::iterator b). T must have an available
operator<().
Vector<T>::iterator begin()This returns an iterator to the beginning of the vector.
Vector<T>::iterator end()This returns an iterator to the end of the vector.
Vector<T>::const_iterator begin() constThis returns a constant iterator to the beginning of the vector.
Vector<T>::const_iterator end() constThis returns a constant iterator to the end of the vector.
void read(istream& in)This reads the contents of a stream into the vector. The vector is reshaped
to a base size of 1024, and if this is insufficient for reading in the entire
stream, the vector is resized by a factor of two, and so on. When the full
stream is read in, the vector is resized, if necessary, to the number of
elements read. This results in the stream being read to the end, so you may
need to use the clear() member function if you wish to continue
using the stream.
void read(istream& in, size_t ncount)This reads the contents of a stream into the vector. The vector is reshaped
to size ncount, and the stream is read in until either the end of
the stream is reached, or until ncount elements have been read.
When the full stream is read in, the vector is resized, if necessary, to the
number of elements read. This function may result in the stream being read to
the end, so you may need to use the clear() member function if you
wish to continue using the stream.
void write(ostream& out) constThis outputs the contents of the vector to a stream. The elements are separated with a carriage return delimiter.
template <class G> Vector<T>& fill(const G&
gen)This function fills the vector with values from the generator
gen. Class G must have a function call operator which
takes no arguments, and returns a value that can be cast to type
T. This includes the four random number generator classes
BaseGen, IntGen, UniformGen, and
NormalGen. Ordinary function pointers can be passed as well, but
note that the compiler will not be able to resolve the function if it is
overloaded. This member function returns itself by reference, allowing for
cascading.
template <class G> Vector<T>& fill2(const F&
fn)This function applies the function object fn to each element of
the vector it is called for. Class F must have a function call
operator which takes one argument of type T, and returns a value
that can be cast to type T. Ordinary function pointers can be
passed as well, but note that the compiler will not be able to resolve the
function if it is overloaded. This member function returns itself by reference,
allowing for cascading.
template <class U> Vector<U> convert(const U& a)
constThis creates a vector of type U from a vector of type
T. It must be possible to explicitly cast type T to
type U, where T is the type of the original vector.
a is a dummy value that is used simply to tell the compiler what
type to convert to. Example:
Vec_DP x(4.5, 10);
Vec_SP y = x.convert(float());
template <class U> Vector<U> shallow_cast(const U& a)
constThis creates a shallow copy of the vector it is called for, but of type
Vector<U>. This is done using pointer casting, which means
the new vector views the same data as the original, but views it as data of
type U instead of as type T. For example, the
internal representation if int and long is the same
on many platforms. This function allows you to view a vector of type
int as a vector of type long, without making a
seperate copy of the data. If used for non-standard types with non-trivial
constructors, this function has the potential to cause some serious bugs, since
the wrong destructor may be called to destroy the vector elements. Types
T and U must have the same size.
Vector<T>& operator=(const Vector<T>&
rhs)This uses the assignment operator of type T to do an
element-by-element copy of rhs to the vector it is called for,
which is the left hand side of the expression. The vectors must be the same
size.
Vector<T>& operator=(const T& rhs)This uses the assignment operator of type T to copy
rhs to each element of the vector it is called for.
Vector<T>& operator+=(const Vector<T>& rhs)
Vector<T>& operator-=(const Vector<T>& rhs)
Vector<T>& operator*=(const Vector<T>& rhs)
Vector<T>& operator/=(const Vector<T>& rhs)
Vector<T>& operator%=(const Vector<T>& rhs)
Vector<T>& operator&=(const Vector<T>& rhs)
Vector<T>& operator^=(const Vector<T>& rhs)
Vector<T>& operator|=(const Vector<T>& rhs)
Vector<T>& operator>>=(const Vector<T>& rhs)
Vector<T>& operator<<=(const Vector<T>& rhs)
These operators use the arithmetic assignment operators of type
T to do element-by-element arithmetic assignment. The vectors must
be the same size.
Vector<T>& operator+=(const T& rhs)
Vector<T>& operator-=(const T& rhs)
Vector<T>& operator*=(const T& rhs)
Vector<T>& operator/=(const T& rhs)
Vector<T>& operator%=(const T& rhs)
Vector<T>& operator&=(const T& rhs)
Vector<T>& operator^=(const T& rhs)
Vector<T>& operator|=(const T& rhs)
Vector<T>& operator>>=(const T& rhs)
Vector<T>& operator<<=(const T& rhs)
These operators use the arithmetic assignment operators of type
T to perform the corresponding operation on each element of the
vector.
Vector<T> operator+() constThis returns a vector such that each element is the result of applying the
unary + operator to each element of the original vector. This
operator must be available for type T.
Vector<T> operator-() constThis returns a vector such that each element is the negation of the original
value. The unary - operator must be available for type
T.
Vector<bool> operator!() constThis returns a vector of bool values such that B[i] =
!V[i], where B is the returned vector, and V
is the original vector. This operator must be available for type
T.
Vector<T> operator~() constThis returns a vector by applying the ~ operator to each
element. This operator must be available for type T.
T& operator[](size_t i)This returns a reference to the ith element of the vector.
const T& operator[](size_t i) constThe constant version of this operator returns a constant reference.
These functions mimic the Standard Library functions of the same name, performing the associated operation on every element of the vector.
Vector<T> abs(const Vector<T>& x)
Vector<T> abs(const Vector<complex<T> >& x)
Vector<T> acos(const Vector<T>& x)
Vector<T> asin(const Vector<T>& x)
Vector<T> atan(const Vector<T>& x)
Vector<T> atan2(const Vector<T>& x, const
Vector<T>& y)
Vector<T> atan2(const Vector<T>& x, const T& y)
Vector<T> atan2(const T& x, const Vector<T>& y)
Vector<T> cos(const Vector<T>& x)
Vector<T> cosh(const Vector<T>& x)
Vector<T> exp(const Vector<T>& x)
Vector<T> log(const Vector<T>& x)
Vector<T> log10(const Vector<T>& x)
Vector<T> pow(const Vector<T>& x, const Vector<T>&
y)
Vector<T> pow(const Vector<T>& x, const T& y)
Vector<T> pow(const T& x, const Vector<T>& y)
Vector<T> sin(const Vector<T>& x)
Vector<T> sinh(const Vector<T>& x)
Vector<T> sqrt(const Vector<T>& x)
Vector<T> tan(const Vector<T>& x)
Vector<T> tanh(const Vector<T>& x)
Vector<T> arg(const Vector<complex<T> >& x)
Vector<T> norm(const Vector<complex<T> >& x)
Vector<complex<T> > polar(const Vector<T>& rho, const
Vector<T>& theta)
Vector<complex<T> > polar(const Vector<T>& rho, const
T& theta)
Vector<complex<T> > polar(const T& rho, const
Vector<T>& theta)
Vector<complex<T> > conj(const Vector<complex<T>
>& x)
Vector<T> real(const Vector<complex<T> >& x)
Vector<T> imag(const Vector<complex<T> >& x)
Note that the real() and imag() functions only
work for complex types with a specialization of the
VMTraits<T> structure. Similarly, the complex version of the
abs() function only works for these types. Attempting to use these
functions for other types will probably result in compiler errors.
These operators perform the associated operation element by element. The two vectors must have the same size. The result is a new vector with the appropriate values.
Vector<T> operator+(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<T> operator-(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<T> operator*(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<T> operator/(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<T> operator%(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<T> operator&(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<T> operator^(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<T> operator|(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<T> operator<<(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<T> operator>>(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<bool> operator==(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<bool> operator!=(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<bool> operator<(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<bool> operator>(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<bool> operator<=(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<bool> operator>=(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<bool> operator&&(const Vector<T>& lhs, const
Vector<T>& rhs)
Vector<bool> operator||(const Vector<T>& lhs, const
Vector<T>& rhs)
These operators perform the associated operation element-by-element with a scalar. The result is a new vector with the appropriate values.
Vector<T> operator+(const Vector<T>& lhs, const T&
a)
Vector<T> operator+(const T& a, const Vector<T>&
rhs)
Vector<T> operator-(const Vector<T>& lhs, const T&
a)
Vector<T> operator-(const T& a, const Vector<T>&
rhs)
Vector<T> operator*(const Vector<T>& lhs, const T&
a)
Vector<T> operator*(const T& a, const Vector<T>&
rhs)
Vector<T> operator/(const Vector<T>& lhs, const T&
a)
Vector<T> operator/(const T& a, const Vector<T>&
rhs)
Vector<T> operator%(const Vector<T>& lhs, const T&
a)
Vector<T> operator%(const T& a, const Vector<T>&
rhs)
Vector<T> operator&(const Vector<T>& lhs, const T&
a)
Vector<T> operator&(const T& a, const Vector<T>&
rhs)
Vector<T> operator^(const Vector<T>& lhs, const T&
a)
Vector<T> operator^(const T& a, const Vector<T>&
rhs)
Vector<T> operator|(const Vector<T>& lhs, const T&
a)
Vector<T> operator|(const T& a, const Vector<T>&
rhs)
Vector<T> operator<<(const Vector<T>& lhs, const
T& a)
Vector<T> operator<<(const T& a, const Vector<T>&
rhs)
Vector<T> operator>>(const Vector<T>& lhs, const
T& a)
Vector<T> operator>>(const T& a, const Vector<T>&
rhs)
Vector<bool> operator==(const Vector<T>& lhs, const T&
a)
Vector<bool> operator==(const T& a, const Vector<T>&
rhs)
Vector<bool> operator!=(const Vector<T>& lhs, const T&
a)
Vector<bool> operator!=(const T& a, const Vector<T>&
rhs)
Vector<bool> operator<(const Vector<T>& lhs, const T&
a)
Vector<bool> operator<(const T& a, const Vector<T>&
rhs)
Vector<bool> operator>(const Vector<T>& lhs, const T&
a)
Vector<bool> operator>(const T& a, const Vector<T>&
rhs)
Vector<bool> operator<=(const Vector<T>& lhs, const
T& a)
Vector<bool> operator<=(const T& a, const Vector<T>&
rhs)
Vector<bool> operator>=(const Vector<T>& lhs, const
T& a)
Vector<bool> operator>=(const T& a, const Vector<T>&
rhs)
Vector<bool> operator&&(const Vector<T>& lhs, const
T& a)
Vector<bool> operator&&(const T& a, const
Vector<T>& rhs)
Vector<bool> operator||(const Vector<T>& lhs, const T&
a)
Vector<bool> operator||(const T& a, const Vector<T>&
rhs)
These operators provide insertion and extraction operators for using vectors
with iostreams. The corresponding operators must exist for type
T.
ostream& operator<<(ostream& out, const
Vector<T>& x)This functions identically to the write() member function,
except that it returns a reference to the stream, for cascading.
istream& operator>>(istream& in, Vector<T>&
x)This reads a stream into a vector. Unlike the read() member
function, this does not reshape the vector. It simply reads in from the stream
until either the stream ends, or the end of the vector is reached.