Computer Science Coursework
Routine Name: inversePowerIteration
Author: Philip Nelson
Language: C++
inversePowerIteration
calculates the smallest Eigenvalue of a N
xN
matrix.
inversePowerIteration(Matrix<T, N, N> const& A, unsigned int const& MAX)
requires:
Matrix<T, N, N> const& A
- an N
xN
matrixunsigned int const& MAX
- the maximum number of iterationsThe smallest Eigenvalue
template <typename T, std::size_t N>
T inversePowerIteration(Matrix<T, N, N> & A, unsigned int const& MAX)
{
std::array<T, N> v;
for (auto&& e : v)
e = randDouble(0.0, 10.0);
T lamda = 0;
for (auto i = 0u; i < MAX; ++i)
{
auto w = A.solveLinearSystemLU(v);
v = w / pNorm(w,2);
lamda = v*(A*v);
}
return lamda;
}
int main()
{
Matrix<double, 5, 5> A(
[](unsigned int const& i, unsigned int const& j) { return 1.0 / (i + j + 1.0); });
auto eigval = inversePowerIteration(A, 1000u);
std::cout << "A\n" << A << std::endl;
std::cout << "Smallest Eigenvalue\n" << eigval << std::endl;
}
A
| 1 0.5 0.333 0.25 0.2 |
| 0.5 0.333 0.25 0.2 0.167 |
| 0.333 0.25 0.2 0.167 0.143 |
| 0.25 0.2 0.167 0.143 0.125 |
| 0.2 0.167 0.143 0.125 0.111 |
Smallest Eigenvalue
3.29e-06
Last Modification date: 27 February 2018