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

RowBufferedVMatrix.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: RowBufferedVMatrix.cc,v 1.10 2004/07/26 20:10:19 tihocan Exp $ 00039 ******************************************************* */ 00040 00041 #include "RowBufferedVMatrix.h" 00042 #include <plearn/math/TMat_maths.h> 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00049 PLEARN_IMPLEMENT_ABSTRACT_OBJECT(RowBufferedVMatrix, 00050 "A base class for VMatrices that keep the last row(s) in a buffer for faster access.", 00051 ""); 00052 00053 RowBufferedVMatrix::RowBufferedVMatrix(int the_length, int the_width) 00054 :VMatrix(the_length, the_width), 00055 current_row_index(-1), current_row(the_width), 00056 other_row_index(-1), other_row(the_width) 00057 {} 00058 00059 RowBufferedVMatrix::RowBufferedVMatrix() 00060 :current_row_index(-1), other_row_index(-1) 00061 {} 00062 00064 // get // 00066 real RowBufferedVMatrix::get(int i, int j) const 00067 { 00068 if(current_row_index!=i) 00069 { 00070 current_row.resize(width_); 00071 getNewRow(i, current_row); 00072 current_row_index = i; 00073 } 00074 return current_row[j]; 00075 } 00076 00078 // getRow // 00080 void RowBufferedVMatrix::getRow(int i, Vec v) const { 00081 if (current_row_index != i) { 00082 current_row.resize(width_); 00083 getNewRow(i, current_row); 00084 current_row_index = i; 00085 } 00086 v.copyFrom(current_row.data(), width_); 00087 } 00088 00090 // getSubRow // 00092 void RowBufferedVMatrix::getSubRow(int i, int j, Vec v) const 00093 { 00094 if(current_row_index!=i) 00095 { 00096 current_row.resize(width_); 00097 getNewRow(i,current_row); 00098 current_row_index = i; 00099 } 00100 v.copyFrom(current_row.data()+j, v.length()); 00101 } 00102 00104 // dot // 00106 real RowBufferedVMatrix::dot(int i1, int i2, int inputsize) const 00107 { 00108 int w = width_; 00109 current_row.resize(w); 00110 other_row.resize(w); 00111 00112 if(i1==current_row_index) 00113 { 00114 if(i2==i1) 00115 return pownorm(current_row.subVec(0,inputsize)); 00116 if(i2!=other_row_index) 00117 { 00118 getNewRow(i2,other_row); 00119 other_row_index = i2; 00120 } 00121 } 00122 else if(i1==other_row_index) 00123 { 00124 if(i2==i1) 00125 return pownorm(other_row.subVec(0,inputsize)); 00126 if(i2!=current_row_index) 00127 { 00128 getNewRow(i2,current_row); 00129 current_row_index = i2; 00130 } 00131 } 00132 else // i1 not cached 00133 { 00134 if(i2==current_row_index) 00135 { 00136 getNewRow(i1,other_row); 00137 other_row_index = i1; 00138 } 00139 else if(i2==other_row_index) 00140 { 00141 getNewRow(i1,current_row); 00142 current_row_index = i1; 00143 } 00144 else // neither i1 nor i2 are cached 00145 { 00146 getNewRow(i1,current_row); 00147 getNewRow(i2,other_row); 00148 current_row_index = i1; 00149 other_row_index = i2; 00150 } 00151 } 00152 return PLearn::dot(current_row.subVec(0,inputsize), other_row.subVec(0,inputsize)); 00153 } 00154 00156 // dot // 00158 real RowBufferedVMatrix::dot(int i, const Vec& v) const 00159 { 00160 if(i!=current_row_index) 00161 { 00162 current_row.resize(width_); 00163 getNewRow(i,current_row); 00164 i = current_row_index; 00165 } 00166 return PLearn::dot(current_row.subVec(0,v.length()),v); 00167 } 00168 00170 // makeDeepCopyFromShallowCopy // 00172 void RowBufferedVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) { 00173 inherited::makeDeepCopyFromShallowCopy(copies); 00174 deepCopyField(current_row, copies); 00175 deepCopyField(other_row, copies); 00176 } 00177 00178 } // end of namespcae PLearn

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