Dev #127
@ -43,7 +43,7 @@ sealed class MST {
|
|||||||
fun <T> NumericAlgebra<T>.evaluate(node: MST): T {
|
fun <T> NumericAlgebra<T>.evaluate(node: MST): T {
|
||||||
return when (node) {
|
return when (node) {
|
||||||
is MST.Numeric -> number(node.value)
|
is MST.Numeric -> number(node.value)
|
||||||
is MST.Singular -> raw(node.value)
|
is MST.Singular -> symbol(node.value)
|
||||||
is MST.Unary -> unaryOperation(node.operation, evaluate(node.value))
|
is MST.Unary -> unaryOperation(node.operation, evaluate(node.value))
|
||||||
is MST.Binary -> when {
|
is MST.Binary -> when {
|
||||||
node.left is MST.Numeric && node.right is MST.Numeric -> {
|
node.left is MST.Numeric && node.right is MST.Numeric -> {
|
||||||
|
@ -12,7 +12,7 @@ class MSTExpression<T>(val algebra: NumericAlgebra<T>, val mst: MST) : Expressio
|
|||||||
* Substitute algebra raw value
|
* Substitute algebra raw value
|
||||||
*/
|
*/
|
||||||
private inner class InnerAlgebra(val arguments: Map<String, T>) : NumericAlgebra<T> by algebra {
|
private inner class InnerAlgebra(val arguments: Map<String, T>) : NumericAlgebra<T> by algebra {
|
||||||
override fun raw(value: String): T = arguments[value] ?: super.raw(value)
|
override fun symbol(value: String): T = arguments[value] ?: super.symbol(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun invoke(arguments: Map<String, T>): T = InnerAlgebra(arguments).evaluate(mst)
|
override fun invoke(arguments: Map<String, T>): T = InnerAlgebra(arguments).evaluate(mst)
|
||||||
|
@ -10,7 +10,10 @@ interface Algebra<T> {
|
|||||||
/**
|
/**
|
||||||
* Wrap raw string or variable
|
* Wrap raw string or variable
|
||||||
*/
|
*/
|
||||||
fun raw(value: String): T = error("Wrapping of '$value' is not supported in $this")
|
fun symbol(value: String): T = error("Wrapping of '$value' is not supported in $this")
|
||||||
|
|
||||||
|
@Deprecated("Symbol is more concise",replaceWith = ReplaceWith("symbol"))
|
||||||
|
fun raw(value: String): T = symbol(value)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dynamic call of unary operation with name [operation] on [arg]
|
* Dynamic call of unary operation with name [operation] on [arg]
|
||||||
|
@ -51,10 +51,10 @@ object ComplexField : ExtendedField<Complex> {
|
|||||||
|
|
||||||
operator fun Double.times(c: Complex) = Complex(c.re * this, c.im * this)
|
operator fun Double.times(c: Complex) = Complex(c.re * this, c.im * this)
|
||||||
|
|
||||||
override fun raw(value: String): Complex = if (value == "i") {
|
override fun symbol(value: String): Complex = if (value == "i") {
|
||||||
i
|
i
|
||||||
} else {
|
} else {
|
||||||
super.raw(value)
|
super.symbol(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user