openGPMP
Open Source Mathematics Package
vector.hpp
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 
34 #ifndef VECTOR_HPP
35 #define VECTOR_HPP
36 #include <cstdint>
37 #include <vector>
38 
39 namespace gpmp {
40 
41 namespace linalg {
42 
51 template <typename T>
52 void vector_add_i8(const T *data1, const T *data2, T *result_data, size_t size);
53 
62 template <typename T>
63 void vector_add_i16(const T *data1,
64  const T *data2,
65  T *result_data,
66  size_t size);
67 
76 template <typename T>
77 void vector_add_i32(const T *data1,
78  const T *data2,
79  T *result_data,
80  size_t size);
81 
90 template <typename T>
91 void vector_add_i64(const T *data1,
92  const T *data2,
93  T *result_data,
94  size_t size);
95 
102 void vector_add(const std::vector<int8_t> &vec1,
103  const std::vector<int8_t> &vec2,
104  std::vector<int8_t> &result);
105 
112 void vector_add(const std::vector<uint8_t> &vec1,
113  const std::vector<uint8_t> &vec2,
114  std::vector<uint8_t> &result);
115 
122 void vector_add(const std::vector<int16_t> &vec1,
123  const std::vector<int16_t> &vec2,
124  std::vector<int16_t> &result);
125 
132 void vector_add(const std::vector<uint16_t> &vec1,
133  const std::vector<uint16_t> &vec2,
134  std::vector<uint16_t> &result);
135 
142 void vector_add(const std::vector<int32_t> &vec1,
143  const std::vector<int32_t> &vec2,
144  std::vector<int32_t> &result);
145 
152 void vector_add(const std::vector<uint32_t> &vec1,
153  const std::vector<uint32_t> &vec2,
154  std::vector<uint32_t> &result);
155 
162 void vector_add(const std::vector<int64_t> &vec1,
163  const std::vector<int64_t> &vec2,
164  std::vector<int64_t> &result);
165 
172 void vector_add(const std::vector<uint64_t> &vec1,
173  const std::vector<uint64_t> &vec2,
174  std::vector<uint64_t> &result);
175 
176 /*****************************************************************************/
185 template <typename T>
186 void vector_sub_i8(const T *data1, const T *data2, T *result_data, size_t size);
187 
196 template <typename T>
197 void vector_sub_i16(const T *data1,
198  const T *data2,
199  T *result_data,
200  size_t size);
201 
210 template <typename T>
211 void vector_sub_i32(const T *data1,
212  const T *data2,
213  T *result_data,
214  size_t size);
215 
224 template <typename T>
225 void vector_sub_i64(const T *data1,
226  const T *data2,
227  T *result_data,
228  size_t size);
229 
236 void vector_sub(const std::vector<int8_t> &vec1,
237  const std::vector<int8_t> &vec2,
238  std::vector<int8_t> &result);
239 
246 void vector_sub(const std::vector<uint8_t> &vec1,
247  const std::vector<uint8_t> &vec2,
248  std::vector<uint8_t> &result);
249 
256 void vector_sub(const std::vector<int16_t> &vec1,
257  const std::vector<int16_t> &vec2,
258  std::vector<int16_t> &result);
259 
266 void vector_sub(const std::vector<uint16_t> &vec1,
267  const std::vector<uint16_t> &vec2,
268  std::vector<uint16_t> &result);
269 
276 void vector_sub(const std::vector<int32_t> &vec1,
277  const std::vector<int32_t> &vec2,
278  std::vector<int32_t> &result);
279 
286 void vector_sub(const std::vector<uint32_t> &vec1,
287  const std::vector<uint32_t> &vec2,
288  std::vector<uint32_t> &result);
289 
296 void vector_sub(const std::vector<int64_t> &vec1,
297  const std::vector<int64_t> &vec2,
298  std::vector<int64_t> &result);
299 
306 void vector_sub(const std::vector<uint64_t> &vec1,
307  const std::vector<uint64_t> &vec2,
308  std::vector<uint64_t> &result);
309 
310 /*****************************************************************************/
319 template <typename T>
320 void scalar_mult_i8(const T *data, int scalar, T *result_data, size_t size);
321 
330 template <typename T>
331 void scalar_mult_i16(const T *data, int scalar, T *result_data, size_t size);
332 
341 template <typename T>
342 void scalar_mult_i32(const T *data, int scalar, T *result_data, size_t size);
343 
352 template <typename T>
353 void scalar_mult_i64(const T *data, int scalar, T *result_data, size_t size);
354 
361 void scalar_mult(const std::vector<int8_t> &vec1,
362  int scalar,
363  std::vector<int8_t> &result);
364 
371 void scalar_mult(const std::vector<uint8_t> &vec1,
372  int scalar,
373  std::vector<uint8_t> &result);
374 
381 void scalar_mult(const std::vector<int16_t> &vec1,
382  int scalar,
383  std::vector<int16_t> &result);
384 
391 void scalar_mult(const std::vector<uint16_t> &vec1,
392  int scalar,
393  std::vector<uint16_t> &result);
394 
401 void scalar_mult(const std::vector<int32_t> &vec1,
402  int scalar,
403  std::vector<int32_t> &result);
404 
411 void scalar_mult(const std::vector<uint32_t> &vec1,
412  int scalar,
413  std::vector<uint32_t> &result);
414 
421 void scalar_mult(const std::vector<int64_t> &vec1,
422  int scalar,
423  std::vector<int64_t> &result);
424 
431 void scalar_mult(const std::vector<uint64_t> &vec1,
432  int scalar,
433  std::vector<uint64_t> &result);
434 
435 /*****************************************************************************/
444 template <typename T>
445 T dot_product_i8(const T *vec1, const T *vec2, size_t size);
446 
455 template <typename T>
456 T dot_product_i16(const T *vec1, const T *vec2, size_t size);
457 
466 template <typename T>
467 T dot_product_i32(const T *vec1, const T *vec2, size_t size);
468 
477 template <typename T>
478 T dot_product_i64(const T *vec1, const T *vec2, size_t size);
479 
486 int dot_product(const std::vector<int8_t> &vec1,
487  const std::vector<int8_t> &vec2);
488 
495 int dot_product(const std::vector<uint8_t> &vec1,
496  const std::vector<uint8_t> &vec2);
497 
504 int dot_product(const std::vector<int16_t> &vec1,
505  const std::vector<int16_t> &vec2);
506 
513 int dot_product(const std::vector<uint16_t> &vec1,
514  const std::vector<uint16_t> &vec2);
515 
522 int dot_product(const std::vector<int32_t> &vec1,
523  const std::vector<int32_t> &vec2);
524 
531 int dot_product(const std::vector<uint32_t> &vec1,
532  const std::vector<uint32_t> &vec2);
533 
540 int dot_product(const std::vector<int64_t> &vec1,
541  const std::vector<int64_t> &vec2);
542 
549 int dot_product(const std::vector<uint64_t> &vec1,
550  const std::vector<uint64_t> &vec2);
551 
552 /*****************************************************************************/
553 
564 void vector_add(const std::vector<double> &vec1,
565  const std::vector<double> &vec2,
566  std::vector<double> &result);
567 
578 void vector_sub(const std::vector<double> &vec1,
579  const std::vector<double> &vec2,
580  std::vector<double> &result);
581 
593 void scalar_mult(const std::vector<double> &vec,
594  double scalar,
595  std::vector<double> &result);
596 
606 double dot_product(const std::vector<double> &vec1,
607  const std::vector<double> &vec2);
608 
609 /*****************************************************************************/
610 
621 template <typename T>
622 void std_vector_add(const std::vector<T> &vec1,
623  const std::vector<T> &vec2,
624  std::vector<T> &result) {
625  result.resize(vec1.size());
626  for (size_t i = 0; i < vec1.size(); ++i) {
627  result[i] = vec1[i] + vec2[i];
628  }
629 }
630 
641 template <typename T>
642 void std_vector_sub(const std::vector<T> &vec1,
643  const std::vector<T> &vec2,
644  std::vector<T> &result) {
645  result.resize(vec1.size());
646  for (size_t i = 0; i < vec1.size(); ++i) {
647  result[i] = vec1[i] - vec2[i];
648  }
649 }
650 
661 template <typename T>
662 void std_scalar_mult(const std::vector<T> &vec,
663  int scalar,
664  std::vector<T> &result) {
665  result.resize(vec.size());
666  for (size_t i = 0; i < vec.size(); ++i) {
667  result[i] = vec[i] * scalar;
668  }
669 }
670 
680 template <typename T>
681 T std_dot_product(const std::vector<T> &vec1, const std::vector<T> &vec2) {
682  T result = 0;
683  for (size_t i = 0; i < vec1.size(); ++i) {
684  result += vec1[i] * vec2[i];
685  }
686  return result;
687 }
688 
699 void std_cross_product(const std::vector<double> &vec1,
700  const std::vector<double> &vec2,
701  std::vector<double> &result);
702 
711 double std_vector_norm(const std::vector<double> &vec);
712 
721 void std_vector_normalize(const std::vector<double> &vec,
722  std::vector<double> &result);
723 
734 void std_vector_projection(const std::vector<double> &vec,
735  const std::vector<double> &onto_vec,
736  std::vector<double> &result);
747 void std_cross_product(const std::vector<int> &vec1,
748  const std::vector<int> &vec2,
749  std::vector<int> &result);
750 
760 double std_vector_norm(const std::vector<int> &vec);
761 
770 void std_vector_normalize(const std::vector<int> &vec,
771  std::vector<double> &result);
772 
783 void std_vector_projection(const std::vector<int> &vec,
784  const std::vector<int> &onto_vec,
785  std::vector<double> &result);
786 
787 } // namespace linalg
788 } // namespace gpmp
789 
790 #endif
void scalar_mult_i16(const T *data, int scalar, T *result_data, size_t size)
Performs scalar multiplication for signed 16-bit integers.
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.
int dot_product(const std::vector< int8_t > &vec1, const std::vector< int8_t > &vec2)
Computes the dot product for vectors of signed 8-bit integers.
void vector_add_i32(const T *data1, const T *data2, T *result_data, size_t size)
Performs vector addition for signed 32-bit integers.
void vector_add_i8(const T *data1, const T *data2, T *result_data, size_t size)
Performs vector addition for signed 8-bit integers.
T dot_product_i64(const T *vec1, const T *vec2, size_t size)
Computes the dot product for signed 64-bit integer vectors.
void vector_sub_i32(const T *data1, const T *data2, T *result_data, size_t size)
Performs vector subtraction for signed 32-bit integers.
void std_scalar_mult(const std::vector< T > &vec, int scalar, std::vector< T > &result)
Multiply each element of a vector by a scalar.
Definition: vector.hpp:662
void vector_add_i16(const T *data1, const T *data2, T *result_data, size_t size)
Performs vector addition for signed 16-bit integers.
void vector_sub(const std::vector< int8_t > &vec1, const std::vector< int8_t > &vec2, std::vector< int8_t > &result)
Performs vector subtraction for vectors of signed 8-bit integers.
void scalar_mult(const std::vector< int8_t > &vec1, int scalar, std::vector< int8_t > &result)
Performs scalar multiplication for vectors of signed 8-bit integers.
void std_vector_sub(const std::vector< T > &vec1, const std::vector< T > &vec2, std::vector< T > &result)
Subtract one vector from another element-wise.
Definition: vector.hpp:642
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.
void scalar_mult_i32(const T *data, int scalar, T *result_data, size_t size)
Performs scalar multiplication for signed 32-bit integers.
double std_vector_norm(const std::vector< double > &vec)
Compute the Euclidean norm (magnitude) of a vector.
void vector_sub_i8(const T *data1, const T *data2, T *result_data, size_t size)
Performs vector subtraction for signed 8-bit integers.
void std_vector_normalize(const std::vector< double > &vec, std::vector< double > &result)
Normalize a vector to have unit length.
void scalar_mult_i64(const T *data, int scalar, T *result_data, size_t size)
Performs scalar multiplication for signed 64-bit integers.
void vector_add(const std::vector< int8_t > &vec1, const std::vector< int8_t > &vec2, std::vector< int8_t > &result)
Performs vector addition for vectors of signed 8-bit integers.
void vector_sub_i64(const T *data1, const T *data2, T *result_data, size_t size)
Performs vector subtraction for signed 64-bit integers.
T dot_product_i16(const T *vec1, const T *vec2, size_t size)
Computes the dot product for signed 16-bit integer vectors.
T dot_product_i8(const T *vec1, const T *vec2, size_t size)
Computes the dot product for signed 8-bit integer vectors.
void scalar_mult_i8(const T *data, int scalar, T *result_data, size_t size)
Performs scalar multiplication for signed 8-bit integers.
void vector_add_i64(const T *data1, const T *data2, T *result_data, size_t size)
Performs vector addition for signed 64-bit integers.
void std_vector_add(const std::vector< T > &vec1, const std::vector< T > &vec2, std::vector< T > &result)
Add two vectors element-wise.
Definition: vector.hpp:622
void vector_sub_i16(const T *data1, const T *data2, T *result_data, size_t size)
Performs vector subtraction for signed 16-bit integers.
T dot_product_i32(const T *vec1, const T *vec2, size_t size)
Computes the dot product for signed 32-bit integer vectors.
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
The source C++ openGPMP namespace.