-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpadic.rb
More file actions
55 lines (46 loc) · 1.39 KB
/
padic.rb
File metadata and controls
55 lines (46 loc) · 1.39 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
require_relative 'expr'
class Padic
include Expr
attr :radix, :digits, :period
def initialize(radix, digits, period)
radix.integer? or raise ArgumentError, "radix must be an integer"
radix >= 2 or raise ArgumentError, "radix must be >= 2"
period <= digits.size or raise ArgumentError, "period #{period} cannot exceed number of digits #{digits.size}"
@radix = radix
@digits = digits
@period = period
end
def inspect
if radix <= 36
digits.zip_index.reverse.map do |d, e|
d.to_s(radix).send_if(e + period >= digits.size, :overline)
end.join + radix.to_subscript
else
"P[#{radix}]{" +
digits.zip_index.reject{|(d, _)| d.zero? }.reverse.map do |(d, e)|
exp = if e + period >= digits.size
"#{e}+#{period}n"
elsif e > 1
e.to_s
else
''
end.to_superscript
if e > 0
"#{d}×#{radix}#{exp}"
else
d.to_s
end
end.join(' + ') + "}"
end
end
end
class Integer
def to_padic(radix)
p = positional_expansion(radix)
Padic.new(radix, (0..p.keys.max).map{|e| p[e] }, 0)
end
end
class Rational
def to_padic(radix)
end
end