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 "MatrixElementsVariable.h"
00044
00045
namespace PLearn {
00046
using namespace std;
00047
00048
00049
00051
00052
00053
00054
00055
PLEARN_IMPLEMENT_OBJECT(MatrixElementsVariable,
00056
"Fills the elements of a matrix using the given scalar Variable "
00057
"expression, that depends on index variables i and j, that loop from "
00058
"0 to ni-1 and 0 to nj-1 respectively.",
00059
"NO HELP");
00060
00061
00062 MatrixElementsVariable::MatrixElementsVariable(
Variable* the_expression,
const Var& i_index,
00063
const Var& j_index,
int number_of_i_values,
00064
int number_of_j_values,
const VarArray& the_parameters)
00065 :
inherited(the_parameters, number_of_i_values, number_of_j_values), i(i_index),
00066 j(j_index), ni(number_of_i_values), nj(number_of_j_values), expression(the_expression),
00067 parameters(the_parameters)
00068 {
00069
build_();
00070 }
00071
00072
void
00073 MatrixElementsVariable::build()
00074 {
00075 inherited::build();
00076 }
00077
00078
void
00079 MatrixElementsVariable::build_()
00080 {
00081
if (
i &&
j && (
parameters.
size() > 0) &&
expression) {
00082
if (!
i->isScalar())
00083
PLERROR(
"MatrixElementsVariable expect a scalar index Var i_index");
00084
if (!
j->isScalar())
00085
PLERROR(
"MatrixElementsVariable expect a scalar index Var j_index");
00086
00087
full_fproppath =
propagationPath(
parameters&(
VarArray)
i&(VarArray)
j,
expression);
00088
fproppath =
propagationPath(
i&j,
expression);
00089
bproppath =
propagationPath(
parameters,
expression);
00090 }
00091 }
00092
00093
void
00094 MatrixElementsVariable::declareOptions(
OptionList &ol)
00095 {
00096
declareOption(ol,
"i", &MatrixElementsVariable::i, OptionBase::buildoption,
"");
00097
declareOption(ol,
"j", &MatrixElementsVariable::j, OptionBase::buildoption,
"");
00098
declareOption(ol,
"ni", &MatrixElementsVariable::ni, OptionBase::buildoption,
"");
00099
declareOption(ol,
"nj", &MatrixElementsVariable::nj, OptionBase::buildoption,
"");
00100
declareOption(ol,
"expression", &MatrixElementsVariable::expression, OptionBase::buildoption,
"");
00101
declareOption(ol,
"parameters", &MatrixElementsVariable::parameters, OptionBase::buildoption,
"");
00102 inherited::declareOptions(ol);
00103 }
00104
00105 void MatrixElementsVariable::recomputeSize(
int& l,
int& w)
const
00106
{ l=
ni; w=
nj; }
00107
00108
00109
extern void varDeepCopyField(
Var& field, CopiesMap& copies);
00110
00111 void MatrixElementsVariable::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00112 {
00113 NaryVariable::makeDeepCopyFromShallowCopy(copies);
00114
varDeepCopyField(
i, copies);
00115
varDeepCopyField(
j, copies);
00116
varDeepCopyField(
expression, copies);
00117
deepCopyField(
parameters, copies);
00118
deepCopyField(
full_fproppath, copies);
00119
deepCopyField(
fproppath, copies);
00120
deepCopyField(
bproppath, copies);
00121 }
00122
00123
00124 void MatrixElementsVariable::fprop()
00125 {
00126
int ii,jj;
00127
00128
for (ii=0;ii<
ni;ii++)
00129 {
00130
i->value[0]=ii;
00131
for (jj=0;jj<
nj;jj++)
00132 {
00133
j->value[0]=jj;
00134
if (ii==0 && jj==0)
00135
00136
full_fproppath.
fprop();
00137
else
00138
fproppath.
fprop();
00139 matValue(ii,jj) =
expression->value[0];
00140 }
00141 }
00142 }
00143
00144
00145 void MatrixElementsVariable::bprop()
00146 {
00147
int ii,jj;
00148
00149
for (ii=0;ii<
ni;ii++)
00150 {
00151
i->value[0]=ii;
00152
for (jj=0;jj<
nj;jj++)
00153 {
00154
j->value[0]=jj;
00155
if (ii==0 && jj==0)
00156
00157
full_fproppath.
fprop();
00158
else
00159
fproppath.
fprop();
00160
bproppath.
clearGradient();
00161
00162
00163
00164
00165
00166
expression->gradient[0] = matGradient(ii,jj);
00167
bproppath.
bprop();
00168 }
00169 }
00170 }
00171
00172
00173 void MatrixElementsVariable::fbprop()
00174 {
00175
int ii,jj;
00176
00177
for (ii=0;ii<
ni;ii++)
00178 {
00179
i->value[0]=ii;
00180
for (jj=0;jj<
nj;jj++)
00181 {
00182
j->value[0]=jj;
00183
if (ii==0 && jj==0)
00184
00185
full_fproppath.
fprop();
00186
else
00187
fproppath.
fprop();
00188 matValue(ii,jj) =
expression->value[0];
00189
bproppath.
clearGradient();
00190
00191
00192
00193
00194
00195
expression->gradient[0] = matGradient(ii,jj);
00196
bproppath.
bprop();
00197 }
00198 }
00199 }
00200
00201 }