PLearn 0.1
PLMathTest.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLMathTest.cc
00004 //
00005 // Copyright (C) 2005 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 "PLMathTest.h"
00045 #include <plearn/math/pl_math.h>
00046 #include <plearn/math/PRandom.h>
00047 
00048 namespace PLearn {
00049 using namespace std;
00050 
00051 PLEARN_IMPLEMENT_OBJECT(
00052     PLMathTest,
00053     "Test various mathematical functions defined in pl_math.",
00054     ""
00055 );
00056 
00058 // PLMathTest //
00060 PLMathTest::PLMathTest() 
00061     /* ### Initialize all fields to their default value */
00062 {
00063     // ...
00064 
00065     // ### You may (or not) want to call build_() to finish building the object
00066     // ### (doing so assumes the parent classes' build_() have been called too
00067     // ### in the parent classes' constructors, something that you must ensure)
00068 }
00069 
00071 // build //
00073 void PLMathTest::build()
00074 {
00075     inherited::build();
00076     build_();
00077 }
00078 
00080 // makeDeepCopyFromShallowCopy //
00082 void PLMathTest::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00083 {
00084     inherited::makeDeepCopyFromShallowCopy(copies);
00085 
00086     // ### Call deepCopyField on all "pointer-like" fields 
00087     // ### that you wish to be deepCopied rather than 
00088     // ### shallow-copied.
00089     // ### ex:
00090     // deepCopyField(trainvec, copies);
00091 
00092     // ### Remove this line when you have fully implemented this method.
00093     PLERROR("PLMathTest::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00094 }
00095 
00097 // declareOptions //
00099 void PLMathTest::declareOptions(OptionList& ol)
00100 {
00101     // ### Declare all of this object's options here
00102     // ### For the "flags" of each option, you should typically specify  
00103     // ### one of OptionBase::buildoption, OptionBase::learntoption or 
00104     // ### OptionBase::tuningoption. Another possible flag to be combined with
00105     // ### is OptionBase::nosave
00106 
00107     declareOption(ol, "results", &PLMathTest::results, OptionBase::buildoption,
00108         "Map storing all test results.");
00109 
00110     // Now call the parent class' declareOptions
00111     inherited::declareOptions(ol);
00112 }
00113 
00115 // build_ //
00117 void PLMathTest::build_()
00118 {
00119     // ### This method should do the real building of the object,
00120     // ### according to set 'options', in *any* situation. 
00121     // ### Typical situations include:
00122     // ###  - Initial building of an object from a few user-specified options
00123     // ###  - Building of a "reloaded" object: i.e. from the complete set of all serialised options.
00124     // ###  - Updating or "re-building" of an object after a few "tuning" options have been modified.
00125     // ### You should assume that the parent class' build_() has already been called.
00126 }
00127 
00129 // perform //
00131 void PLMathTest::perform()
00132 {
00133     int n = 20;
00134     TVec<int> bounds;
00135     string bounds_str = "[ 1 10 50 1000 ]";
00136     PStream read_bounds = openString(bounds_str, PStream::plearn_ascii);
00137     read_bounds >> bounds;
00138     read_bounds.flush();
00139     Vec samples;
00140     Vec vec(n);
00141     for (int i = 0; i < bounds.length(); i++) {
00142         PRandom::common(false)->fill_random_uniform(vec,-bounds[i],bounds[i]);
00143         samples.append(vec);
00144     }
00145     samples.append(0);
00146     samples.append(1);
00147     samples.append(-1);
00148     samples.append(MISSING_VALUE);
00149     samples.append(INFINITY);
00150     samples.append(-INFINITY);
00151     PP<ProgressBar> pb = new ProgressBar("Performing tests", samples.length());
00152     for (int i = 0; i < samples.length(); i++) {
00153         int j;
00154         real x = samples[i];
00155         results["data"].append(x);
00156         DOUBLE_TO_INT(x, j);
00157         results["DOUBLE_TO_INT"].append(j);
00158         results["sign"].append(sign(x));
00159         results["positive"].append(positive(x));
00160         results["negative"].append(negative(x));
00161         results["is_equal_1.00000001"].append(is_equal(x, x * 1.00000001));
00162         results["is_equal_1.001"].append(is_equal(x, x * 1.001));
00163         results["square"].append(square(x));
00164         results["hinge_loss_1"].append(hinge_loss(x, 1));
00165         results["d_hinge_loss_1"].append(d_hinge_loss(x, 1));
00166         results["is_missing"].append(is_missing(x));
00167         results["is_integer"].append(is_integer(x));
00168         results["FABS"].append(FABS(x));
00169         results["mypow"].append(mypow(x,x));
00170         if (int(x) >= 0) {
00171             results["ipow_real"].append(ipow(x,int(x)));
00172             results["ipow_int"].append(ipow(int(x),int(x)));
00173         } else {
00174             results["ipow_real"].append(0);
00175             results["ipow_int"].append(0);
00176         }
00177         results["sigmoid"].append(sigmoid(x));
00178         results["is_positive"].append(is_positive(x));
00179         if (x >= 0 && x <= 1)
00180             results["inverse_sigmoid"].append(inverse_sigmoid(x));
00181         else
00182             results["inverse_sigmoid"].append(0);
00183         results["softplus"].append(softplus(x));
00184         results["inverse_softplus"].append(inverse_softplus(x));
00185         results["hard_slope"].append(hard_slope(x));
00186         results["soft_slope"].append(soft_slope(x));
00187         results["d_soft_slope"].append(d_soft_slope(x));
00188         results["n_choose"].append(n_choose(int(x), int(x) + 2));
00189         if (x >= 0) {
00190             results["safelog"].append(safelog(x));
00191             results["safeflog"].append(safeflog(x));
00192             results["safeflog"].append(safeflog(x, x + 3));
00193             results["safeflog2"].append(safeflog2(x));
00194         } else {
00195             results["safelog"].append(0);
00196             results["safeflog"].append(0);
00197             results["safeflog"].append(0);
00198             results["safeflog2"].append(0);
00199         }
00200         results["sqrt"].append(sqrt(x));
00201         results["tanh"].append(tanh(x));
00202         results["exp"].append(exp(x));
00203         results["safeexp"].append(safeexp(x));
00204         results["pl_log"].append(pl_log(x));
00205         results["log_a_b"].append(log(x, x + 3));
00206         results["logtwo"].append(logtwo(x));
00207         results["logadd"].append(logadd(x, x + 2));
00208         if (!is_missing(x) && !isinf(x)) {
00209             results["logsub"].append(logsub(x, x - 1));
00210             results["dilogarithm"].append(dilogarithm(x));
00211             results["fasttanh"].append(fasttanh(x));
00212             results["fastsigmoid"].append(fastsigmoid(x));
00213             results["ultrafasttanh"].append(ultrafasttanh(x));
00214             results["ultrafastsigmoid"].append(ultrafastsigmoid(x));
00215             results["tabulated_softplus"].append(tabulated_softplus(x));
00216             results["tabulated_soft_slope"].append(tabulated_soft_slope(x));
00217         } else {
00218             results["logsub"].append(0);
00219             results["dilogarithm"].append(0);
00220             results["fasttanh"].append(0);
00221             results["fastsigmoid"].append(0);
00222             results["ultrafasttanh"].append(0);
00223             results["ultrafastsigmoid"].append(0);
00224             results["tabulated_softplus"].append(0);
00225             results["tabulated_soft_slope"].append(0);
00226         }
00227         if (FABS(x) < 100) {
00228             results["softplus_primitive"].append(softplus_primitive(x));
00229             results["tabulated_softplus_primitive"].append(tabulated_softplus_primitive(x));
00230             results["hard_slope_integral"].append(hard_slope_integral(x));
00231             results["soft_slope_integral"].append(soft_slope_integral(x));
00232             results["tabulated_soft_slope_integral"].append(tabulated_soft_slope_integral(x));
00233         } else {
00234             results["softplus_primitive"].append(0);
00235             results["tabulated_softplus_primitive"].append(0);
00236             results["hard_slope_integral"].append(0);
00237             results["soft_slope_integral"].append(0);
00238             results["tabulated_soft_slope_integral"].append(0);
00239         }
00240         if (pb)
00241             pb->update(i + 1);
00242     }
00243 }
00244 
00245 } // end of namespace PLearn
00246 
00247 
00248 /*
00249   Local Variables:
00250   mode:c++
00251   c-basic-offset:4
00252   c-file-style:"stroustrup"
00253   c-file-offsets:((innamespace . 0)(inline-open . 0))
00254   indent-tabs-mode:nil
00255   fill-column:79
00256   End:
00257 */
00258 // 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