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 "ConcatColumnsVMatrix.h"
00042
00043
namespace PLearn {
00044
using namespace std;
00045
00048
PLEARN_IMPLEMENT_OBJECT(ConcatColumnsVMatrix,
"ONE LINE DESCR",
"NO HELP");
00049
00050 ConcatColumnsVMatrix::ConcatColumnsVMatrix(
Array<VMat> the_array)
00051 : array(the_array),
00052 no_duplicate_fieldnames(false)
00053 {
if (
array.
size())
build_(); }
00054
00055 ConcatColumnsVMatrix::ConcatColumnsVMatrix(
VMat d1,
VMat d2)
00056 : array(d1, d2),
00057 no_duplicate_fieldnames(false)
00058 {
build_(); }
00059
00060
00061 void ConcatColumnsVMatrix::declareOptions(
OptionList &ol)
00062 {
00063
declareOption(ol,
"array", &ConcatColumnsVMatrix::array, OptionBase::buildoption,
"Array of VMatrices");
00064
declareOption(ol,
"no_duplicate_fieldnames", &ConcatColumnsVMatrix::no_duplicate_fieldnames, OptionBase::buildoption,
00065
"If set to 1, will ensure no fieldnames are duplicated by adding numerical values '.1', '.2', ...");
00066 inherited::declareOptions(ol);
00067 }
00068
00069 void ConcatColumnsVMatrix::build()
00070 {
00071 inherited::build();
00072
build_();
00073 }
00074
00075 void ConcatColumnsVMatrix::build_()
00076 {
00077 length_ = width_ = 0;
00078
if(
array.
size())
00079 length_ =
array[0]->
length();
00080
00081
00082
00083
for(
int i=0; i<array.size(); i++)
00084 {
00085
if(array[i]->length()!=length_)
00086
PLERROR(
"ConcatColumnsVMatrix: Problem concatenating to VMatrices with different lengths");
00087
if(array[i]->width() == -1)
00088
PLERROR(
"In ConcatColumnsVMatrix constructor. Non-fixed width distribution not supported");
00089 width_ += array[i]->width();
00090 }
00091
00092
00093
00094 fieldinfos.
resize(width_);
00095
TVec<string> names;
00096
int fieldindex = 0;
00097
for (
int i=0; i<array.size(); ++i)
00098 {
00099
int len = array[i]->getFieldInfos().size();
00100
if (len > 0)
00101 {
00102
for (
int j=0; j<len; ++j)
00103 fieldinfos[fieldindex++] = array[i]->getFieldInfos()[j];
00104 }
00105
else
00106 {
00107 len = array[i]->width();
00108
for(
int j=0; j<len; ++j)
00109 fieldinfos[fieldindex++] =
VMField(
tostring(fieldindex));
00110 }
00111 }
00112
if (
no_duplicate_fieldnames) {
00113
unduplicateFieldNames();
00114 }
00115 }
00116
00117 void ConcatColumnsVMatrix::getNewRow(
int i,
const Vec& samplevec)
const
00118
{
00119
if (length_==-1)
00120
PLERROR(
"In ConcatColumnsVMatrix::getNewRow(int i, Vec samplevec) not supported for distributions with different (or infinite) lengths\nCall sample without index instead");
00121
int pos = 0;
00122
for(
int n=0; n<
array.
size(); n++)
00123 {
00124
int nvars =
array[n]->width();
00125
Vec samplesubvec = samplevec.
subVec(pos, nvars);
00126 array[n]->getRow(i,samplesubvec);
00127 pos += nvars;
00128 }
00129 }
00130
00131 real ConcatColumnsVMatrix::getStringVal(
int col,
const string & str)
const
00132
{
00133
if(col>=width_)
00134
PLERROR(
"access out of bound. Width=%i accessed col=%i",width_,col);
00135
int pos=0,
k=0;
00136
while(col>=pos+
array[
k]->width())
00137 {
00138 pos += array[
k]->width();
00139
k++;
00140 }
00141
00142
return array[
k]->getStringVal(col-pos,str);
00143 }
00144
00145 string ConcatColumnsVMatrix::getValString(
int col,
real val)
const
00146
{
00147
if(col>=width_)
00148
PLERROR(
"access out of bound. Width=%i accessed col=%i",width_,col);
00149
int pos=0,
k=0;
00150
while(col>=pos+
array[
k]->width())
00151 {
00152 pos += array[
k]->width();
00153
k++;
00154 }
00155
00156
return array[
k]->getValString(col-pos,
val);
00157 }
00158
00159 const map<string,real>& ConcatColumnsVMatrix::getStringMapping(
int col)
const
00160
{
00161
if(col>=width_)
00162
PLERROR(
"access out of bound. Width=%i accessed col=%i",width_,col);
00163
int pos=0,
k=0;
00164
while(col>=pos+
array[
k]->width())
00165 {
00166 pos += array[
k]->width();
00167
k++;
00168 }
00169
00170
return array[
k]->getStringToRealMapping(col-pos);
00171 }
00172
00173 string ConcatColumnsVMatrix::getString(
int row,
int col)
const
00174
{
00175
if(col>=width_)
00176
PLERROR(
"access out of bound. Width=%i accessed col=%i",width_,col);
00177
int pos=0,
k=0;
00178
while(col>=pos+
array[
k]->width())
00179 {
00180 pos += array[
k]->width();
00181
k++;
00182 }
00183
00184
return array[
k]->getString(row,col-pos);
00185 }
00186
00187
00188
00189 real ConcatColumnsVMatrix::dot(
int i1,
int i2,
int inputsize)
const
00190
{
00191
real res = 0.;
00192
for(
int k=0; ;
k++)
00193 {
00194
const VMat& vm =
array[
k];
00195
int vmwidth = vm.
width();
00196
if(inputsize<=vmwidth)
00197 {
00198 res += vm->dot(i1,i2,inputsize);
00199
break;
00200 }
00201
else
00202 {
00203 res += vm->dot(i1,i2,vmwidth);
00204 inputsize -= vmwidth;
00205 }
00206 }
00207
return res;
00208 }
00209
00210 real ConcatColumnsVMatrix::dot(
int i,
const Vec& v)
const
00211
{
00212
if (length_==-1)
00213
PLERROR(
"In ConcatColumnsVMatrix::getRow(int i, Vec samplevec) not supported for distributions with different (or infinite) lengths\nCall sample without index instead");
00214
00215
real res = 0.;
00216
int pos = 0;
00217
for(
int n=0; n<
array.
size(); n++)
00218 {
00219
int nvars = std::min(
array[n]->
width(),v.
length()-pos);
00220
if(nvars<=0)
00221
break;
00222
Vec subv = v.
subVec(pos, nvars);
00223 res +=
array[n]->dot(i,subv);
00224 pos += nvars;
00225 }
00226
return res;
00227 }
00228
00229
00230 }