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

LocalNeighborsDifferencesVMatrix.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // LocalNeighborsDifferencesVMatrix.cc 00004 // 00005 // Copyright (C) 2004 Martin Monperrus 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: LocalNeighborsDifferencesVMatrix.cc,v 1.8 2004/08/03 18:18:41 larocheh Exp $ 00037 ******************************************************* */ 00038 00039 // Authors: Martin Monperrus 00040 00044 #include "LocalNeighborsDifferencesVMatrix.h" 00045 #include "VMat_maths.h" 00046 00047 namespace PLearn { 00048 using namespace std; 00049 00050 00051 LocalNeighborsDifferencesVMatrix::LocalNeighborsDifferencesVMatrix() 00052 :inherited(), n_neighbors(-1), concat_neighbors(false), append_indexes(false) 00053 /* ### Initialise all fields to their default value */ 00054 { 00055 } 00056 00057 PLEARN_IMPLEMENT_OBJECT(LocalNeighborsDifferencesVMatrix, 00058 "Computes the difference between each input row and its nearest neighbors.", 00059 "For each row x of the source VMatrix, the resulting row will be the\n" 00060 "concatenation of n_neighbors vectors, each of which is the difference\n" 00061 "between one of the nearest neighbors of x in the source and x itself.\n" 00062 ); 00063 00064 void LocalNeighborsDifferencesVMatrix::getNewRow(int i, const Vec& v) const 00065 { 00066 if (width_<0) 00067 PLERROR("LocalNeighborsDifferencesVMatrix::getNewRow called but build was not done yet"); 00068 if (concat_neighbors) 00069 { 00070 // resize des varaibles 00071 //v.resize(source->width()); //we assume that the vector has already the good size 00072 neighbor_row.resize(source->width()); 00073 ith_row.resize(source->width()); 00074 diff_k = v; 00075 // recuperation de ce qu'il faut 00076 source->getRow(i,ith_row); 00077 source->getRow(neighbors(i,n_neighbors-1),neighbor_row); 00078 00079 // on renvoie la valeur 00080 substract(neighbor_row,ith_row, diff_k); 00081 00082 if(append_indexes) 00083 { 00084 v[source->width()] = i; 00085 v[source->width()+1] = neighbors(i,n_neighbors-1); 00086 } 00087 00088 } 00089 else 00090 { 00091 // vue en matrice du vecteur de sortie, ca pointe sur les memes donnees. 00092 Mat differences = v.toMat(n_neighbors,source->width()); 00093 neighbor_row.resize(source->width()); 00094 ith_row.resize(source->width()); 00095 source->getRow(i,ith_row); 00096 for (int k=0;k<n_neighbors;k++) 00097 { 00098 diff_k = differences(k); 00099 source->getRow(neighbors(i,k),neighbor_row); 00100 substract(neighbor_row,ith_row, diff_k); 00101 // normalize result 00102 // now it's done in ProjectionErrorVariable diff_k /= norm(diff_k); 00103 } 00104 00105 if(append_indexes) 00106 { 00107 v[n_neighbors*source->width()] = i; 00108 for(int k=0; k<n_neighbors; k++) 00109 v[n_neighbors*source->width()+k+1] = neighbors(i,k); 00110 } 00111 } 00112 00113 } 00114 00115 void LocalNeighborsDifferencesVMatrix::declareOptions(OptionList& ol) 00116 { 00117 declareOption(ol, "n_neighbors", &LocalNeighborsDifferencesVMatrix::n_neighbors, OptionBase::buildoption, 00118 "Number of nearest neighbors. Determines the width of this vmatrix, which\n" 00119 "is source->width() * n_neighbors.\n"); 00120 declareOption(ol, "concat_neighbors", &LocalNeighborsDifferencesVMatrix::concat_neighbors, OptionBase::buildoption, 00121 "If true, returns the concatenation of nearest neighbors(x) -x from 1 to n_neighbors , else, returns the n_neighbor nearest neighbor(x) - x"); 00122 declareOption(ol, "append_indexes", &LocalNeighborsDifferencesVMatrix::append_indexes, OptionBase::buildoption, 00123 "Indication that the indexes of the current data point and of the k nearest neighbors\n" 00124 "should be appended to the row of the VMatrix.\n"); 00125 00126 // Now call the parent class' declareOptions 00127 inherited::declareOptions(ol); 00128 } 00129 00130 void LocalNeighborsDifferencesVMatrix::build_() 00131 { 00132 // find the nearest neighbors, if not done already 00133 if (source && (neighbors.length()==0 || source->length()!=length_ || source->width()*n_neighbors!=width_)) 00134 // will not work if source is changed but has the same dimensions 00135 { 00136 if (concat_neighbors) 00137 { 00138 width_ = source->width(); 00139 if(append_indexes) width_ += 2; 00140 } 00141 else 00142 { 00143 width_ = source->width()*n_neighbors; 00144 if(append_indexes) width_ += n_neighbors+1; 00145 } 00146 00147 length_ = source->length(); 00148 neighbors.resize(source->length(),n_neighbors); 00149 a_row.resize(source->width()); 00150 for (int i=0;i<source->length();i++) 00151 { 00152 source->getRow(i,a_row); 00153 TVec<int> neighbors_of_i = neighbors(i); 00154 computeNearestNeighbors(source,a_row,neighbors_of_i,i); 00155 } 00156 } 00157 } 00158 00159 // ### Nothing to add here, simply calls build_ 00160 void LocalNeighborsDifferencesVMatrix::build() 00161 { 00162 inherited::build(); 00163 build_(); 00164 } 00165 00166 void LocalNeighborsDifferencesVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00167 { 00168 inherited::makeDeepCopyFromShallowCopy(copies); 00169 deepCopyField(neighbor_row, copies); 00170 deepCopyField(ith_row, copies); 00171 deepCopyField(a_row, copies); 00172 deepCopyField(neighbors, copies); 00173 } 00174 00175 } // end of namespace PLearn 00176

Generated on Tue Aug 17 15:57:54 2004 for PLearn by doxygen 1.3.7