Skip to content

Instantly share code, notes, and snippets.

@thomasnield
Created November 9, 2018 19:23
Show Gist options
  • Select an option

  • Save thomasnield/688ed7efa2b59b3f542f6f194335bc91 to your computer and use it in GitHub Desktop.

Select an option

Save thomasnield/688ed7efa2b59b3f542f6f194335bc91 to your computer and use it in GitHub Desktop.
Quick Hacky Derivative
import kotlin.math.pow
fun main(args: Array<String>) {
val xSquare = { x: Double -> x.pow(2) }
val slope = derivativeOf(1.0, xSquare)
println(slope)
}
inline fun derivativeOf(x: Double, crossinline fn: (Double) -> Double): Double {
val h = 0.0000001
return (fn(x + h) - fn(x)) / 2.0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment