📄 svkernel.c
字号:
/*------------------------------data_instruction-------------------------------------
excute the mode as: svkernel(u,v,'P',p,Dx1,Dy1,Dx2,Dy2);
u and v are kernel arguments;
'L' or 'P' or 'R' is the different kernel;
p is the parameter of kernel;
Dx1 and Dy1 are the Width and the Height of u;
Dx2 and Dy2 are the Width and the Height of v;
*----------------------------------------------------------------------------------*/
#include"stdio.h"
#include"malloc.h" //dynamic storing
#include"stdlib.h"
#include"math.h"
#define linear L
#define poly P
#define rbf R
void linear_op(double **,double **,double **,int Dx1,int Dy1,int Dx2,int Dy2);
void poly_op(double **,double **,double **,int p1,int Dx1,int Dy1,int Dx2,int Dy2);
void rbf_op(double **,double **,int p1,int Dx1,int Dy1,int Dx2,int Dy2);
void Save_kernel_matrix(double **,int Dy1,int Dy2);
double **K;
void svkernel(double **u,double **v,char ch,int p0,int Dx1,int Dy1,int Dx2,int Dy2)//kernel operation
{
int i,j;
double **VT;
VT=(double **)malloc(Dx2*sizeof(double *)); //the transpose of v
for(i=0;i<Dx2;i++)
VT[i]=(double*)malloc(Dy2*sizeof(double));
K=(double **)malloc(Dy1*sizeof(double *)); //the result of the agarithm
for(i=0;i<Dy1;i++)
K[i]=(double*)malloc(Dy2*sizeof(double));
for(i=0;i<Dy2;i++) //transpose
{
for(j=0;j<Dx2;j++)
{
VT[j][i]=v[i][j];
}
}
switch(ch)
{
case 'L':
linear_op(u,v,VT,Dx1,Dy1,Dx2,Dy2); //linear kernel
break;
case 'P':
poly_op(u,v,VT,p0,Dx1,Dy1,Dx2,Dy2); //polynomial kernel
break;
case 'R':
rbf_op(u,v,p0,Dx1,Dy1,Dx2,Dy2);
break;
}
Save_kernel_matrix(K,Dy1,Dy2);
free(VT);
free(K);
}
void linear_op(double **u ,double **v ,double **VT,int Dx1,int Dy1,int Dx2,int Dy2)//linear kernel
{
int i,j,k;
double dot_m=0.0;
for(i=0;i<Dy1;i++)
{
for(j=0;j<Dy2;j++) //dot product
{
for(k=0;k<Dx1;k++)
{
dot_m+=u[i][k]*VT[k][j];
}
K[i][j]=dot_m;
}
}
}
void poly_op(double **u,double **v,double **VT,int p1,int Dx1,int Dy1,int Dx2,int Dy2)//polynomial kernel
{
int i,j,k,l;
double dot_m_p=0.0;
linear_op(u,v,VT,Dx1,Dy1,Dx2,Dy2); //linear kernel is used
for(l=1;l<p1;l++)
{
for(i=0;i<Dy1;i++) //dot product
{
for(j=0;j<Dy2;j++)
{
for(k=0;k<Dy2;k++)
{
dot_m_p=K[i][k]*K[i][k];
}
K[i][j]=dot_m_p;
}
}
}
}
void rbf_op(double **u,double **v,int p1,int Dx1,int Dy1,int Dx2,int Dy2) //rbf kernel
{
int i,j,k;
double dot_m_r=0.0;
double **UV,**UVT;
UV=(double **)malloc(Dy1*sizeof(double *)); //apply for 2d memory space
for(i=0;i<Dy1;i++)
UV[i]=(double*)malloc(Dy2*sizeof(double));
UVT=(double **)malloc(Dy2*sizeof(double *));
for(i=0;i<Dy2;i++)
UVT[i]=(double*)malloc(Dy1*sizeof(double));
for(i=0;i<Dy1;i++) //the deduction of two kinds of data
{
for(j=0;j<Dy2;j++)
{
for(k=0;k<Dx1;k++)
{
UV[i][j]=u[i][k]-v[j][k];
}
}
}
for(i=0;i<Dy1;i++) //transpose
{
for(j=0;j<Dy2;j++)
{
UVT[j][i]=UV[i][j];
}
}
for(i=0;i<Dy1;i++)
{
for(j=0;j<Dy1;j++)
{
for(k=0;k<Dy2;k++) //dot product
{
dot_m_r+=UV[i][k]*UVT[k][j];
}
K[i][j]=exp(-dot_m_r)/(2*p1*p1);
}
}
free(UV);
free(UVT);
}
void Save_kernel_matrix(double **k,int Dy1,int Dy2) //Save_kernel matrix as txt file
{
int i,j;
FILE *fp;
if((fp=fopen("kernelmatrix.txt","w"))==NULL)
{
printf("can not open the file!\n");
exit(0);
}
for(i=0;i<Dy1;i++)
{
for(j=0;j<Dy2;j++)
{
fprintf(fp,"%12f\n",k[i][j]);
}
}
fclose(fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -