VectorOperations.cpp
Go to the documentation of this file.
1 #include "VectorOperations.h"
2 #include <algorithm>
3 #include <iterator>
4 using namespace std;
5 
6 double Norm_Inf(const double *vec, int n)
7 {
8  double norm = -FLT_MAX;
9  for (int i = 0; i < n; ++i)
10  if (abs(vec[i]) > norm)
11  norm = abs(vec[i]);
12  return norm;
13 }
14 
15 double Norm_Inf(const vector<double>& vec)
16 {
17  double norm = -FLT_MAX;
18  for (int i = 0; i < vec.size(); ++i)
19  if (abs(vec[i]) > norm)
20  norm = abs(vec[i]);
21  return norm;
22 }
23 
24 double Norm_1(const double *vec, int n)
25 {
26  double norm = 0;
27  for (int i = 0; i < n; ++i)
28  norm += abs(vec[i]);
29  return norm;
30 }
31 
32 double Norm_1(const vector<double>& vec)
33 {
34  double norm = 0;
35  for (int i = 0; i < vec.size(); ++i)
36  norm += abs(vec[i]);
37  return norm;
38 }
39 
40 double Dot(const double *vec1, const double *vec2, int n)
41 {
42  double dot = 0;
43  for (int i = 0; i < n; ++i)
44  {
45  dot += vec1[i] * vec2[i];
46  }
47  return dot;
48 }
49 
50 double Dot(const vector<double>&vec1, const vector<double>&vec2)
51 {
52  return Dot(&vec1[0], &vec2[0], vec1.size());
53 }
54 
55 double LengthSquare(const double *vec, int n)
56 {
57  return Dot(vec, vec, n);
58 }
59 
60 double LengthSquare(const vector<double>& vec)
61 {
62  return Dot(vec, vec);
63 }
64 
65 double Length(const double *vec, int n)
66 {
67  return sqrt(LengthSquare(vec, n));
68 }
69 
70 double Length(const vector<double>& vec)
71 {
72  return sqrt(LengthSquare(vec));
73 }
74 
75 double Angle(const double *vec1, const double *vec2, int n)
76 {
77  vector<double> vec1_cpy, vec2_cpy;
78  copy(vec1, vec1 + n, back_inserter(vec1_cpy));
79  copy(vec2, vec2 + n, back_inserter(vec2_cpy));
80  double L1 = Norm_Inf(vec1, n);
81  double L2 = Norm_Inf(vec2, n);
82  for (int i = 0; i < n; ++i)
83  {
84  vec1_cpy[i] /= L1;
85  vec2_cpy[i] /= L2;
86  }
87  return acos(Dot(vec1_cpy, vec2_cpy) / sqrt(Dot(vec1_cpy, vec1_cpy)) / sqrt(Dot(vec2_cpy, vec2_cpy)));
88 }
89 
90 double Angle(const vector<double> &vec1, const vector<double>&vec2)
91 {
92  return Angle(&vec1[0], &vec2[0], vec1.size());
93 }
double Angle(const double *vec1, const double *vec2, int n)
double LengthSquare(const double *vec, int n)
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
double Length(const double *vec, int n)
double Dot(const double *vec1, const double *vec2, int n)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const AbsReturnType abs() const
double Norm_Inf(const double *vec, int n)
EIGEN_DEVICE_FUNC const AcosReturnType acos() const
double Norm_1(const double *vec, int n)


co_scan
Author(s):
autogenerated on Mon Feb 28 2022 23:00:56