Skip to content

Commit 34ac92b

Browse files
committed
Removed some more references to random and cleaned up the code
1 parent 5c400fd commit 34ac92b

File tree

9 files changed

+102
-71
lines changed

9 files changed

+102
-71
lines changed

src/Math-Core/PMVector.class.st

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ Class {
2727
PMVector class >> new: size random: maxRandomNumber [
2828
"Answer an instance of me, with number of elements equal to size, each
2929
a randomNumber lower than maxRandomNumber ."
30+
31+
| random |
32+
random := Random new.
3033

31-
^self newFrom: ( (1 to: size ) collect: [:e|maxRandomNumber random ] )
34+
^ self newFrom: ((1 to: size ) collect: [ :each |
35+
random nextBetween: 0 and: maxRandomNumber ]).
3236
]
3337

3438
{ #category : #private }
@@ -43,10 +47,14 @@ PMVector class >> ones: anInteger [
4347
{ #category : #private }
4448
PMVector class >> randomSize: anInteger maxNumber: aMaxNumber [
4549
"Creates a vector of rand numbers from 0 to aMaxNumber."
46-
|a|
47-
a := PMVector new: anInteger.
48-
1 to: a size do: [ :n | a at: n put: (aMaxNumber random)].
49-
^a
50+
| random vector |
51+
random := Random new.
52+
53+
vector := PMVector new: anInteger.
54+
1 to: vector size do: [ :i |
55+
vector at: i put: (random nextBetween: 0 and: aMaxNumber)].
56+
57+
^ vector
5058
]
5159

5260
{ #category : #private }

src/Math-Matrix/PMMatrix.class.st

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,16 @@ PMMatrix class >> rows: nRows columns: nCols element: fillElement [
146146
]
147147

148148
{ #category : #'as yet unclassified' }
149-
PMMatrix class >> rows: rows columns: columns random: aMaxNumber [
149+
PMMatrix class >> rows: aNumberOfRows columns: aNumberOfColumns random: aMaxNumber [
150150
"Answer a new Matrix of the given dimensions filled with random numbers"
151-
|a b|
152-
a:= (1 to: rows) collect: [:row |b:=PMVector new:columns .
153-
1 to: columns do: [:column |
154-
b at: column put: (aMaxNumber random)].
155-
b].
156-
^PMMatrix rows: a
151+
| random rows |
152+
random := Random new.
153+
154+
rows := (1 to: aNumberOfRows) collect: [ :i |
155+
(1 to: aNumberOfColumns) collect: [ :j |
156+
random nextBetween: 0 and: aMaxNumber ] ].
157+
158+
^ self rows: rows
157159
]
158160

159161
{ #category : #'instance creation' }

src/Math-Matrix/PMSymmetricMatrix.class.st

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,21 @@ aBlock value: RowposRespectivelyColpos value: ColposRespectivelyRowpos"
5858
{ #category : #'as yet unclassified' }
5959
PMSymmetricMatrix class >> new: dim random: aMaxNumber [
6060
"Answer a new symmetric matrix of the given dimensions filled with random numbers"
61-
|a aRow|
62-
a:=self new:dim .
63-
1 to: dim do:[:i|a rowAt: 1 columnAt: i put: (aMaxNumber random)].
64-
2 to: dim do: [:rowPos|
61+
| matrix random aRow |
62+
63+
matrix := self new: dim.
64+
random := Random new.
65+
66+
1 to: dim do: [ :i |
67+
matrix rowAt: 1 columnAt: i put: (random nextBetween: 0 and: aMaxNumber)].
68+
69+
2 to: dim do: [ :j |
6570
aRow :=PMVector new: dim .
66-
1 to: rowPos -1 do:[:pos| aRow at: pos put: (a rowAt: pos columnAt: rowPos) ] .
67-
rowPos to: dim do:[:pos| aRow at: pos put: (aMaxNumber random) ].
68-
(a rows) at: rowPos put: aRow ] .
69-
^a
71+
72+
1 to: j - 1 do: [ :i | aRow at: i put: (matrix rowAt: i columnAt: j) ].
73+
j to: dim do: [ :i | aRow at: i put: (random nextBetween: 0 and: aMaxNumber) ].
74+
(matrix rows) at: j put: aRow ] .
75+
^ matrix
7076
]
7177

7278
{ #category : #'as yet unclassified' }

src/Math-TSNE/PMTSNE.class.st

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -438,16 +438,17 @@ PMTSNE >> initializeYWithRandomValues [
438438
"Answer a new Matrix Y with the number of rows of x and number of columns ndims filled with random numbers following a normal distribution (0,1)"
439439
"We should add this to PMMatrix API later"
440440

441-
| a b rows columns d |
442-
rows := x dimension x.
443-
columns := outputDims.
444-
d := PMNormalDistribution new:0 sigma: 1.
445-
a := (1 to: rows)
446-
collect: [ :row |
447-
b := PMVector new: columns.
448-
1 to: columns do: [ :column | b at: column put: d random ].
449-
b ].
450-
y := PMMatrix rows: a
441+
| numberOfRows numberOfColumns distribution rows |
442+
443+
numberOfRows := x dimension x.
444+
numberOfColumns := outputDims.
445+
distribution := PMNormalDistribution new: 0 sigma: 1.
446+
447+
rows := (1 to: numberOfRows) collect: [ :i |
448+
(1 to: numberOfColumns) collect: [ :j |
449+
distribution random ] ].
450+
451+
y := PMMatrix rows: rows
451452
]
452453

453454
{ #category : #accessing }

src/Math-Tests-FastFourierTransform/PMFFTTest.class.st

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,22 @@ PMFFTTest >> testFastFourierTransformCalculatesTheDiscreteFourierTransformWithou
6666
{ #category : #tests }
6767
PMFFTTest >> testFrequencyDomainRepresentationMatchesRandomizedInputSignalClosely [
6868

69-
| cosineWaveSignal randomizedCosineWaveSignal f |
69+
| random cosineWaveSignal randomizedCosineWaveSignal fourier |
70+
7071
cosineWaveSignal := self generateCosineWaveSignal: 256.
71-
randomizedCosineWaveSignal := cosineWaveSignal collect: [ :i | i + 0.001 random - 0.0005 ].
72-
f := PMFastFourierTransform data: randomizedCosineWaveSignal.
73-
74-
f transform.
75-
f chop: 0.01.
76-
f inverseTransform.
77-
78-
self assert: (f realData - cosineWaveSignal) abs max < 4e-5
72+
random := Random new.
73+
74+
randomizedCosineWaveSignal := cosineWaveSignal collect: [ :i |
75+
i + (random nextBetween: 0 and: 0.001) - 0.0005 ].
76+
77+
fourier := PMFastFourierTransform data: randomizedCosineWaveSignal.
78+
79+
fourier transform.
80+
fourier chop: 0.01.
81+
fourier inverseTransform.
82+
83+
fourier realData with: cosineWaveSignal do: [ :expected :actual |
84+
self assert: actual closeTo: expected precision: 4e-5 ].
7985
]
8086

8187
{ #category : #tests }

src/Math-Tests-KolmogorovSmirnov/PMKolmogorovSmirnov1SampleTest.class.st

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,51 @@ Class {
22
#name : #PMKolmogorovSmirnov1SampleTest,
33
#superclass : #TestCase,
44
#instVars : [
5-
'nd',
6-
'ks'
5+
'distribution',
6+
'test'
77
],
88
#category : #'Math-Tests-KolmogorovSmirnov'
99
}
1010

1111
{ #category : #running }
1212
PMKolmogorovSmirnov1SampleTest >> numberOfRejectionsFor: aDistribution [
1313
| n |
14-
ks populationDistribution: aDistribution.
14+
test populationDistribution: aDistribution.
1515
n := 0.
1616
1 to: 100 do: [ :j |
17-
ks data: ((1 to: 300) collect: [ :i | nd random ]).
18-
(ks rejectEqualityHypothesisWithAlpha: 0.05)
17+
test data: ((1 to: 300) collect: [ :i | distribution random ]).
18+
(test rejectEqualityHypothesisWithAlpha: 0.05)
1919
ifTrue: [ n := n + 1 ] ].
2020
^ n
2121
]
2222

2323
{ #category : #running }
2424
PMKolmogorovSmirnov1SampleTest >> setUp [
2525
super setUp .
26-
nd := PMNormalDistribution new.
27-
ks := PMKolmogorovSmirnov1Sample new
26+
distribution := PMNormalDistribution new.
27+
test := PMKolmogorovSmirnov1Sample new
2828
]
2929

3030
{ #category : #tests }
3131
PMKolmogorovSmirnov1SampleTest >> testCorrectPopulationProbabilistic [
3232
"is a probabilistic test that occasionally fails, but it should happen rarely"
3333

34-
| d |
35-
d := self numberOfRejectionsFor: (PMNormalDistribution new: 0 sigma: 1).
36-
self assert: d < 20
34+
| numberOfRejections |
35+
numberOfRejections := self numberOfRejectionsFor: (PMNormalDistribution new: 0 sigma: 1).
36+
self assert: numberOfRejections < 20
3737
]
3838

3939
{ #category : #tests }
4040
PMKolmogorovSmirnov1SampleTest >> testRejectOfEqualityHypothesesForSampleVersusDistribution [
4141
| sample |
4242
"The data below are taken from http://www.maths.qmul.ac.uk/~bb/CTS_Chapter3_Students.pdf"
4343
sample := #(-1.2 0.2 -0.6 0.8 -1.0).
44-
ks := PMKolmogorovSmirnov1Sample
44+
test := PMKolmogorovSmirnov1Sample
4545
compareData: sample
46-
withDistribution: nd.
46+
withDistribution: distribution.
4747

4848
self
49-
assert: (ks rejectEqualityHypothesisWithAlpha: 0.05)
49+
assert: (test rejectEqualityHypothesisWithAlpha: 0.05)
5050
equals: false
5151
]
5252

src/Math-Tests-Matrix/PMAdditionalTest.class.st

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ here are tests that would be in Math-Tests-DHB-Numerical, if it could construct
55
Class {
66
#name : #PMAdditionalTest,
77
#superclass : #TestCase,
8-
#category : 'Math-Tests-Matrix'
8+
#category : #'Math-Tests-Matrix'
99
}
1010

1111
{ #category : #tests }
@@ -17,18 +17,18 @@ PMAdditionalTest >> testMatrixInversionSmall [
1717
3
1818
timesRepeat: [ [ m := PMSymmetricMatrix new: 5 random: 20.
1919
m determinant = 0 ] whileTrue. "singular matrix not allowed"
20-
self assert: (i := m crlInverse) * m equals: c.
20+
self assert: (i := m crlInverse) * m closeTo: c.
2121
self assert: i class equals: PMSymmetricMatrix.
22-
self assert: (i := m inversePureLUP) * m equals: c.
22+
self assert: (i := m inversePureLUP) * m closeTo: c.
2323
self assert: i class equals: PMSymmetricMatrix.
24-
self assert: m * (i := m inversePureCRL) equals: c.
24+
self assert: m * (i := m inversePureCRL) closeTo: c.
2525
self assert: i class equals: PMSymmetricMatrix ].
2626
3
2727
timesRepeat: [ [ m := PMMatrix rows: 5 columns: 5 random: 20.
2828
m determinant = 0 ] whileTrue.
29-
self assert: m * (i := m inverse) equals: c.
29+
self assert: m * (i := m inverse) closeTo: c.
3030
self assert: i class equals: PMMatrix.
31-
self assert: (i := m inversePureCRL) * m equals: c.
31+
self assert: (i := m inversePureCRL) * m closeTo: c.
3232
self assert: i class equals: PMMatrix ]
3333
]
3434

src/Math-Tests-Matrix/PMQRTest.class.st

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Class {
22
#name : #PMQRTest,
33
#superclass : #TestCase,
4-
#category : 'Math-Tests-Matrix'
4+
#category : #'Math-Tests-Matrix'
55
}
66

77
{ #category : #running }
@@ -84,16 +84,20 @@ PMQRTest >> testQRFactorization [
8484

8585
{ #category : #tests }
8686
PMQRTest >> testRank [
87-
| a i |
88-
i := 0.
89-
[ a := PMMatrix rows: 5 columns: 7 random: 5.0.
90-
a rank = 5
91-
ifTrue: [ a atRow: 2 put: (a rowAt: 1) + (3.0 random * (a rowAt: 3)).
92-
a atRow: 4 put: (0.5 + 3.0 random) * (a rowAt: 5).
93-
i := i + 1.
94-
self assert: a rank equals: 3.
95-
self assert: a transpose rank equals: 3 ].
96-
i < 10 ] whileTrue
87+
| random randomNumber matrix |
88+
random := Random new.
89+
90+
10 timesRepeat: [
91+
matrix := PMMatrix rows: 5 columns: 7 random: 5.0.
92+
matrix rank = 5 ifTrue: [
93+
randomNumber := random nextBetween: 0 and: 3.
94+
matrix atRow: 2 put: (matrix rowAt: 1) + (randomNumber * (matrix rowAt: 3)).
95+
96+
randomNumber := random nextBetween: 0 and: 3.
97+
matrix atRow: 4 put: (0.5 + randomNumber) * (matrix rowAt: 5).
98+
99+
self assert: matrix rank equals: 3.
100+
self assert: matrix transpose rank equals: 3 ] ].
97101
]
98102

99103
{ #category : #tests }

src/Math-Tests-Numerical/PMStatisticsBugs.class.st

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ PMStatisticsBugs >> testProbabilityDensity [
5151

5252
"testing this method usually produces (not always and not always the same) three different errors: 'zerodivide', ' 'Function''s derivative seems to be zero everywhere' and 'Supplied derivative is not correct', the latter happens in other cases than the documented one too, i just dont remember which ones they are and i was too lazy to construct them, one example is enough"
5353

54-
| a b |
54+
| a b random |
5555
a := PMNormalDistribution new: -20 sigma: 0.7.
5656
self assert: (a inverseDistributionValue: 0.5) equals: -20.
5757
"ocassionally 'Supplied derivative is not correct'"
@@ -354,7 +354,11 @@ self assert: ((a distributionValue: (a inverseDistributionValue: 0.9))equalsTo:
354354
"self assert: ((a distributionValue: (a inverseDistributionValue: 1.0))equalsTo: 1)."
355355
"zerod"
356356
a := PMHistogram new.
357-
20 timesRepeat: [ a accumulate: 7.0 random ].
357+
random := Random new.
358+
359+
20 timesRepeat: [
360+
a accumulate: (random nextBetween: 0 and: 7) ].
361+
358362
b := PMHistogrammedDistribution histogram: a.
359363
self
360364
assert:

0 commit comments

Comments
 (0)