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 
00037 
#include "ProbSparseMatrix.h"
00038 
00039 
namespace PLearn {
00040 
00041 ProbSparseMatrix::ProbSparseMatrix(
int n_rows, 
int n_cols, 
string name, 
int mode, 
bool double_access) : 
DoubleAccessSparseMatrix<
real>(n_rows, n_cols, name, 
mode, double_access)
00042 {
00043 }
00044 
00045 void ProbSparseMatrix::incr(
int i, 
int j, 
real inc, 
bool warning)
00046 {
00047   
if (inc <= 0.0 && warning)
00048     
PLWARNING(
"incrementing value by: %g", inc);
00049   
DoubleAccessSparseMatrix<real>::incr(i, j, inc);
00050 }
00051 
00052 void ProbSparseMatrix::set(
int i, 
int j, 
real value, 
bool warning)
00053 {
00054   
if (value <= 0.0 && warning)
00055     
PLWARNING(
"setting value: %g", value);
00056   
DoubleAccessSparseMatrix<real>::set(i, j, value);
00057 }
00058 
00059 bool ProbSparseMatrix::checkCondProbIntegrity()
00060 {
00061   
real sum = 0.0;
00062   
if (mode == 
ROW_WISE)
00063   {
00064     
for (
int i = 0; i < height; i++)
00065     {
00066       map<int, real>& row_i = rows[i];
00067       
sum = 0.0;
00068       
for (map<int, real>::iterator it = row_i.begin(); it != row_i.end(); ++it)
00069         
sum += it->second;
00070       
if (fabs(
sum - 1.0) > 1e-4 && (
sum > 0.0))
00071         
return false;
00072     }
00073     
return true;
00074   } 
else
00075   {
00076     
for (
int j = 0; j < width; j++)
00077     {
00078       map<int, real>& col_j = cols[j];
00079       
sum = 0.0;
00080       
for (map<int, real>::iterator it = col_j.begin(); it != col_j.end(); ++it)
00081         
sum += it->second;
00082       
if (fabs(
sum - 1.0) > 1e-4 && (
sum > 0.0))
00083         
return false;
00084     }
00085     
return true;
00086   }
00087 }
00088 
00089 bool ProbSparseMatrix::checkJointProbIntegrity()
00090 {
00091   
return (fabs(
sumOfElements() - 1.0) > 1e-4);
00092 }
00093 
00094 void ProbSparseMatrix::normalizeCond(
ProbSparseMatrix& nXY, 
bool clear_nXY)
00095 {
00096   
if (mode == 
ROW_WISE && (nXY.
getMode() == 
ROW_WISE || nXY.
isDoubleAccessible()))
00097   {
00098     
clear();
00099     
int nXY_height = nXY.
getHeight();
00100     
for (
int i = 0; i < nXY_height; i++)
00101     {
00102       
real sum_row_i = nXY.
sumRow(i);
00103       map<int, real>& row_i = nXY.
getRow(i);
00104       
for (map<int, real>::iterator it = row_i.begin(); it != row_i.end(); ++it)
00105       {
00106         
int j = it->first;
00107         
real nij = it->second;
00108         
real pij = nij / sum_row_i;
00109         
if (pij > 0.0)
00110           
set(i, j, pij);
00111       }
00112     }
00113     
if (clear_nXY)
00114       nXY.
clear();
00115   } 
else if (mode == 
COLUMN_WISE && (nXY.
getMode() == 
COLUMN_WISE || nXY.
isDoubleAccessible()))
00116   {
00117     
clear();
00118     
int nXY_width = nXY.
getWidth();
00119     
for (
int j = 0; j < nXY_width; j++)
00120     {
00121       
real sum_col_j = nXY.
sumCol(j);
00122       map<int, real>& col_j = nXY.
getCol(j);
00123       
for (map<int, real>::iterator it = col_j.begin(); it != col_j.end(); ++it)
00124       {
00125         
int i = it->first;
00126         
real nij = it->second;
00127         
real pij = nij / sum_col_j;
00128         
if (pij > 0.0)
00129           
set(i, j, pij);
00130       }
00131     }
00132     
if (clear_nXY)
00133       nXY.
clear();
00134   } 
else
00135   {
00136     
PLERROR(
"pXY and nXY accessibility modes must match");
00137   }
00138 }
00139 
00140 void ProbSparseMatrix::normalizeCond()
00141 {
00142   
if (mode == 
ROW_WISE)
00143   {
00144     
for (
int i = 0; i < height; i++)
00145     {
00146       
real sum_row_i = sumRow(i);
00147       map<int, real>& row_i = rows[i];
00148       
for (map<int, real>::iterator it = row_i.begin(); it != row_i.end(); ++it)
00149       {
00150         
int j = it->first;
00151         
real nij = it->second;
00152         
real pij = nij / sum_row_i;
00153         
if (pij > 0.0)
00154           
set(i, j, pij);
00155       }
00156     }
00157   } 
else
00158   {
00159     
for (
int j = 0; j < width; j++)
00160     {
00161       
real sum_col_j = sumCol(j);
00162       map<int, real>& col_j = cols[j];
00163       
for (map<int, real>::iterator it = col_j.begin(); it != col_j.end(); ++it)
00164       {
00165         
int i = it->first;
00166         
real nij = it->second;
00167         
real pij = nij / sum_col_j;
00168         
if (pij > 0.0)
00169           
set(i, j, pij);
00170       }
00171     }
00172   }
00173 }
00174 
00175 void ProbSparseMatrix::normalizeJoint(
ProbSparseMatrix& nXY, 
bool clear_nXY)
00176 {
00177   
clear();
00178   
real sum_nXY = nXY.
sumOfElements();
00179   
if (nXY.
getMode() == 
ROW_WISE)
00180   {
00181     
int nXY_height = nXY.
getHeight();
00182     
for (
int i = 0; i < nXY_height; i++)
00183     {
00184       map<int, real>& row_i = nXY.
getRow(i);
00185       
for (map<int, real>::iterator it = row_i.begin(); it != row_i.end(); ++it)
00186       {
00187         
int j = it->first;
00188         
real nij = it->second;
00189         
real pij = nij / sum_nXY;
00190         
if (pij > 0.0)
00191           
set(i, j, pij);
00192       }
00193     }
00194   } 
else if (nXY.
getMode() == 
COLUMN_WISE)
00195   {
00196     
int nXY_width = nXY.
getWidth();
00197     
for (
int j = 0; j < nXY_width; j++)
00198     {
00199       map<int, real>& col_j = nXY.
getCol(j);
00200       
for (map<int, real>::iterator it = col_j.begin(); it != col_j.end(); ++it)
00201       {
00202         
int i = it->first;
00203         
real nij = it->second;
00204         
real pij = nij / sum_nXY;
00205         
if (pij > 0.0)
00206           
set(i, j, pij);
00207       }
00208     }
00209   }
00210   
if (clear_nXY)
00211     nXY.
clear();
00212 }
00213 
00214 void ProbSparseMatrix::normalizeJoint()
00215 {
00216   
if (mode == 
ROW_WISE)
00217   {
00218     
for (
int i = 0; i < height; i++)
00219     {
00220       map<int, real>& row_i = rows[i];
00221       
for (map<int, real>::iterator it = row_i.begin(); it != row_i.end(); ++it)
00222       {
00223         
int j = it->first;
00224         
real nij = it->second;
00225         
real pij = nij / 
sumOfElements();
00226         
if (pij > 0.0)
00227           
set(i, j, pij);
00228       }
00229     }
00230   } 
else
00231   {
00232     
for (
int j = 0; j < width; j++)
00233     {
00234       map<int, real>& col_j = cols[j];
00235       
for (map<int, real>::iterator it = col_j.begin(); it != col_j.end(); ++it)
00236       {
00237         
int i = it->first;
00238         
real nij = it->second;
00239         
real pij = nij / 
sumOfElements();
00240         
if (pij > 0.0)
00241           
set(i, j, pij);
00242       }
00243     }
00244   }
00245 }
00246 
00247 }