apate 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.
- checksums.yaml +15 -0
- data/.gitignore +14 -0
- data/.travis.yml +4 -0
- data/.versions.conf +4 -0
- data/Gemfile +4 -0
- data/GuardFile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +15 -0
- data/apate.gemspec +28 -0
- data/lib/apate.rb +5 -0
- data/lib/apate/all_methods.rb +8 -0
- data/lib/apate/array_methods.rb +48 -0
- data/lib/apate/basic_methods.rb +35 -0
- data/lib/apate/matrix_methods.rb +167 -0
- data/lib/apate/multi_set_methods.rb +15 -0
- data/lib/apate/version.rb +3 -0
- data/spec/lib/apate/array_methods_spec.rb +69 -0
- data/spec/lib/apate/basic_methods_spec.rb +64 -0
- data/spec/lib/apate/matrix_methods_spec.rb +78 -0
- data/spec/lib/apate/multi_set_methods_spec.rb +31 -0
- data/spec/lib/apate_spec.rb +6 -0
- data/spec/spec_helper.rb +5 -0
- metadata +156 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZDdkMGMwYTcxMjc5NTgyN2M3MGExYTZlNjAyM2MzODQ0YjFlZGMwNA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ODUxOWNlMDI5NjkwZjhlYmVhNzEwZmRlNDc2ODczYWJmZDFmOWE0OQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
OGQyODIzMzEwNWVlMjIzNTY2NzkxY2I0MTFlYjM2YjY1ODFiMWE4M2E0NmNl
|
10
|
+
MzQyMjhkZjRkYWE4NzdjOTJiYmY1Y2YzMTk4NmRkNTA4NmE4NTc5MDU1MDZh
|
11
|
+
NmQ1Mjg0NTIyMDk4N2RlNjA4N2YyYjk1NGExOTIxYzUzZGIwN2I=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YmI0NzhhZGFkN2FhOTFjZmU5ZTVhMmIwMWZhZWEyZjdjZDc1NjIzNWYxODFi
|
14
|
+
ZmM2NjQzY2NmOGIwNGJkNWJiNWZkM2NlZGFlYmY4ZWZlMTdjM2IyNGI1ZmJj
|
15
|
+
MWM0MmI4NTllZWIzNjlhNDU3ZDQ2MzBhMzgwNDk0YWFlYjgxZGQ=
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/.versions.conf
ADDED
data/Gemfile
ADDED
data/GuardFile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Albin Svensson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Apate
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'apate'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install apate
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# compute variance
|
25
|
+
Apate.variance arr
|
26
|
+
# compute average of an array
|
27
|
+
Apate.average arr
|
28
|
+
```
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it ( https://github.com/[my-github-username]/apate/fork )
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
task :test => :spec
|
8
|
+
|
9
|
+
task :console do
|
10
|
+
require 'irb'
|
11
|
+
require 'irb/completion'
|
12
|
+
require 'apate'
|
13
|
+
ARGV.clear
|
14
|
+
IRB.start
|
15
|
+
end
|
data/apate.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'apate/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "apate"
|
8
|
+
spec.version = Apate::VERSION
|
9
|
+
spec.authors = ["Albin Svensson"]
|
10
|
+
spec.email = ["albin.svensson@trialbee.com"]
|
11
|
+
spec.summary = %q{Provide tools for statistics computation.}
|
12
|
+
spec.description = %q{Provide tools to be used when making statistical computations.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
24
|
+
spec.add_development_dependency 'simplecov', '~> 0.7'
|
25
|
+
spec.add_development_dependency 'guard', '~> 2.12'
|
26
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.5'
|
27
|
+
|
28
|
+
end
|
data/lib/apate.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module Apate
|
2
|
+
|
3
|
+
def self.subtract(element, value)
|
4
|
+
self.element_wise_computing(:-, element, value)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.add(element, value)
|
8
|
+
self.element_wise_computing(:+, element, value)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.multiply(element, value)
|
12
|
+
self.element_wise_computing(:*, element, value)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.divide(element, value)
|
16
|
+
self.element_wise_computing(:/, element, value.to_f)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.pow(element, value)
|
20
|
+
self.element_wise_computing(:**, element, value.to_f)
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def self.scalar_product x, y
|
25
|
+
return unless x.length == y.length
|
26
|
+
value = 0
|
27
|
+
|
28
|
+
x.each_with_index do |xi, i|
|
29
|
+
value += xi*y[i]
|
30
|
+
end
|
31
|
+
|
32
|
+
value
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def self.element_wise_computing method, elements, value
|
39
|
+
if elements.kind_of?(Array)
|
40
|
+
elements.map do |e|
|
41
|
+
self.element_wise_computing(method, e, value)
|
42
|
+
end
|
43
|
+
else
|
44
|
+
elements.send(method, value)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Apate
|
2
|
+
|
3
|
+
def self.average arr
|
4
|
+
self.sum(arr).to_f / arr.length
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.sum arr
|
8
|
+
arr.inject { |sum, el| sum + el }
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.variance arr
|
12
|
+
m = self.average arr
|
13
|
+
|
14
|
+
arr = self.subtract(arr, m)
|
15
|
+
self.scalar_product(arr, arr).to_f/(arr.length-1)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.median arr
|
19
|
+
sorted_arr = arr.sort
|
20
|
+
length = arr.length
|
21
|
+
if length % 2 == 0
|
22
|
+
self.average(sorted_arr[length/2-1..length/2])
|
23
|
+
else
|
24
|
+
sorted_arr[(length-1)/2]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
def self.mode arr
|
30
|
+
freq = arr.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
|
31
|
+
arr.max_by { |v| freq[v] }
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
module Apate
|
2
|
+
|
3
|
+
def self.matrix_multiply(a, b)
|
4
|
+
am = Matrix.new(a)
|
5
|
+
bm = Matrix.new(b)
|
6
|
+
|
7
|
+
(am*bm).data
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.matrix_add(a, b)
|
11
|
+
am = Matrix.new(a)
|
12
|
+
bm = Matrix.new(b)
|
13
|
+
|
14
|
+
(am+bm).data
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.invert_matrix(a)
|
18
|
+
am = Matrix.new(a)
|
19
|
+
am.invert.data
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
def self.determinant(a)
|
24
|
+
am = Matrix.new(a)
|
25
|
+
det = am.determinant
|
26
|
+
|
27
|
+
det
|
28
|
+
end
|
29
|
+
|
30
|
+
class Matrix
|
31
|
+
|
32
|
+
def self.create_empty(rows, cols)
|
33
|
+
arr = Array.new(rows).map { |_| Array.new(cols) }
|
34
|
+
Matrix.new(arr)
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize(arr)
|
38
|
+
@rows = arr.length
|
39
|
+
@cols = arr[0].length
|
40
|
+
@data = arr
|
41
|
+
end
|
42
|
+
|
43
|
+
def data
|
44
|
+
@data
|
45
|
+
end
|
46
|
+
|
47
|
+
def data=(d)
|
48
|
+
@data = d
|
49
|
+
end
|
50
|
+
|
51
|
+
def rows
|
52
|
+
@rows
|
53
|
+
end
|
54
|
+
|
55
|
+
def cols
|
56
|
+
@cols
|
57
|
+
end
|
58
|
+
|
59
|
+
def set(row, col, value)
|
60
|
+
return nil if row >= @rows || col >= cols
|
61
|
+
@data[row][col] = value
|
62
|
+
end
|
63
|
+
|
64
|
+
def get(row, col)
|
65
|
+
@data[row][col]
|
66
|
+
end
|
67
|
+
|
68
|
+
def get_columns
|
69
|
+
columns = Array.new(cols).map { |_| Array.new(rows) }
|
70
|
+
data.each_with_index do |row, row_i|
|
71
|
+
row.each_with_index do |e, col_i|
|
72
|
+
columns[col_i][row_i] = e
|
73
|
+
end
|
74
|
+
end
|
75
|
+
columns
|
76
|
+
end
|
77
|
+
|
78
|
+
def +(b)
|
79
|
+
c = Matrix.create_empty rows, cols
|
80
|
+
|
81
|
+
data.each_with_index do |row, row_i|
|
82
|
+
b.get_columns.each_with_index do |col, col_i|
|
83
|
+
c.set(row_i, col_i, get(row_i, col_i) + b.get(row_i, col_i))
|
84
|
+
end
|
85
|
+
end
|
86
|
+
c
|
87
|
+
end
|
88
|
+
|
89
|
+
def *(b)
|
90
|
+
c = Matrix.create_empty rows, b.cols
|
91
|
+
data.each_with_index do |row, row_i|
|
92
|
+
b.get_columns.each_with_index do |col, col_i|
|
93
|
+
c.set(row_i, col_i, Apate.scalar_product(row, col))
|
94
|
+
end
|
95
|
+
end
|
96
|
+
c
|
97
|
+
end
|
98
|
+
|
99
|
+
def determinant
|
100
|
+
fail 'Non square matrix' unless rows == cols
|
101
|
+
return data[0][0] if rows == 1
|
102
|
+
det = 0
|
103
|
+
|
104
|
+
data.each_with_index do |_, row_i|
|
105
|
+
cell = get(row_i, 0)
|
106
|
+
next if cell == 0
|
107
|
+
b = get_sub_matrix_without_row_and_col0(row_i)
|
108
|
+
b_det = b.determinant
|
109
|
+
det += ((-1)**row_i)*cell*b_det
|
110
|
+
end
|
111
|
+
det
|
112
|
+
end
|
113
|
+
|
114
|
+
def get_sub_matrix_without_row_and_col0(row)
|
115
|
+
b = Matrix.create_empty rows-1, cols-1
|
116
|
+
ranges = get_range_pair rows, row
|
117
|
+
if ranges[:first] && ranges[:second]
|
118
|
+
b.data = sub_matrix(1..cols-1, ranges[:first])
|
119
|
+
b.append_cols(sub_matrix(1..cols-1, ranges[:second]))
|
120
|
+
elsif ranges[:first]
|
121
|
+
b.data = sub_matrix(1..cols-1, ranges[:first])
|
122
|
+
elsif ranges[:second]
|
123
|
+
b.data = sub_matrix(1..cols-1, ranges[:second])
|
124
|
+
end
|
125
|
+
b
|
126
|
+
end
|
127
|
+
|
128
|
+
def sub_matrix(col_range, row_range)
|
129
|
+
data[col_range].map { |c| c[row_range] }
|
130
|
+
end
|
131
|
+
|
132
|
+
def invert
|
133
|
+
ai = Matrix.create_empty rows, cols
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
def print
|
138
|
+
data.each do |row|
|
139
|
+
puts "| #{row.join(' | ')} |\n"
|
140
|
+
puts '-'*cols*3
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def append_cols(append_data)
|
145
|
+
data.each_with_index do |row, i|
|
146
|
+
data[i] = row + append_data[i]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
private
|
151
|
+
|
152
|
+
def get_range_pair(max, index)
|
153
|
+
return if max == 0 || index >= max
|
154
|
+
pair = {first: nil, second: nil}
|
155
|
+
real_max = max-1
|
156
|
+
if index == 0
|
157
|
+
pair[:second] = (1..real_max)
|
158
|
+
elsif index == (real_max)
|
159
|
+
pair[:first] = (0..real_max-1)
|
160
|
+
else
|
161
|
+
pair[:first] = (0..index-1)
|
162
|
+
pair[:second] = (index+1..real_max)
|
163
|
+
end
|
164
|
+
pair
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Apate do
|
4
|
+
|
5
|
+
|
6
|
+
it 'should be able to multiply 2 arrays' do
|
7
|
+
res = Apate.scalar_product [3,2,1], [3,2,1]
|
8
|
+
expect(res).to eq(14)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should be able to subtract a value from an array' do
|
12
|
+
res = Apate.subtract [4,3,1], 1
|
13
|
+
expected = [3,2,0]
|
14
|
+
|
15
|
+
res.each_with_index do |r, i|
|
16
|
+
expect(r).to eq(expected[i])
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should be able to add a value ta an array' do
|
22
|
+
res = Apate.add [4,3,1], 1
|
23
|
+
expected = [5,4,2]
|
24
|
+
|
25
|
+
res.each_with_index do |r, i|
|
26
|
+
expect(r).to eq(expected[i])
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should be able to multiply an array with an scalar' do
|
32
|
+
res = Apate.multiply [4,3,1], 2
|
33
|
+
expected = [8,6,2]
|
34
|
+
|
35
|
+
res.each_with_index do |r, i|
|
36
|
+
expect(r).to eq(expected[i])
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should be able to divide an array by an scalar' do
|
41
|
+
res = Apate.divide [4,3,1], 2
|
42
|
+
expected = [2,1.5,0.5]
|
43
|
+
|
44
|
+
res.each_with_index do |r, i|
|
45
|
+
expect(r).to eq(expected[i])
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should be able to raise each element in an array to a power' do
|
50
|
+
res = Apate.pow [4,3,1], 2
|
51
|
+
expected = [16,9,1]
|
52
|
+
|
53
|
+
res.each_with_index do |r, i|
|
54
|
+
expect(r).to eq(expected[i])
|
55
|
+
end
|
56
|
+
|
57
|
+
res = Apate.pow [4,9,1], -1
|
58
|
+
expected = [1.0/4,1.0/9,1]
|
59
|
+
|
60
|
+
res.each_with_index do |r, i|
|
61
|
+
expect(r).to eq(expected[i])
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Apate do
|
4
|
+
|
5
|
+
it 'Should be able to compute the sum of an array' do
|
6
|
+
arr = [1,2,3,4,5]
|
7
|
+
res = Apate.sum(arr)
|
8
|
+
expect(res).to eq(15)
|
9
|
+
|
10
|
+
res = Apate.sum [3, 4]
|
11
|
+
expect(res).to eq(7)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'Should be able to compute the average of an array' do
|
15
|
+
arr = [1,2,3,4,5]
|
16
|
+
res = Apate.average(arr)
|
17
|
+
expect(res).to eq(3)
|
18
|
+
|
19
|
+
res = Apate.average [3, 4]
|
20
|
+
expect(res).to eq(3.5)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'Should be able to compute the median of an array' do
|
24
|
+
arr = [1,2,3,4,5]
|
25
|
+
res = Apate.median(arr)
|
26
|
+
expect(res).to eq(3)
|
27
|
+
|
28
|
+
res = Apate.median [2, 3, 4, 2]
|
29
|
+
expect(res).to eq(2.5)
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'estimate variance' do
|
33
|
+
|
34
|
+
it 'should be able to estimate variance in an array' do
|
35
|
+
res = Apate.variance [1, 2, 3, 4, 5]
|
36
|
+
expect(res).to eq(2.5)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should return NaN if only one element in array' do
|
40
|
+
res = Apate.variance [1]
|
41
|
+
expect(res.nan?).to be_truthy
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'mode' do
|
47
|
+
it 'should be able to find the most frequent number in an array' do
|
48
|
+
res = Apate.mode([1,1,1,2,2,3,4,5])
|
49
|
+
expect(res).to eq(1)
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
it 'should select the lowest number if there are the same number of occurenses' do
|
54
|
+
res = Apate.mode([1,1,2,2,3,4,5])
|
55
|
+
expect(res).to eq(1)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should be able to handle negative numbers' do
|
59
|
+
res = Apate.mode([-1,-1,-2,-2,-3,-4,-5])
|
60
|
+
expect(res).to eq(-1)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Apate do
|
4
|
+
|
5
|
+
|
6
|
+
it 'can multiply 2 1x1 matrices' do
|
7
|
+
a = [[2]]
|
8
|
+
b = [[3]]
|
9
|
+
|
10
|
+
res = Apate.matrix_multiply a, b
|
11
|
+
|
12
|
+
expect(res.length).to eq(1)
|
13
|
+
expect(res.first.length).to eq(1)
|
14
|
+
expect(res.first.first).to eq(6)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'can multiply 2 2x2 matrices' do
|
18
|
+
a = [[1,0], [0,1]]
|
19
|
+
b = [[1,2], [3,4]]
|
20
|
+
|
21
|
+
res = Apate.matrix_multiply a, b
|
22
|
+
expect(res.length).to eq(2)
|
23
|
+
expect(res.first.length).to eq(2)
|
24
|
+
expect(res).to eq(b)
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
# it 'can invert a matrix' do
|
29
|
+
# a = [[1,2],[2,1]]
|
30
|
+
|
31
|
+
# res = Apate.invert_matrix a
|
32
|
+
|
33
|
+
# expected = [[1,0], [0,1]]
|
34
|
+
|
35
|
+
# expect(res).to eq(expected)
|
36
|
+
|
37
|
+
# end
|
38
|
+
context 'determinants' do
|
39
|
+
it 'can compute the determinant of a eigenmatrix' do
|
40
|
+
a = [[1,0,0],[0,1,0],[0,0,1]]
|
41
|
+
res = Apate.determinant a
|
42
|
+
expect(res).to eq(1)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'can compute a negative determinant' do
|
46
|
+
a = [[0,1],[1,0]]
|
47
|
+
res = Apate.determinant a
|
48
|
+
expect(res).to eq(-1)
|
49
|
+
|
50
|
+
a = [[1,0,0],[0,0,1],[0,1,0]]
|
51
|
+
res = Apate.determinant a
|
52
|
+
expect(res).to eq(-1)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'can compute the determinant of a singular matrix' do
|
56
|
+
a = [[1,1,1],[1,1,1],[1,1,1]]
|
57
|
+
res = Apate.determinant a
|
58
|
+
expect(res).to eq(0)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'raises error if matrix is not square' do
|
62
|
+
a = [[1,1,1],[1,1,1]]
|
63
|
+
expect { Apate.determinant(a)}.to raise_error
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'can add 2 2x2 matrices' do
|
68
|
+
a = [[1,0], [0,1]]
|
69
|
+
b = [[1,2], [3,4]]
|
70
|
+
|
71
|
+
c = [[2,2], [3,5]]
|
72
|
+
|
73
|
+
res = Apate.matrix_add a, b
|
74
|
+
expect(res.length).to eq(2)
|
75
|
+
expect(res.first.length).to eq(2)
|
76
|
+
expect(res).to eq(c)
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Apate do
|
4
|
+
|
5
|
+
|
6
|
+
it 'should be able to compute the covariance between two arrays' do
|
7
|
+
|
8
|
+
arr1 = [1,2,3]
|
9
|
+
arr2 = [2,3,4]
|
10
|
+
|
11
|
+
expected = 1.0
|
12
|
+
|
13
|
+
c = Apate.covariance(arr1, arr2)
|
14
|
+
|
15
|
+
expect(c).to eq(expected)
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should return nil if the array lengths do not match' do
|
20
|
+
|
21
|
+
arr1 = [1,2,3]
|
22
|
+
arr2 = [2,3,4,2]
|
23
|
+
|
24
|
+
expected = nil
|
25
|
+
|
26
|
+
c = Apate.covariance(arr1, arr2)
|
27
|
+
|
28
|
+
expect(c).to eq(expected)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Albin Svensson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.7'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.7'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.12'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.12'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '4.5'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '4.5'
|
97
|
+
description: Provide tools to be used when making statistical computations.
|
98
|
+
email:
|
99
|
+
- albin.svensson@trialbee.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- .travis.yml
|
106
|
+
- .versions.conf
|
107
|
+
- Gemfile
|
108
|
+
- GuardFile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- apate.gemspec
|
113
|
+
- lib/apate.rb
|
114
|
+
- lib/apate/all_methods.rb
|
115
|
+
- lib/apate/array_methods.rb
|
116
|
+
- lib/apate/basic_methods.rb
|
117
|
+
- lib/apate/matrix_methods.rb
|
118
|
+
- lib/apate/multi_set_methods.rb
|
119
|
+
- lib/apate/version.rb
|
120
|
+
- spec/lib/apate/array_methods_spec.rb
|
121
|
+
- spec/lib/apate/basic_methods_spec.rb
|
122
|
+
- spec/lib/apate/matrix_methods_spec.rb
|
123
|
+
- spec/lib/apate/multi_set_methods_spec.rb
|
124
|
+
- spec/lib/apate_spec.rb
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
homepage: ''
|
127
|
+
licenses:
|
128
|
+
- MIT
|
129
|
+
metadata: {}
|
130
|
+
post_install_message:
|
131
|
+
rdoc_options: []
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ! '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ! '>='
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
requirements: []
|
145
|
+
rubyforge_project:
|
146
|
+
rubygems_version: 2.2.2
|
147
|
+
signing_key:
|
148
|
+
specification_version: 4
|
149
|
+
summary: Provide tools for statistics computation.
|
150
|
+
test_files:
|
151
|
+
- spec/lib/apate/array_methods_spec.rb
|
152
|
+
- spec/lib/apate/basic_methods_spec.rb
|
153
|
+
- spec/lib/apate/matrix_methods_spec.rb
|
154
|
+
- spec/lib/apate/multi_set_methods_spec.rb
|
155
|
+
- spec/lib/apate_spec.rb
|
156
|
+
- spec/spec_helper.rb
|