breandan
10/29/2020, 4:11 AMassertEquals(f.derivativeOrNull(mapOf(x to 2)), f.derivativeOrNull(mapOf(x to 1)).derivativeOrNull(mapOf(x to 1)))
. This helps to clarify what is meant by derivativeOrNull(orders: Map<Symbol, Int>): Expression<T>
and suggests one possible implementation. Assuming you only care about SFun
and SVar
then it should be possible to define derivativeOrNull
using fold
, noting the above restrictions on evaluation order.
More generally, due to the semantics of mixing partial derivatives, depending how Map
is implemented, you cannot assume f.derivativeOrNull(mapOf(y to 1)).derivativeOrNull(mapOf(x to 1)) == f.derivativeOrNull(mapOf(x to 1, y to 1))
without knowing more about the function under differentiation. To implement derivativeOrNull
correctly, you would need a data structure whose traversal order preserves the semantics of the end-user program. Although it is possible to implement something like OrderPreservingMap
, I would just use List<Symbol>
. Then your implementation becomes cleaner:
public override fun derivativeOrNull(orders: List<Symbol>): Expression<T> =
orders.map { MstAlgebra.symbol(it.identity).toSVar(proto) }
.fold(mst.toSFun(proto)) { result, sVar -> result.d(sVar) } // This will produce Derivative(...Derivative(mst.toSFun(proto))...)
.invoke() // Do you want to evaluate it?
.toKMathExpression()
Iaroslav Postovalov
10/29/2020, 4:45 AMbreandan
10/29/2020, 5:05 AMderivativeOrNull
. After thinking about it, I think maybe you want orders
to be List<Symbol>
breandan
10/29/2020, 5:09 AMbreandan
10/29/2020, 5:15 AMIaroslav Postovalov
10/29/2020, 6:27 AMIaroslav Postovalov
10/29/2020, 6:27 AMderivativeOrNull
? @breandan’s List<Symbol> input looks clearer, at least for KG integrationaltavir
10/29/2020, 6:31 AMbreandan
10/29/2020, 6:36 AMMap
is implemented but is ambiguous without additional structureIaroslav Postovalov
10/29/2020, 6:37 AMaltavir
10/29/2020, 6:44 AM