PLearn 0.1
|
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: CrossReferenceVMatrix.cc 8617 2008-03-03 17:45:54Z nouiz $ 00039 ******************************************************* */ 00040 00041 #include "CrossReferenceVMatrix.h" 00042 00043 namespace PLearn { 00044 using namespace std; 00045 00046 00049 PLEARN_IMPLEMENT_OBJECT(CrossReferenceVMatrix, "ONE LINE DESC", "ONE LINE HELP"); 00050 00051 CrossReferenceVMatrix::CrossReferenceVMatrix() 00052 : index(0) 00053 { 00054 } 00055 00056 CrossReferenceVMatrix::CrossReferenceVMatrix(VMat the_master, 00057 int the_index, 00058 VMat the_slave) 00059 : inherited(the_master.length(), the_master.width()+the_slave.width()-1), 00060 master(the_master), 00061 index(the_index), 00062 slave(the_slave) 00063 { 00064 //fieldinfos = the_master->getFieldInfos(); 00065 // fieldinfos &= the_slave->getFieldInfos(); 00066 build(); 00067 } 00068 00069 00070 void 00071 CrossReferenceVMatrix::declareOptions(OptionList &ol) 00072 { 00073 declareOption(ol, "master", &CrossReferenceVMatrix::master, 00074 OptionBase::buildoption, ""); 00075 00076 declareOption(ol, "slave", &CrossReferenceVMatrix::slave, 00077 OptionBase::buildoption, ""); 00078 00079 declareOption(ol, "index", &CrossReferenceVMatrix::index, 00080 OptionBase::buildoption, ""); 00081 00082 inherited::declareOptions(ol); 00083 } 00084 00085 void 00086 CrossReferenceVMatrix::build() 00087 { 00088 inherited::build(); 00089 build_(); 00090 } 00091 00092 void 00093 CrossReferenceVMatrix::build_() 00094 { 00095 if (master && slave) { 00096 fieldinfos = master->getFieldInfos(); 00097 fieldinfos &= slave->getFieldInfos(); 00098 updateMtime(master); 00099 updateMtime(slave); 00100 } 00101 } 00102 00103 void CrossReferenceVMatrix::getRow(int i, Vec samplevec) const 00104 { 00105 #ifdef BOUNDCHECK 00106 if (i<0 || i>=length() || samplevec.length()!=width()) 00107 PLERROR("In CrossReferenceVMatrix::getRow OUT OF BOUNDS"); 00108 #endif 00109 00110 Vec v1(master.width()); 00111 Vec v2(slave.width()); 00112 master->getRow(i, v1); 00113 int index = (int)v1[index]; 00114 slave->getRow(index, v2); 00115 00116 for (int j=0; j<index; j++) samplevec[j] = v1[j]; 00117 for (int j=index+1; j<v1.length(); j++) samplevec[j-1] = v1[j]; 00118 for (int j=0; j<v2.length(); j++) samplevec[j+v1.length()-1] = v2[j]; 00119 } 00120 00121 real CrossReferenceVMatrix::get(int i, int j) const 00122 { 00123 #ifdef BOUNDCHECK 00124 if(i<0 || i>=length() || j<0 || j>=width()) 00125 PLERROR("In CrossReferenceVMatrix::get OUT OF BOUNDS"); 00126 #endif 00127 00128 if (j < index) 00129 return master->get(i,j); 00130 else if (j < master.width()-1) 00131 return master->get(i,j+1); 00132 else { 00133 int ii = (int)master->get(i,index); 00134 int jj = j - master.width() + 1; 00135 return slave->get(ii,jj); 00136 } 00137 } 00138 00139 } // end of namespace PLearn 00140 00141 00142 /* 00143 Local Variables: 00144 mode:c++ 00145 c-basic-offset:4 00146 c-file-style:"stroustrup" 00147 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00148 indent-tabs-mode:nil 00149 fill-column:79 00150 End: 00151 */ 00152 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :