@@ -11,73 +11,83 @@ import XCTest
11
11
12
12
class PolynomialTests : XCTestCase {
13
13
14
- let accuracy = 1.0e-5
14
+ let accuracy : CGFloat = 1.0e-5
15
15
16
16
func testEvaluation( ) {
17
17
let point = BernsteinPolynomial0 ( b0: 3.0 )
18
18
XCTAssertEqual ( point. reduce ( a1: 1 , a2: 2 ) , 0 )
19
- XCTAssertEqual ( point. f ( 0 ) , 3 )
20
- XCTAssertEqual ( point. f ( 0.5 ) , 3 )
21
- XCTAssertEqual ( point. f ( 1 ) , 3 )
19
+ XCTAssertEqual ( point. value ( at : 0 ) , 3 )
20
+ XCTAssertEqual ( point. value ( at : 0.5 ) , 3 )
21
+ XCTAssertEqual ( point. value ( at : 1 ) , 3 )
22
22
XCTAssertEqual ( point. derivative, BernsteinPolynomial0 ( b0: 0.0 ) )
23
- XCTAssertEqual ( point. analyticalRoots ( between: 0 , and: 1 ) , [ ] )
23
+ XCTAssertEqual ( point. distinctAnalyticalRoots ( between: 0 , and: 1 ) , [ ] )
24
24
XCTAssertEqual ( point. coefficients, [ 3.0 ] )
25
25
26
26
let line = BernsteinPolynomial1 ( b0: 2.0 , b1: 4.0 )
27
- XCTAssertEqual ( line. f ( 0 ) , 2 )
28
- XCTAssertEqual ( line. f ( 0.5 ) , 3 )
29
- XCTAssertEqual ( line. f ( 1 ) , 4 )
27
+ XCTAssertEqual ( line. value ( at : 0 ) , 2 )
28
+ XCTAssertEqual ( line. value ( at : 0.5 ) , 3 )
29
+ XCTAssertEqual ( line. value ( at : 1 ) , 4 )
30
30
XCTAssertEqual ( line. derivative, BernsteinPolynomial0 ( b0: 2 ) )
31
- XCTAssertEqual ( line. analyticalRoots ( between: - 2 , and: 1 ) , [ - 1 ] )
32
- XCTAssertEqual ( line. analyticalRoots ( between: 0 , and: 1 ) , [ ] )
31
+ XCTAssertEqual ( line. distinctAnalyticalRoots ( between: - 2 , and: 1 ) , [ - 1 ] )
32
+ XCTAssertEqual ( line. distinctAnalyticalRoots ( between: 0 , and: 1 ) , [ ] )
33
33
XCTAssertEqual ( line. coefficients, [ 2 , 4 ] )
34
34
35
35
let quad = BernsteinPolynomial2 ( b0: - 1 , b1: 1.0 , b2: 0.0 )
36
- XCTAssertEqual ( quad. f ( 0 ) , - 1 )
37
- XCTAssertEqual ( quad. f ( 0.5 ) , 0.25 )
38
- XCTAssertEqual ( quad. f ( 1 ) , 0 )
36
+ XCTAssertEqual ( quad. value ( at : 0 ) , - 1 )
37
+ XCTAssertEqual ( quad. value ( at : 0.5 ) , 0.25 )
38
+ XCTAssertEqual ( quad. value ( at : 1 ) , 0 )
39
39
XCTAssertEqual ( quad. derivative, BernsteinPolynomial1 ( b0: 4 , b1: - 2 ) )
40
40
XCTAssertEqual ( quad. coefficients, [ - 1 , 1 , 0 ] )
41
41
}
42
42
43
43
func testDegree1( ) {
44
44
let polynomial = BernsteinPolynomial1 ( b0: - 3 , b1: 2 )
45
- let roots = findRoots ( of: polynomial, between: - 1 , and: 1 )
45
+ let roots = findDistinctRoots ( of: polynomial, between: - 1 , and: 1 )
46
46
XCTAssertEqual ( roots. count, 1 )
47
- XCTAssertEqual ( roots [ 0 ] , 0.6 , accuracy: accuracy)
47
+ XCTAssertEqual ( roots [ 0 ] , CGFloat ( 0.6 ) , accuracy: accuracy)
48
48
}
49
49
50
50
func testDegree2( ) {
51
51
let polynomial = BernsteinPolynomial2 ( b0: - 5 , b1: - 6 , b2: - 4 )
52
- let roots = findRoots ( of: polynomial, between: - 10 , and: 10 )
52
+ let roots = findDistinctRoots ( of: polynomial, between: - 10 , and: 10 )
53
53
XCTAssertEqual ( roots [ 0 ] , - 1 , accuracy: accuracy)
54
54
XCTAssertEqual ( roots [ 1 ] , 1.0 + 2.0 / 3.0 , accuracy: accuracy)
55
55
}
56
56
57
57
func testDegree3( ) {
58
58
// x^3 - 6x^2 + 11x - 6
59
59
let polynomial = BernsteinPolynomial3 ( b0: - 6 , b1: - 7.0 / 3.0 , b2: - 2.0 / 3.0 , b3: 0 )
60
- XCTAssertEqual ( polynomial. coefficients, [ - 6 , - 7.0 / 3.0 , - 2.0 / 3.0 , 0.0 ] )
61
- let roots = findRoots ( of: polynomial, between: 0 , and: 4 )
60
+ XCTAssertEqual ( polynomial. coefficients, [ - 6 , CGFloat ( - 7.0 / 3.0 ) , CGFloat ( - 2.0 / 3.0 ) , 0.0 ] )
61
+ let roots = findDistinctRoots ( of: polynomial, between: 0 , and: 4 )
62
62
XCTAssertEqual ( roots [ 0 ] , 1 , accuracy: accuracy)
63
63
XCTAssertEqual ( roots [ 1 ] , 2 , accuracy: accuracy)
64
64
XCTAssertEqual ( roots [ 2 ] , 3 , accuracy: accuracy)
65
65
}
66
66
67
- func testDegree3RepeatedRoot ( ) {
67
+ func testDegree3RepeatedRoot1 ( ) {
68
68
// x^3 - 4x^2 + 5x - 2
69
69
// repeated root at x = 1
70
70
let polynomial = BernsteinPolynomial3 ( b0: - 2 , b1: - 1.0 / 3.0 , b2: 0 , b3: 0 )
71
- let roots = findRoots ( of: polynomial, between: - 1 , and: 3 )
71
+ let roots = findDistinctRoots ( of: polynomial, between: - 1 , and: 3 )
72
72
XCTAssertEqual ( roots [ 0 ] , 1 , accuracy: accuracy)
73
73
XCTAssertEqual ( roots [ 1 ] , 2 , accuracy: accuracy)
74
74
}
75
75
76
+ // func testDegree3RootExactlyZero() {
77
+ // // root is exactly t = 0 (at the start of unit interval),
78
+ // // so may be accidentally discarded due to numerical precision
79
+ // let polynomial = BernsteinPolynomial3(b0: 0, b1: 96, b2: -24, b3: -36)
80
+ // let roots = findRoots(of: polynomial, between: 0, and: 1)
81
+ // XCTAssertEqual(roots.count, 2)
82
+ // XCTAssertEqual(roots[0], 0.0)
83
+ // XCTAssertEqual(roots[1], 2.0 / 3.0, accuracy: accuracy)
84
+ // }
85
+
76
86
func testDegree4( ) {
77
87
// x^4 - 2.44x^2 + 1.44
78
- let polynomial = BernsteinPolynomial4 ( b0: 1.44 , b1: 1.44 , b2: 1.44 - 1.22 / 3 , b3: 0.22 , b4: 0 )
79
- XCTAssertEqual ( polynomial. coefficients, [ 1.44 , 1.44 , 1.44 - 1.22 / 3 , 0.22 , 0 ] )
80
- let roots = findRoots ( of: polynomial, between: - 2 , and: 2 )
88
+ let polynomial = BernsteinPolynomial4 ( b0: 1.44 , b1: 1.44 , b2: CGFloat ( 1.44 - 1.22 / 3 ) , b3: 0.22 , b4: 0 )
89
+ XCTAssertEqual ( polynomial. coefficients, [ 1.44 , 1.44 , CGFloat ( 1.44 - 1.22 / 3 ) , 0.22 , 0 ] )
90
+ let roots = findDistinctRoots ( of: polynomial, between: - 2 , and: 2 )
81
91
XCTAssertEqual ( roots [ 0 ] , - 1.2 , accuracy: accuracy)
82
92
XCTAssertEqual ( roots [ 1 ] , - 1 , accuracy: accuracy)
83
93
XCTAssertEqual ( roots [ 2 ] , 1 , accuracy: accuracy)
@@ -87,7 +97,7 @@ class PolynomialTests: XCTestCase {
87
97
func testDegree4RepeatedRoots( ) {
88
98
// x^4 - 2x^2 + 1
89
99
let polynomial = BernsteinPolynomial4 ( b0: 1 , b1: 1 , b2: 2.0 / 3.0 , b3: 0 , b4: 0 )
90
- let roots = findRoots ( of: polynomial, between: - 2 , and: 2 )
100
+ let roots = findDistinctRoots ( of: polynomial, between: - 2 , and: 2 )
91
101
XCTAssertEqual ( roots. count, 2 )
92
102
XCTAssertEqual ( roots [ 0 ] , - 1 , accuracy: accuracy)
93
103
XCTAssertEqual ( roots [ 1 ] , 1 , accuracy: accuracy)
@@ -97,16 +107,15 @@ class PolynomialTests: XCTestCase {
97
107
// 0.2x^5 - 0.813333x^3 - 8.56x
98
108
let polynomial = BernsteinPolynomial5 ( b0: 0 , b1: - 1.712 , b2: - 3.424 , b3: - 5.2173333 , b4: - 7.1733332 , b5: - 9.173333 )
99
109
XCTAssertEqual ( polynomial. coefficients, [ 0 , - 1.712 , - 3.424 , - 5.2173333 , - 7.1733332 , - 9.173333 ] )
100
- let roots = findRoots ( of: polynomial, between: - 4 , and: 4 )
101
- XCTAssertEqual ( polynomial. analyticalRoots ( between: - 5 , and: 5 ) , nil , " shouldn't be possible to solve analytically " )
110
+ let roots = findDistinctRoots ( of: polynomial, between: - 4 , and: 4 )
102
111
XCTAssertEqual ( roots [ 0 ] , - 2.9806382 , accuracy: accuracy)
103
112
XCTAssertEqual ( roots [ 1 ] , 0 , accuracy: accuracy)
104
113
XCTAssertEqual ( roots [ 2 ] , 2.9806382 , accuracy: accuracy)
105
114
}
106
115
107
116
func testDegree4RealWorldIssue( ) {
108
117
let polynomial = BernsteinPolynomial4 ( b0: 1819945.4373168945 , b1: - 3353335.8194732666 , b2: 3712712.6330566406 , b3: - 2836657.1703338623 , b4: 2483314.5947265625 )
109
- let roots = findRoots ( of: polynomial, between : 0 , and : 1 )
118
+ let roots = findDistinctRootsInUnitInterval ( of: polynomial)
110
119
XCTAssertEqual ( roots. count, 2 )
111
120
XCTAssertEqual ( roots [ 0 ] , 0.15977874432923783 , accuracy: 1.0e-5 )
112
121
XCTAssertEqual ( roots [ 1 ] , 0.407811682610126 , accuracy: 1.0e-5 )
0 commit comments