Skip to content

Instantly share code, notes, and snippets.

View jamesthompson's full-sized avatar

James Thompson jamesthompson

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / LegSpire.scala
Created July 25, 2013 22:51
Bonnet's Recursion Formula using Spire for Polynomials... Legendre Polynomials
def legendres(i: Int) : List[Polynomial[Rational]] = {
val one = Polynomial(0 -> r"1/1")
val x = Polynomial(1 -> r"1/1")
lazy val leg : Stream[Polynomial[Rational]] = {
def loop(pnm1: Polynomial[Rational], pn: Polynomial[Rational], n: Int = 1) : Stream[Polynomial[Rational]] = {
pn #:: loop(pn, Polynomial(0 -> Numeric[Rational].fromInt(n + 1).reciprocal) * (
(Polynomial(1 -> Numeric[Rational].fromInt(2 * n + 1)) * pn) +
(Polynomial(0 -> Numeric[Rational].fromInt(n).unary_-) * pnm1)), n + 1)
}
one #:: loop(one, x)
@jamesthompson
jamesthompson / gist:6625529
Created September 19, 2013 15:52
Lines of code in Sublime Text 3
find regex: ^(.*)$
where: *.scala, *.java etc etc...
scala> p
res6: spire.math.poly.MultivariatePolynomial[Double] = (2.0x + 5.0 + 3.0y)
scala> q
res7: spire.math.poly.MultivariatePolynomial[Double] = (xy + 2.0x + 1.0 + 5.0y)
scala> p * q
res8: spire.math.poly.MultivariatePolynomial[Double] = (2.0x^2y + 4.0x^2 + 3.0xy^2 + 21.0xy + 12.0x + 28.0y + 5.0 + 15.0y^2)
scala> p + q