PLearn 0.1
|
00001 00002 // -*- C++ -*- 00003 00004 // PLearn (A C++ Machine Learning Library) 00005 // Copyright (C) 1998 Pascal Vincent 00006 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal 00007 // Copyright (C) 2001-2002 Nicolas Chapados, Ichiro Takeuchi, Jean-Sebastien Senecal 00008 // Copyright (C) 2002 Xiangdong Wang, Christian Dorion 00009 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are met: 00012 // 00013 // 1. Redistributions of source code must retain the above copyright 00014 // notice, this list of conditions and the following disclaimer. 00015 // 00016 // 2. Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the distribution. 00019 // 00020 // 3. The name of the authors may not be used to endorse or promote 00021 // products derived from this software without specific prior written 00022 // permission. 00023 // 00024 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00025 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00026 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00027 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00028 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00029 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00030 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00031 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00032 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00033 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 // 00035 // This file is part of the PLearn library. For more information on the PLearn 00036 // library, go to the PLearn Web site at www.plearn.org 00037 00038 00039 /* ******************************************************* 00040 * $Id: Var_operators.cc 9099 2008-06-03 21:58:54Z plearner $ 00041 * This file is part of the PLearn library. 00042 ******************************************************* */ 00043 00044 #include "Var_operators.h" 00045 00046 #include "PlusConstantVariable.h" 00047 #include "PlusScalarVariable.h" 00048 #include "PlusRowVariable.h" 00049 #include "PlusColumnVariable.h" 00050 #include "PlusVariable.h" 00051 00052 #include "MinusScalarVariable.h" 00053 #include "NegateElementsVariable.h" 00054 #include "MinusRowVariable.h" 00055 #include "MinusColumnVariable.h" 00056 #include "MinusVariable.h" 00057 00058 #include "TimesScalarVariable.h" 00059 #include "TimesColumnVariable.h" 00060 #include "TimesRowVariable.h" 00061 #include "TimesVariable.h" 00062 00063 #include "InvertElementsVariable.h" 00064 #include "DivVariable.h" 00065 00066 #include "EqualVariable.h" 00067 00068 namespace PLearn { 00069 using namespace std; 00070 00071 Var operator+(Var v, real cte) 00072 { return new PlusConstantVariable(v,cte); } 00073 00074 Var operator+(real cte, Var v) 00075 { return new PlusConstantVariable(v,cte); } 00076 00077 Var operator-(Var v, real cte) 00078 { return new PlusConstantVariable(v,-cte); } 00079 00080 Var operator+(Var v1, Var v2) 00081 { 00082 if(v2->isScalar()) 00083 return new PlusScalarVariable(v1,v2); 00084 else if(v1->isScalar()) 00085 return new PlusScalarVariable(v2,v1); 00086 else if(v2->isRowVec()) 00087 return new PlusRowVariable(v1,v2); 00088 else if(v1->isRowVec()) 00089 return new PlusRowVariable(v2,v1); 00090 else if(v2->isColumnVec()) 00091 return new PlusColumnVariable(v1,v2); 00092 else if(v1->isColumnVec()) 00093 return new PlusColumnVariable(v2,v1); 00094 else 00095 return new PlusVariable(v1,v2); 00096 } 00097 00098 void operator+=(Var& v1, const Var& v2) 00099 { 00100 if (!v2.isNull()) 00101 { 00102 if (v1.isNull()) 00103 v1 = v2; 00104 else 00105 v1 = v1 + v2; 00106 } 00107 } 00108 00109 Var operator-(Var v1, Var v2) 00110 { 00111 if(v2->isScalar()) 00112 return new MinusScalarVariable(v1,v2); 00113 else if(v1->isScalar()) 00114 return new PlusScalarVariable(new NegateElementsVariable(v2), v1); 00115 else if(v2->isRowVec()) 00116 return new MinusRowVariable(v1,v2); 00117 else if(v1->isRowVec()) 00118 return new NegateElementsVariable(new MinusRowVariable(v2,v1)); 00119 else if(v2->isColumnVec()) 00120 return new MinusColumnVariable(v1,v2); 00121 else if(v1->isColumnVec()) 00122 return new PlusColumnVariable(new NegateElementsVariable(v2), v1); 00123 else 00124 return new MinusVariable(v1,v2); 00125 } 00126 00127 void operator-=(Var& v1, const Var& v2) 00128 { 00129 if (!v2.isNull()) 00130 { 00131 if (v1.isNull()) 00132 v1 = -v2; 00133 else 00134 v1 = v1 - v2; 00135 } 00136 } 00137 00138 Var operator-(real cte, Var v) 00139 { return new PlusConstantVariable(new NegateElementsVariable(v),cte); } 00140 00141 00143 Var operator*(Var v1, Var v2) 00144 { 00145 if(v2->isScalar()) 00146 return new TimesScalarVariable(v1,v2); 00147 else if(v1->isScalar()) 00148 return new TimesScalarVariable(v2,v1); 00149 else if(v2->isColumnVec()) 00150 return new TimesColumnVariable(v1,v2); 00151 else if(v1->isColumnVec()) 00152 return new TimesColumnVariable(v2,v1); 00153 else if(v2->isRowVec()) 00154 return new TimesRowVariable(v1,v2); 00155 else if(v1->isRowVec()) 00156 return new TimesRowVariable(v2,v1); 00157 else 00158 return new TimesVariable(v1,v2); 00159 } 00160 00161 Var operator/(real cte, Var v) 00162 { 00163 if(fast_exact_is_equal(cte, 1.0)) 00164 return invertElements(v); 00165 else 00166 return cte*invertElements(v); 00167 } 00168 00169 Var operator/(Var v1, Var v2) 00170 { 00171 if(v1->length()==v2->length() && v1->width()==v2->width()) 00172 return new DivVariable(v1,v2); 00173 else 00174 return v1*invertElements(v2); 00175 } 00176 00177 Var operator==(Var v1, Var v2) 00178 { return isequal(v1,v2); } 00179 00180 Var operator!=(Var v1, Var v2) 00181 { return (1.0 - isequal(v1,v2) ); } 00182 00183 Var isdifferent(Var v1, Var v2) 00184 { return (1.0 - isequal(v1,v2) ); } 00185 00186 00187 } // end of namespace PLearn 00188 00189 00190 /* 00191 Local Variables: 00192 mode:c++ 00193 c-basic-offset:4 00194 c-file-style:"stroustrup" 00195 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00196 indent-tabs-mode:nil 00197 fill-column:79 00198 End: 00199 */ 00200 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :