malge 0.0.2 → 0.0.3
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/CHANGES +7 -3
- data/Gemfile +1 -1
- data/VERSION +1 -1
- data/lib/malge.rb +1 -1
- data/lib/malge/leastsquare.rb +65 -9
- data/malge.gemspec +2 -5
- data/test/test_leastsquare.rb +88 -3
- metadata +11 -22
data/CHANGES
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
= vasputils changelog
|
2
2
|
|
3
|
-
== Master (for 0.0.
|
3
|
+
== Master (for 0.0.4)
|
4
|
+
|
5
|
+
== Version 0.0.3
|
6
|
+
* Add Malge::LeastSquare::a_exp_bx
|
7
|
+
* Add Malge::LeastSquare::least_square_a_exp_bx
|
4
8
|
|
5
9
|
== Version 0.0.2
|
6
|
-
|
7
|
-
|
10
|
+
* Change indent style from tab char to two spaces.
|
11
|
+
* Add lib/malge/leastsquare.rb for Malge::LeastSquare::least_square_1st_degree
|
8
12
|
|
9
13
|
== Version 0.0.1
|
data/Gemfile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/lib/malge.rb
CHANGED
data/lib/malge/leastsquare.rb
CHANGED
@@ -1,18 +1,22 @@
|
|
1
1
|
#! /usr/bin/env ruby
|
2
|
-
#
|
2
|
+
#coding: utf-8
|
3
3
|
|
4
4
|
#
|
5
5
|
#
|
6
6
|
#
|
7
7
|
module Malge::LeastSquare
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
8
|
+
|
9
|
+
class UnableCalculationError < Exception; end
|
10
|
+
|
11
|
+
|
12
|
+
#Return values of [a_0, a_1] in y = a_0 x^0 + a_1 x^1.
|
13
|
+
#Argument 'data_pairs' should be Array of Array's. For example,
|
14
|
+
# [
|
15
|
+
# [ 1.0, 2.0],
|
16
|
+
# [ 2.0, 4.0],
|
17
|
+
# [ 3.0, 6.0],
|
18
|
+
# ]
|
19
|
+
#Note that not [a, b] in y = ax + b.
|
16
20
|
def self.least_square_1st_degree(data_pairs)
|
17
21
|
#pp data_pairs
|
18
22
|
a = 0.0 #x^2
|
@@ -34,5 +38,57 @@ module Malge::LeastSquare
|
|
34
38
|
a_0 = (a*e - c*d) / (n*a - c**2)
|
35
39
|
[a_0, a_1]
|
36
40
|
end
|
41
|
+
|
42
|
+
#Return variance when fitted to y = a_0 x^0 + a_1 x^1.
|
43
|
+
#Argument 'data_pairs' should be Array of Array's. For example,
|
44
|
+
# [
|
45
|
+
# [ 1.0, 2.0],
|
46
|
+
# [ 2.0, 4.0],
|
47
|
+
# [ 3.0, 6.0],
|
48
|
+
# ]
|
49
|
+
def self.variance_1st_degree(data_pairs)
|
50
|
+
coefficients = self.least_square_1st_degree(data_pairs)
|
51
|
+
data_pairs.inject(0.0) do |sum, pair|
|
52
|
+
#pp pair
|
53
|
+
#pp sum
|
54
|
+
x = pair[0]
|
55
|
+
y = pair[1]
|
56
|
+
f_x = coefficients[0] + coefficients[1] * x
|
57
|
+
sum += (y - f_x)**2
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
#Return fitted values of [a, b] in a e^{bx} from 2 point data_pairs;
|
62
|
+
# [[x0, y0], [x1, y1]]
|
63
|
+
#Argument 'data_pairs' should have two items.
|
64
|
+
#Raise if including the same data.
|
65
|
+
def self.a_exp_bx(data_pairs)
|
66
|
+
raise UnableCalculationError if data_pairs[0][1] == data_pairs[1][1]
|
67
|
+
raise UnableCalculationError if data_pairs[0][0] == data_pairs[1][0]
|
68
|
+
matrix = data_pairs.map { |pair| [1.0, pair[0]] }
|
69
|
+
values = data_pairs.map { |pair| Math::log pair[1] }
|
70
|
+
coefficients = Malge::SimultaneousEquations.cramer( matrix, values )
|
71
|
+
coefficients[0] = Math::exp coefficients[0]
|
72
|
+
coefficients
|
73
|
+
end
|
74
|
+
|
75
|
+
#Return fitted values of [a, b] in a e^{bx} from more than two data points.
|
76
|
+
#Argument 'data_pairs' should be Array of Array's. For example,
|
77
|
+
# [
|
78
|
+
# [ 1.0, 2.0],
|
79
|
+
# [ 2.0, 4.0],
|
80
|
+
# [ 3.0, 6.0],
|
81
|
+
# ]
|
82
|
+
def self.least_square_a_exp_bx(data_pairs)
|
83
|
+
data_pairs.each do |pair|
|
84
|
+
raise UnableCalculationError if pair[1] == 0.0
|
85
|
+
end
|
86
|
+
data_pairs = data_pairs.map do |pair|
|
87
|
+
[pair[0], Math::log(pair[1])]
|
88
|
+
end
|
89
|
+
coefficients = self.least_square_1st_degree(data_pairs)
|
90
|
+
[Math::exp(coefficients[0]), coefficients[1]]
|
91
|
+
end
|
92
|
+
|
37
93
|
end
|
38
94
|
|
data/malge.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "malge"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["ippei94da"]
|
12
|
-
s.date = "2013-02-
|
12
|
+
s.date = "2013-02-08"
|
13
13
|
s.description = "Mathematical library to deal with basic problems in algebla.\n "
|
14
14
|
s.email = "ippei94da@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -48,20 +48,17 @@ Gem::Specification.new do |s|
|
|
48
48
|
s.add_development_dependency(%q<bundler>, ["~> 1.2.2"])
|
49
49
|
s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
|
50
50
|
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
51
|
-
s.add_development_dependency(%q<psych>, [">= 0"])
|
52
51
|
else
|
53
52
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
54
53
|
s.add_dependency(%q<bundler>, ["~> 1.2.2"])
|
55
54
|
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
56
55
|
s.add_dependency(%q<simplecov>, [">= 0"])
|
57
|
-
s.add_dependency(%q<psych>, [">= 0"])
|
58
56
|
end
|
59
57
|
else
|
60
58
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
61
59
|
s.add_dependency(%q<bundler>, ["~> 1.2.2"])
|
62
60
|
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
63
61
|
s.add_dependency(%q<simplecov>, [">= 0"])
|
64
|
-
s.add_dependency(%q<psych>, [">= 0"])
|
65
62
|
end
|
66
63
|
end
|
67
64
|
|
data/test/test_leastsquare.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#! /usr/bin/env ruby
|
2
|
-
#
|
2
|
+
#coding: utf-8
|
3
3
|
|
4
4
|
require "test/unit"
|
5
5
|
require "malge.rb"
|
@@ -8,7 +8,7 @@ TOLERANCE=1.0e-10
|
|
8
8
|
|
9
9
|
class TC_Malge < Test::Unit::TestCase
|
10
10
|
#def setup
|
11
|
-
#
|
11
|
+
# @k = Malge.new
|
12
12
|
#end
|
13
13
|
|
14
14
|
def test_least_square_1st_degree
|
@@ -36,7 +36,7 @@ class TC_Malge < Test::Unit::TestCase
|
|
36
36
|
[ 9.000, 6.645],
|
37
37
|
[ 10.000, 7.962],
|
38
38
|
]
|
39
|
-
#should be below, confirmed by Excel
|
39
|
+
#should be below, confirmed by Excel.
|
40
40
|
a0 = -2.0787619047619
|
41
41
|
a1 = 1.00077142857143
|
42
42
|
|
@@ -45,5 +45,90 @@ class TC_Malge < Test::Unit::TestCase
|
|
45
45
|
assert_in_delta(a0, results[0], TOLERANCE)
|
46
46
|
assert_in_delta(a1, results[1], TOLERANCE)
|
47
47
|
end
|
48
|
+
|
49
|
+
def test_variance_1st_degree
|
50
|
+
data_pairs = [
|
51
|
+
[0.0, -1.0],
|
52
|
+
[1.0, 2.0],
|
53
|
+
[2.0, 3.0],
|
54
|
+
[3.0, 2.0],
|
55
|
+
]
|
56
|
+
assert_equal( 4.0, Malge::LeastSquare::variance_1st_degree(data_pairs))
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_a_exp_bx
|
60
|
+
data_pairs = [
|
61
|
+
[0.0, 4.0],
|
62
|
+
[1.0, 2.0],
|
63
|
+
]
|
64
|
+
#y = a e^{bx}
|
65
|
+
#should be
|
66
|
+
#y = 4.0 exp^{- log_e 2 x}
|
67
|
+
corrects = [ 4.0, - Math::log(2.0)]
|
68
|
+
assert_equal(corrects, Malge::LeastSquare.a_exp_bx(data_pairs))
|
69
|
+
|
70
|
+
data_pairs = [
|
71
|
+
[0.0, 1.0],
|
72
|
+
[1.0, 4.0],
|
73
|
+
]
|
74
|
+
#y = a e^{bx}
|
75
|
+
#should be
|
76
|
+
#y = 4.0 exp^{- log_e 2 x}
|
77
|
+
corrects = [ 1.0, 2.0 * Math::log(2.0)]
|
78
|
+
assert_equal(corrects, Malge::LeastSquare.a_exp_bx(data_pairs))
|
79
|
+
|
80
|
+
data_pairs = [
|
81
|
+
[0.0, 1.0],
|
82
|
+
[1.0, 1.0],
|
83
|
+
]
|
84
|
+
assert_raise(Malge::LeastSquare::UnableCalculationError){
|
85
|
+
Malge::LeastSquare.a_exp_bx(data_pairs)
|
86
|
+
}
|
87
|
+
|
88
|
+
data_pairs = [
|
89
|
+
[1.0, 1.0],
|
90
|
+
[1.0, 2.0],
|
91
|
+
]
|
92
|
+
assert_raise(Malge::LeastSquare::UnableCalculationError){
|
93
|
+
Malge::LeastSquare.a_exp_bx(data_pairs)
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_least_square_a_exp_bx
|
98
|
+
data_pairs = [
|
99
|
+
[0.0, 4.0],
|
100
|
+
[1.0, 2.0],
|
101
|
+
[2.0, 1.0],
|
102
|
+
]
|
103
|
+
#y = a e^{bx}
|
104
|
+
#should be
|
105
|
+
#y = 4.0 exp^{- log_e 2 x}
|
106
|
+
corrects = [ 4.0, - Math::log(2.0)]
|
107
|
+
results = Malge::LeastSquare.least_square_a_exp_bx(data_pairs)
|
108
|
+
assert_equal(2 , results.size)
|
109
|
+
assert_in_delta(corrects[0], results[0], TOLERANCE)
|
110
|
+
assert_in_delta(corrects[1], results[1], TOLERANCE)
|
111
|
+
|
112
|
+
data_pairs = [
|
113
|
+
[0.0, 1.0],
|
114
|
+
[1.0, 4.0],
|
115
|
+
[2.0, 16.0],
|
116
|
+
]
|
117
|
+
corrects = [ 1.0, 2.0 * Math::log(2.0)]
|
118
|
+
results = Malge::LeastSquare.least_square_a_exp_bx(data_pairs)
|
119
|
+
assert_equal(2 , results.size)
|
120
|
+
assert_in_delta(corrects[0], results[0], TOLERANCE)
|
121
|
+
assert_in_delta(corrects[1], results[1], TOLERANCE)
|
122
|
+
|
123
|
+
data_pairs = [
|
124
|
+
[0.0, 16.0],
|
125
|
+
[1.0, 4.0],
|
126
|
+
[2.0, 0.0],
|
127
|
+
]
|
128
|
+
assert_raise(Malge::LeastSquare::UnableCalculationError) do
|
129
|
+
Malge::LeastSquare.least_square_a_exp_bx(data_pairs)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
48
133
|
end
|
49
134
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: malge
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rdoc
|
16
|
-
requirement: &
|
16
|
+
requirement: &86230380 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.12'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *86230380
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &86229650 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 1.2.2
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *86229650
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: jeweler
|
38
|
-
requirement: &
|
38
|
+
requirement: &86229110 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.8.3
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *86229110
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: simplecov
|
49
|
-
requirement: &
|
49
|
+
requirement: &86228170 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,18 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
58
|
-
- !ruby/object:Gem::Dependency
|
59
|
-
name: psych
|
60
|
-
requirement: &85724910 !ruby/object:Gem::Requirement
|
61
|
-
none: false
|
62
|
-
requirements:
|
63
|
-
- - ! '>='
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
version: '0'
|
66
|
-
type: :development
|
67
|
-
prerelease: false
|
68
|
-
version_requirements: *85724910
|
57
|
+
version_requirements: *86228170
|
69
58
|
description: ! "Mathematical library to deal with basic problems in algebla.\n "
|
70
59
|
email: ippei94da@gmail.com
|
71
60
|
executables: []
|
@@ -105,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
105
94
|
version: '0'
|
106
95
|
segments:
|
107
96
|
- 0
|
108
|
-
hash:
|
97
|
+
hash: -142397791
|
109
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
99
|
none: false
|
111
100
|
requirements:
|