PLearn 0.1
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 7203 2007-05-21 17:19:55Z tihocan $
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 Mat* newMatArray(int n)
00079 {
00080     return new Mat[n];
00081 }
00082 
00083 Mat* newMatArray(int n, int the_length, int the_width)
00084 {
00085     Mat* marray = new Mat[n];
00086     for (int i=0; i<n; i++)
00087         marray[i].resize(the_length,the_width);
00088     return marray;
00089 }
00090 
00091 Mat* newIndexedMatArray(int n, Mat& m, int indexcolumn)
00092 {
00093     if(indexcolumn!=0 && indexcolumn!=m.width()-1)
00094         PLERROR("In newIndexedMatArray(int n, const Mat& m, int indexcolumn): indexcolumn must be either the first or the last column of the matrix");
00095     sortRows(m, indexcolumn);
00096     Mat inputs, classnums;
00097     if(indexcolumn==0)
00098     {
00099         inputs = m.subMatColumns(1,m.width()-1);
00100         classnums = m.column(0);
00101     }
00102     else // indexcolumn is last column
00103     {
00104         inputs = m.subMatColumns(0,m.width()-1);
00105         classnums = m.column(m.width()-1);      
00106     }
00107     if(!fast_exact_is_equal(classnums(0,0),                    0)    ||
00108        !fast_exact_is_equal(classnums(classnums.length()-1,0), n-1))
00109         PLERROR("In newIndexedMatArray(int n, const Mat& m, int indexcolumn) Values in the indexcolumn should range from 0 to n-1");
00110 
00111     Mat* marray = new Mat[n];
00112     int pos = 0;
00113     for(int classnum=0; classnum<n; classnum++)
00114     {
00115         int startpos = pos;
00116         while(pos<classnums.length() && int(classnums(pos,0))==classnum)
00117             pos++;
00118         marray[classnum] = inputs.subMatRows(startpos,pos-startpos);
00119     }
00120     return marray;
00121 }
00122 
00123 
00124 Mat operator^(const Mat& m1, const Mat& m2)
00125 {
00126     Mat result(m1.length()*m2.length(), m1.width()+m2.width());
00127     Mat lefthalf = result.subMatColumns(0,m1.width());
00128     Mat righthalf = result.subMatColumns(m1.width(),m2.width());
00129     int i=0;
00130     for(int i1=0; i1<m1.length(); i1++)
00131         for(int i2=0; i2<m2.length(); i2++)
00132         {
00133             lefthalf(i) << m1(i1);
00134             righthalf(i) << m2(i2);
00135             i++;
00136         }
00137     return result;
00138 }
00139 
00140 Mat unitmatrix(int n)
00141 {
00142     Mat m(n,n);
00143     for(int i=0; i<n; i++)
00144         m(i,i) = 1.0;
00145     return m;
00146 }
00147 
00148 } // end of namespace PLearn
00149 
00150 
00151 // For use within debugger (gdb) only
00152 // For use within debugger (gdb) only
00153 void printvec(const PLearn::Vec& v)
00154 { std::cout << v << std::endl; }
00155 
00156 void printmat(const PLearn::Mat& m)
00157 { std::cout << m << std::endl; }
00158 
00159 
00160 /*
00161   Local Variables:
00162   mode:c++
00163   c-basic-offset:4
00164   c-file-style:"stroustrup"
00165   c-file-offsets:((innamespace . 0)(inline-open . 0))
00166   indent-tabs-mode:nil
00167   fill-column:79
00168   End:
00169 */
00170 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines