Skip to content

Commit

Permalink
calculate link matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrat555 committed May 9, 2017
1 parent ee41016 commit 3c0b982
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 12 deletions.
67 changes: 67 additions & 0 deletions lib/rock/links.ex
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
12 changes: 2 additions & 10 deletions lib/rock/neighbours.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
defmodule Rock.Neighbours do
alias Rock.JaccardCoefficient

def list(points,
theta,
similarity_function \\ &JaccardCoefficient.measure/2)
when is_list(points) do
def list(points, theta, similarity_function) when is_list(points) do
points
|> matrix(theta, similarity_function)
|> index_list
end

def matrix(points,
theta,
similarity_function \\ &JaccardCoefficient.measure/2)
when is_list(points) do
def matrix(points, theta, similarity_function) when is_list(points) do
similarity_function = fn(point1, point2) ->
neighbors?(point1, point2, similarity_function, theta)
end
Expand Down
24 changes: 24 additions & 0 deletions test/rock/links_test.exs
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
9 changes: 7 additions & 2 deletions test/rock/neighbours_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Rock.NeighboursTest do
use ExUnit.Case
alias Rock.Struct.Point
alias Rock.Neighbours
alias Rock.JaccardCoefficient

@points [
Point.new(["1", "2", "3", "4", "5"]),
Expand All @@ -10,7 +11,9 @@ defmodule Rock.NeighboursTest do
]

test "calculates neighbor matrix with jaccard coefficient" do
neighbor_matrix = @points |> Neighbours.matrix(0.1)
neighbor_matrix =
@points
|> Neighbours.matrix(0.1, &JaccardCoefficient.measure/2)

^neighbor_matrix = [
[1, 1, 1],
Expand All @@ -37,7 +40,9 @@ defmodule Rock.NeighboursTest do
end

test "returns neighbor indices list" do
neighbor_list = @points |> Neighbours.list(0.1)
neighbor_list =
@points
|> Neighbours.list(0.1, &JaccardCoefficient.measure/2)

^neighbor_list = [
[0, 1, 2],
Expand Down

0 comments on commit 3c0b982

Please sign in to comment.