PLearn 0.1
TVec_impl.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal
00006 //
00007 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 
00037  
00038 
00039 /* *******************************************************      
00040  * $Id: TVec_impl.h 8319 2007-11-28 21:31:04Z nouiz $
00041  * AUTHORS: Pascal Vincent & Yoshua Bengio
00042  * This file is part of the PLearn library.
00043  ******************************************************* */
00044 
00045 
00046 
00047 #ifndef TVec_impl_INC
00048 #define TVec_impl_INC
00049 
00050 #include "TVec_decl.h"
00051 #include <plearn/io/pl_io.h>
00052 
00053 namespace PLearn {
00054 using namespace std;
00055 
00056 
00057 // *****************************
00058 // **** Fonctions pour TVec ****
00059 // *****************************
00060 
00061 // Deep copying
00062 template<class T>
00063 void TVec<T>::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00064 {
00065     deepCopyField(storage, copies);
00066 }
00067 
00068 template<class T>
00069 TVec<T> TVec<T>::deepCopy(CopiesMap& copies) const
00070 {
00071     // First do a shallow copy
00072     TVec<T> deep_copy = *this;
00073     // Transform the shallow copy into a deep copy
00074     deep_copy.makeDeepCopyFromShallowCopy(copies);
00075     // return the completed deep_copy
00076     return deep_copy;
00077 }
00078 
00079 template <class T>
00080 inline TVec<T> deepCopy(const TVec<T>& source)
00081 { 
00082     CopiesMap copies; 
00083     return deepCopy(source, copies);
00084 }
00085 
00086 template <class T>
00087 inline TVec<T> deepCopy(const TVec<T>& source, CopiesMap& copies)
00088 { 
00089     return source.deepCopy(copies);
00090 }
00091 
00092 template <class T>
00093 inline void deepCopyField(TVec<T>& field, CopiesMap& copies)
00094 {
00095     field.makeDeepCopyFromShallowCopy(copies);
00096 }
00097 
00098 
00099 template<class T>
00100 void swap( TVec<T>& a, TVec<T>& b)
00101 { swap_ranges(a.begin(), a.end(), b.begin()); }
00102 
00104 template<class T>
00105 inline void operator<<(const TVec<T>& m1, const TVec<T>& m2)
00106 {
00107 #ifdef BOUNDCHECK
00108     if(m1.size()!=m2.size())
00109         PLERROR("In operator<<(v1,v2) the 2 TVecs must have the same number of elements (%d != %d)", m1.size(), m2.size());
00110 #endif
00111     if (m1.isNotEmpty())
00112         copy(m2.begin(), m2.end(), m1.begin());
00113 }
00114 
00116 template<class T, class U>
00117 void operator<<(const TVec<T>& m1, const TVec<U>& m2)
00118 {
00119 #ifdef BOUNDCHECK
00120     if(m1.size()!=m2.size())
00121         PLERROR("In operator<<(m1,m2) the 2 matrices must have the same number of elements (%d != %d)", m1.size(), m2.size());
00122 #endif
00123     if (m1.isNotEmpty())
00124         copy_cast(m2.begin(), m2.end(), m1.begin());
00125 }
00126 
00128 template<class T, class U>
00129 inline void operator>>(const TVec<T>& m1, const TVec<U>& m2)
00130 { m2 << m1; }
00131 
00132 // old .pvec format
00133 template<class T>
00134 void savePVec(const string& filename, const TVec<T>& vec)
00135 { PLERROR("savePVec only implemented for float and double"); }
00136 
00137 template<class T>
00138 void loadPVec(const string& filename, TVec<float>& vec)
00139 { PLERROR("loadPVec only implemented for float and double"); }
00140 
00141 
00145 
00146 template <class T> inline PStream &
00147 operator<<(PStream &out, const TVec<T> &v)
00148 { 
00149     v.write(out); 
00150     return out;
00151 }
00152 
00153 template <class T> 
00154 PStream & operator>>(PStream &in, TVec<T> &v)
00155 {
00156     v.read(in);
00157     return in;
00158 }
00159 
00160 
00161 template<class T>      
00162 void binwrite(ostream& out, const TVec<T>& v)
00163 {
00164     int l = v.length();
00165     PLearn::binwrite(out,l);
00166     if (l<200000)
00167         PLearn::binwrite(out,v.data(),l);
00168     else for (int i=0;i<l;i+=200000)
00169         PLearn::binwrite(out,&v[i],std::min(200000,l-i));
00170 }
00171 
00172 template<class T>
00173 void binread(istream& in, TVec<T>& v)
00174 {
00175     int l;
00176     PLearn::binread(in,l);
00177     v.resize(l);
00178     if (l<200000)
00179         PLearn::binread(in,v.data(),l);
00180     else for (int i=0;i<l;i+=200000)
00181         PLearn::binread(in,&v[i],std::min(200000,l-i));
00182 }
00183 
00184 template<class T>      
00185 void binwrite_double(ostream& out, const TVec<T>& v)
00186 {
00187     int l = v.length();
00188     PLearn::binwrite(out,l);
00189     if (l<200000)
00190         PLearn::binwrite_double(out,v.data(),l);
00191     else for (int i=0;i<l;i+=200000)
00192         PLearn::binwrite_double(out,&v[i],std::min(200000,l-i));
00193 }
00194 
00195 template<class T>
00196 void binread_double(istream& in, TVec<T>& v)
00197 {
00198     int l;
00199     PLearn::binread(in,l);
00200     v.resize(l);
00201     if (l<200000)
00202         PLearn::binread_double(in,v.data(),l);
00203     else for (int i=0;i<l;i+=200000)
00204         PLearn::binread_double(in,&v[i],std::min(200000,l-i));
00205 }
00206 
00207 
00208 template<class T>
00209 inline ostream& operator<<(ostream& out, const TVec<T>& v)
00210 { 
00211     v.print(out);
00212     return out;
00213 }
00214 
00215 template<class T>
00216 inline istream& operator>>(istream& in, const TVec<T>& v)
00217 { 
00218     v.input(in);
00219     return in;
00220 }
00221 
00222 
00228 template<class T, class I>
00229 void selectElements(const TVec<T>& source, const TVec<I>& indices, TVec<T>& destination);
00230 
00232 template<class T>
00233 void elementsEqualTo(const TVec<T>& source, const T& value, const TVec<T>& destination);
00234 
00235 template<class T>
00236 TVec<T> concat(const TVec<T>& v1, const TVec<T>& v2)
00237 {
00238     TVec<T> result(v1.length()+v2.length());
00239     for(int i=0; i<v1.length(); i++)
00240         result[i] = v1[i];
00241     for(int i=0; i<v2.length(); i++)
00242         result[i+v1.length()] = v2[i];
00243     return result;
00244 }
00245 
00246 template<class T>
00247 TVec<T> concat(const TVec<T>& v1, const TVec<T>& v2, const TVec<T>& v3)
00248 {
00249     TVec<T> result;
00250     result.concat(v1,v2,v3);
00251     return result;
00252 }
00253 
00254 template<class T>
00255 TVec<T> concat(const TVec<T>& v1, const TVec<T>& v2, const TVec<T>& v3, const TVec<T>& v4)
00256 {
00257     TVec<T> result;
00258     result.concat(v1,v2,v3,v4);
00259     return result;
00260 }
00261 
00262 
00263 //template<class T>
00264 //TVec<T> concat(const Array< TVec<T> >& varray);
00265 
00269 template<class T>
00270 TVec<T> removeElement(const TVec<T>& v, int elemnum);
00271 
00272 
00274 template <class T>
00275 bool operator<=(const TVec<T>& left, const TVec<T>& right)
00276 {
00277     if (left.size() != right.size())
00278         PLERROR("Left and right vectors must have the same size in operator<=");
00279     return std::inner_product(left.begin(), left.end(), right.begin(),
00280                               true, std::logical_and<bool>(),
00281                               std::less_equal<T>());
00282 }
00283 
00284 template <class T>
00285 bool operator>=(const TVec<T>& left, const TVec<T>& right)
00286 {
00287     if (left.size() != right.size())
00288         PLERROR("Left and right vectors must have the same size in operator>=");
00289     return std::inner_product(left.begin(), left.end(), right.begin(),
00290                               true, std::logical_and<bool>(),
00291                               std::greater_equal<T>());
00292 }
00293 
00294 // This is a lexicographical definition for operator<
00295 template <class T>
00296 bool operator<(const TVec<T>& left, const TVec<T>& right)
00297 {
00298     if (left.size() != right.size())
00299         PLERROR("Left and right vectors must have the same size in operator<");
00300     int size = left.size();
00301     const T* ldata = left.data();
00302     const T* rdata = right.data();
00303     for ( ; size ; ++ldata, ++rdata, --size) {
00304         if (*ldata < *rdata)
00305             return true;
00306         if (*ldata > *rdata)
00307             return false;
00308         // Continue loop if both are equal
00309     }
00310     return false;                              // both vectors are equal at
00311     // this point; cannot be <
00312 }
00313 
00314 // This is a lexicographical definition for operator>
00315 template <class T>
00316 bool operator>(const TVec<T>& left, const TVec<T>& right)
00317 {
00318     if (left.size() != right.size())
00319         PLERROR("Left and right vectors must have the same size in operator>");
00320     int size = left.size();
00321     const T* ldata = left.data();
00322     const T* rdata = right.data();
00323     for ( ; size ; ++ldata, ++rdata, --size) {
00324         if (*ldata < *rdata)
00325             return false;
00326         if (*ldata > *rdata)
00327             return true;
00328         // Continue loop if both are equal
00329     }
00330     return false;                              // both vectors are equal at
00331     // this point; cannot be >
00332 }
00333 
00334 } // end of namespace PLearn
00335 
00336 #endif
00337 
00338 
00339 /*
00340   Local Variables:
00341   mode:c++
00342   c-basic-offset:4
00343   c-file-style:"stroustrup"
00344   c-file-offsets:((innamespace . 0)(inline-open . 0))
00345   indent-tabs-mode:nil
00346   fill-column:79
00347   End:
00348 */
00349 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines