Computer Science Coursework
Routine Name: pNorm
Author: Philip Nelson
Language: C++
pNorm
calculates the p norm of a vector
pNorm(std::array<T, N> v, unsigned int const& p)
requires:
std::array<T, N> v
- a column vector of type T
and size M
unsigned int p
- the p norm that is desiredA double with the desired norm value.
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);
}
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;
}
v
[ 11 18 -20 5 ]
1 Norm: 54
2 Norm: 29.5
Last Modification date: 07 February 2018