Algorithms and Subroutines for Solving Differential Equations
Routine Name: oneNorm
Author: Philip Nelson
Language: C++
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.
oneNorm(Matrix<T, M, N>& m)
requires:
Matrix<T, M, N> m
- an M
xN
matrix of type T
A double with the desired one norm of the matrix.
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());
}
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;
}
A
| 6 1 1 |
| 4 -2 5 |
| 2 8 7 |
One Norm: 13
Last Modification date: 10 February 2018