forked from kscience/kmath
Merge branch 'dev' into feature/advanced-optimization
# Conflicts: # kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimization.kt # kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/cmFit.kt # kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DifferentiableExpression.kt # kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt # kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/XYFit.kt
This commit is contained in:
commit
f84f7f8e45
@ -45,6 +45,7 @@
|
||||
- MSTExpression
|
||||
- Expression algebra builders
|
||||
- Complex and Quaternion no longer are elements.
|
||||
- Second generic from DifferentiableExpression
|
||||
|
||||
### Fixed
|
||||
- Ring inherits RingOperations, not GroupOperations
|
||||
|
@ -106,7 +106,7 @@ public class DerivativeStructureField(
|
||||
|
||||
public companion object :
|
||||
AutoDiffProcessor<Double, DerivativeStructure, DerivativeStructureField, Expression<Double>> {
|
||||
public override fun process(function: DerivativeStructureField.() -> DerivativeStructure): DifferentiableExpression<Double, Expression<Double>> =
|
||||
public override fun process(function: DerivativeStructureField.() -> DerivativeStructure): DifferentiableExpression<Double> =
|
||||
DerivativeStructureExpression(function)
|
||||
}
|
||||
}
|
||||
@ -116,7 +116,7 @@ public class DerivativeStructureField(
|
||||
*/
|
||||
public class DerivativeStructureExpression(
|
||||
public val function: DerivativeStructureField.() -> DerivativeStructure,
|
||||
) : DifferentiableExpression<Double, Expression<Double>> {
|
||||
) : DifferentiableExpression<Double> {
|
||||
public override operator fun invoke(arguments: Map<Symbol, Double>): Double =
|
||||
DerivativeStructureField(0, arguments).function().value
|
||||
|
||||
|
@ -15,6 +15,7 @@ import space.kscience.kmath.expressions.derivative
|
||||
import space.kscience.kmath.expressions.withSymbols
|
||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
||||
import space.kscience.kmath.optimization.*
|
||||
import kotlin.collections.set
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
public operator fun PointValuePair.component1(): DoubleArray = point
|
||||
|
@ -22,7 +22,7 @@ public fun FunctionOptimization.Companion.chiSquaredExpression(
|
||||
y: Buffer<Double>,
|
||||
yErr: Buffer<Double>,
|
||||
model: DerivativeStructureField.(x: DerivativeStructure) -> DerivativeStructure,
|
||||
): DifferentiableExpression<Double, Expression<Double>> = chiSquaredExpression(DerivativeStructureField, x, y, yErr, model)
|
||||
): DifferentiableExpression<Double> = chiSquaredExpression(DerivativeStructureField, x, y, yErr, model)
|
||||
|
||||
/**
|
||||
* Generate a chi squared expression from given x-y-sigma data and inline model. Provides automatic differentiation
|
||||
@ -32,7 +32,7 @@ public fun FunctionOptimization.Companion.chiSquaredExpression(
|
||||
y: Iterable<Double>,
|
||||
yErr: Iterable<Double>,
|
||||
model: DerivativeStructureField.(x: DerivativeStructure) -> DerivativeStructure,
|
||||
): DifferentiableExpression<Double, Expression<Double>> = chiSquaredExpression(
|
||||
): DifferentiableExpression<Double> = chiSquaredExpression(
|
||||
DerivativeStructureField,
|
||||
x.toList().asBuffer(),
|
||||
y.toList().asBuffer(),
|
||||
@ -56,12 +56,12 @@ public suspend fun Expression<Double>.optimize(
|
||||
/**
|
||||
* Optimize differentiable expression
|
||||
*/
|
||||
public suspend fun DifferentiableExpression<Double, Expression<Double>>.optimize(
|
||||
public suspend fun DifferentiableExpression<Double>.optimize(
|
||||
vararg symbols: Symbol,
|
||||
configuration: CMOptimization.() -> Unit,
|
||||
): OptimizationResult<Double> = optimizeWith(CMOptimization, symbols = symbols, configuration)
|
||||
|
||||
public suspend fun DifferentiableExpression<Double, Expression<Double>>.minimize(
|
||||
public suspend fun DifferentiableExpression<Double>.minimize(
|
||||
vararg startPoint: Pair<Symbol, Double>,
|
||||
configuration: CMOptimization.() -> Unit = {},
|
||||
): OptimizationResult<Double> {
|
||||
|
@ -11,35 +11,51 @@ package space.kscience.kmath.expressions
|
||||
* @param T the type this expression takes as argument and returns.
|
||||
* @param R the type of expression this expression can be differentiated to.
|
||||
*/
|
||||
public interface DifferentiableExpression<T, out R : Expression<T>> : Expression<T> {
|
||||
public interface DifferentiableExpression<T> : Expression<T> {
|
||||
/**
|
||||
* Differentiates this expression by ordered collection of [symbols].
|
||||
*
|
||||
* @param symbols the symbols.
|
||||
* @return the derivative or `null`.
|
||||
*/
|
||||
public fun derivativeOrNull(symbols: List<Symbol>): R?
|
||||
public fun derivativeOrNull(symbols: List<Symbol>): Expression<T>?
|
||||
}
|
||||
|
||||
public fun <T, R : Expression<T>> DifferentiableExpression<T, R>.derivative(symbols: List<Symbol>): R =
|
||||
public fun <T> DifferentiableExpression<T>.derivative(symbols: List<Symbol>): Expression<T> =
|
||||
derivativeOrNull(symbols) ?: error("Derivative by symbols $symbols not provided")
|
||||
|
||||
public fun <T, R : Expression<T>> DifferentiableExpression<T, R>.derivative(vararg symbols: Symbol): R =
|
||||
public fun <T> DifferentiableExpression<T>.derivative(vararg symbols: Symbol): Expression<T> =
|
||||
derivative(symbols.toList())
|
||||
|
||||
public fun <T, R : Expression<T>> DifferentiableExpression<T, R>.derivative(name: String): R =
|
||||
public fun <T> DifferentiableExpression<T>.derivative(name: String): Expression<T> =
|
||||
derivative(StringSymbol(name))
|
||||
|
||||
/**
|
||||
* A special type of [DifferentiableExpression] which returns typed expressions as derivatives
|
||||
*/
|
||||
public interface SpecialDifferentiableExpression<T, R: Expression<T>>: DifferentiableExpression<T> {
|
||||
override fun derivativeOrNull(symbols: List<Symbol>): R?
|
||||
}
|
||||
|
||||
public fun <T, R : Expression<T>> SpecialDifferentiableExpression<T, R>.derivative(symbols: List<Symbol>): R =
|
||||
derivativeOrNull(symbols) ?: error("Derivative by symbols $symbols not provided")
|
||||
|
||||
public fun <T, R : Expression<T>> SpecialDifferentiableExpression<T, R>.derivative(vararg symbols: Symbol): R =
|
||||
derivative(symbols.toList())
|
||||
|
||||
public fun <T, R : Expression<T>> SpecialDifferentiableExpression<T, R>.derivative(name: String): R =
|
||||
derivative(StringSymbol(name))
|
||||
|
||||
/**
|
||||
* A [DifferentiableExpression] that defines only first derivatives
|
||||
*/
|
||||
public abstract class FirstDerivativeExpression<T, R : Expression<T>> : DifferentiableExpression<T, R> {
|
||||
public abstract class FirstDerivativeExpression<T> : DifferentiableExpression<T> {
|
||||
/**
|
||||
* Returns first derivative of this expression by given [symbol].
|
||||
*/
|
||||
public abstract fun derivativeOrNull(symbol: Symbol): R?
|
||||
public abstract fun derivativeOrNull(symbol: Symbol): Expression<T>?
|
||||
|
||||
public final override fun derivativeOrNull(symbols: List<Symbol>): R? {
|
||||
public final override fun derivativeOrNull(symbols: List<Symbol>): Expression<T>? {
|
||||
val dSymbol = symbols.firstOrNull() ?: return null
|
||||
return derivativeOrNull(dSymbol)
|
||||
}
|
||||
@ -49,5 +65,5 @@ public abstract class FirstDerivativeExpression<T, R : Expression<T>> : Differen
|
||||
* A factory that converts an expression in autodiff variables to a [DifferentiableExpression]
|
||||
*/
|
||||
public fun interface AutoDiffProcessor<T, I, A : ExpressionAlgebra<T, I>, out R : Expression<T>> {
|
||||
public fun process(function: A.() -> I): DifferentiableExpression<T, R>
|
||||
public fun process(function: A.() -> I): DifferentiableExpression<T>
|
||||
}
|
||||
|
@ -232,7 +232,7 @@ public fun <T : Any, F : Field<T>> F.simpleAutoDiff(
|
||||
public class SimpleAutoDiffExpression<T : Any, F : Field<T>>(
|
||||
public val field: F,
|
||||
public val function: SimpleAutoDiffField<T, F>.() -> AutoDiffValue<T>,
|
||||
) : FirstDerivativeExpression<T, Expression<T>>() {
|
||||
) : FirstDerivativeExpression<T>() {
|
||||
public override operator fun invoke(arguments: Map<Symbol, T>): T {
|
||||
//val bindings = arguments.entries.map { it.key.bind(it.value) }
|
||||
return SimpleAutoDiffField(field, arguments).function().value
|
||||
|
@ -24,7 +24,7 @@ import space.kscience.kmath.operations.NumericAlgebra
|
||||
public class KotlingradExpression<T : Number, A : NumericAlgebra<T>>(
|
||||
public val algebra: A,
|
||||
public val mst: MST,
|
||||
) : DifferentiableExpression<T, KotlingradExpression<T, A>> {
|
||||
) : SpecialDifferentiableExpression<T, KotlingradExpression<T, A>> {
|
||||
public override fun invoke(arguments: Map<Symbol, T>): T = mst.interpret(algebra, arguments)
|
||||
|
||||
public override fun derivativeOrNull(symbols: List<Symbol>): KotlingradExpression<T, A> =
|
||||
|
@ -22,7 +22,7 @@ public enum class FunctionOptimizationTarget : OptimizationFeature {
|
||||
|
||||
public class FunctionOptimization<T>(
|
||||
override val features: FeatureSet<OptimizationFeature>,
|
||||
public val expression: DifferentiableExpression<T, Expression<T>>,
|
||||
public val expression: DifferentiableExpression<T>,
|
||||
) : OptimizationProblem{
|
||||
|
||||
public companion object{
|
||||
@ -35,7 +35,7 @@ public class FunctionOptimization<T>(
|
||||
y: Buffer<T>,
|
||||
yErr: Buffer<T>,
|
||||
model: A.(I) -> I,
|
||||
): DifferentiableExpression<T, Expression<T>> where A : ExtendedField<I>, A : ExpressionAlgebra<T, I> {
|
||||
): DifferentiableExpression<T> where A : ExtendedField<I>, A : ExpressionAlgebra<T, I> {
|
||||
require(x.size == y.size) { "X and y buffers should be of the same size" }
|
||||
require(y.size == yErr.size) { "Y and yErr buffer should of the same size" }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user