Skip to content

Commit d4c068e

Browse files
authored
Merge pull request #297 from jecisc/speed-up-permutations-generator
Speed up random permutations + allow to set ourself the random generator
2 parents 48e3ac9 + 1d7bf6a commit d4c068e

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

src/Math-Permutation/PMPermutation.class.st

+26-17
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,20 @@ PMPermutation class >> fromCycles: aCollectionofCollections [
5757
]
5858

5959
{ #category : #accessing }
60-
PMPermutation class >> generator:arrayOfPermutations [
61-
|f max generators|
62-
max:=(arrayOfPermutations collect:[:g|g size])max.
63-
generators:=arrayOfPermutations collect:[:g| g extendTo: max].
64-
f:=PMFixpoint
65-
block: [ :s| |aSet|
66-
aSet:=Set newFrom: s.
67-
s do:[:p|s do:[:q|
68-
aSet add:(p permute:q)]].
69-
aSet]
70-
value: generators.
71-
f verbose:false.
72-
^f evaluate asArray.
60+
PMPermutation class >> generator: arrayOfPermutations [
7361

62+
| f max generators |
63+
max := (arrayOfPermutations collect: [ :g | g size ]) max.
64+
generators := arrayOfPermutations collect: [ :g | g extendTo: max ].
65+
f := PMFixpoint
66+
block: [ :s |
67+
| aSet |
68+
aSet := Set newFrom: s.
69+
s do: [ :p | s do: [ :q | aSet add: (p permute: q) ] ].
70+
aSet ]
71+
value: generators.
72+
f verbose: false.
73+
^ f evaluate asArray
7474
]
7575

7676
{ #category : #'instance creation' }
@@ -87,19 +87,28 @@ uses super withAll: since this way a primitive can be used, which is generally m
8787

8888
{ #category : #'instance creation' }
8989
PMPermutation class >> ordering: aCollection [
90-
"use #newFrom: for an unreduced Permutation! but then most things won't work before you call #reduce.
90+
"use #newFrom: for an unreduced Permutation! but then most things won't work before you call #reduce.
9191
aCollection must consist of elements that can be sorted via #<="
92-
^( super withAll: aCollection ) reduce
92+
93+
^ (super withAll: aCollection) reduce
9394
]
9495

9596
{ #category : #accessing }
9697
PMPermutation class >> randomGenerator [
97-
^RandomGenerator ifNil: [ RandomGenerator := Random new ]
98+
99+
^ RandomGenerator ifNil: [ RandomGenerator := Random new ]
100+
]
101+
102+
{ #category : #accessing }
103+
PMPermutation class >> randomGenerator: aGenerator [
104+
105+
^ RandomGenerator := aGenerator
98106
]
99107

100108
{ #category : #'instance creation' }
101109
PMPermutation class >> randomPermutation: size [
102-
^self ordering: (self randomGenerator next:size)
110+
111+
^ self newFrom: ((1 to: size) asArray shuffleBy: self randomGenerator)
103112
]
104113

105114
{ #category : #'instance creation' }

src/Math-Tests-Permutation/PMPermutationTest.class.st

+10-10
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,17 @@ self assert: g third isFloat.
236236

237237
{ #category : #'class tests' }
238238
PMPermutationTest >> testRandomPermutation [
239-
|p p2 l|
240-
l:=173."relatively high for a randomized test"
241-
PMPermutation randomGenerator seed:10. "can be put into comments for a randomized test"
242-
p:=PMPermutation randomPermutation: l.
243-
p2:=PMPermutation randomPermutation: l.
244-
self deny: p=p2.
245-
self assert: p class equals: PMPermutation.
246-
l:=1 to: l.
247-
self assert: p sorted equals: l.
248-
self assert: p2 sorted equals: l.
249239

240+
| p p2 l |
241+
l := 173. "relatively high for a randomized test"
242+
PMPermutation randomGenerator seed: 10. "can be put into comments for a randomized test"
243+
p := PMPermutation randomPermutation: l.
244+
p2 := PMPermutation randomPermutation: l.
245+
self deny: p equals: p2.
246+
self assert: p class equals: PMPermutation.
247+
l := 1 to: l.
248+
self assert: p sorted equals: l.
249+
self assert: p2 sorted equals: l
250250
]
251251

252252
{ #category : #tests }

0 commit comments

Comments
 (0)