/
Msimilarity.c
95 lines (75 loc) · 2.49 KB
/
Msimilarity.c
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
/* Local similarity measure between two datasets.
September 2015 program of the month:
http://ahay.org/blog/2015/09/14/program-of-the-month-sfsimilarity/
*/
/*
Copyright (C) 2004 University of Texas at Austin
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <rsf.h>
int main(int argc, char* argv[])
{
bool verb;
int dim, dim1, i, n1, i2, n2, niter, n[SF_MAX_DIM];
int rect[SF_MAX_DIM];
char key[7];
float *one, *two, *rat1, *rat2, eps;
sf_file in, other, out;
sf_init(argc,argv);
in = sf_input("in");
out = sf_output("out");
other = sf_input("other");
if (SF_FLOAT != sf_gettype(in) ||
SF_FLOAT != sf_gettype(other)) sf_error("Need float input");
dim = sf_filedims(in,n);
dim1 = -1;
for (i=0; i < dim; i++) {
snprintf(key,7,"rect%d",i+1);
if (!sf_getint(key,rect+i)) rect[i]=1;
/*( rect#=(1,1,...) smoothing radius on #-th axis )*/
if (rect[i] > 1) dim1 = i+1;
}
n1 = n2 = 1;
for (i=0; i < dim; i++) {
if (i < dim1) {
n1 *= n[i];
} else {
n2 *= n[i];
}
}
if (!sf_getbool("verb",&verb)) verb=true;
/* verbosity */
if (!sf_getint("niter",&niter)) niter=20;
/* maximum number of iterations */
if (!sf_getfloat("eps",&eps)) eps=0.0f;
/* regularization */
sf_divn_init(dim1, n1, n, rect, niter, verb);
one = sf_floatalloc(n1);
two = sf_floatalloc(n1);
rat1 = sf_floatalloc(n1);
rat2 = sf_floatalloc(n1);
for (i2=0; i2 < n2; i2++) {
sf_warning(verb?
"record %d of %d":
"record %d of %d;",i2+1,n2);
sf_floatread(one,n1,in);
sf_floatread(two,n1,other);
sf_divne(one,two,rat1,eps);
sf_divne(two,one,rat2,eps);
/* combination */
sf_divn_combine (rat1,rat2,rat1);
sf_floatwrite(rat1,n1,out);
}
if (!verb) sf_warning(".");
exit(0);
}