From 895f788f756f1a2bbacc17c3f117b69c2c9853fd Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Sun, 9 Aug 2020 23:43:38 +0700 Subject: [PATCH] Cleanup doc mistakes, fix number fields corner cases --- .../scientifik/kmath/operations/Algebra.kt | 6 ++---- .../kmath/operations/NumberAlgebra.kt | 11 +++++++++++ .../kmath/operations/OptionalOperations.kt | 18 +++++++++++------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt index eb44cf7a7..9abd43c8f 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt @@ -58,12 +58,10 @@ interface NumericAlgebra : Algebra { inline operator fun , R> A.invoke(block: A.() -> R): R = run(block) /** - * Represents semispace, i.e. algebraic structure with associative binary operation called "addition" as well as + * Represents "semispace", i.e. algebraic structure with associative binary operation called "addition" as well as * multiplication by scalars. * - * In KMath groups are called spaces, and also define multiplication of element by [Number]. - * - * @param T the type of element of this semigroup. + * @param T the type of element of this "semispace". */ interface SpaceOperations : Algebra { /** diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt index 98cf912ac..0735a96da 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt @@ -1,5 +1,6 @@ package scientifik.kmath.operations +import scientifik.kmath.operations.RealField.pow import kotlin.math.abs import kotlin.math.pow as kpow @@ -83,6 +84,11 @@ object RealField : ExtendedField, Norm { override val one: Double get() = 1.0 + override fun binaryOperation(operation: String, left: Double, right: Double): Double = when (operation) { + PowerOperations.POW_OPERATION -> left pow right + else -> super.binaryOperation(operation, left, right) + } + override inline fun add(a: Double, b: Double): Double = a + b override inline fun multiply(a: Double, k: Number): Double = a * k.toDouble() @@ -128,6 +134,11 @@ object FloatField : ExtendedField, Norm { override val one: Float get() = 1.0f + override fun binaryOperation(operation: String, left: Float, right: Float): Float = when (operation) { + PowerOperations.POW_OPERATION -> left pow right + else -> super.binaryOperation(operation, left, right) + } + override inline fun add(a: Float, b: Float): Float = a + b override inline fun multiply(a: Float, k: Number): Float = a * k.toFloat() diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/OptionalOperations.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/OptionalOperations.kt index e19acea3d..9d901746a 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/OptionalOperations.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/OptionalOperations.kt @@ -1,10 +1,9 @@ package scientifik.kmath.operations /** - * A container for trigonometric operations for specific type. They are limited to semifields. + * A container for trigonometric operations for specific type. * - * The operations are not exposed to class directly to avoid method bloat but instead are declared in the field. - * It also allows to override behavior for optional operations. + * @param T the type of element of this structure. */ interface TrigonometricOperations : Algebra { /** @@ -101,11 +100,9 @@ fun >> acos(arg: T): T = arg.cont fun >> atan(arg: T): T = arg.context.atan(arg) /** - * A container for hyperbolic trigonometric operations for specific type. Trigonometric operations are limited to - * fields. + * A container for hyperbolic trigonometric operations for specific type. * - * The operations are not exposed to class directly to avoid method bloat but instead are declared in the field. It - * also allows to override behavior for optional operations. + * @param T the type of element of this structure. */ interface HyperbolicOperations : Algebra { /** @@ -203,6 +200,8 @@ fun >> atanh(arg: T): T = arg.contex /** * A context extension to include power operations based on exponentiation. + * + * @param T the type of element of this structure. */ interface PowerOperations : Algebra { /** @@ -254,6 +253,8 @@ fun >> sqr(arg: T): T = arg pow 2.0 /** * A container for operations related to `exp` and `ln` functions. + * + * @param T the type of element of this structure. */ interface ExponentialOperations : Algebra { /** @@ -291,6 +292,9 @@ fun >> ln(arg: T): T = arg.context. /** * A container for norm functional on element. + * + * @param T the type of element having norm defined. + * @param R the type of norm. */ interface Norm { /**