Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

Var_operators.cc

Go to the documentation of this file.
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,v 1.2 2004/02/20 21:11:54 chrish42 Exp $ 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 "TimesConstantVariable.h" 00059 #include "TimesScalarVariable.h" 00060 #include "TimesColumnVariable.h" 00061 #include "TimesRowVariable.h" 00062 #include "TimesVariable.h" 00063 00064 #include "InvertElementsVariable.h" 00065 #include "DivVariable.h" 00066 00067 #include "EqualVariable.h" 00068 00069 namespace PLearn { 00070 using namespace std; 00071 00072 Var operator+(Var v, real cte) 00073 { return new PlusConstantVariable(v,cte); } 00074 00075 Var operator+(real cte, Var v) 00076 { return new PlusConstantVariable(v,cte); } 00077 00078 Var operator-(Var v, real cte) 00079 { return new PlusConstantVariable(v,-cte); } 00080 00081 Var operator+(Var v1, Var v2) 00082 { 00083 if(v2->isScalar()) 00084 return new PlusScalarVariable(v1,v2); 00085 else if(v1->isScalar()) 00086 return new PlusScalarVariable(v2,v1); 00087 else if(v2->isRowVec()) 00088 return new PlusRowVariable(v1,v2); 00089 else if(v1->isRowVec()) 00090 return new PlusRowVariable(v2,v1); 00091 else if(v2->isColumnVec()) 00092 return new PlusColumnVariable(v1,v2); 00093 else if(v1->isColumnVec()) 00094 return new PlusColumnVariable(v2,v1); 00095 else 00096 return new PlusVariable(v1,v2); 00097 } 00098 00099 void operator+=(Var& v1, const Var& v2) 00100 { 00101 if (!v2.isNull()) 00102 { 00103 if (v1.isNull()) 00104 v1 = v2; 00105 else 00106 v1 = v1 + v2; 00107 } 00108 } 00109 00110 Var operator-(Var v1, Var v2) 00111 { 00112 if(v2->isScalar()) 00113 return new MinusScalarVariable(v1,v2); 00114 else if(v1->isScalar()) 00115 return new PlusScalarVariable(new NegateElementsVariable(v2), v1); 00116 else if(v2->isRowVec()) 00117 return new MinusRowVariable(v1,v2); 00118 else if(v1->isRowVec()) 00119 return new NegateElementsVariable(new MinusRowVariable(v2,v1)); 00120 else if(v2->isColumnVec()) 00121 return new MinusColumnVariable(v1,v2); 00122 else if(v1->isColumnVec()) 00123 return new PlusColumnVariable(new NegateElementsVariable(v2), v1); 00124 else 00125 return new MinusVariable(v1,v2); 00126 } 00127 00128 Var operator-(Var v) 00129 { return new NegateElementsVariable(v); } 00130 00131 void operator-=(Var& v1, const Var& v2) 00132 { 00133 if (!v2.isNull()) 00134 { 00135 if (v1.isNull()) 00136 v1 = -v2; 00137 else 00138 v1 = v1 - v2; 00139 } 00140 } 00141 00142 Var operator-(real cte, Var v) 00143 { return new PlusConstantVariable(new NegateElementsVariable(v),cte); } 00144 00145 00146 00147 Var operator*(Var v, real cte) 00148 { return new TimesConstantVariable(v,cte); } 00149 00150 Var operator*(real cte, Var v) 00151 { return new TimesConstantVariable(v,cte); } 00152 00153 00155 Var operator*(Var v1, Var v2) 00156 { 00157 if(v2->isScalar()) 00158 return new TimesScalarVariable(v1,v2); 00159 else if(v1->isScalar()) 00160 return new TimesScalarVariable(v2,v1); 00161 else if(v2->isColumnVec()) 00162 return new TimesColumnVariable(v1,v2); 00163 else if(v1->isColumnVec()) 00164 return new TimesColumnVariable(v2,v1); 00165 else if(v2->isRowVec()) 00166 return new TimesRowVariable(v1,v2); 00167 else if(v1->isRowVec()) 00168 return new TimesRowVariable(v2,v1); 00169 else 00170 return new TimesVariable(v1,v2); 00171 } 00172 00173 Var operator/(Var v, real cte) 00174 { return new TimesConstantVariable(v, 1.0/cte); } 00175 00176 Var operator/(real cte, Var v) 00177 { 00178 if(cte==1.0) 00179 return invertElements(v); 00180 else 00181 return cte*invertElements(v); 00182 } 00183 00184 Var operator/(Var v1, Var v2) 00185 { 00186 if(v1->length()==v2->length() && v1->width()==v2->width()) 00187 return new DivVariable(v1,v2); 00188 else 00189 return v1*invertElements(v2); 00190 } 00191 00192 Var operator==(Var v1, Var v2) 00193 { return isequal(v1,v2); } 00194 00195 Var operator!=(Var v1, Var v2) 00196 { return (1.0 - isequal(v1,v2) ); } 00197 00198 Var isdifferent(Var v1, Var v2) 00199 { return (1.0 - isequal(v1,v2) ); } 00200 00201 00202 } // end of namespace PLearn 00203

Generated on Tue Aug 17 16:10:02 2004 for PLearn by doxygen 1.3.7