rubysl-bigdecimal 1.0.0 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,8 @@
1
+ require "bigdecimal/ludcmp"
2
+ require "bigdecimal/jacobian"
3
+
1
4
  #
2
- # newton.rb
5
+ # newton.rb
3
6
  #
4
7
  # Solves the nonlinear algebraic equation system f = 0 by Newton's method.
5
8
  # This program is not dependent on BigDecimal.
@@ -22,13 +25,11 @@
22
25
  #
23
26
  # On exit, x is the solution vector.
24
27
  #
25
- require "bigdecimal/ludcmp"
26
- require "bigdecimal/jacobian"
27
-
28
28
  module Newton
29
29
  include LUSolve
30
30
  include Jacobian
31
-
31
+ module_function
32
+
32
33
  def norm(fv,zero=0.0)
33
34
  s = zero
34
35
  n = fv.size
@@ -1,65 +1,109 @@
1
- #
2
- # BigDecimal utility library.
3
- #
4
- # To use these functions, require 'bigdecimal/util'
5
- #
6
- # The following methods are provided to convert other types to BigDecimals:
7
- #
8
- # String#to_d -> BigDecimal
9
- # Float#to_d -> BigDecimal
10
- # Rational#to_d -> BigDecimal
11
- #
12
- # The following method is provided to convert BigDecimals to other types:
13
- #
14
- # BigDecimal#to_r -> Rational
15
- #
16
- # ----------------------------------------------------------------------
17
- #
18
- class Float < Numeric
1
+ class Integer < Numeric
2
+ # call-seq:
3
+ # int.to_d -> bigdecimal
4
+ #
5
+ # Convert +int+ to a BigDecimal and return it.
6
+ #
7
+ # require 'bigdecimal'
8
+ # require 'bigdecimal/util'
9
+ #
10
+ # 42.to_d
11
+ # # => #<BigDecimal:1008ef070,'0.42E2',9(36)>
12
+ #
19
13
  def to_d
20
- BigDecimal(self.to_s)
14
+ BigDecimal(self)
15
+ end
16
+ end
17
+
18
+ class Float < Numeric
19
+ # call-seq:
20
+ # flt.to_d(precision=nil) -> bigdecimal
21
+ #
22
+ # Convert +flt+ to a BigDecimal and return it.
23
+ #
24
+ # require 'bigdecimal'
25
+ # require 'bigdecimal/util'
26
+ #
27
+ # 0.5.to_d
28
+ # # => #<BigDecimal:1dc69e0,'0.5E0',9(18)>
29
+ #
30
+ def to_d(precision=nil)
31
+ BigDecimal(self, precision || Float::DIG+1)
21
32
  end
22
33
  end
23
34
 
24
35
  class String
36
+ # call-seq:
37
+ # string.to_d -> bigdecimal
38
+ #
39
+ # Convert +string+ to a BigDecimal and return it.
40
+ #
41
+ # require 'bigdecimal'
42
+ # require 'bigdecimal/util'
43
+ #
44
+ # "0.5".to_d
45
+ # # => #<BigDecimal:1dc69e0,'0.5E0',9(18)>
46
+ #
25
47
  def to_d
26
48
  BigDecimal(self)
27
49
  end
28
50
  end
29
51
 
30
52
  class BigDecimal < Numeric
53
+ # call-seq:
54
+ # a.to_digits -> string
55
+ #
31
56
  # Converts a BigDecimal to a String of the form "nnnnnn.mmm".
32
57
  # This method is deprecated; use BigDecimal#to_s("F") instead.
58
+ #
59
+ # require 'bigdecimal'
60
+ # require 'bigdecimal/util'
61
+ #
62
+ # d = BigDecimal.new("3.14")
63
+ # d.to_digits
64
+ # # => "3.14"
33
65
  def to_digits
34
- if self.nan? || self.infinite? || self.zero?
35
- self.to_s
36
- else
37
- i = self.to_i.to_s
38
- s,f,y,z = self.frac.split
39
- i + "." + ("0"*(-z)) + f
40
- end
66
+ if self.nan? || self.infinite? || self.zero?
67
+ self.to_s
68
+ else
69
+ i = self.to_i.to_s
70
+ _,f,_,z = self.frac.split
71
+ i + "." + ("0"*(-z)) + f
72
+ end
41
73
  end
42
74
 
43
- # Converts a BigDecimal to a Rational.
44
- def to_r
45
- sign,digits,base,power = self.split
46
- numerator = sign*digits.to_i
47
- denomi_power = power - digits.size # base is always 10
48
- if denomi_power < 0
49
- Rational(numerator,base ** (-denomi_power))
50
- else
51
- Rational(numerator * (base ** denomi_power),1)
52
- end
75
+ # call-seq:
76
+ # a.to_d -> bigdecimal
77
+ #
78
+ # Returns self.
79
+ def to_d
80
+ self
53
81
  end
54
82
  end
55
83
 
56
84
  class Rational < Numeric
57
- # Converts a Rational to a BigDecimal
58
- def to_d(nFig=0)
59
- num = self.numerator.to_s
60
- if nFig<=0
61
- nFig = BigDecimal.double_fig*2+1
62
- end
63
- BigDecimal.new(num).div(self.denominator,nFig)
85
+ # call-seq:
86
+ # r.to_d(sig) -> bigdecimal
87
+ #
88
+ # Converts a Rational to a BigDecimal. Takes an optional parameter +sig+ to
89
+ # limit the amount of significant digits.
90
+ # If a negative precision is given, raise ArgumentError.
91
+ # The zero precision and implicit precision is deprecated.
92
+ #
93
+ # r = (22/7.0).to_r
94
+ # # => (7077085128725065/2251799813685248)
95
+ # r.to_d
96
+ # # => #<BigDecimal:1a52bd8,'0.3142857142 8571427937 0154144999 105E1',45(63)>
97
+ # r.to_d(3)
98
+ # # => #<BigDecimal:1a44d08,'0.314E1',18(36)>
99
+ def to_d(precision=0)
100
+ if precision < 0
101
+ raise ArgumentError, "negative precision"
102
+ elsif precision == 0
103
+ warn "zero and implicit precision is deprecated."
104
+ precision = BigDecimal.double_fig*2+1
105
+ end
106
+ num = self.numerator
107
+ BigDecimal(num).div(self.denominator, precision)
64
108
  end
65
109
  end
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
2
  module BigDecimal
3
- VERSION = "1.0.0"
3
+ VERSION = "2.0.2"
4
4
  end
5
5
  end
@@ -17,8 +17,10 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
+ spec.required_ruby_version = "~> 2.0"
21
+
20
22
  spec.add_development_dependency "bundler", "~> 1.3"
21
23
  spec.add_development_dependency "rake", "~> 10.0"
22
24
  spec.add_development_dependency "mspec", "~> 1.5"
23
- spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
25
+ spec.add_development_dependency "rubysl-prettyprint", "~> 2.0"
24
26
  end
metadata CHANGED
@@ -1,71 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysl-bigdecimal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Shirai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-27 00:00:00.000000000 Z
11
+ date: 2013-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.5'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.5'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rubysl-prettyprint
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '1.0'
61
+ version: '2.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '1.0'
68
+ version: '2.0'
69
69
  description: Ruby standard library bigdecimal.
70
70
  email:
71
71
  - brixen@gmail.com
@@ -74,26 +74,22 @@ extensions:
74
74
  - ext/rubysl/bigdecimal/extconf.rb
75
75
  extra_rdoc_files: []
76
76
  files:
77
- - .gitignore
78
- - .travis.yml
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
79
  - Gemfile
80
80
  - LICENSE
81
81
  - README.md
82
82
  - Rakefile
83
+ - bigdecimal_en.html
84
+ - bigdecimal_ja.html
83
85
  - ext/rubysl/bigdecimal/bigdecimal.c
84
86
  - ext/rubysl/bigdecimal/bigdecimal.h
85
87
  - ext/rubysl/bigdecimal/extconf.rb
86
88
  - lib/bigdecimal.rb
87
- - lib/bigdecimal/README
88
- - lib/bigdecimal/bigdecimal_en.html
89
- - lib/bigdecimal/bigdecimal_ja.html
90
89
  - lib/bigdecimal/jacobian.rb
91
90
  - lib/bigdecimal/ludcmp.rb
92
91
  - lib/bigdecimal/math.rb
93
92
  - lib/bigdecimal/newton.rb
94
- - lib/bigdecimal/sample/linear.rb
95
- - lib/bigdecimal/sample/nlsolve.rb
96
- - lib/bigdecimal/sample/pi.rb
97
93
  - lib/bigdecimal/util.rb
98
94
  - lib/rubysl/bigdecimal.rb
99
95
  - lib/rubysl/bigdecimal/version.rb
@@ -167,12 +163,12 @@ require_paths:
167
163
  - lib
168
164
  required_ruby_version: !ruby/object:Gem::Requirement
169
165
  requirements:
170
- - - '>='
166
+ - - "~>"
171
167
  - !ruby/object:Gem::Version
172
- version: '0'
168
+ version: '2.0'
173
169
  required_rubygems_version: !ruby/object:Gem::Requirement
174
170
  requirements:
175
- - - '>='
171
+ - - ">="
176
172
  - !ruby/object:Gem::Version
177
173
  version: '0'
178
174
  requirements: []
@@ -241,3 +237,4 @@ test_files:
241
237
  - spec/uplus_spec.rb
