metric_space 0.0.3 → 0.0.4
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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +3 -1
- data/lib/metric_space.rb +6 -9
- data/lib/metric_space/british_rail.rb +2 -2
- data/lib/metric_space/metric.rb +2 -0
- data/lib/metric_space/taxicab.rb +2 -2
- data/lib/metric_space/version.rb +2 -1
- data/spec/metric_space/british_rail_spec.rb +21 -21
- data/spec/metric_space/euclidean_spec.rb +9 -8
- data/spec/metric_space/maximum_spec.rb +9 -8
- data/spec/metric_space/taxicab_spec.rb +9 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d03c5cdd0bc4536fad6968870d78ccdc1daca528
|
4
|
+
data.tar.gz: c5104533d60cb21169acabb33650315bd5b81e00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: befda889af180f0b9b56cccd68a9f69b29f3f940797f249fdcc629769796f8e7fc78539b9a6e80d81f556139ab6dc01894e5bcf86e3852c34261063a61c8b1ff
|
7
|
+
data.tar.gz: 2c720ea3751d1ac1c5373b1d95c1dd02ebf3f574f76884192560da747ed51caf96e4a66d8a28cd29c8ddbd63c73e605d666d5b2d836b27c6d1f8de6ebef6c1e2
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
# MetricSpace [](http://badge.fury.io/rb/metric_space) [](https://travis-ci.org/fractalsoft/metric_space) [](https://gemnasium.com/fractalsoft/metric_space) [](https://coveralls.io/r/fractalsoft/metric_space)
|
1
|
+
# MetricSpace [](http://badge.fury.io/rb/metric_space) [](https://travis-ci.org/fractalsoft/metric_space) [](https://gemnasium.com/fractalsoft/metric_space) [](https://coveralls.io/r/fractalsoft/metric_space) [](http://waffle.io/fractalsoft/metric_space)
|
2
|
+
|
3
|
+
[](https://coderwall.com/torrocus)
|
2
4
|
|
3
5
|
Count distance between points in selected metric space
|
4
6
|
|
data/lib/metric_space.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
|
8
|
-
module MetricSpace
|
9
|
-
end
|
1
|
+
require 'metric_space/metric'
|
2
|
+
require 'metric_space/british_rail'
|
3
|
+
require 'metric_space/euclidean'
|
4
|
+
require 'metric_space/maximum'
|
5
|
+
require 'metric_space/taxicab'
|
6
|
+
require 'metric_space/version'
|
@@ -12,7 +12,7 @@ module MetricSpace
|
|
12
12
|
# point2 = {a:-1.5, b:3.0, c:-1.5}
|
13
13
|
# self.distance(point1, point2) #=> 9.353142959975042
|
14
14
|
def self.distance(one, two)
|
15
|
-
first, second =
|
15
|
+
first, second = normalize(one), normalize(two)
|
16
16
|
if first == second
|
17
17
|
Euclidean.distance(one, two)
|
18
18
|
else
|
@@ -27,7 +27,7 @@ module MetricSpace
|
|
27
27
|
def self.normalize(point)
|
28
28
|
hash, max = point.dup, point.values.map(&:abs).max
|
29
29
|
hash.each_pair do |index, value|
|
30
|
-
hash[index] = value/max
|
30
|
+
hash[index] = value / max
|
31
31
|
end
|
32
32
|
hash
|
33
33
|
end
|
data/lib/metric_space/metric.rb
CHANGED
data/lib/metric_space/taxicab.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module MetricSpace
|
2
|
-
# The name relates to the distance a taxi has to drive in
|
3
|
-
# grid to get from the origin to the point x.
|
2
|
+
# The name relates to the distance a taxi has to drive in
|
3
|
+
# a rectangular street grid to get from the origin to the point x.
|
4
4
|
class Taxicab
|
5
5
|
# Distance between two points in taxicab metrics
|
6
6
|
#
|
data/lib/metric_space/version.rb
CHANGED
@@ -1,51 +1,51 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe MetricSpace::BritishRail do
|
4
|
-
|
4
|
+
subject { MetricSpace::BritishRail }
|
5
5
|
|
6
|
-
describe
|
6
|
+
describe '.distance' do
|
7
7
|
[
|
8
8
|
{
|
9
|
-
point1: {a: 3.0, b: 4.0},
|
10
|
-
point2: {a: 8.0, b: 6.0},
|
9
|
+
point1: { a: 3.0, b: 4.0 },
|
10
|
+
point2: { a: 8.0, b: 6.0 },
|
11
11
|
distance: 15.0
|
12
12
|
},
|
13
13
|
{
|
14
|
-
point1: {a: 3.0, c: 4.0},
|
15
|
-
point2: {a: 8.0, b: 6.0},
|
14
|
+
point1: { a: 3.0, c: 4.0 },
|
15
|
+
point2: { a: 8.0, b: 6.0 },
|
16
16
|
distance: 15.0
|
17
17
|
},
|
18
18
|
{
|
19
|
-
point1: {a: 4.0, b: 3.0},
|
20
|
-
point2: {a: 8.0, b: 6.0},
|
19
|
+
point1: { a: 4.0, b: 3.0 },
|
20
|
+
point2: { a: 8.0, b: 6.0 },
|
21
21
|
distance: 5.0
|
22
22
|
},
|
23
23
|
{
|
24
|
-
point1: {a: 4.0, b: -3.0},
|
25
|
-
point2: {a: -8.0, b: 6.0},
|
24
|
+
point1: { a: 4.0, b: -3.0 },
|
25
|
+
point2: { a: -8.0, b: 6.0 },
|
26
26
|
distance: 15.0
|
27
27
|
},
|
28
28
|
{
|
29
|
-
point1: {a: -1.5, b: 1.5},
|
30
|
-
point2: {a: 1.5, b: -1.5},
|
31
|
-
distance: 3*(2.0**0.5)
|
29
|
+
point1: { a: -1.5, b: 1.5 },
|
30
|
+
point2: { a: 1.5, b: -1.5 },
|
31
|
+
distance: 3 * (2.0**0.5)
|
32
32
|
},
|
33
33
|
].each do |example|
|
34
34
|
point1, point2, distance = example.values
|
35
|
-
it "
|
36
|
-
|
35
|
+
it "is #{distance} between #{point1} and #{point2}" do
|
36
|
+
subject.distance(point1, point2).round(5).should eq(distance.round(5))
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
describe
|
41
|
+
describe '.normalize' do
|
42
42
|
{
|
43
|
-
{a:2.0, b:1.0, c:0.5} => {a:1.0, b:0.5, c:0.25},
|
44
|
-
{a
|
43
|
+
{ a: 2.0, b: 1.0, c: 0.5 } => { a: 1.0, b: 0.5, c: 0.25 },
|
44
|
+
{ a: -2.0, b: 4.0, c: -1.0 } => { a: -0.5, b: 1.0, c: -0.25 }
|
45
45
|
}.each_pair do |input, output|
|
46
46
|
it "should return #{output} for #{input}" do
|
47
|
-
|
47
|
+
subject.normalize(input).should eq(output)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
51
|
-
end
|
51
|
+
end
|
@@ -1,22 +1,23 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe MetricSpace::Euclidean do
|
4
|
-
|
4
|
+
subject { MetricSpace::Euclidean }
|
5
|
+
|
5
6
|
[
|
6
7
|
{
|
7
|
-
point1: {a: 7.25, b: 8.5},
|
8
|
-
point2: {a: 4.25, b: 4.5},
|
8
|
+
point1: { a: 7.25, b: 8.5 },
|
9
|
+
point2: { a: 4.25, b: 4.5 },
|
9
10
|
distance: 5.0
|
10
11
|
},
|
11
12
|
{
|
12
|
-
point1: {a: 0.5, c: 0.5},
|
13
|
-
point2: {b: 0.4, c: 0.8},
|
13
|
+
point1: { a: 0.5, c: 0.5 },
|
14
|
+
point2: { b: 0.4, c: 0.8 },
|
14
15
|
distance: 0.5**0.5
|
15
16
|
}
|
16
17
|
].each do |example|
|
17
18
|
point1, point2, distance = example.values
|
18
|
-
it "
|
19
|
-
|
19
|
+
it "is #{distance} between #{point1} and #{point2}" do
|
20
|
+
subject.distance(point1, point2).round(5).should eq(distance.round(5))
|
20
21
|
end
|
21
22
|
end
|
22
23
|
end
|
@@ -1,22 +1,23 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe MetricSpace::Maximum do
|
4
|
-
|
4
|
+
subject { MetricSpace::Maximum }
|
5
|
+
|
5
6
|
[
|
6
7
|
{
|
7
|
-
point1: {a: 7.25, b: 8.5, c: 2.0},
|
8
|
-
point2: {a: 4.25, b: 4.5, c: 7.0},
|
8
|
+
point1: { a: 7.25, b: 8.5, c: 2.0 },
|
9
|
+
point2: { a: 4.25, b: 4.5, c: 7.0 },
|
9
10
|
distance: 5
|
10
11
|
},
|
11
12
|
{
|
12
|
-
point1: {a: 0.3, c: 0.5},
|
13
|
-
point2: {b: 0.8, c: 0.9},
|
13
|
+
point1: { a: 0.3, c: 0.5 },
|
14
|
+
point2: { b: 0.8, c: 0.9 },
|
14
15
|
distance: 0.8
|
15
16
|
}
|
16
17
|
].each do |example|
|
17
18
|
point1, point2, distance = example.values
|
18
|
-
it "
|
19
|
-
|
19
|
+
it "is #{distance} between #{point1} and #{point2}" do
|
20
|
+
subject.distance(point1, point2).round(5).should eq(distance)
|
20
21
|
end
|
21
22
|
end
|
22
23
|
end
|
@@ -1,22 +1,23 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe MetricSpace::Taxicab do
|
4
|
-
|
4
|
+
subject { MetricSpace::Taxicab }
|
5
|
+
|
5
6
|
[
|
6
7
|
{
|
7
|
-
point1: {a: -0.25, b: 3.25, c: 0.8},
|
8
|
-
point2: {a: 0.8, b: 1.25, c: 0.5},
|
8
|
+
point1: { a: -0.25, b: 3.25, c: 0.8 },
|
9
|
+
point2: { a: 0.8, b: 1.25, c: 0.5 },
|
9
10
|
distance: 3.35
|
10
11
|
},
|
11
12
|
{
|
12
|
-
point1: {a: 0.8, c: 0.5},
|
13
|
-
point2: {b: 0.3, c: 1},
|
13
|
+
point1: { a: 0.8, c: 0.5 },
|
14
|
+
point2: { b: 0.3, c: 1 },
|
14
15
|
distance: 1.6
|
15
16
|
}
|
16
17
|
].each do |example|
|
17
18
|
point1, point2, distance = example.values
|
18
|
-
it "
|
19
|
-
|
19
|
+
it "is #{distance} between #{point1} and #{point2}" do
|
20
|
+
subject.distance(point1, point2).round(5).should eq(distance)
|
20
21
|
end
|
21
22
|
end
|
22
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metric_space
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aleksander Malaszkiewicz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|