Skip to content

Instantly share code, notes, and snippets.

@jonathanmarvens
Created July 7, 2014 09:49
Show Gist options
  • Save jonathanmarvens/a8b66cc2393bddc8cfe8 to your computer and use it in GitHub Desktop.
Save jonathanmarvens/a8b66cc2393bddc8cfe8 to your computer and use it in GitHub Desktop.
/**
* The MIT License (MIT).
*
* Copyright (c) 2014 Jonathan Barronville ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package distance
import (
"math"
"sort"
)
type MinkowskiFormula float64
type MinkowskiSubjDistTuple struct {
dist float64
subj string
}
type MinkowskiSubjDistTuples []MinkowskiSubjDistTuple
const (
MinkowskiFormulaEuclidean MinkowskiFormula = 2
MinkowskiFormulaManhattan MinkowskiFormula = 1
)
func Minkowski(x map[string]float64, y map[string]float64, r MinkowskiFormula) float64 {
sum1AddendChan := make(chan float64)
var routineCount int64
for k, xVal := range x {
yVal, ok := y[k]
if !ok {
continue
}
go func() {
sum1AddendChan <- math.Pow(
math.Abs(xVal-yVal),
float64(r),
)
}()
routineCount++
}
var sum1 float64
for v := range sum1AddendChan {
sum1 += v
routineCount--
if routineCount == 0 {
break
}
}
return math.Pow(sum1, (1 / float64(r)))
}
func MinkowskiNearestNeighbors(subj string, subjMap map[string](map[string]float64), r MinkowskiFormula) MinkowskiSubjDistTuples {
minkowskiChan := make(chan MinkowskiSubjDistTuple)
var routineCount int64
for k, v := range subjMap {
if subj != k {
go func() {
minkowskiChan <- MinkowskiSubjDistTuple{
dist: Minkowski(subjMap[subj], v, r),
subj: k,
}
}()
routineCount++
}
}
dists := make(MinkowskiSubjDistTuples, 0)
for v := range minkowskiChan {
dists = append(dists, v)
routineCount--
if routineCount == 0 {
break
}
}
sort.Sort(dists)
return dists
}
func (self MinkowskiSubjDistTuples) Len() int {
return len(self)
}
func (self MinkowskiSubjDistTuples) Less(i int, j int) bool {
return (self[i].dist < self[j].dist)
}
func (self MinkowskiSubjDistTuples) Swap(i int, j int) {
self[i], self[j] = self[j], self[i]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment