malge 0.0.8 → 0.0.9

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.
@@ -3,24 +3,39 @@ require "malge.rb"
3
3
  # Solve simultatneous equations with Cramer\'s formula.
4
4
  #
5
5
  # if you need to solve simultaneous equations,
6
- # (a_11 ... a_1n) (x_1) (c_1)
7
- # (.... ... ....) (...) = (...)
8
- # (a_n1 ... a_nn) (x_n) (c_n)
6
+ # (a_11 ... a_1n) (x_1) (c_1)
7
+ # (.... ... ....) (...) = (...)
8
+ # (a_n1 ... a_nn) (x_n) (c_n)
9
9
  # command
10
10
  # % #{$0} a_11 ... a_1n ... a_n1 ... a_nn c_1 ... c_n
11
11
  #
12
12
 
13
13
  # 連立1次方程式を表現するクラス。
14
14
  class Malge::SimultaneousEquations
15
+
16
+ class NotRegularError < Exception; end
17
+ class SizeMismatchError < Exception; end
18
+ class NotSquareError < Exception; end
19
+
15
20
 
16
21
  # Solve equation by Cramer's.
17
22
  # matrix は2重配列で与える。
18
23
  # 返されるのは、解の値を格納した配列。
19
24
  def self.cramer( matrix, values )
20
25
  matrix = Malge::Matrix.rows( matrix )
21
- raise "Matrix is not squre: #{matrix.row_size} x #{matrix.column_size}." if ( ! matrix.square? )
22
- raise "Matrix size (#{matrix.row_size}) and values size (#{values.size}) mismatched." if ( matrix.row_size != values.size )
23
- raise "Matrix is not regular. Degree of freedom is #{matrix.column_size - matrix.rank}.\n" if ( ! matrix.regular? )
26
+ if ( ! matrix.square? )
27
+ str = "Matrix is not squre: #{matrix.row_size} x #{matrix.column_size}."
28
+ raise NotSquareError, str
29
+ end
30
+ if ( matrix.row_size != values.size )
31
+ str = "Matrix size (#{matrix.row_size}) and values size (#{values.size}) mismatched."
32
+ raise SizeMismatchError, str
33
+ end
34
+ if ( ! matrix.regular? )
35
+ str = "Matrix is not regular. Degree of freedom is #{matrix.column_size - matrix.rank}.\n"
36
+ str += matrix.to_s
37
+ raise NotRegularError, str
38
+ end
24
39
 
25
40
  results = Array.new
26
41
  n = matrix.column_size
@@ -0,0 +1,12 @@
1
+ ENCUT TOTEN
2
+ 500.00000000 -11.12271000
3
+ 550.00000000 -11.14582400
4
+ 600.00000000 -11.13928400
5
+ 650.00000000 -11.13989200
6
+ 700.00000000 -11.15230500
7
+ 750.00000000 -11.14165100
8
+ 800.00000000 -11.15094500
9
+ 850.00000000 -11.15119000
10
+ 900.00000000 -11.14783000
11
+ 950.00000000 -11.14986900
12
+ 1000.00000000 -11.15151900
@@ -1,11 +1,11 @@
1
1
  require 'rubygems'
2
2
  require 'bundler'
3
3
  begin
4
- Bundler.setup(:default, :development)
4
+ Bundler.setup(:default, :development)
5
5
  rescue Bundler::BundlerError => e
6
- $stderr.puts e.message
7
- $stderr.puts "Run `bundle install` to install missing gems"
8
- exit e.status_code
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
9
  end
10
10
  require 'test/unit'
11
11
 
@@ -1,6 +1,7 @@
1
1
  #! /usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
+ require "pp"
4
5
  require "helper"
5
6
  #require "test/unit"
6
7
  #require "malge.rb"
@@ -23,6 +24,10 @@ class Malge::ErrorFittedFunction::Dummy01 < Malge::ErrorFittedFunction
23
24
  return 1.0/2.0 * (y - 1.0)
24
25
  end
25
26
 
27
+ def equation
28
+ "x = 1/2( y - 1.0)"
29
+ end
30
+
26
31
  #Assume: y = 1.0 + 2.0 * x
27
32
  def fit
