PLearn 0.1
SubMatTransposeVariable.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-2002 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal
00006 // Copyright (C) 2001-2002 Nicolas Chapados, Ichiro Takeuchi, Jean-Sebastien Senecal
00007 // Copyright (C) 2002 Xiangdong Wang, Christian Dorion
00008 
00009 // Redistribution and use in source and binary forms, with or without
00010 // modification, are permitted provided that the following conditions are met:
00011 // 
00012 //  1. Redistributions of source code must retain the above copyright
00013 //     notice, this list of conditions and the following disclaimer.
00014 // 
00015 //  2. Redistributions in binary form must reproduce the above copyright
00016 //     notice, this list of conditions and the following disclaimer in the
00017 //     documentation and/or other materials provided with the distribution.
00018 // 
00019 //  3. The name of the authors may not be used to endorse or promote
00020 //     products derived from this software without specific prior written
00021 //     permission.
00022 // 
00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 // 
00034 // This file is part of the PLearn library. For more information on the PLearn
00035 // library, go to the PLearn Web site at www.plearn.org
00036 
00037 
00038 /* *******************************************************      
00039  * $Id: SubMatTransposeVariable.cc 3994 2005-08-25 13:35:03Z chapados $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 #include "ExtendedVariable.h"
00044 #include "SubMatTransposeVariable.h"
00045 //#include "Var_utils.h"
00046 
00047 namespace PLearn {
00048 using namespace std;
00049 
00050 
00053 PLEARN_IMPLEMENT_OBJECT(SubMatTransposeVariable, "ONE LINE DESCR", "NO HELP");
00054 
00055 SubMatTransposeVariable::SubMatTransposeVariable(Variable* v, int i, int j, int the_length, int the_width)
00056     : inherited(v, the_width, the_length),
00057       startk(i*v->length()+j),
00058       length_(the_length),
00059       width_(the_width),
00060       i_(i),
00061       j_(j)
00062 {
00063     build_();
00064 }
00065 
00066 void SubMatTransposeVariable::build()
00067 {
00068     inherited::build();
00069     build_();
00070 }
00071 
00072 void SubMatTransposeVariable::build_()
00073 {
00074     if (input) {
00075         // input is v from constructor
00076         if (i_ < 0 || i_ + length_ > input->length() || j_ < 0 || j_ + width_ > input->width())
00077             PLERROR("In SubMatTransposeVariable: requested sub-matrix is out of matrix bounds");
00078         startk = i_ * input->length() + j_;
00079     }
00080 }
00081 
00082 void
00083 SubMatTransposeVariable::declareOptions(OptionList &ol)
00084 {
00085     declareOption(ol, "length_", &SubMatTransposeVariable::length_, OptionBase::buildoption, "");
00086     declareOption(ol, "width_", &SubMatTransposeVariable::width_, OptionBase::buildoption, "");
00087     declareOption(ol, "startk", &SubMatTransposeVariable::startk, OptionBase::buildoption, "");
00088     declareOption(ol, "i_", &SubMatTransposeVariable::i_, OptionBase::buildoption, "");
00089     declareOption(ol, "j_", &SubMatTransposeVariable::j_, OptionBase::buildoption, "");
00090     inherited::declareOptions(ol);
00091 }
00092 
00093 void SubMatTransposeVariable::recomputeSize(int& l, int& w) const
00094 { l=width_; w=length_; }
00095 
00096 void SubMatTransposeVariable::fprop()
00097 {
00098     if(input->length()==1 || input->width()==1) // optimized version for this special case...
00099     {
00100         real* inputdata = input->valuedata+startk;
00101         for(int k=0; k<nelems(); k++)
00102             valuedata[k] = inputdata[k];
00103     }
00104     else // general case
00105     {
00106         real* inputrowdata = input->valuedata+startk;
00107         int thiskcolstart = 0; // element index of start of column in this var
00108         for(int i=0; i<width(); i++) // the width() of this var is the length() of the submat
00109         {
00110             int thisk = thiskcolstart++;
00111             for(int j=0; j<length(); j++, thisk+=width()) // the length() of this var is the width() of the submat
00112                 valuedata[thisk] = inputrowdata[j];
00113             inputrowdata += input->width();
00114         }
00115     }
00116 }
00117 
00118 
00119 void SubMatTransposeVariable::bprop()
00120 {
00121     if(input->length()==1 || input->width()==1) // optimized version for this special case...
00122     {
00123         real* inputdata = input->gradientdata+startk;
00124         for(int k=0; k<nelems(); k++)
00125             inputdata[k] += gradientdata[k];
00126     }
00127     else // general case
00128     {
00129         real* inputrowdata = input->gradientdata+startk;
00130         int thiskcolstart = 0; // element index of start of column in this var
00131         for(int i=0; i<width(); i++) // the width() of this var is the length() of the submat
00132         {
00133             int thisk = thiskcolstart++;
00134             for(int j=0; j<length(); j++, thisk+=width()) // the length() of this var is the width() of the submat
00135                 inputrowdata[j] += gradientdata[thisk];
00136             inputrowdata += input->width();
00137         }
00138     }
00139 }
00140 
00141 
00142 void SubMatTransposeVariable::symbolicBprop()
00143 {
00144     int i = startk/input->width();
00145     int j = startk%input->width();
00146     int topextent = i;
00147     int bottomextent = input->length()-(i+width()); // the width() of this var is the length() of the submat
00148     int leftextent = j;
00149     int rightextent = input->width()-(j+length()); // the length() of this var is the width() of the submat
00150     input->accg(extend(transpose(g),topextent,bottomextent,leftextent,rightextent));
00151 }
00152 
00153 
00154 void SubMatTransposeVariable::rfprop()
00155 {
00156     if (rValue.length()==0) resizeRValue();
00157     if(input->length()==1 || input->width()==1) // optimized version for this special case...
00158     {
00159         real* inputdata = input->rvaluedata+startk;
00160         for(int k=0; k<nelems(); k++)
00161             rvaluedata[k] = inputdata[k];
00162     }
00163     else // general case
00164     {
00165         real* inputrowdata = input->rvaluedata+startk;
00166         int thiskcolstart = 0; // element index of start of column in this var
00167         for(int i=0; i<width(); i++) // the width() of this var is the length() of the submat
00168         {
00169             int thisk = thiskcolstart++;
00170             for(int j=0; j<length(); j++, thisk+=width()) // the length() of this var is the width() of the submat
00171                 rvaluedata[thisk] = inputrowdata[j];
00172             inputrowdata += input->width();
00173         }
00174     }
00175 }
00176 
00177 
00178 
00179 } // end of namespace PLearn
00180 
00181 
00182 /*
00183   Local Variables:
00184   mode:c++
00185   c-basic-offset:4
00186   c-file-style:"stroustrup"
00187   c-file-offsets:((innamespace . 0)(inline-open . 0))
00188   indent-tabs-mode:nil
00189   fill-column:79
00190   End:
00191 */
00192 // 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