?? cutility.h
字號:
// Copyright (C) 2003
// Gerhard Neumann (gerhard@igi.tu-graz.ac.at)
//
// This file is part of RL Toolbox.
// http://www.igi.tugraz.at/ril_toolbox
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef C_UTILITY_H
#define C_UTILITY_H
#include "cfeaturefunction.h"
#include "ril_debug.h"
#include <map>
#include <assert.h>
class CMyMatrix;
class CMyVector;
int my_round(rlt_real value);
rlt_real my_exp(rlt_real value);
class CMyVector
{
protected:
rlt_real *data;
unsigned int dimensions;
public:
CMyVector(unsigned int dimension, rlt_real *data = NULL);
virtual ~CMyVector();
void addVector(CMyVector *vector);
void addScalar(rlt_real scalar);
void dotVector(CMyVector *vector);
void multMatrix(CMyMatrix *matrix, CMyVector *output);
void multMatrix(CMyMatrix *matrix);
void multVector(CMyVector *vector, CMyMatrix *output);
void multScalar(rlt_real scalar);
rlt_real getLength();
void normalizeVector();
rlt_real scalarProduct(CMyVector *vector);
virtual rlt_real getDistance(CMyVector *vector);
rlt_real getElement(unsigned int index);
void setElement(unsigned int index, rlt_real value);
unsigned int getNumDimensions();
// bool isColumnVector();
rlt_real *getData();
void setVector(CMyVector *vector);
void initVector(rlt_real init);
void saveASCII(FILE *stream);
void loadASCII(FILE *stream);
};
class CMyMatrix
{
protected:
rlt_real *data;
unsigned int columns;
unsigned int rows;
public:
CMyMatrix(unsigned int rows, unsigned int columns, rlt_real *data = NULL);
~CMyMatrix();
void addVector(CMyVector *vector);
void addScalar(rlt_real scalar);
void multMatrix(CMyMatrix *matrix, CMyMatrix *output);
void multVector(CMyVector *vector, CMyVector *output);
void multScalar(rlt_real scalar);
rlt_real getElement(unsigned int row, unsigned int column);
void setElement(unsigned int row, unsigned int column, rlt_real value);
rlt_real *getRow(unsigned int row);
unsigned int getNumRows();
unsigned int getNumColumns();
rlt_real *getData();
void setMatrix(CMyMatrix *matrix);
void initMatrix(rlt_real init);
void saveASCII(FILE *stream);
};
/// A multidimensional Array
/**
Stores an one dimensional array of Type T1, and provides the simulation of a multi-dimensional array.
The number of dimensions and the size of each dimension is given to the constructor. There are accessing functions
for one dimensional an multidimensional indices.
*/
template <typename T1> class CMyArray
{
protected:
T1 *data;
int *dim;
int numDim;
int size;
CMyArray() {};
void initialize(int numDim, int dim[])
{
this->numDim = numDim;
this->dim = new int[numDim];
memcpy(this->dim, dim, numDim * sizeof(int));
size = 1;
for (int i = 0; i < numDim; i++)
{
size = size * dim[i];
}
data = new T1 [size];
}
public:
CMyArray(int numDim, int dim[])
{
initialize(numDim, dim);
}
~CMyArray()
{
delete dim;
delete data;
}
T1 get(int indices[])
{
int index = 0, size = 1;
for (int i = 0; i < numDim; i++)
{
assert(indices[i] < dim[i] && indices[i] >=0);
index += indices[i] * size;
size = size * dim[i];
}
return data[index];
}
void set(int indices[], T1 d)
{
int index = 0, size = 1;
for (int i = 0; i < numDim; i++)
{
assert(indices[i] < dim[i] && indices[i] >=0);
index += indices[i] * size;
size = size * dim[i];
}
data[index] = d;
}
void init(T1 initVal)
{
for (int i = 0; i < size; i++)
{
data[i] = initVal;
}
}
int getSize()
{
return size;
}
void set1D(int index1d, T1 d)
{
data[index1d] = d;
}
T1 get1D(int index1d)
{
return data[index1d];
}
};
/// Collection of Distributions
class CDistributions
{
public:
///Returns the gibs distribution (soft max)
/** beta is the head, used by the formular In the values array there are given the single values. The function
writes then the propability of the indices in the array.
*/
static void getGibbsDistribution(rlt_real beta, rlt_real *values, unsigned int numValues);
static void getS1L0Distribution(rlt_real *values, unsigned int numValues);
static rlt_real getNormalDistributionSample(rlt_real mu, rlt_real sigma);
static int getSampledIndex(rlt_real *distribution, int numValues);
};
/// 2 dimensional Array
template<typename T1> class CMyArray2D : public CMyArray<T1>
{
public:
CMyArray2D(int xDim, int yDim)
{
int *l_dim = new int[2];
l_dim[0] = xDim;
l_dim[1] = yDim;
initialize(2, l_dim);
delete l_dim;
}
~CMyArray2D()
{
}
T1 get(int xIndex, int yIndex)
{
int indices[2];
indices[0] = xIndex;
indices[1] = yIndex;
return CMyArray<T1>::get(indices);
}
void set(int xIndex, int yIndex, T1 d)
{
int indices[2];
indices[0] = xIndex;
indices[1] = yIndex;
CMyArray<T1>::set(indices, d);
}
};
/// 3 dimensional array
template<typename T1> class CMyArray3D : public CMyArray<T1>
{
public:
CMyArray3D(int xDim, int yDim, int zDim) : CMyArray<T1>()
{
int *ldim = new int[3];
ldim[0] = xDim;
ldim[1] = yDim;
ldim[2] = zDim;
initialize(3, ldim);
}
~CMyArray3D()
{
delete dim;
}
T1 get(int xIndex, int yIndex, int zIndex)
{
int indices[3];
indices[0] = xIndex;
indices[1] = yIndex;
indices[2] = zIndex;
return CMyArray<T1>::get(indices);
}
void set(int xIndex, int yIndex, int zIndex, T1 d)
{
int indices[3];
indices[0] = xIndex;
indices[1] = yIndex;
indices[2] = zIndex;
CMyArray<T1>::set(indices, d);
}
};
// Maps feature indices to feature factors
class CFeatureMap : public std::map<int, rlt_real>
{
protected:
rlt_real stdValue;
public:
CFeatureMap(rlt_real stdValue = 0.0);
rlt_real getValue(unsigned int featureIndex);
};
/*
/// N-dimensional Sparse for features
class CFeatureSparse
{
protected:
CMyArray<CFeatureList *> *sparse;
rlt_real stdValue;
char *sparseName;
int numDim;
int *dim;
void initSparse(int numDim, int dim[]);
CFeatureSparse();
public:
CFeatureSparse(FILE *file, int numDim = 0, int *dim = NULL);
CFeatureSparse(int numDim, int dim[]);
virtual ~CFeatureSparse();
virtual rlt_real getFactor(int indeces[], unsigned int featureIndex);
void setFactor(rlt_real factor, int indeces[], unsigned int featureIndex);
void addFactor(rlt_real factor, int indeces[], unsigned int featureIndex);
void loadASCII(FILE *stream);
CFeature *getCFeature(int indeces[], unsigned int featureIndex);
CFeature *getCFeature1D(int index1D, unsigned int featureIndex);
virtual CFeatureList *getFeatureList(int indeces[]);
void saveASCII(FILE *stream);
};
/// 2 dimensional feature sparse.
//so there a a 2 dimensional array of feature lists.
class CFeatureSparse2D : public CFeatureSparse
{
public:
CFeatureSparse2D(FILE *file);
CFeatureSparse2D(int dim1, int dim2);
virtual ~CFeatureSparse2D();
virtual rlt_real getFactor(int ind1, int ind2, unsigned int featureIndex);
virtual void setFactor(rlt_real factor, int ind1, int ind2, unsigned int featureIndex);
virtual void addFactor(rlt_real factor, int ind1, int ind2, unsigned int featureIndex);
virtual CFeatureList *getFeatureList(int ind1, int ind2);
CFeature* getCFeature(int ind1, int ind2, unsigned int featureIndex);
};*/
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -