Improve consistency of operations in MstAlgebra

This commit is contained in:
Iaroslav Postovalov 2020-12-22 13:42:12 +07:00
parent d521fa0d8d
commit 2310aca9db
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
3 changed files with 18 additions and 11 deletions

View File

@ -25,7 +25,9 @@ public object MstSpace : Space<MST>, NumericAlgebra<MST> {
public override fun number(value: Number): MST.Numeric = MstAlgebra.number(value)
public override fun symbol(value: String): MST.Symbolic = MstAlgebra.symbol(value)
public override fun add(a: MST, b: MST): MST.Binary = binaryOperation(SpaceOperations.PLUS_OPERATION)(a, b)
public override fun MST.unaryMinus(): MST = unaryOperation(SpaceOperations.MINUS_OPERATION)(this)
public override operator fun MST.unaryPlus(): MST = unaryOperation(SpaceOperations.PLUS_OPERATION)(this)
public override operator fun MST.unaryMinus(): MST = unaryOperation(SpaceOperations.MINUS_OPERATION)(this)
public override operator fun MST.minus(b: MST): MST = binaryOperation(SpaceOperations.MINUS_OPERATION)(this, b)
public override fun multiply(a: MST, k: Number): MST.Binary =
binaryOperation(RingOperations.TIMES_OPERATION)(a, number(k))
@ -41,17 +43,19 @@ public object MstSpace : Space<MST>, NumericAlgebra<MST> {
* [Ring] over [MST] nodes.
*/
public object MstRing : Ring<MST>, NumericAlgebra<MST> {
override val zero: MST.Numeric
public override val zero: MST.Numeric
get() = MstSpace.zero
override val one: MST.Numeric by lazy { number(1.0) }
public override val one: MST.Numeric by lazy { number(1.0) }
public override fun number(value: Number): MST.Numeric = MstSpace.number(value)
public override fun symbol(value: String): MST.Symbolic = MstSpace.symbol(value)
public override fun add(a: MST, b: MST): MST.Binary = MstSpace.add(a, b)
public override fun multiply(a: MST, k: Number): MST.Binary = MstSpace.multiply(a, k)
public override fun multiply(a: MST, b: MST): MST.Binary = binaryOperation(RingOperations.TIMES_OPERATION)(a, b)
public override fun MST.unaryMinus(): MST = MstSpace.unaryOperation(SpaceOperations.MINUS_OPERATION)(this)
public override operator fun MST.unaryPlus(): MST = MstSpace { this@unaryPlus }
public override operator fun MST.unaryMinus(): MST = MstSpace { -this@unaryMinus }
public override operator fun MST.minus(b: MST): MST = MstSpace { this@minus - b }
public override fun binaryOperation(operation: String): (left: MST, right: MST) -> MST.Binary =
MstSpace.binaryOperation(operation)
@ -76,7 +80,9 @@ public object MstField : Field<MST> {
public override fun multiply(a: MST, k: Number): MST.Binary = MstRing.multiply(a, k)
public override fun multiply(a: MST, b: MST): MST.Binary = MstRing.multiply(a, b)
public override fun divide(a: MST, b: MST): MST.Binary = binaryOperation(FieldOperations.DIV_OPERATION)(a, b)
public override fun MST.unaryMinus(): MST = MstSpace.unaryOperation(SpaceOperations.MINUS_OPERATION)(this)
public override operator fun MST.unaryPlus(): MST = MstRing { this@unaryPlus }
public override operator fun MST.unaryMinus(): MST = MstRing { -this@unaryMinus }
public override operator fun MST.minus(b: MST): MST = MstRing { this@minus - b }
public override fun binaryOperation(operation: String): (left: MST, right: MST) -> MST.Binary =
MstRing.binaryOperation(operation)
@ -111,7 +117,9 @@ public object MstExtendedField : ExtendedField<MST> {
public override fun multiply(a: MST, k: Number): MST.Binary = MstField.multiply(a, k)
public override fun multiply(a: MST, b: MST): MST.Binary = MstField.multiply(a, b)
public override fun divide(a: MST, b: MST): MST.Binary = MstField.divide(a, b)
public override fun MST.unaryMinus(): MST = MstSpace.unaryOperation(SpaceOperations.MINUS_OPERATION)(this)
public override operator fun MST.unaryPlus(): MST = MstField { this@unaryPlus }
public override operator fun MST.unaryMinus(): MST = MstField { -this@unaryMinus }
public override operator fun MST.minus(b: MST): MST = MstField { this@minus - b }
public override fun power(arg: MST, pow: Number): MST.Binary =
binaryOperation(PowerOperations.POW_OPERATION)(arg, number(pow))

View File

@ -275,12 +275,12 @@ public interface SpaceOperations<T> : Algebra<T> {
public companion object {
/**
* The identifier of addition.
* The identifier of addition and unary positive.
*/
public const val PLUS_OPERATION: String = "+"
/**
* The identifier of subtraction (and negation).
* The identifier of subtraction and unary negation.
*/
public const val MINUS_OPERATION: String = "-"
}

View File

@ -1,6 +1,5 @@
package kscience.kmath.operations
import kotlin.math.abs
import kotlin.math.pow as kpow
/**
@ -249,10 +248,10 @@ public object ByteRing : Ring<Byte>, Norm<Byte, Byte> {
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
public object LongRing : Ring<Long>, Norm<Long, Long> {
public override val zero: Long
get() = 0
get() = 0L
public override val one: Long
get() = 1
get() = 1L
public override inline fun add(a: Long, b: Long): Long = a + b
public override inline fun multiply(a: Long, k: Number): Long = a * k.toLong()