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
#include "MemoryVMatrix.h"
00042
00043
namespace PLearn {
00044
using namespace std;
00045
00046
00047
00050
PLEARN_IMPLEMENT_OBJECT(MemoryVMatrix,
00051
"A VMatrix whose data is stored in memory.",
00052
"The data can either be given directly by a Mat, or by another VMat that\n"
00053
"will be precomputed in memory at build time.\n"
00054 );
00055
00056 MemoryVMatrix::MemoryVMatrix() : data(
Mat())
00057 {}
00058
00059 MemoryVMatrix::MemoryVMatrix(
int l,
int w) :
VMatrix(l, w)
00060 {
00061
data.
resize(l,w);
00062 defineSizes(
data.
width(), 0, 0);
00063 }
00064
00065 MemoryVMatrix::MemoryVMatrix(
const Mat& the_data)
00066 :
VMatrix(the_data.length(), the_data.width()), data(the_data)
00067 {
00068 defineSizes(the_data.
width(), 0, 0);
00069 }
00070
00071 void MemoryVMatrix::declareOptions(
OptionList& ol)
00072 {
00073
declareOption(ol,
"data", &MemoryVMatrix::data, OptionBase::buildoption,
00074
"The underlying Mat.");
00075
00076
declareOption(ol,
"data_vm", &MemoryVMatrix::data_vm, OptionBase::buildoption,
00077
"The underlying VMatrix. Will overwrite 'data' if provided.");
00078
00079 inherited::declareOptions(ol);
00080 }
00081
00082 void MemoryVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00083 {
00084 inherited::makeDeepCopyFromShallowCopy(copies);
00085
deepCopyField(
data, copies);
00086
deepCopyField(
data_vm, copies);
00087 }
00088
00090
00092 void MemoryVMatrix::build_()
00093 {
00094
if (
data_vm) {
00095
00096
data =
data_vm->
toMat();
00097 copySizesFrom(
data_vm);
00098 }
00099
if (this->
length() >= 0 && this->
length() !=
data.
length()) {
00100
00101
data.
resize(this->length(),
data.
width());
00102 }
00103
if (this->
width() >= 0 && this->
width() !=
data.
width()) {
00104
00105
data.
resize(
data.
length(), this->
width());
00106 }
00107
if (this->
length() < 0 &&
data.
length() >= 0) {
00108
00109 this->length_ =
data.
length();
00110 }
00111
if (this->
width() < 0 &&
data.
width() >= 0) {
00112
00113 this->width_ =
data.
width();
00114 }
00115 }
00116
00118
00120 void MemoryVMatrix::build()
00121 {
00122 inherited::build();
00123
build_();
00124 }
00125
00126 real MemoryVMatrix::get(
int i,
int j)
const
00127
{
return data(i,j); }
00128
00129 void MemoryVMatrix::put(
int i,
int j,
real value)
00130 {
data(i,j) = value; }
00131
00132 void MemoryVMatrix::getColumn(
int i,
Vec v)
const
00133
{ v <<
data.
column(i); }
00134
00135 void MemoryVMatrix::getSubRow(
int i,
int j,
Vec v)
const
00136
{
00137
#ifdef BOUNDCHECK
00138
if (j+v.
length()>
width())
00139
PLERROR(
"MemoryVMatrix::getSubRow(int i, int j, Vec v) OUT OF BOUNDS. "
00140
"j=%d, v.length()=%d, width()=%d", j, v.
length(),
width());
00141
#endif
00142
v.
copyFrom(
data[i]+j, v.
length());
00143 }
00144
00146
00148 void MemoryVMatrix::getRow(
int i,
Vec v)
const
00149
{
00150 v.
copyFrom(
data[i], width_);
00151 }
00152
00153 void MemoryVMatrix::getMat(
int i,
int j,
Mat m)
const
00154
{ m <<
data.
subMat(i,j,m.
length(),m.
width()); }
00155
00156 void MemoryVMatrix::putSubRow(
int i,
int j,
Vec v)
00157 {
00158
#ifdef BOUNDCHECK
00159
if (j+v.
length()>
width())
00160
PLERROR(
"MemoryVMatrix::putSubRow(int i, int j, Vec v) OUT OF BOUNDS. "
00161
"j=%d, v.length()=%d, width()=%d", j, v.
length(),
width());
00162
#endif
00163
v.
copyTo(
data[i]+j);
00164 }
00165
00166 void MemoryVMatrix::fill(
real value)
00167 {
data.
fill(value); }
00168
00169 void MemoryVMatrix::putRow(
int i,
Vec v)
00170 { v.
copyTo(
data[i]); }
00171
00172 void MemoryVMatrix::putMat(
int i,
int j,
Mat m)
00173 {
data.
subMat(i,j,m.
length(),m.
width()) << m; }
00174
00175 void MemoryVMatrix::appendRow(
Vec v)
00176 {
00177
data.
appendRow(v);
00178 length_++;
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 Mat MemoryVMatrix::toMat()
const
00198
{
return data; }
00199
00200 VMat MemoryVMatrix::subMat(
int i,
int j,
int l,
int w)
00201 {
return new MemoryVMatrix(
data.
subMat(i,j,l,w)); }
00202
00204
00206 real MemoryVMatrix::dot(
int i1,
int i2,
int inputsize)
const
00207
{
00208
#ifdef BOUNDCHECK
00209
if(inputsize>
width())
00210
PLERROR(
"In MemoryVMatrix::dot inputsize>width()");
00211
#endif
00212
real* v1 =
data.
rowdata(i1);
00213
real* v2 =
data.
rowdata(i2);
00214
real res = 0.;
00215
for(
int k=0;
k<inputsize;
k++)
00216 res += (*v1++) * (*v2++);
00217
return res;
00218 }
00219
00220 real MemoryVMatrix::dot(
int i,
const Vec& v)
const
00221
{
00222
#ifdef BOUNDCHECK
00223
if(v.
length()>
width())
00224
PLERROR(
"In MemoryVMatrix::dot length of vector v is greater than VMat's width");
00225
#endif
00226
real* v1 =
data.
rowdata(i);
00227
real* v2 = v.
data();
00228
real res = 0.;
00229
for(
int k=0;
k<v.
length();
k++)
00230 res += v1[
k]*v2[
k];
00231
return res;
00232 }
00233
00234 }