PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 2007 Jerome Louradour 00005 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // 1. Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // 00012 // 2. Redistributions in binary form must reproduce the above copyright 00013 // notice, this list of conditions and the following disclaimer in the 00014 // documentation and/or other materials provided with the distribution. 00015 // 00016 // 3. The name of the authors may not be used to endorse or promote 00017 // products derived from this software without specific prior written 00018 // permission. 00019 // 00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 // 00031 // This file is part of the PLearn library. For more information on the PLearn 00032 // library, go to the PLearn Web site at www.plearn.org 00033 00034 00035 /* ******************************************************* 00036 * $Id: CosKernel.cc 7675 2007-06-29 19:50:49Z tihocan $ 00037 * This file is part of the PLearn library. 00038 ******************************************************* */ 00039 00040 #include "CosKernel.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00045 00046 PLEARN_IMPLEMENT_OBJECT( 00047 CosKernel, 00048 "Implements a distance based on a cosinus.", 00049 "Output k(x,y)=(0.5 - 0.5*cos(x,y)) (between 0 and 1)\n"); 00050 00052 // CosKernel // 00054 CosKernel::CosKernel() 00055 { 00056 n=2.0; 00057 optimized=false; 00058 pow_distance=false; 00059 ignore_missing=false; 00060 } 00061 00063 // declareOptions // 00065 void CosKernel::declareOptions(OptionList& ol) 00066 { 00067 inherited::declareOptions(ol); 00068 00069 redeclareOption(ol, "n", &CosKernel::n, OptionBase::nosave, 00070 "Obsolete option for cosinus kernel."); 00071 00072 redeclareOption(ol, "pow_distance", &CosKernel::pow_distance, OptionBase::nosave, 00073 "Obsolete option for cosinus kernel."); 00074 00075 redeclareOption(ol, "optimized", &CosKernel::optimized, OptionBase::nosave, 00076 "Obsolete option for cosinus kernel."); 00077 00078 redeclareOption(ol, "ignore_missing", &CosKernel::ignore_missing, OptionBase::nosave, 00079 "Obsolete option for cosinus kernel."); 00080 } 00081 00083 // evaluate // 00085 real CosKernel::evaluate(const Vec& x1, const Vec& x2) const { 00086 if (ignore_missing && !pow_distance) 00087 PLERROR("In CosKernel::evaluate(int i, int j) - 'ignore_missing' " 00088 "implemented only if pow_distance is set"); 00089 static real cosinus; 00090 cosinus = dot(x1, x2) / ( norm(x1) * norm(x2) ); 00091 return (1.-cosinus)*.5; 00092 } 00093 00095 // evaluate_i_j // 00097 real CosKernel::evaluate_i_j(int i, int j) const { 00098 static real cosinus; 00099 if (i == j) 00100 // The case 'i == j' can cause precision issues because of the optimized 00101 // formula below. Thus we make sure we always return 0. 00102 return 0; 00103 cosinus = data->dot(i, j, data_inputsize) / sqrt(squarednorms[i] * squarednorms[j]); 00104 return (1.-cosinus)*.5; 00105 } 00106 00107 } // end of namespace PLearn 00108 00109 00110 /* 00111 Local Variables: 00112 mode:c++ 00113 c-basic-offset:4 00114 c-file-style:"stroustrup" 00115 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00116 indent-tabs-mode:nil 00117 fill-column:79 00118 End: 00119 */ 00120 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :