vectors.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // Little library of vectors and sparse vectors
00003 // Copyright (C) 2007- Leon Bottou
00004 
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA
00018 
00019 
00020 
00021 // $Id: vectors.h,v 1.13 2007/10/02 20:40:06 cvs Exp $
00022 
00023 #ifndef VECTORS_H
00024 #define VECTORS_H
00025 
00026 #include <cstring>
00027 #include <string>
00028 #include <sstream>
00029 #include <cassert>
00030 #include <iostream>
00031 #include "wrapper.h"
00032 
00033 
00034 class FVector;
00035 class SVector;
00036 
00037 typedef double VFloat;
00038 
00039 class FVector
00040 {
00041 private:
00042   struct Rep
00043   {
00044     int refcount;
00045     int size;
00046     VFloat *data;
00047     Rep() : size(0), data(0) {}
00048     ~Rep() { delete [] data; }
00049     void resize(int n);
00050     Rep *copy();
00051   };
00052   
00053   Wrapper<Rep> w;
00054   Rep *rep() { return w.rep(); }
00055   const Rep *rep() const { return w.rep(); }
00056   void qset(int i, double v);
00057   
00058 public:
00059   FVector();
00060   FVector(int n);
00061   FVector(const SVector &v);
00062   int size() const { return rep()->size; }
00063 
00064   // these accessors are range-checked.
00065   // get() returns 0 when i is out-of-range.
00066   // set() expands the vector.
00067   double get(int i) const;
00068   double set(int i, double v);
00069 
00070   // warning: you can write vector[i] but
00071   // the subscripts are not range-checked!
00072   // on the other hand, that's fast.
00073   operator const VFloat* () const { return rep()->data; }
00074   operator VFloat* () { w.detach(); return rep()->data; }
00075 
00076   void clear();
00077   void resize(int n);
00078   void touch(int i);
00079   FVector slice(int fi, int ti) const;
00080 
00081   void add(double c1);
00082   void add(const FVector &v2);
00083   void add(const SVector &v2);
00084   void add(const FVector &v2, double c2);
00085   void add(const SVector &v2, double c2);
00086   void add(const FVector &v2, double c2, const FVector &q2);
00087   void add(const SVector &v2, double c2, const FVector &q2);
00088   void scale(double c1);
00089   void combine(double c1, const FVector &v2, double c2);
00090   void combine(double c1, const SVector &v2, double c2);
00091 
00092 
00093   friend std::ostream& operator<<(std::ostream &f, const FVector &v);
00094   friend std::istream& operator>>(std::istream &f, FVector &v);
00095   bool save(std::ostream &f) const;
00096   bool load(std::istream &f);
00097   void loadString(std::string str);
00103   int fromPSQL(std::string str);
00108         double getSpaceUsed();
00109   // binary serialization methods
00115   void *write_bin(size_t &total_size);
00122   int read_bin(void *data, size_t length);
00123 };
00124 
00125 
00126 
00127 class SVector
00128 {
00129 public:
00130   struct Pair 
00131   { 
00132     int i; 
00133     VFloat v; 
00134   };
00135 private:
00136   struct Rep
00137   {
00138     int refcount;
00139     int npairs;
00140     int mpairs;
00141     int size;
00142     struct Pair *pairs;
00143     
00144     Rep() : npairs(0), mpairs(-1), size(0), pairs(0) {}
00145     ~Rep() { delete [] pairs; }
00146     void resize(int n);
00147     double qset(int i, double v);
00148     Rep *copy();
00149   };
00150   
00151   Wrapper<Rep> w;
00152   Rep *rep() { return w.rep(); }
00153   const Rep *rep() const { return w.rep(); }
00154   
00155 public:
00156   SVector();
00157   SVector(const FVector &v);
00158   int size() const { return rep()->size; }
00159 
00160   // these accessors are range-checked.
00161   // get() returns 0 when i is out-of-range.
00162   // set() expands the vector.
00163   double get(int i) const;
00164   double set(int i, double v);
00165 
00166   // to quickly iterate over the non-zero coefficients,
00167   // do for(SVector::Pair *p = x; p->i>=0; p++) { ... }
00168   int npairs() const { return rep()->npairs; }
00169   operator const Pair* () const { return rep()->pairs; }
00170 
00171   void clear();
00172   void trim();
00173   SVector slice(int fi, int ti) const;
00174 
00175   void add(const SVector &v2);
00176   void add(const SVector &v2, double c2);
00177   void scale(double c1);
00178   void combine(double c1, const SVector &v2, double c2);
00179 
00180   friend std::ostream& operator<<(std::ostream &f, const SVector &v);
00181   friend std::istream& operator>>(std::istream &f, SVector &v);
00182   bool save(std::ostream &f) const;
00183   bool load(std::istream &f);
00184   double loadString(std::string str);
00185   void loadFromString(std::string str);
00186   void loadFromString(std::string str1, std::string str2);
00187 
00193   int fromPSQL(std::string str);
00198         double getSpaceUsed();
00199   bool loadStrStream(std::istream&);
00200 
00201   friend SVector combine(const SVector &v1, double a1, 
00202                          const SVector &v2, double a2);
00203 
00204   // binary serialization methods
00210   void *write_bin(size_t &total_size);
00217   int read_bin(void *data, size_t length);
00218 };
00219 
00220 double dot(const FVector &v1, const FVector &v2);
00221 double dot(const FVector &v1, const SVector &v2);
00222 double dot(const SVector &v1, const FVector &v2);
00223 double dot(const SVector &v1, const SVector &v2);
00224 
00225 SVector combine(const SVector &v1, double a1, const SVector &v2, double a2);
00226 FVector combine(const FVector &v1, double a1, const SVector &v2, double a2);
00227 FVector combine(const SVector &v1, double a1, const FVector &v2, double a2);
00228 FVector combine(const FVector &v1, double a1, const FVector &v2, double a2);
00229 
00230 template<class T>
00231 void parse_labeled_vector(std::string ex, int &label, T &f);
00232 
00233 #endif
00234 
00235 /* -------------------------------------------------------------
00236    Local Variables:
00237    c++-font-lock-extra-types: ("\\sw+_t" "[A-Z]\\sw*[a-z]\\sw*" "std::\\sw+")
00238    End:
00239    ------------------------------------------------------------- */

Generated on Wed Dec 15 10:46:15 2010 for Hazy_System by  doxygen 1.4.7