bigdecimal 2.0.0 → 4.1.3
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/LICENSE +56 -0
- data/bigdecimal.gemspec +37 -12
- data/ext/bigdecimal/bigdecimal.c +2538 -2932
- data/ext/bigdecimal/bigdecimal.h +112 -210
- data/ext/bigdecimal/bits.h +144 -0
- data/ext/bigdecimal/div.h +219 -0
- data/ext/bigdecimal/extconf.rb +40 -32
- data/ext/bigdecimal/feature.h +68 -0
- data/ext/bigdecimal/missing/dtoa.c +3509 -0
- data/ext/bigdecimal/missing.c +28 -0
- data/ext/bigdecimal/missing.h +106 -0
- data/ext/bigdecimal/ntt.h +191 -0
- data/ext/bigdecimal/static_assert.h +54 -0
- data/lib/bigdecimal/jacobian.rb +5 -3
- data/lib/bigdecimal/ludcmp.rb +2 -0
- data/lib/bigdecimal/math/erf.rb +291 -0
- data/lib/bigdecimal/math/gamma.rb +513 -0
- data/lib/bigdecimal/math.rb +618 -134
- data/lib/bigdecimal/newton.rb +2 -0
- data/lib/bigdecimal/util.rb +28 -23
- data/lib/bigdecimal.rb +404 -1
- data/sample/linear.rb +73 -37
- data/sample/nlsolve.rb +47 -30
- data/sample/pi.rb +2 -7
- data/sig/big_decimal.rbs +1504 -0
- data/sig/big_decimal_util.rbs +158 -0
- data/sig/big_math.rbs +423 -0
- metadata +23 -66
data/lib/bigdecimal/newton.rb
CHANGED
data/lib/bigdecimal/util.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# frozen_string_literal:
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
#
|
|
3
3
|
#--
|
|
4
4
|
# bigdecimal/util extends various native classes to provide the #to_d method,
|
|
@@ -18,7 +18,7 @@ class Integer < Numeric
|
|
|
18
18
|
#
|
|
19
19
|
# 42.to_d # => 0.42e2
|
|
20
20
|
#
|
|
21
|
-
# See also BigDecimal
|
|
21
|
+
# See also Kernel.BigDecimal.
|
|
22
22
|
#
|
|
23
23
|
def to_d
|
|
24
24
|
BigDecimal(self)
|
|
@@ -33,17 +33,21 @@ class Float < Numeric
|
|
|
33
33
|
#
|
|
34
34
|
# Returns the value of +float+ as a BigDecimal.
|
|
35
35
|
# The +precision+ parameter is used to determine the number of
|
|
36
|
-
# significant digits for the result
|
|
36
|
+
# significant digits for the result. When +precision+ is set to +0+,
|
|
37
|
+
# the number of digits to represent the float being converted is determined
|
|
38
|
+
# automatically.
|
|
39
|
+
# The default +precision+ is +0+.
|
|
37
40
|
#
|
|
38
41
|
# require 'bigdecimal'
|
|
39
42
|
# require 'bigdecimal/util'
|
|
40
43
|
#
|
|
41
44
|
# 0.5.to_d # => 0.5e0
|
|
45
|
+
# 1.234.to_d # => 0.1234e1
|
|
42
46
|
# 1.234.to_d(2) # => 0.12e1
|
|
43
47
|
#
|
|
44
|
-
# See also BigDecimal
|
|
48
|
+
# See also Kernel.BigDecimal.
|
|
45
49
|
#
|
|
46
|
-
def to_d(precision=
|
|
50
|
+
def to_d(precision=0)
|
|
47
51
|
BigDecimal(self, precision)
|
|
48
52
|
end
|
|
49
53
|
end
|
|
@@ -63,7 +67,7 @@ class String
|
|
|
63
67
|
# "123.45e1".to_d # => 0.12345e4
|
|
64
68
|
# "45.67 degrees".to_d # => 0.4567e2
|
|
65
69
|
#
|
|
66
|
-
# See also BigDecimal
|
|
70
|
+
# See also Kernel.BigDecimal.
|
|
67
71
|
#
|
|
68
72
|
def to_d
|
|
69
73
|
BigDecimal.interpret_loosely(self)
|
|
@@ -115,17 +119,20 @@ class Rational < Numeric
|
|
|
115
119
|
#
|
|
116
120
|
# Returns the value as a BigDecimal.
|
|
117
121
|
#
|
|
118
|
-
# The
|
|
119
|
-
# significant digits for the result.
|
|
122
|
+
# The +precision+ parameter is used to determine the number of
|
|
123
|
+
# significant digits for the result. When +precision+ is set to +0+,
|
|
124
|
+
# the number of digits to represent the float being converted is determined
|
|
125
|
+
# automatically.
|
|
126
|
+
# The default +precision+ is +0+.
|
|
120
127
|
#
|
|
121
128
|
# require 'bigdecimal'
|
|
122
129
|
# require 'bigdecimal/util'
|
|
123
130
|
#
|
|
124
131
|
# Rational(22, 7).to_d(3) # => 0.314e1
|
|
125
132
|
#
|
|
126
|
-
# See also BigDecimal
|
|
133
|
+
# See also Kernel.BigDecimal.
|
|
127
134
|
#
|
|
128
|
-
def to_d(precision)
|
|
135
|
+
def to_d(precision=0)
|
|
129
136
|
BigDecimal(self, precision)
|
|
130
137
|
end
|
|
131
138
|
end
|
|
@@ -137,29 +144,27 @@ class Complex < Numeric
|
|
|
137
144
|
# cmp.to_d(precision) -> bigdecimal
|
|
138
145
|
#
|
|
139
146
|
# Returns the value as a BigDecimal.
|
|
147
|
+
# If the imaginary part is not +0+, an error is raised
|
|
140
148
|
#
|
|
141
|
-
# The +precision+ parameter is
|
|
142
|
-
#
|
|
143
|
-
#
|
|
149
|
+
# The +precision+ parameter is used to determine the number of
|
|
150
|
+
# significant digits for the result. When +precision+ is set to +0+,
|
|
151
|
+
# the number of digits to represent the float being converted is determined
|
|
152
|
+
# automatically.
|
|
153
|
+
# The default +precision+ is +0+.
|
|
144
154
|
#
|
|
145
155
|
# require 'bigdecimal'
|
|
146
156
|
# require 'bigdecimal/util'
|
|
147
157
|
#
|
|
148
158
|
# Complex(0.1234567, 0).to_d(4) # => 0.1235e0
|
|
149
159
|
# Complex(Rational(22, 7), 0).to_d(3) # => 0.314e1
|
|
160
|
+
# Complex(1, 1).to_d # raises ArgumentError
|
|
150
161
|
#
|
|
151
|
-
# See also BigDecimal
|
|
162
|
+
# See also Kernel.BigDecimal.
|
|
152
163
|
#
|
|
153
|
-
def to_d(
|
|
154
|
-
BigDecimal(self) unless self.imag.zero? # to raise
|
|
164
|
+
def to_d(precision=0)
|
|
165
|
+
BigDecimal(self) unless self.imag.zero? # to raise error
|
|
155
166
|
|
|
156
|
-
|
|
157
|
-
case self.real
|
|
158
|
-
when Rational
|
|
159
|
-
BigDecimal(self.real) # to raise error
|
|
160
|
-
end
|
|
161
|
-
end
|
|
162
|
-
self.real.to_d(*args)
|
|
167
|
+
BigDecimal(self.real, precision)
|
|
163
168
|
end
|
|
164
169
|
end
|
|
165
170
|
|
data/lib/bigdecimal.rb
CHANGED
|
@@ -1 +1,404 @@
|
|
|
1
|
-
|
|
1
|
+
if RUBY_ENGINE == 'jruby'
|
|
2
|
+
JRuby::Util.load_ext("org.jruby.ext.bigdecimal.BigDecimalLibrary")
|
|
3
|
+
|
|
4
|
+
class BigDecimal
|
|
5
|
+
def _decimal_shift(i) # :nodoc:
|
|
6
|
+
to_java.move_point_right(i).to_d
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
else
|
|
10
|
+
require 'bigdecimal.so'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class BigDecimal
|
|
14
|
+
module Internal # :nodoc:
|
|
15
|
+
# Default extra precision for intermediate calculations
|
|
16
|
+
# This value is currently the same as BigDecimal.double_fig, but defined separately for future changes.
|
|
17
|
+
EXTRA_PREC = 16
|
|
18
|
+
|
|
19
|
+
# Coerce x to BigDecimal with the specified precision.
|
|
20
|
+
# TODO: some methods (example: BigMath.exp) require more precision than specified to coerce.
|
|
21
|
+
def self.coerce_to_bigdecimal(x, prec, method_name) # :nodoc:
|
|
22
|
+
case x
|
|
23
|
+
when BigDecimal
|
|
24
|
+
return x
|
|
25
|
+
when Integer, Float
|
|
26
|
+
return BigDecimal(x, 0)
|
|
27
|
+
when Rational
|
|
28
|
+
return BigDecimal(x, [prec, 2 * BigDecimal.double_fig].max)
|
|
29
|
+
end
|
|
30
|
+
raise ArgumentError, "#{x.inspect} can't be coerced into BigDecimal"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.coerce_validate_prec(prec, method_name, accept_zero: false) # :nodoc:
|
|
34
|
+
unless Integer === prec
|
|
35
|
+
original = prec
|
|
36
|
+
# Emulate Integer.try_convert for ruby < 3.1
|
|
37
|
+
if prec.respond_to?(:to_int)
|
|
38
|
+
prec = prec.to_int
|
|
39
|
+
else
|
|
40
|
+
raise TypeError, "no implicit conversion of #{original.class} into Integer"
|
|
41
|
+
end
|
|
42
|
+
raise TypeError, "can't convert #{original.class} to Integer" unless Integer === prec
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
if accept_zero
|
|
46
|
+
raise ArgumentError, "Negative precision for #{method_name}" if prec < 0
|
|
47
|
+
else
|
|
48
|
+
raise ArgumentError, "Zero or negative precision for #{method_name}" if prec <= 0
|
|
49
|
+
end
|
|
50
|
+
prec
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.infinity_computation_result # :nodoc:
|
|
54
|
+
if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_INFINITY)
|
|
55
|
+
raise FloatDomainError, "Computation results in 'Infinity'"
|
|
56
|
+
end
|
|
57
|
+
BigDecimal::INFINITY
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def self.underflow_computation_result # :nodoc:
|
|
61
|
+
if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_UNDERFLOW)
|
|
62
|
+
raise FloatDomainError, 'Exponent underflow'
|
|
63
|
+
end
|
|
64
|
+
BigDecimal(0)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.nan_computation_result # :nodoc:
|
|
68
|
+
if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_NaN)
|
|
69
|
+
raise FloatDomainError, "Computation results to 'NaN'"
|
|
70
|
+
end
|
|
71
|
+
BigDecimal::NAN
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Iteration for Newton's method with increasing precision
|
|
75
|
+
def self.newton_loop(prec, initial_precision: BigDecimal.double_fig / 2, safe_margin: 2) # :nodoc:
|
|
76
|
+
precs = []
|
|
77
|
+
while prec > initial_precision
|
|
78
|
+
precs << prec
|
|
79
|
+
prec = (precs.last + 1) / 2 + safe_margin
|
|
80
|
+
end
|
|
81
|
+
precs.reverse_each do |p|
|
|
82
|
+
yield p
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Calculates Math.log(x.to_f) considering large or small exponent
|
|
87
|
+
def self.float_log(x) # :nodoc:
|
|
88
|
+
Math.log(x._decimal_shift(-x.exponent).to_f) + x.exponent * Math.log(10)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Calculating Taylor series sum using binary splitting method
|
|
92
|
+
# Calculates f(x) = (x/d0)*(1+(x/d1)*(1+(x/d2)*(1+(x/d3)*(1+...))))
|
|
93
|
+
# x.n_significant_digits or ds.size must be small to be performant.
|
|
94
|
+
def self.taylor_sum_binary_splitting(x, ds, prec) # :nodoc:
|
|
95
|
+
fs = ds.map {|d| [0, BigDecimal(d)] }
|
|
96
|
+
# fs = [[a0, a1], [b0, b1], [c0, c1], ...]
|
|
97
|
+
# f(x) = a0/a1+(x/a1)*(1+b0/b1+(x/b1)*(1+c0/c1+(x/c1)*(1+d0/d1+(x/d1)*(1+...))))
|
|
98
|
+
while fs.size > 1
|
|
99
|
+
# Merge two adjacent fractions
|
|
100
|
+
# from: (1 + a0/a1 + x/a1 * (1 + b0/b1 + x/b1 * rest))
|
|
101
|
+
# to: (1 + (a0*b1+x*(b0+b1))/(a1*b1) + (x*x)/(a1*b1) * rest)
|
|
102
|
+
xn = xn ? xn.mult(xn, prec) : x
|
|
103
|
+
fs = fs.each_slice(2).map do |(a, b)|
|
|
104
|
+
b ||= [0, BigDecimal(1)._decimal_shift([xn.exponent, 0].max + 2)]
|
|
105
|
+
[
|
|
106
|
+
(a[0] * b[1]).add(xn * (b[0] + b[1]), prec),
|
|
107
|
+
a[1].mult(b[1], prec)
|
|
108
|
+
]
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
BigDecimal(fs[0][0]).div(fs[0][1], prec)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# call-seq:
|
|
116
|
+
# self ** other -> bigdecimal
|
|
117
|
+
#
|
|
118
|
+
# Returns the \BigDecimal value of +self+ raised to power +other+:
|
|
119
|
+
#
|
|
120
|
+
# b = BigDecimal('3.14')
|
|
121
|
+
# b ** 2 # => 0.98596e1
|
|
122
|
+
# b ** 2.0 # => 0.98596e1
|
|
123
|
+
# b ** Rational(2, 1) # => 0.98596e1
|
|
124
|
+
#
|
|
125
|
+
# Related: BigDecimal#power.
|
|
126
|
+
#
|
|
127
|
+
def **(y)
|
|
128
|
+
case y
|
|
129
|
+
when BigDecimal, Integer, Float, Rational
|
|
130
|
+
power(y)
|
|
131
|
+
when nil
|
|
132
|
+
raise TypeError, 'wrong argument type NilClass'
|
|
133
|
+
else
|
|
134
|
+
x, y = y.coerce(self)
|
|
135
|
+
x**y
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# call-seq:
|
|
140
|
+
# power(n)
|
|
141
|
+
# power(n, prec)
|
|
142
|
+
#
|
|
143
|
+
# Returns the value raised to the power of n.
|
|
144
|
+
#
|
|
145
|
+
# Also available as the operator **.
|
|
146
|
+
#
|
|
147
|
+
def power(y, prec = 0)
|
|
148
|
+
prec = Internal.coerce_validate_prec(prec, :power, accept_zero: true)
|
|
149
|
+
x = self
|
|
150
|
+
y = Internal.coerce_to_bigdecimal(y, prec.nonzero? || n_significant_digits, :power)
|
|
151
|
+
|
|
152
|
+
return Internal.nan_computation_result if x.nan? || y.nan?
|
|
153
|
+
return BigDecimal(1) if y.zero?
|
|
154
|
+
|
|
155
|
+
if y.infinite?
|
|
156
|
+
if x < 0
|
|
157
|
+
return BigDecimal(0) if x < -1 && y.negative?
|
|
158
|
+
return BigDecimal(0) if x > -1 && y.positive?
|
|
159
|
+
raise Math::DomainError, 'Result undefined for negative base raised to infinite power'
|
|
160
|
+
elsif x < 1
|
|
161
|
+
return y.positive? ? BigDecimal(0) : BigDecimal::Internal.infinity_computation_result
|
|
162
|
+
elsif x == 1
|
|
163
|
+
return BigDecimal(1)
|
|
164
|
+
else
|
|
165
|
+
return y.positive? ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0)
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
if x.infinite? && y < 0
|
|
170
|
+
# Computation result will be +0 or -0. Avoid overflow.
|
|
171
|
+
neg = x < 0 && y.frac.zero? && y % 2 == 1
|
|
172
|
+
return neg ? -BigDecimal(0) : BigDecimal(0)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
if x.zero?
|
|
176
|
+
return BigDecimal(1) if y.zero?
|
|
177
|
+
return BigDecimal(0) if y > 0
|
|
178
|
+
if y.frac.zero? && y % 2 == 1 && x.sign == -1
|
|
179
|
+
return -BigDecimal::Internal.infinity_computation_result
|
|
180
|
+
else
|
|
181
|
+
return BigDecimal::Internal.infinity_computation_result
|
|
182
|
+
end
|
|
183
|
+
elsif x < 0
|
|
184
|
+
if y.frac.zero?
|
|
185
|
+
if y % 2 == 0
|
|
186
|
+
return (-x).power(y, prec)
|
|
187
|
+
else
|
|
188
|
+
return -(-x).power(y, prec)
|
|
189
|
+
end
|
|
190
|
+
else
|
|
191
|
+
raise Math::DomainError, 'Computation results in complex number'
|
|
192
|
+
end
|
|
193
|
+
elsif x == 1
|
|
194
|
+
return BigDecimal(1)
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
limit = BigDecimal.limit
|
|
198
|
+
frac_part = y.frac
|
|
199
|
+
|
|
200
|
+
if frac_part.zero? && prec.zero? && limit.zero?
|
|
201
|
+
# Infinite precision calculation for `x ** int` and `x.power(int)`
|
|
202
|
+
int_part = y.fix.to_i
|
|
203
|
+
int_part = -int_part if (neg = int_part < 0)
|
|
204
|
+
ans = BigDecimal(1)
|
|
205
|
+
n = 1
|
|
206
|
+
xn = x
|
|
207
|
+
while true
|
|
208
|
+
ans *= xn if int_part.allbits?(n)
|
|
209
|
+
n <<= 1
|
|
210
|
+
break if n > int_part
|
|
211
|
+
xn *= xn
|
|
212
|
+
# Detect overflow/underflow before consuming infinite memory
|
|
213
|
+
if (xn.exponent.abs - 1) * int_part / n >= 0x7FFFFFFFFFFFFFFF
|
|
214
|
+
return ((xn.exponent > 0) ^ neg ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0)) * (int_part.even? || x > 0 ? 1 : -1)
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
return neg ? BigDecimal(1) / ans : ans
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
result_prec = prec.nonzero? || [x.n_significant_digits, y.n_significant_digits, BigDecimal.double_fig].max + BigDecimal.double_fig
|
|
221
|
+
result_prec = [result_prec, limit].min if prec.zero? && limit.nonzero?
|
|
222
|
+
|
|
223
|
+
prec2 = result_prec + BigDecimal::Internal::EXTRA_PREC
|
|
224
|
+
|
|
225
|
+
if y < 0
|
|
226
|
+
inv = x.power(-y, prec2)
|
|
227
|
+
return BigDecimal(0) if inv.infinite?
|
|
228
|
+
return BigDecimal::Internal.infinity_computation_result if inv.zero?
|
|
229
|
+
return BigDecimal(1).div(inv, result_prec)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
if frac_part.zero? && y.exponent < Math.log(result_prec) * 5 + 20
|
|
233
|
+
# Use exponentiation by squaring if y is an integer and not too large
|
|
234
|
+
pow_prec = prec2 + y.exponent
|
|
235
|
+
n = 1
|
|
236
|
+
xn = x
|
|
237
|
+
ans = BigDecimal(1)
|
|
238
|
+
int_part = y.fix.to_i
|
|
239
|
+
while true
|
|
240
|
+
ans = ans.mult(xn, pow_prec) if int_part.allbits?(n)
|
|
241
|
+
n <<= 1
|
|
242
|
+
break if n > int_part
|
|
243
|
+
xn = xn.mult(xn, pow_prec)
|
|
244
|
+
end
|
|
245
|
+
ans.mult(1, result_prec)
|
|
246
|
+
else
|
|
247
|
+
if x > 1 && x.finite?
|
|
248
|
+
# To calculate exp(z, prec), z needs prec+max(z.exponent, 0) precision if z > 0.
|
|
249
|
+
# Estimate (y*log(x)).exponent
|
|
250
|
+
logx_exponent = x < 2 ? (x - 1).exponent : Math.log10(x.exponent).round
|
|
251
|
+
ylogx_exponent = y.exponent + logx_exponent
|
|
252
|
+
prec2 += [ylogx_exponent, 0].max
|
|
253
|
+
end
|
|
254
|
+
BigMath.exp(BigMath.log(x, prec2).mult(y, prec2), result_prec)
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# Returns the square root of the value.
|
|
259
|
+
#
|
|
260
|
+
# Result has at least prec significant digits.
|
|
261
|
+
#
|
|
262
|
+
def sqrt(prec)
|
|
263
|
+
prec = Internal.coerce_validate_prec(prec, :sqrt, accept_zero: true)
|
|
264
|
+
return Internal.infinity_computation_result if infinite? == 1
|
|
265
|
+
|
|
266
|
+
raise FloatDomainError, 'sqrt of negative value' if self < 0
|
|
267
|
+
raise FloatDomainError, "sqrt of 'NaN'(Not a Number)" if nan?
|
|
268
|
+
return self if zero?
|
|
269
|
+
|
|
270
|
+
if prec == 0
|
|
271
|
+
limit = BigDecimal.limit
|
|
272
|
+
prec = n_significant_digits + BigDecimal.double_fig
|
|
273
|
+
prec = [limit, prec].min if limit.nonzero?
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
ex = exponent / 2
|
|
277
|
+
x = _decimal_shift(-2 * ex)
|
|
278
|
+
y = BigDecimal(Math.sqrt(x.to_f), 0)
|
|
279
|
+
Internal.newton_loop(prec + BigDecimal::Internal::EXTRA_PREC) do |p|
|
|
280
|
+
y = y.add(x.div(y, p), p).div(2, p)
|
|
281
|
+
end
|
|
282
|
+
y._decimal_shift(ex).mult(1, prec)
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# Core BigMath methods for BigDecimal (log, exp) are defined here.
|
|
287
|
+
# Other methods (sin, cos, atan) are defined in 'bigdecimal/math.rb'.
|
|
288
|
+
module BigMath
|
|
289
|
+
module_function
|
|
290
|
+
|
|
291
|
+
# call-seq:
|
|
292
|
+
# BigMath.log(decimal, numeric) -> BigDecimal
|
|
293
|
+
#
|
|
294
|
+
# Computes the natural logarithm of +decimal+ to the specified number of
|
|
295
|
+
# digits of precision, +numeric+.
|
|
296
|
+
#
|
|
297
|
+
# If +decimal+ is zero or negative, raises Math::DomainError.
|
|
298
|
+
#
|
|
299
|
+
# If +decimal+ is positive infinity, returns Infinity.
|
|
300
|
+
#
|
|
301
|
+
# If +decimal+ is NaN, returns NaN.
|
|
302
|
+
#
|
|
303
|
+
def log(x, prec)
|
|
304
|
+
prec = BigDecimal::Internal.coerce_validate_prec(prec, :log)
|
|
305
|
+
raise Math::DomainError, 'Complex argument for BigMath.log' if Complex === x
|
|
306
|
+
|
|
307
|
+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :log)
|
|
308
|
+
return BigDecimal::Internal.nan_computation_result if x.nan?
|
|
309
|
+
raise Math::DomainError, 'Negative argument for log' if x < 0
|
|
310
|
+
return -BigDecimal::Internal.infinity_computation_result if x.zero?
|
|
311
|
+
return BigDecimal::Internal.infinity_computation_result if x.infinite?
|
|
312
|
+
return BigDecimal(0) if x == 1
|
|
313
|
+
|
|
314
|
+
prec2 = prec + BigDecimal::Internal::EXTRA_PREC
|
|
315
|
+
|
|
316
|
+
# Reduce x to near 1
|
|
317
|
+
if x > 1.01 || x < 0.99
|
|
318
|
+
# log(x) = log(x/exp(logx_approx)) + logx_approx
|
|
319
|
+
logx_approx = BigDecimal(BigDecimal::Internal.float_log(x), 0)
|
|
320
|
+
x = x.div(exp(logx_approx, prec2), prec2)
|
|
321
|
+
else
|
|
322
|
+
logx_approx = BigDecimal(0)
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
# Solve exp(y) - x = 0 with Newton's method
|
|
326
|
+
# Repeat: y -= (exp(y) - x) / exp(y)
|
|
327
|
+
y = BigDecimal(BigDecimal::Internal.float_log(x), 0)
|
|
328
|
+
exp_additional_prec = [-(x - 1).exponent, 0].max
|
|
329
|
+
BigDecimal::Internal.newton_loop(prec2) do |p|
|
|
330
|
+
expy = exp(y, p + exp_additional_prec)
|
|
331
|
+
y = y.sub(expy.sub(x, p).div(expy, p), p)
|
|
332
|
+
end
|
|
333
|
+
y.add(logx_approx, prec)
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
private_class_method def _exp_binary_splitting(x, prec) # :nodoc:
|
|
337
|
+
return BigDecimal(1) if x.zero?
|
|
338
|
+
# Find k that satisfies x**k / k! < 10**(-prec)
|
|
339
|
+
log10 = Math.log(10)
|
|
340
|
+
logx = BigDecimal::Internal.float_log(x.abs)
|
|
341
|
+
step = (1..).bsearch { |k| Math.lgamma(k + 1)[0] - k * logx > prec * log10 }
|
|
342
|
+
# exp(x)-1 = x*(1+x/2*(1+x/3*(1+x/4*(1+x/5*(1+...)))))
|
|
343
|
+
1 + BigDecimal::Internal.taylor_sum_binary_splitting(x, [*1..step], prec)
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
# call-seq:
|
|
347
|
+
# BigMath.exp(decimal, numeric) -> BigDecimal
|
|
348
|
+
#
|
|
349
|
+
# Computes the value of e (the base of natural logarithms) raised to the
|
|
350
|
+
# power of +decimal+, to the specified number of digits of precision.
|
|
351
|
+
#
|
|
352
|
+
# If +decimal+ is infinity, returns Infinity.
|
|
353
|
+
#
|
|
354
|
+
# If +decimal+ is NaN, returns NaN.
|
|
355
|
+
#
|
|
356
|
+
def exp(x, prec)
|
|
357
|
+
prec = BigDecimal::Internal.coerce_validate_prec(prec, :exp)
|
|
358
|
+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :exp)
|
|
359
|
+
return BigDecimal::Internal.nan_computation_result if x.nan?
|
|
360
|
+
if x.infinite? || x.exponent >= 21 # exp(10**20) and exp(-10**20) overflows/underflows 64-bit exponent
|
|
361
|
+
if x.positive?
|
|
362
|
+
return BigDecimal::Internal.infinity_computation_result
|
|
363
|
+
elsif x.infinite?
|
|
364
|
+
# exp(-Infinity) is +0 by definition, this is not an underflow.
|
|
365
|
+
return BigDecimal(0)
|
|
366
|
+
else
|
|
367
|
+
return BigDecimal::Internal.underflow_computation_result
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
return BigDecimal(1) if x.zero?
|
|
372
|
+
|
|
373
|
+
# exp(x * 10**cnt) = exp(x)**(10**cnt)
|
|
374
|
+
cnt = x < -1 || x > 1 ? x.exponent : 0
|
|
375
|
+
prec2 = prec + BigDecimal::Internal::EXTRA_PREC + cnt
|
|
376
|
+
x = x._decimal_shift(-cnt)
|
|
377
|
+
|
|
378
|
+
# Decimal form of bit-burst algorithm
|
|
379
|
+
# Calculate exp(x.xxxxxxxxxxxxxxxx) as
|
|
380
|
+
# exp(x.xx) * exp(0.00xx) * exp(0.0000xxxx) * exp(0.00000000xxxxxxxx)
|
|
381
|
+
x = x.mult(1, prec2)
|
|
382
|
+
n = 2
|
|
383
|
+
y = BigDecimal(1)
|
|
384
|
+
BigDecimal.save_limit do
|
|
385
|
+
BigDecimal.limit(0)
|
|
386
|
+
while x != 0 do
|
|
387
|
+
partial_x = x.truncate(n)
|
|
388
|
+
x -= partial_x
|
|
389
|
+
y = y.mult(_exp_binary_splitting(partial_x, prec2), prec2)
|
|
390
|
+
n *= 2
|
|
391
|
+
end
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
# calculate exp(x * 10**cnt) from exp(x)
|
|
395
|
+
# exp(x * 10**k) = exp(x * 10**(k - 1)) ** 10
|
|
396
|
+
cnt.times do
|
|
397
|
+
y2 = y.mult(y, prec2)
|
|
398
|
+
y5 = y2.mult(y2, prec2).mult(y, prec2)
|
|
399
|
+
y = y5.mult(y5, prec2)
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
y.mult(1, prec)
|
|
403
|
+
end
|
|
404
|
+
end
|
data/sample/linear.rb
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
#!/usr/local/bin/ruby
|
|
2
|
-
# frozen_string_literal: false
|
|
3
|
-
|
|
4
1
|
#
|
|
5
2
|
# linear.rb
|
|
6
3
|
#
|
|
@@ -13,62 +10,101 @@
|
|
|
13
10
|
|
|
14
11
|
# :stopdoc:
|
|
15
12
|
require "bigdecimal"
|
|
16
|
-
require "bigdecimal/ludcmp"
|
|
17
13
|
|
|
18
|
-
#
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
#
|
|
14
|
+
# Requires gem matrix
|
|
15
|
+
require "matrix"
|
|
16
|
+
|
|
17
|
+
class PrecisionSpecifiedValue
|
|
18
|
+
# NOTE:
|
|
19
|
+
# Change following PREC if needed.
|
|
20
|
+
|
|
21
|
+
attr_reader :value
|
|
22
|
+
def initialize(value, prec)
|
|
23
|
+
@value = BigDecimal(value)
|
|
24
|
+
@prec = prec
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def unwrap(value)
|
|
28
|
+
PrecisionSpecifiedValue === value ? value.value : value
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def coerce(other)
|
|
32
|
+
[self.class.new(unwrap(other), @prec), self]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def abs
|
|
36
|
+
self.class.new(@value.abs, @prec)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def >(other)
|
|
40
|
+
@value > unwrap(other)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def <(other)
|
|
44
|
+
@value < unwrap(other)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def -(other)
|
|
48
|
+
self.class.new(@value.sub(unwrap(other), @prec), @prec)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def +(other)
|
|
52
|
+
self.class.new(@value.add(unwrap(other), @prec), @prec)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def *(other)
|
|
56
|
+
self.class.new(@value.mult(unwrap(other), @prec), @prec)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def quo(other)
|
|
60
|
+
self.class.new(@value.div(unwrap(other), @prec), @prec)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
return if __FILE__ != $0
|
|
23
65
|
|
|
24
|
-
include LUSolve
|
|
25
66
|
def rd_order(na)
|
|
26
|
-
|
|
27
|
-
|
|
67
|
+
printf("Number of equations ?") if(na <= 0)
|
|
68
|
+
ARGF.gets().to_i
|
|
28
69
|
end
|
|
29
70
|
|
|
30
|
-
na
|
|
31
|
-
zero = BigDecimal("0.0")
|
|
32
|
-
one = BigDecimal("1.0")
|
|
71
|
+
na = ARGV.size
|
|
33
72
|
|
|
34
73
|
while (n=rd_order(na))>0
|
|
35
74
|
a = []
|
|
36
|
-
as= []
|
|
37
75
|
b = []
|
|
38
76
|
if na <= 0
|
|
39
77
|
# Read data from console.
|
|
40
78
|
printf("\nEnter coefficient matrix element A[i,j]\n")
|
|
41
79
|
for i in 0...n do
|
|
42
|
-
|
|
80
|
+
a << n.times.map do |j|
|
|
43
81
|
printf("A[%d,%d]? ",i,j); s = ARGF.gets
|
|
44
|
-
|
|
45
|
-
as << BigDecimal(s)
|
|
82
|
+
BigDecimal(s)
|
|
46
83
|
end
|
|
47
84
|
printf("Contatant vector element b[%d] ? ",i)
|
|
48
85
|
b << BigDecimal(ARGF.gets)
|
|
49
86
|
end
|
|
50
87
|
else
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
end
|
|
61
|
-
b << BigDecimal(s[n])
|
|
62
|
-
end
|
|
88
|
+
# Read data from specified file.
|
|
89
|
+
printf("Coefficient matrix and constant vector.\n")
|
|
90
|
+
for i in 0...n do
|
|
91
|
+
s = ARGF.gets
|
|
92
|
+
printf("%d) %s",i,s)
|
|
93
|
+
s = s.split
|
|
94
|
+
a << n.times.map {|j| BigDecimal(s[j]) }
|
|
95
|
+
b << BigDecimal(s[n])
|
|
96
|
+
end
|
|
63
97
|
end
|
|
64
|
-
|
|
98
|
+
|
|
99
|
+
prec = 100
|
|
100
|
+
matrix = Matrix[*a.map {|row| row.map {|v| PrecisionSpecifiedValue.new(v, prec) } }]
|
|
101
|
+
vector = b.map {|v| PrecisionSpecifiedValue.new(v, prec) }
|
|
102
|
+
x = matrix.lup.solve(vector).map(&:value)
|
|
103
|
+
|
|
65
104
|
printf("Answer(x[i] & (A*x-b)[i]) follows\n")
|
|
66
105
|
for i in 0...n do
|
|
67
106
|
printf("x[%d]=%s ",i,x[i].to_s)
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
s = s + as[i*n+j]*x[j]
|
|
71
|
-
end
|
|
72
|
-
printf(" & %s\n",(s-b[i]).to_s)
|
|
107
|
+
diff = a[i].zip(x).sum {|aij, xj| aij*xj }.sub(b[i], 10)
|
|
108
|
+
printf(" & %s\n", diff.to_s)
|
|
73
109
|
end
|
|
74
110
|
end
|