28
33
  @coefficients = [1.0, 2.0]
@@ -56,14 +61,38 @@ class Malge::ErrorFittedFunction::Dummy02 < Malge::ErrorFittedFunction
56
61
  end
57
62
  end
58
63
 
64
+ class Malge::ErrorFittedFunction::Dummy03 < Malge::ErrorFittedFunction
65
+ $tolerance = 1E-10
66
+
67
+ def expected_error(x)
68
+ @coefficients[0] + @coefficients[1] * x
69
+ end
70
+
71
+ #Assume: y = 1.0 + 2.0 * x
72
+ #x = 1/2( y - 1.0)
73
+ def x(y)
74
+ return 1.0/2.0 * (y - 1.0)
75
+ end
76
+
77
+ #Assume: y = 1.0 + 2.0 * x
78
+ def fit
79
+ @coefficients = [1.0, 2.0]
80
+ end
81
+
82
+ def most_strict_pair
83
+ @raw_pairs.max_by{ |pair| pair[0] }
84
+ end
85
+
86
+ end
87
+
59
88
 
60
89
  class TC_ErrorFittedFunction < Test::Unit::TestCase
61
90
  def setup
62
91
  @eff01 = Malge::ErrorFittedFunction::Dummy01.new(
63
92
  [
64
93
  [0.0, 0.0],
65
- [1.0, 5.0],
66
94
  [2.0, 4.0],
95
+ [1.0, 5.0],
67
96
  ]
68
97
  )
69
98
 
@@ -82,7 +111,10 @@ class TC_ErrorFittedFunction < Test::Unit::TestCase
82
111
  end
83
112
 
84
113
  def test_variance
85
- assert_equal(38.0, @eff01.variance)
114
+ # diff_abs_pairs = [[0.0, 4.0], [1.0, 1.0]
115
+ # function = 2x + 1
116
+ # Then, 3 **2 + 2**2 = 13
117
+ assert_equal(13.0, @eff01.variance)
86
118
  end
87
119
 
88
120
  def test_expected_error
@@ -92,40 +124,49 @@ class TC_ErrorFittedFunction < Test::Unit::TestCase
92
124
  end
93
125
 
94
126
  def test_equation
127
+ eff = Malge::ErrorFittedFunction::Dummy03.new(
128
+ [
129
+ [0.0, 0.0],
130
+ [1.0, 5.0],
131
+ [2.0, 4.0],
132
+ ]
133
+ )
95
134
  assert_raise(Malge::ErrorFittedFunction::NotImplementedError){
96
- @eff01.equation
135
+ eff.equation
97
136
  }
98
137
  end
99
138
 
100
139
  def test_initialize_diff_abs_pairs
101
140
 
102
- assert_equal(1000.0, @eff02.diff_abs_pairs[0][0])
103
- assert_equal(1200.0, @eff02.diff_abs_pairs[1][0])
104
- assert_equal(1500.0, @eff02.diff_abs_pairs[2][0])
105
- assert_equal( 500.0, @eff02.diff_abs_pairs[3][0])
106
- assert_equal( 600.0, @eff02.diff_abs_pairs[4][0])
107
- assert_equal( 700.0, @eff02.diff_abs_pairs[5][0])
108
- assert_equal( 900.0, @eff02.diff_abs_pairs[6][0])
109
- assert_equal( 500.0, @eff02.diff_abs_pairs[7][0])
110
-
111
- assert_in_delta(0.001930, @eff02.diff_abs_pairs[0][1], $tolerance)
112
- assert_in_delta(0.001081, @eff02.diff_abs_pairs[1][1], $tolerance)
113
- assert_in_delta(0.0 , @eff02.diff_abs_pairs[2][1], $tolerance)
114
- assert_in_delta(0.038457, @eff02.diff_abs_pairs[3][1], $tolerance)
115
- assert_in_delta(0.030196, @eff02.diff_abs_pairs[4][1], $tolerance)
116
- assert_in_delta(0.013779, @eff02.diff_abs_pairs[5][1], $tolerance)
117
- assert_in_delta(0.001336, @eff02.diff_abs_pairs[6][1], $tolerance)
118
- assert_in_delta(0.038457, @eff02.diff_abs_pairs[7][1], $tolerance)
141
+ assert_equal( 500.0, @eff02.diff_abs_pairs[0][0])
142
+ assert_equal( 500.0, @eff02.diff_abs_pairs[1][0])
143
+ assert_equal( 600.0, @eff02.diff_abs_pairs[2][0])
144
+ assert_equal( 700.0, @eff02.diff_abs_pairs[3][0])
145
+ assert_equal( 900.0, @eff02.diff_abs_pairs[4][0])
146
+ assert_equal(1000.0, @eff02.diff_abs_pairs[5][0])
147
+ assert_equal(1200.0, @eff02.diff_abs_pairs[6][0])
148
+
149
+ assert_in_delta(0.038457, @eff02.diff_abs_pairs[0][1], $tolerance)
150
+ assert_in_delta(0.038457, @eff02.diff_abs_pairs[1][1], $tolerance)
151
+ assert_in_delta(0.030196, @eff02.diff_abs_pairs[2][1], $tolerance)
152
+ assert_in_delta(0.013779, @eff02.diff_abs_pairs[3][1], $tolerance)
153
+ assert_in_delta(0.001336, @eff02.diff_abs_pairs[4][1], $tolerance)
154
+ assert_in_delta(0.001930, @eff02.diff_abs_pairs[5][1], $tolerance)
155
+ assert_in_delta(0.001081, @eff02.diff_abs_pairs[6][1], $tolerance)
119
156
  end
