hpsqrt 1.9.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 437b5349ed19ac1c435e0dca0128a7ed7cb832d95dcc95762ed6b97b93cbd7f1
4
- data.tar.gz: 99cc3c0e50c6085a7c228bb19ac99b5dde007fdb62163592cfaa11850ce753aa
3
+ metadata.gz: bdf439f0e309a02b4f3356ae7e8ca8de9488fab3b46a167b0c8cfc0c84db4b55
4
+ data.tar.gz: 2f5e1f9badf7dad530ab0b37b5c138d24a067c665ecd646ebc744c015e650ee9
5
5
  SHA512:
6
- metadata.gz: fcf2f4b2f23c3fc084090aeaff489bc9866a0264eb50464145097efffd304f942f2efe12b7faf77d84135ac9ef9bbe16395c1bcded28ef75b15fc9881ced7732
7
- data.tar.gz: 1334f295b5201649b02a6f67bcdc1c811e6131bb5e224f7434dfdc80f18ae685eb57de7cab024ff51c626b5ad41403085747dd01be6c36d730470c2bc8e1828d
6
+ metadata.gz: 2e4c2b6a8b6e83a73d7b01a34bbb04500f4067d25b2bc335ae318632187544e0e75061f5adf1b75bb922770570937aebdde5a4d4418ab61398e3c13c1a1e0418
7
+ data.tar.gz: 0f588870311f93645d491b4b71da5ea1e7780527bb7a365258ed7aba3b6e7bdfef419b306fc7aeef1db949ce321f5af2fb4ba7e0b49dd3208eda54872b841427
data/README.md CHANGED
@@ -69,7 +69,7 @@ p Sqrt(4).to_r
69
69
  p (Sqrt(2)**2).to_rc
70
70
  # => ((2/1)+(0/1)*i)
71
71
 
72
- # to Complex
72
+ # to Complex (involving Float)
73
73
  p Sqrt(-1i).to_c
74
74
  # => (0.7071067811865476-0.7071067811865476i)
75
75
 
@@ -81,6 +81,7 @@ p Sqrt(-1i).real
81
81
  p Sqrt(1i).imag
82
82
  # => -0.7071067811865476
83
83
 
84
+ # to expression string
84
85
  p (Sqrt(5) + Sqrt(7)).expr
85
86
  # => "√5 + √7"
86
87
  ```
@@ -145,10 +145,19 @@ class HpSqrt < Numeric
145
145
  alias_method :angle, :arg
146
146
  alias_method :phase, :arg
147
147
 
148
+ def to_rc
149
+ @cache[:to_rc] ||= @terms.map {|t, c|
150
+ nc = Complex(t.number.real.to_r, t.number.imag.to_r)
151
+
152
+ sc = Math.sqrt(Complex(t.sqrt))
153
+ sc = Complex(sc.real.to_r, sc.imag.to_r)
154
+
155
+ nc * sc * c
156
+ }.sum(Complex(0.to_r, 0.to_r))
157
+ end
158
+
148
159
  def to_c
149
- @cache[:to_c] ||= @terms.map {|t, c|
150
- t.number * Math.sqrt(Complex(t.sqrt)) * c
151
- }.sum.to_c
160
+ @cache[:to_c] ||= Complex(to_rc.real.to_f, to_rc.imag.to_f)
152
161
  end
153
162
 
154
163
  def real
@@ -176,17 +185,6 @@ class HpSqrt < Numeric
176
185
  end
177
186
  end
178
187
 
179
- def to_rc
180
- @cache[:to_rc] ||= @terms.map {|t, c|
181
- nc = Complex(t.number.real.to_r, t.number.imag.to_r)
182
-
183
- sc = Math.sqrt(Complex(t.sqrt))
184
- sc = Complex(sc.real.to_r, sc.imag.to_r)
185
-
186
- nc * sc * c
187
- }.sum.nonzero? || Complex(0.to_r, 0.to_r)
188
- end
189
-
190
188
  def to_r
191
189
  if to_rc.imag.zero?
192
190
  to_rc.real
@@ -196,43 +194,45 @@ class HpSqrt < Numeric
196
194
  end
197
195
 
198
196
  def expr
199
- value_to_s = -> (v) {
200
- if Complex===v && v.imag.zero?
201
- v = v.real
202
- end
203
- if Rational===v && v.denominator==1
204
- v = v.numerator
205
- end
206
- v = v.to_s
207
- if v !~ /^[\d\.]+$/
208
- v = "(%s)" % v
209
- end
210
- v
211
- }
197
+ @cache[:expr] ||= begin
198
+ value_to_s = -> (v) {
199
+ if Complex===v && v.imag.zero?
200
+ v = v.real
201
+ end
202
+ if Rational===v && v.denominator==1
203
+ v = v.numerator
204
+ end
205
+ v = v.to_s
206
+ if v !~ /^[\d\.]+$/
207
+ v = "(%s)" % v
208
+ end
209
+ v
210
+ }
212
211
 
213
- result = @terms.map {|t, c|
214
- n = t.number * c
215
- s = t.sqrt
216
-
217
- if s!=1
218
- if n==1
219
- "\u221A%s" % value_to_s[s]
220
- elsif 0<n.real
221
- "%s\u221A%s" % [value_to_s[n], value_to_s[s]]
222
- elsif n==-1
223
- "(-\u221A%s)" % value_to_s[s]
212
+ result = @terms.map {|t, c|
213
+ n = t.number * c
214
+ s = t.sqrt
215
+
216
+ if s!=1
217
+ if n==1
218
+ "\u221A%s" % value_to_s[s]
219
+ elsif 0<n.real
220
+ "%s\u221A%s" % [value_to_s[n], value_to_s[s]]
221
+ elsif n==-1
222
+ "(-\u221A%s)" % value_to_s[s]
223
+ else
224
+ "(%s\u221A%s)" % [value_to_s[n], value_to_s[s]]
225
+ end
224
226
  else
225
- "(%s\u221A%s)" % [value_to_s[n], value_to_s[s]]
227
+ value_to_s[n]
226
228
  end
229
+ }
230
+
231
+ if 0<result.length
232
+ result.join(" + ")
227
233
  else
228
- value_to_s[n]
234
+ "0"
229
235
  end
230
- }
231
-
232
- if 0<result.length
233
- result.join(" + ")
234
- else
235
- "0"
236
236
  end
237
237
  end
238
238
 
@@ -256,15 +256,15 @@ class HpSqrt < Numeric
256
256
  end
257
257
 
258
258
  def complex?
259
- !imag.zero?
259
+ !to_rc.imag.zero?
260
260
  end
261
261
 
262
262
  def integer?
263
- imag.zero? && real==real.to_i
263
+ to_rc.imag.zero? && to_rc.real.denominator==1
264
264
  end
265
265
 
266
266
  def float?
267
- imag.zero? && Float===real && real!=real.to_i
267
+ to_rc.imag.zero? && to_rc.real.denominator!=1
268
268
  end
269
269
 
270
270
  def self.create(v)
@@ -285,7 +285,7 @@ class HpSqrt < Numeric
285
285
 
286
286
  def self.sqrt(v)
287
287
  if self===v
288
- v = v.to_c
288
+ v = v.to_rc
289
289
  end
290
290
  if v!=0
291
291
  new({Term.new(sqrt: v) => 1})
@@ -1,3 +1,3 @@
1
1
  class HpSqrt < Numeric
2
- VERSION = "1.9.0"
2
+ VERSION = "1.10.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hpsqrt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 1.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshida Tetsuya
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-18 00:00:00.000000000 Z
11
+ date: 2019-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler