Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 192: Have Number Provide Complex Conjugate Message #235

Merged
merged 14 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
]