From 2ac091fc859b4f218595bdadf015745ac6b18a31 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sat, 2 Apr 2022 21:32:43 +0100 Subject: [PATCH 01/11] Extracted the implementation to a new method that clients should use. --- src/Math-Complex/PMComplex.class.st | 9 ++++++++- src/Math-Complex/PMComplex.extension.st | 14 +++++++------- src/Math-Tests-Complex/PMComplexTest.class.st | 6 +++--- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Math-Complex/PMComplex.class.st b/src/Math-Complex/PMComplex.class.st index ad1b0debb..0c5054084 100644 --- a/src/Math-Complex/PMComplex.class.st +++ b/src/Math-Complex/PMComplex.class.st @@ -403,11 +403,18 @@ PMComplex >> asComplex [ ^self ] +{ #category : #arithmetic } +PMComplex >> complexConjugate [ + + ^ self class real: real imaginary: imaginary negated +] + { #category : #arithmetic } PMComplex >> conjugated [ + "Return the complex conjugate of this complex number." - ^self class real: real imaginary: imaginary negated + ^ self complexConjugate ] { #category : #'mathematical functions' } diff --git a/src/Math-Complex/PMComplex.extension.st b/src/Math-Complex/PMComplex.extension.st index 5cf918b4d..efe456833 100644 --- a/src/Math-Complex/PMComplex.extension.st +++ b/src/Math-Complex/PMComplex.extension.st @@ -17,13 +17,6 @@ PMComplex >> productWithVector: aVector [ ^ aVector collect: [ :each | each * self ] ] -{ #category : #'*Math-Complex' } -PMComplex >> random [ - "analog to Number>>random. However, the only bound is that the abs of the produced complex is less than the length of the receive. The receiver effectively defines a disc within which the random element can be produced." - ^ self class random * self - -] - { #category : #'*Math-Complex' } PMComplex class >> random [ "Answers a random number with abs between 0 and 1." @@ -31,6 +24,13 @@ PMComplex class >> random [ ^ self abs: 1.0 random arg: 2 * Float pi random ] +{ #category : #'*Math-Complex' } +PMComplex >> random [ + "analog to Number>>random. However, the only bound is that the abs of the produced complex is less than the length of the receive. The receiver effectively defines a disc within which the random element can be produced." + ^ self class random * self + +] + { #category : #'*Math-Complex' } PMComplex >> subtractToPolynomial: aPolynomial [ ^ aPolynomial addNumber: self negated diff --git a/src/Math-Tests-Complex/PMComplexTest.class.st b/src/Math-Tests-Complex/PMComplexTest.class.st index 93dc7b80b..eedc92057 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -195,11 +195,11 @@ PMComplexTest >> testComplexCollection [ { #category : #tests } PMComplexTest >> testConjugated [ - | c cc | + | c cc expected | c := 5 - 6 i. cc := c conjugated. - self assert: cc real equals: c real. - self assert: cc imaginary equals: c imaginary negated + expected := 5 + 6 i. + self assert: cc equals: expected. ] { #category : #tests } From 39e6542035be5551270e552b491dedac65f2bd28 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sat, 2 Apr 2022 21:37:53 +0100 Subject: [PATCH 02/11] Improved the name of the variable. --- src/Math-Tests-Complex/PMComplexTest.class.st | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Math-Tests-Complex/PMComplexTest.class.st b/src/Math-Tests-Complex/PMComplexTest.class.st index eedc92057..5d5cca2d1 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -195,11 +195,11 @@ PMComplexTest >> testComplexCollection [ { #category : #tests } PMComplexTest >> testConjugated [ - | c cc expected | - c := 5 - 6 i. - cc := c conjugated. + | z complexConjugateOfZ expected | + z := 5 - 6 i. + complexConjugateOfZ := z conjugated. expected := 5 + 6 i. - self assert: cc equals: expected. + self assert: complexConjugateOfZ equals: expected. ] { #category : #tests } From 4d5770c5046d9be80325d3ec0c03f82079c84278 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 3 Apr 2022 09:10:45 +0100 Subject: [PATCH 03/11] Improved code formatting. --- src/Math-Tests-Complex/PMComplexTest.class.st | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Math-Tests-Complex/PMComplexTest.class.st b/src/Math-Tests-Complex/PMComplexTest.class.st index 5d5cca2d1..ee08bb2b8 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -197,7 +197,9 @@ PMComplexTest >> testComplexCollection [ PMComplexTest >> testConjugated [ | z complexConjugateOfZ expected | z := 5 - 6 i. - complexConjugateOfZ := z conjugated. + + complexConjugateOfZ := z complexConjugate . + expected := 5 + 6 i. self assert: complexConjugateOfZ equals: expected. ] From 48f94b288905be161a6ccffb8ad335f5fb4727c7 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 3 Apr 2022 09:16:38 +0100 Subject: [PATCH 04/11] Corrected the name of the test method to match the method name. Pharo is then able to put a dot next to the method in production. --- src/Math-Tests-Complex/PMComplexTest.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Math-Tests-Complex/PMComplexTest.class.st b/src/Math-Tests-Complex/PMComplexTest.class.st index ee08bb2b8..842605d1f 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -194,7 +194,7 @@ PMComplexTest >> testComplexCollection [ ] { #category : #tests } -PMComplexTest >> testConjugated [ +PMComplexTest >> testComplexConjugate [ | z complexConjugateOfZ expected | z := 5 - 6 i. From c4327cdefaacf9ca756b91503a943a5f76c20646 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 3 Apr 2022 09:19:01 +0100 Subject: [PATCH 05/11] Deprecated an unused method. --- src/Math-Complex/PMComplex.class.st | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Math-Complex/PMComplex.class.st b/src/Math-Complex/PMComplex.class.st index 0c5054084..6d23903d8 100644 --- a/src/Math-Complex/PMComplex.class.st +++ b/src/Math-Complex/PMComplex.class.st @@ -414,6 +414,12 @@ PMComplex >> conjugated [ "Return the complex conjugate of this complex number." + self + deprecated: 'Use #complexConjugate instead' + on: '3 April 2022' + in: + 'Pharo-9.0.0+build.1575.sha.9bb5f998e8a6d016ec7abde3ed09c4a60c0b4551 (64 Bit)'. + ^ self complexConjugate ] From dbe29a3ed5c77d90663f2703c73a910dd1eb08cd Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 3 Apr 2022 09:28:34 +0100 Subject: [PATCH 06/11] Migrated clients. --- src/Math-AutomaticDifferenciation/PMDualNumber.class.st | 2 +- .../PMDualNumberTest.class.st | 2 +- src/Math-Tests-Complex/PMComplexTest.class.st | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Math-AutomaticDifferenciation/PMDualNumber.class.st b/src/Math-AutomaticDifferenciation/PMDualNumber.class.st index 6033766d7..678819c22 100644 --- a/src/Math-AutomaticDifferenciation/PMDualNumber.class.st +++ b/src/Math-AutomaticDifferenciation/PMDualNumber.class.st @@ -137,7 +137,7 @@ PMDualNumber >> asInteger [ PMDualNumber >> conjugated [ ^ self class value: self value conjugated - eps: self eps asComplex conjugated + eps: self eps asComplex complexConjugate ] { #category : #'mathematical functions' } diff --git a/src/Math-Tests-AutomaticDifferenciation/PMDualNumberTest.class.st b/src/Math-Tests-AutomaticDifferenciation/PMDualNumberTest.class.st index 3809029b6..1298678fc 100644 --- a/src/Math-Tests-AutomaticDifferenciation/PMDualNumberTest.class.st +++ b/src/Math-Tests-AutomaticDifferenciation/PMDualNumberTest.class.st @@ -89,7 +89,7 @@ PMDualNumberTest >> testConjugated [ self assert: a value equals: z value absSquared. self assert: a eps - equals: z eps asComplex conjugated * z value + (z value asComplex conjugated * z eps) + equals: z eps asComplex complexConjugate * z value + (z value asComplex conjugated * z eps) ] { #category : #tests } diff --git a/src/Math-Tests-Complex/PMComplexTest.class.st b/src/Math-Tests-Complex/PMComplexTest.class.st index 842605d1f..04cb73aaa 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -488,7 +488,7 @@ PMComplexTest >> testNumberAsComplex [ minusOne := -1 asComplex. self assert: minusOne real equals: -1. self assert: minusOne imaginary equals: 0. - self assert: minusOne conjugated equals: minusOne. + self assert: minusOne complexConjugate equals: minusOne. self assert: minusOne sqrt equals: 1 i ] From 0d5e8735e6761a679cf8e9933ef3e6cf8228e949 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Mon, 4 Apr 2022 19:14:41 +0100 Subject: [PATCH 07/11] We no longer refer to the real class. --- src/Math-Complex/PMComplex.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Math-Complex/PMComplex.class.st b/src/Math-Complex/PMComplex.class.st index 6d23903d8..bed0a7065 100644 --- a/src/Math-Complex/PMComplex.class.st +++ b/src/Math-Complex/PMComplex.class.st @@ -179,7 +179,7 @@ PMComplex >> / anObject [ d := anObject imaginary. newReal := ((a * c) + (b * d)) / ((c * c) + (d * d)). newImaginary := ((b * c) - (a * d)) / ((c * c) + (d * d)). - ^ PMComplex real: newReal imaginary: newImaginary]. + ^ self class real: newReal imaginary: newImaginary]. ^ anObject adaptToComplex: self andSend: #/. ] From 9f3f82f41f40239f658517eeb09a111e92160bb0 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Mon, 4 Apr 2022 19:20:24 +0100 Subject: [PATCH 08/11] Extracted the predicate to an intention revealing method. --- src/Math-Complex/PMComplex.class.st | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Math-Complex/PMComplex.class.st b/src/Math-Complex/PMComplex.class.st index bed0a7065..c392966f9 100644 --- a/src/Math-Complex/PMComplex.class.st +++ b/src/Math-Complex/PMComplex.class.st @@ -557,6 +557,12 @@ PMComplex >> isNumber [ ^ true ] +{ #category : #'mathematical functions' } +PMComplex >> isReal [ + + ^ imaginary = 0 and: [ real >= 0 ] +] + { #category : #testing } PMComplex >> isZero [ ^ real isZero and: [imaginary isZero] @@ -707,14 +713,14 @@ PMComplex >> sinh [ { #category : #'mathematical functions' } PMComplex >> sqrt [ + "Return the square root of the receiver with a positive imaginary part." | u v | - (imaginary = 0 and: [real >= 0]) - ifTrue: [^self class real: real sqrt imaginary: 0]. + self isReal ifTrue: [ ^ self class real: real sqrt imaginary: 0 ]. v := (self abs - real / 2) sqrt. u := imaginary / 2 / v. - ^self class real: u imaginary: v + ^ self class real: u imaginary: v ] { #category : #'mathematical functions' } From 78380911368c6654281e7e43064df2b36d30e9fb Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Mon, 4 Apr 2022 19:21:50 +0100 Subject: [PATCH 09/11] Placed the message to a more appropriate category. --- src/Math-Complex/PMComplex.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Math-Complex/PMComplex.class.st b/src/Math-Complex/PMComplex.class.st index c392966f9..ac4f36aba 100644 --- a/src/Math-Complex/PMComplex.class.st +++ b/src/Math-Complex/PMComplex.class.st @@ -557,7 +557,7 @@ PMComplex >> isNumber [ ^ true ] -{ #category : #'mathematical functions' } +{ #category : #testing } PMComplex >> isReal [ ^ imaginary = 0 and: [ real >= 0 ] From 57b79ab784ded0008408e091828eeac993276f72 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Mon, 4 Apr 2022 20:16:42 +0100 Subject: [PATCH 10/11] Clarified the name of the message argument. --- src/Math-Complex/PMComplex.class.st | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Math-Complex/PMComplex.class.st b/src/Math-Complex/PMComplex.class.st index ac4f36aba..d53194d87 100644 --- a/src/Math-Complex/PMComplex.class.st +++ b/src/Math-Complex/PMComplex.class.st @@ -169,18 +169,18 @@ PMComplex >> - anObject [ ] { #category : #arithmetic } -PMComplex >> / anObject [ +PMComplex >> / aNumber [ "Answer the result of dividing receiver by aNumber" | a b c d newReal newImaginary | - anObject isComplexNumber ifTrue: + aNumber isComplexNumber ifTrue: [a := self real. b := self imaginary. - c := anObject real. - d := anObject imaginary. + c := aNumber real. + d := aNumber imaginary. newReal := ((a * c) + (b * d)) / ((c * c) + (d * d)). newImaginary := ((b * c) - (a * d)) / ((c * c) + (d * d)). ^ self class real: newReal imaginary: newImaginary]. - ^ anObject adaptToComplex: self andSend: #/. + ^ aNumber adaptToComplex: self andSend: #/. ] { #category : #comparing } From 83860b8fd35e02298db754206ed2d8b7a2253502 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Tue, 5 Apr 2022 07:50:17 +0100 Subject: [PATCH 11/11] Inlined a method. We'll need to add tests before refactoring this method. --- src/Math-Complex/PMComplex.class.st | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Math-Complex/PMComplex.class.st b/src/Math-Complex/PMComplex.class.st index d53194d87..50593e8bd 100644 --- a/src/Math-Complex/PMComplex.class.st +++ b/src/Math-Complex/PMComplex.class.st @@ -557,12 +557,6 @@ PMComplex >> isNumber [ ^ true ] -{ #category : #testing } -PMComplex >> isReal [ - - ^ imaginary = 0 and: [ real >= 0 ] -] - { #category : #testing } PMComplex >> isZero [ ^ real isZero and: [imaginary isZero] @@ -717,7 +711,8 @@ PMComplex >> sqrt [ "Return the square root of the receiver with a positive imaginary part." | u v | - self isReal ifTrue: [ ^ self class real: real sqrt imaginary: 0 ]. + (imaginary = 0 and: [ real >= 0 ]) ifTrue: [ + ^ self class real: real sqrt imaginary: 0 ]. v := (self abs - real / 2) sqrt. u := imaginary / 2 / v. ^ self class real: u imaginary: v