forked from kscience/kmath
add gradient example
This commit is contained in:
parent
0cc68e72ce
commit
5a568c4587
@ -0,0 +1,28 @@
|
||||
package space.kscience.kmath.linear
|
||||
|
||||
import space.kscience.kmath.real.*
|
||||
import space.kscience.kmath.structures.RealBuffer
|
||||
|
||||
fun main() {
|
||||
val x0 = Vector(0.0, 0.0, 0.0)
|
||||
val sigma = Vector(1.0, 1.0, 1.0)
|
||||
|
||||
val gaussian: (Vector<Double>) -> Double = { x ->
|
||||
require(x.size == x0.size)
|
||||
kotlin.math.exp(-((x - x0) / sigma).square().sum())
|
||||
}
|
||||
|
||||
fun ((Vector<Double>) -> Double).grad(x: Vector<Double>): Vector<Double> {
|
||||
require(x.size == x0.size)
|
||||
return RealBuffer(x.size) { i ->
|
||||
val h = sigma[i] / 5
|
||||
val dVector = RealBuffer(x.size) { if (it == i) h else 0.0 }
|
||||
val f1 = invoke(x + dVector / 2)
|
||||
val f0 = invoke(x - dVector / 2)
|
||||
(f1 - f0) / h
|
||||
}
|
||||
}
|
||||
|
||||
println(gaussian.grad(x0))
|
||||
|
||||
}
|
@ -432,6 +432,25 @@ public final class space/kscience/kmath/expressions/SymbolIndexerKt {
|
||||
public static final fun withSymbols ([Lspace/kscience/kmath/expressions/Symbol;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
}
|
||||
|
||||
public final class space/kscience/kmath/linear/BufferLinearSpace : space/kscience/kmath/linear/LinearSpace {
|
||||
public fun <init> (Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;)V
|
||||
public fun buildMatrix (IILkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun buildVector (ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer;
|
||||
public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer;
|
||||
public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Ring;
|
||||
public fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun minus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer;
|
||||
public fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun plus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer;
|
||||
public fun times (Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun times (Ljava/lang/Object;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer;
|
||||
public fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun times (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Object;)Lspace/kscience/kmath/structures/Buffer;
|
||||
public fun unaryMinus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun unaryMinus (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer;
|
||||
}
|
||||
|
||||
public abstract interface class space/kscience/kmath/linear/CholeskyDecompositionFeature : space/kscience/kmath/linear/MatrixFeature {
|
||||
public abstract fun getL ()Lspace/kscience/kmath/nd/Structure2D;
|
||||
}
|
||||
@ -514,25 +533,6 @@ public final class space/kscience/kmath/linear/LinearSpaceKt {
|
||||
public static final fun invoke (Lspace/kscience/kmath/linear/LinearSpace;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
}
|
||||
|
||||
public final class space/kscience/kmath/linear/LinearSpaceOverNd : space/kscience/kmath/linear/LinearSpace {
|
||||
public fun <init> (Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;)V
|
||||
public fun buildMatrix (IILkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun buildVector (ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer;
|
||||
public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer;
|
||||
public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Ring;
|
||||
public fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun minus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer;
|
||||
public fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun plus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer;
|
||||
public fun times (Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun times (Ljava/lang/Object;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer;
|
||||
public fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun times (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Object;)Lspace/kscience/kmath/structures/Buffer;
|
||||
public fun unaryMinus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D;
|
||||
public fun unaryMinus (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer;
|
||||
}
|
||||
|
||||
public final class space/kscience/kmath/linear/LupDecomposition : space/kscience/kmath/linear/DeterminantFeature, space/kscience/kmath/linear/LupDecompositionFeature {
|
||||
public fun <init> (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/operations/Field;Lspace/kscience/kmath/nd/Structure2D;[IZ)V
|
||||
public final fun getContext ()Lspace/kscience/kmath/linear/LinearSpace;
|
||||
|
@ -5,6 +5,7 @@ import space.kscience.kmath.operations.Norm
|
||||
import space.kscience.kmath.structures.Buffer
|
||||
import space.kscience.kmath.structures.asBuffer
|
||||
import space.kscience.kmath.structures.asIterable
|
||||
import space.kscience.kmath.structures.indices
|
||||
import kotlin.math.pow
|
||||
import kotlin.math.sqrt
|
||||
|
||||
@ -30,7 +31,7 @@ public inline fun RealVector.mapIndexed(transform: (index: Int, value: Double) -
|
||||
Buffer.real(size) { transform(it, get(it)) }
|
||||
|
||||
public operator fun RealVector.plus(other: RealVector): RealVector {
|
||||
require(size == other.size){"Vector size $size expected but ${other.size} found"}
|
||||
require(size == other.size) { "Vector size $size expected but ${other.size} found" }
|
||||
return mapIndexed { index, value -> value + other[index] }
|
||||
}
|
||||
|
||||
@ -41,7 +42,7 @@ public operator fun Number.plus(vector: RealVector): RealVector = vector + this
|
||||
public operator fun RealVector.unaryMinus(): Buffer<Double> = map { -it }
|
||||
|
||||
public operator fun RealVector.minus(other: RealVector): RealVector {
|
||||
require(size == other.size){"Vector size $size expected but ${other.size} found"}
|
||||
require(size == other.size) { "Vector size $size expected but ${other.size} found" }
|
||||
return mapIndexed { index, value -> value - other[index] }
|
||||
}
|
||||
|
||||
@ -50,7 +51,7 @@ public operator fun RealVector.minus(number: Number): RealVector = map { it - nu
|
||||
public operator fun Number.minus(vector: RealVector): RealVector = vector.map { toDouble() - it }
|
||||
|
||||
public operator fun RealVector.times(other: RealVector): RealVector {
|
||||
require(size == other.size){"Vector size $size expected but ${other.size} found"}
|
||||
require(size == other.size) { "Vector size $size expected but ${other.size} found" }
|
||||
return mapIndexed { index, value -> value * other[index] }
|
||||
}
|
||||
|
||||
@ -59,7 +60,7 @@ public operator fun RealVector.times(number: Number): RealVector = map { it * nu
|
||||
public operator fun Number.times(vector: RealVector): RealVector = vector * this
|
||||
|
||||
public operator fun RealVector.div(other: RealVector): RealVector {
|
||||
require(size == other.size){"Vector size $size expected but ${other.size} found"}
|
||||
require(size == other.size) { "Vector size $size expected but ${other.size} found" }
|
||||
return mapIndexed { index, value -> value / other[index] }
|
||||
}
|
||||
|
||||
@ -88,3 +89,13 @@ public fun tan(vector: RealVector): RealVector = vector.map { kotlin.math.tan(it
|
||||
public fun ln(vector: RealVector): RealVector = vector.map { kotlin.math.ln(it) }
|
||||
|
||||
public fun log10(vector: RealVector): RealVector = vector.map { kotlin.math.log10(it) }
|
||||
|
||||
// reductions methods
|
||||
|
||||
public fun RealVector.sum(): Double {
|
||||
var res = 0.0
|
||||
for (i in indices) {
|
||||
res += get(i)
|
||||
}
|
||||
return res
|
||||
}
|
Loading…
Reference in New Issue
Block a user