libStatGen Software 1
Vector Class Reference
Collaboration diagram for Vector:

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
 
Vectoroperator= (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)
 
Vectoroperator*= (double rhs)
 
Vectoroperator+= (double rhs)
 
Vectoroperator-= (double rhs)
 
Vectoroperator/= (double rhs)
 
Vectoroperator+= (Vector &rhs)
 
Vectoroperator-= (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.

36 {
37 Init();
38 }

◆ Vector() [2/6]

Vector::Vector ( Vector v)
inline

Definition at line 39 of file MathVector.h.

40 {
41 Init();
42 Copy(v);
43 }

◆ Vector() [3/6]

Vector::Vector ( int  d)
inline

Definition at line 44 of file MathVector.h.

45 {
46 Init();
47 Dimension(d);
48 }

◆ Vector() [4/6]

Vector::Vector ( const char *  text)
inline

Definition at line 49 of file MathVector.h.

50 {
51 Init();
52 label = text;
53 }

◆ Vector() [5/6]

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

Definition at line 54 of file MathVector.h.

55 {
56 Init();
57 label = text;
58 Dimension(d);
59 }

◆ Vector() [6/6]

Vector::Vector ( const char *  text,
Vector v 
)
inline

Definition at line 60 of file MathVector.h.

61 {
62 Init();
63 label = text;
64 Copy(v);
65 }

◆ ~Vector()

Vector::~Vector ( )

Definition at line 40 of file MathVector.cpp.

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

Member Function Documentation

◆ Add() [1/2]

void Vector::Add ( double  n)

Definition at line 96 of file MathVector.cpp.

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

◆ Add() [2/2]

void Vector::Add ( Vector v)

Definition at line 123 of file MathVector.cpp.

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

◆ AddMultiple()

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

Definition at line 134 of file MathVector.cpp.

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

◆ Average() [1/2]

double Vector::Average ( ) const

Definition at line 224 of file MathVector.cpp.

225{
226 if (dim == 0)
227 error("Average undefined for null vector %s",
228 (const char *) label);
229
230 return Sum() / dim;
231}

◆ Average() [2/2]

double Vector::Average ( double  returnIfNull)

Definition at line 663 of file MathVector.cpp.

664{
665 if (Length() == 0)
666 return returnIfNull;
667
668 return Average();
669}

◆ AveVar()

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

Definition at line 263 of file MathVector.cpp.

264{
265 // uses a two pass method to correct for
266 // round-off errors
267
268 if (dim == 0)
269 error("Average and Variance undefined for null vector %s",
270 (const char *) label);
271
272 double s, ep;
273
274 ave = var = ep = 0.0;
275
276 for (int j=0; j<dim; j++)
277 ave += data[j];
278
279 ave /= dim;
280
281 for (int j=0; j<dim; j++)
282 {
283 s = data[j] - ave;
284 ep += s;
285 var += s*s;
286 }
287
288 if (dim > 1)
289 var = (var - ep*ep/dim)/(dim-1);
290}

◆ BinarySearch()

int Vector::BinarySearch ( double  value)

Definition at line 336 of file MathVector.cpp.

337{
338 void * pointer = ::BinarySearch
339 (&element, data, dim, sizeof(double), COMPAREFUNC CompareDouble);
340
341 if (pointer == NULL)
342 return -1;
343
344 return ((double *) pointer) - data;
345}

◆ Clear()

void Vector::Clear ( )
inline

Definition at line 243 of file MathVector.h.

244 {
245 dim = 0;
246 }

◆ Copy()

void Vector::Copy ( const Vector v)

Definition at line 108 of file MathVector.cpp.

109{
110 Dimension(v.dim);
111
112 if (v.data != NULL)
113 for (int i=0; i < dim; i++)
114 data[i] = v.data[i];
115}

◆ CountIfGreater()

int Vector::CountIfGreater ( double  treshold) const

Definition at line 371 of file MathVector.cpp.

372{
373 int count = 0;
374
375 for (int i = 0; i < dim; i++)
376 if (data[i] > threshold)
377 count++;
378
379 return count;
380}

◆ CountIfGreaterOrEqual()

int Vector::CountIfGreaterOrEqual ( double  treshold) const

Definition at line 382 of file MathVector.cpp.

383{
384 int count = 0;
385
386 for (int i = 0; i < dim; i++)
387 if (data[i] >= treshold)
388 count++;
389
390 return count;
391}

◆ Delete()

void Vector::Delete ( int  n)
inline

Definition at line 177 of file MathVector.h.

178 {
179 DeleteDimension(n);
180 }

◆ DeleteDimension()

void Vector::DeleteDimension ( int  n)

Definition at line 201 of file MathVector.cpp.

202{
203 for (int i = n; i < dim - 1; i++)
204 data[i] = data[i + 1];
205 dim --;
206}

◆ Dimension() [1/2]

void Vector::Dimension ( int  d)

Definition at line 46 of file MathVector.cpp.

47{
48 if (d > size)
49 {
50 if (size < 1024)
51 {
52 size = (d + alloc) / alloc * alloc;
53 double * newData = new double [size];
54 if (data != NULL)
55 {
56 for (int i = 0; i < dim; i++)
57 newData[i] = data[i];
58 delete [] data;
59 }
60 data = newData;
61 }
62 else
63 {
64 while (size <= d)
65 size *= 2;
66
67 double * newData = new double [size];
68 if (data != NULL)
69 {
70 for (int i = 0; i < dim; i++)
71 newData[i] = data[i];
72 delete [] data;
73 }
74 data = newData;
75 }
76 }
77 dim = d;
78}

◆ Dimension() [2/2]

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

Definition at line 80 of file MathVector.cpp.

81{
82 int original = dim;
83
84 Dimension(d);
85
86 for (int i = original; i < dim; i++)
87 data[i] = value;
88}

◆ FastFind()

int Vector::FastFind ( double  value)
inline

Definition at line 220 of file MathVector.h.

221 {
222 return BinarySearch(value);
223 }

◆ First()

double & Vector::First ( )
inline

Definition at line 231 of file MathVector.h.

232 {
233 return data[0];
234 }

◆ GrowTo() [1/2]

void Vector::GrowTo ( int  d)
inline

Definition at line 72 of file MathVector.h.

73 {
74 Dimension(d > dim ? d : dim);
75 }

◆ GrowTo() [2/2]

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

Definition at line 76 of file MathVector.h.

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

◆ InnerProduct()

double Vector::InnerProduct ( Vector v)

Definition at line 178 of file MathVector.cpp.

179{
180 if (dim != v.dim)
181 error("Vector::InnerProduct - vectors have different dimensions\n"
182 "Vectors - %s[%d] * %s[%d] ",
183 (const char *) label, dim, (const char *) v.label, v.dim);
184
185 double sum = 0.0;
186 for (int i = 0; i < dim; i++)
187 sum += data[i] * v.data[i];
188
189 return sum;
190}

◆ Insert()

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

Definition at line 192 of file MathVector.cpp.

193{
194 Dimension(dim + 1);
195
196 for (int i = dim - 1; i > n; i--)
197 data[i] = data[i - 1];
198 data[n] = value;
199}

◆ InsertInSortedList()

void Vector::InsertInSortedList ( int  item)

Definition at line 625 of file MathVector.cpp.

626{
627 // Skip through large elements
628 int pos = dim - 1;
629
630 while (pos >= 0 && data[pos] > value)
631 pos--;
632
633 // If the value is already in the list, we are done
634 if (pos >= 0 && data[pos] == value)
635 return;
636
637 // Otherwise we need to grow array
638 Dimension(dim + 1);
639
640 // And then shift larger elements to the right
641 pos++;
642 for (int i = dim - 1; i > pos; i--)
643 data[i] = data[i - 1];
644
645 data[pos] = value;
646}

◆ isAscending()

bool Vector::isAscending ( )

Definition at line 446 of file MathVector.cpp.

447{
448 for (int i = 1; i < dim; i++)
449 if (data[i] < data[i - 1])
450 return false;
451 return true;
452}

◆ isDescending()

bool Vector::isDescending ( )

Definition at line 454 of file MathVector.cpp.

455{
456 for (int i = 1; i < dim; i++)
457 if (data[i] > data[i - 1])
458 return false;
459 return true;
460}

◆ Last()

double & Vector::Last ( )
inline

Definition at line 235 of file MathVector.h.

236 {
237 return data[dim - 1];
238 }

◆ Length()

int Vector::Length ( ) const
inline

Definition at line 81 of file MathVector.h.

82 {
83 return dim;
84 }

◆ Max()

double Vector::Max ( ) const

Definition at line 410 of file MathVector.cpp.

411{
412 if (dim == 0)
413 return 0.0;
414
415 double max = data[0];
416
417 for (int i = 1; i < dim; i++)
418 if (data[i] > max)
419 max = data[i];
420
421 return max;
422}

◆ Min()

double Vector::Min ( ) const

Definition at line 396 of file MathVector.cpp.

397{
398 if (dim == 0)
399 return 0.0;
400
401 double min = data[0];
402
403 for (int i = 1; i < dim; i++)
404 if (data[i] < min)
405 min = data[i];
406
407 return min;
408}

◆ Multiply()

void Vector::Multiply ( double  k)

Definition at line 102 of file MathVector.cpp.

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

◆ Negate()

void Vector::Negate ( )

Definition at line 90 of file MathVector.cpp.

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

◆ operator!=()

bool Vector::operator!= ( const Vector v) const
inline

Definition at line 133 of file MathVector.h.

134 {
135 return !(*this == v);
136 }

◆ operator*=()

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

Definition at line 146 of file MathVector.h.

147 {
148 Multiply(rhs);
149 return *this;
150 }

◆ operator+=() [1/2]

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

Definition at line 151 of file MathVector.h.

152 {
153 Add(rhs);
154 return *this;
155 }

◆ operator+=() [2/2]

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

Definition at line 165 of file MathVector.h.

166 {
167 Add(rhs);
168 return * this;
169 }

◆ operator-=() [1/2]

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

Definition at line 156 of file MathVector.h.

157 {
158 return *this += -rhs;
159 }

◆ operator-=() [2/2]

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

Definition at line 170 of file MathVector.h.

171 {
172 Subtract(rhs);
173 return * this;
174 }

◆ operator/=()

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

Definition at line 160 of file MathVector.h.

161 {
162 return *this *= 1/rhs;
163 }

◆ operator=()

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

Definition at line 117 of file MathVector.cpp.

118{
119 Copy(rhs);
120 return *this;
121}

◆ operator==()

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

Definition at line 358 of file MathVector.cpp.

359{
360 if (rhs.dim != dim) return false;
361
362 for (int i = 0; i < dim; i++)
363 if (data[i] != rhs[i])
364 return false;
365 return true;
366}

◆ operator[]() [1/4]

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

Definition at line 122 of file MathVector.h.

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

◆ operator[]() [2/4]

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

Definition at line 126 of file MathVector.h.

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

◆ operator[]() [3/4]

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

Definition at line 111 of file MathVector.h.

112 {
113 assert(n < dim);
114 return data[n];
115 }

◆ operator[]() [4/4]

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

Definition at line 116 of file MathVector.h.

117 {
118 assert(n < dim);
119 return data[n];
120 }

◆ Peek()

double Vector::Peek ( ) const
inline

Definition at line 252 of file MathVector.h.

253 {
254 return data[dim-1];
255 }

◆ Pop()

double Vector::Pop ( )
inline

Definition at line 248 of file MathVector.h.

249 {
250 return data[--dim];
251 }

◆ Print() [1/2]

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

Definition at line 308 of file MathVector.cpp.

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

◆ Print() [2/2]

void Vector::Print ( int  maxDim = -1)
inline

Definition at line 209 of file MathVector.h.

210 {
211 Print(stdout, maxDim);
212 }

◆ Product() [1/2]

double Vector::Product ( ) const

Definition at line 233 of file MathVector.cpp.

234{
235 double product = 1.0;
236
237 for (int j = 0; j < dim; j++)
238 product *= data[j];
239
240 return product;
241}

◆ Product() [2/2]

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

Definition at line 208 of file MathVector.cpp.

209{
210 if (m.cols != v.dim)
211 error("Vector::Product - Cannot Multiply Matrix by Vector\n"
212 "Vectors - %s [%d, %d] * %s [%d]\n",
213 (const char *) m.label, m.rows, m.cols,
214 (const char *) v.label, v.dim);
215
216 Dimension(m.rows);
217 Zero();
218
219 for (int i = 0; i < m.rows; i++)
220 for (int j = 0; j < m.cols; j++)
221 data[i] += m[i][j] * v[j];
222}

◆ Push()

void Vector::Push ( double  value)

Definition at line 427 of file MathVector.cpp.

428{
429 Dimension(dim + 1);
430 data[dim - 1] = value;
431}

◆ RemoveDuplicates()

void Vector::RemoveDuplicates ( )

Definition at line 347 of file MathVector.cpp.

348{
349 int out = 0;
350
351 for (int in = 1; in < Length(); in++)
352 if (data[in] != data[out])
353 data[++out] = data[in];
354
355 Dimension(out + 1);
356}

◆ Reverse()

void Vector::Reverse ( )

Definition at line 619 of file MathVector.cpp.

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

◆ SafeCount()

int Vector::SafeCount ( ) const

Definition at line 570 of file MathVector.cpp.

571{
572 int nonMissing = dim;
573
574 for (int i = 0; i < dim; i++)
575 if (data[i] == _NAN_)
576 nonMissing--;
577
578 return nonMissing;
579}

◆ SafeMax()

double Vector::SafeMax ( ) const

Definition at line 600 of file MathVector.cpp.

601{
602 double max = _NAN_;
603 int i;
604
605 for (i = 0; i < dim; i++)
606 if (data[i] != _NAN_)
607 {
608 max = data[i];
609 break;
610 }
611
612 for (; i < dim; i++)
613 if (data[i] != _NAN_ && data[i] > max)
614 max = data[i];
615
616 return max;
617}

◆ SafeMin()

double Vector::SafeMin ( ) const

Definition at line 581 of file MathVector.cpp.

582{
583 double min = _NAN_;
584 int i;
585
586 for (i = 0; i < dim; i++)
587 if (data[i] != _NAN_)
588 {
589 min = data[i];
590 break;
591 }
592
593 for (; i < dim; i++)
594 if (data[i] != _NAN_ && data[i] < min)
595 min = data[i];
596
597 return min;
598}

◆ Set() [1/2]

void Vector::Set ( double  k)

Definition at line 164 of file MathVector.cpp.

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

◆ Set() [2/2]

void Vector::Set ( Vector v)
inline

Definition at line 93 of file MathVector.h.

94 {
95 Copy(v);
96 };

◆ SetLabel()

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

Definition at line 86 of file MathVector.h.

87 {
88 label = text;
89 }

◆ SetMultiple()

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

Definition at line 170 of file MathVector.cpp.

171{
172 Dimension(v.dim);
173
174 for (int i = 0; i < dim; i++)
175 data[i] = k * v[i];
176}

◆ Sort() [1/2]

void Vector::Sort ( )

Definition at line 325 of file MathVector.cpp.

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

◆ Sort() [2/2]

void Vector::Sort ( Vector freeRider)

Definition at line 330 of file MathVector.cpp.

331{
332 QuickSort2(data, freeRider.data, dim, sizeof(double),
333 COMPAREFUNC CompareDouble);
334}

◆ Stack()

void Vector::Stack ( const Vector v)

Definition at line 433 of file MathVector.cpp.

434{
435 int end = dim;
436
437 Dimension(dim + v.dim);
438
439 for (int i = 0; i < v.dim; i++)
440 data[i + end] = v[i];
441}

◆ StandardDeviation() [1/2]

double Vector::StandardDeviation ( ) const

Definition at line 299 of file MathVector.cpp.

300{
301 double var = Var();
302
303 if (var < 0.0) var = 0.0;
304
305 return sqrt(var);
306}

◆ StandardDeviation() [2/2]

double Vector::StandardDeviation ( double  returnIfNull)

Definition at line 679 of file MathVector.cpp.

680{
681 if (Length() == 0)
682 return returnIfNull;
683
684 return StandardDeviation();
685}

◆ Subtract()

void Vector::Subtract ( Vector v)

Definition at line 146 of file MathVector.cpp.

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

◆ Sum()

double Vector::Sum ( ) const

Definition at line 243 of file MathVector.cpp.

244{
245 double sum = 0.0;
246
247 for (int j=0; j<dim; j++)
248 sum += data[j];
249
250 return sum;
251}

◆ SumSquares()

double Vector::SumSquares ( ) const

Definition at line 253 of file MathVector.cpp.

254{
255 double sum = 0.0;
256
257 for (int j=0; j<dim; j++)
258 sum += data[j] * data[j];
259
260 return sum;
261}

◆ Swap() [1/2]

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

Definition at line 138 of file MathVector.h.

139 {
140 double swap = data[i];
141 data[i] = data[j];
142 data[j] = swap;
143 }

◆ Swap() [2/2]

void Vector::Swap ( Vector rhs)

Definition at line 648 of file MathVector.cpp.

649{
650 double * temp = rhs.data;
651 rhs.data = data;
652 data = temp;
653
654 int swap = rhs.dim;
655 rhs.dim = dim;
656 dim = swap;
657
658 swap = rhs.size;
659 rhs.size = size;
660 size = swap;
661}

◆ Var() [1/2]

double Vector::Var ( ) const

Definition at line 292 of file MathVector.cpp.

293{
294 double mean, var;
295 AveVar(mean, var);
296 return var;
297}

◆ Var() [2/2]

double Vector::Var ( double  returnIfNull)

Definition at line 671 of file MathVector.cpp.

672{
673 if (Length() == 0)
674 return returnIfNull;
675
676 return Var();
677}

◆ Zero()

void Vector::Zero ( )

Definition at line 158 of file MathVector.cpp.

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

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: