ATuin

PPM Minimum Distance Point

Jan 5th, 2014
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.38 KB | None | 0 0
  1. package com.logikujo
  2. package pebeeme
  3.  
  4. import scalaz._
  5. import Scalaz._
  6. import java.io.File
  7. import scala.collection.Map
  8.  
  9. /**
  10.  *
  11.  * pebeeme / LogiDev - [Fun Functional] / Logikujo.com
  12.  *
  13.  * com.logikujo 5/01/14 :: 11:50 :: eof
  14.  *
  15.  */
  16.  
  17. case class PPM(pixels: List[Pixel]) extends PBM
  18.  
  19. object PPM {
  20.   type D = List[List[((Int,Int), Double)]]
  21.  
  22.   def pointDistances(l: List[Pixel], res: D, n:Int): D = (l.length <= 1)? res | {
  23.     val listOfHeads = List.tabulate(l.length -1)(_ => l.head)
  24.     val lSums = listOfHeads.zip(l.tail).map {
  25.       case (p1,p2) => p1.distance(p2)
  26.     }.zipWithIndex.map {
  27.       case (s,i) => (n, i+1+n) -> s
  28.     }
  29.     pointDistances(l.tail, lSums :: res, n+1)
  30.   }
  31.   def getIndex(m:Map[(Int,Int),Double], idx:(Int,Int)) = idx match {
  32.     case (i,j) => m.get(i->j).orElse(m.get(j->i))
  33.   }
  34.  
  35.   /* DANGER: Double doesnt conform a Monoid. Addition is not associative! */
  36.   implicit val doubleInstance: Monoid[Double] = new Monoid[Double] {
  37.     def append(d1: Double, d2: => Double): Double = d1 + d2
  38.     def zero: Double = 0
  39.   }
  40.  
  41.   def minimumDistancePoint(ppm: PPM) = {
  42.     val len = ppm.pixels.length
  43.     val distances = pointDistances(ppm.pixels, List(), 1).flatten.toMap
  44.     (for {
  45.       i <- 1 to len
  46.     } yield for {
  47.         j <- 1 to len
  48.       } yield getIndex(distances, i -> j)).map { l =>
  49.       l.reduce(_ |+| _)
  50.     }.toList.min
  51.   }
Advertisement
Add Comment
Please, Sign In to add comment