From 9709c544c2934e317a94f95bfbe51cdda0debffc Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 16:52:15 +0000 Subject: [PATCH 01/20] refactor: using Parallel Change, introduced a new message that returns roots in order. --- src/Math-Polynomials/PMPolynomial.class.st | 8 ++++++++ src/Math-Tests-Polynomials/PMPolynomialTest.class.st | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Math-Polynomials/PMPolynomial.class.st b/src/Math-Polynomials/PMPolynomial.class.st index d77820df..8a9c7ca1 100644 --- a/src/Math-Polynomials/PMPolynomial.class.st +++ b/src/Math-Polynomials/PMPolynomial.class.st @@ -193,6 +193,14 @@ PMPolynomial >> negated [ ^ self * -1 ] +{ #category : #information } +PMPolynomial >> orderedRoots [ + "comment stating purpose of instance-side method" + "scope: class-variables & instance-variables" + + ^ self roots asSortedCollection asArray +] + { #category : #printing } PMPolynomial >> printOn: aStream [ "Append to aStream a written representation of the receiver." diff --git a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st index 3b5a2fc9..919518b4 100644 --- a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st +++ b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st @@ -235,7 +235,7 @@ PMPolynomialTest >> testPolynomialRoots [ | polynomial roots | polynomial := PMPolynomial coefficients: #( -10 -13 -2 1 ). - roots := polynomial roots asSortedCollection asArray. + roots := polynomial orderedRoots . self assert: roots size equals: 3. self assert: (roots at: 1) + 2 closeTo: 0. self assert: (roots at: 2) + 1 closeTo: 0. From 1487a6482ee7cd59d65d1841cc2eaa425e8a0550 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 16:54:19 +0000 Subject: [PATCH 02/20] refactor: migrated the client to no longer use roots message. --- src/Math-Polynomials/PMPolynomial.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Math-Polynomials/PMPolynomial.class.st b/src/Math-Polynomials/PMPolynomial.class.st index 8a9c7ca1..80d97101 100644 --- a/src/Math-Polynomials/PMPolynomial.class.st +++ b/src/Math-Polynomials/PMPolynomial.class.st @@ -198,7 +198,7 @@ PMPolynomial >> orderedRoots [ "comment stating purpose of instance-side method" "scope: class-variables & instance-variables" - ^ self roots asSortedCollection asArray + ^ (self roots: Float defaultComparisonPrecision) asSortedCollection asArray ] { #category : #printing } From b8434f0a086720777785e17a903c54a3c840e2bb Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 17:02:55 +0000 Subject: [PATCH 03/20] refactor: Extract Method where a method instantiates a root finding algorithm with the desired precision. --- src/Math-Polynomials/PMPolynomial.class.st | 34 ++++++++++++++-------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/Math-Polynomials/PMPolynomial.class.st b/src/Math-Polynomials/PMPolynomial.class.st index 80d97101..edc4454a 100644 --- a/src/Math-Polynomials/PMPolynomial.class.st +++ b/src/Math-Polynomials/PMPolynomial.class.st @@ -239,6 +239,15 @@ PMPolynomial >> reciprocal [ ^ (PMPolynomial coefficients: #(1)) / self ] +{ #category : #information } +PMPolynomial >> rootFindingAlgorithmWith: precision [ + + | rootFinder | + rootFinder := PMNewtonZeroFinder new. + rootFinder desiredPrecision: precision. + ^ rootFinder +] + { #category : #information } PMPolynomial >> roots [ @@ -249,19 +258,20 @@ PMPolynomial >> roots [ PMPolynomial >> roots: aNumber [ | pol roots x rootFinder | - rootFinder := PMNewtonZeroFinder new. - rootFinder desiredPrecision: aNumber. - pol := self class coefficients: ( coefficients reverse collect: [ :each | each asFloat]). + rootFinder := self rootFindingAlgorithmWith: aNumber. + pol := self class coefficients: + (coefficients reverse collect: [ :each | each asFloat ]). roots := OrderedCollection new: self degree. - [ rootFinder setFunction: pol; setDerivative: pol derivative. - x := rootFinder evaluate. - rootFinder hasConverged - ] whileTrue: [ roots add: x. - pol := pol deflatedAt: x. - pol degree > 0 - ifFalse: [ ^roots]. - ]. - ^roots + [ + rootFinder + setFunction: pol; + setDerivative: pol derivative. + x := rootFinder evaluate. + rootFinder hasConverged ] whileTrue: [ + roots add: x. + pol := pol deflatedAt: x. + pol degree > 0 ifFalse: [ ^ roots ] ]. + ^ roots ] { #category : #'double dispatching' } From eee00a7044453ab321f0e6c4c1f04ee08ce2a6c2 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 17:04:17 +0000 Subject: [PATCH 04/20] refactor: made the method a class method. Next we can inline the instance method. --- src/Math-Polynomials/PMPolynomial.class.st | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Math-Polynomials/PMPolynomial.class.st b/src/Math-Polynomials/PMPolynomial.class.st index edc4454a..9ab2600e 100644 --- a/src/Math-Polynomials/PMPolynomial.class.st +++ b/src/Math-Polynomials/PMPolynomial.class.st @@ -26,6 +26,15 @@ PMPolynomial class >> coefficients: anArray [ ^self new initialize: anArray reverse ] +{ #category : #information } +PMPolynomial class >> rootFindingAlgorithmWith: precision [ + + | rootFinder | + rootFinder := PMNewtonZeroFinder new. + rootFinder desiredPrecision: precision. + ^ rootFinder +] + { #category : #operation } PMPolynomial >> * aNumberOrPolynomial [ @@ -240,12 +249,8 @@ PMPolynomial >> reciprocal [ ] { #category : #information } -PMPolynomial >> rootFindingAlgorithmWith: precision [ - - | rootFinder | - rootFinder := PMNewtonZeroFinder new. - rootFinder desiredPrecision: precision. - ^ rootFinder +PMPolynomial >> rootFindingAlgorithmWith: precision [ + ^ self class rootFindingAlgorithmWith: precision ] { #category : #information } From ea7419ebf3ea8cc554a93d50949890cda5f11312 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 17:11:52 +0000 Subject: [PATCH 05/20] refactor: Move Method to a more sensible place. --- src/Math-Numerical/PMNewtonZeroFinder.class.st | 9 +++++++++ src/Math-Polynomials/PMPolynomial.class.st | 11 +---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Math-Numerical/PMNewtonZeroFinder.class.st b/src/Math-Numerical/PMNewtonZeroFinder.class.st index a253bdd3..5b9ab5bb 100644 --- a/src/Math-Numerical/PMNewtonZeroFinder.class.st +++ b/src/Math-Numerical/PMNewtonZeroFinder.class.st @@ -39,6 +39,15 @@ PMNewtonZeroFinder class >> function: aBlock1 derivative: aBlock2 [ ^(self new) setFunction: aBlock1; setDerivative: aBlock2; yourself ] +{ #category : #information } +PMNewtonZeroFinder class >> rootFindingAlgorithmWith: precision [ + + | rootFinder | + rootFinder := PMNewtonZeroFinder new. + rootFinder desiredPrecision: precision. + ^ rootFinder +] + { #category : #operation } PMNewtonZeroFinder >> computeInitialValues [ "Private - If no derivative has been defined, take an ad-hoc definition. diff --git a/src/Math-Polynomials/PMPolynomial.class.st b/src/Math-Polynomials/PMPolynomial.class.st index 9ab2600e..32a68ef2 100644 --- a/src/Math-Polynomials/PMPolynomial.class.st +++ b/src/Math-Polynomials/PMPolynomial.class.st @@ -26,15 +26,6 @@ PMPolynomial class >> coefficients: anArray [ ^self new initialize: anArray reverse ] -{ #category : #information } -PMPolynomial class >> rootFindingAlgorithmWith: precision [ - - | rootFinder | - rootFinder := PMNewtonZeroFinder new. - rootFinder desiredPrecision: precision. - ^ rootFinder -] - { #category : #operation } PMPolynomial >> * aNumberOrPolynomial [ @@ -250,7 +241,7 @@ PMPolynomial >> reciprocal [ { #category : #information } PMPolynomial >> rootFindingAlgorithmWith: precision [ - ^ self class rootFindingAlgorithmWith: precision + ^ PMNewtonZeroFinder rootFindingAlgorithmWith: precision ] { #category : #information } From 32ebd8af4962bb4eac9f5652ad775825572c284f Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 17:13:14 +0000 Subject: [PATCH 06/20] refactor: Inline Method, as it has no clients. --- src/Math-Polynomials/PMPolynomial.class.st | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Math-Polynomials/PMPolynomial.class.st b/src/Math-Polynomials/PMPolynomial.class.st index 32a68ef2..1c5645bc 100644 --- a/src/Math-Polynomials/PMPolynomial.class.st +++ b/src/Math-Polynomials/PMPolynomial.class.st @@ -239,11 +239,6 @@ PMPolynomial >> reciprocal [ ^ (PMPolynomial coefficients: #(1)) / self ] -{ #category : #information } -PMPolynomial >> rootFindingAlgorithmWith: precision [ - ^ PMNewtonZeroFinder rootFindingAlgorithmWith: precision -] - { #category : #information } PMPolynomial >> roots [ @@ -254,7 +249,7 @@ PMPolynomial >> roots [ PMPolynomial >> roots: aNumber [ | pol roots x rootFinder | - rootFinder := self rootFindingAlgorithmWith: aNumber. + rootFinder := PMNewtonZeroFinder rootFindingAlgorithmWith: aNumber. pol := self class coefficients: (coefficients reverse collect: [ :each | each asFloat ]). roots := OrderedCollection new: self degree. From 218a2e517e86664fe6860332517a6730b7031a29 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 17:15:23 +0000 Subject: [PATCH 07/20] refactor: clarified the name of the method. --- src/Math-Numerical/PMNewtonZeroFinder.class.st | 2 +- src/Math-Polynomials/PMPolynomial.class.st | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Math-Numerical/PMNewtonZeroFinder.class.st b/src/Math-Numerical/PMNewtonZeroFinder.class.st index 5b9ab5bb..dadcd971 100644 --- a/src/Math-Numerical/PMNewtonZeroFinder.class.st +++ b/src/Math-Numerical/PMNewtonZeroFinder.class.st @@ -40,7 +40,7 @@ PMNewtonZeroFinder class >> function: aBlock1 derivative: aBlock2 [ ] { #category : #information } -PMNewtonZeroFinder class >> rootFindingAlgorithmWith: precision [ +PMNewtonZeroFinder class >> with: precision [ | rootFinder | rootFinder := PMNewtonZeroFinder new. diff --git a/src/Math-Polynomials/PMPolynomial.class.st b/src/Math-Polynomials/PMPolynomial.class.st index 1c5645bc..14d5e7bd 100644 --- a/src/Math-Polynomials/PMPolynomial.class.st +++ b/src/Math-Polynomials/PMPolynomial.class.st @@ -249,7 +249,7 @@ PMPolynomial >> roots [ PMPolynomial >> roots: aNumber [ | pol roots x rootFinder | - rootFinder := PMNewtonZeroFinder rootFindingAlgorithmWith: aNumber. + rootFinder := PMNewtonZeroFinder with: aNumber. pol := self class coefficients: (coefficients reverse collect: [ :each | each asFloat ]). roots := OrderedCollection new: self degree. From 91e14e30b2a46da0b07d5fc3531f9425f0d462cb Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 17:16:45 +0000 Subject: [PATCH 08/20] style: corrected a Smalltalk code critique. --- src/Math-Numerical/PMNewtonZeroFinder.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Math-Numerical/PMNewtonZeroFinder.class.st b/src/Math-Numerical/PMNewtonZeroFinder.class.st index dadcd971..9f5f2303 100644 --- a/src/Math-Numerical/PMNewtonZeroFinder.class.st +++ b/src/Math-Numerical/PMNewtonZeroFinder.class.st @@ -43,7 +43,7 @@ PMNewtonZeroFinder class >> function: aBlock1 derivative: aBlock2 [ PMNewtonZeroFinder class >> with: precision [ | rootFinder | - rootFinder := PMNewtonZeroFinder new. + rootFinder := self new. rootFinder desiredPrecision: precision. ^ rootFinder ] From 251c5b671a34364ce0063fe2ab96bfb3962b7f3a Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 18:02:39 +0000 Subject: [PATCH 09/20] refactor: Inline Method as it has no clients. --- src/Math-Polynomials/PMPolynomial.class.st | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Math-Polynomials/PMPolynomial.class.st b/src/Math-Polynomials/PMPolynomial.class.st index 14d5e7bd..5ced4cc4 100644 --- a/src/Math-Polynomials/PMPolynomial.class.st +++ b/src/Math-Polynomials/PMPolynomial.class.st @@ -239,12 +239,6 @@ PMPolynomial >> reciprocal [ ^ (PMPolynomial coefficients: #(1)) / self ] -{ #category : #information } -PMPolynomial >> roots [ - - ^ self roots: Float defaultComparisonPrecision -] - { #category : #information } PMPolynomial >> roots: aNumber [ From 8dbe9174e313e0069412ccca5f9c88e32812e99a Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 18:19:56 +0000 Subject: [PATCH 10/20] refactor: Rename Method, and now we have the interface that a client (in this case the test) needs. --- src/Math-Polynomials/PMPolynomial.class.st | 16 ++++++++-------- .../PMPolynomialTest.class.st | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Math-Polynomials/PMPolynomial.class.st b/src/Math-Polynomials/PMPolynomial.class.st index 5ced4cc4..66f85007 100644 --- a/src/Math-Polynomials/PMPolynomial.class.st +++ b/src/Math-Polynomials/PMPolynomial.class.st @@ -193,14 +193,6 @@ PMPolynomial >> negated [ ^ self * -1 ] -{ #category : #information } -PMPolynomial >> orderedRoots [ - "comment stating purpose of instance-side method" - "scope: class-variables & instance-variables" - - ^ (self roots: Float defaultComparisonPrecision) asSortedCollection asArray -] - { #category : #printing } PMPolynomial >> printOn: aStream [ "Append to aStream a written representation of the receiver." @@ -239,6 +231,14 @@ PMPolynomial >> reciprocal [ ^ (PMPolynomial coefficients: #(1)) / self ] +{ #category : #information } +PMPolynomial >> roots [ + "comment stating purpose of instance-side method" + "scope: class-variables & instance-variables" + + ^ (self roots: Float defaultComparisonPrecision) asSortedCollection asArray +] + { #category : #information } PMPolynomial >> roots: aNumber [ diff --git a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st index 919518b4..5d9bfd11 100644 --- a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st +++ b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st @@ -235,7 +235,7 @@ PMPolynomialTest >> testPolynomialRoots [ | polynomial roots | polynomial := PMPolynomial coefficients: #( -10 -13 -2 1 ). - roots := polynomial orderedRoots . + roots := polynomial roots . self assert: roots size equals: 3. self assert: (roots at: 1) + 2 closeTo: 0. self assert: (roots at: 2) + 1 closeTo: 0. From 6ebf707337413e3f74713ab7df66147b4f7ad919 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 18:27:08 +0000 Subject: [PATCH 11/20] style: removed redundant comment. --- src/Math-Polynomials/PMPolynomial.class.st | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Math-Polynomials/PMPolynomial.class.st b/src/Math-Polynomials/PMPolynomial.class.st index 66f85007..a49a32e0 100644 --- a/src/Math-Polynomials/PMPolynomial.class.st +++ b/src/Math-Polynomials/PMPolynomial.class.st @@ -233,10 +233,7 @@ PMPolynomial >> reciprocal [ { #category : #information } PMPolynomial >> roots [ - "comment stating purpose of instance-side method" - "scope: class-variables & instance-variables" - - ^ (self roots: Float defaultComparisonPrecision) asSortedCollection asArray + ^ (self roots: Float defaultComparisonPrecision) asSortedCollection asArray ] { #category : #information } From a585d96c8f22fdc0dd6fd9853a0b5e4462755799 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 18:37:58 +0000 Subject: [PATCH 12/20] refactor: Rename Variable - it looks like it is a root. --- src/Math-Polynomials/PMPolynomial.class.st | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Math-Polynomials/PMPolynomial.class.st b/src/Math-Polynomials/PMPolynomial.class.st index a49a32e0..d941313e 100644 --- a/src/Math-Polynomials/PMPolynomial.class.st +++ b/src/Math-Polynomials/PMPolynomial.class.st @@ -239,7 +239,7 @@ PMPolynomial >> roots [ { #category : #information } PMPolynomial >> roots: aNumber [ - | pol roots x rootFinder | + | pol roots root rootFinder | rootFinder := PMNewtonZeroFinder with: aNumber. pol := self class coefficients: (coefficients reverse collect: [ :each | each asFloat ]). @@ -248,10 +248,10 @@ PMPolynomial >> roots: aNumber [ rootFinder setFunction: pol; setDerivative: pol derivative. - x := rootFinder evaluate. + root := rootFinder evaluate. rootFinder hasConverged ] whileTrue: [ - roots add: x. - pol := pol deflatedAt: x. + roots add: root. + pol := pol deflatedAt: root. pol degree > 0 ifFalse: [ ^ roots ] ]. ^ roots ] From 22005cbfae5ebba4677ecf79ac090208aacf3338 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 19:06:13 +0000 Subject: [PATCH 13/20] test: added a simple test for x^2 + 2x + 1, whose roots are x = -1 and -1 --- src/Math-Tests-Polynomials/PMPolynomialTest.class.st | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st index 5d9bfd11..56b30e00 100644 --- a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st +++ b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st @@ -242,6 +242,16 @@ PMPolynomialTest >> testPolynomialRoots [ self assert: (roots at: 3) - 5 closeTo: 0 ] +{ #category : #'iterative algorithms' } +PMPolynomialTest >> testPolynomialRootsForQuadratic [ + | polynomial roots | + polynomial := PMPolynomial coefficients: #(1 2 1). + roots := polynomial roots . + self assert: roots size equals: 2. + self assert: (roots at: 1) closeTo: -1 . + self assert: (roots at: 2) closeTo: -1 . +] + { #category : #'function evaluation' } PMPolynomialTest >> testPolynomialSubtraction [ | polynomial | From 3dfdc8e5f3483817dd943a8f9f7f876fee1347ee Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 19:10:21 +0000 Subject: [PATCH 14/20] test: changed the example and added a comment to make it clear what the coefficients correspond to. --- src/Math-Tests-Polynomials/PMPolynomialTest.class.st | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st index 56b30e00..edef0d21 100644 --- a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st +++ b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st @@ -245,11 +245,12 @@ PMPolynomialTest >> testPolynomialRoots [ { #category : #'iterative algorithms' } PMPolynomialTest >> testPolynomialRootsForQuadratic [ | polynomial roots | - polynomial := PMPolynomial coefficients: #(1 2 1). + "Here, compute the roots of the quadratic (2x + 1)^2 = 4 x^2 + 4 x + 1" + polynomial := PMPolynomial coefficients: #(1 4 4). roots := polynomial roots . self assert: roots size equals: 2. - self assert: (roots at: 1) closeTo: -1 . - self assert: (roots at: 2) closeTo: -1 . + self assert: (roots at: 1) closeTo: -0.5 . + self assert: (roots at: 2) closeTo: -0.5 . ] { #category : #'function evaluation' } From 371e9c8ca4b65740e865af48f17b8f44e8c10157 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 19:26:33 +0000 Subject: [PATCH 15/20] test: added some interesting and missing tests. --- .../PMPolynomialTest.class.st | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st index edef0d21..b40144b2 100644 --- a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st +++ b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st @@ -242,6 +242,25 @@ PMPolynomialTest >> testPolynomialRoots [ self assert: (roots at: 3) - 5 closeTo: 0 ] +{ #category : #'iterative algorithms' } +PMPolynomialTest >> testPolynomialRootsForConstant [ + | polynomial | + "Here, compute the roots of the quadratic (2x + 1)" + polynomial := PMPolynomial coefficients: #(1). + self should: [ polynomial roots ] raise: Error description: 'Function''s derivative seems to be zero everywhere'. + +] + +{ #category : #'iterative algorithms' } +PMPolynomialTest >> testPolynomialRootsForLinear [ + | polynomial roots | + "Here, compute the roots of the quadratic (2x + 1)" + polynomial := PMPolynomial coefficients: #(1 2). + roots := polynomial roots . + self assert: roots size equals: 1. + self assert: (roots at: 1) closeTo: -0.5 . +] + { #category : #'iterative algorithms' } PMPolynomialTest >> testPolynomialRootsForQuadratic [ | polynomial roots | From 22e219cba0574026f4ec55ff6d8350795f60ac26 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 19:27:40 +0000 Subject: [PATCH 16/20] fix: corrected the comment. --- src/Math-Tests-Polynomials/PMPolynomialTest.class.st | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st index b40144b2..6b9157af 100644 --- a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st +++ b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st @@ -244,11 +244,14 @@ PMPolynomialTest >> testPolynomialRoots [ { #category : #'iterative algorithms' } PMPolynomialTest >> testPolynomialRootsForConstant [ + | polynomial | - "Here, compute the roots of the quadratic (2x + 1)" - polynomial := PMPolynomial coefficients: #(1). - self should: [ polynomial roots ] raise: Error description: 'Function''s derivative seems to be zero everywhere'. - + "Here, compute the roots of the constant C = 1" + polynomial := PMPolynomial coefficients: #( 1 ). + self + should: [ polynomial roots ] + raise: Error + description: 'Function''s derivative seems to be zero everywhere' ] { #category : #'iterative algorithms' } From 25ba113c4712ad29d74e9dd8ca74339dfb4a7829 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 19:36:36 +0000 Subject: [PATCH 17/20] test: clarified naming using domain language (repeated roots). --- .../PMPolynomialTest.class.st | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st index 6b9157af..1b83ae41 100644 --- a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st +++ b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st @@ -264,17 +264,6 @@ PMPolynomialTest >> testPolynomialRootsForLinear [ self assert: (roots at: 1) closeTo: -0.5 . ] -{ #category : #'iterative algorithms' } -PMPolynomialTest >> testPolynomialRootsForQuadratic [ - | polynomial roots | - "Here, compute the roots of the quadratic (2x + 1)^2 = 4 x^2 + 4 x + 1" - polynomial := PMPolynomial coefficients: #(1 4 4). - roots := polynomial roots . - self assert: roots size equals: 2. - self assert: (roots at: 1) closeTo: -0.5 . - self assert: (roots at: 2) closeTo: -0.5 . -] - { #category : #'function evaluation' } PMPolynomialTest >> testPolynomialSubtraction [ | polynomial | @@ -286,3 +275,14 @@ PMPolynomialTest >> testPolynomialSubtraction [ self assert: (polynomial at: 3) equals: -1. self assert: (polynomial at: 4) equals: 0 ] + +{ #category : #'iterative algorithms' } +PMPolynomialTest >> testPolynomialWithRepeatedRoots [ + | polynomial roots | + "Here, compute the roots of the quadratic (2x + 1)^2 = 4 x^2 + 4 x + 1" + polynomial := PMPolynomial coefficients: #(1 4 4). + roots := polynomial roots . + self assert: roots size equals: 2. + self assert: (roots at: 1) closeTo: -0.5 . + self assert: (roots at: 2) closeTo: -0.5 . +] From fc66d1dfa84f18b70dd935109c40d0d6bf4ff051 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 19:41:00 +0000 Subject: [PATCH 18/20] fix: corrected a comment. --- src/Math-Tests-Polynomials/PMPolynomialTest.class.st | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st index 1b83ae41..ed644b12 100644 --- a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st +++ b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st @@ -256,12 +256,13 @@ PMPolynomialTest >> testPolynomialRootsForConstant [ { #category : #'iterative algorithms' } PMPolynomialTest >> testPolynomialRootsForLinear [ + | polynomial roots | - "Here, compute the roots of the quadratic (2x + 1)" - polynomial := PMPolynomial coefficients: #(1 2). - roots := polynomial roots . + "Here, compute the roots of the linear (2x + 1)" + polynomial := PMPolynomial coefficients: #( 1 2 ). + roots := polynomial roots. self assert: roots size equals: 1. - self assert: (roots at: 1) closeTo: -0.5 . + self assert: (roots at: 1) closeTo: -0.5 ] { #category : #'function evaluation' } From 2fc4e5d9641b49614c77bde0031676f82ea6fdb3 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 20:34:00 +0000 Subject: [PATCH 19/20] test: clarified the name of a test. --- src/Math-Tests-Polynomials/PMPolynomialTest.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st index ed644b12..0aebf984 100644 --- a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st +++ b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st @@ -243,7 +243,7 @@ PMPolynomialTest >> testPolynomialRoots [ ] { #category : #'iterative algorithms' } -PMPolynomialTest >> testPolynomialRootsForConstant [ +PMPolynomialTest >> testPolynomialRootsConstantsHaveNoRoots [ | polynomial | "Here, compute the roots of the constant C = 1" From 6de9e7456ff3bc97955d01b992eae960f8449036 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 24 Dec 2023 20:50:19 +0000 Subject: [PATCH 20/20] refactor: Rename Variable where we make the roles of the objects clearer in the tests. --- .../PMPolynomialTest.class.st | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st index 0aebf984..aa20322a 100644 --- a/src/Math-Tests-Polynomials/PMPolynomialTest.class.st +++ b/src/Math-Tests-Polynomials/PMPolynomialTest.class.st @@ -245,11 +245,11 @@ PMPolynomialTest >> testPolynomialRoots [ { #category : #'iterative algorithms' } PMPolynomialTest >> testPolynomialRootsConstantsHaveNoRoots [ - | polynomial | + | constant | "Here, compute the roots of the constant C = 1" - polynomial := PMPolynomial coefficients: #( 1 ). + constant := PMPolynomial coefficients: #( 1 ). self - should: [ polynomial roots ] + should: [ constant roots ] raise: Error description: 'Function''s derivative seems to be zero everywhere' ] @@ -257,10 +257,10 @@ PMPolynomialTest >> testPolynomialRootsConstantsHaveNoRoots [ { #category : #'iterative algorithms' } PMPolynomialTest >> testPolynomialRootsForLinear [ - | polynomial roots | + | linearPolynomial roots | "Here, compute the roots of the linear (2x + 1)" - polynomial := PMPolynomial coefficients: #( 1 2 ). - roots := polynomial roots. + linearPolynomial := PMPolynomial coefficients: #( 1 2 ). + roots := linearPolynomial roots. self assert: roots size equals: 1. self assert: (roots at: 1) closeTo: -0.5 ] @@ -279,10 +279,10 @@ PMPolynomialTest >> testPolynomialSubtraction [ { #category : #'iterative algorithms' } PMPolynomialTest >> testPolynomialWithRepeatedRoots [ - | polynomial roots | + | polynomialWithRepeatedRoots roots | "Here, compute the roots of the quadratic (2x + 1)^2 = 4 x^2 + 4 x + 1" - polynomial := PMPolynomial coefficients: #(1 4 4). - roots := polynomial roots . + polynomialWithRepeatedRoots := PMPolynomial coefficients: #(1 4 4). + roots := polynomialWithRepeatedRoots roots . self assert: roots size equals: 2. self assert: (roots at: 1) closeTo: -0.5 . self assert: (roots at: 2) closeTo: -0.5 .