quadratic_number 0.0.1
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +56 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/quadratic_number/imag.rb +81 -0
- data/lib/quadratic_number/real.rb +78 -0
- data/lib/quadratic_number.rb +324 -0
- data/quadratic_number.gemspec +25 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 705d8a42df709867ad6b91d3640cef83984ef48b
|
4
|
+
data.tar.gz: 34a4ac05039bdbbc0a79b8b5d3b3f3e9a22a9fd0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a324310465b08035e341a90d58fe1eefcfa89fce53c2e16384794090a7a1cc8c78f6d628dcaeede2e39d9d0c7b63f545443618c31b597596075344e361de9165
|
7
|
+
data.tar.gz: 29bd1b248cffc3b4fb7678ae609186323fd30851bac9fec0414e116981b40cd7f408f0d26fb9b93216cb66acceaac76fe732f76d952266ada9cbde0f64b5e408
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Masahiro Nomoto
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Quadratic
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'quadratic_number'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install quadratic_number
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'quadratic_number'
|
23
|
+
|
24
|
+
### construction ###
|
25
|
+
phi = Quadratic[5].new(1, 1) / 2 #=> ((1/2)+(1/2)*√5)
|
26
|
+
omega = Quadratic[-3].new(-1, 1) / 2 #=> ((-1/2)+(1/2)*√-3)
|
27
|
+
|
28
|
+
### type conversion ###
|
29
|
+
phi.to_f #=> 1.618033988749895
|
30
|
+
omega.to_c #=> ((-1/2)+(0+(1/2)*√3)*i)
|
31
|
+
omega * 1.0 #=> (-0.5+0.8660254037844386i)
|
32
|
+
|
33
|
+
### calculation ###
|
34
|
+
phi ** 2 == phi + 1 #=> true
|
35
|
+
omega ** 2 + omega + 1 #=> ((0/1)+(0/1)*√-3)
|
36
|
+
phi * omega #=> (-0.8090169943749475+1.4012585384440734i)
|
37
|
+
|
38
|
+
### quadratic equation ###
|
39
|
+
puts "x^2 - #{phi.trace} x + #{phi.norm} = 0 <--> x = #{phi}, #{phi.qconj}"
|
40
|
+
```
|
41
|
+
|
42
|
+
## Development
|
43
|
+
|
44
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
45
|
+
|
46
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hmmnrst/quadratic_number.
|
51
|
+
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
56
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "quadratic_number"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require_relative '../quadratic_number'
|
2
|
+
|
3
|
+
class Quadratic::Imag # < Quadratic < Numeric
|
4
|
+
### Classification ###
|
5
|
+
|
6
|
+
# defined by Numeric:
|
7
|
+
# * integer? #=> false
|
8
|
+
|
9
|
+
def real?
|
10
|
+
false
|
11
|
+
end
|
12
|
+
|
13
|
+
### Basic arithmetic operations ###
|
14
|
+
|
15
|
+
undef div, modulo, %, remainder, divmod
|
16
|
+
undef floor, ceil, round, truncate
|
17
|
+
|
18
|
+
### Comparisons ###
|
19
|
+
|
20
|
+
undef <=>
|
21
|
+
undef_method(*Comparable.public_instance_methods(false))
|
22
|
+
def ==(other)
|
23
|
+
if other.kind_of?(Numeric)
|
24
|
+
self.to_c - other == 0
|
25
|
+
else
|
26
|
+
other == self
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
defined_methods = public_instance_methods
|
31
|
+
undef positive? if defined_methods.include?(:positive?)
|
32
|
+
undef negative? if defined_methods.include?(:negative?)
|
33
|
+
|
34
|
+
undef step
|
35
|
+
|
36
|
+
### Type conversions ###
|
37
|
+
|
38
|
+
def to_c
|
39
|
+
d = self.class::D
|
40
|
+
if d == -1
|
41
|
+
Complex.rect(@a, @b)
|
42
|
+
else
|
43
|
+
rev_class = Quadratic[-d]
|
44
|
+
Complex.rect(@a, rev_class.new(0, @b))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
{
|
49
|
+
:to_f => Float,
|
50
|
+
:to_r => Rational,
|
51
|
+
:rationalize => Rational,
|
52
|
+
:to_i => Integer
|
53
|
+
}.each do |sym,klass|
|
54
|
+
define_method(sym) do |*args|
|
55
|
+
if @b != 0
|
56
|
+
raise RangeError,
|
57
|
+
"can't convert #{self} into #{klass}"
|
58
|
+
end
|
59
|
+
@a.send(sym, *args)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
### Complex operations ###
|
64
|
+
|
65
|
+
alias conj qconj
|
66
|
+
alias conjugate conj
|
67
|
+
alias abs2 norm
|
68
|
+
|
69
|
+
[:rect, :real, :imag, :abs, :arg, :polar].each do |sym|
|
70
|
+
define_method(sym) do
|
71
|
+
to_c.send(sym)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
alias rectangular rect
|
75
|
+
alias imaginary imag
|
76
|
+
alias magnitude abs
|
77
|
+
alias angle arg
|
78
|
+
alias phase arg
|
79
|
+
|
80
|
+
undef i
|
81
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require_relative '../quadratic_number'
|
2
|
+
|
3
|
+
class Quadratic::Real # < Quadratic < Numeric
|
4
|
+
### Classification ###
|
5
|
+
|
6
|
+
# defined by Numeric:
|
7
|
+
# * integer? #=> false
|
8
|
+
# * real? #=> true
|
9
|
+
|
10
|
+
### Basic arithmetic operations ###
|
11
|
+
|
12
|
+
# defined by Numeric:
|
13
|
+
# * div #=> (self / other).floor
|
14
|
+
# * modulo, % #=> self - self.div(other) * other
|
15
|
+
# * remainder
|
16
|
+
# * divmod
|
17
|
+
# * floor #=> to_f.floor
|
18
|
+
# * ceil
|
19
|
+
# * round
|
20
|
+
# * truncate
|
21
|
+
|
22
|
+
### Comparisons ###
|
23
|
+
|
24
|
+
def <=>(other)
|
25
|
+
if other.kind_of?(Numeric)
|
26
|
+
if other.real?
|
27
|
+
(self - other).to_f <=> 0
|
28
|
+
else
|
29
|
+
n1, n2 = other.coerce(self)
|
30
|
+
n1 <=> n2
|
31
|
+
end
|
32
|
+
else
|
33
|
+
nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def ==(other)
|
38
|
+
if other.kind_of?(Numeric) && other.real?
|
39
|
+
(self - other).to_f == 0
|
40
|
+
else
|
41
|
+
other == self
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
### Type conversions ###
|
46
|
+
|
47
|
+
# defined by Numeric:
|
48
|
+
# * to_c #=> Complex.rect(self, 0)
|
49
|
+
|
50
|
+
def to_f
|
51
|
+
d = self.class::D
|
52
|
+
if @a * @b < 0
|
53
|
+
# Using #fdiv instead of #/
|
54
|
+
# because Rational#/ returns Rational for a big Float divisor in ruby2.0
|
55
|
+
(@a * @a - @b * @b * d).fdiv(@a - @b * Math.sqrt(d))
|
56
|
+
else
|
57
|
+
@a + @b * Math.sqrt(d)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
[:to_r, :rationalize, :to_i].each do |sym|
|
62
|
+
define_method(sym) do |*args|
|
63
|
+
(@b == 0 ? @a : to_f).send(sym, *args)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
### Complex operations ###
|
68
|
+
|
69
|
+
# defined by Numeric:
|
70
|
+
# * conj, conjugate #=> self
|
71
|
+
# * abs2 #=> self * self
|
72
|
+
# * rect, rectangular #=> [self, 0]
|
73
|
+
# * real #=> self
|
74
|
+
# * imag, imaginary #=> 0
|
75
|
+
# * abs, magnitude #=> self.negative? ? -self : self
|
76
|
+
# * arg, angle, phase #=> self.to_f >= +0.0 ? 0 : Math::PI
|
77
|
+
# * polar #=> [abs, arg]
|
78
|
+
end
|
@@ -0,0 +1,324 @@
|
|
1
|
+
require 'prime' # Prime.prime_division
|
2
|
+
|
3
|
+
class Quadratic < Numeric
|
4
|
+
@@classes = {}
|
5
|
+
|
6
|
+
def self.[](d)
|
7
|
+
# return a memoized subclass if exists
|
8
|
+
return @@classes[d] if @@classes[d]
|
9
|
+
|
10
|
+
unless d.kind_of?(Integer)
|
11
|
+
raise TypeError, 'not an integer'
|
12
|
+
end
|
13
|
+
|
14
|
+
if d == 0 || d == 1 ||
|
15
|
+
Prime.prime_division(d).any? { |p,k| k > 1 }
|
16
|
+
raise RangeError, 'd must be square-free other than 0 or 1'
|
17
|
+
end
|
18
|
+
|
19
|
+
# memoize a new subclass and return it
|
20
|
+
base = (d >= 0) ? Real : Imag
|
21
|
+
@@classes[d] = Class.new(base) do
|
22
|
+
# In this scope, `self` indicates a concrete subclass.
|
23
|
+
self.const_set(:D, d)
|
24
|
+
|
25
|
+
class << self
|
26
|
+
def name
|
27
|
+
"Quadratic[#{self::D}]"
|
28
|
+
end
|
29
|
+
alias to_s name
|
30
|
+
alias inspect name
|
31
|
+
|
32
|
+
public :new
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
Real = Class.new(self) { class << self; undef []; end }
|
38
|
+
Imag = Class.new(self) { class << self; undef []; end }
|
39
|
+
|
40
|
+
attr_reader :a, :b
|
41
|
+
protected :a, :b
|
42
|
+
|
43
|
+
def initialize(a, b = 0)
|
44
|
+
unless [a, b].all? { |x| __rational__(x) }
|
45
|
+
raise TypeError, "not a rational"
|
46
|
+
end
|
47
|
+
@a = a
|
48
|
+
@b = b
|
49
|
+
end
|
50
|
+
private_class_method :new
|
51
|
+
|
52
|
+
### Basic arithmetic operations ###
|
53
|
+
|
54
|
+
def coerce(other)
|
55
|
+
my_class = self.class
|
56
|
+
if other.kind_of?(my_class)
|
57
|
+
# my_class
|
58
|
+
[other, self]
|
59
|
+
elsif __rational__(other)
|
60
|
+
# Integer and Rational
|
61
|
+
[my_class.new(other, 0), self]
|
62
|
+
elsif other.kind_of?(Quadratic)
|
63
|
+
# Quadratic::Real and Quadratic::Imag
|
64
|
+
if self.real? && other.real?
|
65
|
+
[other.to_f, self.to_f]
|
66
|
+
else
|
67
|
+
[other.to_c, self.to_c]
|
68
|
+
end
|
69
|
+
elsif __real__(other)
|
70
|
+
# Float and BigDecimal
|
71
|
+
[other, self.to_builtin]
|
72
|
+
elsif __complex__(other)
|
73
|
+
# Complex
|
74
|
+
[other, self.to_c]
|
75
|
+
else
|
76
|
+
# others
|
77
|
+
raise TypeError,
|
78
|
+
"#{other.class} can't be coerced into #{self.class}"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def +(other)
|
83
|
+
my_class = self.class
|
84
|
+
if other.kind_of?(my_class)
|
85
|
+
my_class.new(@a + other.a, @b + other.b)
|
86
|
+
elsif __rational__(other)
|
87
|
+
my_class.new(@a + other, @b)
|
88
|
+
else
|
89
|
+
__coerce_exec__(:+, other)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def -(other)
|
94
|
+
my_class = self.class
|
95
|
+
if other.kind_of?(my_class)
|
96
|
+
my_class.new(@a - other.a, @b - other.b)
|
97
|
+
elsif __rational__(other)
|
98
|
+
my_class.new(@a - other, @b)
|
99
|
+
else
|
100
|
+
__coerce_exec__(:-, other)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def *(other)
|
105
|
+
my_class = self.class
|
106
|
+
if other.kind_of?(my_class)
|
107
|
+
_a = other.a
|
108
|
+
_b = other.b
|
109
|
+
my_class.new(@a * _a + @b * _b * my_class::D, @a * _b + @b * _a)
|
110
|
+
elsif __rational__(other)
|
111
|
+
my_class.new(@a * other, @b * other)
|
112
|
+
else
|
113
|
+
__coerce_exec__(:*, other)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def quo(other)
|
118
|
+
my_class = self.class
|
119
|
+
if other.kind_of?(my_class)
|
120
|
+
_a = other.a
|
121
|
+
_b = other.b
|
122
|
+
d = _a * _a - _b * _b * my_class::D
|
123
|
+
self * my_class.new(_a.quo(d), -_b.quo(d))
|
124
|
+
elsif __rational__(other)
|
125
|
+
my_class.new(@a.quo(other), @b.quo(other))
|
126
|
+
else
|
127
|
+
__coerce_exec__(:quo, other)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
alias / quo
|
131
|
+
|
132
|
+
def fdiv(other)
|
133
|
+
if other.kind_of?(Quadratic)
|
134
|
+
self.to_builtin.fdiv(other.to_builtin)
|
135
|
+
elsif other.kind_of?(Numeric)
|
136
|
+
self.to_builtin.fdiv(other)
|
137
|
+
else
|
138
|
+
n1, n2 = other.coerce(self)
|
139
|
+
n1.fdiv(other)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def **(index)
|
144
|
+
unless index.kind_of?(Numeric)
|
145
|
+
num1, num2 = other.coerce(self)
|
146
|
+
return num1 ** num2
|
147
|
+
end
|
148
|
+
|
149
|
+
# return 1 if index is exactly zero
|
150
|
+
begin
|
151
|
+
1 / index
|
152
|
+
rescue ZeroDivisionError
|
153
|
+
return self.class.new(1, 0)
|
154
|
+
end
|
155
|
+
|
156
|
+
# complex -> real
|
157
|
+
begin
|
158
|
+
index.to_f
|
159
|
+
rescue
|
160
|
+
else
|
161
|
+
index = index.real
|
162
|
+
end
|
163
|
+
|
164
|
+
# quadratic -> rational or integer / float or complex
|
165
|
+
if index.kind_of?(Quadratic) && index.b == 0
|
166
|
+
if index.b == 0
|
167
|
+
index = index.a
|
168
|
+
else
|
169
|
+
index = index.to_builtin
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# rational -> integer
|
174
|
+
if index.kind_of?(Rational) && index.denominator == 1
|
175
|
+
index = index.numerator
|
176
|
+
end
|
177
|
+
|
178
|
+
if index.integer?
|
179
|
+
# binary method
|
180
|
+
x = (index >= 0) ? self : 1 / self
|
181
|
+
n = index.abs
|
182
|
+
|
183
|
+
z = self.class.new(1, 0)
|
184
|
+
while true
|
185
|
+
n, i = n.divmod(2)
|
186
|
+
z *= x if i == 1
|
187
|
+
return z if n == 0
|
188
|
+
x *= x
|
189
|
+
end
|
190
|
+
else
|
191
|
+
return self.to_builtin ** index
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
### Comparisons ###
|
196
|
+
|
197
|
+
def eql?(other)
|
198
|
+
if other.kind_of?(self.class)
|
199
|
+
@a.eql?(other.a) && @b.eql?(other.b)
|
200
|
+
else
|
201
|
+
false
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def hash
|
206
|
+
[@a, @b, self.class::D].hash
|
207
|
+
end
|
208
|
+
|
209
|
+
# defined by Numeric:
|
210
|
+
# * zero? #=> self == 0
|
211
|
+
# * nonzero? #=> zero? ? nil : self
|
212
|
+
# * positive? #=> self > 0
|
213
|
+
# * negative? #=> self < 0
|
214
|
+
# * finite? #=> true
|
215
|
+
# * infinite? #=> nil
|
216
|
+
|
217
|
+
### Type conversions ###
|
218
|
+
|
219
|
+
def to_builtin
|
220
|
+
real? ? to_f : to_c
|
221
|
+
end
|
222
|
+
protected :to_builtin
|
223
|
+
|
224
|
+
def inspect
|
225
|
+
'(' << __format__(:inspect) << ')'
|
226
|
+
end
|
227
|
+
alias to_s inspect
|
228
|
+
|
229
|
+
### Utilities ###
|
230
|
+
|
231
|
+
def denominator
|
232
|
+
ad = @a.denominator
|
233
|
+
bd = @b.denominator
|
234
|
+
ad.lcm(bd)
|
235
|
+
end
|
236
|
+
|
237
|
+
def numerator
|
238
|
+
an = @a.numerator
|
239
|
+
ad = @a.denominator
|
240
|
+
bn = @b.numerator
|
241
|
+
bd = @b.denominator
|
242
|
+
abd = ad.lcm(bd)
|
243
|
+
self.class.new(an * (abd / ad), bn * (abd / bd))
|
244
|
+
end
|
245
|
+
|
246
|
+
### Extensions for Quadratic ###
|
247
|
+
|
248
|
+
def qconj
|
249
|
+
self.class.new(@a, -@b)
|
250
|
+
end
|
251
|
+
alias quadratic_conjugate qconj
|
252
|
+
|
253
|
+
def trace
|
254
|
+
# self + self.qconj
|
255
|
+
@a * 2
|
256
|
+
end
|
257
|
+
|
258
|
+
def norm
|
259
|
+
# self * self.qconj
|
260
|
+
@a * @a - @b * @b * self.class::D
|
261
|
+
end
|
262
|
+
alias qabs2 norm
|
263
|
+
alias quadratic_abs2 qabs2
|
264
|
+
|
265
|
+
def discriminant
|
266
|
+
# trace ** 2 - 4 * norm
|
267
|
+
@b * @b * (self.class::D * 4)
|
268
|
+
end
|
269
|
+
|
270
|
+
private
|
271
|
+
|
272
|
+
# Integer and Rational
|
273
|
+
def __rational__(x)
|
274
|
+
x.kind_of?(Integer) || x.kind_of?(Rational)
|
275
|
+
end
|
276
|
+
|
277
|
+
# Integer, Rational,
|
278
|
+
# Quadratic::Real, Float, and BigDecimal
|
279
|
+
def __real__(x)
|
280
|
+
x.kind_of?(Numeric) && x.real?
|
281
|
+
end
|
282
|
+
|
283
|
+
# Complex and Quadratic::Imag
|
284
|
+
def __complex__(x)
|
285
|
+
x.kind_of?(Complex) || x.kind_of?(Imag)
|
286
|
+
end
|
287
|
+
|
288
|
+
def __coerce_exec__(op, other)
|
289
|
+
if other.kind_of?(Quadratic)
|
290
|
+
if self.real? && other.real?
|
291
|
+
n1 = self.to_f
|
292
|
+
n2 = other.to_f
|
293
|
+
else
|
294
|
+
n1 = self.to_c
|
295
|
+
n2 = other.to_c
|
296
|
+
end
|
297
|
+
elsif __real__(other)
|
298
|
+
n1 = self.to_builtin
|
299
|
+
n2 = other
|
300
|
+
elsif __complex__(other)
|
301
|
+
n1 = self.to_c
|
302
|
+
n2 = other
|
303
|
+
else
|
304
|
+
n1, n2 = other.coerce(self)
|
305
|
+
end
|
306
|
+
|
307
|
+
n1.send(op, n2)
|
308
|
+
end
|
309
|
+
|
310
|
+
def __format__(sym)
|
311
|
+
str = ''
|
312
|
+
str << @a.send(sym)
|
313
|
+
if @b >= 0
|
314
|
+
str << '+' << @b.send(sym)
|
315
|
+
else
|
316
|
+
str << '-' << (-@b).send(sym)
|
317
|
+
end
|
318
|
+
str << "*" if /\D\z/ === str
|
319
|
+
str << "\u221a#{self.class::D}"
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
require_relative 'quadratic_number/real'
|
324
|
+
require_relative 'quadratic_number/imag'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "quadratic_number"
|
6
|
+
spec.version = "0.0.1"
|
7
|
+
spec.authors = ["Masahiro Nomoto"]
|
8
|
+
spec.email = ["hmmnrst@users.noreply.github.com"]
|
9
|
+
|
10
|
+
spec.summary = %q{Quadratic class}
|
11
|
+
spec.description = %q{Provides a numeric class Quadratic to represent a+b\u221ad exactly}
|
12
|
+
spec.homepage = "https://github.com/hmmnrst/quadratic_number"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: quadratic_number
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masahiro Nomoto
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Provides a numeric class Quadratic to represent a+b\u221ad exactly
|
56
|
+
email:
|
57
|
+
- hmmnrst@users.noreply.github.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- .travis.yml
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/quadratic_number.rb
|
72
|
+
- lib/quadratic_number/imag.rb
|
73
|
+
- lib/quadratic_number/real.rb
|
74
|
+
- quadratic_number.gemspec
|
75
|
+
homepage: https://github.com/hmmnrst/quadratic_number
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.0.14
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Quadratic class
|
99
|
+
test_files: []
|