openGPMP
Open Source Mathematics Package
vector_naive.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 #include <cmath>
34 #include <cstdint>
35 #include <iostream>
36 #include <numeric>
38 #include <stdexcept>
39 #include <vector>
40 
41 /************************************************************************
42  *
43  * Standard/Naive Vector Operations on vector<>
44  *
45  ************************************************************************/
46 
47 // function to compute the cross product of two 3D vectors
48 void gpmp::linalg::std_cross_product(const std::vector<double> &vec1,
49  const std::vector<double> &vec2,
50  std::vector<double> &result) {
51  result[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
52  result[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
53  result[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
54 }
55 
56 double gpmp::linalg::std_vector_norm(const std::vector<double> &vec) {
57  double norm = 0.0;
58  for (double value : vec) {
59  norm += value * value;
60  }
61  return std::sqrt(norm);
62 }
63 
64 // function to normalize a vector to have unit length
65 void gpmp::linalg::std_vector_normalize(const std::vector<double> &vec,
66  std::vector<double> &result) {
67  double norm = gpmp::linalg::std_vector_norm(vec);
68  result.resize(vec.size());
69  for (size_t i = 0; i < vec.size(); ++i) {
70  result[i] = vec[i] / norm;
71  }
72 }
73 
74 // function to compute the projection of one vector onto another
75 void gpmp::linalg::std_vector_projection(const std::vector<double> &vec,
76  const std::vector<double> &onto_vec,
77  std::vector<double> &result) {
78  double dot = std_dot_product(vec, onto_vec);
79  double onto_vec_norm_sq = std_dot_product(onto_vec, onto_vec);
80 
81  result.resize(onto_vec.size());
82  for (size_t i = 0; i < onto_vec.size(); ++i) {
83  result[i] = (dot / onto_vec_norm_sq) * onto_vec[i];
84  }
85 }
86 
87 // function to compute the cross product of two 3D vectors of integers
88 void gpmp::linalg::std_cross_product(const std::vector<int> &vec1,
89  const std::vector<int> &vec2,
90  std::vector<int> &result) {
91  result.resize(3);
92  result[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
93  result[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
94  result[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
95 }
96 
97 double gpmp::linalg::std_vector_norm(const std::vector<int> &vec) {
98  double norm = 0.0;
99  for (int value : vec) {
100  norm += value * value;
101  }
102  return std::sqrt(norm);
103 }
104 
105 // function to normalize a vector of integers to have unit length
106 void gpmp::linalg::std_vector_normalize(const std::vector<int> &vec,
107  std::vector<double> &result) {
108  double norm = std_vector_norm(vec);
109  result.resize(vec.size());
110  for (size_t i = 0; i < vec.size(); ++i) {
111  result[i] = static_cast<double>(vec[i]) / norm;
112  }
113 }
114 
115 // function to compute the projection of one vector of integers onto another
116 void gpmp::linalg::std_vector_projection(const std::vector<int> &vec,
117  const std::vector<int> &onto_vec,
118  std::vector<double> &result) {
119  double dot = std_dot_product(vec, onto_vec);
120  double onto_vec_norm_sq = std_dot_product(onto_vec, onto_vec);
121 
122  result.resize(onto_vec.size());
123  for (size_t i = 0; i < onto_vec.size(); ++i) {
124  result[i] = (dot / onto_vec_norm_sq) * onto_vec[i];
125  }
126 }
void std_cross_product(const std::vector< double > &vec1, const std::vector< double > &vec2, std::vector< double > &result)
Compute the cross product of two 3D vectors.
void std_vector_projection(const std::vector< double > &vec, const std::vector< double > &onto_vec, std::vector< double > &result)
Compute the projection of one vector onto another.
double std_vector_norm(const std::vector< double > &vec)
Compute the Euclidean norm (magnitude) of a vector.
void std_vector_normalize(const std::vector< double > &vec, std::vector< double > &result)
Normalize a vector to have unit length.
T std_dot_product(const std::vector< T > &vec1, const std::vector< T > &vec2)
Compute the dot product of two vectors.
Definition: vector.hpp:681