📄 npoint.cpp
字号:
#include "StdAfx.h"
#include "NPoint.h"
#include "math.h"
IMPLEMENT_SERIAL( NPoint,CObject,1 )//使用IMPLEMENT_SERIAL宏
NPoint::NPoint(void)
{
dimension=0;
value=NULL;
}
NPoint::NPoint(int Dim)
{
dimension=Dim;
value=new double[Dim];
}
NPoint::~NPoint(void)
{
delete value;
}
// 获得样本点的维度
int NPoint::GetDim(void)
{
return dimension;
}
// 获得样本点中第i个维度的数值,i从0开始
double NPoint::GetValueAt(int i)
{
if((i>=0)&&(i<=dimension-1)) //合法索引范围
return *(value+i); //返回相应位置的数值
else //非法索引范围
return *value; //返回第一个位置的数值
}
// 设置样本点中第i个维度的数值为v,成功返回真,位置错误返回假
bool NPoint::SetValueAt(int i, double v)
{
if((i>=0)&&(i<=dimension-1)) //合法索引范围
{
*(value+i)=v; //设置相应位置的数值
return true; //返回真
}
else //非法索引范围
{
return false; //返回假
}
}
// 串行化样本点
void NPoint::Serialize(CArchive & ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
ar<<dimension;
for(int i=0;i<dimension;i++)
{
ar<<*(value+i);
}
}
else
{
// TODO: add loading code here
ar>>dimension;
value=new double[dimension];
for(int i=0;i<dimension;i++)
{
ar>>*(value+i);
}
}
}
// 计算样本点到自己在L+1层结构中的距离
double NPoint::DistanceL(NPoint* PointPtr, int Level)
{
int dim=LevelProjection(Level);
double Temp=0;
for(int i=0;i<dim;i++)
{
Temp+=pow((PointPtr->GetValueAt(i)-this->GetValueAt(i)),2);
}
return sqrt(Temp);
}
// 生成在L+1层结构中对应的维度映射
int NPoint::LevelProjection(int Level)
{
int result=(int)pow(2.0,Level);
if(result<=dimension)
return result;
else
return dimension;
}
// 对点坐标进行Haar小波变换
void NPoint::Haar(void)
{
double *p1, *p2, *s, scale;
double *pd=new double[dimension];
if(value!=NULL)
{
scale=1/sqrt(2.0);
int i=0;
for(int k=dimension;k>1;k/=2)
{
i++;
}
for (; i>0; i-- )
{
s = value+LevelProjection(i)-2;
p1 = pd+LevelProjection(i)-1;
p2 = pd+LevelProjection(i-1)-1;
for (int j=0; j<LevelProjection(i); j+=2, s-=2 )
{
*p1-- = scale*(*s - *(s+1));
*p2-- = scale*(*s + *(s+1));
}
memcpy(value, pd, sizeof(double)*LevelProjection(i-1));
}
value=pd;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -