From 546d56aeee3615d66fcbfda22d7e27329eb711ca Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 19 Sep 2021 21:13:07 +0300 Subject: [PATCH] Refactor buffer builders to suit new discoverability pattern --- CHANGELOG.md | 1 + .../space/kscience/kmath/operations/ComplexDemo.kt | 4 ++-- .../kotlin/space/kscience/kmath/complex/Complex.kt | 2 ++ .../kotlin/space/kscience/kmath/operations/BigInt.kt | 10 ++++++++++ .../space/kscience/kmath/operations/numbers.kt | 12 ++++++++++++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec3f5252a..19b7dae32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ - Rename `DifferentiableMstExpression` to `KotlingradExpression` - `FeatureSet` now accepts only `Feature`. It is possible to override keys and use interfaces. - Use `Symbol` factory function instead of `StringSymbol` +- New discoverability pattern: `.algebra.` ### Deprecated - Specialized `DoubleBufferAlgebra` diff --git a/examples/src/main/kotlin/space/kscience/kmath/operations/ComplexDemo.kt b/examples/src/main/kotlin/space/kscience/kmath/operations/ComplexDemo.kt index 8b511f961..743f05b13 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/operations/ComplexDemo.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/operations/ComplexDemo.kt @@ -7,13 +7,13 @@ package space.kscience.kmath.operations import space.kscience.kmath.complex.Complex import space.kscience.kmath.complex.ComplexField +import space.kscience.kmath.complex.nd import space.kscience.kmath.complex.withNd import space.kscience.kmath.nd.StructureND -import space.kscience.kmath.nd.autoNd fun main() { // 2d element - val element = ComplexField.autoNd(2, 2).produce { (i, j) -> + val element = ComplexField.nd(2, 2).produce { (i, j) -> Complex(i - j, i + j) } println(element) diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt index 08bd12205..793587492 100644 --- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt +++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt @@ -203,6 +203,8 @@ public data class Complex(val re: Double, val im: Double) { } } +public val Complex.Companion.algebra: ComplexField get() = ComplexField + /** * Creates a complex number with real part equal to this real. diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt index 02741e6ab..9797027c7 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt @@ -526,11 +526,21 @@ public fun String.parseBigInteger(): BigInt? { } } +public val BigInt.algebra: BigIntField get() = BigIntField + +@Deprecated("Use BigInt::buffer") public inline fun Buffer.Companion.bigInt(size: Int, initializer: (Int) -> BigInt): Buffer = boxing(size, initializer) +public inline fun BigInt.buffer(size: Int, initializer: (Int) -> BigInt): Buffer = + Buffer.boxing(size, initializer) + +@Deprecated("Use BigInt::mutableBuffer") public inline fun MutableBuffer.Companion.bigInt(size: Int, initializer: (Int) -> BigInt): MutableBuffer = boxing(size, initializer) +public inline fun BigInt.mutableBuffer(size: Int, initializer: (Int) -> BigInt): Buffer = + Buffer.boxing(size, initializer) + public fun BigIntField.nd(vararg shape: Int): BufferedRingND = BufferedRingND(shape, BigIntField, Buffer.Companion::bigInt) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt index e75d815cf..c90553aaa 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt @@ -107,6 +107,8 @@ public object DoubleField : ExtendedField, Norm, ScaleOp override inline fun Double.div(b: Double): Double = this / b } +public val Double.Companion.algebra: DoubleField get() = DoubleField + /** * A field for [Float] without boxing. Does not produce appropriate field element. */ @@ -158,6 +160,8 @@ public object FloatField : ExtendedField, Norm { override inline fun Float.div(b: Float): Float = this / b } +public val Float.Companion.algebra: FloatField get() = FloatField + /** * A field for [Int] without boxing. Does not produce corresponding ring element. */ @@ -180,6 +184,8 @@ public object IntRing : Ring, Norm, NumericAlgebra { override inline fun Int.times(b: Int): Int = this * b } +public val Int.Companion.algebra: IntRing get() = IntRing + /** * A field for [Short] without boxing. Does not produce appropriate ring element. */ @@ -202,6 +208,8 @@ public object ShortRing : Ring, Norm, NumericAlgebra override inline fun Short.times(b: Short): Short = (this * b).toShort() } +public val Short.Companion.algebra: ShortRing get() = ShortRing + /** * A field for [Byte] without boxing. Does not produce appropriate ring element. */ @@ -224,6 +232,8 @@ public object ByteRing : Ring, Norm, NumericAlgebra { override inline fun Byte.times(b: Byte): Byte = (this * b).toByte() } +public val Byte.Companion.algebra: ByteRing get() = ByteRing + /** * A field for [Double] without boxing. Does not produce appropriate ring element. */ @@ -245,3 +255,5 @@ public object LongRing : Ring, Norm, NumericAlgebra { override inline fun Long.minus(b: Long): Long = (this - b) override inline fun Long.times(b: Long): Long = (this * b) } + +public val Long.Companion.algebra: LongRing get() = LongRing