Skip to content

Commit

Permalink
Merge pull request #325 from PolyMathOrg/refactor-complex-number
Browse files Browse the repository at this point in the history
Add tests for complex numbers
  • Loading branch information
hernanmd authored Jan 18, 2024
2 parents 11c8e01 + b04f7c9 commit 8555b80
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 37 deletions.
22 changes: 11 additions & 11 deletions src/Math-Complex/PMComplexNumber.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -621,18 +621,18 @@ PMComplexNumber >> productWithVector: aVector [
]

{ #category : #'mathematical functions' }
PMComplexNumber >> raisedTo: aNumber [
"Answer the receiver raised to aNumber."
PMComplexNumber >> raisedTo: index [
"Answer the receiver raised to the power of an index."

aNumber isInteger ifTrue: [ "Do the special case of integer power" ^ self raisedToInteger: aNumber ].
index isInteger ifTrue: [ ^ self raisedToInteger: index ].

0 = aNumber ifTrue: [ ^ self class one ]. "Special case of exponent=0"
1 = aNumber ifTrue: [ ^ self ]. "Special case of exponent=1"
0 = self ifTrue: [ "Special case of self = 0"
^ aNumber < 0
0 = index ifTrue: [ ^ self class one ].
1 = index ifTrue: [ ^ self ].
0 = self ifTrue: [
^ index < 0
ifTrue: [ (ZeroDivide dividend: self) signal ]
ifFalse: [ self ] ].
^ (aNumber * self ln) exp "Otherwise use logarithms"
^ (index * self ln) exp "Otherwise use logarithms"
]

{ #category : #'mathematical functions' }
Expand Down Expand Up @@ -672,10 +672,10 @@ PMComplexNumber >> real [
]

{ #category : #private }
PMComplexNumber >> real: aNumber1 imaginary: aNumber2 [
PMComplexNumber >> real: realPart imaginary: imaginaryPart [
"Private - initialize the real and imaginary parts of a Complex"
real := aNumber1.
imaginary := aNumber2
real := realPart.
imaginary := imaginaryPart
]

{ #category : #arithmetic }
Expand Down
126 changes: 100 additions & 26 deletions src/Math-Tests-Complex/PMComplexNumberTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ PMComplexNumberTest >> testAbs [
"self debug: #testAbs"

| c |
c := 6 i: -6.
c := 6 - 6 i.
self assert: c abs equals: 72 sqrt
]

Expand Down Expand Up @@ -206,7 +206,11 @@ PMComplexNumberTest >> testArgumentOfPureNegativeImaginaryNumber [

{ #category : #'testing - bugs' }
PMComplexNumberTest >> testBug1 [
self assert: (0.5 * (2 + 0 i) ln) exp equals: (0.5 * 2 ln) exp
| logOfRootTwo logRootTwo |
logOfRootTwo := (0.5 * (2 + 0 i) ln).
self assert: logOfRootTwo exp closeTo: 2 sqrt + 0.0 i.
logRootTwo := (0.5 * 2 ln).
self assert: logOfRootTwo exp equals: logRootTwo exp
]

{ #category : #'testing - close to' }
Expand Down Expand Up @@ -304,26 +308,6 @@ PMComplexNumberTest >> testCosh2MinusSinh2 [
self assert: (c cosh squared - c sinh squared) imaginary closeTo: 0.0 ] ]
]

{ #category : #'testing - expressing complex numbers' }
PMComplexNumberTest >> testCreation [
| c |
c := 5 i.
self assert: c real equals: 0.
self assert: c imaginary equals: 5.
c := 6 + 7 i.
self assert: c real equals: 6.
self assert: c imaginary equals: 7.
c := 5.6 - 8 i.
self assert: c real equals: 5.6.
self assert: c imaginary equals: -8.
c := PMComplexNumber real: 10 imaginary: 5.
self assert: c real equals: 10.
self assert: c imaginary equals: 5.
c := PMComplexNumber abs: 5 arg: Float pi / 2.
self assert: c real rounded equals: 0.
self assert: c imaginary equals: 5
]

{ #category : #'testing - arithmetic' }
PMComplexNumberTest >> testDividingALargeComplexNumbersByItself [
| c1 c2 quotient |
Expand Down Expand Up @@ -637,6 +621,15 @@ PMComplexNumberTest >> testRaisedTo [
self assert: c3 imaginary closeTo: c imaginary
]

{ #category : #'testing - mathematical functions' }
PMComplexNumberTest >> testRaisedToFractionalPower [

| z expected |
z := 0 + 1 i.
expected := 3 sqrt / 2 + (1 / 2) i.
self assert: (z raisedTo: 1 / 3) closeTo: expected
]

{ #category : #'testing - mathematical functions' }
PMComplexNumberTest >> testRaisedToInteger [
| c c3 |
Expand All @@ -646,6 +639,44 @@ PMComplexNumberTest >> testRaisedToInteger [
self assert: c3 reciprocal equals: (c raisedToInteger: -3)
]

{ #category : #'testing - mathematical functions' }
PMComplexNumberTest >> testRaisedToIntegerWithNonIntegersRaisesAnError [
|z|
z := 5 - 9 i.
self should: [ z raisedToInteger: 3.0 ] raise: ArithmeticError .
]

{ #category : #'testing - mathematical functions' }
PMComplexNumberTest >> testRaisedToNegativeInteger [
"
Suppose z = cos(pi / 3) + i sin(pi / 3). By De Moivre's theorem, z**-3 is
z ** 3 = cos(-3 pi / 3) + i sin(-3 pi / 3) = cos(-pi) + sin(pi) = cos(pi) - i sin(pi)
z ** 3 = -1 + 0 i
"
| z |
z := (1 / 2) + (3 sqrt / 2) i.
self assert: (z raisedTo: -3) closeTo: (-1 + 0 i).
]

{ #category : #'testing - mathematical functions' }
PMComplexNumberTest >> testRaisedToPositiveInteger [
| z zCubed |
"
Suppose z = cos(pi / 6) + i sin(pi / 6). By De Moivre's theorem, z**3 is
z ** 3 = cos(3 pi / 6) + i sin(3 pi / 6) = cos(pi / 2) + sin(pi / 2) = 0 + i
"
z := (3 sqrt / 2) + (1 / 2) i.
zCubed := (z raisedTo: 3) .
self assert: zCubed closeTo: (0 + 1 i).
]

{ #category : #'testing - mathematical functions' }
PMComplexNumberTest >> testRaisingZeroToThePowerOfNegativeIndex [
| zero |
zero := PMComplexNumber zero.
self should: [ zero raisedTo: -4 ] raise: ZeroDivide
]

{ #category : #tests }
PMComplexNumberTest >> testRandom [
| random c r |
Expand Down Expand Up @@ -837,11 +868,10 @@ PMComplexNumberTest >> testSquareRootOfZeroIsZero [

{ #category : #'testing - mathematical functions' }
PMComplexNumberTest >> testSquared [
| c c2 |

| c |
c := 6 - 6 i.
c2 := c squared.
self assert: c2 imaginary equals: -72.
self assert: c2 real equals: 0
self assert: c squared equals: 0 - 72 i
]

{ #category : #'testing - arithmetic' }
Expand Down Expand Up @@ -897,6 +927,32 @@ PMComplexNumberTest >> testTwoComplexNumbersWithDifferentRealPartsAreNotEqual [
self deny: z equals: w
]

{ #category : #'testing - expressing complex numbers' }
PMComplexNumberTest >> testWritingComplexNumbersInCartesianCoordinates [
| c |
c := 5 i.
self assert: c real equals: 0.
self assert: c imaginary equals: 5.
c := 6 + 7 i.
self assert: c real equals: 6.
self assert: c imaginary equals: 7.
c := 5.6 - 8 i.
self assert: c real equals: 5.6.
self assert: c imaginary equals: -8.
c := PMComplexNumber real: 10 imaginary: 5.
self assert: c real equals: 10.
self assert: c imaginary equals: 5.
]

{ #category : #'testing - expressing complex numbers' }
PMComplexNumberTest >> testWritingComplexNumbersInPolarCoordinates [
| c |

c := PMComplexNumber abs: 5 arg: Float pi / 2.
self assert: c real rounded equals: 0.
self assert: c imaginary equals: 5
]

{ #category : #'testing - expressing complex numbers' }
PMComplexNumberTest >> testWritingComplexNumbersWhoseRealAndImaginaryPartsAreFractions [
| z |
Expand Down Expand Up @@ -926,3 +982,21 @@ PMComplexNumberTest >> testZeroComplexNumberIsEqualToIntegerZero [
PMComplexNumberTest >> testZeroComplexNumbersDoNotHaveAReciprocal [
self should: [ PMComplexNumber zero reciprocal ] raise: ZeroDivide
]

{ #category : #'testing - mathematical functions' }
PMComplexNumberTest >> testZeroRaisedToThePowerOfZero [

| zeroRaisedToZero |
zeroRaisedToZero := (PMComplexNumber zero) raisedTo: 0.
self assert: zeroRaisedToZero equals: PMComplexNumber one.
]

{ #category : #tests }
PMComplexNumberTest >> testZeroToThePowerOfZero [
"comment stating purpose of instance-side method"
"scope: class-variables & instance-variables"

| zero |
zero := PMComplexNumber zero.
self assert: (zero raisedTo: 0) equals: PMComplexNumber one.
]

0 comments on commit 8555b80

Please sign in to comment.