Mercurial > hg > qm-dsp
annotate maths/CosineDistance.cpp @ 511:15cce5df8366
Correct Free Software Foundation address
| author | Guido Aulisi <guido.aulisi@gmail.com> |
|---|---|
| date | Mon, 07 Oct 2019 08:53:22 +0200 |
| parents | bb78ca3fe7de |
| children |
| rev | line source |
|---|---|
| c@256 | 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ |
| c@256 | 2 |
| c@256 | 3 /* |
| c@256 | 4 QM DSP Library |
| c@256 | 5 |
| c@256 | 6 Centre for Digital Music, Queen Mary, University of London. |
| c@256 | 7 This file copyright 2008 Kurt Jacobson. |
| c@309 | 8 |
| c@309 | 9 This program is free software; you can redistribute it and/or |
| c@309 | 10 modify it under the terms of the GNU General Public License as |
| c@309 | 11 published by the Free Software Foundation; either version 2 of the |
| c@309 | 12 License, or (at your option) any later version. See the file |
| c@309 | 13 COPYING included with this distribution for more information. |
| c@256 | 14 */ |
| c@256 | 15 |
| c@256 | 16 #include "CosineDistance.h" |
| c@256 | 17 |
| c@256 | 18 #include <iostream> |
| c@256 | 19 #include <limits> |
| c@256 | 20 |
| c@256 | 21 using std::cerr; |
| cannam@493 | 22 using std::vector; |
| c@256 | 23 |
| c@256 | 24 double CosineDistance::distance(const vector<double> &v1, |
| c@256 | 25 const vector<double> &v2) |
| c@256 | 26 { |
| c@256 | 27 dist = 1.0; dDenTot = 0; dDen1 = 0; dDen2 = 0; dSum1 =0; |
| c@299 | 28 double small = 1e-20; |
| c@256 | 29 |
| c@256 | 30 //check if v1, v2 same size |
| c@256 | 31 if (v1.size() != v2.size()) |
| c@256 | 32 { |
| c@256 | 33 cerr << "CosineDistance::distance: ERROR: vectors not the same size\n"; |
| c@256 | 34 return 1.0; |
| c@256 | 35 } |
| c@256 | 36 else |
| c@256 | 37 { |
| c@414 | 38 for(int i=0; i<int(v1.size()); i++) |
| c@256 | 39 { |
| c@256 | 40 dSum1 += v1[i]*v2[i]; |
| c@256 | 41 dDen1 += v1[i]*v1[i]; |
| c@256 | 42 dDen2 += v2[i]*v2[i]; |
| c@256 | 43 } |
| c@299 | 44 dDenTot = sqrt(fabs(dDen1*dDen2)) + small; |
| c@256 | 45 dist = 1-((dSum1)/dDenTot); |
| c@256 | 46 return dist; |
| c@256 | 47 } |
| c@256 | 48 } |
