From fb35071b7b732478a9cf17030da39d779639d78b Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Fri, 8 Apr 2022 19:46:56 +0100 Subject: [PATCH 01/14] Added a complexConjugate message to the Short Float. --- src/Math-Complex/SmallFloat64.extension.st | 6 ++++++ src/Math-Tests-Complex/PMNumberTest.class.st | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/Math-Complex/SmallFloat64.extension.st create mode 100644 src/Math-Tests-Complex/PMNumberTest.class.st diff --git a/src/Math-Complex/SmallFloat64.extension.st b/src/Math-Complex/SmallFloat64.extension.st new file mode 100644 index 00000000..62da4a6e --- /dev/null +++ b/src/Math-Complex/SmallFloat64.extension.st @@ -0,0 +1,6 @@ +Extension { #name : #SmallFloat64 } + +{ #category : #'*Math-Complex' } +SmallFloat64 >> complexConjugate [ + ^ self. +] diff --git a/src/Math-Tests-Complex/PMNumberTest.class.st b/src/Math-Tests-Complex/PMNumberTest.class.st new file mode 100644 index 00000000..67a4dfcc --- /dev/null +++ b/src/Math-Tests-Complex/PMNumberTest.class.st @@ -0,0 +1,16 @@ +Class { + #name : #PMNumberTest, + #superclass : #TestCase, + #category : #'Math-Tests-Complex' +} + +{ #category : #tests } +PMNumberTest >> testComplexConjugateOfRealNumberIsItself [ + "comment stating purpose of instance-side method" + "scope: class-variables & instance-variables" + + |realNumber| + realNumber := 4.5 complexConjugate. + + self assert: realNumber equals: 4.5 +] From 4d860b1c6ae3f9d0c51e65a009d9edbab2c9c2f4 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Fri, 8 Apr 2022 19:48:33 +0100 Subject: [PATCH 02/14] Moved the method to the parent class. --- src/Math-Complex/Float.extension.st | 6 ++++++ src/Math-Complex/SmallFloat64.extension.st | 6 ------ 2 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 src/Math-Complex/Float.extension.st delete mode 100644 src/Math-Complex/SmallFloat64.extension.st diff --git a/src/Math-Complex/Float.extension.st b/src/Math-Complex/Float.extension.st new file mode 100644 index 00000000..f623925b --- /dev/null +++ b/src/Math-Complex/Float.extension.st @@ -0,0 +1,6 @@ +Extension { #name : #Float } + +{ #category : #'*Math-Complex' } +Float >> complexConjugate [ + ^ self. +] diff --git a/src/Math-Complex/SmallFloat64.extension.st b/src/Math-Complex/SmallFloat64.extension.st deleted file mode 100644 index 62da4a6e..00000000 --- a/src/Math-Complex/SmallFloat64.extension.st +++ /dev/null @@ -1,6 +0,0 @@ -Extension { #name : #SmallFloat64 } - -{ #category : #'*Math-Complex' } -SmallFloat64 >> complexConjugate [ - ^ self. -] From 1ea2d9f4c205a6ea783a030e4da4dc3cc97bd734 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Fri, 8 Apr 2022 19:54:28 +0100 Subject: [PATCH 03/14] Added a failing test to get passing for next session --- src/Math-Tests-Complex/PMNumberTest.class.st | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Math-Tests-Complex/PMNumberTest.class.st b/src/Math-Tests-Complex/PMNumberTest.class.st index 67a4dfcc..f002dda7 100644 --- a/src/Math-Tests-Complex/PMNumberTest.class.st +++ b/src/Math-Tests-Complex/PMNumberTest.class.st @@ -4,6 +4,15 @@ Class { #category : #'Math-Tests-Complex' } +{ #category : #tests } +PMNumberTest >> testComplexConjugateOfAnIntegerIsAnInteger [ + |complexConjugateOfInteger| + + complexConjugateOfInteger := -5 complexConjugate. + + self assert: complexConjugateOfInteger equals: -5. +] + { #category : #tests } PMNumberTest >> testComplexConjugateOfRealNumberIsItself [ "comment stating purpose of instance-side method" From 861ef4759d7415328e99b431e2869c80fa137699 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sat, 9 Apr 2022 19:18:05 +0100 Subject: [PATCH 04/14] Integer now responds to the complexConjugate message. --- src/Math-Complex/Integer.extension.st | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/Math-Complex/Integer.extension.st diff --git a/src/Math-Complex/Integer.extension.st b/src/Math-Complex/Integer.extension.st new file mode 100644 index 00000000..a90b8b4e --- /dev/null +++ b/src/Math-Complex/Integer.extension.st @@ -0,0 +1,6 @@ +Extension { #name : #Integer } + +{ #category : #'*Math-Complex' } +Integer >> complexConjugate [ + ^ self. +] From 00bd816ffa843a669ca78202a7a1b5b34f3d2aae Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sat, 9 Apr 2022 19:19:51 +0100 Subject: [PATCH 05/14] Moved the methods up to Number --- src/Math-Complex/Float.extension.st | 6 ------ src/Math-Complex/Integer.extension.st | 6 ------ src/Math-Complex/Number.extension.st | 5 +++++ 3 files changed, 5 insertions(+), 12 deletions(-) delete mode 100644 src/Math-Complex/Float.extension.st delete mode 100644 src/Math-Complex/Integer.extension.st diff --git a/src/Math-Complex/Float.extension.st b/src/Math-Complex/Float.extension.st deleted file mode 100644 index f623925b..00000000 --- a/src/Math-Complex/Float.extension.st +++ /dev/null @@ -1,6 +0,0 @@ -Extension { #name : #Float } - -{ #category : #'*Math-Complex' } -Float >> complexConjugate [ - ^ self. -] diff --git a/src/Math-Complex/Integer.extension.st b/src/Math-Complex/Integer.extension.st deleted file mode 100644 index a90b8b4e..00000000 --- a/src/Math-Complex/Integer.extension.st +++ /dev/null @@ -1,6 +0,0 @@ -Extension { #name : #Integer } - -{ #category : #'*Math-Complex' } -Integer >> complexConjugate [ - ^ self. -] diff --git a/src/Math-Complex/Number.extension.st b/src/Math-Complex/Number.extension.st index 9fdceb6f..10805751 100644 --- a/src/Math-Complex/Number.extension.st +++ b/src/Math-Complex/Number.extension.st @@ -13,6 +13,11 @@ Number >> asComplex [ ^ PMComplex real: self imaginary: 0 ] +{ #category : #'*Math-Complex' } +Number >> complexConjugate [ + ^ self. +] + { #category : #'*Math-Complex' } Number >> i [ ^ PMComplex real: 0 imaginary: self From 622034011b1cf1b28cedcfa7b9d3621e0445b9f9 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sat, 9 Apr 2022 19:22:52 +0100 Subject: [PATCH 06/14] Tested that the complex conjugate of a real fraction is itself. --- src/Math-Tests-Complex/PMNumberTest.class.st | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Math-Tests-Complex/PMNumberTest.class.st b/src/Math-Tests-Complex/PMNumberTest.class.st index f002dda7..c81f4151 100644 --- a/src/Math-Tests-Complex/PMNumberTest.class.st +++ b/src/Math-Tests-Complex/PMNumberTest.class.st @@ -13,6 +13,14 @@ PMNumberTest >> testComplexConjugateOfAnIntegerIsAnInteger [ self assert: complexConjugateOfInteger equals: -5. ] +{ #category : #tests } +PMNumberTest >> testComplexConjugateOfRealFractionIsARealFraction [ + | complexConjugateOfFraction | + complexConjugateOfFraction := (Fraction numerator: 1 denominator: 6) complexConjugate. + + self assert: complexConjugateOfFraction equals: (Fraction numerator: 1 denominator: 6) . +] + { #category : #tests } PMNumberTest >> testComplexConjugateOfRealNumberIsItself [ "comment stating purpose of instance-side method" From 03df70da8e124d03495f700a097f00425fcad3ea Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sat, 9 Apr 2022 19:36:27 +0100 Subject: [PATCH 07/14] We cannot instanciate a fraction whose numerator and denominator are complex numbers. It may be that we need to normalise the denominator first. --- src/Math-Tests-Complex/PMComplexTest.class.st | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Math-Tests-Complex/PMComplexTest.class.st b/src/Math-Tests-Complex/PMComplexTest.class.st index 8dac6008..54dfba2f 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -750,6 +750,17 @@ PMComplexTest >> testTimesPolynomial [ self assert: (poly * c at: 0) equals: c ] +{ #category : #tests } +PMComplexTest >> testWeCannotInstanciateAFractionWhoseNumeratorAndDenominatorAreComplexNumbers [ + | numerator denominator | + "It is interesting that we cannot instanciate a fraction of the form z/w" + "Perhaps, we could do something like z * (w*)/ |w|**2" + numerator := PMComplex real: 1 imaginary: 1. + denominator := PMComplex real: 3 imaginary: 4 . + + self should: [ Fraction numerator: numerator denominator: denominator ] raise: Exception . +] + { #category : #tests } PMComplexTest >> testZero [ | z | From ffcb3aee469b0e623857cdfc4275d3a25fa0f234 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sat, 9 Apr 2022 19:59:30 +0100 Subject: [PATCH 08/14] We can now write fractions of complex numbers that have already been normalized. --- src/Math-Complex/PMComplex.class.st | 5 +++++ src/Math-Tests-Complex/PMComplexTest.class.st | 14 +++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Math-Complex/PMComplex.class.st b/src/Math-Complex/PMComplex.class.st index 50593e8b..93d5f5a8 100644 --- a/src/Math-Complex/PMComplex.class.st +++ b/src/Math-Complex/PMComplex.class.st @@ -403,6 +403,11 @@ PMComplex >> asComplex [ ^self ] +{ #category : #converting } +PMComplex >> asInteger [ + ^ self. +] + { #category : #arithmetic } PMComplex >> complexConjugate [ diff --git a/src/Math-Tests-Complex/PMComplexTest.class.st b/src/Math-Tests-Complex/PMComplexTest.class.st index 54dfba2f..06520bd6 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -751,7 +751,19 @@ PMComplexTest >> testTimesPolynomial [ ] { #category : #tests } -PMComplexTest >> testWeCannotInstanciateAFractionWhoseNumeratorAndDenominatorAreComplexNumbers [ +PMComplexTest >> testWeCanWriteFractionsOfComplexNumbersWithDenominatorNormalized [ + + | z w fraction expected | + z := 1 + 2 i. + w := 3 + 4 i. + fraction := Fraction numerator: (z * w complexConjugate) denominator: w squaredNorm. + + expected := Fraction numerator: (11 + 2 i) denominator: 25. + self assert: fraction equals: expected. +] + +{ #category : #tests } +PMComplexTest >> testWeCannotWriteFractionsWhoseNumeratorAndDenominatorAreComplexNumbers [ | numerator denominator | "It is interesting that we cannot instanciate a fraction of the form z/w" "Perhaps, we could do something like z * (w*)/ |w|**2" From 20e28db1e2b2a6fcf24a1dbe095f565af826eedb Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 10 Apr 2022 21:41:08 +0100 Subject: [PATCH 09/14] Added a test to demonstrate that we can write a complex number whose real and imaginary parts are Fractions. --- src/Math-Tests-Complex/PMComplexTest.class.st | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Math-Tests-Complex/PMComplexTest.class.st b/src/Math-Tests-Complex/PMComplexTest.class.st index 06520bd6..35b71353 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -750,6 +750,15 @@ PMComplexTest >> testTimesPolynomial [ self assert: (poly * c at: 0) equals: c ] +{ #category : #tests } +PMComplexTest >> testWeCanWriteComplexNumbersWhoRealAndImaginaryPartsAreFractions [ + | z | + z := PMComplex real: 3 / 5 imaginary: 4 / 5. + + self assert: (z real) equals: (Fraction numerator: 3 denominator: 5). + self assert: (z imaginary) equals: (Fraction numerator: 4 denominator: 5). +] + { #category : #tests } PMComplexTest >> testWeCanWriteFractionsOfComplexNumbersWithDenominatorNormalized [ From 53e6509afd2cf46e3ea7a0bddf3ddaeb8cb5c4ee Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Sun, 10 Apr 2022 21:47:44 +0100 Subject: [PATCH 10/14] Remove unnecessary comment. --- src/Math-Tests-Complex/PMNumberTest.class.st | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Math-Tests-Complex/PMNumberTest.class.st b/src/Math-Tests-Complex/PMNumberTest.class.st index c81f4151..e78156da 100644 --- a/src/Math-Tests-Complex/PMNumberTest.class.st +++ b/src/Math-Tests-Complex/PMNumberTest.class.st @@ -22,10 +22,7 @@ PMNumberTest >> testComplexConjugateOfRealFractionIsARealFraction [ ] { #category : #tests } -PMNumberTest >> testComplexConjugateOfRealNumberIsItself [ - "comment stating purpose of instance-side method" - "scope: class-variables & instance-variables" - +PMNumberTest >> testComplexConjugateOfRealNumberIsItself [ |realNumber| realNumber := 4.5 complexConjugate. From 7306d7b15778aa672e4748ed804cfdcf78b30e76 Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Mon, 11 Apr 2022 09:50:44 +0100 Subject: [PATCH 11/14] Corrected the name of the test method. --- 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 35b71353..21c81676 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -751,7 +751,7 @@ PMComplexTest >> testTimesPolynomial [ ] { #category : #tests } -PMComplexTest >> testWeCanWriteComplexNumbersWhoRealAndImaginaryPartsAreFractions [ +PMComplexTest >> testWeCanWriteComplexNumbersWhoseRealAndImaginaryPartsAreFractions [ | z | z := PMComplex real: 3 / 5 imaginary: 4 / 5. From 37cb8d6b97bb3bb3e32944f7445b66c8004bb09e Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Tue, 12 Apr 2022 08:44:45 +0100 Subject: [PATCH 12/14] May not make sense to convert a complex number to an integer, so inlined method and corrected a programmer test. --- src/Math-Complex/PMComplex.class.st | 5 ----- src/Math-Tests-Complex/PMComplexTest.class.st | 10 +++++----- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Math-Complex/PMComplex.class.st b/src/Math-Complex/PMComplex.class.st index 93d5f5a8..50593e8b 100644 --- a/src/Math-Complex/PMComplex.class.st +++ b/src/Math-Complex/PMComplex.class.st @@ -403,11 +403,6 @@ PMComplex >> asComplex [ ^self ] -{ #category : #converting } -PMComplex >> asInteger [ - ^ self. -] - { #category : #arithmetic } PMComplex >> complexConjugate [ diff --git a/src/Math-Tests-Complex/PMComplexTest.class.st b/src/Math-Tests-Complex/PMComplexTest.class.st index 21c81676..a5f0b097 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -760,15 +760,15 @@ PMComplexTest >> testWeCanWriteComplexNumbersWhoseRealAndImaginaryPartsAreFracti ] { #category : #tests } -PMComplexTest >> testWeCanWriteFractionsOfComplexNumbersWithDenominatorNormalized [ +PMComplexTest >> testWeCannotWriteFractionsOfComplexNumbersWithDenominatorNormalized [ - | z w fraction expected | + | z w numerator denominator | z := 1 + 2 i. w := 3 + 4 i. - fraction := Fraction numerator: (z * w complexConjugate) denominator: w squaredNorm. + numerator := z * w complexConjugate. + denominator := w squaredNorm. - expected := Fraction numerator: (11 + 2 i) denominator: 25. - self assert: fraction equals: expected. + self should: [ Fraction numerator: numerator denominator: w squaredNorm ] raise: Exception. ] { #category : #tests } From 8d5254c7a284067665835b53ef18a083e7b9a64a Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Thu, 14 Apr 2022 20:18:41 +0100 Subject: [PATCH 13/14] Clarified the category of the tests. --- src/Math-Tests-Complex/PMNumberTest.class.st | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Math-Tests-Complex/PMNumberTest.class.st b/src/Math-Tests-Complex/PMNumberTest.class.st index e78156da..bbed9c0e 100644 --- a/src/Math-Tests-Complex/PMNumberTest.class.st +++ b/src/Math-Tests-Complex/PMNumberTest.class.st @@ -4,7 +4,7 @@ Class { #category : #'Math-Tests-Complex' } -{ #category : #tests } +{ #category : #'complex conjugation' } PMNumberTest >> testComplexConjugateOfAnIntegerIsAnInteger [ |complexConjugateOfInteger| @@ -13,7 +13,7 @@ PMNumberTest >> testComplexConjugateOfAnIntegerIsAnInteger [ self assert: complexConjugateOfInteger equals: -5. ] -{ #category : #tests } +{ #category : #'complex conjugation' } PMNumberTest >> testComplexConjugateOfRealFractionIsARealFraction [ | complexConjugateOfFraction | complexConjugateOfFraction := (Fraction numerator: 1 denominator: 6) complexConjugate. @@ -21,7 +21,7 @@ PMNumberTest >> testComplexConjugateOfRealFractionIsARealFraction [ self assert: complexConjugateOfFraction equals: (Fraction numerator: 1 denominator: 6) . ] -{ #category : #tests } +{ #category : #'complex conjugation' } PMNumberTest >> testComplexConjugateOfRealNumberIsItself [ |realNumber| realNumber := 4.5 complexConjugate. From 691feb839964d6c05990e9d0275dd839a9d1de9f Mon Sep 17 00:00:00 2001 From: hemalvarambhia Date: Thu, 14 Apr 2022 20:19:56 +0100 Subject: [PATCH 14/14] Clarified the category of the messages. --- src/Math-Tests-Complex/PMComplexTest.class.st | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Math-Tests-Complex/PMComplexTest.class.st b/src/Math-Tests-Complex/PMComplexTest.class.st index a5f0b097..e98a94a6 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -750,7 +750,7 @@ PMComplexTest >> testTimesPolynomial [ self assert: (poly * c at: 0) equals: c ] -{ #category : #tests } +{ #category : #'expressing complex numbers' } PMComplexTest >> testWeCanWriteComplexNumbersWhoseRealAndImaginaryPartsAreFractions [ | z | z := PMComplex real: 3 / 5 imaginary: 4 / 5. @@ -759,7 +759,7 @@ PMComplexTest >> testWeCanWriteComplexNumbersWhoseRealAndImaginaryPartsAreFracti self assert: (z imaginary) equals: (Fraction numerator: 4 denominator: 5). ] -{ #category : #tests } +{ #category : #'expressing complex numbers' } PMComplexTest >> testWeCannotWriteFractionsOfComplexNumbersWithDenominatorNormalized [ | z w numerator denominator | @@ -771,7 +771,7 @@ PMComplexTest >> testWeCannotWriteFractionsOfComplexNumbersWithDenominatorNormal self should: [ Fraction numerator: numerator denominator: w squaredNorm ] raise: Exception. ] -{ #category : #tests } +{ #category : #'expressing complex numbers' } PMComplexTest >> testWeCannotWriteFractionsWhoseNumeratorAndDenominatorAreComplexNumbers [ | numerator denominator | "It is interesting that we cannot instanciate a fraction of the form z/w"