bigdecimal-fix 1.4.4

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,80 @@
1
+ # frozen_string_literal: false
2
+ require "bigdecimal/ludcmp"
3
+ require "bigdecimal/jacobian"
4
+
5
+ #
6
+ # newton.rb
7
+ #
8
+ # Solves the nonlinear algebraic equation system f = 0 by Newton's method.
9
+ # This program is not dependent on BigDecimal.
10
+ #
11
+ # To call:
12
+ # n = nlsolve(f,x)
13
+ # where n is the number of iterations required,
14
+ # x is the initial value vector
15
+ # f is an Object which is used to compute the values of the equations to be solved.
16
+ # It must provide the following methods:
17
+ #
18
+ # f.values(x):: returns the values of all functions at x
19
+ #
20
+ # f.zero:: returns 0.0
21
+ # f.one:: returns 1.0
22
+ # f.two:: returns 2.0
23
+ # f.ten:: returns 10.0
24
+ #
25
+ # f.eps:: returns the convergence criterion (epsilon value) used to determine whether two values are considered equal. If |a-b| < epsilon, the two values are considered equal.
26
+ #
27
+ # On exit, x is the solution vector.
28
+ #
29
+ module Newton
30
+ include LUSolve
31
+ include Jacobian
32
+ module_function
33
+
34
+ def norm(fv,zero=0.0) # :nodoc:
35
+ s = zero
36
+ n = fv.size
37
+ for i in 0...n do
38
+ s += fv[i]*fv[i]
39
+ end
40
+ s
41
+ end
42
+
43
+ # See also Newton
44
+ def nlsolve(f,x)
45
+ nRetry = 0
46
+ n = x.size
47
+
48
+ f0 = f.values(x)
49
+ zero = f.zero
50
+ one = f.one
51
+ two = f.two
52
+ p5 = one/two
53
+ d = norm(f0,zero)
54
+ minfact = f.ten*f.ten*f.ten
55
+ minfact = one/minfact
56
+ e = f.eps
57
+ while d >= e do
58
+ nRetry += 1
59
+ # Not yet converged. => Compute Jacobian matrix
60
+ dfdx = jacobian(f,f0,x)
61
+ # Solve dfdx*dx = -f0 to estimate dx
62
+ dx = lusolve(dfdx,f0,ludecomp(dfdx,n,zero,one),zero)
63
+ fact = two
64
+ xs = x.dup
65
+ begin
66
+ fact *= p5
67
+ if fact < minfact then
68
+ raise "Failed to reduce function values."
69
+ end
70
+ for i in 0...n do
71
+ x[i] = xs[i] - dx[i]*fact
72
+ end
73
+ f0 = f.values(x)
74
+ dn = norm(f0,zero)
75
+ end while(dn>=d)
76
+ d = dn
77
+ end
78
+ nRetry
79
+ end
80
+ end
@@ -0,0 +1,146 @@
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
+ require 'bigdecimal/util.so'
10
+
11
+ class Integer < Numeric
12
+ # call-seq:
13
+ # int.to_d -> bigdecimal
14
+ #
15
+ # Returns the value of +int+ as a BigDecimal.
16
+ #
17
+ # require 'bigdecimal'
18
+ # require 'bigdecimal/util'
19
+ #
20
+ # 42.to_d # => 0.42e2
21
+ #
22
+ # See also BigDecimal::new.
23
+ #
24
+ def to_d
25
+ BigDecimal(self)
26
+ end
27
+ end
28
+
29
+
30
+ class Float < Numeric
31
+ # call-seq:
32
+ # float.to_d -> bigdecimal
33
+ # float.to_d(precision) -> bigdecimal
34
+ #
35
+ # Returns the value of +float+ as a BigDecimal.
36
+ # The +precision+ parameter is used to determine the number of
37
+ # significant digits for the result (the default is Float::DIG).
38
+ #
39
+ # require 'bigdecimal'
40
+ # require 'bigdecimal/util'
41
+ #
42
+ # 0.5.to_d # => 0.5e0
43
+ # 1.234.to_d(2) # => 0.12e1
44
+ #
45
+ # See also BigDecimal::new.
46
+ #
47
+ def to_d(precision=Float::DIG)
48
+ BigDecimal(self, precision)
49
+ end
50
+ end
51
+
52
+
53
+ class String
54
+ # call-seq:
55
+ # str.to_d -> bigdecimal
56
+ #
57
+ # Returns the result of interpreting leading characters in +str+
58
+ # as a BigDecimal.
59
+ #
60
+ # require 'bigdecimal'
61
+ # require 'bigdecimal/util'
62
+ #
63
+ # "0.5".to_d # => 0.5e0
64
+ # "123.45e1".to_d # => 0.12345e4
65
+ # "45.67 degrees".to_d # => 0.4567e2
66
+ #
67
+ # See also BigDecimal::new.
68
+ #
69
+ end
70
+
71
+
72
+ class BigDecimal < Numeric
73
+ # call-seq:
74
+ # a.to_digits -> string
75
+ #
76
+ # Converts a BigDecimal to a String of the form "nnnnnn.mmm".
77
+ # This method is deprecated; use BigDecimal#to_s("F") instead.
78
+ #
79
+ # require 'bigdecimal/util'
80
+ #
81
+ # d = BigDecimal("3.14")
82
+ # d.to_digits # => "3.14"
83
+ #
84
+ def to_digits
85
+ if self.nan? || self.infinite? || self.zero?
86
+ self.to_s
87
+ else
88
+ i = self.to_i.to_s
89
+ _,f,_,z = self.frac.split
90
+ i + "." + ("0"*(-z)) + f
91
+ end
92
+ end
93
+
94
+ # call-seq:
95
+ # a.to_d -> bigdecimal
96
+ #
97
+ # Returns self.
98
+ #
99
+ # require 'bigdecimal/util'
100
+ #
101
+ # d = BigDecimal("3.14")
102
+ # d.to_d # => 0.314e1
103
+ #
104
+ def to_d
105
+ self
106
+ end
107
+ end
108
+
109
+
110
+ class Rational < Numeric
111
+ # call-seq:
112
+ # rat.to_d(precision) -> bigdecimal
113
+ #
114
+ # Returns the value as a BigDecimal.
115
+ #
116
+ # The required +precision+ parameter is used to determine the number of
117
+ # significant digits for the result.
118
+ #
119
+ # require 'bigdecimal'
120
+ # require 'bigdecimal/util'
121
+ #
122
+ # Rational(22, 7).to_d(3) # => 0.314e1
123
+ #
124
+ # See also BigDecimal::new.
125
+ #
126
+ def to_d(precision)
127
+ BigDecimal(self, precision)
128
+ end
129
+ end
130
+
131
+
132
+ class NilClass
133
+ # call-seq:
134
+ # nil.to_d -> bigdecimal
135
+ #
136
+ # Returns nil represented as a BigDecimal.
137
+ #
138
+ # require 'bigdecimal'
139
+ # require 'bigdecimal/util'
140
+ #
141
+ # nil.to_d # => 0.0
142
+ #
143
+ def to_d
144
+ BigDecimal(0)
145
+ end
146
+ end
@@ -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
@@ -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
@@ -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
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bigdecimal-fix
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.4.4
5
+ platform: ruby
6
+ authors:
7
+ - Kenta Murata
8
+ - Zachary Scott
9
+ - Shigeo Kobayashi
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2019-04-30 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '10.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '10.0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake-compiler
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0.9'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0.9'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rake-compiler-dock
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 0.6.1
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 0.6.1
57
+ - !ruby/object:Gem::Dependency
58
+ name: minitest
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "<"
62
+ - !ruby/object:Gem::Version
63
+ version: 5.0.0
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "<"
69
+ - !ruby/object:Gem::Version
70
+ version: 5.0.0
71
+ - !ruby/object:Gem::Dependency
72
+ name: pry
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ description: This library provides arbitrary-precision decimal floating-point number
86
+ class.
87
+ email:
88
+ - mrkn@mrkn.jp
89
+ executables: []
90
+ extensions:
91
+ - ext/bigdecimal/extconf.rb
92
+ - ext/bigdecimal/util/extconf.rb
93
+ extra_rdoc_files: []
94
+ files:
95
+ - bigdecimal.gemspec
96
+ - ext/bigdecimal/bigdecimal.c
97
+ - ext/bigdecimal/bigdecimal.def
98
+ - ext/bigdecimal/bigdecimal.h
99
+ - ext/bigdecimal/depend
100
+ - ext/bigdecimal/extconf.rb
101
+ - ext/bigdecimal/util/extconf.rb
102
+ - ext/bigdecimal/util/util.c
103
+ - lib/bigdecimal.rb
104
+ - lib/bigdecimal/jacobian.rb
105
+ - lib/bigdecimal/ludcmp.rb
106
+ - lib/bigdecimal/math.rb
107
+ - lib/bigdecimal/newton.rb
108
+ - lib/bigdecimal/util.rb
109
+ - sample/linear.rb
110
+ - sample/nlsolve.rb
111
+ - sample/pi.rb
112
+ homepage: https://github.com/ruby/bigdecimal
113
+ licenses:
114
+ - ruby
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 2.3.0
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.5.2.1
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Arbitrary-precision decimal floating-point number library.
136
+ test_files: []