Skip to content

Instantly share code, notes, and snippets.

Avatar

James Thompson jamesthompson

View GitHub Profile
@jamesthompson
jamesthompson / Leg.scala
Created Jun 10, 2012
Legendre Polynomials
View Leg.scala
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 Jul 17, 2012
CLEAN algorithm Implemented in Scala
View Clean.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
View Track.scala
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 Aug 13, 2012
JavaFX Image from a byte array
View JavaFXImageConversion.java
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 Aug 20, 2012
Cooley-Tukey FFT - Scala
View 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 Sep 5, 2012
JavaFX images from byte array data in Scala
View JFXImageUtil.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 Sep 5, 2012
Primitive Chung-Kennedy Filter
View CKF.scala
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 Sep 19, 2012
Linear Regression in Scala
View LinearRegression.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 Oct 1, 2012
Sublime sbt build settings
View sbt.sublime-build
{
"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 Oct 1, 2012
Legendre & Associated Legendre Polynomials
View LegAssocLeg.scala
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)
}