File: SOGP_aux.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (146 lines) | stat: -rw-r--r-- 3,942 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "SOGP_aux.h"
#include <string.h>

//Basic cascades - Not efficient
ReturnMatrix SOGPKernel::kernelM(const ColumnVector& in, const Matrix &BV){
	ColumnVector k(BV.Ncols());
	for(int i=1;i<=BV.Ncols();i++)
	{
		Matrix BVCol = BV.Column(i);
		k(i)=kernel(in,BVCol);
	}
	k.Release();
	return k;
}
double SOGPKernel::kstar(const ColumnVector& in){
	return kernel(in,in);
}
double SOGPKernel::kstar(){
	ColumnVector foo(1);
	foo(1)=0;
	return kernel(foo,foo);
}
//RBF
double RBFKernel::kernel(const ColumnVector &a, const ColumnVector &b){ 
	double d = a.Nrows();
	if(d!=widths.Ncols())
	{//Expand if necessary
		//printf("RBFKernel:  Resizing width to %d\n",(int)d);
		double wtmp=widths(1);
        RowVector newWidths(d);
        for(int i=1;i<=widths.Ncols();i++) newWidths(i) = widths(i);
        for(int i=widths.Ncols();i<=d;i++) newWidths(i) = wtmp;
        widths = newWidths;
    }
	//I think this bumps up against numerical stability issues.
	Matrix c = a-b;
	Real ss = SumSquare(SP(c,widths.t()));
	return A*exp(-(1/(2*d)) * ss);
}
//POL
double POLKernel::kernel(const ColumnVector &a, const ColumnVector &b){
    double d = a.Nrows();
    double resp=1;
    double inner = (a.t()*b).AsScalar();
    for(int i=1;i<=scales.Ncols();i++)
        resp += pow((inner/(d*scales(i))),i);
    return resp;
}
//POLY
double POLYKernel::kernel(const ColumnVector &a, const ColumnVector &b){
    double d = a.Nrows();
    double resp=1;
    double inner = (a.t()*b).AsScalar() + offset;
    resp = pow(inner, degree);
    return resp;
}

//-------------------------------------------------------------
//Newmat printers
void printScalar(double value, FILE *fp,const char *name,bool ascii){
    if(name) fprintf(fp,"%s ",name);
    if(ascii) fprintf(fp,"%lf ",value);
    else fwrite(&value,sizeof(double),1,fp);
    fprintf(fp,"\n");
}
void readScalar(double &value,FILE *fp,const char *name,bool ascii){
    if(name)
    {
        char tn[128];//bad
        fscanf(fp,"%s ",tn);
        if(strcmp(tn,name)) printf("readRV: Expected '%s', got '%s'\n",name,tn);
    }
    if(ascii) fscanf(fp,"%lf ",&value);
    else fread(&(value),sizeof(double),1,fp);
    fscanf(fp,"\n");
}
void printRV(RowVector rv,FILE *fp,const char *name,bool ascii){
	if(name) fprintf(fp,"%s ",name);
	fprintf(fp,"%d:",rv.Ncols());
	for(int i=0;i<rv.Ncols();i++)
	{
		if(ascii) fprintf(fp,"%lf ",rv(i+1));
		else fwrite(&rv(i+1),sizeof(double),1,fp);
	}
	fprintf(fp,"\n");
}
void readRV(RowVector &rv,FILE *fp,const char *name,bool ascii){
	if(name)
	{
		char tn[128];//bad
		fscanf(fp,"%s ",tn);
		if(strcmp(tn,name)) printf("readRV: Expected '%s', got '%s'\n",name,tn);
	}
	int len;
	fscanf(fp,"%d:",&len);
	rv.ReSize(len);
	for(int i=0;i<rv.Ncols();i++)
	{
		if(ascii) fscanf(fp,"%lf ",&rv(i+1));
		else fread(&(rv(i+1)),sizeof(double),1,fp);
	}
	fscanf(fp,"\n");
}
void printCV(ColumnVector cv,FILE *fp,const char *name,bool ascii){
	printRV(cv.t(),fp,name,ascii);
}
void readCV(ColumnVector &cv,FILE *fp,const char *name,bool ascii){
	RowVector rv;
	readRV(rv,fp,name,ascii);
	cv=rv.t();
}
void printMatrix(Matrix m,FILE *fp,const char *name,bool ascii){
	if(name) fprintf(fp,"%s ",name); 
	fprintf(fp,"(%d:%d)",m.Nrows(),m.Ncols());
	for(int i=0;i<m.Nrows();i++)
	{
		for(int j=0;j<m.Ncols();j++)
		{
			if(ascii) fprintf(fp,"%lf ",m(i+1,j+1));
			else fwrite(&(m(i+1,j+1)),sizeof(double),1,fp);
		}
		if(ascii)fprintf(fp,"\n");
	}
	if(ascii)fprintf(fp,"\n");
}
void readMatrix(Matrix &m,FILE *fp,const char *name,bool ascii){
	if(name){
		char tn[128];
		fscanf(fp,"%s ",tn);
		if(strcmp(tn,name))
			printf("readMatrix: Expected '%s', got '%s'\n",name,tn);
	}
	int wid,hgt;
	fscanf(fp,"(%d:%d)",&wid,&hgt);
	m.ReSize(wid,hgt);
	for(int i=0;i<m.Nrows();i++){
		for(int j=0;j<m.Ncols();j++){
			if(ascii)
				fscanf(fp,"%lf ",&m(i+1,j+1));
			else
				fread(&(m(i+1,j+1)),sizeof(double),1,fp);
		} 
		if(ascii)fscanf(fp,"\n");
	}
	if(ascii)fscanf(fp,"\n");
}