Skip to content

Instantly share code, notes, and snippets.

@thomasnield
Last active December 18, 2018 22:45
Show Gist options
  • Select an option

  • Save thomasnield/34d51e52c37a8e9a146d56e24d201e6c to your computer and use it in GitHub Desktop.

Select an option

Save thomasnield/34d51e52c37a8e9a146d56e24d201e6c to your computer and use it in GitHub Desktop.
Sampling an Arbitrary Curve
import kotlin.math.pow
// https://www.desmos.com/calculator/qjxcsxsl6t
// https://www.wolframalpha.com/input/?i=integral+-2(x-1)%5E2+%2B+2
fun main(args: Array<String>) {
println(f(1.0))
println(integratedF(0.0,2.0))
println(inverseIntegratedF(4.0/3.0))
}
fun f(x: Double) = -2.0 * (x-1.0).pow(2) + 2.0
fun integratedF(x: Double) = -2.0 * ((1.0/3.0)*x.pow(3) - x.pow(2))
fun integratedF(lower: Double, upper: Double) = integratedF(upper) - integratedF(lower)
fun inverseIntegratedF(a: Double): Double {
fun expr1(a: Double) = (3.0.sqrt() * (3.0*a.pow(2.0) - 8.0*a).sqrt() - 3.0*a + 4.0).cbrt() // square rooting a negative here :/
fun expr2(a: Double) = 2.0.pow(2.0/3.0)
fun expr(a: Double) = (expr1(a) / expr2(a)) + (expr2(a) / expr1(a)) + 1
return expr(a)
}
fun Double.sqrt() = pow(1.0/2.0)
fun Double.cbrt() = Math.cbrt(this)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment