add queen mary DSP library
[ardour2.git] / libs / qm-dsp / maths / CosineDistance.cpp
blob13ab9ce0e85512c51628ed41ee092adf7f8f53c0
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
3 /*
4 QM DSP Library
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2008 Kurt Jacobson.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
16 #include "CosineDistance.h"
18 #include <iostream>
19 #include <limits>
21 using std::cerr;
23 double CosineDistance::distance(const vector<double> &v1,
24 const vector<double> &v2)
26 dist = 1.0; dDenTot = 0; dDen1 = 0; dDen2 = 0; dSum1 =0;
27 double small = 1e-20;
29 //check if v1, v2 same size
30 if (v1.size() != v2.size())
32 cerr << "CosineDistance::distance: ERROR: vectors not the same size\n";
33 return 1.0;
35 else
37 for(int i=0; i<v1.size(); i++)
39 dSum1 += v1[i]*v2[i];
40 dDen1 += v1[i]*v1[i];
41 dDen2 += v2[i]*v2[i];
43 dDenTot = sqrt(fabs(dDen1*dDen2)) + small;
44 dist = 1-((dSum1)/dDenTot);
45 return dist;