hpsqrt 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2a73083839c9b2fce6ed374ef8616680f69bdbe2b715e783d59bc8005f794af
4
- data.tar.gz: d8d8af76ec6a07094b63320e84db270805fb7dcc1aa7d5c4cc13f52b4ffdaf97
3
+ metadata.gz: f80e0b8ac735fd3a75e12ec4de06a63b99fb497e0eb75a246376f43ec101d4e3
4
+ data.tar.gz: 2e35e71c15dd11d7573e1798ab71dbff83c0719f2b44c245e7d33c226a69e7f2
5
5
  SHA512:
6
- metadata.gz: 532ed7783b5ccde7f167b029d23f81c1d38c76cdef0f8ae85e92c2d97eb096b051bceec9c65426f2c0101b074d4b512a75bd6a4f56ddbaa7c053ca8f583af30a
7
- data.tar.gz: 8c78512a2df7e272b0ac9b1face766732c240fc40127503dfb661e52f85908766dba744c711221c2f484a619e7e9ce40832b55ba005e137c1a24c5bf5418d92a
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
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "bundler/setup"
4
4
  require "hpsqrt"
5
+ require "hpsqrt/core_ext"
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -1,6 +1,6 @@
1
1
  class HpSqrt < Numeric
2
2
 
3
- class Value
3
+ class Term
4
4
  attr_reader :number
5
5
  attr_reader :sqrt
6
6
 
@@ -1,3 +1,3 @@
1
1
  class HpSqrt < Numeric
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/hpsqrt.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'hpsqrt/inspect_mode'
2
- require 'hpsqrt/value'
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 :values
19
+ attr_reader :terms
20
20
 
21
- def initialize(values)
22
- @values = values.freeze
21
+ def initialize(terms)
22
+ @terms = terms.freeze
23
23
  freeze
24
24
  end
25
25
 
26
26
  def -@
27
- values = self.values.map{|v,c| [v, -c]}.to_h
28
- self.class.new(values)
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
- values = @values.merge(other.values) {|v, c1, c2|
34
+ terms = @terms.merge(other.terms) {|t, c1, c2|
35
35
  c1 + c2
36
36
  }
37
- values.delete_if {|v,c| c==0}
37
+ terms.delete_if {|t,c| c==0}
38
38
 
39
- self.class.new(values)
39
+ self.class.new(terms)
40
40
  end
41
41
 
42
42
  def -(other)
43
43
  other = self.class.create(other)
44
44
 
45
- other_values = other.values.map{|v,c| [v, -c]}.to_h
46
- values = @values.merge(other_values) {|v, c1, c2|
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
- values.delete_if {|v,c| v==0}
49
+ terms.delete_if {|t,c| c==0}
50
50
 
51
- self.class.new(values)
51
+ self.class.new(terms)
52
52
  end
53
53
 
54
54
  def *(other)
55
55
  other = self.class.create(other)
56
56
 
57
- values = {}
58
- @values.each {|v1, c1|
59
- other.values.each {|v2, c2|
60
- v = v1 * v2
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
- values[v] ||= 0
64
- values[v] += c
63
+ terms[t] ||= 0
64
+ terms[t] += c
65
65
  }
66
66
  }
67
- values.delete_if {|v,c| c==0}
67
+ terms.delete_if {|t,c| c==0}
68
68
 
69
- self.class.new(values)
69
+ self.class.new(terms)
70
70
  end
71
71
 
72
72
  def /(other)
73
73
  other = self.class.create(other)
74
- other_inv = Value.new(number: Rational(1, other.value))
74
+ other_inv = Term.new(number: Rational(1, other.to_c))
75
75
 
76
- values = {}
77
- @values.each {|v, c|
78
- v *= other_inv
76
+ terms = {}
77
+ @terms.each {|t, c|
78
+ t *= other_inv
79
79
 
80
- values[v] ||= 0
81
- values[v] += c
80
+ terms[t] ||= 0
81
+ terms[t] += c
82
82
  }
83
- values.delete_if {|v,c| c==0}
83
+ terms.delete_if {|t,c| c==0}
84
84
 
85
- self.class.new(values)
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.value ** other.value)
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.value==other.value
112
+ self.to_c==other.to_c
113
113
  elsif Numeric===other
114
- self.value==other
114
+ self.to_c==other
115
115
  else
116
116
  super.==(other)
117
117
  end
118
118
  end
119
119
 
120
- def value
121
- @values.map {|v, c|
122
- v.number * Math.sqrt(Complex(v.sqrt)) * c
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
- value.real
127
+ to_c.real
128
128
  end
129
129
 
130
130
  def imag
131
- Complex(value).imag
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
- value.to_r
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
- elsif Rational===v
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 = @values.map {|v, c|
175
- n = v.number * c
176
- s = v.sqrt
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
- value.to_s
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, value, expr]
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
- v = value
225
- is_imag = Complex===v && !v.imag.zero?
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
- v = value
231
- is_imag = Complex===v && !v.imag.zero?
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
- new({Value.new(number: v) => 1})
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
- new({Value.new(sqrt: v) => 1})
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.1.0
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 00:00:00.000000000 Z
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/value.rb
74
+ - lib/hpsqrt/term.rb
75
75
  - lib/hpsqrt/version.rb
76
76
  homepage: https://github.com/yoshida-eth0/ruby-sqrt
77
77
  licenses: