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 +]