Add orx-gradient-descent

This commit is contained in:
Edwin Jakobs
2019-09-19 16:18:19 +02:00
parent e2d62e1b02
commit 6eec2919f9
5 changed files with 254 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
import org.amshove.kluent.`should be equal to`
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
object TestDot : Spek({
describe("some vectors") {
val a = doubleArrayOf(10.0)
val b = doubleArrayOf(4.0)
dot(a,b) `should be equal to` 40.0
}
describe("a matrix and a vector") {
val a = arrayOf(doubleArrayOf(10.0))
val b = doubleArrayOf(1.0)
val d = dot(a,b)
d[0] `should be equal to` 10.0
}
describe("a matrix and a vector") {
val a = arrayOf(doubleArrayOf(1.0))
val b = doubleArrayOf(19.99999999995339)
val d = dot(a,b)
d[0] `should be equal to` 19.99999999995339
}
})

View File

@@ -0,0 +1,43 @@
import org.amshove.kluent.`should be equal to`
import org.amshove.kluent.`should equal`
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
object TestGradient : Spek({
describe("a simple 1d function") {
fun parabola(x: DoubleArray): Double {
return x[0] * x[0]
}
it("its gradient at x=0 is 0.0") {
val g0 = gradient(doubleArrayOf(0.0), ::parabola)
g0.size `should equal` 1
g0[0] `should be equal to` 0.0
}
it("its gradient at x=1 is ~2.0") {
val g1 = gradient(doubleArrayOf(1.0), ::parabola)
}
it("its gradient at x=-1 is ~-2.0") {
val g1 = gradient(doubleArrayOf(-1.0), ::parabola)
}
}
describe("a simple 2d function") {
fun parabola(x: DoubleArray): Double {
return x[0] * x[0] + x[1] * x[1]
}
it("its gradient at x=0 is 0.0") {
val g0 = gradient(doubleArrayOf(0.0, 0.0), ::parabola)
g0.size `should equal` 2
g0[0] `should be equal to` 0.0
}
it("its gradient at x=1 is ~2.0") {
val g1 = gradient(doubleArrayOf(1.0, 1.0), ::parabola)
}
it("its gradient at x=-1 is ~-2.0") {
val g1 = gradient(doubleArrayOf(-1.0, -1.0), ::parabola)
}
}
})

View File

@@ -0,0 +1,18 @@
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
object TestMinimize : Spek({
describe("a simple 1d function") {
fun parabola(x: DoubleArray): Double {
return (x[0]+1) * (x[0]+1)
}
it("it can be minimized") {
val result = minimize(doubleArrayOf(10.0), f = ::parabola)
println(result.solution[0])
}
}
})