bigdecimal 3.2.3 → 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.
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
- require "bigdecimal/newton"
11
- include Newton
7
+ require_relative "linear"
12
8
 
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
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
- f = BigDecimal.limit(100)
37
- f = Function.new
38
- x = [f.zero,f.zero] # Initial values
39
- n = nlsolve(f,x)
40
- p x
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:)
data/sample/pi.rb CHANGED
@@ -1,21 +1,16 @@
1
- #!/usr/local/bin/ruby
2
- # frozen_string_literal: false
3
-
4
1
  #
5
2
  # pi.rb
6
3
  #
7
4
  # Calculates 3.1415.... (the number of times that a circle's diameter
8
- # will fit around the circle) using J. Machin's formula.
5
+ # will fit around the circle)
9
6
  #
10
7
 
11
8
  require "bigdecimal"
12
9
  require "bigdecimal/math.rb"
13
10
 
14
- include BigMath
15
-
16
11
  if ARGV.size == 1
17
12
  print "PI("+ARGV[0]+"):\n"
18
- p PI(ARGV[0].to_i)
13
+ p BigMath.PI(ARGV[0].to_i)
19
14
  else
20
15
  print "TRY: ruby pi.rb 1000 \n"
21
16
  end