Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions src/Math-Permutation/PMPermutation.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand All @@ -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' }
Expand Down
20 changes: 10 additions & 10 deletions src/Math-Tests-Permutation/PMPermutationTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down