Skip to content

Commit 11c8e01

Browse files
authored
Merge pull request #323 from PolyMathOrg/refactor-polynomials
Refactor polynomials
2 parents 761fb9a + 85c2398 commit 11c8e01

File tree

3 files changed

+163
-111
lines changed

3 files changed

+163
-111
lines changed

src/Math-Numerical/PMNewtonZeroFinder.class.st

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ PMNewtonZeroFinder >> initialize [
114114
^ self
115115
]
116116

117+
{ #category : #operation }
118+
PMNewtonZeroFinder >> rootOf: function [
119+
self setFunction: function;
120+
setDerivative: function derivative.
121+
^ self evaluate.
122+
]
123+
117124
{ #category : #initialization }
118125
PMNewtonZeroFinder >> setDerivative: aBlock [
119126
"Defines the derivative of the function for which zeroes will be found.

src/Math-Polynomials/PMPolynomial.class.st

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,20 +239,17 @@ PMPolynomial >> roots [
239239
{ #category : #information }
240240
PMPolynomial >> roots: aNumber [
241241

242-
| pol roots root rootFinder |
242+
| polynomial roots root rootFinder |
243243
rootFinder := PMNewtonZeroFinder with: aNumber.
244-
pol := self class coefficients:
245-
(coefficients reverse collect: [ :each | each asFloat ]).
244+
polynomial := self class coefficients:
245+
(coefficients reverse collect: [ :each | each asFloat ]).
246246
roots := OrderedCollection new: self degree.
247247
[
248-
rootFinder
249-
setFunction: pol;
250-
setDerivative: pol derivative.
251-
root := rootFinder evaluate.
248+
root := rootFinder rootOf: polynomial.
252249
rootFinder hasConverged ] whileTrue: [
253250
roots add: root.
254-
pol := pol deflatedAt: root.
255-
pol degree > 0 ifFalse: [ ^ roots ] ].
251+
polynomial := polynomial deflatedAt: root.
252+
polynomial degree strictlyPositive ifFalse: [ ^ roots ] ].
256253
^ roots
257254
]
258255

src/Math-Tests-Polynomials/PMPolynomialTest.class.st

Lines changed: 150 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Class {
44
#category : #'Math-Tests-Polynomials'
55
}
66

7-
{ #category : #comparing }
7+
{ #category : #'testing - comparing' }
88
PMPolynomialTest >> testIsZero [
99
| p1 p2 |
1010
p1 := PMPolynomial coefficients: #(0 0 0 0 0).
@@ -13,32 +13,50 @@ PMPolynomialTest >> testIsZero [
1313
self shouldnt: [ p2 isZero ]
1414
]
1515

16-
{ #category : #'function evaluation' }
16+
{ #category : #'testing - addition' }
1717
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.
2525
self assert: (polynomial at: 4) equals: 0
2626
]
2727

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' }
2941
PMPolynomialTest >> testPolynomialDerivative [
3042
"Code example 2.3"
43+
"
44+
p(x) = x^3 + 2x^2 + 7x - 3, therefore:
45+
p'(x) = 3x^2 + 4x + 7
46+
"
3147

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
3957
]
4058

41-
{ #category : #'function evaluation' }
59+
{ #category : #'testing - division' }
4260
PMPolynomialTest >> testPolynomialDivision [
4361
| pol1 pol2 polynomial |
4462
pol1 := PMPolynomial coefficients: #(2 -3 1).
@@ -53,7 +71,7 @@ PMPolynomialTest >> testPolynomialDivision [
5371
self assert: (polynomial at: 6) equals: 0
5472
]
5573

56-
{ #category : #'function evaluation' }
74+
{ #category : #'testing - division' }
5775
PMPolynomialTest >> testPolynomialDivisionBug [
5876
"identify an error when trying to create a zero dividend"
5977

@@ -63,7 +81,7 @@ PMPolynomialTest >> testPolynomialDivisionBug [
6381
self shouldnt: [ pol1 / pol2 ] raise: Error
6482
]
6583

66-
{ #category : #arithmetic }
84+
{ #category : #'testing - arithmetic' }
6785
PMPolynomialTest >> testPolynomialDoubleDispatch [
6886
| n p |
6987
n := 3.2.
@@ -80,7 +98,7 @@ PMPolynomialTest >> testPolynomialDoubleDispatch [
8098
self assert: n - p equals: (p - n) negated
8199
]
82100

83-
{ #category : #'function evaluation' }
101+
{ #category : #'testing - algebra' }
84102
PMPolynomialTest >> testPolynomialEvaluation [
85103
"Code example 2.2"
86104

@@ -89,7 +107,7 @@ PMPolynomialTest >> testPolynomialEvaluation [
89107
self assert: 0 equals: (polynomial value: 1)
90108
]
91109

92-
{ #category : #comparing }
110+
{ #category : #'testing - comparing' }
93111
PMPolynomialTest >> testPolynomialHash [
94112
"polynomial hash is hash of coefficient array"
95113

@@ -105,118 +123,148 @@ PMPolynomialTest >> testPolynomialHash [
105123
self assert: p3 hash equals: p2 hash
106124
]
107125

108-
{ #category : #'function evaluation' }
126+
{ #category : #'testing - algebra' }
109127
PMPolynomialTest >> testPolynomialIntegral [
110128
"Code example 2.3"
111129

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 .
119146
self assert: (polynomial at: 5) equals: 0
120147
]
121148

122-
{ #category : #'function evaluation' }
149+
{ #category : #'testing - algebra' }
123150
PMPolynomialTest >> testPolynomialIntegralWithConstant [
124151
"Code example 2.3"
125152

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.
133165
self assert: (polynomial at: 5) equals: 0
134166
]
135167

136-
{ #category : #'function evaluation' }
168+
{ #category : #'testing - multiplication' }
137169
PMPolynomialTest >> testPolynomialMultiplication [
138170
"Code example 2.3"
139171

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
151193
]
152194

153-
{ #category : #'function evaluation' }
195+
{ #category : #'testing - addition' }
154196
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.
160203
self assert: (polynomial at: 3) equals: 0
161204
]
162205

163-
{ #category : #'function evaluation' }
206+
{ #category : #'testing - addition' }
164207
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.
170214
self assert: (polynomial at: 3) equals: 0
171215
]
172216

173-
{ #category : #'function evaluation' }
217+
{ #category : #'testing - division' }
174218
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.
180226
self assert: (polynomial at: 3) equals: 0
181227
]
182228

183-
{ #category : #'function evaluation' }
229+
{ #category : #'testing - multiplication' }
184230
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
191238
]
192239

193-
{ #category : #'function evaluation' }
240+
{ #category : #'testing - multiplication' }
194241
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
201249
]
202250

203-
{ #category : #'function evaluation' }
251+
{ #category : #'testing - subtraction' }
204252
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.
210258
self assert: (polynomial at: 3) equals: 0
211259
]
212260

213-
{ #category : #'function evaluation' }
261+
{ #category : #'testing - subtraction' }
214262
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.
220268
self assert: (polynomial at: 3) equals: 0
221269
]
222270

@@ -265,15 +313,15 @@ PMPolynomialTest >> testPolynomialRootsForLinear [
265313
self assert: (roots at: 1) closeTo: -0.5
266314
]
267315

268-
{ #category : #'function evaluation' }
316+
{ #category : #'testing - subtraction' }
269317
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.
277325
self assert: (polynomial at: 4) equals: 0
278326
]
279327

0 commit comments

Comments
 (0)