Skip to content

Instantly share code, notes, and snippets.

View jamesthompson's full-sized avatar

James Thompson jamesthompson

View GitHub Profile
@jamesthompson
jamesthompson / Leg.scala
Created June 10, 2012 19:00
Legendre Polynomials
def leg(mode: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)
}
stream.take(mode + 1).toIndexedSeq
}
@jamesthompson
jamesthompson / Clean.scala
Created July 17, 2012 21:53
CLEAN algorithm Implemented in Scala
package CLEAN
import scalala.tensor.dense.{DenseMatrix => Image}
import collection.mutable.ArrayBuffer
/**
* Clean class implemented in Scala
* Author: James R. Thompson, D.Phil
* Date: 7/16/12
* Time: 9:31 AM
@jamesthompson
jamesthompson / Track.scala
Created July 18, 2012 22:20
Tracking class
package Spots
import collection.mutable.ArrayBuffer
/**
* Track class
* Author: James R. Thompson, D.Phil
* Date: 7/16/12
* Time: 9:31 AM
* USC Mork Family Dept. of Chem. Eng. & Mat. Sci.
@jamesthompson
jamesthompson / JavaFXImageConversion.java
Created August 13, 2012 21:03
JavaFX Image from a byte array
import javafx.scene.image.Image;
import javax.imageio.ImageIO;
import java.awt.image.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
public Image getJavaFXImage(byte[] rawPixels, int width, int height) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write((RenderedImage) createBufferedImage(rawPixels, width, height), "png", out);
@jamesthompson
jamesthompson / FFT.scala
Created August 20, 2012 18:48
Cooley-Tukey FFT - Scala
import scala.math._
case class Complex(re: Double, im: Double = 0.0) {
def +(x: Complex): Complex = Complex((this.re+x.re), (this.im+x.im))
def -(x: Complex): Complex = Complex((this.re-x.re), (this.im-x.im))
def *(x: Complex): Complex = Complex(this.re*x.re-this.im*x.im, this.re*x.im+this.im*x.re)
}
def transformReal(input:IndexedSeq[Double]) = {
val data = padder(input.map(i => Complex(i)).toList)
@jamesthompson
jamesthompson / JFXImageUtil.scala
Created September 5, 2012 15:53
JavaFX images from byte array data in Scala
import javafx.scene.image.Image
import javax.imageio.ImageIO
import java.awt.image._
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, IOException}
import java.util.logging.{Level, Logger}
object JFXImageUtil {
def getJavaFXImage(rawPixels:Array[Byte], width:Int, height:Int) = {
val out = new ByteArrayOutputStream
@jamesthompson
jamesthompson / CKF.scala
Created September 5, 2012 16:10
Primitive Chung-Kennedy Filter
def calcStDev(in: List[Double]) = {
def squaredDifference(v1:Double, v2:Double) = math.pow(v1 - v2,2.0)
val mean = in.sum / in.length
val squared = in.foldLeft(0.0)(_ + squaredDifference(_, mean))
math.sqrt(squared / in.length.toDouble)
}
def ckf(in:List[Double], windowSize:Int) : List[Double] = {
val out = for(i <- windowSize until in.length - windowSize) yield {
val forward = calcStDev(in.slice(i, i + windowSize + 1))
@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
@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 / 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)
}