MATH5620

Algorithms and Subroutines for Solving Differential Equations

Table of Contents

Matrix One Norm

Routine Name: oneNorm

Author: Philip Nelson

Language: C++

Description

oneNorm calculates the one norm of a matrix. Give a matrix \(A\), the one norm, \(|A|_{1}\), is the maximum of the absolute column sums.

Input

oneNorm(Matrix<T, M, N>& m) requires:

Output

A double with the desired one norm of the matrix.

Code

template <typename T, std::size_t M, std::size_t N>
T oneNorm(Matrix<T, M, N>& m)
{
  std::array<T, N> colSum;
  for (auto j = 0u; j < M; ++j)
  {
    colSum[j] = 0;
    for (auto i = 0u; i < N; ++i)
    {
      colSum[j] += std::abs(m[i][j]);
    }
  }

  return *std::max_element(colSum.begin(), colSum.end());
}

Example

int main()
{
Matrix<int, 3, 3> A({
                    {6, 1, 1},
                    {4, -2, 5},
                    {2, 8, 7}
                    });

std::cout << " A\n" << A << std::endl;
std::cout << "One Norm: " << oneNorm(A) << std::endl;
}

Result

 A
|          6         1         1 |
|          4        -2         5 |
|          2         8         7 |

One Norm: 13

Last Modification date: 10 February 2018