11using System ;
22using Algorithms . LinearAlgebra . Eigenvalue ;
33using FluentAssertions ;
4- using Utilities . Extensions ;
54using NUnit . Framework ;
5+ using Utilities . Extensions ;
66
77namespace Algorithms . Tests . LinearAlgebra . Eigenvalue
88{
99 public class PowerIterationTests
1010 {
11+ private static readonly object [ ] DominantVectorTestCases =
12+ {
13+ new object [ ]
14+ {
15+ 3.0 ,
16+ new [ ] { 0.7071039 , 0.70710966 } ,
17+ new [ , ] { { 2.0 , 1.0 } , { 1.0 , 2.0 } } ,
18+ } ,
19+ new object [ ]
20+ {
21+ 4.235889 ,
22+ new [ ] { 0.91287093 , 0.40824829 } ,
23+ new [ , ] { { 2.0 , 5.0 } , { 1.0 , 2.0 } } ,
24+ } ,
25+ } ;
26+
1127 private readonly double epsilon = Math . Pow ( 10 , - 5 ) ;
1228
1329 [ Test ]
1430 public void Dominant_ShouldThrowArgumentException_WhenSourceMatrixIsNotSquareShaped ( )
1531 {
1632 // Arrange
17- var source = new double [ , ] { { 1 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 1 } , { 0 , 0 , 0 } } ;
18-
33+ var source = new double [ , ] { { 1 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 1 } , { 0 , 0 , 0 } } ;
34+
1935 // Act
2036 Action action = ( ) => PowerIteration . Dominant ( source , StartVector ( source . GetLength ( 0 ) ) , epsilon ) ;
21-
37+
2238 // Assert
2339 action . Should ( ) . Throw < ArgumentException > ( ) . WithMessage ( "The source matrix is not square-shaped." ) ;
2440 }
25-
41+
2642 [ Test ]
2743 public void Dominant_ShouldThrowArgumentException_WhenStartVectorIsNotSameSizeAsMatrix ( )
2844 {
2945 // Arrange
30- var source = new double [ , ] { { 1 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 1 } } ;
31- var startVector = new double [ ] { 1 , 0 , 0 , 0 } ;
32-
46+ var source = new double [ , ] { { 1 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 1 } } ;
47+ var startVector = new double [ ] { 1 , 0 , 0 , 0 } ;
48+
3349 // Act
3450 Action action = ( ) => PowerIteration . Dominant ( source , startVector , epsilon ) ;
35-
51+
3652 // Assert
3753 action . Should ( ) . Throw < ArgumentException > ( )
3854 . WithMessage ( "The length of the start vector doesn't equal the size of the source matrix." ) ;
3955 }
4056
41- [ Test , TestCaseSource ( nameof ( DominantVectorTestCases ) ) ]
57+ [ TestCaseSource ( nameof ( DominantVectorTestCases ) ) ]
4258 public void Dominant_ShouldCalculateDominantEigenvalueAndEigenvector (
43- double eigenvalue , double [ ] eigenvector , double [ , ] source )
59+ double eigenvalue ,
60+ double [ ] eigenvector ,
61+ double [ , ] source )
4462 {
4563 // Act
46- var ( actualEigVal , actualEigVec ) = PowerIteration . Dominant ( source , StartVector ( source . GetLength ( 0 ) ) , epsilon ) ;
47-
64+ var ( actualEigVal , actualEigVec ) =
65+ PowerIteration . Dominant ( source , StartVector ( source . GetLength ( 0 ) ) , epsilon ) ;
66+
4867 // Assert
4968 actualEigVal . Should ( ) . BeApproximately ( eigenvalue , epsilon ) ;
5069 actualEigVec . Magnitude ( ) . Should ( ) . BeApproximately ( eigenvector . Magnitude ( ) , epsilon ) ;
5170 }
5271
53- static readonly object [ ] DominantVectorTestCases =
54- {
55- new object [ ]
56- {
57- 3.0 ,
58- new [ ] { 0.7071039 , 0.70710966 } ,
59- new [ , ] { { 2.0 , 1.0 } , { 1.0 , 2.0 } }
60- } ,
61- new object [ ]
62- {
63- 4.235889 ,
64- new [ ] { 0.91287093 , 0.40824829 } ,
65- new [ , ] { { 2.0 , 5.0 } , { 1.0 , 2.0 } }
66- }
67- } ;
68-
69- private double [ ] StartVector ( int length ) => new Random ( Seed : 111111 ) . NextVector ( length ) ;
72+ private double [ ] StartVector ( int length ) => new Random ( 111111 ) . NextVector ( length ) ;
7073 }
71- }
74+ }
0 commit comments