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

Add tests for complex numbers #325

Merged
merged 23 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
af69a66
test: added a test for positive integer.
hemalvarambhia Jan 11, 2024
ccd3076
test: added some missing tests.
hemalvarambhia Jan 11, 2024
7abf584
refactor: corrected the category.
hemalvarambhia Jan 11, 2024
d64a1bd
test: changed the test data to cos(pi/3) + i sin(pi/3).
hemalvarambhia Jan 15, 2024
f96485e
comment: added documentation that shows how to get the answer using D…
hemalvarambhia Jan 15, 2024
f4d8a3c
test: added another missing test.
hemalvarambhia Jan 15, 2024
a04e512
refactor: made the test name shorter and hopefully more concise.
hemalvarambhia Jan 15, 2024
eca5b1d
fix: corrected the documentation.
hemalvarambhia Jan 15, 2024
abf6a61
test: demonstrated that sending raiseToInteger: with floating point n…
hemalvarambhia Jan 15, 2024
45b4d3c
refactor: clarified the name of a method.
hemalvarambhia Jan 15, 2024
1a0b3ca
refactor: ComplexNumber is a Whole Value and we can assert equality.
hemalvarambhia Jan 15, 2024
fdae061
refactor: Inline Variable as it is not paying its rent.
hemalvarambhia Jan 15, 2024
ede673d
test: simplified the test a little.
hemalvarambhia Jan 16, 2024
baaa8a3
refactor: extracted computations to local variables.
hemalvarambhia Jan 16, 2024
ec19743
refactor: extracted writing complex numbers in polar coordinates to t…
hemalvarambhia Jan 16, 2024
2e882c9
refactor: corrected the name of the test.
hemalvarambhia Jan 16, 2024
94e6d49
refactor: renamed the test to make it clear we are writing complex nu…
hemalvarambhia Jan 16, 2024
4ad4f60
refactor: clarified the names of some message arguments.
hemalvarambhia Jan 17, 2024
bca0a1f
Removed the comment, as the code is self-explanatory.
hemalvarambhia Jan 17, 2024
91af93f
refactor: Rename Variable where we clarify the name of a variable.
hemalvarambhia Jan 17, 2024
9aaedf0
chore: corrected the name of the comment.
hemalvarambhia Jan 17, 2024
a9a955e
refactor/test: removed a comment, and added a missing test.
hemalvarambhia Jan 17, 2024
b04f7c9
test: added another missing test.
hemalvarambhia Jan 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
hernanmd marked this conversation as resolved.
Show resolved Hide resolved
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.
]