Skip to content

Commit b8434f0

Browse files
refactor: Extract Method where a method instantiates a root finding algorithm with the desired precision.
1 parent 1487a64 commit b8434f0

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/Math-Polynomials/PMPolynomial.class.st

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,15 @@ PMPolynomial >> reciprocal [
239239
^ (PMPolynomial coefficients: #(1)) / self
240240
]
241241

242+
{ #category : #information }
243+
PMPolynomial >> rootFindingAlgorithmWith: precision [
244+
245+
| rootFinder |
246+
rootFinder := PMNewtonZeroFinder new.
247+
rootFinder desiredPrecision: precision.
248+
^ rootFinder
249+
]
250+
242251
{ #category : #information }
243252
PMPolynomial >> roots [
244253

@@ -249,19 +258,20 @@ PMPolynomial >> roots [
249258
PMPolynomial >> roots: aNumber [
250259

251260
| pol roots x rootFinder |
252-
rootFinder := PMNewtonZeroFinder new.
253-
rootFinder desiredPrecision: aNumber.
254-
pol := self class coefficients: ( coefficients reverse collect: [ :each | each asFloat]).
261+
rootFinder := self rootFindingAlgorithmWith: aNumber.
262+
pol := self class coefficients:
263+
(coefficients reverse collect: [ :each | each asFloat ]).
255264
roots := OrderedCollection new: self degree.
256-
[ rootFinder setFunction: pol; setDerivative: pol derivative.
257-
x := rootFinder evaluate.
258-
rootFinder hasConverged
259-
] whileTrue: [ roots add: x.
260-
pol := pol deflatedAt: x.
261-
pol degree > 0
262-
ifFalse: [ ^roots].
263-
].
264-
^roots
265+
[
266+
rootFinder
267+
setFunction: pol;
268+
setDerivative: pol derivative.
269+
x := rootFinder evaluate.
270+
rootFinder hasConverged ] whileTrue: [
271+
roots add: x.
272+
pol := pol deflatedAt: x.
273+
pol degree > 0 ifFalse: [ ^ roots ] ].
274+
^ roots
265275
]
266276

267277
{ #category : #'double dispatching' }

0 commit comments

Comments
 (0)