-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule Rock.Links do | ||
alias Rock.JaccardCoefficient | ||
alias Rock.Neighbours | ||
|
||
def matrix(points, theta, similarity_function \\ &JaccardCoefficient.measure/2) | ||
when is_number(theta) | ||
when is_list(points)do | ||
neighbour_lists = | ||
points | ||
|> Neighbours.list(theta, similarity_function) | ||
|
||
points | ||
|> Enum.count | ||
|> initialize_links | ||
|> outer_loop(neighbour_lists) | ||
end | ||
|
||
def initialize_links(size) do | ||
1..size | ||
|> Enum.map(fn(_) -> | ||
1..size | ||
|> Enum.map(fn(_) -> | ||
0 | ||
end) | ||
end) | ||
end | ||
|
||
def outer_loop(link_matrix, [neighbour_list | []]) do | ||
link_matrix | ||
|> links_from_neighbours(neighbour_list) | ||
end | ||
|
||
def outer_loop(link_matrix, [neighbour_list | tail]) do | ||
link_matrix | ||
|> links_from_neighbours(neighbour_list) | ||
|> outer_loop(tail) | ||
end | ||
|
||
def links_from_neighbours(link_matrix, [_neighbour | []]) do | ||
link_matrix | ||
end | ||
|
||
def links_from_neighbours(link_matrix, [neighbour | neighbour_tail]) do | ||
link_matrix | ||
|> add_links(neighbour_tail, neighbour) | ||
|> links_from_neighbours(neighbour_tail) | ||
end | ||
|
||
def add_links(link_matrix, [neighbour | []], row_index) do | ||
link_matrix | ||
|> add_link(row_index, neighbour) | ||
end | ||
|
||
def add_links(link_matrix, [neighbour | neighbour_tail], row_index) do | ||
link_matrix | ||
|> add_link(row_index, neighbour) | ||
|> add_links(neighbour_tail, row_index) | ||
end | ||
|
||
def add_link(link_matrix, row_index, column_index) do | ||
link_matrix | ||
|> List.update_at(row_index, fn(row) -> | ||
row | ||
|> List.update_at(column_index, &(&1 + 1)) | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Rock.LinksTest do | ||
use ExUnit.Case | ||
alias Rock.Links | ||
alias Rock.Struct.Point | ||
|
||
@points [ | ||
Point.new(["1", "2", "3", "4", "5"]), | ||
Point.new(["1"]), | ||
Point.new(["5", "6", "7"]) | ||
] | ||
|
||
test "calculates link matrix" do | ||
link_matrix = | ||
@points | ||
|> Links.matrix(0) | ||
|
||
^link_matrix = | ||
[ | ||
[0, 2, 2], | ||
[0, 0, 1], | ||
[0, 0, 0] | ||
] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters