Skip to content

Commit 8555b80

Browse files
authored
Merge pull request #325 from PolyMathOrg/refactor-complex-number
Add tests for complex numbers
2 parents 11c8e01 + b04f7c9 commit 8555b80

File tree

2 files changed

+111
-37
lines changed

2 files changed

+111
-37
lines changed

src/Math-Complex/PMComplexNumber.class.st

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -621,18 +621,18 @@ PMComplexNumber >> productWithVector: aVector [
621621
]
622622

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

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

629-
0 = aNumber ifTrue: [ ^ self class one ]. "Special case of exponent=0"
630-
1 = aNumber ifTrue: [ ^ self ]. "Special case of exponent=1"
631-
0 = self ifTrue: [ "Special case of self = 0"
632-
^ aNumber < 0
629+
0 = index ifTrue: [ ^ self class one ].
630+
1 = index ifTrue: [ ^ self ].
631+
0 = self ifTrue: [
632+
^ index < 0
633633
ifTrue: [ (ZeroDivide dividend: self) signal ]
634634
ifFalse: [ self ] ].
635-
^ (aNumber * self ln) exp "Otherwise use logarithms"
635+
^ (index * self ln) exp "Otherwise use logarithms"
636636
]
637637

638638
{ #category : #'mathematical functions' }
@@ -672,10 +672,10 @@ PMComplexNumber >> real [
672672
]
673673

674674
{ #category : #private }
675-
PMComplexNumber >> real: aNumber1 imaginary: aNumber2 [
675+
PMComplexNumber >> real: realPart imaginary: imaginaryPart [
676676
"Private - initialize the real and imaginary parts of a Complex"
677-
real := aNumber1.
678-
imaginary := aNumber2
677+
real := realPart.
678+
imaginary := imaginaryPart
679679
]
680680

681681
{ #category : #arithmetic }

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

Lines changed: 100 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ PMComplexNumberTest >> testAbs [
1717
"self debug: #testAbs"
1818

1919
| c |
20-
c := 6 i: -6.
20+
c := 6 - 6 i.
2121
self assert: c abs equals: 72 sqrt
2222
]
2323

@@ -206,7 +206,11 @@ PMComplexNumberTest >> testArgumentOfPureNegativeImaginaryNumber [
206206

207207
{ #category : #'testing - bugs' }
208208
PMComplexNumberTest >> testBug1 [
209-
self assert: (0.5 * (2 + 0 i) ln) exp equals: (0.5 * 2 ln) exp
209+
| logOfRootTwo logRootTwo |
210+
logOfRootTwo := (0.5 * (2 + 0 i) ln).
211+
self assert: logOfRootTwo exp closeTo: 2 sqrt + 0.0 i.
212+
logRootTwo := (0.5 * 2 ln).
213+
self assert: logOfRootTwo exp equals: logRootTwo exp
210214
]
211215

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

307-
{ #category : #'testing - expressing complex numbers' }
308-
PMComplexNumberTest >> testCreation [
309-
| c |
310-
c := 5 i.
311-
self assert: c real equals: 0.
312-
self assert: c imaginary equals: 5.
313-
c := 6 + 7 i.
314-
self assert: c real equals: 6.
315-
self assert: c imaginary equals: 7.
316-
c := 5.6 - 8 i.
317-
self assert: c real equals: 5.6.
318-
self assert: c imaginary equals: -8.
319-
c := PMComplexNumber real: 10 imaginary: 5.
320-
self assert: c real equals: 10.
321-
self assert: c imaginary equals: 5.
322-
c := PMComplexNumber abs: 5 arg: Float pi / 2.
323-
self assert: c real rounded equals: 0.
324-
self assert: c imaginary equals: 5
325-
]
326-
327311
{ #category : #'testing - arithmetic' }
328312
PMComplexNumberTest >> testDividingALargeComplexNumbersByItself [
329313
| c1 c2 quotient |
@@ -637,6 +621,15 @@ PMComplexNumberTest >> testRaisedTo [
637621
self assert: c3 imaginary closeTo: c imaginary
638622
]
639623

624+
{ #category : #'testing - mathematical functions' }
625+
PMComplexNumberTest >> testRaisedToFractionalPower [
626+
627+
| z expected |
628+
z := 0 + 1 i.
629+
expected := 3 sqrt / 2 + (1 / 2) i.
630+
self assert: (z raisedTo: 1 / 3) closeTo: expected
631+
]
632+
640633
{ #category : #'testing - mathematical functions' }
641634
PMComplexNumberTest >> testRaisedToInteger [
642635
| c c3 |
@@ -646,6 +639,44 @@ PMComplexNumberTest >> testRaisedToInteger [
646639
self assert: c3 reciprocal equals: (c raisedToInteger: -3)
647640
]
648641

642+
{ #category : #'testing - mathematical functions' }
643+
PMComplexNumberTest >> testRaisedToIntegerWithNonIntegersRaisesAnError [
644+
|z|
645+
z := 5 - 9 i.
646+
self should: [ z raisedToInteger: 3.0 ] raise: ArithmeticError .
647+
]
648+
649+
{ #category : #'testing - mathematical functions' }
650+
PMComplexNumberTest >> testRaisedToNegativeInteger [
651+
"
652+
Suppose z = cos(pi / 3) + i sin(pi / 3). By De Moivre's theorem, z**-3 is
653+
z ** 3 = cos(-3 pi / 3) + i sin(-3 pi / 3) = cos(-pi) + sin(pi) = cos(pi) - i sin(pi)
654+
z ** 3 = -1 + 0 i
655+
"
656+
| z |
657+
z := (1 / 2) + (3 sqrt / 2) i.
658+
self assert: (z raisedTo: -3) closeTo: (-1 + 0 i).
659+
]
660+
661+
{ #category : #'testing - mathematical functions' }
662+
PMComplexNumberTest >> testRaisedToPositiveInteger [
663+
| z zCubed |
664+
"
665+
Suppose z = cos(pi / 6) + i sin(pi / 6). By De Moivre's theorem, z**3 is
666+
z ** 3 = cos(3 pi / 6) + i sin(3 pi / 6) = cos(pi / 2) + sin(pi / 2) = 0 + i
667+
"
668+
z := (3 sqrt / 2) + (1 / 2) i.
669+
zCubed := (z raisedTo: 3) .
670+
self assert: zCubed closeTo: (0 + 1 i).
671+
]
672+
673+
{ #category : #'testing - mathematical functions' }
674+
PMComplexNumberTest >> testRaisingZeroToThePowerOfNegativeIndex [
675+
| zero |
676+
zero := PMComplexNumber zero.
677+
self should: [ zero raisedTo: -4 ] raise: ZeroDivide
678+
]
679+
649680
{ #category : #tests }
650681
PMComplexNumberTest >> testRandom [
651682
| random c r |
@@ -837,11 +868,10 @@ PMComplexNumberTest >> testSquareRootOfZeroIsZero [
837868

838869
{ #category : #'testing - mathematical functions' }
839870
PMComplexNumberTest >> testSquared [
840-
| c c2 |
871+
872+
| c |
841873
c := 6 - 6 i.
842-
c2 := c squared.
843-
self assert: c2 imaginary equals: -72.
844-
self assert: c2 real equals: 0
874+
self assert: c squared equals: 0 - 72 i
845875
]
846876

847877
{ #category : #'testing - arithmetic' }
@@ -897,6 +927,32 @@ PMComplexNumberTest >> testTwoComplexNumbersWithDifferentRealPartsAreNotEqual [
897927
self deny: z equals: w
898928
]
899929

930+
{ #category : #'testing - expressing complex numbers' }
931+
PMComplexNumberTest >> testWritingComplexNumbersInCartesianCoordinates [
932+
| c |
933+
c := 5 i.
934+
self assert: c real equals: 0.
935+
self assert: c imaginary equals: 5.
936+
c := 6 + 7 i.
937+
self assert: c real equals: 6.
938+
self assert: c imaginary equals: 7.
939+
c := 5.6 - 8 i.
940+
self assert: c real equals: 5.6.
941+
self assert: c imaginary equals: -8.
942+
c := PMComplexNumber real: 10 imaginary: 5.
943+
self assert: c real equals: 10.
944+
self assert: c imaginary equals: 5.
945+
]
946+
947+
{ #category : #'testing - expressing complex numbers' }
948+
PMComplexNumberTest >> testWritingComplexNumbersInPolarCoordinates [
949+
| c |
950+
951+
c := PMComplexNumber abs: 5 arg: Float pi / 2.
952+
self assert: c real rounded equals: 0.
953+
self assert: c imaginary equals: 5
954+
]
955+
900956
{ #category : #'testing - expressing complex numbers' }
901957
PMComplexNumberTest >> testWritingComplexNumbersWhoseRealAndImaginaryPartsAreFractions [
902958
| z |
@@ -926,3 +982,21 @@ PMComplexNumberTest >> testZeroComplexNumberIsEqualToIntegerZero [
926982
PMComplexNumberTest >> testZeroComplexNumbersDoNotHaveAReciprocal [
927983
self should: [ PMComplexNumber zero reciprocal ] raise: ZeroDivide
928984
]
985+
986+
{ #category : #'testing - mathematical functions' }
987+
PMComplexNumberTest >> testZeroRaisedToThePowerOfZero [
988+
989+
| zeroRaisedToZero |
990+
zeroRaisedToZero := (PMComplexNumber zero) raisedTo: 0.
991+
self assert: zeroRaisedToZero equals: PMComplexNumber one.
992+
]
993+
994+
{ #category : #tests }
995+
PMComplexNumberTest >> testZeroToThePowerOfZero [
996+
"comment stating purpose of instance-side method"
997+
"scope: class-variables & instance-variables"
998+
999+
| zero |
1000+
zero := PMComplexNumber zero.
1001+
self assert: (zero raisedTo: 0) equals: PMComplexNumber one.
1002+
]

0 commit comments

Comments
 (0)