Skip to content

Instantly share code, notes, and snippets.

View MarkCLewis's full-sized avatar

Mark Lewis MarkCLewis

View GitHub Profile
import io.Source
val exportRegex = """.*PIMSDB"."(\S+)".*""".r
val exportSource = Source.fromFile("DBExportList.txt")
val exportData = (for(exportRegex(fname) <- exportSource.getLines) yield fname.trim).toArray
exportSource.close
val tableSource = Source.fromFile("TableDefTable.txt")
val tableData = tableSource.getLines.map(_.split("\t")(1).trim.toUpperCase).toArray
tableSource.close
@MarkCLewis
MarkCLewis / REAddition.scala
Created December 7, 2015 15:04
This is a Scala program that runs a Chomsky recursively enumerable grammar. The grammar that it is set up to run adds binary numbers.
package grammars
import io.StdIn._
/**
* @author mlewis
*/
object REAddition extends App {
val prods1 = Map[String, String](
"N+N1" -> "SN+'NL1",
@MarkCLewis
MarkCLewis / NBodyArray.scala
Created January 1, 2017 21:05
This is a little code segment that I wrote to do a time comparison of the speed of Scala for and while loops between 2.11 and 2.12. This code uses a lot of mutation with a value class that makes things more OO. I plan to test this against other approaches for overall speed later.
object NBodyArray extends App {
def timeCode[T](warmups: Int, timeRuns: Int)(body: => T): Seq[Double] = {
for(_ <- 1 to warmups) body
for(_ <- 1 to timeRuns) yield {
val start = System.nanoTime()
body
(System.nanoTime()-start)*1e-9
}
}
def printTimeInfo(times: Seq[Double]): Unit = {
@MarkCLewis
MarkCLewis / NBodyMutableClass.scala
Created January 3, 2017 16:51
This code does a basic N-body simulation using arrays of mutable classes. This approach mirrors what is done in languages like C++, but the memory model is different so the array actually holds references to the bodies and the bodies could be distributed through memory.
class MVect3(var x: Double, var y: Double, var z: Double) {
def zero(): Unit = {
x = 0.0
y = 0.0
z = 0.0
}
}
class MutableBody(
val p: MVect3,
@MarkCLewis
MarkCLewis / NBodyImmutableClass.scala
Last active January 3, 2017 19:07
This code uses immutable body and 3-D vector classes with a mutable array to do a basic N-body simulation.
case class Vect3(x: Double, y: Double, z: Double) {
def +(v: Vect3) = Vect3(x+v.x, y+v.y, z+v.z)
def -(v: Vect3) = Vect3(x-v.x, y-v.y, z-v.z)
def *(c: Double) = Vect3(x*c, y*c, z*c)
def /(c: Double) = Vect3(x/c, y/c, z/c)
}
case class ImmutableBody(p: Vect3, v: Vect3, mass: Double) {
def step(a: Vect3, dt: Double) = {
val nv = v+a*dt
@MarkCLewis
MarkCLewis / NBodyValClass.scala
Created January 3, 2017 19:12
This code does a simple N-body integration using arrays of doubles for storing values and a value class to make the code more readable without introducing overhead.
object NBodyValClass {
private var numBodies = 0
private var dt = 0.0
private var positions = Array.fill(0)(0.0)
private var velocities = Array.fill(0)(0.0)
private var accel = Array.fill(0)(0.0)
private var masses = Array.fill(0)(1e-10)
class Particle(val index: Int) extends AnyVal {
def x = positions(index*3)
@MarkCLewis
MarkCLewis / NBodyFunctional.scala
Created January 4, 2017 00:09
This code gives two versions of an N-body integrator that are functional. The first one uses the approach of the mutable methods that does the minimum number of distance calculations, but at the cost of using updated on Vector. The second version does twice as many distance calculations, but doesn't have to rebuild data structures nearly as much…
object NBodyFunctional {
def initBodies(numBodies: Int): Vector[ImmutableBody] = {
Vector.tabulate(numBodies) { i =>
ImmutableBody(Vect3(i,0,0), Vect3(0,math.sqrt(1.0/i),0),
if(i==0) 1 else 1e-10)
}
}
def forSim(bodies: Vector[ImmutableBody], steps: Int, dt: Double): Vector[ImmutableBody] = {
@MarkCLewis
MarkCLewis / NBodyMutable.cpp
Created January 4, 2017 21:15
This file does a simple N-body simulation in C++ and times how long it takes to complete 1000 steps.
#include<iostream>
#include<valarray>
#include<cmath>
#include <chrono>
using namespace std;
struct MVect3 {
double x, y, z;
MVect3() {}
@MarkCLewis
MarkCLewis / MainTiming.scala
Created January 4, 2017 21:18
This shows two different timing applications that I used for timing some N-body simulations in Scala.
object MainTiming extends App {
def timeCode[T](warmups: Int, timeRuns: Int)(body: => T): Seq[Double] = {
for(_ <- 1 to warmups) body
for(_ <- 1 to timeRuns) yield {
val start = System.nanoTime()
body
(System.nanoTime()-start)*1e-9
}
}
def printTimeInfo(times: Seq[Double]): Unit = {
@MarkCLewis
MarkCLewis / Renderer2D.scala
Created August 26, 2017 13:03
Renderers that students can use for their graphical games in CSCI 1321.
import scalafx.scene.canvas.GraphicsContext
import scalafx.scene.image.Image
/**
* This is a 2D renderer that with draw your game elements to a Canvas. You should change the
* images to fit the style of your game. Also, alter the entities to match what you have in
* your game.
*/
class Renderer2D(gc: GraphicsContext, blockSize: Double) {
private var lastCenterX = 0.0