-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathring.rb
More file actions
335 lines (284 loc) · 7.24 KB
/
ring.rb
File metadata and controls
335 lines (284 loc) · 7.24 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
require 'active_support/concern'
require_relative 'ext'
require_relative 'enumerable'
require_relative 'math'
module Ring
extend ActiveSupport::Concern
module ClassMethods
include Enumerable
def units
raise NotImplementedError
end
def naturals
raise NotImplementedError
end
def primes
raise NotImplementedError
end
def naturalize(x)
raise NotImplementedError
end
def divides?(x, y)
raise NotImplementedError
end
def mul(x, y)
raise NotImplementedError
end
# Return the Euclidean remainder of x/y
def mod(x, y)
raise NotImplementedError
end
def divmod(x, y)
raise NotImplementedError
end
def associates(x)
x = naturalize(x)
units.map{|u| mul(u, x) }
end
def associated?(x, y)
divides?(x, y) && divides?(y, x)
end
def ideal(x)
transform{|y| mul(x, y) }
end
def pow(x, y)
if y.zero?
1
elsif y.one?
x
elsif y.integer? && !y.negative?
binary_pow(x, y)
else
raise ArgumentError, "Eisenstein integer #{x} cannot be raised to exponent #{y}"
end
end
# Return the common divisor of x and y with the largest norm
def gcd(x, y)
until y.zero?
# puts "#{x} #{y}"
x, y = y, mod(x, y)
end
x
end
def coprime?(x, y)
gcd(x, y).unit?
end
# Return the common multiple of x and y with the smallest norm
def lcm(x, y)
mul(x, div(y, gcd(x, y)))
end
# Return [c, a, b, u, v] where
# c = gcd(x, y)
# ax + by = c
# |uc| = |x|
# |vc| = |y|
def gcd_ex(x, y)
s1 = t0 = 0
s0 = t1 = 1
until y.zero?
x, (q, y) = y, divmod(x, y)
s0, s1 = s1, s0 - q*s1
t0, t1 = t1, t0 - q*t1
# puts "x=#{x} y=#{y} s0=#{s0} s1=#{s1} t0=#{t0} t1=#{t1} q=#{q}"
end
return x, s0, t0, t1, s1
end
def prime_factors(x)
if x.zero? || x.unit?
{}
else
f = Hash.new(0)
primes.each do |p|
loop do
q, r = divmod(x, p)
break unless r.zero?
f[p] += 1
return f.freeze if q.unit?
x = q
end
end
end
end
end
end
class Integer
include Ring
class << self
def each
yield 0
1.andup do |i|
yield i
yield -i
end
end
enum_method :each
def units
[1, -1].freeze
end
cache_method :units
def naturals(&block)
1.andup(&block)
end
def primes(&block)
Prime.each(&block)
end
def naturalize(x)
x.abs
end
def prime?(x)
x.prime?
end
def associated?(x, y)
x == y || x == -y
end
def divides?(x, y)
y % x == 0
end
def mul(x, y)
x * y
end
def mod(x, y)
x % y
end
def divmod(x, y)
x.divmod(y)
end
end
end
module GaussianInteger
extend ActiveSupport::Concern
include Ring
module ClassMethods
def each
yield 0
1.andup do |r|
(-r...r).each do |k|
yield r + k*I
yield -k + r*I
yield -r - k*I
yield k - r*I
end
end
end
enum_method :each
def units
[1, Complex::I, -1, -Complex::I].freeze
end
cache_method :units
def naturals
1.andup do |a|
(0..a).each do |b|
yield a-b + b*I
end
end
end
enum_method :naturals
def seminaturals
1.andup do |a|
(0..a).each do |b|
yield a + b*I
end
end
end
enum_method :seminaturals
def primes
seminaturals.each do |p|
if prime? p
yield p
yield p.imag + p.real*I unless p.imag.zero? || p.imag == p.real
end
end
end
enum_method :primes
def pythagorean?(x)
unless x.real.zero? || x.imag.zero?
n = x.norm
sqrt(n).floor**2 == n
end
end
def pythagoreans
1.andup do |i|
(0...i).each do |j|
m = 2*(i-j)
n = 2*j+1
if Integer.coprime?(m, n)
b, a = [m**2 - n**2, 2*m*n].map(&:abs).sort
yield a + b*I
end
end
end
# seminaturals.each do |p|
# if pythagorean? p
# yield p
# yield p.imag + p.real*I unless p.imag.zero? || p.imag == p.real
# end
# end
end
enum_method :pythagoreans
def naturalize(x)
if x.real.negative?
if x.imag.negative?
-x
else
x.imag - x.real*I
end
else
if x.imag.negative?
-x.imag + x.real*I
else
x
end
end
end
def mul(x, y)
x * y
end
def div(x, y)
x / y
end
def mod(x, y)
yc = y.conj
u = mul(x, yc)
v = y.norm
(u.real % v + (u.imag % v)*I) / yc
end
def divmod(x, y)
yc = y.conj
u = mul(x, yc)
v = y.norm
q1,r1 = u.real.divmod(v)
q2,r2 = u.imag.divmod(v)
return q1 + q2*I, (r1 + r2*I) / yc
end
def divides?(y, x)
yc = y.conj
u = mul(x, yc)
v = y.norm
v.divides?(u.real) && v.divides?(u.imag)
end
def prime?(x)
n = if x.real.zero?
x.imag.abs
elsif x.imag.zero?
x.real.abs
end
if n
n.integer? && n % 4 == 3 && Integer.prime?(n)
else
Integer.prime?(x.norm)
end
end
def grid(gen=1)
Plot.drawable do |ctx|
ctx.grill 0, gen
ctx.grill 0, gen*I
end
end
end
end
class Complex
include GaussianInteger
def gaussian_integer?
real.integer? && imag.integer?
end
end