hpsqrt 1.8.0 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -3
  3. data/lib/hpsqrt.rb +30 -20
  4. data/lib/hpsqrt/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 05dcb3de244a72c5d31cc3a6cb9591aedebe865b23a0622f36938efeb432b484
4
- data.tar.gz: 6062e3d23c3ed35228b19caf4ab8e84d53e06365609e3499acf6f82536c78712
3
+ metadata.gz: 437b5349ed19ac1c435e0dca0128a7ed7cb832d95dcc95762ed6b97b93cbd7f1
4
+ data.tar.gz: 99cc3c0e50c6085a7c228bb19ac99b5dde007fdb62163592cfaa11850ce753aa
5
5
  SHA512:
6
- metadata.gz: 06e28c7a14e200465bfb6f578ce1d252a58e67fe0f786907ce98bd95c0b5b70292f80790113bbc8c3bca61bf0f0fb8b74e159b7fc25a3eb794f349a83427d5c5
7
- data.tar.gz: 2537b6dee6cd313f3be6520c3a069a09a6ee3d170f1de6822efd1b80ebf3769c50d977ea798fd8a2ec350fe96cb2a4d0517883a0edd9f936e8477925ecc6bf59
6
+ metadata.gz: fcf2f4b2f23c3fc084090aeaff489bc9866a0264eb50464145097efffd304f942f2efe12b7faf77d84135ac9ef9bbe16395c1bcded28ef75b15fc9881ced7732
7
+ data.tar.gz: 1334f295b5201649b02a6f67bcdc1c811e6131bb5e224f7434dfdc80f18ae685eb57de7cab024ff51c626b5ad41403085747dd01be6c36d730470c2bc8e1828d
data/README.md CHANGED
@@ -65,6 +65,10 @@ p Sqrt(5).to_i
65
65
  p Sqrt(4).to_r
66
66
  # => (2/1)
67
67
 
68
+ # to Complex (involving Rational)
69
+ p (Sqrt(2)**2).to_rc
70
+ # => ((2/1)+(0/1)*i)
71
+
68
72
  # to Complex
69
73
  p Sqrt(-1i).to_c
70
74
  # => (0.7071067811865476-0.7071067811865476i)
@@ -78,7 +82,7 @@ p Sqrt(1i).imag
78
82
  # => -0.7071067811865476
79
83
 
80
84
  p (Sqrt(5) + Sqrt(7)).expr
81
- # => "√5 + √7"
85
+ # => "√5 + √7"
82
86
  ```
83
87
 
84
88
  Type check:
@@ -86,17 +90,23 @@ Type check:
86
90
  ```ruby
87
91
  require 'hpsqrt/core_ext'
88
92
 
89
- # integer? return true if after the real decimal point is 0 and imaginary number is 0
93
+ # integer? returns true if after the real decimal point is 0 and imaginary number is 0
90
94
  p Sqrt(2).integer?
91
95
  # => false
92
96
  p Sqrt(4).integer?
93
97
  # => true
94
98
 
95
- # float? return true if after the real decimal point is not 0 and imaginary number is 0
99
+ # float? returns true if after the real decimal point is not 0 and imaginary number is 0
96
100
  p Sqrt(2).float?
97
101
  # => true
98
102
  p Sqrt(4).float?
99
103
  # => false
104
+
105
+ # complex? returns true if imaginary number is not 0
106
+ p Sqrt(1).complex?
107
+ # => false
108
+ p Sqrt(-1).complex?
109
+ # => true
100
110
  ```
101
111
 
102
112
  ## Contributing
data/lib/hpsqrt.rb CHANGED
@@ -73,7 +73,7 @@ class HpSqrt < Numeric
73
73
 
74
74
  def /(other)
75
75
  other = self.class.create(other)
76
- other_inv = Term.new(number: Rational(1, other.to_c))
76
+ other_inv = Term.new(number: Rational(1, other.to_rc))
77
77
 
78
78
  terms = {}
79
79
  @terms.each {|t, c|
@@ -101,7 +101,7 @@ class HpSqrt < Numeric
101
101
  end
102
102
  result
103
103
  else
104
- self.class.number(self.to_c ** other.to_c)
104
+ self.class.number(self.to_rc ** other.to_rc)
105
105
  end
106
106
  end
107
107
 
@@ -111,9 +111,9 @@ class HpSqrt < Numeric
111
111
 
112
112
  def ==(other)
113
113
  if self.class==other
114
- self.to_c==other.to_c
114
+ self.to_rc==other.to_rc
115
115
  elsif Numeric===other
116
- self.to_c==other
116
+ self.to_rc==other
117
117
  else
118
118
  super.==(other)
119
119
  end
@@ -161,29 +161,37 @@ class HpSqrt < Numeric
161
161
  alias_method :imaginary, :imag
162
162
 
163
163
  def to_i
164
- c = to_c
165
- if c.imag.zero?
166
- c.real.to_i
164
+ if imag.zero?
165
+ real.to_i
167
166
  else
168
- raise RangeError, "can't convert %s into Integer" % c
167
+ raise RangeError, "can't convert %s into Integer" % to_c
169
168
  end
170
169
  end
171
170
 
172
171
  def to_f
173
- c = to_c
174
- if c.imag.zero?
175
- c.real.to_f
172
+ if imag.zero?
173
+ real.to_f
176
174
  else
177
- raise RangeError, "can't convert %s into Float" % c
175
+ raise RangeError, "can't convert %s into Float" % to_c
178
176
  end
179
177
  end
180
178
 
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
+
181
190
  def to_r
182
- c = to_c
183
- if c.imag.zero?
184
- Rational(c.real, 1)
191
+ if to_rc.imag.zero?
192
+ to_rc.real
185
193
  else
186
- raise RangeError, "can't convert %s into Rational" % c
194
+ raise RangeError, "can't convert %s into Rational" % to_rc
187
195
  end
188
196
  end
189
197
 
@@ -247,14 +255,16 @@ class HpSqrt < Numeric
247
255
  false
248
256
  end
249
257
 
258
+ def complex?
259
+ !imag.zero?
260
+ end
261
+
250
262
  def integer?
251
- c = to_c
252
- c.imag.zero? && c.real==c.real.to_i
263
+ imag.zero? && real==real.to_i
253
264
  end
254
265
 
255
266
  def float?
256
- c = to_c
257
- c.imag.zero? && Float===c.real && c.real!=c.real.to_i
267
+ imag.zero? && Float===real && real!=real.to_i
258
268
  end
259
269
 
260
270
  def self.create(v)
@@ -1,3 +1,3 @@
1
1
  class HpSqrt < Numeric
2
- VERSION = "1.8.0"
2
+ VERSION = "1.9.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.8.0
4
+ version: 1.9.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-17 00:00:00.000000000 Z
11
+ date: 2019-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler