fork download
  1.  
  2. #include <array>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. constexpr std::array<double, 5> p = { 1.0, 0.0, 3.0, 5.0, 0.0 };
  7.  
  8. template<size_t index, bool isZero>
  9. struct DotProductCalculator
  10. {
  11. static double Calculate(const std::vector<double>& xArg)
  12. {
  13. return (xArg[index] * p[index])
  14. + DotProductCalculator<index - 1, p[index - 1] == 0.0>::Calculate(xArg);
  15. }
  16. };
  17.  
  18. template<>
  19. struct DotProductCalculator<0, true>
  20. {
  21. static double Calculate(const std::vector<double>& xArg)
  22. {
  23. return 0.0;
  24. }
  25. };
  26.  
  27. template<>
  28. struct DotProductCalculator<0, false>
  29. {
  30. static double Calculate(const std::vector<double>& xArg)
  31. {
  32. return xArg[0] * p[0];
  33. }
  34. };
  35.  
  36. template<size_t index>
  37. struct DotProductCalculator<index, true>
  38. {
  39. static double Calculate(const std::vector<double>& xArg)
  40. {
  41. return 0.0 + DotProductCalculator<index - 1, p[index - 1] == 0.0>::Calculate(xArg);
  42. }
  43. };
  44.  
  45. template<typename ArrayType>
  46. double f_p_driver(const std::vector<double>& xArg, const ArrayType& pAsArgument)
  47. {
  48. return DotProductCalculator<std::tuple_size<ArrayType>::value - 1,
  49. p[std::tuple_size<ArrayType>::value -1] == 0.0>::Calculate(xArg);
  50. }
  51.  
  52. int main()
  53. {
  54. std::vector<double> x = { 1.0, 2.0, 3.0, 4.0, 5.0 };
  55. double result = f_p_driver(x, p);
  56. std::cout << "Result: " << result;
  57. return 0;
  58. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Result: 30