Skip to content

Instantly share code, notes, and snippets.

@agazzarini
Created August 16, 2020 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agazzarini/77b06db5bc1c6aeb245e5dc029bc4975 to your computer and use it in GitHub Desktop.
Save agazzarini/77b06db5bc1c6aeb245e5dc029bc4975 to your computer and use it in GitHub Desktop.
package io.sease.ra
object IntervalsMatrix {
case class IntervalsMatrix(matrix: Iterator[Seq[Int]]) {
def length: Int = Option(matrix).map(_.length).getOrElse(0)
def isEmpty: Boolean = matrix.isEmpty
override def toString: String = toString(_.toString)
override def equals(that: Any): Boolean = {
that match {
case other: IntervalsMatrix => other.matrix sameElements matrix
case _ => false
}
}
def toString(valueExtractor: Int => String): String = {
safeCopy.zipWithIndex.map {
case (vector, index) =>
s"t$index - t${index + 1}\t=>" + vector
.map(valueExtractor)
.mkString(" [", "] [", "]\n")
}.mkString
}
private def safeCopy = matrix.duplicate._1
}
def apply(chromaMatrix: IndexedSeq[Seq[Double]],
k: Int,
table: IntervalsTable): IntervalsMatrix = {
val matrix = Option(chromaMatrix).getOrElse(Seq.empty)
require(matrix.nonEmpty, "Chroma Matrix cannot be empty!")
require(k <= 12, "Chroma Matrix must be [m,<=12]")
require(Option(table).isDefined, "IntervalsTable cannot be null")
IntervalsMatrix(
matrix
.filter(_.length >= k)
.map(
vector =>
vector.zipWithIndex
.sortBy(_._1)(Ordering[Double].reverse)
.take(k)
.map(_._2))
.sliding(2)
.map(t => table.distanceBetween(t.head, t.tail.head)))
}
def apply(chromaMatrix: Array[Array[Double]],
k: Int,
table: IntervalsTable): IntervalsMatrix = {
this(for (i <- chromaMatrix.indices) yield chromaMatrix(i).toSeq, k, table)
}
def apply(matrix: Iterator[Seq[Int]]): IntervalsMatrix =
IntervalsMatrix(matrix)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment