Skip to content

Commit 3f5808f

Browse files
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.
1 parent b3633ef commit 3f5808f

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/Math-Complex/Number.extension.st

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ Number >> asComplex [
1313
^ PMComplex real: self imaginary: 0
1414
]
1515

16+
{ #category : #'*Math-Complex' }
17+
Number >> complexConjugate [
18+
^ self.
19+
]
20+
1621
{ #category : #'*Math-Complex' }
1722
Number >> i [
1823
^ PMComplex real: 0 imaginary: self

src/Math-Tests-Complex/PMComplexTest.class.st

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,38 @@ PMComplexTest >> testTimesPolynomial [
750750
self assert: (poly * c at: 0) equals: c
751751
]
752752

753+
{ #category : #'expressing complex numbers' }
754+
PMComplexTest >> testWeCanWriteComplexNumbersWhoseRealAndImaginaryPartsAreFractions [
755+
| z |
756+
z := PMComplex real: 3 / 5 imaginary: 4 / 5.
757+
758+
self assert: (z real) equals: (Fraction numerator: 3 denominator: 5).
759+
self assert: (z imaginary) equals: (Fraction numerator: 4 denominator: 5).
760+
]
761+
762+
{ #category : #'expressing complex numbers' }
763+
PMComplexTest >> testWeCannotWriteFractionsOfComplexNumbersWithDenominatorNormalized [
764+
765+
| z w numerator denominator |
766+
z := 1 + 2 i.
767+
w := 3 + 4 i.
768+
numerator := z * w complexConjugate.
769+
denominator := w squaredNorm.
770+
771+
self should: [ Fraction numerator: numerator denominator: w squaredNorm ] raise: Exception.
772+
]
773+
774+
{ #category : #'expressing complex numbers' }
775+
PMComplexTest >> testWeCannotWriteFractionsWhoseNumeratorAndDenominatorAreComplexNumbers [
776+
| numerator denominator |
777+
"It is interesting that we cannot instanciate a fraction of the form z/w"
778+
"Perhaps, we could do something like z * (w*)/ |w|**2"
779+
numerator := PMComplex real: 1 imaginary: 1.
780+
denominator := PMComplex real: 3 imaginary: 4 .
781+
782+
self should: [ Fraction numerator: numerator denominator: denominator ] raise: Exception .
783+
]
784+
753785
{ #category : #tests }
754786
PMComplexTest >> testZero [
755787
| z |
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Class {
2+
#name : #PMNumberTest,
3+
#superclass : #TestCase,
4+
#category : #'Math-Tests-Complex'
5+
}
6+
7+
{ #category : #'complex conjugation' }
8+
PMNumberTest >> testComplexConjugateOfAnIntegerIsAnInteger [
9+
|complexConjugateOfInteger|
10+
11+
complexConjugateOfInteger := -5 complexConjugate.
12+
13+
self assert: complexConjugateOfInteger equals: -5.
14+
]
15+
16+
{ #category : #'complex conjugation' }
17+
PMNumberTest >> testComplexConjugateOfRealFractionIsARealFraction [
18+
| complexConjugateOfFraction |
19+
complexConjugateOfFraction := (Fraction numerator: 1 denominator: 6) complexConjugate.
20+
21+
self assert: complexConjugateOfFraction equals: (Fraction numerator: 1 denominator: 6) .
22+
]
23+
24+
{ #category : #'complex conjugation' }
25+
PMNumberTest >> testComplexConjugateOfRealNumberIsItself [
26+
|realNumber|
27+
realNumber := 4.5 complexConjugate.
28+
29+
self assert: realNumber equals: 4.5
30+
]

0 commit comments

Comments
 (0)