📄 cutility.cpp
字号:
return columns;
}
void CMyMatrix::setMatrix(CMyMatrix *matrix)
{
assert(matrix->getNumRows() == getNumRows() && matrix->getNumColumns() == getNumColumns());
memcpy(data, matrix->getData(), getNumColumns() * getNumRows() * sizeof(rlt_real));
}
void CMyMatrix::initMatrix(rlt_real init)
{
for (unsigned int i = 0; i < getNumRows() * getNumColumns(); i++)
{
data[i] = init;
}
}
void CMyMatrix::saveASCII(FILE *stream)
{
fprintf(stream,"[ ");
for(unsigned int i = 0; i < getNumRows(); i++)
{
for (unsigned int j = 0; j < getNumColumns(); j ++)
{
fprintf(stream, "%f ", getElement(i, j));
}
fprintf(stream, "\n");
}
fprintf(stream, "]");
}
void CDistributions::getGibbsDistribution(rlt_real beta, rlt_real *values, unsigned int numValues)
{
rlt_real sum = 0.0;
unsigned int i;
for (i = 0; i < numValues; i++)
{
values[i] = my_exp(beta * values[i]);
sum += values[i];
}
for (i = 0; i < numValues; i++)
{
values[i] = values[i] / sum;
}
}
void CDistributions::getS1L0Distribution(rlt_real *values, unsigned int numValues)
{
rlt_real smallest, largest, sum;
unsigned int i;
//transform: smallest Value = 0, Valuesum = 1;
smallest = sum = largest = values[0];
for(i = 1; i < numValues; i++)
{
if (smallest > values[i]) smallest = values[i];
if (largest < values[i]) largest = values[i];
sum += values[i];
}
sum -= numValues * smallest;
if (largest == smallest) // alle Werte gleich ==> Werte so setzen, das alle Values das Gewicht 1 / numValues bekommen)
{
sum = numValues;
smallest = smallest - 1;
}
for(i = 0; i < numValues; i++)
{
values[i] = (values[i] - smallest) / sum;
}
}
rlt_real CDistributions::getNormalDistributionSample(rlt_real mu, rlt_real sigma)
{
rlt_real x1 = (rlt_real) (rand() + 1) / ((rlt_real) RAND_MAX + 1);
rlt_real x2 = (rlt_real) rand() / (rlt_real) RAND_MAX;
rlt_real z = sqrt(- 2 * log(x1)) * cos(2 * M_PI * x2);
return z * sqrt(sigma) + mu;
}
int CDistributions::getSampledIndex(rlt_real *distribution, int numValues)
{
rlt_real z = (rlt_real) (rand()) / RAND_MAX;
rlt_real sum = distribution[0];
unsigned int index = 0;
while (sum <= z && index < numValues - 1)
{
index++;
sum += distribution[index];
}
return index;
}
/*
CFeatureSparse::CFeatureSparse(FILE *file, int numDim, int *dim)
{
this->stdValue = 0.0;
sparse = NULL;
this->numDim = numDim;
this->dim = dim;
loadASCII(file);
}
CFeatureSparse::CFeatureSparse(int numDim, int dim[])
{
this->stdValue = 0.0;
sparse = NULL;
numDim = 0;
dim = NULL;
initSparse(numDim, dim);
}
CFeatureSparse::CFeatureSparse()
{
this->stdValue = 0.0;
sparse = NULL;
numDim = 0;
dim = NULL;
}
CFeatureSparse::~CFeatureSparse()
{
CFeatureList *featList = NULL;
for (int i = 0; i < sparse->getSize(); i++)
{
featList = sparse->get1D(i);
featList->clearAndDelete();
delete featList;
}
delete sparse;
delete dim;
}
void CFeatureSparse::initSparse(int numDim, int dim[])
{
this->numDim = numDim;
this->dim = new int[numDim];
memcpy(this->dim, dim, sizeof(int) * numDim);
sparse = new CMyArray<CFeatureList *>(numDim, this->dim);
for (int i = 0; i < sparse->getSize(); i++)
{
sparse->set1D(i, new CFeatureList());
}
}
void CFeatureSparse::setFactor(rlt_real factor, int indeces[], unsigned int featureIndex)
{
CFeature *feat = getCFeature( indeces, featureIndex);
assert(feat->featureIndex == featureIndex);
feat->factor = factor;
}
void CFeatureSparse::addFactor(rlt_real factor, int indeces[], unsigned int featureIndex)
{
CFeature *feat = getCFeature( indeces, featureIndex);
assert(feat->featureIndex == featureIndex);
feat->factor += factor;
}
CFeatureList *CFeatureSparse::getFeatureList(int indeces[])
{
return sparse->get(indeces);
}
CFeature* CFeatureSparse::getCFeature(int indeces[], unsigned int featureIndex)
{
CFeature *feat = NULL;
CFeatureList *featList = sparse->get(indeces);
CFeatureList::iterator it = featList->begin();
while (it != featList->end() && (*it)->featureIndex < featureIndex)
{
it ++;
}
if (it != featList->end() && (*it)->featureIndex == featureIndex)
{
feat = *it;
}
else
{
feat = new CFeature(featureIndex, stdValue);
featList->insert(it, feat);
}
return feat;
}
CFeature* CFeatureSparse::getCFeature1D(int index1D, unsigned int featureIndex)
{
CFeature *feat = NULL;
CFeatureList *featList = sparse->get1D(index1D);
CFeatureList::iterator it = featList->begin();
while (it != featList->end() && (*it)->featureIndex < featureIndex)
{
it ++;
}
if (it != featList->end() && (*it)->featureIndex == featureIndex)
{
feat = *it;
}
else
{
feat = new CFeature(featureIndex, stdValue);
featList->insert(it, feat);
}
return feat;
}
rlt_real CFeatureSparse::getFactor(int indeces[], unsigned int featureIndex)
{
rlt_real factor = stdValue;
CFeatureList *featList = sparse->get(indeces);
CFeatureList::iterator it = featList->begin();
while (it != featList->end() && (*it)->featureIndex < featureIndex)
{
it ++;
}
if (it != featList->end() && (*it)->featureIndex == featureIndex)
{
factor = (*it)->factor;
}
return factor;
}
void CFeatureSparse::loadASCII(FILE *stream)
{
int bufIndex, bufFeatures, numDim, i;
rlt_real bufFactor;
int *dim;
fscanf(stream,"%dD-Sparse: [", &numDim);
dim = new int[numDim];
for (i = 0; i < numDim; i++)
{
fscanf(stream, "%d ", &dim[i]);
}
fscanf(stream, "]\n");
if (this->numDim > 0)
{
assert(this->numDim == numDim);
if (this->dim != NULL)
{
for (int i = 0; i < numDim; i++)
{
assert(dim[i] == this->dim[i]);
}
}
}
else
{
initSparse(numDim, dim);
}
for (i = 0; i < sparse->getSize(); i++)
{
fscanf(stream, "%d: ", &bufFeatures);
for (int k = 0; k < bufFeatures; k++)
{
fscanf(stream, "(%d %lf)", &bufIndex, &bufFactor);
getCFeature1D(i, bufIndex)->factor = bufFactor;
}
fscanf(stream, "\n");
}
}
void CFeatureSparse::saveASCII(FILE *stream)
{
CFeatureList *featList;
CFeatureList::iterator it;
int i;
fprintf(stream,"%dD-Sparse: [", numDim);
for (i = 0; i < numDim; i++)
{
fprintf(stream, "%d ", dim[i]);
}
fprintf(stream, "]\n");
for (i = 0; i < sparse->getSize(); i++)
{
featList = sparse->get1D(i);
fprintf(stream, "%d: ", featList->size());
for (it = featList->begin(); it != featList->end(); it++)
{
fprintf(stream, "(%d %f)", (*it)->featureIndex, (*it)->factor);
}
fprintf(stream, "\n");
}
}
CFeatureSparse2D::CFeatureSparse2D(FILE *file) : CFeatureSparse(file, 2)
{
}
CFeatureSparse2D::CFeatureSparse2D(int dim1, int dim2) : CFeatureSparse()
{
int dim[2];
dim[0] = dim1;
dim[1] = dim2;
initSparse(2, dim);
}
CFeatureSparse2D::~CFeatureSparse2D()
{
}
void CFeatureSparse2D::setFactor(rlt_real factor, int ind1, int ind2, unsigned int featureIndex)
{
int indeces[2];
indeces[0] = ind1;
indeces[1] = ind2;
CFeatureSparse::setFactor(factor, indeces, featureIndex);
}
void CFeatureSparse2D::addFactor(rlt_real factor, int ind1, int ind2, unsigned int featureIndex)
{
int indeces[2];
indeces[0] = ind1;
indeces[1] = ind2;
CFeatureSparse::addFactor(factor, indeces, featureIndex);
}
CFeatureList *CFeatureSparse2D::getFeatureList(int ind1, int ind2)
{
int indeces[2];
indeces[0] = ind1;
indeces[1] = ind2;
return sparse->get(indeces);
}
CFeature* CFeatureSparse2D::getCFeature(int ind1, int ind2, unsigned int featureIndex)
{
int indeces[2];
indeces[0] = ind1;
indeces[1] = ind2;
return CFeatureSparse::getCFeature(indeces, featureIndex);
}
rlt_real CFeatureSparse2D::getFactor(int ind1, int ind2, unsigned int featureIndex)
{
int indeces[2];
indeces[0] = ind1;
indeces[1] = ind2;
return CFeatureSparse::getFactor(indeces, featureIndex);
}
*/
CFeatureMap::CFeatureMap(rlt_real stdValue )
{
this->stdValue = stdValue;
}
rlt_real CFeatureMap::getValue(unsigned int featureIndex)
{
CFeatureMap::iterator reward = find(featureIndex);
if (reward != end())
{
return (*reward).second;
}
else
{
return stdValue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -