PLearn 0.1
InsertZerosVariable.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 2004 Jasmin Lapalme
00005 
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 // 
00009 //  1. Redistributions of source code must retain the above copyright
00010 //     notice, this list of conditions and the following disclaimer.
00011 // 
00012 //  2. Redistributions in binary form must reproduce the above copyright
00013 //     notice, this list of conditions and the following disclaimer in the
00014 //     documentation and/or other materials provided with the distribution.
00015 // 
00016 //  3. The name of the authors may not be used to endorse or promote
00017 //     products derived from this software without specific prior written
00018 //     permission.
00019 // 
00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 // 
00031 // This file is part of the PLearn library. For more information on the PLearn
00032 // library, go to the PLearn Web site at www.plearn.org
00033 
00034 #include "InsertZerosVariable.h"
00035 #include "Var_operators.h"
00036 
00037 namespace PLearn {
00038 using namespace std;
00039 
00040 
00043 PLEARN_IMPLEMENT_OBJECT(InsertZerosVariable,
00044                         "Insert rows with zeros",
00045                         "NO HELP");
00046 
00047 InsertZerosVariable::InsertZerosVariable(Variable* input, TVec<int> the_rows) 
00048     : inherited(input, input->length()+the_rows.length(), input->width()), rows(the_rows)
00049 {}
00050 
00051 void InsertZerosVariable::recomputeSize(int& l, int& w) const {
00052     if (input) {
00053         l = input->length()+rows.length();
00054         w = input->width();
00055     } else
00056         l = w = 0;
00057 }
00058 
00059 void InsertZerosVariable::build_()
00060 { 
00061     if(input)
00062     {
00063         sortElements(rows);
00064         // Check if all elements are unique
00065         for(int i=0; i<rows.length()-1; i++)
00066         {
00067             if(rows[i] == rows[i+1]) PLERROR("InsertZerosVariable::build_(): some elements in rows are the same");
00068             
00069             if(rows[i] > input->length()+i) PLERROR("InsertZerosVariable::build_(): cannot add row of zeros at positon %d", rows[i+1]);
00070         }
00071     }
00072 }
00073 
00074 void InsertZerosVariable::build()
00075 {
00076     inherited::build();
00077     build_();
00078 }
00079 
00080 void InsertZerosVariable::fprop() {
00081     real* ptr;
00082     real* inputptr;
00083     int r=0;
00084     for(int i=0; i<matValue.length(); i++)
00085     {
00086         ptr = matValue[i];
00087         if(r<rows.length() && i==rows[r])
00088         {
00089             for(int j=0; j<matValue.width(); j++)         
00090                 *ptr++ = 0;
00091             r++;
00092         }
00093         else
00094         {
00095             inputptr = input->matValue[i-r];
00096             for(int j=0; j<matValue.width(); j++)
00097                 *ptr++ = *inputptr++;
00098         }
00099     }
00100 }
00101 
00102 
00103 void InsertZerosVariable::bprop() {
00104     real* gptr;
00105     real* ginputptr;
00106     int r=0;
00107     for(int i=0; i<matGradient.length(); i++)
00108     {
00109         if(r<rows.length() && i==rows[r])
00110             r++;
00111         else
00112         {
00113             gptr = matGradient[i];
00114             ginputptr = input->matGradient[i-r];
00115             for(int j=0; j<matGradient.width(); j++)
00116                 *ginputptr++ += *gptr++;
00117         }
00118     }
00119 }
00120 
00121 void InsertZerosVariable::bbprop() {
00122     PLERROR("bbprop is not implemented for InsertZerosVariable");
00123 }
00124 
00125 
00126 void InsertZerosVariable::symbolicBprop() {
00127     PLERROR("symbolicBprop is not implemented for InsertZerosVariable");
00128 }
00129 
00130 void InsertZerosVariable::rfprop() {
00131     PLERROR("rfprop is not implemented for InsertZerosVariable");
00132 }
00133 
00134 void InsertZerosVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00135 {
00136     inherited::makeDeepCopyFromShallowCopy(copies);
00137     deepCopyField(rows, copies);
00138 }
00139 
00140 
00141 } // end of namespace PLearn
00142 
00143 
00144 /*
00145   Local Variables:
00146   mode:c++
00147   c-basic-offset:4
00148   c-file-style:"stroustrup"
00149   c-file-offsets:((innamespace . 0)(inline-open . 0))
00150   indent-tabs-mode:nil
00151   fill-column:79
00152   End:
00153 */
00154 // 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