ed-precompiled_bigdecimal 3.3.1-arm64-darwin

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.
@@ -0,0 +1,186 @@
1
+ # frozen_string_literal: false
2
+ #
3
+ #--
4
+ # bigdecimal/util extends various native classes to provide the #to_d method,
5
+ # and provides BigDecimal#to_d and BigDecimal#to_digits.
6
+ #++
7
+
8
+ require 'bigdecimal'
9
+
10
+ class Integer < Numeric
11
+ # call-seq:
12
+ # int.to_d -> bigdecimal
13
+ #
14
+ # Returns the value of +int+ as a BigDecimal.
15
+ #
16
+ # require 'bigdecimal'
17
+ # require 'bigdecimal/util'
18
+ #
19
+ # 42.to_d # => 0.42e2
20
+ #
21
+ # See also Kernel.BigDecimal.
22
+ #
23
+ def to_d
24
+ BigDecimal(self)
25
+ end
26
+ end
27
+
28
+
29
+ class Float < Numeric
30
+ # call-seq:
31
+ # float.to_d -> bigdecimal
32
+ # float.to_d(precision) -> bigdecimal
33
+ #
34
+ # Returns the value of +float+ as a BigDecimal.
35
+ # The +precision+ parameter is used to determine the number of
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+.
40
+ #
41
+ # require 'bigdecimal'
42
+ # require 'bigdecimal/util'
43
+ #
44
+ # 0.5.to_d # => 0.5e0
45
+ # 1.234.to_d # => 0.1234e1
46
+ # 1.234.to_d(2) # => 0.12e1
47
+ #
48
+ # See also Kernel.BigDecimal.
49
+ #
50
+ def to_d(precision=0)
51
+ BigDecimal(self, precision)
52
+ end
53
+ end
54
+
55
+
56
+ class String
57
+ # call-seq:
58
+ # str.to_d -> bigdecimal
59
+ #
60
+ # Returns the result of interpreting leading characters in +str+
61
+ # as a BigDecimal.
62
+ #
63
+ # require 'bigdecimal'
64
+ # require 'bigdecimal/util'
65
+ #
66
+ # "0.5".to_d # => 0.5e0
67
+ # "123.45e1".to_d # => 0.12345e4
68
+ # "45.67 degrees".to_d # => 0.4567e2
69
+ #
70
+ # See also Kernel.BigDecimal.
71
+ #
72
+ def to_d
73
+ BigDecimal.interpret_loosely(self)
74
+ end
75
+ end
76
+
77
+
78
+ class BigDecimal < Numeric
79
+ # call-seq:
80
+ # a.to_digits -> string
81
+ #
82
+ # Converts a BigDecimal to a String of the form "nnnnnn.mmm".
83
+ # This method is deprecated; use BigDecimal#to_s("F") instead.
84
+ #
85
+ # require 'bigdecimal/util'
86
+ #
87
+ # d = BigDecimal("3.14")
88
+ # d.to_digits # => "3.14"
89
+ #
90
+ def to_digits
91
+ if self.nan? || self.infinite? || self.zero?
92
+ self.to_s
93
+ else
94
+ i = self.to_i.to_s
95
+ _,f,_,z = self.frac.split
96
+ i + "." + ("0"*(-z)) + f
97
+ end
98
+ end
99
+
100
+ # call-seq:
101
+ # a.to_d -> bigdecimal
102
+ #
103
+ # Returns self.
104
+ #
105
+ # require 'bigdecimal/util'
106
+ #
107
+ # d = BigDecimal("3.14")
108
+ # d.to_d # => 0.314e1
109
+ #
110
+ def to_d
111
+ self
112
+ end
113
+ end
114
+
115
+
116
+ class Rational < Numeric
117
+ # call-seq:
118
+ # rat.to_d(precision) -> bigdecimal
119
+ #
120
+ # Returns the value as a BigDecimal.
121
+ #
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+.
127
+ #
128
+ # require 'bigdecimal'
129
+ # require 'bigdecimal/util'
130
+ #
131
+ # Rational(22, 7).to_d(3) # => 0.314e1
132
+ #
133
+ # See also Kernel.BigDecimal.
134
+ #
135
+ def to_d(precision=0)
136
+ BigDecimal(self, precision)
137
+ end
138
+ end
139
+
140
+
141
+ class Complex < Numeric
142
+ # call-seq:
143
+ # cmp.to_d -> bigdecimal
144
+ # cmp.to_d(precision) -> bigdecimal
145
+ #
146
+ # Returns the value as a BigDecimal.
147
+ # If the imaginary part is not +0+, an error is raised
148
+ #
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+.
154
+ #
155
+ # require 'bigdecimal'
156
+ # require 'bigdecimal/util'
157
+ #
158
+ # Complex(0.1234567, 0).to_d(4) # => 0.1235e0
159
+ # Complex(Rational(22, 7), 0).to_d(3) # => 0.314e1
160
+ # Complex(1, 1).to_d # raises ArgumentError
161
+ #
162
+ # See also Kernel.BigDecimal.
163
+ #
164
+ def to_d(precision=0)
165
+ BigDecimal(self) unless self.imag.zero? # to raise error
166
+
167
+ BigDecimal(self.real, precision)
168
+ end
169
+ end
170
+
171
+
172
+ class NilClass
173
+ # call-seq:
174
+ # nil.to_d -> bigdecimal
175
+ #
176
+ # Returns nil represented as a BigDecimal.
177
+ #
178
+ # require 'bigdecimal'
179
+ # require 'bigdecimal/util'
180
+ #
181
+ # nil.to_d # => 0.0
182
+ #
183
+ def to_d
184
+ BigDecimal(0)
185
+ end
186
+ end
data/lib/bigdecimal.rb ADDED
@@ -0,0 +1,361 @@
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
+ begin
11
+ ruby_version = /(\d+\.\d+)/.match(::RUBY_VERSION)
12
+ require "#{ruby_version}/bigdecimal.so"
13
+ rescue LoadError
14
+ require 'bigdecimal.so'
15
+ end
16
+ end
17
+
18
+ class BigDecimal
19
+ module Internal # :nodoc:
20
+
21
+ # Coerce x to BigDecimal with the specified precision.
22
+ # TODO: some methods (example: BigMath.exp) require more precision than specified to coerce.
23
+ def self.coerce_to_bigdecimal(x, prec, method_name) # :nodoc:
24
+ case x
25
+ when BigDecimal
26
+ return x
27
+ when Integer, Float
28
+ return BigDecimal(x, 0)
29
+ when Rational
30
+ return BigDecimal(x, [prec, 2 * BigDecimal.double_fig].max)
31
+ end
32
+ raise ArgumentError, "#{x.inspect} can't be coerced into BigDecimal"
33
+ end
34
+
35
+ def self.coerce_validate_prec(prec, method_name, accept_zero: false) # :nodoc:
36
+ unless Integer === prec
37
+ original = prec
38
+ # Emulate Integer.try_convert for ruby < 3.1
39
+ if prec.respond_to?(:to_int)
40
+ prec = prec.to_int
41
+ else
42
+ raise TypeError, "no implicit conversion of #{original.class} into Integer"
43
+ end
44
+ raise TypeError, "can't convert #{original.class} to Integer" unless Integer === prec
45
+ end
46
+
47
+ if accept_zero
48
+ raise ArgumentError, "Negative precision for #{method_name}" if prec < 0
49
+ else
50
+ raise ArgumentError, "Zero or negative precision for #{method_name}" if prec <= 0
51
+ end
52
+ prec
53
+ end
54
+
55
+ def self.infinity_computation_result # :nodoc:
56
+ if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_INFINITY)
57
+ raise FloatDomainError, "Computation results in 'Infinity'"
58
+ end
59
+ BigDecimal::INFINITY
60
+ end
61
+
62
+ def self.nan_computation_result # :nodoc:
63
+ if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_NaN)
64
+ raise FloatDomainError, "Computation results to 'NaN'"
65
+ end
66
+ BigDecimal::NAN
67
+ end
68
+ end
69
+
70
+ # call-seq:
71
+ # self ** other -> bigdecimal
72
+ #
73
+ # Returns the \BigDecimal value of +self+ raised to power +other+:
74
+ #
75
+ # b = BigDecimal('3.14')
76
+ # b ** 2 # => 0.98596e1
77
+ # b ** 2.0 # => 0.98596e1
78
+ # b ** Rational(2, 1) # => 0.98596e1
79
+ #
80
+ # Related: BigDecimal#power.
81
+ #
82
+ def **(y)
83
+ case y
84
+ when BigDecimal, Integer, Float, Rational
85
+ power(y)
86
+ when nil
87
+ raise TypeError, 'wrong argument type NilClass'
88
+ else
89
+ x, y = y.coerce(self)
90
+ x**y
91
+ end
92
+ end
93
+
94
+ # call-seq:
95
+ # power(n)
96
+ # power(n, prec)
97
+ #
98
+ # Returns the value raised to the power of n.
99
+ #
100
+ # Also available as the operator **.
101
+ #
102
+ def power(y, prec = 0)
103
+ prec = Internal.coerce_validate_prec(prec, :power, accept_zero: true)
104
+ x = self
105
+ y = Internal.coerce_to_bigdecimal(y, prec.nonzero? || n_significant_digits, :power)
106
+
107
+ return Internal.nan_computation_result if x.nan? || y.nan?
108
+ return BigDecimal(1) if y.zero?
109
+
110
+ if y.infinite?
111
+ if x < 0
112
+ return BigDecimal(0) if x < -1 && y.negative?
113
+ return BigDecimal(0) if x > -1 && y.positive?
114
+ raise Math::DomainError, 'Result undefined for negative base raised to infinite power'
115
+ elsif x < 1
116
+ return y.positive? ? BigDecimal(0) : BigDecimal::Internal.infinity_computation_result
117
+ elsif x == 1
118
+ return BigDecimal(1)
119
+ else
120
+ return y.positive? ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0)
121
+ end
122
+ end
123
+
124
+ if x.infinite? && y < 0
125
+ # Computation result will be +0 or -0. Avoid overflow.
126
+ neg = x < 0 && y.frac.zero? && y % 2 == 1
127
+ return neg ? -BigDecimal(0) : BigDecimal(0)
128
+ end
129
+
130
+ if x.zero?
131
+ return BigDecimal(1) if y.zero?
132
+ return BigDecimal(0) if y > 0
133
+ if y.frac.zero? && y % 2 == 1 && x.sign == -1
134
+ return -BigDecimal::Internal.infinity_computation_result
135
+ else
136
+ return BigDecimal::Internal.infinity_computation_result
137
+ end
138
+ elsif x < 0
139
+ if y.frac.zero?
140
+ if y % 2 == 0
141
+ return (-x).power(y, prec)
142
+ else
143
+ return -(-x).power(y, prec)
144
+ end
145
+ else
146
+ raise Math::DomainError, 'Computation results in complex number'
147
+ end
148
+ elsif x == 1
149
+ return BigDecimal(1)
150
+ end
151
+
152
+ prec = BigDecimal.limit if prec.zero?
153
+ frac_part = y.frac
154
+
155
+ if frac_part.zero? && prec.zero?
156
+ # Infinite precision calculation for `x ** int` and `x.power(int)`
157
+ int_part = y.fix.to_i
158
+ int_part = -int_part if (neg = int_part < 0)
159
+ ans = BigDecimal(1)
160
+ n = 1
161
+ xn = x
162
+ while true
163
+ ans *= xn if int_part.allbits?(n)
164
+ n <<= 1
165
+ break if n > int_part
166
+ xn *= xn
167
+ # Detect overflow/underflow before consuming infinite memory
168
+ if (xn.exponent.abs - 1) * int_part / n >= 0x7FFFFFFFFFFFFFFF
169
+ return ((xn.exponent > 0) ^ neg ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0)) * (int_part.even? || x > 0 ? 1 : -1)
170
+ end
171
+ end
172
+ return neg ? BigDecimal(1) / ans : ans
173
+ end
174
+
175
+ prec = [x.n_significant_digits, y.n_significant_digits, BigDecimal.double_fig].max + BigDecimal.double_fig if prec.zero?
176
+
177
+ if y < 0
178
+ inv = x.power(-y, prec)
179
+ return BigDecimal(0) if inv.infinite?
180
+ return BigDecimal::Internal.infinity_computation_result if inv.zero?
181
+ return BigDecimal(1).div(inv, prec)
182
+ end
183
+
184
+ prec2 = prec + BigDecimal.double_fig
185
+
186
+ if frac_part.zero? && y.exponent < Math.log(prec) * 5 + 20
187
+ # Use exponentiation by squaring if y is an integer and not too large
188
+ pow_prec = prec2 + y.exponent
189
+ n = 1
190
+ xn = x
191
+ ans = BigDecimal(1)
192
+ int_part = y.fix.to_i
193
+ while true
194
+ ans = ans.mult(xn, pow_prec) if int_part.allbits?(n)
195
+ n <<= 1
196
+ break if n > int_part
197
+ xn = xn.mult(xn, pow_prec)
198
+ end
199
+ ans.mult(1, prec)
200
+ else
201
+ if x > 1
202
+ # To calculate exp(z, prec), z needs prec+max(z.exponent, 0) precision if z > 0.
203
+ # Estimate (y*log(x)).exponent
204
+ logx_exponent = x < 2 ? (x - 1).exponent : Math.log10(x.exponent).round
205
+ ylogx_exponent = y.exponent + logx_exponent
206
+ prec2 += [ylogx_exponent, 0].max
207
+ end
208
+ BigMath.exp(BigMath.log(x, prec2).mult(y, prec2), prec)
209
+ end
210
+ end
211
+
212
+ # Returns the square root of the value.
213
+ #
214
+ # Result has at least prec significant digits.
215
+ #
216
+ def sqrt(prec)
217
+ prec = Internal.coerce_validate_prec(prec, :sqrt, accept_zero: true)
218
+ return Internal.infinity_computation_result if infinite? == 1
219
+
220
+ raise FloatDomainError, 'sqrt of negative value' if self < 0
221
+ raise FloatDomainError, "sqrt of 'NaN'(Not a Number)" if nan?
222
+ return self if zero?
223
+
224
+ if prec == 0
225
+ prec = BigDecimal.limit.nonzero? || n_significant_digits + BigDecimal.double_fig
226
+ end
227
+
228
+ ex = exponent / 2
229
+ x = _decimal_shift(-2 * ex)
230
+ y = BigDecimal(Math.sqrt(x.to_f), 0)
231
+ precs = [prec + BigDecimal.double_fig]
232
+ precs << 2 + precs.last / 2 while precs.last > BigDecimal.double_fig
233
+ precs.reverse_each do |p|
234
+ y = y.add(x.div(y, p), p).div(2, p)
235
+ end
236
+ y._decimal_shift(ex).mult(1, prec)
237
+ end
238
+ end
239
+
240
+ # Core BigMath methods for BigDecimal (log, exp) are defined here.
241
+ # Other methods (sin, cos, atan) are defined in 'bigdecimal/math.rb'.
242
+ module BigMath
243
+
244
+ # call-seq:
245
+ # BigMath.log(decimal, numeric) -> BigDecimal
246
+ #
247
+ # Computes the natural logarithm of +decimal+ to the specified number of
248
+ # digits of precision, +numeric+.
249
+ #
250
+ # If +decimal+ is zero or negative, raises Math::DomainError.
251
+ #
252
+ # If +decimal+ is positive infinity, returns Infinity.
253
+ #
254
+ # If +decimal+ is NaN, returns NaN.
255
+ #
256
+ def self.log(x, prec)
257
+ prec = BigDecimal::Internal.coerce_validate_prec(prec, :log)
258
+ raise Math::DomainError, 'Complex argument for BigMath.log' if Complex === x
259
+
260
+ x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :log)
261
+ return BigDecimal::Internal.nan_computation_result if x.nan?
262
+ raise Math::DomainError, 'Negative argument for log' if x < 0
263
+ return -BigDecimal::Internal.infinity_computation_result if x.zero?
264
+ return BigDecimal::Internal.infinity_computation_result if x.infinite?
265
+ return BigDecimal(0) if x == 1
266
+
267
+ prec2 = prec + BigDecimal.double_fig
268
+ BigDecimal.save_limit do
269
+ BigDecimal.limit(0)
270
+ if x > 10 || x < 0.1
271
+ log10 = log(BigDecimal(10), prec2)
272
+ exponent = x.exponent
273
+ x = x._decimal_shift(-exponent)
274
+ if x < 0.3
275
+ x *= 10
276
+ exponent -= 1
277
+ end
278
+ return (log10 * exponent).add(log(x, prec2), prec)
279
+ end
280
+
281
+ x_minus_one_exponent = (x - 1).exponent
282
+
283
+ # log(x) = log(sqrt(sqrt(sqrt(sqrt(x))))) * 2**sqrt_steps
284
+ sqrt_steps = [Integer.sqrt(prec2) + 3 * x_minus_one_exponent, 0].max
285
+
286
+ lg2 = 0.3010299956639812
287
+ sqrt_prec = prec2 + [-x_minus_one_exponent, 0].max + (sqrt_steps * lg2).ceil
288
+
289
+ sqrt_steps.times do
290
+ x = x.sqrt(sqrt_prec)
291
+ end
292
+
293
+ # Taylor series for log(x) around 1
294
+ # log(x) = -log((1 + X) / (1 - X)) where X = (x - 1) / (x + 1)
295
+ # log(x) = 2 * (X + X**3 / 3 + X**5 / 5 + X**7 / 7 + ...)
296
+ x = (x - 1).div(x + 1, sqrt_prec)
297
+ y = x
298
+ x2 = x.mult(x, prec2)
299
+ 1.step do |i|
300
+ n = prec2 + x.exponent - y.exponent + x2.exponent
301
+ break if n <= 0 || x.zero?
302
+ x = x.mult(x2.round(n - x2.exponent), n)
303
+ y = y.add(x.div(2 * i + 1, n), prec2)
304
+ end
305
+
306
+ y.mult(2 ** (sqrt_steps + 1), prec)
307
+ end
308
+ end
309
+
310
+ # Taylor series for exp(x) around 0
311
+ private_class_method def self._exp_taylor(x, prec) # :nodoc:
312
+ xn = BigDecimal(1)
313
+ y = BigDecimal(1)
314
+ 1.step do |i|
315
+ n = prec + xn.exponent
316
+ break if n <= 0 || xn.zero?
317
+ xn = xn.mult(x, n).div(i, n)
318
+ y = y.add(xn, prec)
319
+ end
320
+ y
321
+ end
322
+
323
+ # call-seq:
324
+ # BigMath.exp(decimal, numeric) -> BigDecimal
325
+ #
326
+ # Computes the value of e (the base of natural logarithms) raised to the
327
+ # power of +decimal+, to the specified number of digits of precision.
328
+ #
329
+ # If +decimal+ is infinity, returns Infinity.
330
+ #
331
+ # If +decimal+ is NaN, returns NaN.
332
+ #
333
+ def self.exp(x, prec)
334
+ prec = BigDecimal::Internal.coerce_validate_prec(prec, :exp)
335
+ x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :exp)
336
+ return BigDecimal::Internal.nan_computation_result if x.nan?
337
+ return x.positive? ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0) if x.infinite?
338
+ return BigDecimal(1) if x.zero?
339
+
340
+ # exp(x * 10**cnt) = exp(x)**(10**cnt)
341
+ cnt = x < -1 || x > 1 ? x.exponent : 0
342
+ prec2 = prec + BigDecimal.double_fig + cnt
343
+ x = x._decimal_shift(-cnt)
344
+
345
+ # Calculation of exp(small_prec) is fast because calculation of x**n is fast
346
+ # Calculation of exp(small_abs) converges fast.
347
+ # exp(x) = exp(small_prec_part + small_abs_part) = exp(small_prec_part) * exp(small_abs_part)
348
+ x_small_prec = x.round(Integer.sqrt(prec2))
349
+ y = _exp_taylor(x_small_prec, prec2).mult(_exp_taylor(x.sub(x_small_prec, prec2), prec2), prec2)
350
+
351
+ # calculate exp(x * 10**cnt) from exp(x)
352
+ # exp(x * 10**k) = exp(x * 10**(k - 1)) ** 10
353
+ cnt.times do
354
+ y2 = y.mult(y, prec2)
355
+ y5 = y2.mult(y2, prec2).mult(y, prec2)
356
+ y = y5.mult(y5, prec2)
357
+ end
358
+
359
+ y.mult(1, prec)
360
+ end
361
+ end
data/sample/linear.rb ADDED
@@ -0,0 +1,74 @@
1
+ #!/usr/local/bin/ruby
2
+ # frozen_string_literal: false
3
+
4
+ #
5
+ # linear.rb
6
+ #
7
+ # Solves linear equation system(A*x = b) by LU decomposition method.
8
+ # where A is a coefficient matrix,x is an answer vector,b is a constant vector.
9
+ #
10
+ # USAGE:
11
+ # ruby linear.rb [input file solved]
12
+ #
13
+
14
+ # :stopdoc:
15
+ require "bigdecimal"
16
+ require "bigdecimal/ludcmp"
17
+
18
+ #
19
+ # NOTE:
20
+ # Change following BigDecimal.limit() if needed.
21
+ BigDecimal.limit(100)
22
+ #
23
+
24
+ include LUSolve
25
+ def rd_order(na)
26
+ printf("Number of equations ?") if(na <= 0)
27
+ n = ARGF.gets().to_i
28
+ end
29
+
30
+ na = ARGV.size
31
+ zero = BigDecimal("0.0")
32
+ one = BigDecimal("1.0")
33
+
34
+ while (n=rd_order(na))>0
35
+ a = []
36
+ as= []
37
+ b = []
38
+ if na <= 0
39
+ # Read data from console.
40
+ printf("\nEnter coefficient matrix element A[i,j]\n")
41
+ for i in 0...n do
42
+ for j in 0...n do
43
+ printf("A[%d,%d]? ",i,j); s = ARGF.gets
44
+ a << BigDecimal(s)
45
+ as << BigDecimal(s)
46
+ end
47
+ printf("Contatant vector element b[%d] ? ",i)
48
+ b << BigDecimal(ARGF.gets)
49
+ end
50
+ else
51
+ # Read data from specified file.
52
+ printf("Coefficient matrix and constant vector.\n")
53
+ for i in 0...n do
54
+ s = ARGF.gets
55
+ printf("%d) %s",i,s)
56
+ s = s.split
57
+ for j in 0...n do
58
+ a << BigDecimal(s[j])
59
+ as << BigDecimal(s[j])
60
+ end
61
+ b << BigDecimal(s[n])
62
+ end
63
+ end
64
+ x = lusolve(a,b,ludecomp(a,n,zero,one),zero)
65
+ printf("Answer(x[i] & (A*x-b)[i]) follows\n")
66
+ for i in 0...n do
67
+ printf("x[%d]=%s ",i,x[i].to_s)
68
+ s = zero
69
+ for j in 0...n do
70
+ s = s + as[i*n+j]*x[j]
71
+ end
72
+ printf(" & %s\n",(s-b[i]).to_s)
73
+ end
74
+ end
data/sample/nlsolve.rb ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/local/bin/ruby
2
+ # frozen_string_literal: false
3
+
4
+ #
5
+ # nlsolve.rb
6
+ # An example for solving nonlinear algebraic equation system.
7
+ #
8
+
9
+ require "bigdecimal"
10
+ require "bigdecimal/newton"
11
+ include Newton
12
+
13
+ class Function # :nodoc: all
14
+ def initialize()
15
+ @zero = BigDecimal("0.0")
16
+ @one = BigDecimal("1.0")
17
+ @two = BigDecimal("2.0")
18
+ @ten = BigDecimal("10.0")
19
+ @eps = BigDecimal("1.0e-16")
20
+ end
21
+ def zero;@zero;end
22
+ def one ;@one ;end
23
+ def two ;@two ;end
24
+ def ten ;@ten ;end
25
+ def eps ;@eps ;end
26
+ def values(x) # <= defines functions solved
27
+ f = []
28
+ f1 = x[0]*x[0] + x[1]*x[1] - @two # f1 = x**2 + y**2 - 2 => 0
29
+ f2 = x[0] - x[1] # f2 = x - y => 0
30
+ f <<= f1
31
+ f <<= f2
32
+ f
33
+ end
34
+ end
35
+
36
+ f = BigDecimal.limit(100)
37
+ f = Function.new
38
+ x = [f.zero,f.zero] # Initial values
39
+ n = nlsolve(f,x)
40
+ p x
data/sample/pi.rb ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/local/bin/ruby
2
+ # frozen_string_literal: false
3
+
4
+ #
5
+ # pi.rb
6
+ #
7
+ # Calculates 3.1415.... (the number of times that a circle's diameter
8
+ # will fit around the circle) using J. Machin's formula.
9
+ #
10
+
11
+ require "bigdecimal"
12
+ require "bigdecimal/math.rb"
13
+
14
+ include BigMath
15
+
16
+ if ARGV.size == 1
17
+ print "PI("+ARGV[0]+"):\n"
18
+ p PI(ARGV[0].to_i)
19
+ else
20
+ print "TRY: ruby pi.rb 1000 \n"
21
+ end