PLearn 0.1
ConcatRowsSubVMatrix.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: ConcatRowsSubVMatrix.cc 8617 2008-03-03 17:45:54Z nouiz $
00039  ******************************************************* */
00040 
00041 #include "ConcatRowsSubVMatrix.h"
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 
00049 PLEARN_IMPLEMENT_OBJECT(ConcatRowsSubVMatrix,
00050                         "Concatenates subVMatrices of a source VMatrix",
00051                         "This class concatenates several (virtual)"
00052                         " subVMatrices of the same\n"
00053                         "source VMatrix. For each sub-vmatrix block, the"
00054                         " user\n"
00055                         "specifies the starting row and the number of rows in"
00056                         " the\n"
00057                         "source VMatrix.\n"
00058                         "The resulting vmatrix sees first all the rows of"
00059                         " the\n"
00060                         "first sub-vmatrix, then all the rows of the 2nd,"
00061                         " etc.\n"
00062                         );
00063 
00064 ConcatRowsSubVMatrix::ConcatRowsSubVMatrix(bool call_build_)
00065     : inherited(call_build_)
00066 {
00067     if( call_build_ )
00068         build_();
00069 }
00070 
00071 ConcatRowsSubVMatrix::ConcatRowsSubVMatrix(VMat the_source,
00072                                            TVec<int>& the_start,
00073                                            TVec<int>& the_len,
00074                                            bool call_build_)
00075     : inherited(the_source, -1, the_source->width(), call_build_),
00076       start(the_start),
00077       len(the_len)
00078 {
00080 /*
00081   fieldinfos = the_source->getFieldInfos();
00082   check();
00083 */
00084     if( call_build_ )
00085         build_();
00086 }
00087 
00088 ConcatRowsSubVMatrix::ConcatRowsSubVMatrix(VMat the_source,
00089                                            int start1, int len1,
00090                                            int start2, int len2,
00091                                            bool call_build_)
00092     : inherited(the_source, -1, the_source->width(), call_build_),
00093       start(2), len(2)
00094 {
00096     //fieldinfos = the_source->getFieldInfos();
00097 
00098     start[0]=start1;
00099     start[1]=start2;
00100     len[0]=len1;
00101     len[1]=len2;
00102     //check();
00103     if( call_build_ )
00104         build_();
00105 }
00106 /*
00107   void ConcatRowsSubVMatrix::check()
00108   {
00109   length_=0;
00110   for (int i=0;i<start.length();i++)
00111   {
00112   if (start[i]<0 || start[i]+len[i]>source->length())
00113   PLERROR("ConcatRowsSubVMatrix: out-of-range specs for submat %d, "
00114   "start=%d, len=%d, source length=%d",i,start[i],len[i],
00115   distr->length());
00116   length_ += len[i];
00117   }
00118   }
00119 */
00120 
00121 void ConcatRowsSubVMatrix::getpositions(int i, int& whichvm, int& rowofvm) const
00122 {
00123 #ifdef BOUNDCHECK
00124     if(i<0 || i>=length())
00125         PLERROR("In ConcatRowsSubVMatrix::getpositions OUT OF BOUNDS");
00126 #endif
00127 
00128     int pos = 0;
00129     int k=0;
00130     while(i>=pos+len[k])
00131     {
00132         pos += len[k];
00133         k++;
00134     }
00135 
00136     whichvm = k;
00137     rowofvm = i-pos;
00138 }
00139 
00140 real ConcatRowsSubVMatrix::get(int i, int j) const
00141 {
00142     int whichvm, rowofvm;
00143     getpositions(i,whichvm,rowofvm);
00144     return source->get(start[whichvm]+rowofvm,j);
00145 }
00146 
00147 void ConcatRowsSubVMatrix::getSubRow(int i, int j, Vec v) const
00148 {
00149     int whichvm, rowofvm;
00150     getpositions(i,whichvm,rowofvm);
00151     source->getSubRow(start[whichvm]+rowofvm, j, v);
00152 }
00153 
00154 real ConcatRowsSubVMatrix::dot(int i1, int i2, int inputsize) const
00155 {
00156     int whichvm1, rowofvm1;
00157     getpositions(i1,whichvm1,rowofvm1);
00158     int whichvm2, rowofvm2;
00159     getpositions(i2,whichvm2,rowofvm2);
00160     return source->dot(start[whichvm1]+rowofvm1, start[whichvm2]+rowofvm2, inputsize);
00161 }
00162 
00163 real ConcatRowsSubVMatrix::dot(int i, const Vec& v) const
00164 {
00165     int whichvm, rowofvm;
00166     getpositions(i,whichvm,rowofvm);
00167     return source->dot(start[whichvm]+rowofvm,v);
00168 }
00169 
00170 void ConcatRowsSubVMatrix::declareOptions(OptionList &ol)
00171 {
00172     declareOption(ol, "distr", &ConcatRowsSubVMatrix::source,
00173                   (OptionBase::learntoption | OptionBase::nosave),
00174                   "DEPRECATED - Use 'source' instead");
00175 
00176     declareOption(ol, "start", &ConcatRowsSubVMatrix::start,
00177                   OptionBase::buildoption,
00178                   "");
00179 
00180     declareOption(ol, "len", &ConcatRowsSubVMatrix::len,
00181                   OptionBase::buildoption,
00182                   "");
00183 
00184     inherited::declareOptions(ol);
00185 }
00186 
00187 void ConcatRowsSubVMatrix::build()
00188 {
00189     inherited::build();
00190     build_();
00191 }
00192 
00193 void ConcatRowsSubVMatrix::build_()
00194 {
00195     if (source) {
00196         fieldinfos = source->getFieldInfos();
00197         setMetaInfoFrom(source);
00198         length_=0;
00199         for (int i = 0; i < start.length(); i++) {
00200             if (start[i]<0 || start[i]+len[i]>source->length())
00201                 PLERROR("ConcatRowsSubVMatrix: out-of-range specs for submat"
00202                         " %d,\n"
00203                         "start=%d, len=%d, underlying distr length=%d\n",
00204                         i, start[i], len[i], source->length());
00205             length_ += len[i];
00206         }
00207     }
00208 }
00209 
00210 } // end of namespace PLearn
00211 
00212 
00213 /*
00214   Local Variables:
00215   mode:c++
00216   c-basic-offset:4
00217   c-file-style:"stroustrup"
00218   c-file-offsets:((innamespace . 0)(inline-open . 0))
00219   indent-tabs-mode:nil
00220   fill-column:79
00221   End:
00222 */
00223 // 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