Skip to content

Commit

Permalink
Issue 192: Have Number Provide Complex Conjugate Message (#235)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
hemalvarambhia authored Apr 18, 2022
1 parent b3633ef commit 3f5808f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Math-Complex/Number.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/Math-Tests-Complex/PMComplexTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
30 changes: 30 additions & 0 deletions src/Math-Tests-Complex/PMNumberTest.class.st
Original file line number Diff line number Diff line change
@@ -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
]

0 comments on commit 3f5808f

Please sign in to comment.