Created
November 9, 2018 19:23
-
-
Save thomasnield/688ed7efa2b59b3f542f6f194335bc91 to your computer and use it in GitHub Desktop.
Quick Hacky Derivative
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
| 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