PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // TMatTest.cc 00004 // 00005 // Copyright (C) 2005-2006 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 /* ******************************************************* 00036 * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 00037 ******************************************************* */ 00038 00039 // Authors: Olivier Delalleau 00040 00044 #include "TMatTest.h" 00045 #include <plearn/math/PRandom.h> 00046 #include <plearn/math/TMat_maths.h> 00047 #include <plearn/math/TMat_maths_specialisation.h> 00048 00049 namespace PLearn { 00050 using namespace std; 00051 00052 PLEARN_IMPLEMENT_OBJECT( 00053 TMatTest, 00054 "Test TMat mathematical functions.", 00055 "" 00056 ); 00057 00059 // TMatTest // 00061 TMatTest::TMatTest(): 00062 bound(10), 00063 mat_length(2), 00064 mat_width(3), 00065 vec_length(10) 00066 {} 00067 00069 // build // 00071 void TMatTest::build() 00072 { 00073 inherited::build(); 00074 build_(); 00075 } 00076 00078 // makeDeepCopyFromShallowCopy // 00080 void TMatTest::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00081 { 00082 inherited::makeDeepCopyFromShallowCopy(copies); 00083 00084 // ### Call deepCopyField on all "pointer-like" fields 00085 // ### that you wish to be deepCopied rather than 00086 // ### shallow-copied. 00087 // ### ex: 00088 // deepCopyField(trainvec, copies); 00089 00090 // ### Remove this line when you have fully implemented this method. 00091 PLERROR("TMatTest::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00092 } 00093 00095 // declareOptions // 00097 void TMatTest::declareOptions(OptionList& ol) 00098 { 00099 declareOption(ol, "vec_length", &TMatTest::vec_length, 00100 OptionBase::buildoption, 00101 "Length of the vector on which the TMat functions are to be applied."); 00102 00103 declareOption(ol, "mat_length", &TMatTest::mat_length, 00104 OptionBase::buildoption, 00105 "Length of the matrix on which the TMat functions are to be applied."); 00106 00107 declareOption(ol, "mat_width", &TMatTest::mat_width, 00108 OptionBase::buildoption, 00109 "Width of the matrix on which the TMat functions are to be applied."); 00110 00111 declareOption(ol, "bound", &TMatTest::bound, 00112 OptionBase::buildoption, 00113 "Bound for the (real) values sampled in the vector."); 00114 00115 declareOption(ol, "mat_options", &TMatTest::mat_options, 00116 OptionBase::learntoption, 00117 "Matrices."); 00118 00119 declareOption(ol, "real_options", &TMatTest::real_options, 00120 OptionBase::learntoption, 00121 "Real numbers"); 00122 00123 declareOption(ol, "vec_options", &TMatTest::vec_options, 00124 OptionBase::learntoption, 00125 "Vectors"); 00126 00127 // Now call the parent class' declareOptions 00128 inherited::declareOptions(ol); 00129 } 00130 00132 // build_ // 00134 void TMatTest::build_() 00135 { 00136 // ### This method should do the real building of the object, 00137 // ### according to set 'options', in *any* situation. 00138 // ### Typical situations include: 00139 // ### - Initial building of an object from a few user-specified options 00140 // ### - Building of a "reloaded" object: i.e. from the complete set of all serialised options. 00141 // ### - Updating or "re-building" of an object after a few "tuning" options have been modified. 00142 // ### You should assume that the parent class' build_() has already been called. 00143 } 00144 00146 // perform // 00148 void TMatTest::perform() 00149 { 00150 PLASSERT( vec_length > 0 ); // TODO Make it work with vec_length == 0 00151 PLASSERT( bound > 0 ); 00152 Vec vec( vec_length ); 00153 PRandom::common(false)->fill_random_uniform(vec, -bound, bound); 00154 Mat mat(mat_length, mat_width); 00155 PRandom::common(false)->fill_random_uniform(mat, -bound, bound); 00156 00157 pout << "Starting TMatTest with vector of length " << vec_length 00158 << " and matrix of size (" << mat_length << " x " << mat_width 00159 << ")" << flush; 00160 00161 this->vec_options["vector"] = vec; 00162 00163 this->vec_options ["sign"] = sign ( vec ); 00164 00165 this->real_options["sum"] = sum ( vec ); 00166 this->real_options["sum (ignore missing)"] = sum ( vec, true ); 00167 00168 this->real_options["sumabs"] = sumabs ( vec ); 00169 this->real_options["sumsquare"] = sumsquare ( vec ); 00170 this->real_options["sum_of_log"] = sum_of_log ( vec ); 00171 this->real_options["product"] = product ( vec ); 00172 00173 this->real_options["mean"] = mean ( vec ); 00174 this->real_options["mean (ignore missing)"] = mean ( vec, true ); 00175 00176 this->real_options["harmonic_mean"] = harmonic_mean ( vec ); 00177 this->real_options["harmonic_mean (ignore_missing)"] 00178 = harmonic_mean ( vec, true ); 00179 00180 this->real_options["min"] = min ( vec ); 00181 this->real_options["argmin"] = argmin ( vec ); 00182 00183 this->real_options["max"] = max ( vec ); 00184 this->real_options["argmax"] = argmax ( vec ); 00185 00186 this->real_options["norm"] = norm ( vec ); 00187 this->vec_options ["log"] = log ( vec ); 00188 this->vec_options ["sqrt"] = sqrt ( vec ); 00189 this->vec_options ["tanh"] = tanh ( vec ); 00190 this->vec_options ["fasttanh"] = fasttanh ( vec ); 00191 00192 this->vec_options ["inverted"] = inverted ( vec ); 00193 this->vec_options ["square"] = square ( vec ); 00194 this->vec_options ["squareroot"] = squareroot ( vec ); 00195 this->vec_options ["remove_missing"] = remove_missing( vec ); 00196 this->vec_options ["softmax"] = softmax ( vec ); 00197 this->vec_options ["exp"] = exp ( vec ); 00198 this->vec_options ["nonZeroIndices"] = nonZeroIndices( vec ); 00199 this->real_options["logadd"] = logadd ( vec ); 00200 this->real_options["median"] = median ( vec ); 00201 00202 Vec scaled_vec = vec.copy(); 00203 scaled_vec *= scaled_vec[0]; 00204 this->vec_options["operator*=_vec"] = scaled_vec; 00205 00206 Vec multiply_acc_vec = vec.copy(); 00207 multiplyAcc(multiply_acc_vec, vec, -scaled_vec[0]); 00208 this->vec_options["multiplyAcc_vec"] = multiply_acc_vec; 00209 00210 Mat scaled_mat = mat.copy(); 00211 scaled_mat *= scaled_mat(0, 0); 00212 this->mat_options["operator*=_mat"] = scaled_mat; 00213 00214 Mat multiply_acc_mat = mat.copy(); 00215 multiplyAcc(multiply_acc_mat, mat, -scaled_mat(0, 0)); 00216 this->mat_options["multiplyAcc_mat"] = multiply_acc_mat; 00217 00218 // Now test a specific case where problems can occur: the multiplication of 00219 // a matrix (m x 0) by a vector of size 0. The result must be a vector of 00220 // size m, filled with 0. 00221 Vec vec_result(2, 1); 00222 Mat mat_m_by_zero(2, 0); 00223 Vec vec_zero; 00224 product(vec_result, mat_m_by_zero, vec_zero); 00225 this->vec_options["product_empty_matrix_by_empty_vector"] = 00226 vec_result.copy(); 00227 00228 // Similar but with accumulation. 00229 vec_result.fill(1); 00230 productAcc(vec_result, mat_m_by_zero, vec_zero); 00231 this->vec_options["product_empty_matrix_by_empty_vector_acc"] = vec_result; 00232 00233 // Similar tests with matrices. 00234 Mat mat_result(2, 1, 1); 00235 Mat mat_zero(0, 1); 00236 product(mat_result, mat_m_by_zero, mat_zero); 00237 this->mat_options["product_empty_matrix_by_empty_matrix"] = 00238 mat_result.copy(); 00239 mat_result.fill(1); 00240 productAcc(mat_result, mat_m_by_zero, mat_zero); 00241 this->mat_options["product_empty_matrix_by_empty_matrix_acc"] = mat_result; 00242 00243 pout << "... DONE!" << endl; 00244 } 00245 00246 } // end of namespace PLearn 00247 00248 00249 /* 00250 Local Variables: 00251 mode:c++ 00252 c-basic-offset:4 00253 c-file-style:"stroustrup" 00254 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00255 indent-tabs-mode:nil 00256 fill-column:79 00257 End: 00258 */ 00259 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :