Skip to content

Instantly share code, notes, and snippets.

@mads-hartmann
Created March 31, 2010 17:56
Show Gist options
  • Save mads-hartmann/350635 to your computer and use it in GitHub Desktop.
Save mads-hartmann/350635 to your computer and use it in GitHub Desktop.
Coding is a social activity - This was coded by me and a friend some lazy afternoon while drinking beer. This is a script which calculates the path integral for a function (does so horrible wrong now though).
import java.math._
def calculatePoints(xvals: List[Double], func: Double=> (Double,Double) ) = xvals.map{ x => func(x)}
def calculateLengthOfPoints(points: List[(Double,Double)], length: Double): Double = {
def distance(p1: (Double,Double), p2: (Double,Double)) =
Math.sqrt(Math.pow(p2._1-p1._1,2)+Math.pow(p2._2-p1._2,2))
points match {
case Nil => length
case point::rest if rest == Nil => calculateLengthOfPoints(rest, length)
case point::rest => calculateLengthOfPoints(rest, length+distance(point,rest.first))
}
}
def calculateLengthOfFunctionInInterval(func: Double => Double, startX: Double, endX: Double) = {
val points = calculatePoints( (startX to endX).by(1).toList, (x) => (x,func(x)))
calculateLengthOfPoints(points, 0)
}
def calculateLengthOfFunctionInInterval2(func: Double => Double, startX: Double, endX: Double, stops: Int) = {
val xValues = {
val step = (endX - startX + 1.0) / stops
def increment(x: Double): Double = startX + (x*step)
vals = (0 to stops-1).toList.map { _ match {
case 0 => startX
case x if x == stops-1 => endX
case x => increment(x)
}}
}
val points = calculatePoints( xValues, x => (x,func(x)))
calculateLengthOfPoints(points, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment