LCOV - code coverage report
Current view: top level - modules/linalg - vector_naive.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 47 47 100.0 %
Date: 2024-05-13 05:06:06 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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>
      37             : #include <openGPMP/linalg/vector.hpp>
      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           1 : void gpmp::linalg::std_cross_product(const std::vector<double> &vec1,
      49             :                                      const std::vector<double> &vec2,
      50             :                                      std::vector<double> &result) {
      51           1 :     result[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
      52           1 :     result[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
      53           1 :     result[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
      54           1 : }
      55             : 
      56           2 : double gpmp::linalg::std_vector_norm(const std::vector<double> &vec) {
      57           2 :     double norm = 0.0;
      58           6 :     for (double value : vec) {
      59           4 :         norm += value * value;
      60             :     }
      61           2 :     return std::sqrt(norm);
      62             : }
      63             : 
      64             : // function to normalize a vector to have unit length
      65           1 : void gpmp::linalg::std_vector_normalize(const std::vector<double> &vec,
      66             :                                         std::vector<double> &result) {
      67           1 :     double norm = gpmp::linalg::std_vector_norm(vec);
      68           1 :     result.resize(vec.size());
      69           3 :     for (size_t i = 0; i < vec.size(); ++i) {
      70           2 :         result[i] = vec[i] / norm;
      71             :     }
      72           1 : }
      73             : 
      74             : // function to compute the projection of one vector onto another
      75           1 : void gpmp::linalg::std_vector_projection(const std::vector<double> &vec,
      76             :                                          const std::vector<double> &onto_vec,
      77             :                                          std::vector<double> &result) {
      78           1 :     double dot = std_dot_product(vec, onto_vec);
      79           1 :     double onto_vec_norm_sq = std_dot_product(onto_vec, onto_vec);
      80             : 
      81           1 :     result.resize(onto_vec.size());
      82           3 :     for (size_t i = 0; i < onto_vec.size(); ++i) {
      83           2 :         result[i] = (dot / onto_vec_norm_sq) * onto_vec[i];
      84             :     }
      85           1 : }
      86             : 
      87             : // function to compute the cross product of two 3D vectors of integers
      88           1 : void gpmp::linalg::std_cross_product(const std::vector<int> &vec1,
      89             :                                      const std::vector<int> &vec2,
      90             :                                      std::vector<int> &result) {
      91           1 :     result.resize(3);
      92           1 :     result[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
      93           1 :     result[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
      94           1 :     result[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
      95           1 : }
      96             : 
      97           2 : double gpmp::linalg::std_vector_norm(const std::vector<int> &vec) {
      98           2 :     double norm = 0.0;
      99           6 :     for (int value : vec) {
     100           4 :         norm += value * value;
     101             :     }
     102           2 :     return std::sqrt(norm);
     103             : }
     104             : 
     105             : // function to normalize a vector of integers to have unit length
     106           1 : void gpmp::linalg::std_vector_normalize(const std::vector<int> &vec,
     107             :                                         std::vector<double> &result) {
     108           1 :     double norm = std_vector_norm(vec);
     109           1 :     result.resize(vec.size());
     110           3 :     for (size_t i = 0; i < vec.size(); ++i) {
     111           2 :         result[i] = static_cast<double>(vec[i]) / norm;
     112             :     }
     113           1 : }
     114             : 
     115             : // function to compute the projection of one vector of integers onto another
     116           1 : void gpmp::linalg::std_vector_projection(const std::vector<int> &vec,
     117             :                                          const std::vector<int> &onto_vec,
     118             :                                          std::vector<double> &result) {
     119           1 :     double dot = std_dot_product(vec, onto_vec);
     120           1 :     double onto_vec_norm_sq = std_dot_product(onto_vec, onto_vec);
     121             : 
     122           1 :     result.resize(onto_vec.size());
     123           3 :     for (size_t i = 0; i < onto_vec.size(); ++i) {
     124           2 :         result[i] = (dot / onto_vec_norm_sq) * onto_vec[i];
     125             :     }
     126           1 : }

Generated by: LCOV version 1.14