Vector Class Reference
Collaboration diagram for Vector:
[legend]

## Public Member Functions

| Vector (Vector &v)
| Vector (int d)
| Vector (const char *text)
| Vector (const char *text, int d)
| Vector (const char *text, Vector &v)
void | Dimension (int d)
void | Dimension (int d, double value)
void | GrowTo (int d)
void | GrowTo (int d, double value)
int | Length () const
void | SetLabel (const char *text)
void | Zero ()
void | Set (double k)
void | Set (Vector &v)
void | SetMultiple (double k, Vector &v)
void | Negate ()
void | Add (double n)
void | Multiply (double k)
double | InnerProduct (Vector &v)
void | Copy (const Vector &v)
void | Add (Vector &v)
void | AddMultiple (double k, Vector &v)
void | Subtract (Vector &v)
void | Product (Matrix &m, Vector &v)
double & | operator[] (int n)
double | operator[] (int n) const
double | operator[] (double fraction)
double & | operator[] (double fraction) const
Vector & | operator= (const Vector &v)
bool | operator== (const Vector &v) const
bool | operator!= (const Vector &v) const
void | Swap (int i, int j)
void | Swap (Vector &rhs)
Vector & | operator*= (double rhs)
Vector & | operator+= (double rhs)
Vector & | operator-= (double rhs)
Vector & | operator/= (double rhs)
Vector & | operator+= (Vector &rhs)
Vector & | operator-= (Vector &rhs)
void | DeleteDimension (int n)
void | Delete (int n)
void | Insert (int n, double value)
void | AveVar (double &ave, double &var) const
double | Average () const
double | Var () const
double | StandardDeviation () const
double | Average (double returnIfNull)
double | Var (double returnIfNull)
double | StandardDeviation (double returnIfNull)
double | Sum () const
double | SumSquares () const
double | Product () const
double | Min () const
double | Max () const
int | CountIfGreater (double treshold) const
int | CountIfGreaterOrEqual (double treshold) const
void | Stack (const Vector &v)
void | Print (int maxDim=-1)
void | Print (FILE *output, int maxDim=-1)
void | Sort ()
void | Reverse ()
void | Sort (Vector &freeRider)
int | BinarySearch (double value)
int | FastFind (double value)
void | RemoveDuplicates ()
double & | First ()
double & | Last ()
void | Clear ()
void | Push (double value)
double | Pop ()
double | Peek () const
void | InsertInSortedList (int item)
bool | isAscending ()
bool | isDescending ()
int | SafeCount () const
double | SafeMin () const
double | SafeMax () const

## Public Attributes

int | dim
int | size
double * | data
String | label

## Static Public Attributes

static int | alloc = 32

## Detailed Description

Definition at line 28 of file MathVector.h.

## Constructor & Destructor Documentation

## Vector() [1/6]

Vector::Vector | ( |  | ) |

inline

Definition at line 35 of file MathVector.h.

{
Init();
}

## Vector() [2/6]

Vector::Vector | ( | Vector & | v | ) |

inline

Definition at line 39 of file MathVector.h.

{
Init();
Copy(v);
}

## Vector() [3/6]

Vector::Vector | ( | int | d | ) |

inline

Definition at line 44 of file MathVector.h.

{
Init();
Dimension(d);
}

## Vector() [4/6]

Vector::Vector | ( | const char * | text | ) |

inline

Definition at line 49 of file MathVector.h.

{
Init();
label = text;
}

## Vector() [5/6]

Vector::Vector | ( | const char * | text,
|  | int | d
| ) |  |

inline

Definition at line 54 of file MathVector.h.

{
Init();
label = text;
Dimension(d);
}

## Vector() [6/6]

Vector::Vector | ( | const char * | text,
|  | Vector & | v
| ) |  |

inline

Definition at line 60 of file MathVector.h.

{
Init();
label = text;
Copy(v);
}

## ~Vector()

Vector::~Vector | ( |  | ) |

Definition at line 40 of file MathVector.cpp.

{
// printf(" Deleting vector %s ...\n", (const char *) label);
if (data != NULL) delete [] data;
}

## Member Function Documentation

## Add() [1/2]

void Vector::Add | ( | double | n | ) |

Definition at line 96 of file MathVector.cpp.

{
for (int i = 0; i< dim; i++)
data[i] += n;
}

## Add() [2/2]

void Vector::Add | ( | Vector & | v | ) |

Definition at line 123 of file MathVector.cpp.

{
if (dim != v.dim)
error("Vector::Add - vectors have different dimensions\n"
"Vectors - %s [%d] + %s [%d] ",
(const char *) label, dim, (const char *) v.label, v.dim);
for (int i = 0; i < dim; i++)
data[i] += v.data[i];
}

## AddMultiple()

void Vector::AddMultiple | ( | double | k,
|  | Vector & | v
| ) |  |

Definition at line 134 of file MathVector.cpp.

{
if (dim != v.dim)
error("Vector::AddMultiple - vectors are incompatible\n"
"Vectors - %s [%d] + %s [%d] ",
(const char *) label, dim, (const char *) v.label, v.dim);
for (int i = 0; i < dim; i++)
data[i] += k * v.data[i];
}

## Average() [1/2]

double Vector::Average | ( |  | ) | const

Definition at line 224 of file MathVector.cpp.

{
if (dim == 0)
error("Average undefined for null vector %s",
(const char *) label);
return Sum() / dim;
}

## Average() [2/2]

double Vector::Average | ( | double | returnIfNull | ) |

Definition at line 663 of file MathVector.cpp.

{
if (Length() == 0)
return returnIfNull;
return Average();
}

## AveVar()

void Vector::AveVar | ( | double & | ave,
|  | double & | var
| ) |  | const

Definition at line 263 of file MathVector.cpp.

{
// uses a two pass method to correct for
// round-off errors
if (dim == 0)
error("Average and Variance undefined for null vector %s",
(const char *) label);
double s, ep;
ave = var = ep = 0.0;
for (int j=0; j<dim; j++)
ave += data[j];
ave /= dim;
for (int j=0; j<dim; j++)
{
s = data[j] - ave;
ep += s;
var += s*s;
}
if (dim > 1)
var = (var - ep*ep/dim)/(dim-1);
}

## BinarySearch()

int Vector::BinarySearch | ( | double | value | ) |

Definition at line 336 of file MathVector.cpp.

{
void * pointer = ::BinarySearch
(&element, data, dim, sizeof(double), COMPAREFUNC CompareDouble);
if (pointer == NULL)
return -1;
return ((double *) pointer) - data;
}

## Clear()

void Vector::Clear | ( |  | ) |

inline

Definition at line 243 of file MathVector.h.

{
dim = 0;
}

## Copy()

void Vector::Copy | ( | const Vector & | v | ) |

Definition at line 108 of file MathVector.cpp.

{
Dimension(v.dim);
if (v.data != NULL)
for (int i=0; i < dim; i++)
data[i] = v.data[i];
}

## CountIfGreater()

int Vector::CountIfGreater | ( | double | treshold | ) | const

Definition at line 371 of file MathVector.cpp.

{
int count = 0;
for (int i = 0; i < dim; i++)
if (data[i] > threshold)
count++;
return count;
}

## CountIfGreaterOrEqual()

int Vector::CountIfGreaterOrEqual | ( | double | treshold | ) | const

Definition at line 382 of file MathVector.cpp.

{
int count = 0;
for (int i = 0; i < dim; i++)
if (data[i] >= treshold)
count++;
return count;
}

## Delete()

void Vector::Delete | ( | int | n | ) |

inline

Definition at line 177 of file MathVector.h.

{
DeleteDimension(n);
}

## DeleteDimension()

