PLearn 0.1
MatrixElementsVariable.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: MatrixElementsVariable.cc 6861 2007-04-09 19:04:15Z saintmlx $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 #include "MatrixElementsVariable.h"
00044 
00045 namespace PLearn {
00046 using namespace std;
00047 
00048 
00049 
00051 /* fills the elements of a matrix using the given scalar Variable expression,
00052    that depends on index variables i and j, that loop from
00053    0 to ni-1 and 0 to nj-1 respectively. */
00054 
00055 PLEARN_IMPLEMENT_OBJECT(MatrixElementsVariable, 
00056                         "Fills the elements of a matrix using the given scalar Variable "
00057                         "expression, that depends on index variables i and j, that loop from "
00058                         "0 to ni-1 and 0 to nj-1 respectively.",
00059                         "NO HELP");
00060 
00061 
00062 MatrixElementsVariable::MatrixElementsVariable(Variable* the_expression, const Var& i_index,
00063                                                const Var& j_index, int number_of_i_values,
00064                                                int number_of_j_values, const VarArray& the_parameters)
00065     : inherited(the_parameters, number_of_i_values, number_of_j_values), i(i_index), 
00066       j(j_index), ni(number_of_i_values), nj(number_of_j_values), expression(the_expression), 
00067       parameters(the_parameters)
00068 {
00069     build_();
00070 }
00071 
00072 void
00073 MatrixElementsVariable::build()
00074 {
00075     inherited::build();
00076 }
00077 
00078 void
00079 MatrixElementsVariable::build_()
00080 {
00081     if (i && j && (parameters.size() > 0) && expression) {
00082         if (!i->isScalar())
00083             PLERROR("MatrixElementsVariable expect a scalar index Var i_index");
00084         if (!j->isScalar())
00085             PLERROR("MatrixElementsVariable expect a scalar index Var j_index");
00086 
00087         full_fproppath = propagationPath(parameters&(VarArray)i&(VarArray)j, expression);
00088         fproppath = propagationPath(i&j, expression);
00089         bproppath = propagationPath(parameters, expression);
00090     }
00091 }
00092 
00093 void
00094 MatrixElementsVariable::declareOptions(OptionList &ol)
00095 {
00096     declareOption(ol, "i", &MatrixElementsVariable::i, OptionBase::buildoption, "");
00097     declareOption(ol, "j", &MatrixElementsVariable::j, OptionBase::buildoption, "");
00098     declareOption(ol, "ni", &MatrixElementsVariable::ni, OptionBase::buildoption, "");
00099     declareOption(ol, "nj", &MatrixElementsVariable::nj, OptionBase::buildoption, "");
00100     declareOption(ol, "expression", &MatrixElementsVariable::expression, OptionBase::buildoption, "");
00101     declareOption(ol, "parameters", &MatrixElementsVariable::parameters, OptionBase::buildoption, "");
00102     inherited::declareOptions(ol);
00103 }
00104 
00105 void MatrixElementsVariable::recomputeSize(int& l, int& w) const
00106 { l=ni; w=nj; }
00107 
00108 
00109 #ifdef __INTEL_COMPILER
00110 #pragma warning(disable:1419)  // Get rid of compiler warning.
00111 #endif
00112 extern void varDeepCopyField(Var& field, CopiesMap& copies);
00113 #ifdef __INTEL_COMPILER
00114 #pragma warning(default:1419)
00115 #endif
00116 
00117 void MatrixElementsVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00118 {
00119     inherited::makeDeepCopyFromShallowCopy(copies);
00120     varDeepCopyField(i, copies);
00121     varDeepCopyField(j, copies);
00122     varDeepCopyField(expression, copies);
00123     deepCopyField(parameters, copies);
00124     deepCopyField(full_fproppath, copies);
00125     deepCopyField(fproppath, copies);
00126     deepCopyField(bproppath, copies);
00127 }
00128 
00129 
00130 void MatrixElementsVariable::fprop()
00131 {
00132     int ii,jj;
00133 
00134     for (ii=0;ii<ni;ii++)
00135     {
00136         i->value[0]=ii;
00137         for (jj=0;jj<nj;jj++)
00138         {
00139             j->value[0]=jj;
00140             if (ii==0 && jj==0)
00141                 // effect of parameters is computed only once
00142                 full_fproppath.fprop();
00143             else
00144                 fproppath.fprop();
00145             matValue(ii,jj) = expression->value[0];
00146         }
00147     }
00148 }
00149 
00150 
00151 void MatrixElementsVariable::bprop()
00152 {
00153     int ii,jj;
00154 
00155     for (ii=0;ii<ni;ii++)
00156     {
00157         i->value[0]=ii;
00158         for (jj=0;jj<nj;jj++)
00159         {
00160             j->value[0]=jj;
00161             if (ii==0 && jj==0)
00162                 // effect of parameters is computed only once
00163                 full_fproppath.fprop();
00164             else
00165                 fproppath.fprop();
00166             bproppath.clearGradient();
00167             // PASCAL: peut-etre qu'ici on devrait avoir += ?
00168             // (si l'expression est une Var utilisee ailleurs!)
00169             // REPONSE: Non! (expression ne devrait etre utilisee 
00170             // nulle part ailleurs. To do: utilisation de Func 
00171             // pour le garantir...)
00172             expression->gradient[0] = matGradient(ii,jj);
00173             bproppath.bprop();
00174         }
00175     }
00176 }
00177 
00178 
00179 void MatrixElementsVariable::fbprop()
00180 {
00181     int ii,jj;
00182 
00183     for (ii=0;ii<ni;ii++)
00184     {
00185         i->value[0]=ii;
00186         for (jj=0;jj<nj;jj++)
00187         {
00188             j->value[0]=jj;
00189             if (ii==0 && jj==0)
00190                 // effect of parameters is computed only once
00191                 full_fproppath.fprop();
00192             else
00193                 fproppath.fprop();
00194             matValue(ii,jj) = expression->value[0];
00195             bproppath.clearGradient();
00196             // PASCAL: peut-etre qu'ici on devrait avoir += ?
00197             // (si l'expression est une Var utilisee ailleurs!)
00198             // REPONSE: Non! (expression ne devrait etre utilisee 
00199             // nulle part ailleurs. To do: utilisation de Func 
00200             // pour le garantir...)
00201             expression->gradient[0] = matGradient(ii,jj);
00202             bproppath.bprop();
00203         }
00204     }
00205 }
00206 
00207 } // end of namespace PLearn
00208 
00209 
00210 /*
00211   Local Variables:
00212   mode:c++
00213   c-basic-offset:4
00214   c-file-style:"stroustrup"
00215   c-file-offsets:((innamespace . 0)(inline-open . 0))
00216   indent-tabs-mode:nil
00217   fill-column:79
00218   End:
00219 */
00220 // 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