PLearn 0.1
BinaryOpVMatrix.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // BinaryOpVMatrix.cc
00004 //
00005 // Copyright (C) 2005 Nicolas Chapados
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 //
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 //
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 //
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************
00036  * $Id: BinaryOpVMatrix.cc 9980 2009-03-03 20:15:28Z tihocan $
00037  ******************************************************* */
00038 
00039 // Authors: Nicolas Chapados
00040 
00044 #include "BinaryOpVMatrix.h"
00045 
00046 namespace PLearn {
00047 using namespace std;
00048 
00049 
00050 BinaryOpVMatrix::BinaryOpVMatrix()
00051 {}
00052 
00053 BinaryOpVMatrix::BinaryOpVMatrix(VMat source1, VMat source2, const string& op,
00054                                  bool call_build_):
00055     inherited(call_build_),
00056     source1(source1),
00057     source2(source2),
00058     op(op)
00059 {
00060     if (call_build_)
00061         build_();
00062 }
00063 
00064 PLEARN_IMPLEMENT_OBJECT(BinaryOpVMatrix,
00065         "This VMat allows simple binary operations on two VMatrix.",
00066         "It is assumed that the two source matrices have the same size."
00067 );
00068 
00069 void BinaryOpVMatrix::getNewRow(int i, const Vec& v) const
00070 {
00071     PLASSERT( source1 && source2 );
00072     row1.resize(source1.width());
00073     row2.resize(source2.width());
00074     PLASSERT( row1.size() == row2.size() );
00075     PLASSERT( v.size()    == row1.size() );
00076 
00077     source1->getRow(i, row1);
00078     source2->getRow(i, row2);
00079 
00080     for (int i=0, n=row1.size() ; i<n ; ++i)
00081         v[i] = selected_op(row1[i], row2[i]);
00082 }
00083 
00084 void BinaryOpVMatrix::declareOptions(OptionList& ol)
00085 {
00086     declareOption(ol, "source1", &BinaryOpVMatrix::source1,
00087                   OptionBase::buildoption,
00088                   "First source VMatrix to operate on");
00089 
00090     declareOption(ol, "source2", &BinaryOpVMatrix::source2,
00091                   OptionBase::buildoption,
00092                   "Second source VMatrix to operate on");
00093 
00094     declareOption(ol, "op", &BinaryOpVMatrix::op, OptionBase::buildoption,
00095                   "Operation to perform; may be \"add\", \"sub\", \"mult\", \"div\"");
00096 
00097     // Now call the parent class' declareOptions
00098     inherited::declareOptions(ol);
00099 }
00100 
00101 void BinaryOpVMatrix::build_()
00102 {
00103     if (! source1)
00104         PLERROR("BinaryOpVMatrix::build_: source1 not defined");
00105     if (! source2)
00106         PLERROR("BinaryOpVMatrix::build_: source2 not defined");
00107     updateMtime(source1);
00108     updateMtime(source2);
00109     if (source1.length() != source2.length())
00110         PLERROR("BinaryOpVMatrix::build_: source1 has %d rows but\n"
00111                 "source2 has %d rows; both must have the same number of"
00112                 " rows.\n", source1.length(), source2.length());
00113 
00114     if (source1.width() != source2.width())
00115         PLERROR("BinaryOpVMatrix::build_: source1 has %d columns but\n"
00116                 "source2 has %d columns; both must have the same number of"
00117                 " columns.", source1.width(), source2.width());
00118 
00119     if (op == "add")
00120         selected_op = op_add;
00121     else if (op == "sub")
00122         selected_op = op_sub;
00123     else if (op == "mult")
00124         selected_op = op_mul;
00125     else if (op == "div")
00126         selected_op = op_div;
00127     else
00128         PLERROR("BinaryOpVMatrix::build_: unknown operation type \"%s\"; supported operatrions "
00129                 "are \"add\", \"sub\", \"mult\", \"div\"", op.c_str());
00130 
00131     // Copy the metainformation from first VMat
00132     setMetaInfoFrom(source1);
00133 }
00134 
00135 // ### Nothing to add here, simply calls build_
00136 void BinaryOpVMatrix::build()
00137 {
00138     inherited::build();
00139     build_();
00140 }
00141 
00142 void BinaryOpVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00143 {
00144     inherited::makeDeepCopyFromShallowCopy(copies);
00145     deepCopyField(row1, copies);
00146     deepCopyField(row2, copies);
00147     deepCopyField(source1, copies);
00148     deepCopyField(source2, copies);
00149 }
00150 
00151 } // end of namespace PLearn
00152 
00153 
00154 /*
00155   Local Variables:
00156   mode:c++
00157   c-basic-offset:4
00158   c-file-style:"stroustrup"
00159   c-file-offsets:((innamespace . 0)(inline-open . 0))
00160   indent-tabs-mode:nil
00161   fill-column:79
00162   End:
00163 */
00164 // 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