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

SelectColumnsVMatrix.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 // Copyright (C) 2003 Olivier Delalleau 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are met: 00011 // 00012 // 1. Redistributions of source code must retain the above copyright 00013 // notice, this list of conditions and the following disclaimer. 00014 // 00015 // 2. Redistributions in binary form must reproduce the above copyright 00016 // notice, this list of conditions and the following disclaimer in the 00017 // documentation and/or other materials provided with the distribution. 00018 // 00019 // 3. The name of the authors may not be used to endorse or promote 00020 // products derived from this software without specific prior written 00021 // permission. 00022 // 00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 // 00034 // This file is part of the PLearn library. For more information on the PLearn 00035 // library, go to the PLearn Web site at www.plearn.org 00036 00037 00038 /* ******************************************************* 00039 * $Id: SelectColumnsVMatrix.cc,v 1.9 2004/08/09 16:23:52 tihocan Exp $ 00040 ******************************************************* */ 00041 00042 #include "SelectColumnsVMatrix.h" 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00049 PLEARN_IMPLEMENT_OBJECT(SelectColumnsVMatrix, 00050 "Selects variables from a source matrix according to given vector of indices.", 00051 "Alternatively, the variables can be given by their names." 00052 ); 00053 00055 // SelectColumnsVMatrix // 00057 SelectColumnsVMatrix::SelectColumnsVMatrix() 00058 {} 00059 00060 SelectColumnsVMatrix::SelectColumnsVMatrix(VMat the_source, TVec<string> the_fields) 00061 : fields(the_fields) 00062 { 00063 source = the_source; 00064 build_(); 00065 } 00066 00067 SelectColumnsVMatrix::SelectColumnsVMatrix(VMat the_source, TVec<int> the_indices) 00068 : indices(the_indices) 00069 { 00070 source = the_source; 00071 build_(); 00072 } 00073 00074 SelectColumnsVMatrix::SelectColumnsVMatrix(VMat the_source, Vec the_indices) 00075 { 00076 source = the_source; 00077 indices.resize(the_indices.length()); 00078 // copy the real the_indices into the integer indices 00079 indices << the_indices; 00080 build_(); 00081 } 00082 00083 real SelectColumnsVMatrix::get(int i, int j) const 00084 { return source->get(i, indices[j]); } 00085 00086 void SelectColumnsVMatrix::getSubRow(int i, int j, Vec v) const 00087 { 00088 for(int jj=0; jj<v.length(); jj++) 00089 v[jj] = source->get(i, indices[j+jj]); 00090 } 00091 00092 void SelectColumnsVMatrix::declareOptions(OptionList &ol) 00093 { 00094 declareOption(ol, "fields", &SelectColumnsVMatrix::fields, OptionBase::buildoption, 00095 "The names of the fields to extract (will override 'indices' if provided)."); 00096 00097 declareOption(ol, "indices", &SelectColumnsVMatrix::indices, OptionBase::buildoption, 00098 "The array of column indices to extract."); 00099 00100 inherited::declareOptions(ol); 00101 } 00102 00103 void SelectColumnsVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00104 { 00105 inherited::makeDeepCopyFromShallowCopy(copies); 00106 deepCopyField(indices, copies); 00107 } 00108 00110 // build // 00112 void SelectColumnsVMatrix::build() 00113 { 00114 inherited::build(); 00115 build_(); 00116 } 00117 00119 // build_ // 00121 void SelectColumnsVMatrix::build_() 00122 { 00123 if (source) { 00124 if (fields.isNotEmpty()) { 00125 // Find out the indices from the fields. 00126 indices.clear(); 00127 for (int i = 0; i < fields.length(); i++) { 00128 string the_field = fields[i]; 00129 indices.append(source->getFieldIndex(the_field)); 00130 } 00131 } 00132 width_ = indices.length(); 00133 length_ = source->length(); 00134 // Copy the appropriate VMFields 00135 fieldinfos.resize(width()); 00136 if (source->getFieldInfos().size() > 0) 00137 for (int i=0; i<width(); ++i) 00138 fieldinfos[i] = source->getFieldInfos()[indices[i]]; 00139 } 00140 } 00141 00143 // getStringToRealMapping // 00145 const map<string,real>& SelectColumnsVMatrix::getStringToRealMapping(int col) const { 00146 return source->getStringToRealMapping(indices[col]); 00147 } 00148 00150 // getStringVal // 00152 real SelectColumnsVMatrix::getStringVal(int col, const string & str) const { 00153 return source->getStringVal(indices[col], str); 00154 } 00155 00156 00158 // getRealToStringMapping // 00160 const map<real,string>& SelectColumnsVMatrix::getRealToStringMapping(int col) const { 00161 return source->getRealToStringMapping(indices[col]); 00162 } 00163 00165 // getValString // 00167 string SelectColumnsVMatrix::getValString(int col, real val) const { 00168 return source->getValString(indices[col], val); 00169 } 00170 00171 } // end of namespcae PLearn

Generated on Tue Aug 17 16:04:47 2004 for PLearn by doxygen 1.3.7