class-projects

Computer Science Coursework

View the Project on GitHub

Table of Contents

Infinity Norm

Routine Name: infNorm

Author: Philip Nelson

Language: C++

Description

infNorm calculates the infinity norm of a vector which is also the maximum element

Input

infNorm(std::array<T, N> v) requires:

Output

A double with the value of the infinity norm

Code

template <typename T, std::size_t N>
double infNorm(std::array<T, N> v)
{
  T max = std::abs(v[0]);
  for (auto&& x : v)
    max = std::max(max, std::abs(x));
  return max;
}

Example

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

  std::cout << " v\n" << v << std::endl;
  std::cout << "Infinity Norm: " << infNorm(v) << std::endl;
}

Result

 v
[        11       18      -20        5 ]

Infinity Norm: 20

Last Modification date: 07 February 2018