PLearn 0.1
InterleaveVMatrix.cc
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-2001 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal
00006 // Copyright (C) 2002 Pascal Vincent, Julien Keable, Xavier Saint-Mleux
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  * $Id: InterleaveVMatrix.cc 8617 2008-03-03 17:45:54Z nouiz $
00039  ******************************************************* */
00040 
00041 #include "InterleaveVMatrix.h"
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 
00049 PLEARN_IMPLEMENT_OBJECT(
00050     InterleaveVMatrix,
00051     "Interleave several VMats row-wise",
00052     "This class interleaves several VMats (with consecutive rows always coming\n"
00053     "from a different source VMat) thus possibly including more than once the\n"
00054     "rows of the small VMats.  For example, if source1.length()==10 and\n"
00055     "source2.length()==30 then the resulting VM will have 60 rows, and 3\n"
00056     "repetitions of each row of source1, with rows taken as follows:\n"
00057     "\n"
00058     "  source1.row(0), source2.row(0), source1.row(1), source2.row(1), ...,\n"
00059     "  source1.row(9), source2.row(9), source1.row(0), cource2.row(10), ...\n"
00060     "\n"
00061     "Note that if source2.length() is not a multiple of source1.length() some\n"
00062     "records from source1 will be repeated once more than others.\n"
00063     );
00064 
00065 InterleaveVMatrix::InterleaveVMatrix()
00066 { }
00067 
00068 InterleaveVMatrix::InterleaveVMatrix(TVec<VMat> the_sources)
00069     : sources(the_sources)
00070 {
00071     if (sources.size() > 0)
00072         build();
00073 }
00074 
00075 InterleaveVMatrix::InterleaveVMatrix(VMat source1, VMat source2)
00076     : sources(2)
00077 {
00078     sources[0] = source1;
00079     sources[1] = source2;
00080     build();
00081 }
00082 
00083 void InterleaveVMatrix::build()
00084 {
00085     inherited::build();
00086     build_();
00087 }
00088 
00089 void InterleaveVMatrix::build_()
00090 {
00091     if (sources) {
00092         int n = sources.size();
00093         if (n<1)
00094             PLERROR("InterleaveVMatrix expects >= 1 sources, got %d",n);
00095 
00096         width_ = sources[0]->width();
00097         int maxl = 0;
00098         for (int i = 0; i < n; i++) {
00099             if (sources[i]->width() != width_)
00100                 PLERROR("InterleaveVMatrix: source %d has %d width, while 0-th has %d",
00101                         i, sources[i]->width(), width_);
00102             int l = sources[i]->length();
00103             if (l > maxl)
00104                 maxl=l;
00105             updateMtime(sources[i]);
00106         }
00107         length_ = n * maxl;
00108 
00109         // Finally copy remaining meta information from first VMatrix
00110         setMetaInfoFrom(sources[0]);
00111     }
00112 }
00113 
00114 void InterleaveVMatrix::declareOptions(OptionList &ol)
00115 {
00116     declareOption(ol, "sources", &InterleaveVMatrix::sources,
00117                   OptionBase::buildoption,
00118                   "Set of VMats to be concatenated");
00119 
00120     inherited::declareOptions(ol);
00121 }
00122 
00123 real InterleaveVMatrix::get(int i, int j) const
00124 {
00125 #ifdef BOUNDCHECK
00126     if(i<0 || i>=length() || j<0 || j>=width())
00127         PLERROR("In InterleaveVMatrix::get OUT OF BOUNDS");
00128 #endif
00129     int n = sources.size();
00130     int m = i%n; // which source
00131     int pos = int(i/n) % sources[m].length(); // position within sources[m]
00132     return sources[m]->get(pos,j);
00133 }
00134 
00135 void InterleaveVMatrix::getSubRow(int i, int j, Vec v) const
00136 {
00137 #ifdef BOUNDCHECK
00138     if(i<0 || i>=length() || j<0 || j+v.length()>width())
00139         PLERROR("In InterleaveVMatrix::getRow OUT OF BOUNDS");
00140 #endif
00141     int n = sources.size();
00142     int m = i%n; // which source
00143     int pos = int(i/n) % sources[m].length(); // position within sources[m]
00144     sources[m]->getSubRow(pos, j, v);
00145 }
00146 
00147 void InterleaveVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00148 {
00149     inherited::makeDeepCopyFromShallowCopy(copies);
00150     deepCopyField(sources, copies);
00151 }
00152 
00153 } // end of namespace PLearn
00154 
00155 
00156 /*
00157   Local Variables:
00158   mode:c++
00159   c-basic-offset:4
00160   c-file-style:"stroustrup"
00161   c-file-offsets:((innamespace . 0)(inline-open . 0))
00162   indent-tabs-mode:nil
00163   fill-column:79
00164   End:
00165 */
00166 // 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