MathVector.h

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 
00188     double Average(double returnIfNull);
00189     double Var(double returnIfNull);
00190 
00191     // Common descriptive functions
00192     double Sum() const;
00193     double SumSquares() const;
00194     double Product() const;
00195 
00196     // Find extreme values
00197     double Min() const;
00198     double Max() const;
00199 
00200     // Return the number of elements in a subset
00201     int  CountIfGreater(double treshold) const;
00202     int  CountIfGreaterOrEqual(double treshold) const;
00203 
00204     // Append another vector to the end
00205     void Stack(const Vector & v);
00206 
00207     void Print(int maxDim = -1)
00208     {
00209         Print(stdout, maxDim);
00210     }
00211     void Print(FILE * output, int maxDim = -1);
00212 
00213     // Routines for creating and searching through sorted vectors
00214     void Sort();
00215     void Reverse();
00216     void Sort(Vector & freeRider);
00217     int  BinarySearch(double value);
00218     int  FastFind(double value)
00219     {
00220         return BinarySearch(value);
00221     }
00222 
00223     // Remove consecutive duplicate elements from vector
00224     void RemoveDuplicates();
00225 
00226     // Query first and last elements
00227     //
00228 
00229     double & First()
00230     {
00231         return data[0];
00232     }
00233     double & Last()
00234     {
00235         return data[dim - 1];
00236     }
00237 
00238     // Routines for using a vector as a stack of doubles
00239     //
00240 
00241     void   Clear()
00242     {
00243         dim = 0;
00244     }
00245     void   Push(double value);
00246     double Pop()
00247     {
00248         return data[--dim];
00249     }
00250     double Peek() const
00251     {
00252         return data[dim-1];
00253     }
00254 
00255     // This routine adds items to a sorted list
00256     //
00257 
00258     void   InsertInSortedList(int item);
00259 
00260     static int alloc;
00261 
00262     bool   isAscending();
00263     bool   isDescending();
00264 
00265     // Routines for dealing with vectors that include missing data
00266     //
00267 
00268     int SafeCount() const;
00269     double SafeMin() const;
00270     double SafeMax() const;
00271 
00272 private:
00273     static int CompareDouble(const double * a, const double * b);
00274     void Init();
00275 };
00276 
00277 
00278 
00279 class VectorFunc
00280 // Wrapper for multi-dimensional functions
00281 // so that they can be used as parameters
00282 // and keep private data
00283 {
00284 private:
00285     double(*f)(Vector &);
00286 
00287 public:
00288     // Constructors
00289     VectorFunc();
00290     VectorFunc(double(*func)(Vector &));
00291 
00292     // Virtual destructor ensures that dynamic objects are
00293     // handled correctly
00294     virtual ~VectorFunc() { }
00295 
00296     virtual double Evaluate(Vector & v);
00297 
00298     // Calculate derivatives along each direction. Delta is a guess value
00299     // for the initial stepsize in numerical derivation
00300     virtual void   Derivative(Vector & point, Vector & d, double delta = 1.0);
00301 
00302     // Minimum function value found while evaluating derivative
00303     // and its location...
00304     double dfmin;
00305     Vector dpmin;
00306 };
00307 
00308 #endif
00309 
00310 
00311 
Generated on Wed Nov 17 15:38:29 2010 for StatGen Software by  doxygen 1.6.3