Skip to content

Commit

Permalink
Merge pull request #319 from jecisc/better-equality-assertions
Browse files Browse the repository at this point in the history
Better equality assertions
  • Loading branch information
jecisc authored Mar 14, 2023
2 parents ce92f8d + f1e2a10 commit d9a495e
Show file tree
Hide file tree
Showing 53 changed files with 1,881 additions and 1,793 deletions.
9 changes: 7 additions & 2 deletions src/Math-AutomaticDifferenciation/PMDualNumber.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ PMDualNumber >> asInteger [
{ #category : #comparing }
PMDualNumber >> closeTo: aDualNumber [

^ (value closeTo: aDualNumber value) and: [
eps closeTo: aDualNumber eps ]
^ self closeTo: aDualNumber precision: Float defaultComparisonPrecision
]

{ #category : #comparing }
PMDualNumber >> closeTo: aDualNumber precision: aPrecision [

^ (value closeTo: aDualNumber value precision: aPrecision) and: [ eps closeTo: aDualNumber eps precision: aPrecision ]
]

{ #category : #'mathematical functions' }
Expand Down
7 changes: 3 additions & 4 deletions src/Math-AutomaticDifferenciation/PMHyperDualNumber.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,10 @@ PMHyperDualNumber >> arcTan [
]

{ #category : #comparing }
PMHyperDualNumber >> closeTo: aHyperDualNumber [
PMHyperDualNumber >> closeTo: aHyperDualNumber precision: aPrecision [

^ (super closeTo: aHyperDualNumber) and: [
(eps2 closeTo: aHyperDualNumber eps2) and: [
eps1eps2 closeTo: aHyperDualNumber eps1eps2 ] ]
^ (super closeTo: aHyperDualNumber precision: aPrecision) and: [
(eps2 closeTo: aHyperDualNumber eps2 precision: aPrecision) and: [ eps1eps2 closeTo: aHyperDualNumber eps1eps2 precision: aPrecision ] ]
]

{ #category : #'mathematical functions' }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Extension { #name : #SequenceableCollection }

{ #category : #'*Math-CompatibilityUpToPharo11' }
SequenceableCollection >> closeTo: aSequenceableCollection precision: aPrecision [
"Return true if all my elements are close to the elements of same index of the parameter with a certain precision"

"(#(1.9283 2.3029) closeTo: #(1.9284 2.3028) precision: 0.001) >>> true"

self with: aSequenceableCollection do: [ :a :b | (a closeTo: b precision: aPrecision) ifFalse: [ ^ false ] ].
^ true
]
16 changes: 8 additions & 8 deletions src/Math-Tests-Accuracy/PMAccuracyTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,21 @@ PMAccuracyTest >> testIfSeveralterations [

{ #category : #tests }
PMAccuracyTest >> testIterations [

| s d |
a
iterations: 2;
run.
d := a dataTree at: 'names'.
s := (d atPath: #('Aaa' #(1 2) #(5 3) '+dev')) first negated.
self
assert: ((d atPath: #('Aaa' #(1 2) #(5 3) '-dev')) first closeTo: s).
s := d atPath: #('Aaa' #(3 2.8888) #(5 3) 'standard deviation').
s := (d atPath: #( 'Aaa' #( 1 2 ) #( 5 3 ) '+dev' )) first negated.
self assert: (d atPath: #( 'Aaa' #( 1 2 ) #( 5 3 ) '-dev' )) first closeTo: s.
s := d atPath: #( 'Aaa' #( 3 2.8888 ) #( 5 3 ) 'standard deviation' ).
self assert: s first > 0.
self assert: s second equals: 0.
self assert: (d atPath: #('Ddd' 'data')) equals: #(#(2 3) #(2 3)).
s := d atPath: #('Ddd' 'error').
self assert: (s first closeTo: -45).
self assert: (s second closeTo: -26.6666667)
self assert: (d atPath: #( 'Ddd' 'data' )) equals: #( #( 2 3 ) #( 2 3 ) ).
s := d atPath: #( 'Ddd' 'error' ).
self assert: s first closeTo: -45.
self assert: s second closeTo: -26.6666667
]

{ #category : #tests }
Expand Down
71 changes: 27 additions & 44 deletions src/Math-Tests-AutomaticDifferenciation/PMDualNumberTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ PMDualNumberTest >> testAdd [
PMDualNumberTest >> testArcCos [

| a |
self assert:
(zero arcCos closeTo: (PMDualNumber value: Float halfPi eps: -1)).
self assert: zero arcCos closeTo: (PMDualNumber value: Float halfPi eps: -1).
a := (PMDualNumber value: -1.0 successor eps: 1) arcCos.
self assert: (a value closeTo: Float pi).
self assert: a value closeTo: Float pi.
self assert: a eps < -1e6
]

Expand All @@ -69,7 +68,7 @@ PMDualNumberTest >> testArcSin [
| a |
self assert: zero arcSin equals: zero.
a := (PMDualNumber value: 1.0 predecessor eps: 1) arcSin.
self assert: (a value closeTo: Float halfPi).
self assert: a value closeTo: Float halfPi.
self assert: a eps > 1e6.
a := (PMDualNumber value: -0.5 eps: 1) arcSin.
self assert: a value equals: -0.5 arcSin.
Expand All @@ -80,10 +79,8 @@ PMDualNumberTest >> testArcSin [
PMDualNumberTest >> testArcTan [

self assertEquality: zero arcTan and: zero.
self
assertEquality: one negated arcTan
and: (PMDualNumber value: -1 arcTan eps: -1 / 2).
self assert: (three arcTan eps closeTo: 0.1)
self assertEquality: one negated arcTan and: (PMDualNumber value: -1 arcTan eps: -1 / 2).
self assert: three arcTan eps closeTo: 0.1
]

{ #category : #test }
Expand Down Expand Up @@ -117,13 +114,9 @@ PMDualNumberTest >> testConverting [
{ #category : #'tests-mathematical functions' }
PMDualNumberTest >> testCos [

self assert:
((PMDualNumber value: Float halfPi negated eps: 1) cos closeTo:
(PMDualNumber value: 0 eps: 1)).
self assert: ((PMDualNumber value: Float halfPi eps: 1) cos closeTo:
(PMDualNumber value: 0 eps: -1)).
self assert:
((PMDualNumber value: Float halfPi eps: 0) cos closeTo: zeroc)
self assert: (PMDualNumber value: Float halfPi negated eps: 1) cos closeTo: (PMDualNumber value: 0 eps: 1).
self assert: (PMDualNumber value: Float halfPi eps: 1) cos closeTo: (PMDualNumber value: 0 eps: -1).
self assert: (PMDualNumber value: Float halfPi eps: 0) cos closeTo: zeroc
]

{ #category : #'tests-arithmetic' }
Expand Down Expand Up @@ -167,16 +160,13 @@ PMDualNumberTest >> testEqual [
{ #category : #tests }
PMDualNumberTest >> testEqualsTo [

self assert: (zeroc closeTo: zeroc).
self deny: (zeroc closeTo: zero).
self assert: (zero closeTo: zero).
self assert:
(one closeTo: (PMDualNumber value: 1.0000000001 eps: 1.0000000001)).
self deny:
(one closeTo: (PMDualNumber value: 1.0000000001 eps: 1.001)).
self deny:
(one closeTo: (PMDualNumber value: 1.001 eps: 1.0000000001)).
self deny: (one closeTo: (PMDualNumber value: 1.001 eps: 1.001))
self assert: zeroc closeTo: zeroc.
self deny: zeroc closeTo: zero.
self assert: zero closeTo: zero.
self assert: one closeTo: (PMDualNumber value: 1.0000000001 eps: 1.0000000001).
self deny: one closeTo: (PMDualNumber value: 1.0000000001 eps: 1.001).
self deny: one closeTo: (PMDualNumber value: 1.001 eps: 1.0000000001).
self deny: one closeTo: (PMDualNumber value: 1.001 eps: 1.001)
]

{ #category : #'tests-mathematical functions' }
Expand All @@ -185,7 +175,7 @@ PMDualNumberTest >> testExp [
| a b |
b := 3 exp.
a := three exp.
self assert: (a eps closeTo: b).
self assert: a eps closeTo: b.
self assert: a value equals: b.
self assert: one equals: zero exp
]
Expand All @@ -209,7 +199,7 @@ PMDualNumberTest >> testLn [

| a |
a := three ln.
self assert: (a eps closeTo: 1 / 3).
self assert: a eps closeTo: 1 / 3.
self assert: a value equals: 3 ln.
self assert: one ln equals: zero
]
Expand Down Expand Up @@ -259,21 +249,17 @@ PMDualNumberTest >> testRaisedTo [
| a |
self assertEquality: (three raisedTo: 2) and: three squared.
self assertEquality: (three raisedTo: 0) and: onec.
self
assertEquality: (three + one raisedTo: 1 / 2)
and: (PMDualNumber value: 2 eps: 1 / 2).
self assert:
((three + one raisedTo: 3 / 2) closeTo:
(PMDualNumber value: 8 eps: 6)).
self assertEquality: (three + one raisedTo: 1 / 2) and: (PMDualNumber value: 2 eps: 1 / 2).
self assert: (three + one raisedTo: 3 / 2) closeTo: (PMDualNumber value: 8 eps: 6).
self assertEquality: (zero raisedTo: 1.4) and: zeroc.
a := 2 raisedTo: three.
self assert: (a value closeTo: 8).
self assert: (a eps closeTo: 2 ln * (2 raisedTo: 3)).
self assert: a value closeTo: 8.
self assert: a eps closeTo: 2 ln * (2 raisedTo: 3).
self assertEquality: (1 raisedTo: three) and: onec.
self assertEquality: (one raisedTo: one) and: one.
a := three raisedTo: three.
self assert: (a value closeTo: 27).
self assert: (a eps closeTo: (3 raisedTo: 3) * (3 ln + 1))
self assert: a value closeTo: 27.
self assert: a eps closeTo: (3 raisedTo: 3) * (3 ln + 1)
]

{ #category : #'tests-mathematical functions' }
Expand Down Expand Up @@ -303,11 +289,8 @@ PMDualNumberTest >> testReciprocal [
{ #category : #'tests-mathematical functions' }
PMDualNumberTest >> testSin [

self assert:
((PMDualNumber value: Float halfPi negated eps: 1) sin closeTo:
(PMDualNumber value: -1.0 eps: 0.0)).
self assert: ((PMDualNumber value: Float halfPi eps: 1) sin closeTo:
(PMDualNumber value: 1.0 eps: 0.0)).
self assert: (PMDualNumber value: Float halfPi negated eps: 1) sin closeTo: (PMDualNumber value: -1.0 eps: 0.0).
self assert: (PMDualNumber value: Float halfPi eps: 1) sin closeTo: (PMDualNumber value: 1.0 eps: 0.0).
self assertEquality: zero sin and: zero
]

Expand Down Expand Up @@ -359,11 +342,11 @@ PMDualNumberTest >> testTan [
| a b |
a := three tan.
self assert: a value equals: 3 tan.
self assert: (a eps closeTo: 3 tan squared + 1).
self assert: a eps closeTo: 3 tan squared + 1.
b := Float halfPi - 0.000000000001.
a := (PMDualNumber value: b eps: 1) tan.
self assert: a value equals: b tan.
self assert: (a eps closeTo: b tan squared + 1)
self assert: a eps closeTo: b tan squared + 1
]

{ #category : #'tests-testing' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ PMGradientAndHessianTest >> test2 [
g := PMGradient of: f.
(h := PMHessian of: f) value: #( 0 0 ).
r := #( 0.5 0.5 ).
self assert: ((g value: #( 0 0 )) closeTo: r).
self assert: (h gradient closeTo: r).
self assert: (g value: #( 0 0 )) closeTo: r.
self assert: h gradient closeTo: r.
r := (-1 exp + 1 exp) reciprocal.
self assert: ((g value: #( -1 1 )) closeTo: r * #( -1 1 ) exp).
self assert: (h result closeTo:
(PMMatrix rows: #( #( 0.25 -0.25 ) #( -0.25 0.25 ) )))
self assert: (g value: #( -1 1 )) closeTo: r * #( -1 1 ) exp.
self assert: h result closeTo: (PMMatrix rows: #( #( 0.25 -0.25 ) #( -0.25 0.25 ) ))
]
Loading

0 comments on commit d9a495e

Please sign in to comment.