PLearn 0.1
PlusManyVariable.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PlusManyVariable.cc
00004 //
00005 // Copyright (C) 2008 Olivier Delalleau
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 //
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 //
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 //
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 // Authors: Olivier Delalleau
00036 
00040 #include "PlusManyVariable.h"
00041 
00042 namespace PLearn {
00043 using namespace std;
00044 
00047 PLEARN_IMPLEMENT_OBJECT(
00048     PlusManyVariable,
00049     "Sum of an arbitrary number of variables (that must have same sizes).",
00050     ""
00051 );
00052 
00054 // PlusManyVariable //
00056 PlusManyVariable::PlusManyVariable()
00057 {}
00058 
00059 PlusManyVariable::PlusManyVariable(const VarArray& the_array,
00060                                    bool call_build_):
00061     inherited(the_array, the_array[0]->length(), the_array[0]->width(),
00062               call_build_)
00063 {
00064     if (call_build_)
00065         build_();
00066 }
00067 
00069 // recomputeSize //
00071 void PlusManyVariable::recomputeSize(int& l, int& w) const
00072 {
00073     if (varray.length() > 0) {
00074         l = varray[0]->length();
00075         w = varray[0]->width();
00076     } else
00077         l = w = 0;
00078 }
00079 
00081 // fprop //
00083 void PlusManyVariable::fprop()
00084 {
00085     fillValue(0);
00086     int n = nelems();
00087     for (int i = 0; i < varray.length(); i++) {
00088         real* vi = varray[i]->valuedata;
00089         for (int k=0; k<n; k++)
00090             valuedata[k] += vi[k];
00091     }
00092 }
00093 
00095 // bprop //
00097 void PlusManyVariable::bprop()
00098 {
00099     int n = nelems();
00100     for (int i = 0; i < varray.length(); i++) {
00101         real* vi = varray[i]->gradientdata;
00102         for (int k=0; k<n; k++)
00103             vi[k] += gradientdata[k];
00104     }
00105 }
00106 
00108 // build //
00110 void PlusManyVariable::build()
00111 {
00112     inherited::build();
00113     build_();
00114 }
00115 
00117 // makeDeepCopyFromShallowCopy //
00119 void PlusManyVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00120 {
00121     inherited::makeDeepCopyFromShallowCopy(copies);
00122 }
00123 
00125 // declareOptions //
00127 void PlusManyVariable::declareOptions(OptionList& ol)
00128 {
00129     // Now call the parent class' declareOptions
00130     inherited::declareOptions(ol);
00131 }
00132 
00134 // build_ //
00136 void PlusManyVariable::build_()
00137 {
00138     // Check sizes are coherent.
00139     if (varray.length() >= 2) {
00140         int l = varray[0]->length();
00141         int w = varray[1]->width();
00142         for (int i = 1; i < varray.length(); i++)
00143             if (varray[i]->length() != l || varray[i]->width() != w)
00144                 PLERROR("In PlusManyVariable::build_ - All source variables "
00145                         "must have the same size");
00146     }
00147 }
00148 
00149 
00150 } // end of namespace PLearn
00151 
00152 
00153 /*
00154   Local Variables:
00155   mode:c++
00156   c-basic-offset:4
00157   c-file-style:"stroustrup"
00158   c-file-offsets:((innamespace . 0)(inline-open . 0))
00159   indent-tabs-mode:nil
00160   fill-column:79
00161   End:
00162 */
00163 // 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