120
157
 
121
158
  def test_most_strict_pair
122
159
  assert_equal(2.0, @eff01.most_strict_pair[0])
123
160
  assert_equal(4.0, @eff01.most_strict_pair[1])
124
161
 
125
- assert_equal(1500.0 , @eff02.most_strict_pair[0])
162
+ assert_equal(1500.0 , @eff02.most_strict_pair[0])
126
163
  assert_equal(-3.151397, @eff02.most_strict_pair[1])
127
164
  end
128
165
 
166
+ def test_count_equal_under_over
167
+ #pp @eff01
168
+ assert_equal([0, 1, 1], @eff01.count_equal_under_over)
169
+ end
129
170
 
130
171
  def test_initialize
131
172
  assert_raise(Malge::ErrorFittedFunction::NotImplementedError){
@@ -150,5 +191,21 @@ class TC_ErrorFittedFunction < Test::Unit::TestCase
150
191
  }
151
192
  end
152
193
 
194
+ def test_summary
195
+ io = StringIO.new
196
+ @eff01.summary(io)
197
+ io.rewind
198
+ #pp io.readlines
199
+ io.rewind
200
+ assert_equal(
201
+ ["Fitted function: x = 1/2( y - 1.0)\n",
202
+ " x, raw_y, |diff_y_best|, expected_error\n",
203
+ " 0.0000000000, 0.0000000000, 4.0000000000, 1.0000000000\n",
204
+ " 1.0000000000, 5.0000000000, 1.0000000000, 3.0000000000\n",
205
+ " 2.0000000000, 4.0000000000, ------------, 5.0000000000\n"
206
+ ],
207
+ io.readlines
208
+ )
209
+ end
153
210
  end
154
211
 
@@ -38,7 +38,7 @@ class TC_ErrorFittedFunction_AExpBX < Test::Unit::TestCase
38
38
  #end
39
39
 
40
40
  def test_equation
41
- assert_equal("f(x) = 4.000000 * exp(-0.693147 * x)", @aebx00.equation)
41
+ assert_equal("4.000000 * exp(-0.693147 * x)", @aebx00.equation)
42
42
  end
43
43
 
44
44
  def test_fit
@@ -63,12 +63,13 @@ class TC_ErrorFittedFunction_AExpBX < Test::Unit::TestCase
63
63
  end
64
64
 
65
65
  def test_most_strict
66
- assert_in_delta( 3.0, @aebx00.most_strict_pair[0])
66
+ assert_in_delta( 3.0, @aebx00.most_strict_pair[0])
67
67
  assert_in_delta(100.0, @aebx00.most_strict_pair[1])
68
68
  end
69
69
 
70
70
  def test_variance
