PLearn 0.1
|
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: BinaryVariable.cc 9100 2008-06-03 22:02:41Z plearner $ 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 00043 #include "BinaryVariable.h" 00044 00045 namespace PLearn { 00046 00047 using namespace std; 00048 00049 PLEARN_IMPLEMENT_ABSTRACT_OBJECT( 00050 BinaryVariable, 00051 "Variable that takes two other variables as input.", 00052 "" 00053 ); 00054 00056 // BinaryVariable // 00058 BinaryVariable::BinaryVariable(Variable* v1, Variable* v2, int thelength, 00059 int thewidth, bool call_build_): 00060 inherited(thelength, thewidth, call_build_), 00061 input1(v1), 00062 input2(v2) 00063 { 00064 if (input1) 00065 input1->disallowPartialUpdates(); 00066 if (input2) 00067 input2->disallowPartialUpdates(); 00068 if (call_build_) 00069 build_(); 00070 } 00071 00073 // declareOptions // 00075 void BinaryVariable::declareOptions(OptionList& ol) 00076 { 00077 declareOption(ol, "input1", &BinaryVariable::input1, OptionBase::buildoption, 00078 "The first parent variable that this one depends on\n"); 00079 00080 declareOption(ol, "input2", &BinaryVariable::input2, OptionBase::buildoption, 00081 "The second parent variable that this one depends on\n"); 00082 00083 inherited::declareOptions(ol); 00084 } 00085 00087 // build // 00089 void BinaryVariable::build() 00090 { 00091 inherited::build(); 00092 build_(); 00093 } 00094 00096 // build_ // 00098 void BinaryVariable::build_() {} 00099 00101 // setParents // 00103 void BinaryVariable::setParents(const VarArray& parents) 00104 { 00105 if(parents.length() != 2) 00106 PLERROR("In BinaryVariable::setParents VarArray length must be 2;" 00107 " you are trying to set %d parents for this BinaryVariable...", parents.length()); 00108 00109 input1= parents[0]; 00110 input2= parents[1]; 00111 // int dummy_l, dummy_w; 00112 //recomputeSize(dummy_l, dummy_w); 00113 sizeprop(); 00114 } 00115 00116 #ifdef __INTEL_COMPILER 00117 #pragma warning(disable:1419) // Get rid of compiler warning. 00118 #endif 00119 extern void varDeepCopyField(Var& field, CopiesMap& copies); 00120 #ifdef __INTEL_COMPILER 00121 #pragma warning(default:1419) 00122 #endif 00123 00125 // makeDeepCopyFromShallowCopy // 00127 void BinaryVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00128 { 00129 inherited::makeDeepCopyFromShallowCopy(copies); 00130 varDeepCopyField(input1, copies); 00131 varDeepCopyField(input2, copies); 00132 } 00133 00135 // markPath // 00137 bool BinaryVariable::markPath() 00138 { 00139 if(!marked) 00140 marked = (input1 ? input1->markPath() : false) | 00141 (input2 ? input2->markPath() : false); 00142 return marked; 00143 } 00144 00146 // buildPath // 00148 void BinaryVariable::buildPath(VarArray& proppath) 00149 { 00150 if(marked) 00151 { 00152 if (input1) 00153 input1->buildPath(proppath); 00154 if (input2) 00155 input2->buildPath(proppath); 00156 proppath &= Var(this); 00157 //cout<<"proppath="<<this->getName()<<endl; 00158 clearMark(); 00159 } 00160 } 00161 00163 // sources // 00165 VarArray BinaryVariable::sources() 00166 { 00167 if (marked) 00168 return VarArray(0,0); 00169 marked = true; 00170 if (!input1) 00171 return input2->sources(); 00172 if (!input2) 00173 return input1->sources(); 00174 return input1->sources() & input2->sources(); 00175 } 00176 00178 // random_sources // 00180 VarArray BinaryVariable::random_sources() 00181 { 00182 if (marked) 00183 return VarArray(0,0); 00184 marked = true; 00185 return input1->random_sources() & input2->random_sources(); 00186 } 00187 00189 // ancestors // 00191 VarArray BinaryVariable::ancestors() 00192 { 00193 if (marked) 00194 return VarArray(0,0); 00195 marked = true; 00196 return (input1 ? input1->ancestors() : VarArray(0, 0)) & 00197 (input2 ? input2->ancestors() : VarArray(0, 0)) & Var(this); 00198 } 00199 00201 // unmarkAncestors // 00203 void BinaryVariable::unmarkAncestors() 00204 { 00205 if (marked) 00206 { 00207 marked = false; 00208 if (input1) 00209 input1->unmarkAncestors(); 00210 if (input2) 00211 input2->unmarkAncestors(); 00212 } 00213 } 00214 00216 // parents // 00218 VarArray BinaryVariable::parents() 00219 { 00220 VarArray unmarked_parents; 00221 if (input1 && !input1->marked) 00222 unmarked_parents.append(input1); 00223 if (input2 && !input2->marked) 00224 unmarked_parents.append(input2); 00225 return unmarked_parents; 00226 } 00227 00229 // resizeRValue // 00231 void BinaryVariable::resizeRValue() 00232 { 00233 inherited::resizeRValue(); 00234 if (input1 && !input1->rvaluedata) 00235 input1->resizeRValue(); 00236 if (input2 && !input2->rvaluedata) 00237 input2->resizeRValue(); 00238 } 00239 00240 } // end of namespace PLearn 00241 00242 00243 /* 00244 Local Variables: 00245 mode:c++ 00246 c-basic-offset:4 00247 c-file-style:"stroustrup" 00248 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00249 indent-tabs-mode:nil 00250 fill-column:79 00251 End: 00252 */ 00253 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :