hpsqrt 1.8.0 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -3
- data/lib/hpsqrt.rb +30 -20
- data/lib/hpsqrt/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 437b5349ed19ac1c435e0dca0128a7ed7cb832d95dcc95762ed6b97b93cbd7f1
|
4
|
+
data.tar.gz: 99cc3c0e50c6085a7c228bb19ac99b5dde007fdb62163592cfaa11850ce753aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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?
|
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?
|
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.
|
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.
|
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.
|
114
|
+
self.to_rc==other.to_rc
|
115
115
|
elsif Numeric===other
|
116
|
-
self.
|
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
|
-
|
165
|
-
|
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" %
|
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
|
-
|
174
|
-
|
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" %
|
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
|
-
|
183
|
-
|
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" %
|
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
|
-
|
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
|
-
|
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)
|
data/lib/hpsqrt/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2019-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|