hpsqrt 1.1.0 → 1.2.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 +4 -4
- data/README.md +61 -0
- data/bin/console +1 -0
- data/lib/hpsqrt/{value.rb → term.rb} +1 -1
- data/lib/hpsqrt/version.rb +1 -1
- data/lib/hpsqrt.rb +72 -57
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f80e0b8ac735fd3a75e12ec4de06a63b99fb497e0eb75a246376f43ec101d4e3
|
4
|
+
data.tar.gz: 2e35e71c15dd11d7573e1798ab71dbff83c0719f2b44c245e7d33c226a69e7f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd88c719dc477c9371103b9071849da9378ff998b40fe88c974bcf79f86cf940498b493f89ee322ec094d18a98ab4ba8ccbe8309d63e609bc75a72a73c68d2d7
|
7
|
+
data.tar.gz: 53323b844461f1638ea8df0bbbc39b1f73e9dde397727e97bdf1fde432ee8a1531518169670fd7dc086e461b5f96c443669ee084ddb3f088ea07cd2d490cf952
|
data/README.md
CHANGED
@@ -20,6 +20,8 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
+
Calculation:
|
24
|
+
|
23
25
|
require 'hpsqrt/core_ext'
|
24
26
|
|
25
27
|
p Sqrt(2)
|
@@ -37,6 +39,65 @@ Or install it yourself as:
|
|
37
39
|
p (Sqrt(7) + Sqrt(11)) * (Sqrt(7) - Sqrt(11))
|
38
40
|
# => -4.0+0.0i
|
39
41
|
|
42
|
+
Type casting:
|
43
|
+
|
44
|
+
require 'hpsqrt/core_ext'
|
45
|
+
|
46
|
+
# to Float
|
47
|
+
p Sqrt(5).to_f
|
48
|
+
# => 2.23606797749979
|
49
|
+
|
50
|
+
# to Integer
|
51
|
+
p Sqrt(5).to_i
|
52
|
+
# => 2
|
53
|
+
|
54
|
+
# to Rational
|
55
|
+
p Sqrt(4).to_r
|
56
|
+
# => (2/1)
|
57
|
+
|
58
|
+
# to Complex
|
59
|
+
p Sqrt(-1i).to_c
|
60
|
+
# => (0.7071067811865476-0.7071067811865476i)
|
61
|
+
|
62
|
+
# to Complex real number
|
63
|
+
p Sqrt(-1i).real
|
64
|
+
# => 0.7071067811865476
|
65
|
+
|
66
|
+
# to Complex imaginary number
|
67
|
+
p Sqrt(1i).imag
|
68
|
+
# => -0.7071067811865476
|
69
|
+
|
70
|
+
p (Sqrt(5) + Sqrt(7)).expr
|
71
|
+
# => "√5 + √7"
|
72
|
+
|
73
|
+
Type check:
|
74
|
+
|
75
|
+
require 'hpsqrt/core_ext'
|
76
|
+
|
77
|
+
# real? returns true if imaginary number is 0
|
78
|
+
p Sqrt(5).real?
|
79
|
+
# => true
|
80
|
+
p Sqrt(1i).real?
|
81
|
+
# => false
|
82
|
+
|
83
|
+
# imag? returns true if imaginary number is not 0
|
84
|
+
p Sqrt(5).imag?
|
85
|
+
# => false
|
86
|
+
p Sqrt(1i).imag?
|
87
|
+
# => true
|
88
|
+
|
89
|
+
# int? return true if after the real decimal point is 0 and imaginary number is 0
|
90
|
+
p Sqrt(2).int?
|
91
|
+
# => false
|
92
|
+
p Sqrt(4).int?
|
93
|
+
# => true
|
94
|
+
|
95
|
+
# float? return true if after the real decimal point is not 0 and imaginary number is 0
|
96
|
+
p Sqrt(2).float?
|
97
|
+
# => true
|
98
|
+
p Sqrt(4).float?
|
99
|
+
# => false
|
100
|
+
|
40
101
|
## Contributing
|
41
102
|
|
42
103
|
Bug reports and pull requests are welcome on GitHub at https://github.com/yoshida-eth0/ruby-sqrt.
|
data/bin/console
CHANGED
data/lib/hpsqrt/version.rb
CHANGED
data/lib/hpsqrt.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'hpsqrt/inspect_mode'
|
2
|
-
require 'hpsqrt/
|
2
|
+
require 'hpsqrt/term'
|
3
3
|
require 'hpsqrt/version'
|
4
4
|
|
5
5
|
|
@@ -16,73 +16,73 @@ class HpSqrt < Numeric
|
|
16
16
|
end
|
17
17
|
|
18
18
|
|
19
|
-
attr_reader :
|
19
|
+
attr_reader :terms
|
20
20
|
|
21
|
-
def initialize(
|
22
|
-
@
|
21
|
+
def initialize(terms)
|
22
|
+
@terms = terms.freeze
|
23
23
|
freeze
|
24
24
|
end
|
25
25
|
|
26
26
|
def -@
|
27
|
-
|
28
|
-
self.class.new(
|
27
|
+
terms = @terms.map{|t,c| [t, -c]}.to_h
|
28
|
+
self.class.new(terms)
|
29
29
|
end
|
30
30
|
|
31
31
|
def +(other)
|
32
32
|
other = self.class.create(other)
|
33
33
|
|
34
|
-
|
34
|
+
terms = @terms.merge(other.terms) {|t, c1, c2|
|
35
35
|
c1 + c2
|
36
36
|
}
|
37
|
-
|
37
|
+
terms.delete_if {|t,c| c==0}
|
38
38
|
|
39
|
-
self.class.new(
|
39
|
+
self.class.new(terms)
|
40
40
|
end
|
41
41
|
|
42
42
|
def -(other)
|
43
43
|
other = self.class.create(other)
|
44
44
|
|
45
|
-
|
46
|
-
|
45
|
+
other_terms = other.terms.map{|t,c| [t, -c]}.to_h
|
46
|
+
terms = @terms.merge(other_terms) {|t, c1, c2|
|
47
47
|
c1 + c2
|
48
48
|
}
|
49
|
-
|
49
|
+
terms.delete_if {|t,c| c==0}
|
50
50
|
|
51
|
-
self.class.new(
|
51
|
+
self.class.new(terms)
|
52
52
|
end
|
53
53
|
|
54
54
|
def *(other)
|
55
55
|
other = self.class.create(other)
|
56
56
|
|
57
|
-
|
58
|
-
@
|
59
|
-
other.
|
60
|
-
|
57
|
+
terms = {}
|
58
|
+
@terms.each {|t1, c1|
|
59
|
+
other.terms.each {|t2, c2|
|
60
|
+
t = t1 * t2
|
61
61
|
c = c1 * c2
|
62
62
|
|
63
|
-
|
64
|
-
|
63
|
+
terms[t] ||= 0
|
64
|
+
terms[t] += c
|
65
65
|
}
|
66
66
|
}
|
67
|
-
|
67
|
+
terms.delete_if {|t,c| c==0}
|
68
68
|
|
69
|
-
self.class.new(
|
69
|
+
self.class.new(terms)
|
70
70
|
end
|
71
71
|
|
72
72
|
def /(other)
|
73
73
|
other = self.class.create(other)
|
74
|
-
other_inv =
|
74
|
+
other_inv = Term.new(number: Rational(1, other.to_c))
|
75
75
|
|
76
|
-
|
77
|
-
@
|
78
|
-
|
76
|
+
terms = {}
|
77
|
+
@terms.each {|t, c|
|
78
|
+
t *= other_inv
|
79
79
|
|
80
|
-
|
81
|
-
|
80
|
+
terms[t] ||= 0
|
81
|
+
terms[t] += c
|
82
82
|
}
|
83
|
-
|
83
|
+
terms.delete_if {|t,c| c==0}
|
84
84
|
|
85
|
-
self.class.new(
|
85
|
+
self.class.new(terms)
|
86
86
|
end
|
87
87
|
|
88
88
|
def **(other)
|
@@ -99,7 +99,7 @@ class HpSqrt < Numeric
|
|
99
99
|
end
|
100
100
|
result
|
101
101
|
else
|
102
|
-
self.class.number(self.
|
102
|
+
self.class.number(self.to_c ** other.to_c)
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
@@ -109,26 +109,26 @@ class HpSqrt < Numeric
|
|
109
109
|
|
110
110
|
def ==(other)
|
111
111
|
if self.class==other
|
112
|
-
self.
|
112
|
+
self.to_c==other.to_c
|
113
113
|
elsif Numeric===other
|
114
|
-
self.
|
114
|
+
self.to_c==other
|
115
115
|
else
|
116
116
|
super.==(other)
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
120
|
-
def
|
121
|
-
@
|
122
|
-
|
123
|
-
}.sum
|
120
|
+
def to_c
|
121
|
+
@terms.map {|t, c|
|
122
|
+
t.number * Math.sqrt(Complex(t.sqrt)) * c
|
123
|
+
}.sum.to_c
|
124
124
|
end
|
125
125
|
|
126
126
|
def real
|
127
|
-
|
127
|
+
to_c.real
|
128
128
|
end
|
129
129
|
|
130
130
|
def imag
|
131
|
-
|
131
|
+
to_c.imag
|
132
132
|
end
|
133
133
|
|
134
134
|
def to_i
|
@@ -149,19 +149,21 @@ class HpSqrt < Numeric
|
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
152
|
-
def to_c
|
153
|
-
value.to_c
|
154
|
-
end
|
155
|
-
|
156
152
|
def to_r
|
157
|
-
|
153
|
+
c = to_c
|
154
|
+
if c.imag.zero?
|
155
|
+
Rational(c.real, 1)
|
156
|
+
else
|
157
|
+
raise RangeError, "can't convert %s into Rational" % c
|
158
|
+
end
|
158
159
|
end
|
159
160
|
|
160
161
|
def expr
|
161
162
|
value_to_s = -> (v) {
|
162
163
|
if Complex===v && v.imag.zero?
|
163
164
|
v = v.real
|
164
|
-
|
165
|
+
end
|
166
|
+
if Rational===v
|
165
167
|
v = v.to_s.sub(/\/1$/, "")
|
166
168
|
end
|
167
169
|
v = v.to_s
|
@@ -171,9 +173,9 @@ class HpSqrt < Numeric
|
|
171
173
|
v
|
172
174
|
}
|
173
175
|
|
174
|
-
result = @
|
175
|
-
n =
|
176
|
-
s =
|
176
|
+
result = @terms.map {|t, c|
|
177
|
+
n = t.number * c
|
178
|
+
s = t.sqrt
|
177
179
|
|
178
180
|
if s!=1
|
179
181
|
if n==1
|
@@ -204,11 +206,11 @@ class HpSqrt < Numeric
|
|
204
206
|
def to_s
|
205
207
|
case @@inspect_mode
|
206
208
|
when INSPECT_MODE::VALUE
|
207
|
-
|
209
|
+
to_c.to_s
|
208
210
|
when INSPECT_MODE::EXPR
|
209
211
|
expr
|
210
212
|
else
|
211
|
-
"#<%s:0x%016x value=(%s) expr=(%s)>" % [self.class.name, self.object_id,
|
213
|
+
"#<%s:0x%016x value=(%s) expr=(%s)>" % [self.class.name, self.object_id, to_c, expr]
|
212
214
|
end
|
213
215
|
end
|
214
216
|
|
@@ -221,15 +223,13 @@ class HpSqrt < Numeric
|
|
221
223
|
end
|
222
224
|
|
223
225
|
def int?
|
224
|
-
|
225
|
-
|
226
|
-
!is_imag && v.real==v.real.to_i
|
226
|
+
c = to_c
|
227
|
+
c.imag.zero? && c.real==c.real.to_i
|
227
228
|
end
|
228
229
|
|
229
230
|
def float?
|
230
|
-
|
231
|
-
|
232
|
-
!is_imag && Float===v.real && v.real!=v.real.to_i
|
231
|
+
c = to_c
|
232
|
+
c.imag.zero? && Float===c.real && c.real!=c.real.to_i
|
233
233
|
end
|
234
234
|
|
235
235
|
def self.create(v)
|
@@ -241,10 +241,25 @@ class HpSqrt < Numeric
|
|
241
241
|
end
|
242
242
|
|
243
243
|
def self.number(v)
|
244
|
-
|
244
|
+
if v!=0
|
245
|
+
new({Term.new(number: v) => 1})
|
246
|
+
else
|
247
|
+
zero
|
248
|
+
end
|
245
249
|
end
|
246
250
|
|
247
251
|
def self.sqrt(v)
|
248
|
-
|
252
|
+
if self===v
|
253
|
+
v = v.to_c
|
254
|
+
end
|
255
|
+
if v!=0
|
256
|
+
new({Term.new(sqrt: v) => 1})
|
257
|
+
else
|
258
|
+
zero
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
def self.zero
|
263
|
+
new({})
|
249
264
|
end
|
250
265
|
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.
|
4
|
+
version: 1.2.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-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,7 +71,7 @@ files:
|
|
71
71
|
- lib/hpsqrt.rb
|
72
72
|
- lib/hpsqrt/core_ext.rb
|
73
73
|
- lib/hpsqrt/inspect_mode.rb
|
74
|
-
- lib/hpsqrt/
|
74
|
+
- lib/hpsqrt/term.rb
|
75
75
|
- lib/hpsqrt/version.rb
|
76
76
|
homepage: https://github.com/yoshida-eth0/ruby-sqrt
|
77
77
|
licenses:
|