📄 vector.h
字号:
///Vector.h and .cpp
#ifndef __VECTOR_H__
#define __VECTOR_H__
// System includes
#include <iostream>
#include <fstream>
#include <math.h>
namespace Flood
{
/// Forward declaration of Matrix template
template <class Type> class Matrix;
/// This template class defines a vector for general purpose use.
///
/// @see Matrix.
template <class Type>
class Vector
{
private:
/// Size of vector.
int size;
/// Pointer to a Type.
Type* vector;
public:
// CONSTRUCTORS
Vector(void);
Vector(int);
Vector(int, const Type&);
Vector(int, const Type*);
Vector(const Vector&);
// ASSINGMENT OPERATOR
Vector& operator=(const Vector&);
// REFERENCE OPERATORS
inline Type& operator[](const int);
inline const Type& operator[](const int) const;
// METHODS
inline int getSize(void);
inline void setSize(int);
inline void resize(int);
inline void fillAtRandom(void);
inline void fillAtRandom(double, double);
inline Type calculateMean(void);
inline Type calculateStandardDeviation(void);
inline Type calculateMinimum(void);
inline Type calculateMaximum(void);
inline int calculateMinimalIndex(void);
inline int calculateMaximalIndex(void);
inline double calculateNorm(void);
inline Vector<Type> operator+(Type);
inline Vector<Type> operator+(Vector<Type>);
inline Vector<Type> operator-(Type);
inline Vector<Type> operator-(Vector<Type>);
inline Vector<Type> operator*(Type);
inline Type dot(Vector<Type>);
inline Vector<Type> operator*(Vector<Type>);
inline Vector<Type> operator*(Matrix<Type>);
inline Matrix<Type> outer(Vector<Type>);
inline Vector<Type> operator/(Type);
inline Vector<Type> operator/(Vector<Type>);
inline Type* begin();
inline Type* end();
inline void insert(int, Vector<Type>);
inline Vector<Type> extract(int, int);
inline Vector<Type> assemble(Vector<Type>);
void load(char*);
void save(char*);
// DESTRUCTOR
~Vector();
};
// CONSTRUCTORS
/// Default constructor. It creates a vector of size zero.
template <typename Type>
Vector<Type>::Vector(void) : size(0), vector(0)
{
}
/// Constructor. It creates a vector of size n, containing n copies of the default value for Type.
///
/// @param newSize Size of Vector.
template <typename Type>
Vector<Type>::Vector(int newSize) : size(newSize), vector(new Type[newSize])
{
}
/// Constructor. It creates a vector of size n, containing n copies of the type value of Type.
///
/// @param newSize Size of Vector.
/// @param type Value of Type.
template <typename Type> Vector<Type> ::Vector(int newSize, const Type& type)
: size(newSize), vector(new Type[newSize])
{
for(int i = 0; i < newSize; i++)
{
vector[i] = type;
}
}
/// Constructor. It creates a vector of size n, containing n copies of the type value of Type.
///
/// @param newSize Size of Vector.
/// @param type Value of Type.
template <typename Type> Vector<Type>::Vector(int newSize, const Type* type)
: size(newSize), vector(new Type[newSize])
{
for(int i = 0; i < newSize; i++)
{
vector[i] = *type++;
}
}
/// Copy constructor. It creates a copy of an existing Vector.
///
/// @param oldVector Vector to be copied.
template <typename Type> Vector<Type>::Vector(const Vector<Type>& oldVector)
: size(oldVector.size), vector(new Type[size])
{
for(int i = 0; i < size; i++)
{
vector[i] = oldVector[i];
}
}
// ASSIGNMENT OPERATORS
/// Assignment operator. It assigns to self a copy of an existing Vector.
///
/// @param oldVector Vector to be assigned.
template <typename Type>
Vector<Type>& Vector<Type>::operator=(const Vector<Type>& oldVector)
{
if(this != &oldVector)
{
if(size != oldVector.size)
{
if(vector != 0)
{
delete [] (vector);
}
size = oldVector.size;
vector = new Type[size];
}
for(int i = 0; i < size; i++)
{
vector[i] = oldVector[i];
}
}
return(*this);
}
// REFERENCE OPERATORS
/// Reference operator.
template <typename Type>
inline Type& Vector<Type>::operator[](const int i)
{
// Control sentence (if debug)
#ifndef NDEBUG
if(i >= size)
{
std::cerr << std::endl
<< "Flood Error: Vector Template. " << std::endl
<< "Reference operator []." << std::endl
<< "Index is " << i << " and it must be less than " << size << "." << std::endl
<< std::endl;
exit(1);
}
#endif
// Return vector element
return(vector[i]);
}
/// Reference operator.
template <typename Type>
inline const Type& Vector<Type>::operator[](const int i) const
{
// Control sentence (if debug)
#ifndef NDEBUG
if(i >= size)
{
std::cerr << std::endl
<< "Flood Error: Vector Template. " << std::endl
<< "Reference operator []." << std::endl
<< "Index is " << i << " and it must be less than " << size << "." << std::endl
<< std::endl;
exit(1);
}
#endif
return(vector[i]);
}
// METHODS
// int getSize(void) method
/// This method returns the number of elements in the vector.
template <typename Type>
inline int Vector<Type>::getSize()
{
return(size);
}
// void setSize(int) method
/// This method sets a new size in the vector.
/// It also initializes the new vector with the previous values.
///
/// @param newSize New number of elements.
template <typename Type>
inline void Vector<Type>::setSize(int newSize)
{
vector = new Type[newSize]; size = newSize;
}
// void resize(int) method
/// This method sets a new size in the vector.
/// It also initializes the new vector with the previous values.
///
/// @param newSize New number of elements.
template <typename Type>
inline void Vector<Type>::resize(int newSize)
{
if(newSize > size)
{
Vector<Type> newVector(newSize);
for(int i = 0; i < size; i++)
{
newVector[i] = vector[i];
}
vector = new Type[newSize];
for(int i = 0; i < size; i++)
{
vector[i] = newVector[i];
}
size = newSize;
}
else if(newSize < size)
{
Vector<Type> newVector(newSize);
for(int i = 0; i < newSize; i++)
{
newVector[i] = vector[i];
}
vector = new Type[newSize];
for(int i = 0; i < newSize; i++)
{
vector[i] = newVector[i];
}
size = newSize;
}
else
{
// Do nothing
}
}
// void fillAtRandom(void) method
/// This method assigns a random value comprised between -1 and 1 to each element in the vector.
template <class Type>
inline void Vector<Type>::fillAtRandom(void)
{
double random = 0.0;
for(int i = 0; i < size; i++)
{
random = (double)rand()/(RAND_MAX+1.0);
vector[i] = -1.0 + 2.0*random;
}
}
// void fillAtRandom(double, double) method
/// This method assigns a random value comprised between a minimum and a maximum values to each element in the
/// vector.
///
/// @param minimum Minimum filling value.
/// @param maximum Maximum filling value.
template <class Type>
inline void Vector<Type>::fillAtRandom(double minimum, double maximum)
{
// Control sentence (if debug)
#ifndef NDEBUG
if(minimum > maximum)
{
std::cerr << std::endl
<< "Flood Error: Vector Template." << std::endl
<< "void fillAtRandom(double, double) method." << std::endl
<< "Minimum value must be less or equal than maximum value." << std::endl
<< std::endl;
exit(1);
}
#endif
double random = 0.0;
for(int i = 0; i < size; i++)
{
random = (double)rand()/(RAND_MAX+1.0);
vector[i] = minimum + (maximum - minimum)*random;
}
}
// Type calculateMinimum(void) method
/// This method returns the smallest element in the vector.
template <typename Type>
inline Type Vector<Type>::calculateMinimum()
{
Type minimum = vector[0];
for(int i = 1; i < size; i++)
{
if(vector[i] < minimum)
{
minimum = vector[i];
}
}
return(minimum);
}
// Type calculateMaximum(void) method
/// This method returns the largest element in the vector.
template <typename Type>
inline Type Vector<Type>::calculateMaximum()
{
Type maximum = vector[0];
for(int i = 1; i < size; i++)
{
if(vector[i] > maximum)
{
maximum = vector[i];
}
}
return(maximum);
}
// int calculateMinimalIndex(void) method
/// This method returns the index of the smallest element in the vector.
template <typename Type>
inline int Vector<Type>::calculateMinimalIndex()
{
Type minimum = vector[0];
int minimalIndex = 0;
for(int i = 1; i < size; i++)
{
if(vector[i] < minimum)
{
minimum = vector[i];
minimalIndex = i;
}
}
return(minimalIndex);
}
// int calculateMaximalIndex(void) method
/// This method returns the index of the largest element in the vector.
template <typename Type>
inline int Vector<Type>::calculateMaximalIndex()
{
Type maximum = vector[0];
int maximalIndex = 0;
for(int i = 1; i < size; i++)
{
if(vector[i] > maximum)
{
maximum = vector[i];
maximalIndex = i;
}
}
return(maximalIndex);
}
// Type calculateMean(void) method
/// This method returns the mean of the elements in the vector.
template <typename Type>
inline Type Vector<Type>::calculateMean(void)
{
Type mean = 0;
double sum = 0.0;
for(int i = 0; i < size; i++)
{
sum += vector[i];
}
mean = sum/(double)size;
return(mean);
}
// Type calculateStandardDeviation(void) method
/// This method returns the standard deviation of the elements in the vector.
template <typename Type>
inline Type Vector<Type>::calculateStandardDeviation(void)
{
Type standardDeviation = 0;
double mean = calculateMean();
double sum = 0.0;
for(int i = 0; i < size; i++)
{
sum += pow(vector[i] - mean, 2);
}
standardDeviation = sqrt(sum/(double)size);
return(standardDeviation);
}
// double calculateNorm(void) method
/// This element returns the vector norm.
template <typename Type>
inline double Vector<Type>::calculateNorm()
{ // Control sentence (if debug)
#ifndef NDEBUG
if(size == 0)
{
std::cerr << std::endl
<< "Flood Error: Vector Template." << std::endl
<< "double calculateNorm(void) method." << std::endl
<< "Size of vector is zero." << std::endl
<< std::endl;
exit(1);
}
#endif
double norm = 0.0;
for(int i = 0; i < size; i++)
{
norm += vector[i]*vector[i];
}
norm = sqrt(norm);
return(norm);
}
// Vector<Type> operator + (Type) method
/// Sum vector+scalar arithmetic operator.
template <typename Type>
inline Vector<Type> Vector<Type>::operator + (Type scalar)
{
Vector<Type> sum(size);
for(int i = 0; i < size; i++)
{
sum[i] = vector[i] + scalar;
}
return(sum);
}
// Vector<Type> operator + (Vector<Type>)
/// Sum vector+vector arithmetic operator.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -