Skip to content

Instantly share code, notes, and snippets.

View jamesthompson's full-sized avatar

James Thompson jamesthompson

View GitHub Profile
@jamesthompson
jamesthompson / LinReg.scala
Created May 6, 2013 18:41
Linear Regression
implicit class LinearRegression[NA: Numeric, NB: Numeric, M[+_]](pts: (M[NA], M[NB])) {
def getVector[A: Numeric](in: M[A]) : Vector[Double] = {
val n = implicitly[Numeric[A]]
in.asInstanceOf[TraversableLike[A, Traversable[A]]].map(n.toDouble(_)).toVector
}
def lnfit = {
val xpts = getVector(pts._1)
val ypts = getVector(pts._2)
xpts.isEmpty match {
case true => None
@jamesthompson
jamesthompson / Normalizer.scala
Created April 27, 2013 15:24
Normalizer. Takes any TraversableLike collection of Numeric values and returns those values normalized as Double between 0 and 1.
import scala.collection.generic.CanBuildFrom
import scala.collection.TraversableLike
implicit class Normalizer[N:Numeric, M[+_]](xs:M[N]) {
def normalize(implicit bf: CanBuildFrom[M[N], Double, M[Double]],
ev: M[N] => TraversableLike[N, M[N]],
eb: M[Double] => TraversableLike[Double, M[Double]]) = {
val n = implicitly[Numeric[N]]
val mind = n.toDouble(xs.min)
val maxd = n.toDouble(xs.max)
xs.map(v => (n.toDouble(v) - mind) * (1 / (maxd - mind)))
@jamesthompson
jamesthompson / Meter.scala
Last active December 16, 2015 16:29
implicit class / value class
implicit class Meter(val num: Double) extends AnyVal {
def m : Double = num
def mm : Double = num / 1e3
def um : Double = num / 1e6
def nm : Double = num / 1e9
}
@jamesthompson
jamesthompson / Timer.scala
Last active December 14, 2015 17:09
Scala Implicit Timer
implicit class Timer[T](f: => T) {
def timems = {
val before = System.currentTimeMillis
val result = f
val end = System.currentTimeMillis
println(s"Operation elapsed time = ${end - before} ms >>> Result = $result")
}
def timens = {
val before = System.nanoTime
val result = f
@jamesthompson
jamesthompson / build.gradle
Last active December 12, 2015 06:58
JavaFX Gradle Build File
group = 'com.malmstadt'
version = 1.0
apply plugin: 'scala'
apply from: 'https://repository-javafx-gradle-plugin.forge.cloudbees.com/release/javafx.plugin'
repositories {
mavenCentral()
}
@jamesthompson
jamesthompson / build.gradle
Created February 4, 2013 18:24
build.gradle for ImageJ Plugins
apply plugin: 'java'
sourceCompatibility=1.6
targetCompatibility=1.6
dependencies {
compile files("/Users/James/Desktop/GUVJesper/lib/ij.jar", "/Users/James/Desktop/GUVJesper/lib/loci_tools.jar")
}
mainClass = 'ND2_GUVPlugin'
@jamesthompson
jamesthompson / SublimeJava.sublime-settings
Created October 26, 2012 21:43
Sublime Java Settings
{
// You probably want to configure this to something of your own
// ${home}, ${env:<variable>}, ${project_path:} and ${folder:} tokens can be used in the sublimejava_classpath option.
//
// ${home} is replaced with the value of the HOME environment variable.
//
// ${env:<variable>} is replaced with the "variable" environment variable.
//
// ${project_path:} tries to find a file with the given name in all the registered project folders and
// returns the first file found, or the original file name if none is found.
@jamesthompson
jamesthompson / LegAssocLeg.scala
Created October 1, 2012 23:27
Legendre & Associated Legendre Polynomials
object Legendre {
// Returns all the Legendre Polynomials Up to that l number using For Loop -> Yield - to get the l-th term take .tail (m = 0 for the Legendre Polynomials)
// Horner's Rule implemented as a Stream
def leg(l : Int, angle : Double) : IndexedSeq[Double] = {
val cosAngle = math.cos(angle)
lazy val stream : Stream[Double] = {
def loop(last:Double, curr:Double, k:Double = 0.0) : Stream[Double] = curr #:: loop(curr, ((2 * k + 1) * cosAngle * curr - k * last) / (k + 1), k + 1)
loop(0.0, 1.0)
}
@jamesthompson
jamesthompson / sbt.sublime-build
Created October 1, 2012 17:23
Sublime sbt build settings
{
"cmd": ["/Users/James/Documents/Code/sbt/bin/sbt -no-colors compile run"],
"file_regex": "^\\[error\\] ([.a-zA-Z0-9/-]+[.]scala):([0-9]+):",
"selector": "source.scala",
"working_dir": "${project_path:${folder}}",
"shell": "true"
}
@jamesthompson
jamesthompson / LinearRegression.scala
Created September 19, 2012 21:50
Linear Regression in Scala
object LinearRegression {
def fit(data: List[(Double,Double)]) = {
def sqr(in:Double) : Double = in * in
val sumx = data.map(_._1).sum
val sumx2 = data.map((d:(Double,Double)) => sqr(d._1)).sum
val sumy = data.map(_._2).sum
val xbar = sumx / data.length
val ybar = sumy / data.length
val xxbar = data.map(d => sqr(d._1 - xbar)).sum