npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
multivariateCosine.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_MULTIVARIATECOSINE_HH_
2 #define NPSTAT_MULTIVARIATECOSINE_HH_
3 
4 /*!
5 // \file multivariateCosine.hh
6 //
7 // \brief Calculate scalar products of vectors whose length is normalized
8 // internally
9 //
10 // Author: I. Volobouev
11 //
12 // February 2023
13 */
14 
15 #include <cmath>
16 #include <cassert>
17 #include <stdexcept>
18 
19 namespace npstat {
20  template<typename Real>
21  long double multivariateCosine(const Real* x, const unsigned long strideX,
22  const Real* y, const unsigned long strideY,
23  const unsigned long len)
24  {
25  assert(len);
26  assert(x);
27  assert(y);
28 
29  long double sumX = 0.0L, sumY = 0.0L, sumSprod = 0.0L;
30  for (unsigned long i=0; i<len; ++i, x+=strideX, y+=strideY)
31  {
32  sumX += *x * *x;
33  sumSprod += *x * *y;
34  sumY += *y * *y;
35  }
36  if (sumX == 0.0L || sumY == 0.0L) throw std::domain_error(
37  "In npstat::multivariateCosine: zero vector norm encountered");
38  long double c = sumSprod/sqrtl(sumX)/sqrtl(sumY);
39  if (c < -1.0L)
40  c = -1.0L;
41  if (c > 1.0L)
42  c = 1.0L;
43  return c;
44  }
45 }
46 
47 #endif // NPSTAT_MULTIVARIATECOSINE_HH_
Definition: AbsArrayProjector.hh:14