correlation 0.0.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/gemspec +20 -0
- data/lib/correlation.rb +45 -0
- data/rakefile +96 -0
- data/test/test_correlation.rb +20 -0
- metadata +55 -0
data/gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
spec = Gem::Specification.new do |s|
|
4
|
+
s.name = "correlation"
|
5
|
+
s.version = "0.0.1"
|
6
|
+
s.author = "Ralf M�ller"
|
7
|
+
s.email = "stark.dreamdetective@gmail.com"
|
8
|
+
s.homepage = "http://extcsv.rubyforge.org/correlation"
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.summary = "Extension of GSL: Compute correaltion of 2 datasets"
|
11
|
+
candidates = Dir.glob("lib/*.rb") + [ "rakefile", "gemspec"]
|
12
|
+
s.files = candidates.delete_if do |item|
|
13
|
+
item.include?(".hg") || item.include?("doc")
|
14
|
+
end
|
15
|
+
s.require_path = "lib"
|
16
|
+
s.test_files = Dir.glob("test/test_*.rb")
|
17
|
+
s.has_rdoc = true
|
18
|
+
end
|
19
|
+
|
20
|
+
# vim:ft=ruby
|
data/lib/correlation.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require "rbgsl"
|
2
|
+
|
3
|
+
# This Extension of the GNU Scientific Library bindings by Yoshiki Tsunesada
|
4
|
+
# (http://rb-gsl.rubyforge.org) provides the computation of the correlation of two
|
5
|
+
# GSL::Vectors. It is implemented as a method of a GSL::Vector for most easy
|
6
|
+
# usage.
|
7
|
+
class GSL::Vector
|
8
|
+
|
9
|
+
# Follow the usual definition, e.g. from Sheriff and Geldart "Exploitation
|
10
|
+
# Seismology", p. 289: cor(v,w)(i) = sum_over_k (v[k]*w[k+i])
|
11
|
+
#
|
12
|
+
# This means, that
|
13
|
+
# * for positive values of i, w is shifted to the left, i.e. in the direction of smaller indizees of v
|
14
|
+
# * for negative i, w is shifted to the right, i.e. in the direction of larger indizees of v
|
15
|
+
def correlation(other)
|
16
|
+
unless size == other.size
|
17
|
+
warn "Vectors/Datasets must have the same size."
|
18
|
+
raise
|
19
|
+
end
|
20
|
+
|
21
|
+
# predefine result vector
|
22
|
+
correlation = GSL::Vector.alloc(1)
|
23
|
+
correlation.shift
|
24
|
+
|
25
|
+
# Alternate definition, which is actually the opposite direction of the definition
|
26
|
+
# (0...size).each {|i|
|
27
|
+
# correlation << self[0..i]*other[-i-1..-1].col
|
28
|
+
# }
|
29
|
+
# (1...size).each {|i|
|
30
|
+
# correlation << self[i..size-1]*other[0...size-i].col
|
31
|
+
# }
|
32
|
+
|
33
|
+
(0...size).each {|i|
|
34
|
+
correlation << self[-i-1..-1]*other[0..i].col
|
35
|
+
}
|
36
|
+
(1...size).each {|i|
|
37
|
+
correlation << self[0...size-i]*other[i..size-1].col
|
38
|
+
}
|
39
|
+
|
40
|
+
[GSL::Vector.linspace(-size+1, size-1, 2*size-1) , correlation]
|
41
|
+
end
|
42
|
+
def autocorrelation
|
43
|
+
correlation(self)
|
44
|
+
end
|
45
|
+
end
|
data/rakefile
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
begin
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
rescue Exception
|
5
|
+
nil
|
6
|
+
end
|
7
|
+
require 'rake/clean'
|
8
|
+
require 'rake/testtask'
|
9
|
+
require 'rake/rdoctask'
|
10
|
+
|
11
|
+
SPEC = eval(File.open("gemspec","r").read)
|
12
|
+
|
13
|
+
def filename_to_sym(filename)
|
14
|
+
File.basename(filename,File.extname(filename)).to_sym
|
15
|
+
end
|
16
|
+
|
17
|
+
# ====================================================================
|
18
|
+
# TEST TASKS
|
19
|
+
test_tasks = {
|
20
|
+
:test_all => ["Run all tests"],
|
21
|
+
}
|
22
|
+
# Syntax checkning task
|
23
|
+
task :test_syn do
|
24
|
+
Dir.glob("**/*.rb").each {|file|
|
25
|
+
printf "Checking Syntax of #{file} ..."
|
26
|
+
system("ruby -c #{file}")
|
27
|
+
}
|
28
|
+
end
|
29
|
+
# Test tasks for each test file
|
30
|
+
SPEC.test_files.each do |test_file|
|
31
|
+
next unless File.extname(test_file) == ".rb"
|
32
|
+
Rake::TestTask.new(filename_to_sym(test_file)) do |t|
|
33
|
+
test_tasks[:test_all] << filename_to_sym(test_file)
|
34
|
+
t.test_files = FileList[test_file]
|
35
|
+
t.warning = false
|
36
|
+
t.verbose = true
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Test Tasks for groups of test files
|
41
|
+
test_tasks.each do |k,v|
|
42
|
+
desc v[0]
|
43
|
+
task k => v[1..-1]
|
44
|
+
end
|
45
|
+
# ====================================================================
|
46
|
+
# Create a task that will package the software into distributable
|
47
|
+
# tar, zip and gem files.
|
48
|
+
if ! defined?(Gem)
|
49
|
+
puts "Package Target requires RubyGEMs"
|
50
|
+
else
|
51
|
+
package_task = Rake::GemPackageTask.new(SPEC) do |pkg|
|
52
|
+
pkg.need_zip = false
|
53
|
+
pkg.need_tar = false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
# ====================================================================
|
57
|
+
desc "Install the Library with docs"
|
58
|
+
task :install => [:repackage] do
|
59
|
+
command = "gem install pkg/#{SPEC.name}-#{SPEC.version}.gem"
|
60
|
+
puts command
|
61
|
+
system(command)
|
62
|
+
end
|
63
|
+
task :smallInstall => [:repackage] do
|
64
|
+
command = "gem install pkg/#{SPEC.name}-#{SPEC.version}.gem --no-ri --no-rdoc"
|
65
|
+
puts command
|
66
|
+
system(command)
|
67
|
+
end
|
68
|
+
# ====================================================================
|
69
|
+
# Create a task to build the RDOC documentation tree.
|
70
|
+
Rake::RDocTask.new("rdoc") { |rdoc|
|
71
|
+
rdoc.rdoc_dir = 'rdoc'
|
72
|
+
rdoc.title = "Correlation Function as GSL Extension"
|
73
|
+
rdoc.options << '-ad' << '--line-numbers' << '--inline-source'
|
74
|
+
rdoc.rdoc_files.include('lib/**/*.rb', 'doc/**/*.rdoc')
|
75
|
+
}
|
76
|
+
############################################################
|
77
|
+
files = Dir.glob("{lib,test}/*.rb")
|
78
|
+
visual_mode = (ENV["vimode"].nil?) ? '-p' : ENV["vimode"]
|
79
|
+
desc "Edit texfiles #{files.join(", ")}"
|
80
|
+
task :edit do
|
81
|
+
com = (File.exist?("Session.vim"))\
|
82
|
+
? 'vim -S'\
|
83
|
+
: "vim #{files.join(" ")} rakefile gemspec #{visual_mode}"
|
84
|
+
puts com
|
85
|
+
system(com)
|
86
|
+
end
|
87
|
+
############################################################
|
88
|
+
desc "renew the tags file"
|
89
|
+
task :tags do
|
90
|
+
com = "rtags --vi -f tags lib/*.rb"
|
91
|
+
system(com)
|
92
|
+
end
|
93
|
+
#
|
94
|
+
#
|
95
|
+
# vim:ft=ruby
|
96
|
+
#
|
@@ -0,0 +1,20 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),"..","lib")
|
2
|
+
require 'test/unit'
|
3
|
+
require 'correlation'
|
4
|
+
require 'pp'
|
5
|
+
|
6
|
+
class TestCorrelation < Test::Unit::TestCase
|
7
|
+
def test_simple
|
8
|
+
v = GSL::Vector.alloc([0,1,0,0,0,0,0,0,0,0])
|
9
|
+
w = GSL::Vector.alloc([0,0,0,0,0,0,0,0,1,0])
|
10
|
+
|
11
|
+
#GSL::graph(v.correlation(w), "-X 'Correlation v with w' -C -g 3")
|
12
|
+
#GSL::graph(v.autocorrelation,"-X 'Autocorrelation v' -C -g 3")
|
13
|
+
|
14
|
+
assert_equal(1, v.correlation(w)[1][-3])
|
15
|
+
assert_equal(0, v.correlation(w)[1][9])
|
16
|
+
|
17
|
+
assert_equal(1,v.autocorrelation[1][v.autocorrelation[0].where {|i| i == 0}[0]])
|
18
|
+
assert_equal(0,v.autocorrelation[1][v.autocorrelation[0].where {|i| i != 0}[0]])
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: correlation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Ralf M\xFCller"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-24 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: stark.dreamdetective@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/correlation.rb
|
26
|
+
- rakefile
|
27
|
+
- gemspec
|
28
|
+
has_rdoc: true
|
29
|
+
homepage: http://extcsv.rubyforge.org/correlation
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.2.0
|
51
|
+
signing_key:
|
52
|
+
specification_version: 2
|
53
|
+
summary: "Extension of GSL: Compute correaltion of 2 datasets"
|
54
|
+
test_files:
|
55
|
+
- test/test_correlation.rb
|