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