libStatGen Software
1
|
00001 /* 00002 * Copyright (C) 2010 Regents of the University of Michigan 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #ifndef __MATHVECTOR_H__ 00019 #define __MATHVECTOR_H__ 00020 00021 #include "StringBasics.h" 00022 00023 #include <stdio.h> 00024 #include <assert.h> 00025 00026 class Matrix; 00027 00028 class Vector 00029 { 00030 public: 00031 int dim, size; 00032 double * data; 00033 String label; 00034 00035 Vector() 00036 { 00037 Init(); 00038 } 00039 Vector(Vector & v) 00040 { 00041 Init(); 00042 Copy(v); 00043 } 00044 Vector(int d) 00045 { 00046 Init(); 00047 Dimension(d); 00048 } 00049 Vector(const char * text) 00050 { 00051 Init(); 00052 label = text; 00053 } 00054 Vector(const char * text, int d) 00055 { 00056 Init(); 00057 label = text; 00058 Dimension(d); 00059 } 00060 Vector(const char * text, Vector & v) 00061 { 00062 Init(); 00063 label = text; 00064 Copy(v); 00065 } 00066 00067 ~Vector(); 00068 00069 void Dimension(int d); 00070 void Dimension(int d, double value); 00071 00072 void GrowTo(int d) 00073 { 00074 Dimension(d > dim ? d : dim); 00075 } 00076 void GrowTo(int d, double value) 00077 { 00078 Dimension(d > dim ? d : dim, value); 00079 } 00080 00081 int Length() const 00082 { 00083 return dim; 00084 } 00085 00086 void SetLabel(const char * text) 00087 { 00088 label = text; 00089 } 00090 00091 void Zero(); 00092 void Set(double k); 00093 void Set(Vector & v) 00094 { 00095 Copy(v); 00096 }; 00097 void SetMultiple(double k, Vector & v); 00098 00099 void Negate(); 00100 void Add(double n); 00101 void Multiply(double k); 00102 00103 double InnerProduct(Vector & v); 00104 void Copy(const Vector & v); 00105 void Add(Vector & v); 00106 void AddMultiple(double k, Vector & v); 00107 void Subtract(Vector & v); 00108 00109 void Product(Matrix & m, Vector & v); 00110 00111 double & operator [](int n) 00112 { 00113 assert(n < dim); 00114 return data[n]; 00115 } 00116 double operator [](int n) const 00117 { 00118 assert(n < dim); 00119 return data[n]; 00120 } 00121 00122 double operator [](double fraction) 00123 { 00124 return data[(int)(dim * fraction)]; 00125 } 00126 double & operator [](double fraction) const 00127 { 00128 return data[(int)(dim * fraction)]; 00129 } 00130 00131 Vector & operator = (const Vector & v); 00132 bool operator == (const Vector & v) const; 00133 bool operator != (const Vector & v) const 00134 { 00135 return !(*this == v); 00136 } 00137 00138 void Swap(int i, int j) 00139 { 00140 double swap = data[i]; 00141 data[i] = data[j]; 00142 data[j] = swap; 00143 } 00144 void Swap(Vector & rhs); 00145 00146 Vector & operator *= (double rhs) 00147 { 00148 Multiply(rhs); 00149 return *this; 00150 } 00151 Vector & operator += (double rhs) 00152 { 00153 Add(rhs); 00154 return *this; 00155 } 00156 Vector & operator -= (double rhs) 00157 { 00158 return *this += -rhs; 00159 } 00160 Vector & operator /= (double rhs) 00161 { 00162 return *this *= 1/rhs; 00163 } 00164 00165 Vector & operator += (Vector & rhs) 00166 { 00167 Add(rhs); 00168 return * this; 00169 } 00170 Vector & operator -= (Vector & rhs) 00171 { 00172 Subtract(rhs); 00173 return * this; 00174 } 00175 00176 void DeleteDimension(int n); 00177 void Delete(int n) 00178 { 00179 DeleteDimension(n); 00180 } 00181 void Insert(int n, double value); 00182 00183 // Calculates average and variance 00184 void AveVar(double & ave, double & var) const; 00185 double Average() const; 00186 double Var() const; 00187 double StandardDeviation() const; 00188 00189 double Average(double returnIfNull); 00190 double Var(double returnIfNull); 00191 double StandardDeviation(double returnIfNull); 00192 00193 // Common descriptive functions 00194 double Sum() const; 00195 double SumSquares() const; 00196 double Product() const; 00197 00198 // Find extreme values 00199 double Min() const; 00200 double Max() const; 00201 00202 // Return the number of elements in a subset 00203 int CountIfGreater(double treshold) const; 00204 int CountIfGreaterOrEqual(double treshold) const; 00205 00206 // Append another vector to the end 00207 void Stack(const Vector & v); 00208 00209 void Print(int maxDim = -1) 00210 { 00211 Print(stdout, maxDim); 00212 } 00213 void Print(FILE * output, int maxDim = -1); 00214 00215 // Routines for creating and searching through sorted vectors 00216 void Sort(); 00217 void Reverse(); 00218 void Sort(Vector & freeRider); 00219 int BinarySearch(double value); 00220 int FastFind(double value) 00221 { 00222 return BinarySearch(value); 00223 } 00224 00225 // Remove consecutive duplicate elements from vector 00226 void RemoveDuplicates(); 00227 00228 // Query first and last elements 00229 // 00230 00231 double & First() 00232 { 00233 return data[0]; 00234 } 00235 double & Last() 00236 { 00237 return data[dim - 1]; 00238 } 00239 00240 // Routines for using a vector as a stack of doubles 00241 // 00242 00243 void Clear() 00244 { 00245 dim = 0; 00246 } 00247 void Push(double value); 00248 double Pop() 00249 { 00250 return data[--dim]; 00251 } 00252 double Peek() const 00253 { 00254 return data[dim-1]; 00255 } 00256 00257 // This routine adds items to a sorted list 00258 // 00259 00260 void InsertInSortedList(int item); 00261 00262 static int alloc; 00263 00264 bool isAscending(); 00265 bool isDescending(); 00266 00267 // Routines for dealing with vectors that include missing data 00268 // 00269 00270 int SafeCount() const; 00271 double SafeMin() const; 00272 double SafeMax() const; 00273 00274 private: 00275 static int CompareDouble(const double * a, const double * b); 00276 void Init(); 00277 }; 00278 00279 00280 00281 class VectorFunc 00282 // Wrapper for multi-dimensional functions 00283 // so that they can be used as parameters 00284 // and keep private data 00285 { 00286 private: 00287 double(*f)(Vector &); 00288 00289 public: 00290 // Constructors 00291 VectorFunc(); 00292 VectorFunc(double(*func)(Vector &)); 00293 00294 // Virtual destructor ensures that dynamic objects are 00295 // handled correctly 00296 virtual ~VectorFunc() { } 00297 00298 virtual double Evaluate(Vector & v); 00299 00300 // Calculate derivatives along each direction. Delta is a guess value 00301 // for the initial stepsize in numerical derivation 00302 virtual void Derivative(Vector & point, Vector & d, double delta = 1.0); 00303 00304 // Minimum function value found while evaluating derivative 00305 // and its location... 00306 double dfmin; 00307 Vector dpmin; 00308 }; 00309 00310 #endif 00311 00312 00313