Class: HpSqrt
- Inherits:
-
Numeric
show all
- Defined in:
- lib/hpsqrt.rb,
lib/hpsqrt/term.rb,
lib/hpsqrt/version.rb,
lib/hpsqrt/inspect_mode.rb
Defined Under Namespace
Modules: INSPECT_MODE
Classes: Term
Constant Summary
collapse
- VERSION =
"1.10.0"
- @@inspect_mode =
INSPECT_MODE::VALUE
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#terms ⇒ Object
Returns the value of attribute terms.
19
20
21
|
# File 'lib/hpsqrt.rb', line 19
def terms
@terms
end
|
Class Method Details
.create(v) ⇒ Object
270
271
272
273
274
275
276
|
# File 'lib/hpsqrt.rb', line 270
def self.create(v)
if self===v
v
else
number(v)
end
end
|
.inspect_mode ⇒ Object
10
11
12
|
# File 'lib/hpsqrt.rb', line 10
def self.inspect_mode
@@inspect_mode
end
|
.inspect_mode=(v) ⇒ Object
14
15
16
|
# File 'lib/hpsqrt.rb', line 14
def self.inspect_mode=(v)
@@inspect_mode = v
end
|
.number(v) ⇒ Object
278
279
280
281
282
283
284
|
# File 'lib/hpsqrt.rb', line 278
def self.number(v)
if v!=0
new({Term.new(number: v) => 1})
else
zero
end
end
|
.sqrt(v) ⇒ Object
286
287
288
289
290
291
292
293
294
295
|
# File 'lib/hpsqrt.rb', line 286
def self.sqrt(v)
if self===v
v = v.to_rc
end
if v!=0
new({Term.new(sqrt: v) => 1})
else
zero
end
end
|
.zero ⇒ Object
297
298
299
|
# File 'lib/hpsqrt.rb', line 297
def self.zero
new({})
end
|
Instance Method Details
#*(other) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/hpsqrt.rb', line 56
def *(other)
other = self.class.create(other)
terms = {}
@terms.each {|t1, c1|
other.terms.each {|t2, c2|
t = t1 * t2
c = c1 * c2
terms[t] ||= 0
terms[t] += c
}
}
terms.delete_if {|t,c| c==0}
self.class.new(terms)
end
|
#**(other) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/hpsqrt.rb', line 90
def **(other)
other = self.class.create(other)
if other.integer?
result = self.class.create(1)
other_i = other.real.to_i
other_i.abs.times {|i|
result *= self
}
if other_i<0
result = Rational(1, result)
end
result
else
self.class.number(self.to_rc ** other.to_rc)
end
end
|
#+(other) ⇒ Object
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/hpsqrt.rb', line 33
def +(other)
other = self.class.create(other)
terms = @terms.merge(other.terms) {|t, c1, c2|
c1 + c2
}
terms.delete_if {|t,c| c==0}
self.class.new(terms)
end
|
#-(other) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/hpsqrt.rb', line 44
def -(other)
other = self.class.create(other)
other_terms = other.terms.map{|t,c| [t, -c]}.to_h
terms = @terms.merge(other_terms) {|t, c1, c2|
c1 + c2
}
terms.delete_if {|t,c| c==0}
self.class.new(terms)
end
|
#-@ ⇒ Object
28
29
30
31
|
# File 'lib/hpsqrt.rb', line 28
def -@
terms = @terms.map{|t,c| [t, -c]}.to_h
self.class.new(terms)
end
|
#/(other) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/hpsqrt.rb', line 74
def /(other)
other = self.class.create(other)
other_inv = Term.new(number: Rational(1, other.to_rc))
terms = {}
@terms.each {|t, c|
t *= other_inv
terms[t] ||= 0
terms[t] += c
}
terms.delete_if {|t,c| c==0}
self.class.new(terms)
end
|
#<=>(other) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/hpsqrt.rb', line 122
def <=>(other)
if !(Numeric===other)
nil
elsif self==other
0
elsif !self.imag.zero? || !other.imag.zero?
nil
else
self.real <=> other.real
end
end
|
#==(other) ⇒ Object
112
113
114
115
116
117
118
119
120
|
# File 'lib/hpsqrt.rb', line 112
def ==(other)
if self.class==other
self.to_rc==other.to_rc
elsif Numeric===other
self.to_rc==other
else
super.==(other)
end
end
|
#arg ⇒ Object
Also known as:
angle, phase
142
143
144
|
# File 'lib/hpsqrt.rb', line 142
def arg
to_c.arg
end
|
#coerce(other) ⇒ Object
108
109
110
|
# File 'lib/hpsqrt.rb', line 108
def coerce(other)
[self.class.create(other), self]
end
|
#complex? ⇒ Boolean
258
259
260
|
# File 'lib/hpsqrt.rb', line 258
def complex?
!to_rc.imag.zero?
end
|
#expr ⇒ Object
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
|
# File 'lib/hpsqrt.rb', line 196
def expr
@cache[:expr] ||= begin
value_to_s = -> (v) {
if Complex===v && v.imag.zero?
v = v.real
end
if Rational===v && v.denominator==1
v = v.numerator
end
v = v.to_s
if v !~ /^[\d\.]+$/
v = "(%s)" % v
end
v
}
result = @terms.map {|t, c|
n = t.number * c
s = t.sqrt
if s!=1
if n==1
"\u221A%s" % value_to_s[s]
elsif 0<n.real
"%s\u221A%s" % [value_to_s[n], value_to_s[s]]
elsif n==-1
"(-\u221A%s)" % value_to_s[s]
else
"(%s\u221A%s)" % [value_to_s[n], value_to_s[s]]
end
else
value_to_s[n]
end
}
if 0<result.length
result.join(" + ")
else
"0"
end
end
end
|
#float? ⇒ Boolean
266
267
268
|
# File 'lib/hpsqrt.rb', line 266
def float?
to_rc.imag.zero? && to_rc.real.denominator!=1
end
|
#imag ⇒ Object
Also known as:
imaginary
167
168
169
|
# File 'lib/hpsqrt.rb', line 167
def imag
to_c.imag
end
|
#inspect ⇒ Object
239
240
241
|
# File 'lib/hpsqrt.rb', line 239
def inspect
to_s
end
|
#integer? ⇒ Boolean
262
263
264
|
# File 'lib/hpsqrt.rb', line 262
def integer?
to_rc.imag.zero? && to_rc.real.denominator==1
end
|
#polar ⇒ Object
138
139
140
|
# File 'lib/hpsqrt.rb', line 138
def polar
to_c.polar
end
|
#real ⇒ Object
163
164
165
|
# File 'lib/hpsqrt.rb', line 163
def real
to_c.real
end
|
#real? ⇒ Boolean
254
255
256
|
# File 'lib/hpsqrt.rb', line 254
def real?
false
end
|
#rect ⇒ Object
134
135
136
|
# File 'lib/hpsqrt.rb', line 134
def rect
to_c.rect
end
|
#to_c ⇒ Object
159
160
161
|
# File 'lib/hpsqrt.rb', line 159
def to_c
@cache[:to_c] ||= Complex(to_rc.real.to_f, to_rc.imag.to_f)
end
|
#to_f ⇒ Object
180
181
182
183
184
185
186
|
# File 'lib/hpsqrt.rb', line 180
def to_f
if imag.zero?
real.to_f
else
raise RangeError, "can't convert %s into Float" % to_c
end
end
|
#to_i ⇒ Object
172
173
174
175
176
177
178
|
# File 'lib/hpsqrt.rb', line 172
def to_i
if imag.zero?
real.to_i
else
raise RangeError, "can't convert %s into Integer" % to_c
end
end
|
#to_r ⇒ Object
188
189
190
191
192
193
194
|
# File 'lib/hpsqrt.rb', line 188
def to_r
if to_rc.imag.zero?
to_rc.real
else
raise RangeError, "can't convert %s into Rational" % to_rc
end
end
|
#to_rc ⇒ Object
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/hpsqrt.rb', line 148
def to_rc
@cache[:to_rc] ||= @terms.map {|t, c|
nc = Complex(t.number.real.to_r, t.number.imag.to_r)
sc = Math.sqrt(Complex(t.sqrt))
sc = Complex(sc.real.to_r, sc.imag.to_r)
nc * sc * c
}.sum(Complex(0.to_r, 0.to_r))
end
|
#to_s ⇒ Object
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/hpsqrt.rb', line 243
def to_s
case @@inspect_mode
when INSPECT_MODE::VALUE
to_c.to_s
when INSPECT_MODE::EXPR
expr
else
"#<%s:0x%016x value=(%s) expr=(%s)>" % [self.class.name, self.object_id, to_c, expr]
end
end
|