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

VMat.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-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 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 /* ******************************************************* 00039 * $Id: VMat.cc,v 1.18 2004/07/21 16:30:55 chrish42 Exp $ * 00040 * This file is part of the PLearn library. * 00041 ******************************************************** */ 00042 #include "VMat.h" 00043 #include <plearn/io/fileutils.h> 00044 00045 #include "MemoryVMatrix.h" 00046 #include "FileVMatrix.h" 00047 #include "SelectRowsVMatrix.h" 00048 #include "SelectColumnsVMatrix.h" 00049 #include "SelectRowsFileIndexVMatrix.h" 00050 00051 namespace PLearn { 00052 using namespace std; 00053 00056 VMat::VMat() {} 00057 VMat::VMat(VMatrix* d): PP<VMatrix>(d) {} 00058 VMat::VMat(const VMat& d) :PP<VMatrix>(d) {} 00059 VMat::VMat(const Mat& datamat): PP<VMatrix>(new MemoryVMatrix(datamat)) {} 00060 VMat::~VMat() {} 00061 00062 VMat VMat::subMatRows(int i, int l) const 00063 { 00064 VMat res = ptr->subMat(i,0,l,width()); 00065 res->defineSizes(ptr->inputsize(), ptr->targetsize(), ptr->weightsize()); 00066 return res; 00067 } 00068 00069 VMat VMat::rows(TVec<int> rows_indices) const 00070 { return new SelectRowsVMatrix(*this, rows_indices); } 00071 00072 VMat VMat::rows(Vec rows_indices) const 00073 { return new SelectRowsVMatrix(*this, rows_indices); } 00074 00075 VMat VMat::rows(const string& indexfile) const 00076 { return new SelectRowsFileIndexVMatrix(*this, indexfile); } 00077 00078 VMat VMat::columns(TVec<int> columns_indices) const 00079 { return new SelectColumnsVMatrix(*this, columns_indices); } 00080 00081 VMat VMat::columns(Vec columns_indices) const 00082 { return new SelectColumnsVMatrix(*this, columns_indices); } 00083 00085 // precompute // 00087 void VMat::precompute() { 00088 VMat backup = *this; 00089 *this = new MemoryVMatrix(Mat(*this)); 00090 (*this)->setFieldInfos( backup->getFieldInfos() ); 00091 00092 // We restore the sizes info (lost in the Mat conversion). 00093 // Note that there would probably be more info to restore (like 00094 // field infos, string mappings, ...). 00095 (*this)->copySizesFrom(backup); 00096 00097 //TODO 00098 //(*this)->copyFieldInfosFrom(backup); 00099 } 00100 00101 void VMat::precompute(const string& pmatfile, bool use_existing_file) 00102 { 00103 VMat backup = *this; 00104 Array<VMField> infos = (*this)->getFieldInfos(); 00105 if(!use_existing_file || !file_exists(pmatfile)) 00106 save(pmatfile); 00107 *this = new FileVMatrix(pmatfile); 00108 (*this)->setFieldInfos( infos ); 00109 (*this)->copySizesFrom(backup); 00110 // TODO same as above 00111 } 00112 00113 00114 template <> 00115 void deepCopyField(VMat& field, CopiesMap& copies) 00116 { 00117 if (field) 00118 field = static_cast<VMatrix*>(field->deepCopy(copies)); 00119 } 00120 00122 // loadAsciiAsVMat // 00124 VMat loadAsciiAsVMat(const string& filename) 00125 { 00126 Mat m; 00127 TVec<string> fn; 00128 TVec< map<string,real> > map_sr; // String -> real mappings. 00129 loadAscii(filename, m, fn, &map_sr); 00130 VMat vm = new MemoryVMatrix(m); 00131 vm->setMtime(mtime(filename)); 00132 // Set the discovered string -> real mappings. 00133 for (int i = 0; i < map_sr.length(); i++) { 00134 vm->setStringMapping(i, map_sr[i]); 00135 } 00136 for(int i=0;i<fn.size();i++) 00137 vm->declareField(i, fn[i]); 00138 return vm; 00139 } 00140 00141 } // end of namespace PLearn

Generated on Tue Aug 17 16:10:27 2004 for PLearn by doxygen 1.3.7