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

ConcatColumnsVMatrix.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2001 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal 00006 // Copyright (C) 2002 Pascal Vincent, Julien Keable, Xavier Saint-Mleux 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 /* ******************************************************* 00038 * $Id: ConcatColumnsVMatrix.cc,v 1.12 2004/07/22 21:33:34 tatien Exp $ 00039 ******************************************************* */ 00040 00041 #include "ConcatColumnsVMatrix.h" 00042 00043 namespace PLearn { 00044 using namespace std; 00045 00048 PLEARN_IMPLEMENT_OBJECT(ConcatColumnsVMatrix, "ONE LINE DESCR", "NO HELP"); 00049 00050 ConcatColumnsVMatrix::ConcatColumnsVMatrix(Array<VMat> the_array) 00051 : array(the_array), 00052 no_duplicate_fieldnames(false) 00053 { if (array.size()) build_(); } 00054 00055 ConcatColumnsVMatrix::ConcatColumnsVMatrix(VMat d1, VMat d2) 00056 : array(d1, d2), 00057 no_duplicate_fieldnames(false) 00058 { build_(); } 00059 00060 00061 void ConcatColumnsVMatrix::declareOptions(OptionList &ol) 00062 { 00063 declareOption(ol, "array", &ConcatColumnsVMatrix::array, OptionBase::buildoption, "Array of VMatrices"); 00064 declareOption(ol, "no_duplicate_fieldnames", &ConcatColumnsVMatrix::no_duplicate_fieldnames, OptionBase::buildoption, 00065 "If set to 1, will ensure no fieldnames are duplicated by adding numerical values '.1', '.2', ..."); 00066 inherited::declareOptions(ol); 00067 } 00068 00069 void ConcatColumnsVMatrix::build() 00070 { 00071 inherited::build(); 00072 build_(); 00073 } 00074 00075 void ConcatColumnsVMatrix::build_() 00076 { 00077 length_ = width_ = 0; 00078 if(array.size()) 00079 length_ = array[0]->length(); 00080 // else 00081 // PLERROR("ConcatColumnsVMatrix expects >= 1 underlying-array, got 0"); 00082 00083 for(int i=0; i<array.size(); i++) 00084 { 00085 if(array[i]->length()!=length_) 00086 PLERROR("ConcatColumnsVMatrix: Problem concatenating to VMatrices with different lengths"); 00087 if(array[i]->width() == -1) 00088 PLERROR("In ConcatColumnsVMatrix constructor. Non-fixed width distribution not supported"); 00089 width_ += array[i]->width(); 00090 } 00091 00092 // Copy the original fieldinfos. Be careful if only some of the 00093 // matrices have fieldinfos 00094 fieldinfos.resize(width_); 00095 TVec<string> names; 00096 int fieldindex = 0; 00097 for (int i=0; i<array.size(); ++i) 00098 { 00099 int len = array[i]->getFieldInfos().size(); 00100 if (len > 0) // infos exist for this VMat 00101 { 00102 for (int j=0; j<len; ++j) 00103 fieldinfos[fieldindex++] = array[i]->getFieldInfos()[j]; 00104 } 00105 else // infos don't exist for this VMat, use the index as the name for those fields. 00106 { 00107 len = array[i]->width(); 00108 for(int j=0; j<len; ++j) 00109 fieldinfos[fieldindex++] = VMField(tostring(fieldindex)); 00110 } 00111 } 00112 if (no_duplicate_fieldnames) { 00113 unduplicateFieldNames(); 00114 } 00115 } 00116 00117 void ConcatColumnsVMatrix::getNewRow(int i, const Vec& samplevec) const 00118 { 00119 if (length_==-1) 00120 PLERROR("In ConcatColumnsVMatrix::getNewRow(int i, Vec samplevec) not supported for distributions with different (or infinite) lengths\nCall sample without index instead"); 00121 int pos = 0; 00122 for(int n=0; n<array.size(); n++) 00123 { 00124 int nvars = array[n]->width(); 00125 Vec samplesubvec = samplevec.subVec(pos, nvars); 00126 array[n]->getRow(i,samplesubvec); 00127 pos += nvars; 00128 } 00129 } 00130 00131 real ConcatColumnsVMatrix::getStringVal(int col, const string & str) const 00132 { 00133 if(col>=width_) 00134 PLERROR("access out of bound. Width=%i accessed col=%i",width_,col); 00135 int pos=0,k=0; 00136 while(col>=pos+array[k]->width()) 00137 { 00138 pos += array[k]->width(); 00139 k++; 00140 } 00141 // return array[k]->getStringVal(pos+col,str); 00142 return array[k]->getStringVal(col-pos,str); 00143 } 00144 00145 string ConcatColumnsVMatrix::getValString(int col, real val) const 00146 { 00147 if(col>=width_) 00148 PLERROR("access out of bound. Width=%i accessed col=%i",width_,col); 00149 int pos=0,k=0; 00150 while(col>=pos+array[k]->width()) 00151 { 00152 pos += array[k]->width(); 00153 k++; 00154 } 00155 // return array[k]->getValString(pos+col,val); 00156 return array[k]->getValString(col-pos,val); 00157 } 00158 00159 const map<string,real>& ConcatColumnsVMatrix::getStringMapping(int col) const 00160 { 00161 if(col>=width_) 00162 PLERROR("access out of bound. Width=%i accessed col=%i",width_,col); 00163 int pos=0,k=0; 00164 while(col>=pos+array[k]->width()) 00165 { 00166 pos += array[k]->width(); 00167 k++; 00168 } 00169 // return array[k]->getStringToRealMapping(pos+col); 00170 return array[k]->getStringToRealMapping(col-pos); 00171 } 00172 00173 string ConcatColumnsVMatrix::getString(int row, int col) const 00174 { 00175 if(col>=width_) 00176 PLERROR("access out of bound. Width=%i accessed col=%i",width_,col); 00177 int pos=0,k=0; 00178 while(col>=pos+array[k]->width()) 00179 { 00180 pos += array[k]->width(); 00181 k++; 00182 } 00183 // return array[k]->getString(row,pos+col); 00184 return array[k]->getString(row,col-pos); 00185 } 00186 00187 00188 00189 real ConcatColumnsVMatrix::dot(int i1, int i2, int inputsize) const 00190 { 00191 real res = 0.; 00192 for(int k=0; ;k++) 00193 { 00194 const VMat& vm = array[k]; 00195 int vmwidth = vm.width(); 00196 if(inputsize<=vmwidth) 00197 { 00198 res += vm->dot(i1,i2,inputsize); 00199 break; 00200 } 00201 else 00202 { 00203 res += vm->dot(i1,i2,vmwidth); 00204 inputsize -= vmwidth; 00205 } 00206 } 00207 return res; 00208 } 00209 00210 real ConcatColumnsVMatrix::dot(int i, const Vec& v) const 00211 { 00212 if (length_==-1) 00213 PLERROR("In ConcatColumnsVMatrix::getRow(int i, Vec samplevec) not supported for distributions with different (or infinite) lengths\nCall sample without index instead"); 00214 00215 real res = 0.; 00216 int pos = 0; 00217 for(int n=0; n<array.size(); n++) 00218 { 00219 int nvars = std::min(array[n]->width(),v.length()-pos); 00220 if(nvars<=0) 00221 break; 00222 Vec subv = v.subVec(pos, nvars); 00223 res += array[n]->dot(i,subv); 00224 pos += nvars; 00225 } 00226 return res; 00227 } 00228 00229 00230 } // end of namespcae PLearn

Generated on Tue Aug 17 15:49:41 2004 for PLearn by doxygen 1.3.7