00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
#include "ExtendedVariable.h"
00044 
#include "SubMatTransposeVariable.h"
00045 
00046 
00047 
namespace PLearn {
00048 
using namespace std;
00049 
00050 
00053 
PLEARN_IMPLEMENT_OBJECT(SubMatTransposeVariable, 
"ONE LINE DESCR", 
"NO HELP");
00054 
00055 SubMatTransposeVariable::SubMatTransposeVariable(
Variable* v, 
int i, 
int j, 
int the_length, 
int the_width)
00056 : 
inherited(v, the_width, the_length),
00057   startk(i*v->length()+j),
00058   length_(the_length),
00059   width_(the_width),
00060   i_(i),
00061   j_(j)
00062 {
00063     
build_();
00064 }
00065 
00066 void SubMatTransposeVariable::build()
00067 {
00068     inherited::build();
00069     
build_();
00070 }
00071 
00072 void SubMatTransposeVariable::build_()
00073 {
00074     
if (input) {
00075         
00076         
if (i_ < 0 || i_ + length_ > input->
length() || j_ < 0 || j_ + width_ > input->
width())
00077             
PLERROR(
"In SubMatTransposeVariable: requested sub-matrix is out of matrix bounds");
00078         
startk = 
i_ * input->
length() + 
j_;
00079     }
00080 }
00081 
00082 
void
00083 SubMatTransposeVariable::declareOptions(
OptionList &ol)
00084 {
00085     
declareOption(ol, 
"length_", &SubMatTransposeVariable::length_, OptionBase::buildoption, 
"");
00086     
declareOption(ol, 
"width_", &SubMatTransposeVariable::width_, OptionBase::buildoption, 
"");
00087     
declareOption(ol, 
"startk", &SubMatTransposeVariable::startk, OptionBase::buildoption, 
"");
00088     
declareOption(ol, 
"i_", &SubMatTransposeVariable::i_, OptionBase::buildoption, 
"");
00089     
declareOption(ol, 
"j_", &SubMatTransposeVariable::j_, OptionBase::buildoption, 
"");
00090     inherited::declareOptions(ol);
00091 }
00092 
00093 void SubMatTransposeVariable::recomputeSize(
int& l, 
int& w)
 const
00094 
{ l=
width_; w=
length_; }
00095 
00096 void SubMatTransposeVariable::fprop()
00097 {
00098   
if(input->
length()==1 || input->
width()==1) 
00099     {
00100       
real* inputdata = input->valuedata+
startk;
00101       
for(
int k=0; 
k<
nelems(); 
k++)
00102         valuedata[
k] = inputdata[
k];
00103     }
00104   
else 
00105     {
00106       
real* inputrowdata = input->valuedata+
startk;
00107       
int thiskcolstart = 0; 
00108       
for(
int i=0; i<
width(); i++) 
00109         {
00110           
int thisk = thiskcolstart++;
00111           
for(
int j=0; j<
length(); j++, thisk+=
width()) 
00112             valuedata[thisk] = inputrowdata[j];
00113           inputrowdata += input->
width();
00114         }
00115     }
00116 }
00117 
00118 
00119 void SubMatTransposeVariable::bprop()
00120 {
00121   
if(input->
length()==1 || input->
width()==1) 
00122     {
00123       
real* inputdata = input->gradientdata+
startk;
00124       
for(
int k=0; 
k<
nelems(); 
k++)
00125         inputdata[
k] += gradientdata[
k];
00126     }
00127   
else 
00128     {
00129       
real* inputrowdata = input->gradientdata+
startk;
00130       
int thiskcolstart = 0; 
00131       
for(
int i=0; i<
width(); i++) 
00132         {
00133           
int thisk = thiskcolstart++;
00134           
for(
int j=0; j<
length(); j++, thisk+=
width()) 
00135             inputrowdata[j] += gradientdata[thisk];
00136           inputrowdata += input->
width();
00137         }
00138     }
00139 }
00140 
00141 
00142 void SubMatTransposeVariable::symbolicBprop()
00143 {
00144   
int i = 
startk/input->
width();
00145   
int j = 
startk%input->
width();
00146   
int topextent = i;
00147   
int bottomextent = input->
length()-(i+
width()); 
00148   
int leftextent = j;
00149   
int rightextent = input->
width()-(j+
length()); 
00150   input->accg(
extend(
transpose(g),topextent,bottomextent,leftextent,rightextent));
00151 }
00152 
00153 
00154 void SubMatTransposeVariable::rfprop()
00155 {
00156   
if (rValue.
length()==0) 
resizeRValue();
00157   
if(input->
length()==1 || input->
width()==1) 
00158     {
00159       
real* inputdata = input->rvaluedata+
startk;
00160       
for(
int k=0; 
k<
nelems(); 
k++)
00161         rvaluedata[
k] = inputdata[
k];
00162     }
00163   
else 
00164     {
00165       
real* inputrowdata = input->rvaluedata+
startk;
00166       
int thiskcolstart = 0; 
00167       
for(
int i=0; i<
width(); i++) 
00168         {
00169           
int thisk = thiskcolstart++;
00170           
for(
int j=0; j<
length(); j++, thisk+=
width()) 
00171             rvaluedata[thisk] = inputrowdata[j];
00172           inputrowdata += input->
width();
00173         }
00174     }
00175 }
00176 
00177 
00178 
00179 } 
00180 
00181