4
4
#category : #' Math-Tests-Polynomials'
5
5
}
6
6
7
- { #category : #comparing }
7
+ { #category : #' testing - comparing' }
8
8
PMPolynomialTest >> testIsZero [
9
9
| p1 p2 |
10
10
p1 := PMPolynomial coefficients: #(0 0 0 0 0) .
@@ -13,32 +13,50 @@ PMPolynomialTest >> testIsZero [
13
13
self shouldnt: [ p2 isZero ]
14
14
]
15
15
16
- { #category : #' function evaluation ' }
16
+ { #category : #' testing - addition ' }
17
17
PMPolynomialTest >> testPolynomialAddition [
18
- | polynomial |
19
- polynomial := ( PMPolynomial coefficients: #(2 -3 1) )
20
- + ( PMPolynomial coefficients: #(-3 7 2 1) ).
21
- self assert: (polynomial at: 0 ) equals: - 1 .
22
- self assert: ( polynomial at: 1 ) equals: 4 .
23
- self assert: (polynomial at: 2 ) equals: 3 .
24
- self assert: ( polynomial at: 3 ) equals: 1 .
18
+
19
+ | polynomial expected p q |
20
+ p := PMPolynomial coefficients: #( 2 -3 1 ) .
21
+ q := PMPolynomial coefficients: #( -3 7 2 1 ) .
22
+ polynomial := p + q .
23
+ expected := PMPolynomial coefficients: #( -1 4 3 1 ) .
24
+ self assert: polynomial equals: expected .
25
25
self assert: (polynomial at: 4 ) equals: 0
26
26
]
27
27
28
- { #category : #' function evaluation' }
28
+ { #category : #' testing - addition' }
29
+ PMPolynomialTest >> testPolynomialAdditionIsCommutative [
30
+
31
+ | p q expected |
32
+ p := PMPolynomial coefficients: #(1 2 4 8) .
33
+ q := PMPolynomial coefficients: #(-1 3 7 -4) .
34
+
35
+ expected := PMPolynomial coefficients: #(0 5 11 4) .
36
+ self assert: p + q equals: expected.
37
+ self assert: q + p equals: expected
38
+ ]
39
+
40
+ { #category : #' testing - algebra' }
29
41
PMPolynomialTest >> testPolynomialDerivative [
30
42
" Code example 2.3"
43
+ "
44
+ p(x) = x^3 + 2x^2 + 7x - 3, therefore:
45
+ p'(x) = 3x^2 + 4x + 7
46
+ "
31
47
32
- | polynomial |
33
- polynomial := (PMPolynomial coefficients: #(-3 7 2 1) ) derivative.
34
- self assert: (polynomial at: 0 ) equals: 7 .
35
- self assert: (polynomial at: 1 ) equals: 4 .
36
- self assert: (polynomial at: 2 ) equals: 3 .
37
- self assert: (polynomial at: 3 ) equals: 0 .
38
- self assert: (polynomial at: 4 ) equals: 0
48
+ | p derivative expectedDerivative |
49
+ p := PMPolynomial coefficients: #( -3 7 2 1 ) .
50
+
51
+ derivative := p derivative.
52
+
53
+ expectedDerivative := PMPolynomial coefficients: #( 7 4 3 ) .
54
+ self assert: derivative equals: expectedDerivative.
55
+ self assert: (derivative at: 3 ) equals: 0 .
56
+ self assert: (derivative at: 4 ) equals: 0
39
57
]
40
58
41
- { #category : #' function evaluation ' }
59
+ { #category : #' testing - division ' }
42
60
PMPolynomialTest >> testPolynomialDivision [
43
61
| pol1 pol2 polynomial |
44
62
pol1 := PMPolynomial coefficients: #(2 -3 1) .
@@ -53,7 +71,7 @@ PMPolynomialTest >> testPolynomialDivision [
53
71
self assert: (polynomial at: 6 ) equals: 0
54
72
]
55
73
56
- { #category : #' function evaluation ' }
74
+ { #category : #' testing - division ' }
57
75
PMPolynomialTest >> testPolynomialDivisionBug [
58
76
" identify an error when trying to create a zero dividend"
59
77
@@ -63,7 +81,7 @@ PMPolynomialTest >> testPolynomialDivisionBug [
63
81
self shouldnt: [ pol1 / pol2 ] raise: Error
64
82
]
65
83
66
- { #category : #arithmetic }
84
+ { #category : #' testing - arithmetic' }
67
85
PMPolynomialTest >> testPolynomialDoubleDispatch [
68
86
| n p |
69
87
n := 3.2 .
@@ -80,7 +98,7 @@ PMPolynomialTest >> testPolynomialDoubleDispatch [
80
98
self assert: n - p equals: (p - n) negated
81
99
]
82
100
83
- { #category : #' function evaluation ' }
101
+ { #category : #' testing - algebra ' }
84
102
PMPolynomialTest >> testPolynomialEvaluation [
85
103
" Code example 2.2"
86
104
@@ -89,7 +107,7 @@ PMPolynomialTest >> testPolynomialEvaluation [
89
107
self assert: 0 equals: (polynomial value: 1 )
90
108
]
91
109
92
- { #category : #comparing }
110
+ { #category : #' testing - comparing' }
93
111
PMPolynomialTest >> testPolynomialHash [
94
112
" polynomial hash is hash of coefficient array"
95
113
@@ -105,118 +123,148 @@ PMPolynomialTest >> testPolynomialHash [
105
123
self assert: p3 hash equals: p2 hash
106
124
]
107
125
108
- { #category : #' function evaluation ' }
126
+ { #category : #' testing - algebra ' }
109
127
PMPolynomialTest >> testPolynomialIntegral [
110
128
" Code example 2.3"
111
129
112
- | polynomial |
113
- polynomial := (PMPolynomial coefficients: #(-3 7 2 1) ) integral.
114
- self assert: (polynomial at: 0 ) equals: 0 .
115
- self assert: (polynomial at: 1 ) equals: - 3 .
116
- self assert: (polynomial at: 2 ) equals: 7 / 2 .
117
- self assert: (polynomial at: 3 ) equals: 2 / 3 .
118
- self assert: (polynomial at: 4 ) equals: 1 / 4 .
130
+ "
131
+ Given p(x) = x^3 + 2x^2 + 7x - 3
132
+ then the integral is I(x) = 1/4 x^4 + 2/3 x^3 + 7/2 x^2 - 3x + C, where C is an arbitary
133
+ constant.
134
+ "
135
+
136
+ | polynomial expectedCoefficients expected |
137
+ polynomial := (PMPolynomial coefficients: #( -3 7 2 1 ) ) integral.
138
+ expectedCoefficients := Array
139
+ with: 0
140
+ with: - 3
141
+ with: 7 / 2
142
+ with: 2 / 3
143
+ with: 1 / 4 .
144
+ expected := PMPolynomial coefficients: expectedCoefficients.
145
+ self assert: polynomial equals: expected .
119
146
self assert: (polynomial at: 5 ) equals: 0
120
147
]
121
148
122
- { #category : #' function evaluation ' }
149
+ { #category : #' testing - algebra ' }
123
150
PMPolynomialTest >> testPolynomialIntegralWithConstant [
124
151
" Code example 2.3"
125
152
126
- | polynomial |
127
- polynomial := (PMPolynomial coefficients: #(-3 7 2 1) ) integral: 5 .
128
- self assert: (polynomial at: 0 ) equals: 5 .
129
- self assert: (polynomial at: 1 ) equals: - 3 .
130
- self assert: (polynomial at: 2 ) equals: 7 / 2 .
131
- self assert: (polynomial at: 3 ) equals: 2 / 3 .
132
- self assert: (polynomial at: 4 ) equals: 1 / 4 .
153
+ | polynomial arbitraryConstant integrand expectedCoefficients expected |
154
+ arbitraryConstant := 5 .
155
+ integrand := PMPolynomial coefficients: #( -3 7 2 1 ) .
156
+ polynomial := integrand integral: arbitraryConstant.
157
+ expectedCoefficients := Array
158
+ with: arbitraryConstant
159
+ with: - 3
160
+ with: 7 / 2
161
+ with: 2 / 3
162
+ with: 1 / 4 .
163
+ expected := PMPolynomial coefficients: expectedCoefficients.
164
+ self assert: polynomial equals: expected.
133
165
self assert: (polynomial at: 5 ) equals: 0
134
166
]
135
167
136
- { #category : #' function evaluation ' }
168
+ { #category : #' testing - multiplication ' }
137
169
PMPolynomialTest >> testPolynomialMultiplication [
138
170
" Code example 2.3"
139
171
140
- | pol1 pol2 polynomial |
141
- pol1 := PMPolynomial coefficients: #(2 -3 1) .
142
- pol2 := PMPolynomial coefficients: #(-3 7 2 1) .
143
- polynomial := pol1 * pol2.
144
- self assert: (polynomial at: 0 ) equals: - 6 .
145
- self assert: (polynomial at: 1 ) equals: 23 .
146
- self assert: (polynomial at: 2 ) equals: - 20 .
147
- self assert: (polynomial at: 3 ) equals: 3 .
148
- self assert: (polynomial at: 4 ) equals: - 1 .
149
- self assert: (polynomial at: 5 ) equals: 1 .
150
- self assert: (polynomial at: 6 ) equals: 0
172
+ | p q product expected |
173
+ p := PMPolynomial coefficients: #( 2 -3 1 ) .
174
+ q := PMPolynomial coefficients: #( -3 7 2 1 ) .
175
+ product := p * q.
176
+ expected := PMPolynomial coefficients: #( -6 23 -20 3 -1 1 ) .
177
+ self assert: product equals: expected.
178
+ ]
179
+
180
+ { #category : #' testing - multiplication' }
181
+ PMPolynomialTest >> testPolynomialMultiplicationIsCommutative [
182
+
183
+ | expected p q |
184
+ " p(x) = (x - 3) (x - 4), q(x) = x^3 + 1 therefore:
185
+
186
+ p(x) * q(x) = q(x) * p(x) = x^5 - 7 x^4 + 12 x^3 + x^2 - 7x + 12"
187
+ p := PMPolynomial coefficients: #( 12 -7 1 ) .
188
+ q := PMPolynomial coefficients: #( 1 0 0 1 ) .
189
+
190
+ expected := PMPolynomial coefficients: #( 12 -7 1 12 -7 1 ) .
191
+ self assert: p * q equals: expected.
192
+ self assert: q * p equals: expected
151
193
]
152
194
153
- { #category : #' function evaluation ' }
195
+ { #category : #' testing - addition ' }
154
196
PMPolynomialTest >> testPolynomialNumberAddition [
155
- | polynomial |
156
- polynomial := 2 + (PMPolynomial coefficients: #(2 -3 1) ).
157
- self assert: (polynomial at: 0 ) equals: 4 .
158
- self assert: (polynomial at: 1 ) equals: - 3 .
159
- self assert: (polynomial at: 2 ) equals: 1 .
197
+
198
+ | polynomial expected p |
199
+ p := PMPolynomial coefficients: #( 2 -3 1 ) .
200
+ polynomial := 2 + p.
201
+ expected := PMPolynomial coefficients: #( 4 -3 1 ) .
202
+ self assert: polynomial equals: expected.
160
203
self assert: (polynomial at: 3 ) equals: 0
161
204
]
162
205
163
- { #category : #' function evaluation ' }
206
+ { #category : #' testing - addition ' }
164
207
PMPolynomialTest >> testPolynomialNumberAdditionInverse [
165
- | polynomial |
166
- polynomial := (PMPolynomial coefficients: #(2 -3 1) ) + 2 .
167
- self assert: (polynomial at: 0 ) equals: 4 .
168
- self assert: (polynomial at: 1 ) equals: - 3 .
169
- self assert: (polynomial at: 2 ) equals: 1 .
208
+
209
+ | polynomial expected p |
210
+ p := PMPolynomial coefficients: #( 2 -3 1 ) .
211
+ polynomial := p + 2 .
212
+ expected := PMPolynomial coefficients: #( 4 -3 1 ) .
213
+ self assert: polynomial equals: expected.
170
214
self assert: (polynomial at: 3 ) equals: 0
171
215
]
172
216
173
- { #category : #' function evaluation ' }
217
+ { #category : #' testing - division ' }
174
218
PMPolynomialTest >> testPolynomialNumberDivision [
175
- | polynomial |
176
- polynomial := (PMPolynomial coefficients: #(2 -3 1) ) / 2 .
177
- self assert: (polynomial at: 0 ) equals: 1 .
178
- self assert: (polynomial at: 1 ) equals: - 3 / 2 .
179
- self assert: (polynomial at: 2 ) equals: 1 / 2 .
219
+
220
+ | polynomial expected expectedCoefficients p |
221
+ p := PMPolynomial coefficients: #( 2 -3 1 ) .
222
+ polynomial := p / 2 .
223
+ expectedCoefficients := Array with: 1 with: - 3 / 2 with: 1 / 2 .
224
+ expected := PMPolynomial coefficients: expectedCoefficients.
225
+ self assert: polynomial equals: expected.
180
226
self assert: (polynomial at: 3 ) equals: 0
181
227
]
182
228
183
- { #category : #' function evaluation ' }
229
+ { #category : #' testing - multiplication ' }
184
230
PMPolynomialTest >> testPolynomialNumberMultiplication [
185
- | polynomial |
186
- polynomial := 2 * (PMPolynomial coefficients: #(2 -3 1) ).
187
- self assert: (polynomial at: 0 ) equals: 4 .
188
- self assert: (polynomial at: 1 ) equals: - 6 .
189
- self assert: (polynomial at: 2 ) equals: 2 .
190
- self assert: (polynomial at: 3 ) equals: 0
231
+
232
+ | product expected p |
233
+ p := PMPolynomial coefficients: #( 2 -3 1 ) .
234
+ product := 2 * p.
235
+
236
+ expected := PMPolynomial coefficients: #( 4 -6 2 ) .
237
+ self assert: product equals: expected
191
238
]
192
239
193
- { #category : #' function evaluation ' }
240
+ { #category : #' testing - multiplication ' }
194
241
PMPolynomialTest >> testPolynomialNumberMultiplicationInverse [
195
- | polynomial |
196
- polynomial := (PMPolynomial coefficients: #(2 -3 1) ) * 2 .
197
- self assert: (polynomial at: 0 ) equals: 4 .
198
- self assert: (polynomial at: 1 ) equals: - 6 .
199
- self assert: (polynomial at: 2 ) equals: 2 .
200
- self assert: (polynomial at: 3 ) equals: 0
242
+
243
+ | product expected p |
244
+ p := PMPolynomial coefficients: #( 2 -3 1 ) .
245
+ product := p * 2 .
246
+
247
+ expected := PMPolynomial coefficients: #( 4 -6 2 ) .
248
+ self assert: product equals: expected
201
249
]
202
250
203
- { #category : #' function evaluation ' }
251
+ { #category : #' testing - subtraction ' }
204
252
PMPolynomialTest >> testPolynomialNumberSubtraction [
205
- | polynomial |
206
- polynomial := 2 - ( PMPolynomial coefficients: #(2 -3 1) ).
207
- self assert: (polynomial at: 0 ) equals: 0 .
208
- self assert: (polynomial at: 1 ) equals: 3 .
209
- self assert: ( polynomial at: 2 ) equals: - 1 .
253
+
254
+ | polynomial expected |
255
+ polynomial := 2 - ( PMPolynomial coefficients: #( 2 -3 1 ) ) .
256
+ expected := PMPolynomial coefficients: #( 0 3 -1 ) .
257
+ self assert: polynomial equals: expected .
210
258
self assert: (polynomial at: 3 ) equals: 0
211
259
]
212
260
213
- { #category : #' function evaluation ' }
261
+ { #category : #' testing - subtraction ' }
214
262
PMPolynomialTest >> testPolynomialNumberSubtractionInverse [
215
- | polynomial |
216
- polynomial := ( PMPolynomial coefficients: #(2 -3 1) ) - 2 .
217
- self assert: (polynomial at: 0 ) equals: 0 .
218
- self assert: (polynomial at: 1 ) equals: - 3 .
219
- self assert: ( polynomial at: 2 ) equals: 1 .
263
+
264
+ | polynomial expected |
265
+ polynomial := ( PMPolynomial coefficients: #( 2 -3 1 ) ) - 2 .
266
+ expected := PMPolynomial coefficients: #( 0 -3 1 ) .
267
+ self assert: polynomial equals: expected .
220
268
self assert: (polynomial at: 3 ) equals: 0
221
269
]
222
270
@@ -265,15 +313,15 @@ PMPolynomialTest >> testPolynomialRootsForLinear [
265
313
self assert: (roots at: 1 ) closeTo: - 0.5
266
314
]
267
315
268
- { #category : #' function evaluation ' }
316
+ { #category : #' testing - subtraction ' }
269
317
PMPolynomialTest >> testPolynomialSubtraction [
270
- | polynomial |
271
- polynomial := ( PMPolynomial coefficients: #(2 -3 1) )
272
- - ( PMPolynomial coefficients: #(-3 7 2 1) ).
273
- self assert: (polynomial at: 0 ) equals: 5 .
274
- self assert: ( polynomial at: 1 ) equals: - 10 .
275
- self assert: (polynomial at: 2 ) equals: - 1 .
276
- self assert: ( polynomial at: 3 ) equals: - 1 .
318
+
319
+ | polynomial p q expected |
320
+ p := PMPolynomial coefficients: #( 2 -3 1 ) .
321
+ q := PMPolynomial coefficients: #( -3 7 2 1 ) .
322
+ polynomial := p - q .
323
+ expected := PMPolynomial coefficients: #( 5 -10 -1 -1 ) .
324
+ self assert: polynomial equals: expected .
277
325
self assert: (polynomial at: 4 ) equals: 0
278
326
]
279
327
0 commit comments