Vector.h

Go to the documentation of this file.
00001 //Copyright (c) 2004-2005, Baris Sumengen
00002 //All rights reserved.
00003 //
00004 // CIMPL Matrix Performance Library
00005 //
00006 //Redistribution and use in source and binary
00007 //forms, with or without modification, are
00008 //permitted provided that the following
00009 //conditions are met:
00010 //
00011 //    * No commercial use is allowed. 
00012 //    This software can only be used
00013 //    for non-commercial purposes. This 
00014 //    distribution is mainly intended for
00015 //    academic research and teaching.
00016 //    * Redistributions of source code must
00017 //    retain the above copyright notice, this
00018 //    list of conditions and the following
00019 //    disclaimer.
00020 //    * Redistributions of binary form must
00021 //    mention the above copyright notice, this
00022 //    list of conditions and the following
00023 //    disclaimer in a clearly visible part 
00024 //    in associated product manual, 
00025 //    readme, and web site of the redistributed 
00026 //    software.
00027 //    * Redistributions in binary form must
00028 //    reproduce the above copyright notice,
00029 //    this list of conditions and the
00030 //    following disclaimer in the
00031 //    documentation and/or other materials
00032 //    provided with the distribution.
00033 //    * The name of Baris Sumengen may not be
00034 //    used to endorse or promote products
00035 //    derived from this software without
00036 //    specific prior written permission.
00037 //
00038 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
00039 //HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00040 //EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
00041 //NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00042 //MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00043 //PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00044 //CONTRIBUTORS BE LIABLE FOR ANY
00045 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00046 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00047 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00048 //OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00049 //DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00050 //HOWEVER CAUSED AND ON ANY THEORY OF
00051 //LIABILITY, WHETHER IN CONTRACT, STRICT
00052 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00053 //OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00054 //OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00055 //POSSIBILITY OF SUCH DAMAGE.
00056 
00057 
00058 
00059 #pragma once
00060 #ifndef VECTOR_H
00061 #define VECTOR_H
00062 
00063 #include <iostream>
00064 
00065 using std::cout;
00066 using std::cerr;
00067 using std::endl;
00068 using std::ostream;
00069 using std::right;
00070 using std::fixed;
00071 
00072 #include <iomanip>
00073 
00074 using std::setw;
00075 
00076 #include <math.h>
00077 
00078 #include <typeinfo>
00079 
00080 #include "./cimpl.h"
00081 
00082 
00083 #include <sstream>
00084 
00085 using std::ostringstream;
00086 
00087 #include <limits>
00088 using std::numeric_limits;
00089 
00090 using namespace std;
00091 
00092 
00093 namespace CIMPL
00094 {
00095 
00096 
00097 // forward declaration
00098 template< class T > class Array;
00099 template< class T > class Matrix;
00100 template< class T > class Vector;
00101 
00102 
00104 template< class T >
00105 class Vector
00106 {
00107         friend class Array<T>;
00108         friend class Matrix<T>;
00109 
00110 
00111 protected:
00112         T *data;
00113         int length;
00114         bool memoryManaged;
00115         Cleaner<T> *clean;   // Reference counting Garbage collector.
00116 
00117 
00118 public:
00119         Vector(void);
00120         explicit Vector(int l);
00121         Vector(string str);
00122         Vector(int l, T init);
00123         Vector(T* _data, int l); // creates unmanaged memory
00124         Vector(Vector<T> &v);
00125         
00126         ~Vector(void);
00127         void Set(T* _data, const int l); // creates unmanaged memory
00128 
00129         void Clean();
00130 
00131         const T* DataPtr() const;
00132         T* Data();
00133 
00134         Vector<T> Clone() const;
00135         Vector<T> Slice(int start, int end);
00136         Vector<T> Slice(string str);
00137 
00138         const bool IsMemoryManaged() const;
00139 
00140         const int Length() const;
00141         const int Numel() const;
00142         void Init(const T init);
00143         Vector<T>& Rand(const double max);
00144         static Vector<T> Rand(const int l, const double max);
00145 
00146         void ReadFromVector(const Vector<T>& m, const int index=0);
00147 
00148         static Vector<T> Cat(Vector<T>& m1, Vector<T>& m2);
00149 
00150         static Vector<T> Ones(int side);
00151         static Vector<T> Zeros(int side);
00152 
00153 
00154         static T Inner(Vector<T>& m1, Vector<T>& m2);
00155 
00156         // Boolean Operations...
00157         static Vector<int> And(Vector<T>& m1, Vector<T>& m2);
00158         static Vector<int> Or(Vector<T>& m1, Vector<T>& m2);
00159         static Vector<int> Lt(Vector<T>& m1, Vector<T>& m2);
00160         static Vector<int> Gt(Vector<T>& m1, Vector<T>& m2);
00161         static Vector<int> Le(Vector<T>& m1, Vector<T>& m2);
00162         static Vector<int> Ge(Vector<T>& m1, Vector<T>& m2);
00163         static Vector<int> Eq(Vector<T>& m1, Vector<T>& m2);
00164         static Vector<int> Ne(Vector<T>& m1, Vector<T>& m2);
00165 
00166         // Boolean Operations with value type...
00167         static Vector<int> And(Vector<T>& m, T v);
00168         static Vector<int> Or(Vector<T>& m, T v);
00169         static Vector<int> Lt(Vector<T>& m, T v);
00170         static Vector<int> Gt(Vector<T>& m, T v);
00171         static Vector<int> Le(Vector<T>& m, T v);
00172         static Vector<int> Ge(Vector<T>& m, T v);
00173         static Vector<int> Eq(Vector<T>& m, T v);
00174         static Vector<int> Ne(Vector<T>& m, T v);
00175 
00176 
00177 
00178 // Add vector to another vector
00179         static Vector<T> Add(Vector<T>& m1, Vector<T>& m2);
00180         static Vector<T> Subtract(Vector<T>& m1, Vector<T>& m2);
00181         static Vector<T> Multiply(Vector<T>& m1, Vector<T>& m2);
00182         static Vector<T> Divide(Vector<T>& m1, Vector<T>& m2);
00183 
00184 // Add value to another vector
00185         static Vector<T> Add(Vector<T>& m1, T v2);
00186         static Vector<T> Subtract(Vector<T>& m1, T v2);
00187         static Vector<T> Subtract(T v2, Vector<T>& m1);
00188         static Vector<T> Multiply(Vector<T>& m1, T v2);
00189         static Vector<T> Divide(Vector<T>& m1, T v2);
00190         static Vector<T> Divide(T v2, Vector<T>& m1);
00191 
00192 
00193 // Add vector to "this" vector
00194         Vector<T>& Add(Vector<T>& m);
00195         Vector<T>& Subtract(Vector<T>& m);
00196         Vector<T>& Multiply(Vector<T>& m);
00197         Vector<T>& Divide(Vector<T>& m);
00198 
00199 // Add value to "this" vector
00200         Vector<T>& Add(T v);
00201         Vector<T>& Subtract(T v);
00202         Vector<T>& Multiply(T v);
00203         Vector<T>& Divide(T v);
00204 
00205 
00206 //OPERATORS
00207         Vector<T>& operator= (Vector<T>& m);
00208         Vector<T>& operator= (Matrix<T>& m);
00209         Vector<T>& operator= (Array<T>& m);
00210 
00211         Vector<T>& operator= (string str);
00212 
00213         Vector<T> operator+ ();
00214         Vector<T> operator- ();
00215         Vector<int> operator! ();
00216 
00217         friend Matrix<T> operator, (Vector<T>& m1, Vector<T>& m2)
00218         {
00219                 return Vector<T>::Cat(m1, m2);
00220         }
00221         
00222         friend Matrix<T> operator| (Vector<T>& m1, Vector<T>& m2)
00223         {
00224                 return Matrix<T>::Cat(1, m1, m2);
00225         }
00226 
00227 
00228 
00229         friend ostream& operator<< (ostream& output, const Vector<T>& v)
00230         {
00231                 int lm = v.Length();
00232                 int rowNoWidth = (int)log10((double)(lm-1))+2;
00233                 int maxLength  = 1;
00234                 for(int i=0; i<v.Length(); i++)
00235                 {
00236                         ostringstream oS;
00237                         oS << v.data[i];
00238                         int l = (int)oS.str().length();
00239                         if(l>maxLength)
00240                         {
00241                                 maxLength = l;
00242                         }
00243                 }
00244 
00245                 output << typeid(v).name() << " of size " << v.length << endl;
00246                 output << "----------------------" << endl;
00247                 for(int i=0;i<v.length;i++)
00248                 {
00249                         output << "ROW" << setw(rowNoWidth) << i+1 << "|" << right << setw(maxLength+3) << v.data[i] << " |" << endl;
00250                 }
00251                 output << "----------------------" << endl;
00252                 output << endl;
00253                 return output;
00254         }
00255         
00256 
00257         T& operator() (const int i);
00258         T& operator[] (const int i); // no bounds check
00259                 
00260         // slice x slice: returns a vector.
00261         Vector<T> operator() (int start, int end);
00262         Vector<T> operator() (string str);
00263 
00264         Vector<T> operator() (Vector<int>& ind);
00265         
00266         T& Elem(const int i);
00267         T& ElemNC(const int i); // No bounds check
00268         
00269         
00270         // Vector to Vector inner product
00271         friend T operator& (Vector<T>& m1, Vector<T>& m2)
00272         {
00273                 return Vector<T>::Inner(m1, m2);
00274         }
00275         
00276         
00277         // Boolean operations
00278         friend Vector<int> operator&& (Vector<T>& m1, Vector<T>& m2)
00279         {
00280                 return Vector<T>::And(m1, m2);
00281         }
00282 
00283         friend Vector<int> operator|| (Vector<T>& m1, Vector<T>& m2)
00284         {
00285                 return Vector<T>::Or(m1, m2);
00286         }
00287 
00288         friend Vector<int> operator< (Vector<T>& m1, Vector<T>& m2)
00289         {
00290                 return Vector<T>::Lt(m1, m2);
00291         }
00292 
00293         friend Vector<int> operator> (Vector<T>& m1, Vector<T>& m2)
00294         {
00295                 return Vector<T>::Gt(m1, m2);
00296         }
00297 
00298         friend Vector<int> operator<= (Vector<T>& m1, Vector<T>& m2)
00299         {
00300                 return Vector<T>::Le(m1, m2);
00301         }
00302 
00303         friend Vector<int> operator>= (Vector<T>& m1, Vector<T>& m2)
00304         {
00305                 return Vector<T>::Ge(m1, m2);
00306         }
00307 
00308         friend Vector<int> operator== (Vector<T>& m1, Vector<T>& m2)
00309         {
00310                 return Vector<T>::Eq(m1, m2);
00311         }
00312 
00313         friend Vector<int> operator!= (Vector<T>& m1, Vector<T>& m2)
00314         {
00315                 return Vector<T>::Ne(m1, m2);
00316         }
00317 
00318 
00319         // Boolean operations with value type
00320         friend Vector<int> operator&& (Vector<T>& m, T v)
00321         {
00322                 return Vector<T>::And(m, v);
00323         }
00324         friend Vector<int> operator&& (T v, Vector<T>& m)
00325         {
00326                 return Vector<T>::And(m, v);
00327         }
00328 
00329         friend Vector<int> operator|| (Vector<T>& m, T v)
00330         {
00331                 return Vector<T>::Or(m, v);
00332         }
00333         friend Vector<int> operator|| (T v, Vector<T>& m)
00334         {
00335                 return Vector<T>::Or(m, v);
00336         }
00337 
00338         friend Vector<int> operator< (Vector<T>& m, T v)
00339         {
00340                 return Vector<T>::Lt(m, v);
00341         }
00342         friend Vector<int> operator< (T v, Vector<T>& m)
00343         {
00344                 return Vector<T>::Gt(m, v);
00345         }
00346 
00347         friend Vector<int> operator> (Vector<T>& m, T v)
00348         {
00349                 return Vector<T>::Gt(m, v);
00350         }
00351         friend Vector<int> operator> (T v, Vector<T>& m)
00352         {
00353                 return Vector<T>::Lt(m, v);
00354         }
00355 
00356         friend Vector<int> operator<= (Vector<T>& m, T v)
00357         {
00358                 return Vector<T>::Le(m, v);
00359         }
00360         friend Vector<int> operator<= (T v, Vector<T>& m)
00361         {
00362                 return Vector<T>::Ge(m, v);
00363         }
00364 
00365         friend Vector<int> operator>= (Vector<T>& m, T v)
00366         {
00367                 return Vector<T>::Ge(m, v);
00368         }
00369         friend Vector<int> operator>= (T v, Vector<T>& m)
00370         {
00371                 return Vector<T>::Le(m, v);
00372         }
00373 
00374         friend Vector<int> operator== (Vector<T>& m, T v)
00375         {
00376                 return Vector<T>::Eq(m, v);
00377         }
00378         friend Vector<int> operator== (T v, Vector<T>& m)
00379         {
00380                 return Vector<T>::Eq(m, v);
00381         }
00382 
00383         friend Vector<int> operator!= (Vector<T>& m, T v)
00384         {
00385                 return Vector<T>::Ne(m, v);
00386         }
00387         friend Vector<int> operator!= (T v, Vector<T>& m)
00388         {
00389                 return Vector<T>::Ne(m, v);
00390         }
00391 
00392 
00393 
00394 // Add Vector to another Vector
00395         friend Vector<T> operator+ (Vector<T>& m1, Vector<T>& m2)
00396         {
00397                 return Vector<T>::Add(m1, m2);
00398         }
00399 
00400         friend Vector<T> operator- (Vector<T>& m1, Vector<T>& m2)
00401         {
00402                 return Vector<T>::Subtract(m1, m2);
00403         }
00404 
00405         friend Vector<T> operator* (Vector<T>& m1, Vector<T>& m2)
00406         {
00407                 return Vector<T>::Multiply(m1, m2);
00408         }
00409 
00410         friend Vector<T> operator/ (Vector<T>& m1, Vector<T>& m2)
00411         {
00412                 return Vector<T>::Divide(m1, m2);
00413         }
00414 
00415 
00416 // Add value to another matrix
00417         friend Vector<T> operator+ (Vector<T>& m, T v)
00418         {
00419                 return Vector<T>::Add(m, v);
00420         }
00421 
00422         friend Vector<T> operator+ (T v, Vector<T>& m)
00423         {
00424                 return Vector<T>::Add(m, v);
00425         }
00426 
00427         friend Vector<T> operator- (Vector<T>& m, T v)
00428         {
00429                 return Vector<T>::Subtract(m, v);
00430         }
00431 
00432         friend Vector<T> operator- (T v, Vector<T>& m)
00433         {
00434                 return Vector<T>::Subtract(v, m);
00435         }
00436 
00437         friend Vector<T> operator* (Vector<T>& m, T v)
00438         {
00439                 return Vector<T>::Multiply(m, v);
00440         }
00441 
00442         friend Vector<T> operator* (T v, Vector<T>& m)
00443         {
00444                 return Vector<T>::Multiply(m, v);
00445         }
00446 
00447         friend Vector<T> operator/ (Vector<T>& m, T v)
00448         {
00449                 return Vector<T>::Divide(m, v);
00450         }
00451 
00452         friend Vector<T> operator/ (T v, Vector<T>& m)
00453         {
00454                 return Vector<T>::Divide(v, m);
00455         }
00456 
00457 
00458 
00459 
00460 // Add inline
00461         Vector<T>& operator+= (Vector<T>& m);
00462         Vector<T>& operator-= (Vector<T>& m);
00463         Vector<T>& operator*= (Vector<T>& m);
00464         Vector<T>& operator/= (Vector<T>& m);
00465 
00466         Vector<T>& operator+= (T v);
00467         Vector<T>& operator-= (T v);
00468         Vector<T>& operator*= (T v);
00469         Vector<T>& operator/= (T v);
00470 
00471         
00472 // TYPE CONVERSIONS
00473         Vector(Array<T> &m);
00474         Vector(Matrix<T> &m);
00475 
00476 };
00477 
00478 }; //namespace
00479 
00480 
00481 
00482 #include "./Vector.inl"
00483 
00484 
00485 
00486 
00487 
00488 #endif
00489 
00490 
00491 
00492 
00493 
00494 
00495 

Generated on Thu Jan 20 08:43:46 2005 for CIMPL by  doxygen 1.3.9.1