From 7342d751bfd8314715f208966ca6bba640b290c9 Mon Sep 17 00:00:00 2001 From: Oleksandr Zaitsev Date: Sat, 23 Apr 2022 04:52:51 +0200 Subject: [PATCH] Fixed #247. Added complexConjugate to Number and removed isCompleConjugateOf: --- src/Math-Complex/Number.extension.st | 17 +++--- src/Math-Complex/PMComplex.class.st | 10 ++-- src/Math-Complex/PMComplex.extension.st | 14 ++--- src/Math-Matrix/PMMatrix.class.st | 2 +- .../NumberTest.extension.st | 7 +++ src/Math-Tests-Complex/PMComplexTest.class.st | 54 ------------------- src/Math-Tests-Complex/PMNumberTest.class.st | 30 ----------- 7 files changed, 29 insertions(+), 105 deletions(-) create mode 100644 src/Math-Tests-Complex/NumberTest.extension.st delete mode 100644 src/Math-Tests-Complex/PMNumberTest.class.st diff --git a/src/Math-Complex/Number.extension.st b/src/Math-Complex/Number.extension.st index 10805751f..93b550dcf 100644 --- a/src/Math-Complex/Number.extension.st +++ b/src/Math-Complex/Number.extension.st @@ -15,7 +15,12 @@ Number >> asComplex [ { #category : #'*Math-Complex' } Number >> complexConjugate [ - ^ self. + "The complex conjugate of a complex number (a + bi) is another complex number (a - bi). + Every real number x is also a complex number with imaginary part equal to 0. + In other words, x = x + 0i and x = x - 0i. + Therefore, the complex conjugate of a real number is the same real number" + + ^ self ] { #category : #'*Math-Complex' } @@ -37,13 +42,11 @@ Number >> i: aNumber [ { #category : #'*Math-Complex' } Number >> isComplexConjugateOf: aNumber [ "A complex conjugate of a real number is same real number" - ^ self = aNumber -] + self + deprecated: 'This method is redundant. Just check the equality with complexConjugate' + transformWith: '`@rec isComplexConjugate: `@arg' -> '`@rec complexConjugate = `@arg'. -{ #category : #'*Math-Complex' } -Number >> isComplexConjugateOfAComplexNumber: aComplexNumber [ - "A complex conjugate of a real number is same real number" - ^ self isComplexConjugateOf: aComplexNumber + ^ self complexConjugate = aNumber ] { #category : #'*Math-Complex' } diff --git a/src/Math-Complex/PMComplex.class.st b/src/Math-Complex/PMComplex.class.st index 50593e8bd..e373e4cf6 100644 --- a/src/Math-Complex/PMComplex.class.st +++ b/src/Math-Complex/PMComplex.class.st @@ -538,13 +538,11 @@ PMComplex >> imaginary [ { #category : #testing } PMComplex >> isComplexConjugateOf: aNumber [ "Answer true if self and aNumber are complex conjugates of each other. The complex conjugate of a complex number is the number with an equal real part and an imaginary part equal in magnitude but opposite in sign." - ^ aNumber isComplexConjugateOfAComplexNumber: self -] + self + deprecated: 'This method is redundant. Just check the equality with complexConjugate' + transformWith: '`@rec isComplexConjugate: `@arg' -> '`@rec complexConjugate = `@arg'. -{ #category : #testing } -PMComplex >> isComplexConjugateOfAComplexNumber: aComplexNumber [ - "Answer true if self and aComplexNumber are complex conjugates of each other. The complex conjugate of a complex number is the number with an equal real part and an imaginary part equal in magnitude but opposite in sign." - ^ (aComplexNumber real = real) and: [ aComplexNumber imaginary = (-1 * imaginary) ] + ^ self complexConjugate = aNumber ] { #category : #testing } diff --git a/src/Math-Complex/PMComplex.extension.st b/src/Math-Complex/PMComplex.extension.st index efe456833..5cf918b4d 100644 --- a/src/Math-Complex/PMComplex.extension.st +++ b/src/Math-Complex/PMComplex.extension.st @@ -17,13 +17,6 @@ PMComplex >> productWithVector: aVector [ ^ aVector collect: [ :each | each * 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." @@ -31,6 +24,13 @@ PMComplex >> random [ ] +{ #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 >> subtractToPolynomial: aPolynomial [ ^ aPolynomial addNumber: self negated diff --git a/src/Math-Matrix/PMMatrix.class.st b/src/Math-Matrix/PMMatrix.class.st index 7e56672a5..6f1c4803d 100644 --- a/src/Math-Matrix/PMMatrix.class.st +++ b/src/Math-Matrix/PMMatrix.class.st @@ -567,7 +567,7 @@ PMMatrix >> isHermitian [ 1 to: self numberOfRows do: [ :i | 1 to: (i - 1) do: [ :j | - ((self at: i at: j) isComplexConjugateOf: (self at: j at: i)) + ((self at: i at: j) complexConjugate = (self at: j at: i)) ifFalse: [ ^ false ] ] ]. ^ true diff --git a/src/Math-Tests-Complex/NumberTest.extension.st b/src/Math-Tests-Complex/NumberTest.extension.st new file mode 100644 index 000000000..ae88d8d89 --- /dev/null +++ b/src/Math-Tests-Complex/NumberTest.extension.st @@ -0,0 +1,7 @@ +Extension { #name : #NumberTest } + +{ #category : #'*Math-Tests-Complex' } +NumberTest >> testComplexConjugate [ + + self assert: 5 complexConjugate equals: 5 +] diff --git a/src/Math-Tests-Complex/PMComplexTest.class.st b/src/Math-Tests-Complex/PMComplexTest.class.st index e0ec78b8b..3fcd0b93a 100644 --- a/src/Math-Tests-Complex/PMComplexTest.class.st +++ b/src/Math-Tests-Complex/PMComplexTest.class.st @@ -389,60 +389,6 @@ PMComplexTest >> testHash [ self assert: aComplex copy hash equals: aComplex hash ] -{ #category : #tests } -PMComplexTest >> testIsComplexConjugateOfConjugateComplex [ - - self assert: ((3 + 2i) isComplexConjugateOf: (3 - 2i)) -] - -{ #category : #tests } -PMComplexTest >> testIsComplexConjugateOfConjugateComplexAndReal [ - - self assert: ((5 + 0i) isComplexConjugateOf: 5) -] - -{ #category : #tests } -PMComplexTest >> testIsComplexConjugateOfConjugateRealAndComplex [ - - self assert: (5 isComplexConjugateOf: (5 - 0i)) -] - -{ #category : #tests } -PMComplexTest >> testIsComplexConjugateOfDifferentReal [ - - self deny: (-5 isComplexConjugateOf: 5) -] - -{ #category : #tests } -PMComplexTest >> testIsComplexConjugateOfNonConjugateComplexAndReal [ - - self deny: ((5 + 3i) isComplexConjugateOf: 5) -] - -{ #category : #tests } -PMComplexTest >> testIsComplexConjugateOfNonConjugateDifferentComplex [ - - self deny: ((-0.5 - 1i) isComplexConjugateOf: (3 - 2i)) -] - -{ #category : #tests } -PMComplexTest >> testIsComplexConjugateOfNonConjugateRealAndComplex [ - - self deny: (5 isComplexConjugateOf: (5 - 3i)) -] - -{ #category : #tests } -PMComplexTest >> testIsComplexConjugateOfSameComplex [ - - self deny: ((3 - 2i) isComplexConjugateOf: (3 - 2i)) -] - -{ #category : #tests } -PMComplexTest >> testIsComplexConjugateOfSameReal [ - - self assert: (5 isComplexConjugateOf: 5) -] - { #category : #tests } PMComplexTest >> testIsComplexNumberOnComplex [ diff --git a/src/Math-Tests-Complex/PMNumberTest.class.st b/src/Math-Tests-Complex/PMNumberTest.class.st deleted file mode 100644 index bbed9c0e8..000000000 --- a/src/Math-Tests-Complex/PMNumberTest.class.st +++ /dev/null @@ -1,30 +0,0 @@ -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 -]