Skip to content

Commit

Permalink
Merge pull request #231 from PolyMathOrg/refactor-to-deeper-insight
Browse files Browse the repository at this point in the history
Refactor: Introduce complexConjugate Message, Deprecate conjugated
  • Loading branch information
SergeStinckwich authored Apr 5, 2022
2 parents d70167c + 83860b8 commit 33bfd2c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Math-AutomaticDifferenciation/PMDualNumber.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ PMDualNumber >> asInteger [
PMDualNumber >> conjugated [
^ self class
value: self value conjugated
eps: self eps asComplex conjugated
eps: self eps asComplex complexConjugate
]

{ #category : #'mathematical functions' }
Expand Down
34 changes: 24 additions & 10 deletions src/Math-Complex/PMComplex.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,18 @@ PMComplex >> - anObject [
]

{ #category : #arithmetic }
PMComplex >> / anObject [
PMComplex >> / aNumber [
"Answer the result of dividing receiver by aNumber"
| a b c d newReal newImaginary |
anObject isComplexNumber ifTrue:
aNumber isComplexNumber ifTrue:
[a := self real.
b := self imaginary.
c := anObject real.
d := anObject imaginary.
c := aNumber real.
d := aNumber imaginary.
newReal := ((a * c) + (b * d)) / ((c * c) + (d * d)).
newImaginary := ((b * c) - (a * d)) / ((c * c) + (d * d)).
^ PMComplex real: newReal imaginary: newImaginary].
^ anObject adaptToComplex: self andSend: #/.
^ self class real: newReal imaginary: newImaginary].
^ aNumber adaptToComplex: self andSend: #/.
]

{ #category : #comparing }
Expand Down Expand Up @@ -403,11 +403,24 @@ PMComplex >> asComplex [
^self
]

{ #category : #arithmetic }
PMComplex >> complexConjugate [

^ self class real: real imaginary: imaginary negated
]

{ #category : #arithmetic }
PMComplex >> conjugated [

"Return the complex conjugate of this complex number."

^self class real: real imaginary: imaginary negated
self
deprecated: 'Use #complexConjugate instead'
on: '3 April 2022'
in:
'Pharo-9.0.0+build.1575.sha.9bb5f998e8a6d016ec7abde3ed09c4a60c0b4551 (64 Bit)'.

^ self complexConjugate
]

{ #category : #'mathematical functions' }
Expand Down Expand Up @@ -694,14 +707,15 @@ PMComplex >> sinh [

{ #category : #'mathematical functions' }
PMComplex >> sqrt [

"Return the square root of the receiver with a positive imaginary part."

| u v |
(imaginary = 0 and: [real >= 0])
ifTrue: [^self class real: real sqrt imaginary: 0].
(imaginary = 0 and: [ real >= 0 ]) ifTrue: [
^ self class real: real sqrt imaginary: 0 ].
v := (self abs - real / 2) sqrt.
u := imaginary / 2 / v.
^self class real: u imaginary: v
^ self class real: u imaginary: v
]

{ #category : #'mathematical functions' }
Expand Down
14 changes: 7 additions & 7 deletions src/Math-Complex/PMComplex.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ PMComplex >> productWithVector: aVector [
^ aVector collect: [ :each | each * self ]
]

{ #category : #'*Math-Complex' }
PMComplex >> random [
"analog to Number>>random. However, the only bound is that the abs of the produced complex is less than the length of the receive. The receiver effectively defines a disc within which the random element can be produced."
^ self class random * self

]

{ #category : #'*Math-Complex' }
PMComplex class >> random [
"Answers a random number with abs between 0 and 1."

^ self abs: 1.0 random arg: 2 * Float pi random
]

{ #category : #'*Math-Complex' }
PMComplex >> random [
"analog to Number>>random. However, the only bound is that the abs of the produced complex is less than the length of the receive. The receiver effectively defines a disc within which the random element can be produced."
^ self class random * self

]

{ #category : #'*Math-Complex' }
PMComplex >> subtractToPolynomial: aPolynomial [
^ aPolynomial addNumber: self negated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ PMDualNumberTest >> testConjugated [
self assert: a value equals: z value absSquared.
self
assert: a eps
equals: z eps asComplex conjugated * z value + (z value asComplex conjugated * z eps)
equals: z eps asComplex complexConjugate * z value + (z value asComplex conjugated * z eps)
]

{ #category : #tests }
Expand Down
16 changes: 9 additions & 7 deletions src/Math-Tests-Complex/PMComplexTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,14 @@ PMComplexTest >> testComplexCollection [
]

{ #category : #tests }
PMComplexTest >> testConjugated [
| c cc |
c := 5 - 6 i.
cc := c conjugated.
self assert: cc real equals: c real.
self assert: cc imaginary equals: c imaginary negated
PMComplexTest >> testComplexConjugate [
| z complexConjugateOfZ expected |
z := 5 - 6 i.

complexConjugateOfZ := z complexConjugate .

expected := 5 + 6 i.
self assert: complexConjugateOfZ equals: expected.
]

{ #category : #tests }
Expand Down Expand Up @@ -486,7 +488,7 @@ PMComplexTest >> testNumberAsComplex [
minusOne := -1 asComplex.
self assert: minusOne real equals: -1.
self assert: minusOne imaginary equals: 0.
self assert: minusOne conjugated equals: minusOne.
self assert: minusOne complexConjugate equals: minusOne.
self assert: minusOne sqrt equals: 1 i
]

Expand Down

0 comments on commit 33bfd2c

Please sign in to comment.