From 3f5808fd400adecbb39ddc75e840af4555d0b6db Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 18 Apr 2022 09:15:44 +0100 Subject: [PATCH] Issue 192: Have Number Provide Complex Conjugate Message (#235) * Added a complexConjugate message to the Short Float. * Moved the method to the parent class. * Added a failing test to get passing for next session * Integer now responds to the complexConjugate message. * Moved the methods up to Number * Tested that the complex conjugate of a real fraction is itself. * We cannot instanciate a fraction whose numerator and denominator are complex numbers. It may be that we need to normalise the denominator first. * We can now write fractions of complex numbers that have already been normalized. * Added a test to demonstrate that we can write a complex number whose real and imaginary parts are Fractions. * Remove unnecessary comment. * Corrected the name of the test method. * May not make sense to convert a complex number to an integer, so inlined method and corrected a programmer test. * Clarified the category of the tests. * Clarified the category of the messages. --- src/Math-Complex/Number.extension.st | 5 +++ src/Math-Tests-Complex/PMComplexTest.class.st | 32 +++++++++++++++++++ src/Math-Tests-Complex/PMNumberTest.class.st | 30 +++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/Math-Tests-Complex/PMNumberTest.class.st 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 diff --git a/src/Math-Tests-Complex/PMComplexTest.class.st b/src/Math-Tests-Complex/PMComplexTest.class.st index 8dac6008..e98a94a6 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -750,6 +750,38 @@ PMComplexTest >> testTimesPolynomial [ self assert: (poly * c at: 0) equals: c ] +{ #category : #'expressing complex numbers' } +PMComplexTest >> testWeCanWriteComplexNumbersWhoseRealAndImaginaryPartsAreFractions [ + | 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 : #'expressing complex numbers' } +PMComplexTest >> testWeCannotWriteFractionsOfComplexNumbersWithDenominatorNormalized [ + + | z w numerator denominator | + z := 1 + 2 i. + w := 3 + 4 i. + numerator := z * w complexConjugate. + denominator := w squaredNorm. + + self should: [ Fraction numerator: numerator denominator: w squaredNorm ] raise: Exception. +] + +{ #category : #'expressing complex numbers' } +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" + 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 | diff --git a/src/Math-Tests-Complex/PMNumberTest.class.st b/src/Math-Tests-Complex/PMNumberTest.class.st new file mode 100644 index 00000000..bbed9c0e --- /dev/null +++ b/src/Math-Tests-Complex/PMNumberTest.class.st @@ -0,0 +1,30 @@ +Class { + #name : #PMNumberTest, + #superclass : #TestCase, + #category : #'Math-Tests-Complex' +} + +{ #category : #'complex conjugation' } +PMNumberTest >> testComplexConjugateOfAnIntegerIsAnInteger [ + |complexConjugateOfInteger| + + complexConjugateOfInteger := -5 complexConjugate. + + self assert: complexConjugateOfInteger equals: -5. +] + +{ #category : #'complex conjugation' } +PMNumberTest >> testComplexConjugateOfRealFractionIsARealFraction [ + | complexConjugateOfFraction | + complexConjugateOfFraction := (Fraction numerator: 1 denominator: 6) complexConjugate. + + self assert: complexConjugateOfFraction equals: (Fraction numerator: 1 denominator: 6) . +] + +{ #category : #'complex conjugation' } +PMNumberTest >> testComplexConjugateOfRealNumberIsItself [ + |realNumber| + realNumber := 4.5 complexConjugate. + + self assert: realNumber equals: 4.5 +]