242
238
  - spec/ver_spec.rb
243
239
  - spec/zero_spec.rb
240
+ has_rdoc:
@@ -1,60 +0,0 @@
1
-
2
- Ruby BIGDECIMAL(Variable Precision) extension library.
3
- Copyright (C) 1999 by Shigeo Kobayashi(shigeo@tinyforest.gr.jp)
4
-
5
- BigDecimal is copyrighted free software by Shigeo Kobayashi <shigeo@tinyforest.gr.jp>.
6
- You can redistribute it and/or modify it under either the terms of the GPL
7
- (see COPYING file), or the conditions below:
8
-
9
- 1. You may make and give away verbatim copies of the source form of the
10
- software without restriction, provided that you duplicate all of the
11
- original copyright notices and associated disclaimers.
12
-
13
- 2. You may modify your copy of the software in any way, provided that
14
- you do at least ONE of the following:
15
-
16
- a) place your modifications in the Public Domain or otherwise
17
- make them Freely Available, such as by posting said
18
- modifications to Usenet or an equivalent medium, or by allowing
19
- the author to include your modifications in the software.
20
-
21
- b) use the modified software only within your corporation or
22
- organization.
23
-
24
- c) rename any non-standard executables so the names do not conflict
25
- with standard executables, which must also be provided.
26
-
27
- d) make other distribution arrangements with the author.
28
-
29
- 3. You may distribute the software in object code or executable
30
- form, provided that you do at least ONE of the following:
31
-
32
- a) distribute the executables and library files of the software,
33
- together with instructions (in the manual page or equivalent)
34
- on where to get the original distribution.
35
-
36
- b) accompany the distribution with the machine-readable source of
37
- the software.
38
-
39
- c) give non-standard executables non-standard names, with
40
- instructions on where to get the original software distribution.
41
-
42
- d) make other distribution arrangements with the author.
43
-
44
- 4. You may modify and include the part of the software into any other
45
- software (possibly commercial).
46
-
47
- 5. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
48
- IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
49
- WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
50
- PURPOSE.
51
-
52
- * The Author
53
-
54
- Feel free to send comments and bug reports to the author. Here is the
55
- author's latest mail address:
56
-
57
- shigeo@tinyforest.gr.jp
58
-
59
- -------------------------------------------------------
60
- created at: Thu Dec 22 1999
@@ -1,71 +0,0 @@
1
- #!/usr/local/bin/ruby
2
-
3
- #
4
- # linear.rb
5
- #
6
- # Solves linear equation system(A*x = b) by LU decomposition method.
7
- # where A is a coefficient matrix,x is an answer vector,b is a constant vector.
8
- #
9
- # USAGE:
10
- # ruby linear.rb [input file solved]
11
- #
12
-
13
- require "bigdecimal"
14
- require "bigdecimal/ludcmp"
15
-
16
- #
17
- # NOTE:
18
- # Change following BigDecimal::limit() if needed.
19
- BigDecimal::limit(100)
20
- #
21
-
22
- include LUSolve
23
- def rd_order(na)
24
- printf("Number of equations ?") if(na <= 0)
25
- n = ARGF.gets().to_i
26
- end
27
-
28
- na = ARGV.size
29
- zero = BigDecimal::new("0.0")
30
- one = BigDecimal::new("1.0")
31
-
32
- while (n=rd_order(na))>0
33
- a = []
34
- as= []
35
- b = []
36
- if na <= 0
37
- # Read data from console.
38
- printf("\nEnter coefficient matrix element A[i,j]\n");
39
- for i in 0...n do
40
- for j in 0...n do
41
- printf("A[%d,%d]? ",i,j); s = ARGF.gets
42
- a << BigDecimal::new(s);
43
- as << BigDecimal::new(s);
44
- end
45
- printf("Contatant vector element b[%d] ? ",i); b << BigDecimal::new(ARGF.gets);
46
- end
47
- else
48
- # Read data from specified file.
49
- printf("Coefficient matrix and constant vector.\n");
50
- for i in 0...n do
51
- s = ARGF.gets
52
- printf("%d) %s",i,s)
53
- s = s.split
54
- for j in 0...n do
55
- a << BigDecimal::new(s[j]);
56
- as << BigDecimal::new(s[j]);
57
- end
58
- b << BigDecimal::new(s[n]);
59
- end
60
- end
61
- x = lusolve(a,b,ludecomp(a,n,zero,one),zero)
62
- printf("Answer(x[i] & (A*x-b)[i]) follows\n")
63
- for i in 0...n do
64
- printf("x[%d]=%s ",i,x[i].to_s)
65
- s = zero
66
- for j in 0...n do
67
- s = s + as[i*n+j]*x[j]
68
- end
69
- printf(" & %s\n",(s-b[i]).to_s)
70
- end
71
- end