The VecMat software provides many global support functions and classes for use with vectors and matrices. They are provided in the header files "vm_traits.h", "vm_tools.h", and "vm_io.h".
This is a numeric traits structure used to provide information about various types. It's default definition is:
template <class T>
struct VMTraits
{
typedef T real_type;
enum {is_specialized = false};
enum {is_simple = false};
};
The real_type type is used for the complex constructors, and
for the abs() function, to determine the real base of a complex
type. The is_specialized flag should be true for any
type that has a specialization of this structure. The is_simple
flag should only be true for types without constructors and
destructors, or classes for which the default constructor and destructor don't
do anything. If this flag is set, then the NoInit vector and
matrix constructors will not initialize the values of their elements. If it is
not set, then they will initialize their elements with the default constructor
of type T. Note that this is still different that using the
Vector<T>(size_t n) and Matrix<T>(size_t n,
size_t m) constructors, because they initialize their elements with the
copy constructor, making each element a copy of a default constructed object.
For most classes this distinction is not important, though. If the
is_simple flag is set, then the elements will not be destroyed by
the vector or matrix destructors either. This improves the efficiency of
working with standard numeric types, where it is not necessary to go through
the trouble of looping through the data, and calling the destructor for each
element.
The following types have specializations of this structure:
bool, char, signed char, unsigned
char, short, unsigned short, int,
unsigned int, long, unsigned long,
float, double,
std::complex<float>, and
std::complex<double>.
This support class allows you to construct histograms of vectors. The constructor allows you to set the minimum and maximum values of the histogram to be produced, and the number of bins to be used.
explicit Histogram(double low = 0, double high = 0, size_t bins =
0)Initializes the histogram to a range, and sets the number of bins. If the optional range parameters are not set, then the class will determine these values from the vector that is histogrammed. If the number of bins is not set, or is set to zero, then the number of bins is calculated according to
n = exp(0.626 + 0.41 * log(m - 1)
where m is the number of elements in the histogrammed vector
falling in the closed interval (low, high).
Histogram(const Histogram& hist)The copy constructor copies the parameters that are set for
hist.
Histogram& operator=(const Histogram& hist)The assignment operator copies the parameters from one histogram to another.
void set_range(double low, double high)Use this function to set, or reset the range of an already constructed
histogram object. If low is not less than high, then
this sets the object to determine the values from the vector to be
histogrammed.
void set_bins(size_t bins)Use this to set, or reset the number of bins to be used for histogramming.
If bins is zero, then this sets the object to determine the
number of bins from the vector to be histogrammed.
double low() constReturns the low edge of the range.
double high() constReturns the high edge of the range.
size_t bins() constReturns the number of bins the object is set to.
template <class T> Vector<int> make_hist(const
Vector<T>& x) constThis returns a vector of integers indicating the number of elements found in
each bin of the histogram. The range corresponding to each bin is simply
(high - low) / bins. Thus bin n corresponds to the
range:
low + n * (high - low) / bins
through
low + (n + 1) * (high - low) / bins
This function object compares two integers by comparing the elements of a
vector with indices given by those integers. This function object is used by
the support function Index() to create a vector of integers sorted
according to the ordering that would sort the vector passed to the
function.
IndirectComp<T>()The default constructor just creates the object. A vector can be attached
later using the attach() member function.
IndirectComp<T>(const IndirectComp<T>&
icomp)The copy constructor attaches the vector attached to icomp to
the new object.
IndirectComp<T>(const Vector<T>& x)This constructor attaches the vector x to the function object.
This means that the IndirectComp object contains an iterator to
the beginning of the vector. If this function object is explicitely used, keep
in mind that the internal iterator will be invalidated if anything happens to
the attached vector that would cause its view to be changed.
void attach(const Vector<T>& x)This attaches a vector to the function object, by storing an iterator to the beginning of the vector.
bool operator()(int a, int b) constThe function call operator takes two integers, a and
b, and returns true if x[a] < x[b],
or false otherwise. Note that if either a or
b is negative, or greater than the size of the attached vector,
then this will result in the vector being indexed out of bounds.
template <class T> T Zero()This function returns an object of type T. For each instantiation of this
function, a static object of the appropriate type is created. The function
returns a copy of this object. The static object is decared in the function
without an explicit initialization. This means that the memory occupied by the
object is first initialized with zeros, and then the default constructor is
called (assuming it is a class with a constructor). This means that any
standard types, including pointers and complex numbers, will be set to zero.
Any classes or structures without constructors will have all their data members
set to zero, and any classes with constructors will be constructed with their
default constructor. This function is used by the Vector<T>
and Matrix<T> classes for initialization when no specific
value is indicated.
Note that this template function has no arguments, so the template parameter must be specified when the function is called. For example:
string s = Zero<string>();
void ReadBinary(const string& filename, Vector<T>&
array, size_t skip)This reads a binary file into a vector. Like the read() member
function, this function reshapes the vector to the appropriate size. The file
should not be open when the function is called. The parameter
skip, indicates the number of bytes that should be skipped before
reading begins.
void WriteBinary(const string& filename, const
Vector<T>& array)This outputs a vector to a binary file. The file should not be open when the function is called.
void ReadBinary(const string& filename, Matrix<T>&
array, size_t skip)This reads a binary file into a matrix. Like the read() member
function, this function reshapes the matrix to a single row of the appropriate
size. The file should not be open when the function is called. The parameter
skip, indicates the number of bytes that should be skipped before
reading begins.
void WriteBinary(const string& filename, const
Matrix<T>& array)This outputs a matrix to a binary file. The data is output in row major format, meaning that the zeroth row is output sequentially, followed by the first row, etc... The file should not be open when the function is called.
void Stat(const Vector<T>& x, double& m, double&
var)Calculates the mean and variance of the vector x.
T must be a real type promotable to double. The mean
and variance are stored in m and var,
respectively.
Vector<T> Sort(const Vector<T>& x)This uses the std::sort() routine to do an out-of-place sort on
x. This will often be faster than doing an in-place sort with the
sort() member function, because this guarantees that standard
pointers will be used instead of iterators.
Vector<int> Index(const Vector<T>& x)This returns a vector of integers indx, such that
x[indx[I]] gives the vector x in sorted order. This
function uses the std::sort() routine, along with the support
function object IndirectComp<T> to do the sorting. The
sorting is done with standard pointers.
Matrix<T> MatMult(const Matrix<T>& x, const
Matrix<T>& y)This performs standard matrix multiplication. The result is an n X
m matrix, where n is the number of rows in x,
and m is the number of columns in y.
Vector<T> MatMult(const Vector<T>& x, const
Matrix<T>& y)This performs matrix multiplication between a vector and a matrix. The
vector is treated as a matrix with one row. The result is a vector whose size
is equal to the number of columns in y.
Vector<T> MatMult(const Matrix<T>& x, const
Vector<T>& y)This performs matrix multiplication between a matrix and a vector. The
vector is treated as a matrix with one column. The result is a vector whose
size is equal to the number of rows in x.
Matrix<T> MatMult(const Vector<T>& x, const
Vector<T>& y)This calculates the direct product of two vectors. The result is an n
X m matrix, where n is the size of x, and
m is the size of y.
T DotProduct(const Vector<T>& x, const Vector<T>&
y)This calculates the scalar product of two vectors. Both vectors must be the same size.