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

Mat.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 * $Id: Mat.cc,v 1.6 2004/04/17 00:44:55 plearner Exp $ 00039 * This file is part of the PLearn library. 00040 ******************************************************* */ 00041 00042 #include "Mat.h" 00043 #include "TMat_maths.h" 00044 00045 // for sort... 00046 // #include <vector> 00047 // #include <functional> 00048 // #include <algorithm> 00049 00050 namespace PLearn { 00051 using namespace std; 00052 00053 /* 00054 bool Vec::hasMissing() const 00055 { 00056 real* v = data(); // get vec data start 00057 for (int i=0; i<length(); i++) 00058 if (is_missing(v[i])) 00059 return true; 00060 return false; 00061 } 00062 */ 00063 00064 00065 Vec* newVecArray(int n) 00066 { 00067 return new Vec[n]; 00068 } 00069 00070 Vec* newVecArray(int n, int the_length) 00071 { 00072 Vec* varray = new Vec[n]; 00073 for(int i=0; i<n; i++) 00074 varray[i].resize(the_length); 00075 return varray; 00076 } 00077 00078 00079 /* 00080 template <> 00081 void deepCopyField(Vec& field, CopiesMap& copies) 00082 { 00083 field.makeDeepCopyFromShallowCopy(copies); 00084 } 00085 */ 00086 00087 Mat* newMatArray(int n) 00088 { 00089 return new Mat[n]; 00090 } 00091 00092 Mat* newMatArray(int n, int the_length, int the_width) 00093 { 00094 Mat* marray = new Mat[n]; 00095 for (int i=0; i<n; i++) 00096 marray[i].resize(the_length,the_width); 00097 return marray; 00098 } 00099 00100 Mat* newIndexedMatArray(int n, Mat& m, int indexcolumn) 00101 { 00102 if(indexcolumn!=0 && indexcolumn!=m.width()-1) 00103 PLERROR("In newIndexedMatArray(int n, const Mat& m, int indexcolumn): indexcolumn must be either the first or the last column of the matrix"); 00104 sortRows(m, indexcolumn); 00105 Mat inputs, classnums; 00106 if(indexcolumn==0) 00107 { 00108 inputs = m.subMatColumns(1,m.width()-1); 00109 classnums = m.column(0); 00110 } 00111 else // indexcolumn is last column 00112 { 00113 inputs = m.subMatColumns(0,m.width()-1); 00114 classnums = m.column(m.width()-1); 00115 } 00116 if(classnums(0,0)!=0 || classnums(classnums.length()-1,0)!=n-1) 00117 PLERROR("In newIndexedMatArray(int n, const Mat& m, int indexcolumn) Values in the indexcolumn should range from 0 to n-1"); 00118 00119 Mat* marray = new Mat[n]; 00120 int pos = 0; 00121 for(int classnum=0; classnum<n; classnum++) 00122 { 00123 int startpos = pos; 00124 while(pos<classnums.length() && int(classnums(pos,0))==classnum) 00125 pos++; 00126 marray[classnum] = inputs.subMatRows(startpos,pos-startpos); 00127 } 00128 return marray; 00129 } 00130 00131 00132 Mat operator^(const Mat& m1, const Mat& m2) 00133 { 00134 Mat result(m1.length()*m2.length(), m1.width()+m2.width()); 00135 Mat lefthalf = result.subMatColumns(0,m1.width()); 00136 Mat righthalf = result.subMatColumns(m1.width(),m2.width()); 00137 int i=0; 00138 for(int i1=0; i1<m1.length(); i1++) 00139 for(int i2=0; i2<m2.length(); i2++) 00140 { 00141 lefthalf(i) << m1(i1); 00142 righthalf(i) << m2(i2); 00143 i++; 00144 } 00145 return result; 00146 } 00147 00148 Mat unitmatrix(int n) 00149 { 00150 Mat m(n,n); 00151 for(int i=0; i<n; i++) 00152 m(i,i) = 1.0; 00153 return m; 00154 } 00155 00156 /* 00157 template <> 00158 void deepCopyField(Mat& field, CopiesMap& copies) 00159 { 00160 field.makeDeepCopyFromShallowCopy(copies); 00161 } 00162 */ 00163 00164 } // end of namespace PLearn 00165 00166 00167 // For use within debugger (gdb) only 00168 // For use within debugger (gdb) only 00169 void printvec(const PLearn::Vec& v) 00170 { std::cout << v << std::endl; } 00171 00172 void printmat(const PLearn::Mat& m) 00173 { std::cout << m << std::endl; } 00174

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