-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStandardPermutation.class.st
More file actions
160 lines (137 loc) · 3.89 KB
/
Copy pathStandardPermutation.class.st
File metadata and controls
160 lines (137 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"
Bijective functions on a set [1..n]. Permutations under composition form groups (see StandardSymmetricGroup and PermutationGroup).
"
Class {
#name : #StandardPermutation,
#superclass : #AbstractPermutation,
#instVars : [
'map'
],
#category : #'Mathematics-Groups-Permutations'
}
{ #category : #'instance creation' }
StandardPermutation class >> map: anArray [
^ self basicNew map: anArray
]
{ #category : #'instance creation' }
StandardPermutation class >> new [
^ self shouldNotImplement
]
{ #category : #'instance creation' }
StandardPermutation class >> new: anInteger [
^ self map: (1 to: anInteger) asArray
]
{ #category : #'instance creation' }
StandardPermutation class >> new: n cycle: aCollection [
| answer first last |
answer := self new: n.
aCollection do: [:each|
first isNil
ifTrue: [first := each]
ifFalse: [answer at: last put: each].
last := each].
answer at: last put: first.
^ answer
]
{ #category : #'instance creation' }
StandardPermutation class >> new: n cycles: anArray [
^ anArray inject: (self new: n) into: [:answer :each| answer * (self new: n cycle: each)]
]
{ #category : #'instance creation' }
StandardPermutation class >> new: anInteger evaluating: aBlock [
^ self map: ((1 to: anInteger) collect: aBlock)
]
{ #category : #'instance creation' }
StandardPermutation class >> new: n transposing: a with: b [
^ (self new: n)
at: a put: b;
at: b put: a;
yourself
]
{ #category : #arithmetic }
StandardPermutation >> * aPermutation [
"Answer the product (function composition) of the receiver with the argument.
This is the group operation."
| otherMap newMap |
(map size = aPermutation map size)
ifFalse: [^ DomainError signal: 'permutation of different degree'].
otherMap := aPermutation map.
newMap := map class new: map size.
1 to: map size do: [:i| newMap at: i put: (map at: (otherMap at: i))].
^ self species map: newMap
]
{ #category : #comparing }
StandardPermutation >> = aPermutation [
^ self class = aPermutation class and: [map = aPermutation map]
]
{ #category : #converting }
StandardPermutation >> asArray [
^ map
]
{ #category : #accessing }
StandardPermutation >> at: anInteger [
^ map at: anInteger ifAbsent: [anInteger]
]
{ #category : #accessing }
StandardPermutation >> at: anInteger put: anotherInteger [
^ map at: anInteger put: anotherInteger
]
{ #category : #enumerating }
StandardPermutation >> changesDo: aBlock [
1 to: map size do: [:i| (map at: i) = i ifFalse: [aBlock value: i]]
]
{ #category : #accessing }
StandardPermutation >> degree [
^ map size
]
{ #category : #accessing }
StandardPermutation >> domain [
^ 1 to: map size
]
{ #category : #comparing }
StandardPermutation >> hash [
| answer |
answer := 0.
1 to: map size do: [:i| | value |
(value := map at: i) = i ifFalse: [answer := answer bitXor: i hash hashMultiply + value hash]].
^ answer
]
{ #category : #arithmetic }
StandardPermutation >> identity [
"Answer the identity permutation."
^ self species new: self degree
]
{ #category : #arithmetic }
StandardPermutation >> inverse [
"Answer the compositive inverse of the receiver."
| values |
values := map class new: map size.
1 to: map size do: [:i| values at: (map at: i) put: i].
^ self species map: values
]
{ #category : #'accessing-private' }
StandardPermutation >> map [
^ map
]
{ #category : #'accessing-private' }
StandardPermutation >> map: aCollection [
map := aCollection
]
{ #category : #private }
StandardPermutation >> species [
^ StandardPermutation
]
{ #category : #accessing }
StandardPermutation >> transpositions [
"Answer the decomposition of the receiver in product of transpositions."
| answer last value |
answer := OrderedCollection new.
self
changesDo: [ :each |
last := each.
answer reverseDo: [ :one | last := one at: last ].
(value := self at: each) = last
ifFalse: [ answer
addFirst: (self species new: self degree transposing: last with: value) ] ].
^ answer
]