1
1
using System ;
2
2
using Algorithms . LinearAlgebra . Eigenvalue ;
3
3
using FluentAssertions ;
4
- using Utilities . Extensions ;
5
4
using NUnit . Framework ;
5
+ using Utilities . Extensions ;
6
6
7
7
namespace Algorithms . Tests . LinearAlgebra . Eigenvalue
8
8
{
9
9
public class PowerIterationTests
10
10
{
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
+
11
27
private readonly double epsilon = Math . Pow ( 10 , - 5 ) ;
12
28
13
29
[ Test ]
14
30
public void Dominant_ShouldThrowArgumentException_WhenSourceMatrixIsNotSquareShaped ( )
15
31
{
16
32
// 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
+
19
35
// Act
20
36
Action action = ( ) => PowerIteration . Dominant ( source , StartVector ( source . GetLength ( 0 ) ) , epsilon ) ;
21
-
37
+
22
38
// Assert
23
39
action . Should ( ) . Throw < ArgumentException > ( ) . WithMessage ( "The source matrix is not square-shaped." ) ;
24
40
}
25
-
41
+
26
42
[ Test ]
27
43
public void Dominant_ShouldThrowArgumentException_WhenStartVectorIsNotSameSizeAsMatrix ( )
28
44
{
29
45
// 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
+
33
49
// Act
34
50
Action action = ( ) => PowerIteration . Dominant ( source , startVector , epsilon ) ;
35
-
51
+
36
52
// Assert
37
53
action . Should ( ) . Throw < ArgumentException > ( )
38
54
. WithMessage ( "The length of the start vector doesn't equal the size of the source matrix." ) ;
39
55
}
40
56
41
- [ Test , TestCaseSource ( nameof ( DominantVectorTestCases ) ) ]
57
+ [ TestCaseSource ( nameof ( DominantVectorTestCases ) ) ]
42
58
public void Dominant_ShouldCalculateDominantEigenvalueAndEigenvector (
43
- double eigenvalue , double [ ] eigenvector , double [ , ] source )
59
+ double eigenvalue ,
60
+ double [ ] eigenvector ,
61
+ double [ , ] source )
44
62
{
45
63
// 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
+
48
67
// Assert
49
68
actualEigVal . Should ( ) . BeApproximately ( eigenvalue , epsilon ) ;
50
69
actualEigVec . Magnitude ( ) . Should ( ) . BeApproximately ( eigenvector . Magnitude ( ) , epsilon ) ;
51
70
}
52
71
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 ) ;
70
73
}
71
- }
74
+ }
0 commit comments