void Vector::DeleteDimension | ( | int | n | ) |

Definition at line 201 of file MathVector.cpp.

{
for (int i = n; i < dim - 1; i++)
data[i] = data[i + 1];
dim --;
}

## Dimension() [1/2]

void Vector::Dimension | ( | int | d | ) |

Definition at line 46 of file MathVector.cpp.

{
if (d > size)
{
if (size < 1024)
{
size = (d + alloc) / alloc * alloc;
double * newData = new double [size];
if (data != NULL)
{
for (int i = 0; i < dim; i++)
newData[i] = data[i];
delete [] data;
}
data = newData;
}
else
{
while (size <= d)
size *= 2;
double * newData = new double [size];
if (data != NULL)
{
for (int i = 0; i < dim; i++)
newData[i] = data[i];
delete [] data;
}
data = newData;
}
}
dim = d;
}

## Dimension() [2/2]

void Vector::Dimension | ( | int | d,
|  | double | value
| ) |  |

Definition at line 80 of file MathVector.cpp.

{
int original = dim;
Dimension(d);
for (int i = original; i < dim; i++)
data[i] = value;
}

## FastFind()

int Vector::FastFind | ( | double | value | ) |

inline

Definition at line 220 of file MathVector.h.

{
return BinarySearch(value);
}

## First()

double & Vector::First | ( |  | ) |

inline

Definition at line 231 of file MathVector.h.

{
return data[0];
}

## GrowTo() [1/2]

void Vector::GrowTo | ( | int | d | ) |

inline

Definition at line 72 of file MathVector.h.

{
Dimension(d > dim ? d : dim);
}

## GrowTo() [2/2]

void Vector::GrowTo | ( | int | d,
|  | double | value
| ) |  |

inline

Definition at line 76 of file MathVector.h.

{
Dimension(d > dim ? d : dim, value);
}

## InnerProduct()

double Vector::InnerProduct | ( | Vector & | v | ) |

Definition at line 178 of file MathVector.cpp.

{
if (dim != v.dim)
error("Vector::InnerProduct - vectors have different dimensions\n"
"Vectors - %s[%d] * %s[%d] ",
(const char *) label, dim, (const char *) v.label, v.dim);
double sum = 0.0;
for (int i = 0; i < dim; i++)
sum += data[i] * v.data[i];
return sum;
}

## Insert()

void Vector::Insert | ( | int | n,
|  | double | value
| ) |  |

Definition at line 192 of file MathVector.cpp.

{
Dimension(dim + 1);
for (int i = dim - 1; i > n; i--)
data[i] = data[i - 1];
data[n] = value;
}

## InsertInSortedList()

void Vector::InsertInSortedList | ( | int | item | ) |

Definition at line 625 of file MathVector.cpp.

{
// Skip through large elements
int pos = dim - 1;
while (pos >= 0 && data[pos] > value)
pos--;
// If the value is already in the list, we are done
if (pos >= 0 && data[pos] == value)
return;
// Otherwise we need to grow array
Dimension(dim + 1);
// And then shift larger elements to the right
pos++;
for (int i = dim - 1; i > pos; i--)
data[i] = data[i - 1];
data[pos] = value;
}

## isAscending()

bool Vector::isAscending | ( |  | ) |

Definition at line 446 of file MathVector.cpp.

{
for (int i = 1; i < dim; i++)
if (data[i] < data[i - 1])
return false;
return true;
}

## isDescending()

bool Vector::isDescending | ( |  | ) |

Definition at line 454 of file MathVector.cpp.

{
for (int i = 1; i < dim; i++)
if (data[i] > data[i - 1])
return false;
return true;
}

## Last()

double & Vector::Last | ( |  | ) |

inline

Definition at line 235 of file MathVector.h.

{
return data[dim - 1];
}

## Length()

int Vector::Length | ( |  | ) | const

inline

Definition at line 81 of file MathVector.h.

{
return dim;
}

## Max()

double Vector::Max | ( |  | ) | const

Definition at line 410 of file MathVector.cpp.

{
if (dim == 0)
return 0.0;
double max = data[0];
for (int i = 1; i < dim; i++)
if (data[i] > max)
max = data[i];
return max;
}

## Min()

double Vector::Min | ( |  | ) | const

Definition at line 396 of file MathVector.cpp.

{
if (dim == 0)
return 0.0;
double min = data[0];
for (int i = 1; i < dim; i++)
if (data[i] < min)
min = data[i];
return min;
}

## Multiply()

void Vector::Multiply | ( | double | k | ) |

Definition at line 102 of file MathVector.cpp.

{
for (int i = 0; i < dim; i++)
data[i] *= k;
}

## Negate()

void Vector::Negate | ( |  | ) |

Definition at line 90 of file MathVector.cpp.

{
for (int i = 0; i < dim; i++)
data[i] = -data[i];
}

## operator!=()

bool Vector::operator!= | ( | const Vector & | v | ) | const

inline

Definition at line 133 of file MathVector.h.

{
return !(*this == v);
}

## operator*=()

Vector & Vector::operator*= | ( | double | rhs | ) |

inline

Definition at line 146 of file MathVector.h.

{
Multiply(rhs);
return *this;
}

## operator+=() [1/2]

Vector & Vector::operator+= | ( | double | rhs | ) |

inline

Definition at line 151 of file MathVector.h.

{
Add(rhs);
return *this;
}

## operator+=() [2/2]

Vector & Vector::operator+= | ( | Vector & | rhs | ) |

inline

Definition at line 165 of file MathVector.h.

{
Add(rhs);
return * this;
}

## operator-=() [1/2]

Vector & Vector::operator-= | ( | double | rhs | ) |

inline

Definition at line 156 of file MathVector.h.

{
return *this += -rhs;
}

## operator-=() [2/2]

Vector & Vector::operator-= | ( | Vector & | rhs | ) |

inline

Definition at line 170 of file MathVector.h.

{
Subtract(rhs);
return * this;
}

## operator/=()

Vector & Vector::operator/= | ( | double | rhs | ) |

inline

Definition at line 160 of file MathVector.h.

{
return *this *= 1/rhs;
}

## operator=()

Vector & Vector::operator= | ( | const Vector & | v | ) |

Definition at line 117 of file MathVector.cpp.

{
Copy(rhs);
return *this;
}

## operator==()

bool Vector::operator== | ( | const Vector & | v | ) | const

Definition at line 358 of file MathVector.cpp.

{
if (rhs.dim != dim) return false;
for (int i = 0; i < dim; i++)
if (data[i] != rhs[i])
return false;
return true;
}

## operator[]() [1/4]

double Vector::operator[] | ( | double | fraction | ) |

inline

Definition at line 122 of file MathVector.h.

{
return data[(int)(dim * fraction)];
}

## operator[]() [2/4]

double & Vector::operator[] | ( | double | fraction | ) | const

inline

Definition at line 126 of file MathVector.h.

{
return data[(int)(dim * fraction)];
}

## operator[]() [3/4]

double & Vector::operator[] | ( | int | n | ) |

inline

Definition at line 111 of file MathVector.h.

{
assert(n < dim);
return data[n];
}

## operator[]() [4/4]

double Vector::operator[] | ( | int | n | ) | const

inline

Definition at line 116 of file MathVector.h.

{
assert(n < dim);
return data[n];
}

## Peek()

double Vector::Peek | ( |  | ) | const

inline

Definition at line 252 of file MathVector.h.

{
return data[dim-1];
}

## Pop()

double Vector::Pop | ( |  | ) |

inline

Definition at line 248 of file MathVector.h.

{
return data[--dim];
}

## Print() [1/2]

void Vector::Print | ( | FILE * | output,
|  | int | maxDim = `-1`
| ) |  |

Definition at line 308 of file MathVector.cpp.

{
if (d == -1 || d > dim) d = dim;
fprintf(f, "%.15s : ", (const char *) label);
for (int i = 0; i < d; i++)
fprintf(f, "%7.3f ", data[i]);
fprintf(f, "\n");
}

## Print() [2/2]

void Vector::Print | ( | int | maxDim = `-1` | ) |

inline

Definition at line 209 of file MathVector.h.

{
Print(stdout, maxDim);
}

## Product() [1/2]

double Vector::Product | ( |  | ) | const

Definition at line 233 of file MathVector.cpp.

{
double product = 1.0;
for (int j = 0; j < dim; j++)
product *= data[j];
return product;
}

## Product() [2/2]

void Vector::Product | ( | Matrix & | m,
|  | Vector & | v
| ) |  |

Definition at line 208 of file MathVector.cpp.

{
if (m.cols != v.dim)
error("Vector::Product - Cannot Multiply Matrix by Vector\n"
"Vectors - %s [%d, %d] * %s [%d]\n",
(const char *) m.label, m.rows, m.cols,
(const char *) v.label, v.dim);
Dimension(m.rows);
Zero();
for (int i = 0; i < m.rows; i++)
for (int j = 0; j < m.cols; j++)
data[i] += m[i][j] * v[j];
}

## Push()

void Vector::Push | ( | double | value | ) |

Definition at line 427 of file MathVector.cpp.

{
Dimension(dim + 1);
data[dim - 1] = value;
}

## RemoveDuplicates()

void Vector::RemoveDuplicates | ( |  | ) |

Definition at line 347 of file MathVector.cpp.

{
int out = 0;
for (int in = 1; in < Length(); in++)
if (data[in] != data[out])
data[++out] = data[in];
Dimension(out + 1);
}

## Reverse()

void Vector::Reverse | ( |  | ) |

Definition at line 619 of file MathVector.cpp.

{
for (int i = 0, j = dim - 1; i < j; i++, j--)
Swap(i, j);
}

## SafeCount()

int Vector::SafeCount | ( |  | ) | const

Definition at line 570 of file MathVector.cpp.

{
int nonMissing = dim;
for (int i = 0; i < dim; i++)
if (data[i] == _NAN_)
nonMissing--;
return nonMissing;
}

## SafeMax()

double Vector::SafeMax | ( |  | ) | const

Definition at line 600 of file MathVector.cpp.

{
double max = _NAN_;
int i;
for (i = 0; i < dim; i++)
if (data[i] != _NAN_)
{
max = data[i];
break;
}
for (; i < dim; i++)
if (data[i] != _NAN_ && data[i] > max)
max = data[i];
return max;
}

## SafeMin()

double Vector::SafeMin | ( |  | ) | const

Definition at line 581 of file MathVector.cpp.

{
double min = _NAN_;
int i;
for (i = 0; i < dim; i++)
if (data[i] != _NAN_)
{
min = data[i];
break;
}
for (; i < dim; i++)
if (data[i] != _NAN_ && data[i] < min)
min = data[i];
return min;
}

## Set() [1/2]

void Vector::Set | ( | double | k | ) |

Definition at line 164 of file MathVector.cpp.

{
for (int i = 0; i < dim; i++)
data[i] = k;
}

## Set() [2/2]

void Vector::Set | ( | Vector & | v | ) |

inline

Definition at line 93 of file MathVector.h.

{
Copy(v);
};

## SetLabel()

void Vector::SetLabel | ( | const char * | text | ) |

inline

Definition at line 86 of file MathVector.h.

{
label = text;
}

## SetMultiple()

void Vector::SetMultiple | ( | double | k,
|  | Vector & | v
| ) |  |

Definition at line 170 of file MathVector.cpp.

