1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\phpermutations\Iterators; |
4
|
|
|
|
5
|
|
|
use drupol\phpermutations\Combinatorics; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class FiniteGroup. |
9
|
|
|
* |
10
|
|
|
* The finite group is an abelian finite cyclic group. |
11
|
|
|
*/ |
12
|
|
|
class FiniteGroup extends Combinatorics implements \Iterator, \Countable |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The group size. |
16
|
|
|
* |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
protected $size; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The group. |
23
|
|
|
* |
24
|
|
|
* @var int[] |
25
|
|
|
*/ |
26
|
|
|
protected $group; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The key. |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $key; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Combinatorics constructor. |
37
|
|
|
*/ |
38
|
2 |
|
public function __construct() |
39
|
|
|
{ |
40
|
2 |
|
parent::__construct([], null); |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
2 |
|
public function current() |
47
|
|
|
{ |
48
|
2 |
|
return current($this->group); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
2 |
|
public function next() |
55
|
|
|
{ |
56
|
2 |
|
++$this->key; |
57
|
2 |
|
next($this->group); |
58
|
2 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
2 |
|
public function key() |
64
|
|
|
{ |
65
|
2 |
|
return $this->key; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
2 |
|
public function valid() |
72
|
|
|
{ |
73
|
2 |
|
return isset($this->group[$this->key()]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
2 |
|
public function rewind() |
80
|
|
|
{ |
81
|
2 |
|
$this->key = 0; |
82
|
2 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Count elements of an object. |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
* The number of element |
89
|
|
|
*/ |
90
|
2 |
|
public function count() |
91
|
|
|
{ |
92
|
2 |
|
return count($this->group); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Convert the iterator into an array. |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
* The elements |
100
|
|
|
*/ |
101
|
2 |
|
public function toArray() |
102
|
|
|
{ |
103
|
2 |
|
$data = []; |
104
|
|
|
|
105
|
2 |
|
for ($this->rewind(); $this->valid(); $this->next()) { |
106
|
2 |
|
$data[] = $this->current(); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
return $data; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set the group size. |
114
|
|
|
* |
115
|
|
|
* @param int $size |
116
|
|
|
* The size |
117
|
|
|
*/ |
118
|
2 |
|
public function setSize($size) |
119
|
|
|
{ |
120
|
2 |
|
$this->size = $size; |
121
|
2 |
|
$this->computeGroup(); |
122
|
2 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the group size. |
126
|
|
|
* |
127
|
|
|
* @return int |
128
|
|
|
* The size |
129
|
|
|
*/ |
130
|
2 |
|
public function getSize() |
131
|
|
|
{ |
132
|
2 |
|
return intval($this->size); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the order. |
137
|
|
|
* |
138
|
|
|
* @param int $generator |
139
|
|
|
* The generator |
140
|
|
|
* |
141
|
|
|
* @return int |
142
|
|
|
* The order |
143
|
|
|
*/ |
144
|
|
|
public function order($generator) |
145
|
|
|
{ |
146
|
|
|
$result = []; |
147
|
|
|
|
148
|
|
|
foreach (range(1, $this->getSize() - 1) as $number) { |
149
|
|
|
$value = pow($generator, $number) % $this->getSize(); |
150
|
|
|
$result[$value] = $value; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return count($result); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Clean out the group from unwanted values. |
158
|
|
|
*/ |
159
|
2 |
|
private function computeGroup() |
160
|
|
|
{ |
161
|
2 |
|
$this->group = []; |
162
|
|
|
|
163
|
2 |
|
foreach (range(1, $this->getSize() - 1) as $number) { |
164
|
2 |
|
if (1 === $this->gcd($number, $this->getSize() - 1)) { |
165
|
2 |
|
$this->group[] = $number; |
166
|
|
|
} |
167
|
|
|
} |
168
|
2 |
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the greater common divisor between two numbers. |
172
|
|
|
* |
173
|
|
|
* @param int $a |
174
|
|
|
* The first number |
175
|
|
|
* @param int $b |
176
|
|
|
* The second number |
177
|
|
|
* |
178
|
|
|
* @return int |
179
|
|
|
* The greater common divisor between $a and $b |
180
|
|
|
*/ |
181
|
2 |
|
private function gcd($a, $b) |
|
|
|
|
182
|
|
|
{ |
183
|
2 |
|
return $b ? $this->gcd($b, $a % $b) : $a; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.