71
- assert_in_delta( 0.25, @aebx00.variance, TOLERANCE)
71
+ assert_in_delta( 0.0, @aebx00.variance, TOLERANCE)
72
+ #assert_in_delta( 0.25, @aebx00.variance, TOLERANCE)
72
73
  #diff_abs = [4,1]
73
74
  #expected = [1,3]
74
75
  end
@@ -15,19 +15,19 @@ class TC_ErrorFittedFunction_AExpBX32 < Test::Unit::TestCase
15
15
  def setup
16
16
  @aebx00 = Malge::ErrorFittedFunction::AExpBX32.new(
17
17
  [
18
- [ 0.0, 100.0 + 3.0* 2.0**(-2.0 * 0 )],
19
- [ 1.0, 100.0 + 3.0* 2.0**(-2.0 * 1.0 ) ],
18
+ [ 0.0, 100.0 + 3.0* 2.0**(-2.0 * 0 )],
19
+ [ 1.0, 100.0 + 3.0* 2.0**(-2.0 * 1.0 ) ],
20
20
  [ 2.0, 100.0 + 3.0* 2.0**(-2.0 * (2.0**(3.0/2.0)))],
21
- [ 3.0, 100.0 ],
21
+ [ 3.0, 100.0 ],
22
22
  ]
23
23
  )
24
24
 
25
25
  @aebx01 = Malge::ErrorFittedFunction::AExpBX32.new(
26
26
  [
27
- [ 0.0, 100.0 + 3.0* 2.0**(-2.0 * 0 )],
28
- [ 1.0, 100.0 - 3.0* 2.0**(-2.0 * 1.0 ) ],
27
+ [ 0.0, 100.0 + 3.0* 2.0**(-2.0 * 0 )],
28
+ [ 1.0, 100.0 - 3.0* 2.0**(-2.0 * 1.0 ) ],
29
29
  [ 2.0, 100.0 + 3.0* 2.0**(-2.0 * (2.0**(3.0/2.0)))],
30
- [ 3.0, 100.0 ],
30
+ [ 3.0, 100.0 ],
31
31
  ]
32
32
  )
33
33
  end
@@ -36,7 +36,7 @@ class TC_ErrorFittedFunction_AExpBX32 < Test::Unit::TestCase
36
36
  #end
37
37
 
38
38
  def test_equation
39
- assert_equal("f(x) = 3.000000 * exp(-1.386294 * x^{3/2})", @aebx00.equation)
39
+ assert_equal("3.000000 * exp(-1.386294 * x**(3.0/2.0))", @aebx00.equation)
40
40
  end
41
41
 
42
42
 
@@ -78,12 +78,13 @@ class TC_ErrorFittedFunction_AExpBX32 < Test::Unit::TestCase
78
78
  end
79
79
 
80
80
  def test_most_strict_pair
81
- assert_in_delta( 3.0, @aebx00.most_strict_pair[0])
81
+ assert_in_delta( 3.0, @aebx00.most_strict_pair[0])
82
82
  assert_in_delta(100.0, @aebx00.most_strict_pair[1])
83
83
  end
84
84
 
85
85
  def test_variance
86
- correct = ( 3.0* 2.0**(-2.0 * (3.0**(3.0/2.0))))**2.0
86
+ #correct = (3.0* 2.0**(-2.0 * (3.0**(3.0/2.0))))**2.0
87
+ correct = 0.0
87
88
  assert_in_delta(correct, @aebx00.variance, TOLERANCE)
88
89
  assert_in_delta(correct, @aebx01.variance, TOLERANCE)
89
90
  end
@@ -4,7 +4,6 @@
4
4
  require "helper"
5
5
  #require "test/unit"
6
6
  #require "malge.rb"
7
- #require "malge/errorfittedfunction.rb"
8
7
 
9
8
  class TC_ErrorFittedFunction_AXInv < Test::Unit::TestCase
10
9
 
@@ -13,14 +12,15 @@ class TC_ErrorFittedFunction_AXInv < Test::Unit::TestCase
13
12
  def setup
14
13
  @axi00 = Malge::ErrorFittedFunction::AXInv.new(
15
14
  [
16
- [1.0, 1.0],
17
15
  [0.5, 3.0],
16
+ [1.0, 1.0],
17
+ [2.0, 1.0],
18
18
  ]
19
19
  )