{
Dimension(v.dim);
for (int i = 0; i < dim; i++)
data[i] = k * v[i];
}

## Sort() [1/2]

void Vector::Sort | ( |  | ) |

Definition at line 325 of file MathVector.cpp.

{
QuickSort(data, dim, sizeof(double), COMPAREFUNC CompareDouble);
}

## Sort() [2/2]

void Vector::Sort | ( | Vector & | freeRider | ) |

Definition at line 330 of file MathVector.cpp.

{
QuickSort2(data, freeRider.data, dim, sizeof(double),
COMPAREFUNC CompareDouble);
}

## Stack()

void Vector::Stack | ( | const Vector & | v | ) |

Definition at line 433 of file MathVector.cpp.

{
int end = dim;
Dimension(dim + v.dim);
for (int i = 0; i < v.dim; i++)
data[i + end] = v[i];
}

## StandardDeviation() [1/2]

double Vector::StandardDeviation | ( |  | ) | const

Definition at line 299 of file MathVector.cpp.

{
double var = Var();
if (var < 0.0) var = 0.0;
return sqrt(var);
}

## StandardDeviation() [2/2]

double Vector::StandardDeviation | ( | double | returnIfNull | ) |

Definition at line 679 of file MathVector.cpp.

{
if (Length() == 0)
return returnIfNull;
return StandardDeviation();
}

## Subtract()

void Vector::Subtract | ( | Vector & | v | ) |

Definition at line 146 of file MathVector.cpp.

{
if (dim != v.dim)
error("Vector::Subtract - vectors have different dimensions\n"
"Vectors - %s [%d] + %s [%d] ",
(const char *) label, dim, (const char *) v.label, v.dim);
for (int i = 0; i < dim; i++)
data[i] -= v.data[i];
}

## Sum()

double Vector::Sum | ( |  | ) | const

Definition at line 243 of file MathVector.cpp.

{
double sum = 0.0;
for (int j=0; j<dim; j++)
sum += data[j];
return sum;
}

## SumSquares()

double Vector::SumSquares | ( |  | ) | const

Definition at line 253 of file MathVector.cpp.

{
double sum = 0.0;
for (int j=0; j<dim; j++)
sum += data[j] * data[j];
return sum;
}

## Swap() [1/2]

void Vector::Swap | ( | int | i,
|  | int | j
| ) |  |

inline

Definition at line 138 of file MathVector.h.

{
double swap = data[i];
data[i] = data[j];
data[j] = swap;
}

## Swap() [2/2]

void Vector::Swap | ( | Vector & | rhs | ) |

Definition at line 648 of file MathVector.cpp.

{
double * temp = rhs.data;
rhs.data = data;
data = temp;
int swap = rhs.dim;
rhs.dim = dim;
dim = swap;
swap = rhs.size;
rhs.size = size;
size = swap;
}

## Var() [1/2]

double Vector::Var | ( |  | ) | const

Definition at line 292 of file MathVector.cpp.

{
double mean, var;
AveVar(mean, var);
return var;
}

## Var() [2/2]

double Vector::Var | ( | double | returnIfNull | ) |

Definition at line 671 of file MathVector.cpp.

{
if (Length() == 0)
return returnIfNull;
return Var();
}

## Zero()

void Vector::Zero | ( |  | ) |

Definition at line 158 of file MathVector.cpp.

{
for (int i = 0; i < dim; i++)
data[i] = 0.0;
}

## Member Data Documentation

## alloc

int Vector::alloc = 32

static

Definition at line 262 of file MathVector.h.

## data

double* Vector::data

Definition at line 32 of file MathVector.h.

## dim

int Vector::dim

Definition at line 31 of file MathVector.h.

## label

String Vector::label

Definition at line 33 of file MathVector.h.

## size

int Vector::size

Definition at line 31 of file MathVector.h.

The documentation for this class was generated from the following files:

- general/MathVector.h
- general/MathVector.cpp