Created
June 30, 2012 11:44
-
-
Save scan/3023494 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class InterpolationMethod(val s: Traversable[(Double, Double)]) extends (Double => Double) { | |
protected val x_i = s.map(_._1) | |
protected val y_i = s.map(_._2) | |
def this(f: Double => Double, f2: Int => Double, n: Int) = this(for(i <- 0 to n) yield { | |
val x = f2(i) | |
(x, f(x)) | |
}) | |
def this(f: Double => Double, n: Int) = this(f, _.toDouble, n) | |
} | |
class Lagrange(s: Traversable[(Double, Double)]) extends InterpolationMethod(s) { | |
val level = s.size | |
private def poly(i: Int)(x: Double): Double = { | |
val xi = x_i(i) | |
x_i.drop(i).map(xj => (xj - x) / (xj - xi)).reduce[Double](_ * _) | |
} | |
def apply(x: Double) = (for(i <- 0 to x_i.size) yield y_i(i) * poly(i)(x)).sum[Double] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment