fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <numeric>
  4. #include <cmath>
  5. #include <cassert>
  6.  
  7.  
  8. double cos(std::vector<double> const& lhs, std::vector<double> const& rhs) {
  9. assert(lhs.size() == rhs.size());
  10. return std::inner_product(lhs.begin(), lhs.end(), rhs.begin(), 0.)
  11. / (std::sqrt(std::inner_product(lhs.begin(), lhs.end(), lhs.begin(), 0.))
  12. * std::sqrt(std::inner_product(rhs.begin(), rhs.end(), rhs.begin(), 0.)));
  13. }
  14.  
  15.  
  16. int main() {
  17. std::cout << cos({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}) << std::endl;
  18. }
  19.  
Success #stdin #stdout 0s 2960KB
stdin
Standard input is empty
stdout
0.571429