From 1d7bf6accb64a1ee9a75cf9889d941a713d2d5c2 Mon Sep 17 00:00:00 2001 From: CyrilFerlicot Date: Fri, 17 Feb 2023 15:52:10 +0100 Subject: [PATCH] Speed up random permutations + allow to set ourself the random generator "Before" [PMPermutation randomPermutation: 30000] timeToRun. "0:00:00:05.666" "After" [PMPermutation randomPermutation: 30000] timeToRun. "0:00:00:00.006" --- src/Math-Permutation/PMPermutation.class.st | 43 +++++++++++-------- .../PMPermutationTest.class.st | 20 ++++----- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/Math-Permutation/PMPermutation.class.st b/src/Math-Permutation/PMPermutation.class.st index 57c1e3510..363755096 100644 --- a/src/Math-Permutation/PMPermutation.class.st +++ b/src/Math-Permutation/PMPermutation.class.st @@ -57,20 +57,20 @@ PMPermutation class >> fromCycles: aCollectionofCollections [ ] { #category : #accessing } -PMPermutation class >> generator:arrayOfPermutations [ -|f max generators| -max:=(arrayOfPermutations collect:[:g|g size])max. -generators:=arrayOfPermutations collect:[:g| g extendTo: max]. -f:=PMFixpoint - block: [ :s| |aSet| - aSet:=Set newFrom: s. - s do:[:p|s do:[:q| - aSet add:(p permute:q)]]. - aSet] - value: generators. -f verbose:false. -^f evaluate asArray. +PMPermutation class >> generator: arrayOfPermutations [ + | f max generators | + max := (arrayOfPermutations collect: [ :g | g size ]) max. + generators := arrayOfPermutations collect: [ :g | g extendTo: max ]. + f := PMFixpoint + block: [ :s | + | aSet | + aSet := Set newFrom: s. + s do: [ :p | s do: [ :q | aSet add: (p permute: q) ] ]. + aSet ] + value: generators. + f verbose: false. + ^ f evaluate asArray ] { #category : #'instance creation' } @@ -87,19 +87,28 @@ uses super withAll: since this way a primitive can be used, which is generally m { #category : #'instance creation' } PMPermutation class >> ordering: aCollection [ -"use #newFrom: for an unreduced Permutation! but then most things won't work before you call #reduce. + "use #newFrom: for an unreduced Permutation! but then most things won't work before you call #reduce. aCollection must consist of elements that can be sorted via #<=" -^( super withAll: aCollection ) reduce + + ^ (super withAll: aCollection) reduce ] { #category : #accessing } PMPermutation class >> randomGenerator [ -^RandomGenerator ifNil: [ RandomGenerator := Random new ] + + ^ RandomGenerator ifNil: [ RandomGenerator := Random new ] +] + +{ #category : #accessing } +PMPermutation class >> randomGenerator: aGenerator [ + + ^ RandomGenerator := aGenerator ] { #category : #'instance creation' } PMPermutation class >> randomPermutation: size [ -^self ordering: (self randomGenerator next:size) + + ^ self newFrom: ((1 to: size) asArray shuffleBy: self randomGenerator) ] { #category : #'instance creation' } diff --git a/src/Math-Tests-Permutation/PMPermutationTest.class.st b/src/Math-Tests-Permutation/PMPermutationTest.class.st index 2e54a1890..94cd36b00 100644 --- a/src/Math-Tests-Permutation/PMPermutationTest.class.st +++ b/src/Math-Tests-Permutation/PMPermutationTest.class.st @@ -236,17 +236,17 @@ self assert: g third isFloat. { #category : #'class tests' } PMPermutationTest >> testRandomPermutation [ -|p p2 l| -l:=173."relatively high for a randomized test" -PMPermutation randomGenerator seed:10. "can be put into comments for a randomized test" -p:=PMPermutation randomPermutation: l. -p2:=PMPermutation randomPermutation: l. -self deny: p=p2. -self assert: p class equals: PMPermutation. -l:=1 to: l. -self assert: p sorted equals: l. -self assert: p2 sorted equals: l. + | p p2 l | + l := 173. "relatively high for a randomized test" + PMPermutation randomGenerator seed: 10. "can be put into comments for a randomized test" + p := PMPermutation randomPermutation: l. + p2 := PMPermutation randomPermutation: l. + self deny: p equals: p2. + self assert: p class equals: PMPermutation. + l := 1 to: l. + self assert: p sorted equals: l. + self assert: p2 sorted equals: l ] { #category : #tests }