PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // Ker_VMat_utils.cc 00004 // 00005 // Copyright (C) 2004 Pascal Vincent 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: Ker_VMat_utils.cc 3994 2005-08-25 13:35:03Z chapados $ 00037 ******************************************************* */ 00038 00039 // Authors: Pascal Vincent 00040 00044 #include "Ker_VMat_utils.h" 00045 #include <plearn/ker/Kernel.h> 00046 #include <plearn/vmat/VMat.h> 00047 #include <plearn/math/TVec.h> 00048 #include <plearn/math/TopNI.h> 00049 #include <plearn/math/BottomNI.h> 00050 00051 namespace PLearn { 00052 using namespace std; 00053 00054 // This will compute for this vmat m a result vector (whose length must be tha same as m's) 00055 // s.t. result[i] = ker( m(i).subVec(v1_startcol,v1_ncols) , v2) 00056 // i.e. the kernel value betweeen each (sub)row of m and v2 00057 void evaluateKernel(Ker ker, VMat vm, int v1_startcol, int v1_ncols, 00058 const Vec& v2, const Vec& result, int startrow, int nrows) 00059 { 00060 int l = vm->length(); 00061 int endrow = (nrows>0) ?startrow+nrows :l; 00062 if(result.length() != endrow-startrow) 00063 PLERROR("In evaluateKernel length of result vector does not match the row range"); 00064 00065 Vec v1(v1_ncols); 00066 for(int i=startrow; i<endrow; i++) 00067 { 00068 vm->getSubRow(i,v1_startcol,v1); 00069 result[i] = ker(v1,v2); 00070 } 00071 } 00072 00073 // returns sum_i [ ker( m(i).subVec(v1_startcol,v1_ncols) , v2) ] 00074 real evaluateKernelSum(Ker ker, VMat vm, int v1_startcol, int v1_ncols, 00075 const Vec& v2, int startrow, int nrows, int ignore_this_row) 00076 { 00077 int l = vm->length(); 00078 int endrow = (nrows>0) ?startrow+nrows :l; 00079 double result = 0.; 00080 Vec v1(v1_ncols); 00081 for(int i=startrow; i<endrow; i++) 00082 if(i!=ignore_this_row) 00083 { 00084 vm->getSubRow(i,v1_startcol,v1); 00085 result += ker(v1,v2); 00086 } 00087 return (real)result; 00088 } 00089 00090 // targetsum := sum_i [ m(i).subVec(t_startcol,t_ncols) * ker( m(i).subVec(v1_startcol,v1_ncols) , v2) ] 00091 // and returns sum_i [ ker( m(i).subVec(v1_startcol,v1_ncols) , v2) ] 00092 real evaluateKernelWeightedTargetSum(Ker ker, VMat vm, int v1_startcol, int v1_ncols, const Vec& v2, 00093 int t_startcol, int t_ncols, Vec& targetsum, int startrow, int nrows, int ignore_this_row) 00094 { 00095 int l = vm->length(); 00096 int endrow = (nrows>0) ?startrow+nrows :l; 00097 targetsum.clear(); 00098 double result = 0.; 00099 Vec v1(v1_ncols); 00100 Vec target(t_ncols); 00101 for(int i=startrow; i<endrow; i++) 00102 if(i!=ignore_this_row) 00103 { 00104 vm->getSubRow(i,v1_startcol,v1); 00105 vm->getSubRow(i,t_startcol,target); 00106 real kerval = ker(v1,v2); 00107 result += kerval; 00108 multiplyAcc(targetsum, target, kerval); 00109 } 00110 return (real)result; 00111 } 00112 00113 TVec< pair<real,int> > evaluateKernelTopN(int N, Ker ker, VMat vm, int v1_startcol, int v1_ncols, 00114 const Vec& v2, int startrow, int nrows, int ignore_this_row) 00115 { 00116 int l = vm->length(); 00117 int endrow = (nrows>0) ?startrow+nrows :l; 00118 TopNI<real> extrema(N); 00119 Vec v1(v1_ncols); 00120 for(int i=startrow; i<endrow; i++) 00121 if(i!=ignore_this_row) 00122 { 00123 vm->getSubRow(i,v1_startcol,v1); 00124 real kerval = ker(v1,v2); 00125 extrema.update(kerval,i); 00126 } 00127 extrema.sort(); 00128 return extrema.getTopN(); 00129 } 00130 00131 TVec< pair<real,int> > evaluateKernelBottomN(int N, Ker ker, VMat vm, int v1_startcol, int v1_ncols, 00132 const Vec& v2, int startrow, int nrows, int ignore_this_row) 00133 { 00134 int l = vm->length(); 00135 int endrow = (nrows>0) ?startrow+nrows :l; 00136 BottomNI<real> extrema(N); 00137 Vec v1(v1_ncols); 00138 for(int i=startrow; i<endrow; i++) 00139 if(i!=ignore_this_row) 00140 { 00141 vm->getSubRow(i,v1_startcol,v1); 00142 real kerval = ker(v1,v2); 00143 extrema.update(kerval,i); 00144 } 00145 extrema.sort(); 00146 return extrema.getBottomN(); 00147 } 00148 00149 00150 00151 00152 } // end of namespace PLearn 00153 00154 00155 /* 00156 Local Variables: 00157 mode:c++ 00158 c-basic-offset:4 00159 c-file-style:"stroustrup" 00160 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00161 indent-tabs-mode:nil 00162 fill-column:79 00163 End: 00164 */ 00165 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :