All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.space.kscience.kmath.expressions.DifferentiableExpression.kt Maven / Gradle / Ivy

package space.kscience.kmath.expressions

/**
 * Represents expression which structure can be differentiated.
 *
 * @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> : Expression {
    /**
     * Differentiates this expression by ordered collection of [symbols].
     *
     * @param symbols the symbols.
     * @return the derivative or `null`.
     */
    public fun derivativeOrNull(symbols: List): R?
}

public fun > DifferentiableExpression.derivative(symbols: List): R =
    derivativeOrNull(symbols) ?: error("Derivative by symbols $symbols not provided")

public fun > DifferentiableExpression.derivative(vararg symbols: Symbol): R =
    derivative(symbols.toList())

public fun > DifferentiableExpression.derivative(name: String): R =
    derivative(StringSymbol(name))

/**
 * A [DifferentiableExpression] that defines only first derivatives
 */
public abstract class FirstDerivativeExpression> : DifferentiableExpression {
    /**
     * Returns first derivative of this expression by given [symbol].
     */
    public abstract fun derivativeOrNull(symbol: Symbol): R?

    public final override fun derivativeOrNull(symbols: List): R? {
        val dSymbol = symbols.firstOrNull() ?: return null
        return derivativeOrNull(dSymbol)
    }
}

/**
 * A factory that converts an expression in autodiff variables to a [DifferentiableExpression]
 */
public fun interface AutoDiffProcessor, out R : Expression> {
    public fun process(function: A.() -> I): DifferentiableExpression
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy