Class: Algebra::LocalizedRing
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
create, superior?, sysvar, wedge
#+@, #-@, append_features, #coerce, #devide?, #ground, #ground=, #regulate, #unity, #unity?, #zero, #zero?
Constructor Details
#initialize(num, den = ground.unity) ⇒ LocalizedRing
113
114
115
116
117
118
119
120
|
# File 'lib/algebra/localized-ring.rb', line 113
def initialize(num, den = ground.unity)
if den.respond_to?(:<=>) && den < ground.zero
num = -num
den = -den
end
@numerator = num
@denominator = den
end
|
Instance Attribute Details
#denominator ⇒ Object
Returns the value of attribute denominator.
63
64
65
|
# File 'lib/algebra/localized-ring.rb', line 63
def denominator
@denominator
end
|
#numerator ⇒ Object
Returns the value of attribute numerator.
62
63
64
|
# File 'lib/algebra/localized-ring.rb', line 62
def numerator
@numerator
end
|
Class Method Details
.[](num, den = nil) ⇒ Object
69
70
71
|
# File 'lib/algebra/localized-ring.rb', line 69
def self.[](num, den = nil)
den ? reduce(num, den) : unity * num
end
|
.create(ground) ⇒ Object
48
49
50
51
52
|
# File 'lib/algebra/localized-ring.rb', line 48
def self.create(ground)
klass = super(ground)
klass.sysvar(:reducible, true)
klass
end
|
.reduce(num, den) ⇒ Object
73
74
75
|
# File 'lib/algebra/localized-ring.rb', line 73
def self.reduce(num, den)
reducible ? simplify(num, den) : new(num, den)
end
|
.reducible ⇒ Object
54
55
56
|
# File 'lib/algebra/localized-ring.rb', line 54
def self.reducible
@@reducible
end
|
.reducible=(sw) ⇒ Object
58
59
60
|
# File 'lib/algebra/localized-ring.rb', line 58
def self.reducible=(sw)
@@reducible = sw
end
|
.simplify(num, den) ⇒ Object
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
|
# File 'lib/algebra/localized-ring.rb', line 77
def self.simplify(num, den)
raise ZeroDivisionError, 'denometor is 0' if den.zero?
return zero if num.zero?
if ground.field?
num /= den
den = ground.unit
elsif ground.ufd?
gcd = num.gcd(den)
num /= gcd
den /= gcd
end
if defined?(Polynomial) && ground <= Polynomial
if ground.ground.field?
m = den.lc
num /= m
den /= m
elsif ground.ground.euclidian?
m = num.cont.gcd(den.cont)
num /= m
den /= m
end
end
new(num, den)
end
|
Instance Method Details
148
149
150
151
152
153
154
|
# File 'lib/algebra/localized-ring.rb', line 148
def *(o)
super do |o|
num = @numerator * o.numerator
den = @denominator * o.denominator
self.class.reduce(num, den)
end
end
|
#**(other) ⇒ Object
def % (o)
raise ZeroDivisionError, "devided by 0" if o.zero?
den, a, = @denominator.gcd_coeff(o)
num = (@numerator * a) % o
q, r = num.divmod den
raise "#@denominator can not divide #@numerator mod #{o}" unless r.zero?
q
end
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/algebra/localized-ring.rb', line 182
def **(other)
case other
when Integer
if other > 0
num = @numerator**other
den = @denominator**other
self.class.new(num, den)
elsif other < 0
num = @denominator**-other
den = @numerator**-other
self.class.new(num, den)
elsif other.zero?
unity
end
else
x, y = other.coerce(self)
x**y
end
end
|
132
133
134
135
136
137
138
|
# File 'lib/algebra/localized-ring.rb', line 132
def +(o)
super do |o|
num = @numerator * o.denominator
num_o = o.numerator * @denominator
self.class.reduce(num + num_o, @denominator * o.denominator)
end
end
|
140
141
142
143
144
145
146
|
# File 'lib/algebra/localized-ring.rb', line 140
def -(o)
super do |o|
num = @numerator * o.denominator
num_o = o.numerator * @denominator
self.class.reduce(num - num_o, @denominator * o.denominator)
end
end
|
156
157
158
159
160
161
162
163
|
# File 'lib/algebra/localized-ring.rb', line 156
def /(o)
raise ZeroDivisionError, 'devided by 0' if o.zero?
super do |o|
num = @numerator * o.denominator
den = @denominator * o.numerator
self.class.reduce(num, den)
end
end
|
126
127
128
129
130
|
# File 'lib/algebra/localized-ring.rb', line 126
def ==(o)
super do |o|
@numerator * o.denominator == o.numerator * @denominator
end
end
|
243
244
245
|
# File 'lib/algebra/localized-ring.rb', line 243
def hash
raise 'hash is undefined'
end
|
238
239
240
241
|
# File 'lib/algebra/localized-ring.rb', line 238
def inspect
to_s
end
|
#monomial? ⇒ Boolean
65
66
67
|
# File 'lib/algebra/localized-ring.rb', line 65
def monomial?
true
end
|
#need_paren_in_coeff? ⇒ Boolean
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/algebra/localized-ring.rb', line 202
def need_paren_in_coeff?
if @denominator.unity?
if @numerator.respond_to?(:need_paren_in_coeff?)
@numerator.need_paren_in_coeff?
elsif @numerator.is_a?(Numeric)
false
else
true
end
else
false
end
end
|
#pdivmod(other) ⇒ Object
169
170
171
|
# File 'lib/algebra/localized-ring.rb', line 169
def pdivmod(other)
[self / other, zero]
end
|
122
123
124
|
# File 'lib/algebra/localized-ring.rb', line 122
def simplify
self.class.simplify(@numerator, @denominator)
end
|
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
# File 'lib/algebra/localized-ring.rb', line 216
def to_s
n = if @numerator.respond_to?(:need_paren_in_coeff?) &&
@numerator.need_paren_in_coeff? &&
!@denominator.unity?
"(#{@numerator})"
else
@numerator.to_s
end
d = if @denominator.respond_to?(:need_paren_in_coeff?) &&
@denominator.need_paren_in_coeff? &&
!@denominator.is_a?(Integer)
"(#{@denominator})"
else
@denominator.to_s
end
if @denominator.unity?
n.to_s
else
n + '/' + d
end
end
|
#unit? ⇒ Boolean
165
166
167
|
# File 'lib/algebra/localized-ring.rb', line 165
def unit?
!zero?
end
|