Skip to content

Commit 761fb9a

Browse files
authored
Merge pull request #322 from PolyMathOrg/refactor-polynomials-tests
Refactor Polynomials: Extract Method to Introduce RootFinder Factory Method and Tighten Encapsulation
2 parents d9a495e + 6de9e74 commit 761fb9a

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

src/Math-Numerical/PMNewtonZeroFinder.class.st

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ PMNewtonZeroFinder class >> function: aBlock1 derivative: aBlock2 [
3939
^(self new) setFunction: aBlock1; setDerivative: aBlock2; yourself
4040
]
4141

42+
{ #category : #information }
43+
PMNewtonZeroFinder class >> with: precision [
44+
45+
| rootFinder |
46+
rootFinder := self new.
47+
rootFinder desiredPrecision: precision.
48+
^ rootFinder
49+
]
50+
4251
{ #category : #operation }
4352
PMNewtonZeroFinder >> computeInitialValues [
4453
"Private - If no derivative has been defined, take an ad-hoc definition.

src/Math-Polynomials/PMPolynomial.class.st

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -233,27 +233,27 @@ PMPolynomial >> reciprocal [
233233

234234
{ #category : #information }
235235
PMPolynomial >> roots [
236-
237-
^ self roots: Float defaultComparisonPrecision
236+
^ (self roots: Float defaultComparisonPrecision) asSortedCollection asArray
238237
]
239238

240239
{ #category : #information }
241240
PMPolynomial >> roots: aNumber [
242241

243-
| pol roots x rootFinder |
244-
rootFinder := PMNewtonZeroFinder new.
245-
rootFinder desiredPrecision: aNumber.
246-
pol := self class coefficients: ( coefficients reverse collect: [ :each | each asFloat]).
242+
| pol roots root rootFinder |
243+
rootFinder := PMNewtonZeroFinder with: aNumber.
244+
pol := self class coefficients:
245+
(coefficients reverse collect: [ :each | each asFloat ]).
247246
roots := OrderedCollection new: self degree.
248-
[ rootFinder setFunction: pol; setDerivative: pol derivative.
249-
x := rootFinder evaluate.
250-
rootFinder hasConverged
251-
] whileTrue: [ roots add: x.
252-
pol := pol deflatedAt: x.
253-
pol degree > 0
254-
ifFalse: [ ^roots].
255-
].
256-
^roots
247+
[
248+
rootFinder
249+
setFunction: pol;
250+
setDerivative: pol derivative.
251+
root := rootFinder evaluate.
252+
rootFinder hasConverged ] whileTrue: [
253+
roots add: root.
254+
pol := pol deflatedAt: root.
255+
pol degree > 0 ifFalse: [ ^ roots ] ].
256+
^ roots
257257
]
258258

259259
{ #category : #'double dispatching' }

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,36 @@ PMPolynomialTest >> testPolynomialRoots [
235235

236236
| polynomial roots |
237237
polynomial := PMPolynomial coefficients: #( -10 -13 -2 1 ).
238-
roots := polynomial roots asSortedCollection asArray.
238+
roots := polynomial roots .
239239
self assert: roots size equals: 3.
240240
self assert: (roots at: 1) + 2 closeTo: 0.
241241
self assert: (roots at: 2) + 1 closeTo: 0.
242242
self assert: (roots at: 3) - 5 closeTo: 0
243243
]
244244

245+
{ #category : #'iterative algorithms' }
246+
PMPolynomialTest >> testPolynomialRootsConstantsHaveNoRoots [
247+
248+
| constant |
249+
"Here, compute the roots of the constant C = 1"
250+
constant := PMPolynomial coefficients: #( 1 ).
251+
self
252+
should: [ constant roots ]
253+
raise: Error
254+
description: 'Function''s derivative seems to be zero everywhere'
255+
]
256+
257+
{ #category : #'iterative algorithms' }
258+
PMPolynomialTest >> testPolynomialRootsForLinear [
259+
260+
| linearPolynomial roots |
261+
"Here, compute the roots of the linear (2x + 1)"
262+
linearPolynomial := PMPolynomial coefficients: #( 1 2 ).
263+
roots := linearPolynomial roots.
264+
self assert: roots size equals: 1.
265+
self assert: (roots at: 1) closeTo: -0.5
266+
]
267+
245268
{ #category : #'function evaluation' }
246269
PMPolynomialTest >> testPolynomialSubtraction [
247270
| polynomial |
@@ -253,3 +276,14 @@ PMPolynomialTest >> testPolynomialSubtraction [
253276
self assert: (polynomial at: 3) equals: -1.
254277
self assert: (polynomial at: 4) equals: 0
255278
]
279+
280+
{ #category : #'iterative algorithms' }
281+
PMPolynomialTest >> testPolynomialWithRepeatedRoots [
282+
| polynomialWithRepeatedRoots roots |
283+
"Here, compute the roots of the quadratic (2x + 1)^2 = 4 x^2 + 4 x + 1"
284+
polynomialWithRepeatedRoots := PMPolynomial coefficients: #(1 4 4).
285+
roots := polynomialWithRepeatedRoots roots .
286+
self assert: roots size equals: 2.
287+
self assert: (roots at: 1) closeTo: -0.5 .
288+
self assert: (roots at: 2) closeTo: -0.5 .
289+
]

0 commit comments

Comments
 (0)