20
20
  end
21
21
 
22
22
  def test_equation
23
- assert_equal("f(x) = 0.800000 / x", @axi00.equation)
23
+ assert_equal("0.800000 / x", @axi00.equation)
24
24
  end
25
25
 
26
26
  def test_fit
@@ -30,15 +30,14 @@ class TC_ErrorFittedFunction_AXInv < Test::Unit::TestCase
30
30
  def test_expected_error
31
31
  assert_in_delta(0.8, @axi00.expected_error(1.0), $tolerance)
32
32
  assert_in_delta(0.4, @axi00.expected_error(2.0), $tolerance)
33
-
34
33
  end
35
34
 
36
35
  def test_most_strict_pair
37
- assert_in_delta( 1.0, @axi00.most_strict_pair[0])
38
- assert_in_delta( 1.0, @axi00.most_strict_pair[1])
36
+ #pp @axi00.raw_pairs
37
+ assert_in_delta( 2.0, @axi00.most_strict_pair[0])
38
+ assert_in_delta( 1.0, @axi00.most_strict_pair[1])
39
39
  end
40
40
 
41
-
42
41
  def test_variance
43
42
  assert_equal( 0.8, @axi00.variance)
44
43
  end
@@ -12,14 +12,15 @@ class TC_ErrorFittedFunction_AXInv2 < Test::Unit::TestCase
12
12
  def setup
13
13
  @axi200 = Malge::ErrorFittedFunction::AXInv2.new(
14
14
  [
15
- [1.0, 1.0],
16
- [2.0**(-1.0/2.0), 3.0],
15
+ [(2.0 ** (-1.0/2.0)), 3.0],
16
+ [(2.0 ** ( 0 )), 1.0],
17
+ [(2.0 ** ( 1.0/2.0)), 1.0],
17
18
  ]
18
19
  )
19
20
  end
20
21
 
21
22
  def test_equation
22
- assert_equal("f(x) = 0.800000 / (x^2)", @axi200.equation)
23
+ assert_equal("0.800000 / (x**2)", @axi200.equation)
23
24
  end
24
25
 
25
26
  def test_fit
@@ -32,11 +33,10 @@ class TC_ErrorFittedFunction_AXInv2 < Test::Unit::TestCase
32
33
  end
33
34
 
34
35
  def test_most_strict_pair
35
- assert_in_delta( 1.0, @axi200.most_strict_pair[0])
36
- assert_in_delta( 1.0, @axi200.most_strict_pair[1])
36
+ assert_in_delta((2.0 ** ( 1.0/2.0)), @axi200.most_strict_pair[0])
37
+ assert_in_delta( 1.0, @axi200.most_strict_pair[1])
37
38
  end
38
39
 
39
-
40
40
  def test_variance
41
41
  assert_in_delta( 0.8, @axi200.variance, $tolerance)
42
42
  end
@@ -45,53 +45,5 @@ class TC_ErrorFittedFunction_AXInv2 < Test::Unit::TestCase
45
45
  assert_in_delta(1.0, @axi200.x(0.8), $tolerance)
46
46
  end
47
47
 
48
-
49
- #def setup
50
- # @axi201 = Malge::ErrorFittedFunction::AXInv2.new(
51
- # [
52
- # [1.0, 20.0],
53
- # [2.0, 8.0],
54
- # [4.0, 5.0],
55
- # [8.0, 4.0],
56
- # ]
57
- # )
58
- #end
59
-
60
- ##def test_initialize
61
- ##end
62
-
63
- #def test_equation
64
- # assert_equal("f(x) = 16.000000 / (x^2)", @axi201.equation)
65
- #end
66
-
67
- #def test_fit
68
- # assert_equal([16.0], @axi201.coefficients)
69
- #end
70
-
71
- #def test_expected_error
72
- # assert_equal(16.0 , @axi201.expected_error(1.0))
73
- # assert_equal( 4.0 , @axi201.expected_error(2.0))
74
- # assert_equal( 1.0 , @axi201.expected_error(4.0))
75
- # assert_equal( 0.25, @axi201.expected_error(8.0))
76
- #end
77
-
78
- #def test_most_strict_y
79
- # assert_equal( 4.0, @axi201.most_strict_y)
80
- #end
81
-
82
- #def test_most_strict_x
83
- # assert_equal(8.0, @axi201.most_strict_x)
84
- #end
85
-
86
- #def test_variance
87
- # assert_equal( 0.0, @axi201.variance)
88
- # #diff_abs = [4,1]
89
- # #expected = [1,3]
90
- #end
91
-
92
- #def test_x
93
- # assert_equal(2.0, @axi201.x(4.0))
94
- #end
95
-
96
48
  end
