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