malge 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/lib/malge/simultaneousequations.rb +62 -0
- data/lib/malge.rb +2 -0
- data/test/helper.rb +17 -0
- data/test/test_simultaneousequations.rb +103 -0
- metadata +124 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rdoc", "~> 3.12"
|
10
|
+
gem "bundler", "~> 1.1.3"
|
11
|
+
gem "jeweler", "~> 1.8.3"
|
12
|
+
gem "simplecov", ">= 0"
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 ippei94da
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= malge
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to malge
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
9
|
+
* Fork the project.
|
10
|
+
* Start a feature/bugfix branch.
|
11
|
+
* Commit and push until you are happy with your contribution.
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2012 ippei94da. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "malge"
|
18
|
+
gem.homepage = "http://github.com/ippei94da/malge"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Math ALGEbra library.}
|
21
|
+
gem.description = %Q{Mathematical library to deal with basic problems in algebla.
|
22
|
+
}
|
23
|
+
gem.email = "ippei94da@gmail.com"
|
24
|
+
gem.authors = ["ippei94da"]
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
#require 'rcov/rcovtask'
|
37
|
+
#Rcov::RcovTask.new do |test|
|
38
|
+
# test.libs << 'test'
|
39
|
+
# test.pattern = 'test/**/test_*.rb'
|
40
|
+
# test.verbose = true
|
41
|
+
# test.rcov_opts << '--exclude "gems/*"'
|
42
|
+
#end
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rdoc/task'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "malge #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'matrix'
|
2
|
+
|
3
|
+
# Solve simultatneous equations with Cramer\'s formula.
|
4
|
+
#
|
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)
|
9
|
+
# command
|
10
|
+
# % #{$0} a_11 ... a_1n ... a_n1 ... a_nn c_1 ... c_n
|
11
|
+
#
|
12
|
+
|
13
|
+
class Matrix
|
14
|
+
def []=(i,j,x)
|
15
|
+
@rows[i][j]=x
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Vector
|
20
|
+
def []=(i,x)
|
21
|
+
@elements[i]=x
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# 連立1次方程式を表現するクラス。
|
26
|
+
class SimultaneousEquations
|
27
|
+
|
28
|
+
# Cramer の公式で解くクラスメソッド。
|
29
|
+
# matrix は2重配列で与える。
|
30
|
+
# 返されるのは、解の値を格納した配列。
|
31
|
+
def self.cramer( matrix, values )
|
32
|
+
matrix = Matrix.rows( matrix )
|
33
|
+
raise "Matrix is not squre: #{matrix.row_size} x #{matrix.column_size}." if ( ! matrix.square? )
|
34
|
+
raise "Matrix size (#{matrix.row_size}) and values size (#{values.size}) mismatched." if ( matrix.row_size != values.size )
|
35
|
+
raise "Matrix is not regular. Degree of freedom is #{matrix.column_size - matrix.rank}.\n" if ( ! matrix.regular? )
|
36
|
+
|
37
|
+
results = Array.new
|
38
|
+
n = matrix.column_size
|
39
|
+
n.times do |i|
|
40
|
+
tmp = Marshal.load( Marshal.dump( matrix ) )
|
41
|
+
n.times do |j|
|
42
|
+
tmp[ j, i ] = values[ j ]
|
43
|
+
end
|
44
|
+
results[i] = tmp.determinant / ( matrix.determinant )
|
45
|
+
end
|
46
|
+
results
|
47
|
+
end
|
48
|
+
|
49
|
+
# matrix は左辺の行列、values は右辺の値の配列
|
50
|
+
# ともに配列で与える。
|
51
|
+
def initialize( matrix, values )
|
52
|
+
@matrix = matrix
|
53
|
+
@values = values
|
54
|
+
end
|
55
|
+
|
56
|
+
# Cramer's formula で解く。
|
57
|
+
def cramer
|
58
|
+
SimultaneousEquations.cramer( @matrix, @values )
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
data/lib/malge.rb
ADDED
data/test/helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
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
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
require 'malge'
|
15
|
+
|
16
|
+
class Test::Unit::TestCase
|
17
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
|
3
|
+
require 'helper'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'malge/simultaneousequations.rb'
|
6
|
+
|
7
|
+
class TC_SimultaneousEquations < Test::Unit::TestCase
|
8
|
+
$tolerance = 10.0**(-10)
|
9
|
+
def setup
|
10
|
+
@se00 = SimultaneousEquations.new(
|
11
|
+
[ [ 1.0, 2.0, -3.0 ],
|
12
|
+
[ 2.0, 1.0, -2.0 ],
|
13
|
+
[ 3.0, 2.0, 1.0 ] ],
|
14
|
+
[ 1.0, 2.0, -1.0]
|
15
|
+
)
|
16
|
+
|
17
|
+
@se01 = SimultaneousEquations.new(
|
18
|
+
[ [ 1.0, 1.0, 1.0 ],
|
19
|
+
[ 0.0, 1.0, 1.0 ],
|
20
|
+
[ 0.0, 0.0, 1.0 ] ],
|
21
|
+
[ 3.0, 2.0, 1.0]
|
22
|
+
)
|
23
|
+
|
24
|
+
#実数の行列を前提としても良さそうだ。
|
25
|
+
#@se02 = SimultaneousEquations.new(
|
26
|
+
# [ [ 1, 2, -3 ],
|
27
|
+
# [ 2, 1, -2 ],
|
28
|
+
# [ 3, 2, 1 ] ],
|
29
|
+
# [ 1, 2, -1]
|
30
|
+
#)
|
31
|
+
|
32
|
+
@se03 = SimultaneousEquations.new(
|
33
|
+
[ [ 1, 1, 1 ],
|
34
|
+
[ 0, 1, 1 ],
|
35
|
+
[ 0, 0, 1 ] ],
|
36
|
+
[ 3, 2, 1]
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_self_cramer
|
41
|
+
t = SimultaneousEquations.cramer(
|
42
|
+
[ [ 1.0, 2.0, -3.0 ],
|
43
|
+
[ 2.0, 1.0, -2.0 ],
|
44
|
+
[ 3.0, 2.0, 1.0 ] ],
|
45
|
+
[ 1.0, 2.0, -1.0]
|
46
|
+
)
|
47
|
+
assert_in_delta( 0.714285714285714, t[0], $tolerance )
|
48
|
+
assert_in_delta( -1.142857142857143, t[1], $tolerance )
|
49
|
+
assert_in_delta( -0.857142857142857, t[2], $tolerance )
|
50
|
+
|
51
|
+
t = SimultaneousEquations.cramer(
|
52
|
+
[ [ 1.0, 1.0, 1.0 ],
|
53
|
+
[ 0.0, 1.0, 1.0 ],
|
54
|
+
[ 0.0, 0.0, 1.0 ] ],
|
55
|
+
[ 3.0, 2.0, 1.0 ]
|
56
|
+
)
|
57
|
+
assert_in_delta( 1.0, t[0], $tolerance )
|
58
|
+
assert_in_delta( 1.0, t[1], $tolerance )
|
59
|
+
assert_in_delta( 1.0, t[2], $tolerance )
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_cramer
|
63
|
+
t = @se00.cramer
|
64
|
+
assert_in_delta( 0.714285714285714, t[0], $tolerance )
|
65
|
+
assert_in_delta( -1.142857142857143, t[1], $tolerance )
|
66
|
+
assert_in_delta( -0.857142857142857, t[2], $tolerance )
|
67
|
+
|
68
|
+
t = @se01.cramer
|
69
|
+
assert_in_delta( 1.0, t[0], $tolerance )
|
70
|
+
assert_in_delta( 1.0, t[1], $tolerance )
|
71
|
+
assert_in_delta( 1.0, t[2], $tolerance )
|
72
|
+
|
73
|
+
#t = @se02.cramer
|
74
|
+
#assert_in_delta( 0.714285714285714, t[0], $tolerance )
|
75
|
+
#assert_in_delta( -1.142857142857143, t[1], $tolerance )
|
76
|
+
#assert_in_delta( -0.857142857142857, t[2], $tolerance )
|
77
|
+
|
78
|
+
t = @se03.cramer
|
79
|
+
assert_in_delta( 1.0, t[0], $tolerance )
|
80
|
+
assert_in_delta( 1.0, t[1], $tolerance )
|
81
|
+
assert_in_delta( 1.0, t[2], $tolerance )
|
82
|
+
|
83
|
+
#assert_raise( ExceptionForMatrix ){
|
84
|
+
# SimultaneousEquations.cramer(
|
85
|
+
# [ [ 1.0, 2.0, -3.0 ],
|
86
|
+
# [ 2.0, 1.0 ],
|
87
|
+
# [ 3.0, 2.0, 1.0 ] ],
|
88
|
+
# [ 1.0, 2.0, -1.0]
|
89
|
+
# )
|
90
|
+
#}
|
91
|
+
|
92
|
+
assert_raise( RuntimeError ){
|
93
|
+
SimultaneousEquations.cramer(
|
94
|
+
[ [ 1.0, 2.0, -3.0 ],
|
95
|
+
[ 2.0, 1.0, -2.0 ],
|
96
|
+
[ 3.0, 2.0, 1.0 ] ],
|
97
|
+
[ 1.0, 2.0]
|
98
|
+
)
|
99
|
+
}
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: malge
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- ippei94da
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rdoc
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.12'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.12'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.1.3
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.1.3
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jeweler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.8.3
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.8.3
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: simplecov
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: ! "Mathematical library to deal with basic problems in algebla.\n "
|
79
|
+
email: ippei94da@gmail.com
|
80
|
+
executables: []
|
81
|
+
extensions: []
|
82
|
+
extra_rdoc_files:
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.rdoc
|
85
|
+
files:
|
86
|
+
- .document
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.rdoc
|
90
|
+
- Rakefile
|
91
|
+
- VERSION
|
92
|
+
- lib/malge.rb
|
93
|
+
- lib/malge/simultaneousequations.rb
|
94
|
+
- test/helper.rb
|
95
|
+
- test/test_simultaneousequations.rb
|
96
|
+
homepage: http://github.com/ippei94da/malge
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
hash: 920976495
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 1.8.21
|
121
|
+
signing_key:
|
122
|
+
specification_version: 3
|
123
|
+
summary: Math ALGEbra library.
|
124
|
+
test_files: []
|