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

MemoryVMatrix.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: MemoryVMatrix.cc,v 1.21 2004/07/26 20:09:56 tihocan Exp $ 00039 ******************************************************* */ 00040 00041 #include "MemoryVMatrix.h" 00042 00043 namespace PLearn { 00044 using namespace std; 00045 00046 00047 00050 PLEARN_IMPLEMENT_OBJECT(MemoryVMatrix, 00051 "A VMatrix whose data is stored in memory.", 00052 "The data can either be given directly by a Mat, or by another VMat that\n" 00053 "will be precomputed in memory at build time.\n" 00054 ); 00055 00056 MemoryVMatrix::MemoryVMatrix() : data(Mat()) 00057 {} 00058 00059 MemoryVMatrix::MemoryVMatrix(int l, int w) : VMatrix(l, w) 00060 { 00061 data.resize(l,w); 00062 defineSizes(data.width(), 0, 0); 00063 } 00064 00065 MemoryVMatrix::MemoryVMatrix(const Mat& the_data) 00066 :VMatrix(the_data.length(), the_data.width()), data(the_data) 00067 { 00068 defineSizes(the_data.width(), 0, 0); 00069 } 00070 00071 void MemoryVMatrix::declareOptions(OptionList& ol) 00072 { 00073 declareOption(ol, "data", &MemoryVMatrix::data, OptionBase::buildoption, 00074 "The underlying Mat."); 00075 00076 declareOption(ol, "data_vm", &MemoryVMatrix::data_vm, OptionBase::buildoption, 00077 "The underlying VMatrix. Will overwrite 'data' if provided."); 00078 00079 inherited::declareOptions(ol); 00080 } 00081 00082 void MemoryVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00083 { 00084 inherited::makeDeepCopyFromShallowCopy(copies); 00085 deepCopyField(data, copies); 00086 deepCopyField(data_vm, copies); 00087 } 00088 00090 // build_ // 00092 void MemoryVMatrix::build_() 00093 { 00094 if (data_vm) { 00095 // Precompute data from data_vm; 00096 data = data_vm->toMat(); 00097 copySizesFrom(data_vm); 00098 } 00099 if (this->length() >= 0 && this->length() != data.length()) { 00100 // New length specified. 00101 data.resize(this->length(), data.width()); 00102 } 00103 if (this->width() >= 0 && this->width() != data.width()) { 00104 // New width specified. 00105 data.resize(data.length(), this->width()); 00106 } 00107 if (this->length() < 0 && data.length() >= 0) { 00108 // Take the length from the data matrix. 00109 this->length_ = data.length(); 00110 } 00111 if (this->width() < 0 && data.width() >= 0) { 00112 // Take the width from the data matrix. 00113 this->width_ = data.width(); 00114 } 00115 } 00116 00118 // build // 00120 void MemoryVMatrix::build() 00121 { 00122 inherited::build(); 00123 build_(); 00124 } 00125 00126 real MemoryVMatrix::get(int i, int j) const 00127 { return data(i,j); } 00128 00129 void MemoryVMatrix::put(int i, int j, real value) 00130 { data(i,j) = value; } 00131 00132 void MemoryVMatrix::getColumn(int i, Vec v) const 00133 { v << data.column(i); } 00134 00135 void MemoryVMatrix::getSubRow(int i, int j, Vec v) const 00136 { 00137 #ifdef BOUNDCHECK 00138 if (j+v.length()>width()) 00139 PLERROR("MemoryVMatrix::getSubRow(int i, int j, Vec v) OUT OF BOUNDS. " 00140 "j=%d, v.length()=%d, width()=%d", j, v.length(), width()); 00141 #endif 00142 v.copyFrom(data[i]+j, v.length()); 00143 } 00144 00146 // getRow // 00148 void MemoryVMatrix::getRow(int i, Vec v) const 00149 { 00150 v.copyFrom(data[i], width_); 00151 } 00152 00153 void MemoryVMatrix::getMat(int i, int j, Mat m) const 00154 { m << data.subMat(i,j,m.length(),m.width()); } 00155 00156 void MemoryVMatrix::putSubRow(int i, int j, Vec v) 00157 { 00158 #ifdef BOUNDCHECK 00159 if (j+v.length()>width()) 00160 PLERROR("MemoryVMatrix::putSubRow(int i, int j, Vec v) OUT OF BOUNDS. " 00161 "j=%d, v.length()=%d, width()=%d", j, v.length(), width()); 00162 #endif 00163 v.copyTo(data[i]+j); 00164 } 00165 00166 void MemoryVMatrix::fill(real value) 00167 { data.fill(value); } 00168 00169 void MemoryVMatrix::putRow(int i, Vec v) 00170 { v.copyTo(data[i]); } 00171 00172 void MemoryVMatrix::putMat(int i, int j, Mat m) 00173 { data.subMat(i,j,m.length(),m.width()) << m; } 00174 00175 void MemoryVMatrix::appendRow(Vec v) 00176 { 00177 data.appendRow(v); 00178 length_++; 00179 } 00180 /* 00181 void MemoryVMatrix::write(ostream& out) const 00182 { 00183 writeHeader(out, "MemoryVMatrix"); 00184 VMatrix::write(out); // save higher-level stuff 00185 writeField(out, "data", data); 00186 writeFooter(out, "MemoryVMatrix"); 00187 } 00188 00189 void MemoryVMatrix::oldread(istream& in) 00190 { 00191 readHeader(in, "MemoryVMatrix"); 00192 VMatrix::oldread(in); // read higher-level stuff 00193 readField(in, "data", data); 00194 readFooter(in, "MemoryVMatrix"); 00195 } 00196 */ 00197 Mat MemoryVMatrix::toMat() const 00198 { return data; } 00199 00200 VMat MemoryVMatrix::subMat(int i, int j, int l, int w) 00201 { return new MemoryVMatrix(data.subMat(i,j,l,w)); } 00202 00204 // dot // 00206 real MemoryVMatrix::dot(int i1, int i2, int inputsize) const 00207 { 00208 #ifdef BOUNDCHECK 00209 if(inputsize>width()) 00210 PLERROR("In MemoryVMatrix::dot inputsize>width()"); 00211 #endif 00212 real* v1 = data.rowdata(i1); 00213 real* v2 = data.rowdata(i2); 00214 real res = 0.; 00215 for(int k=0; k<inputsize; k++) 00216 res += (*v1++) * (*v2++); 00217 return res; 00218 } 00219 00220 real MemoryVMatrix::dot(int i, const Vec& v) const 00221 { 00222 #ifdef BOUNDCHECK 00223 if(v.length()>width()) 00224 PLERROR("In MemoryVMatrix::dot length of vector v is greater than VMat's width"); 00225 #endif 00226 real* v1 = data.rowdata(i); 00227 real* v2 = v.data(); 00228 real res = 0.; 00229 for(int k=0; k<v.length(); k++) 00230 res += v1[k]*v2[k]; 00231 return res; 00232 } 00233 00234 } // end of namespcae PLearn

Generated on Tue Aug 17 15:58:40 2004 for PLearn by doxygen 1.3.7