class-projects

Computer Science Coursework

View the Project on GitHub

Table of Contents

P Norm

Routine Name: pNorm

Author: Philip Nelson

Language: C++

Description

pNorm calculates the p norm of a vector

Input

pNorm(std::array<T, N> v, unsigned int const& p) requires:

Output

A double with the desired norm value.

Code

template <typename T, std::size_t N>
double pNorm(std::array<T, N> v, unsigned int const& p)
{
  double sum = 0.0;
  for (auto&& x : v)
    sum += std::pow(std::abs(x), p);
  return std::pow(sum, 1.0 / p);
}

Example

int main()
{
  std::array<double, 4> v = {11, 18, -20, 5};

  std::cout << " v\n" << v << std::endl;
  std::cout << "1 Norm: " << pNorm(v, 1) << std::endl;
  std::cout << "2 Norm: " << pNorm(v, 2) << std::endl;
}

Result

 v
[        11       18      -20        5 ]

1 Norm: 54
2 Norm: 29.5

Last Modification date: 07 February 2018