Add adapters of scalar functions to MST and vice versa #150
@ -8,8 +8,8 @@ import kscience.kmath.operations.*
|
|||||||
|
|||||||
/**
|
/**
|
||||||
* Maps [SFun] objects to [MST]. Some unsupported operations like [Derivative] are bound and converted then.
|
* Maps [SFun] objects to [MST]. Some unsupported operations like [Derivative] are bound and converted then.
|
||||||
*
|
*
|
||||||
* @receiver a scalar function.
|
* @receiver the scalar function.
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
* @return the [MST].
|
* @return a node.
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
*/
|
*/
|
||||||
public fun <X : SFun<X>> SFun<X>.mst(): MST = MstExtendedField {
|
public fun <X : SFun<X>> SFun<X>.mst(): MST = MstExtendedField {
|
||||||
when (this@mst) {
|
when (this@mst) {
|
||||||
@ -30,36 +30,52 @@ public fun <X : SFun<X>> SFun<X>.mst(): MST = MstExtendedField {
|
|||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
change to toMst() change to toMst()
|
|||||||
|
* Maps [MST.Numeric] to [SConst] directly.
|
||||||
change to toMst() change to toMst()
|
|||||||
|
*
|
||||||
change to toMst() change to toMst()
|
|||||||
|
* @receiver the node.
|
||||||
change to toMst() change to toMst()
|
|||||||
|
* @return a new constant.
|
||||||
change to toMst() change to toMst()
|
|||||||
|
*/
|
||||||
change to toMst() change to toMst()
|
|||||||
|
public fun <X : SFun<X>> MST.Numeric.sconst(): SConst<X> = SConst(value)
|
||||||
change to toMst() change to toMst()
|
|||||||
|
|
||||||
change to toMst() change to toMst()
|
|||||||
|
/**
|
||||||
change to toMst() change to toMst()
|
|||||||
|
* Maps [MST.Symbolic] to [SVar] directly.
|
||||||
change to toMst() change to toMst()
|
|||||||
|
*
|
||||||
change to toMst() change to toMst()
|
|||||||
|
* @receiver the node.
|
||||||
change to toMst() change to toMst()
|
|||||||
|
* @param proto the prototype instance.
|
||||||
change to toMst() change to toMst()
|
|||||||
|
* @return a new variable.
|
||||||
change to toMst() change to toMst()
|
|||||||
|
*/
|
||||||
change to toMst() change to toMst()
|
|||||||
|
public fun <X : SFun<X>> MST.Symbolic.svar(proto: X): SVar<X> = SVar(proto, value)
|
||||||
change to toMst() change to toMst()
|
|||||||
|
|
||||||
change to toMst() change to toMst()
|
|||||||
/**
|
/**
|
||||||
* Maps [MST] objects to [SFun]. Unsupported operations throw [IllegalStateException].
|
* Maps [MST] objects to [SFun]. Unsupported operations throw [IllegalStateException].
|
||||||
*
|
*
|
||||||
* @receiver an [MST].
|
* @receiver the node.
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
* @return the scalar function.
|
* @param proto the prototype instance.
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
|
* @return a scalar function.
|
||||||
change to toMst() change to toMst()
|
|||||||
*/
|
*/
|
||||||
public fun <X : SFun<X>> MST.sfun(proto: X): SFun<X> {
|
public fun <X : SFun<X>> MST.sfun(proto: X): SFun<X> = when (this) {
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
return when (this) {
|
is MST.Numeric -> sconst()
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
is MST.Numeric -> SConst(value)
|
is MST.Symbolic -> svar(proto)
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
is MST.Symbolic -> SVar(proto, value)
|
|
||||||
change to toMst() change to toMst()
|
|||||||
|
|
||||||
is MST.Unary -> when (operation) {
|
is MST.Unary -> when (operation) {
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
SpaceOperations.PLUS_OPERATION -> value.sfun(proto)
|
SpaceOperations.PLUS_OPERATION -> value.sfun(proto)
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
SpaceOperations.MINUS_OPERATION -> Negative(value.sfun(proto))
|
SpaceOperations.MINUS_OPERATION -> Negative(value.sfun(proto))
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
TrigonometricOperations.SIN_OPERATION -> Sine(value.sfun(proto))
|
TrigonometricOperations.SIN_OPERATION -> Sine(value.sfun(proto))
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
TrigonometricOperations.COS_OPERATION -> Cosine(value.sfun(proto))
|
TrigonometricOperations.COS_OPERATION -> Cosine(value.sfun(proto))
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
TrigonometricOperations.TAN_OPERATION -> Tangent(value.sfun(proto))
|
TrigonometricOperations.TAN_OPERATION -> Tangent(value.sfun(proto))
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
PowerOperations.SQRT_OPERATION -> Power(value.sfun(proto), SConst(0.5))
|
PowerOperations.SQRT_OPERATION -> Power(value.sfun(proto), SConst(0.5))
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
ExponentialOperations.EXP_OPERATION -> Power(value.sfun(proto), E())
|
ExponentialOperations.EXP_OPERATION -> Power(value.sfun(proto), E())
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
ExponentialOperations.LN_OPERATION -> Log(value.sfun(proto))
|
ExponentialOperations.LN_OPERATION -> Log(value.sfun(proto))
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
else -> error("Unary operation $operation not defined in $this")
|
else -> error("Unary operation $operation not defined in $this")
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
}
|
}
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
|
|
||||||
is MST.Binary -> when (operation) {
|
is MST.Binary -> when (operation) {
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
SpaceOperations.PLUS_OPERATION -> Sum(left.sfun(proto), right.sfun(proto))
|
SpaceOperations.PLUS_OPERATION -> Sum(left.sfun(proto), right.sfun(proto))
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
SpaceOperations.MINUS_OPERATION -> Sum(left.sfun(proto), Negative(right.sfun(proto)))
|
SpaceOperations.MINUS_OPERATION -> Sum(left.sfun(proto), Negative(right.sfun(proto)))
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
RingOperations.TIMES_OPERATION -> Prod(left.sfun(proto), right.sfun(proto))
|
RingOperations.TIMES_OPERATION -> Prod(left.sfun(proto), right.sfun(proto))
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
FieldOperations.DIV_OPERATION -> Prod(left.sfun(proto), Power(right.sfun(proto), Negative(One())))
|
FieldOperations.DIV_OPERATION -> Prod(left.sfun(proto), Power(right.sfun(proto), Negative(One())))
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
PowerOperations.POW_OPERATION -> Power(left.sfun(proto), SConst((right as MST.Numeric).value))
|
PowerOperations.POW_OPERATION -> Power(left.sfun(proto), SConst((right as MST.Numeric).value))
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
else -> error("Binary operation $operation not defined in $this")
|
else -> error("Binary operation $operation not defined in $this")
|
||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
|||||||
}
|
|
||||||
change to toMst() change to toMst()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
change to toMst() change to toMst()
change to toMst() change to toMst()
|
change to toMst()
change to toMst()