97
49
 
@@ -6,19 +6,21 @@ require "helper"
6
6
  #require "malge.rb"
7
7
 
8
8
  class TC_ErrorFittedFunction_AXInv3 < Test::Unit::TestCase
9
+
9
10
  $tolerance = 1E-10
10
11
 
11
12
  def setup
12
13
  @axi300 = Malge::ErrorFittedFunction::AXInv3.new(
13
14
  [
14
- [1.0, 1.0],
15
- [2.0**(-1.0/3.0), 3.0],
15
+ [(2.0 ** (-1.0/3.0)), 3.0],
16
+ [(2.0 ** ( 0 )), 1.0],
17
+ [(2.0 ** ( 1.0/3.0)), 1.0],
16
18
  ]
17
19
  )
18
20
  end
19
21
 
20
22
  def test_equation
21
- assert_equal("f(x) = 0.800000 / (x^3)", @axi300.equation)
23
+ assert_equal("0.800000 / (x**3)", @axi300.equation)
22
24
  end
23
25
 
24
26
  def test_fit
@@ -31,11 +33,10 @@ class TC_ErrorFittedFunction_AXInv3 < Test::Unit::TestCase
31
33
  end
32
34
 
33
35
  def test_most_strict_pair
34
- assert_in_delta( 1.0, @axi300.most_strict_pair[0])
35
- assert_in_delta( 1.0, @axi300.most_strict_pair[1])
36
+ assert_in_delta( (2.0 ** ( 1.0/3.0)), @axi300.most_strict_pair[0])
37
+ assert_in_delta( 1.0, @axi300.most_strict_pair[1])
36
38
  end
37
39
 
38
-
39
40
  def test_variance
40
41
  assert_in_delta( 0.8, @axi300.variance, $tolerance)
41
42
  end
@@ -44,52 +45,5 @@ class TC_ErrorFittedFunction_AXInv3 < Test::Unit::TestCase
44
45
  assert_in_delta(1.0, @axi300.x(0.8), $tolerance)
45
46
  end
46
47
 
47
- #def setup
48
- # @axi301 = Malge::ErrorFittedFunction::AXInv3.new(
49
- # [
50
- # [1.0, 68.0],
51
- # [2.0, 12.0],
52
- # [4.0, 5.0],
53
- # [8.0, 4.0],
54
- # ]
55
- # )
56
- #end
57
-
58
- ##def test_initialize
59
- ##end
60
-
61
- #def test_equation
62
- # assert_equal("f(x) = 64.000000 / (x^3)", @axi301.equation)
63
- #end
64
-
65
- #def test_fit
66
- # assert_equal([64.0], @axi301.coefficients)
67
- #end
68
-
69
- #def test_expected_error
70
- # assert_equal(64.0 , @axi301.expected_error(1.0))
71
- # assert_equal( 8.0 , @axi301.expected_error(2.0))
72
- # assert_equal( 1.0 , @axi301.expected_error(4.0))
73
- # assert_equal( 0.125, @axi301.expected_error(8.0))
74
- #end
75
-
76
- #def test_most_strict_y
77
- # assert_equal( 4.0, @axi301.most_strict_y)
78
- #end
79
-
80
- #def test_most_strict_x
81
- # assert_equal(8.0, @axi301.most_strict_x)
82
- #end
83
-
84
- #def test_variance
85
- # assert_equal( 0.0, @axi301.variance)
86
- # #diff_abs = [4,1]
87
- # #expected = [1,3]
88
- #end
89
-
90
- #def test_x
91
- # assert_equal(2.0, @axi301.x(8.0))
92
- #end
93
-
94
48
  end
95
49