Refactor buffer builders to suit new discoverability pattern

This commit is contained in:
Alexander Nozik 2021-09-19 21:13:07 +03:00
parent 49ec5d1554
commit 546d56aeee
5 changed files with 27 additions and 2 deletions

View File

@ -39,6 +39,7 @@
- Rename `DifferentiableMstExpression` to `KotlingradExpression` - Rename `DifferentiableMstExpression` to `KotlingradExpression`
- `FeatureSet` now accepts only `Feature`. It is possible to override keys and use interfaces. - `FeatureSet` now accepts only `Feature`. It is possible to override keys and use interfaces.
- Use `Symbol` factory function instead of `StringSymbol` - Use `Symbol` factory function instead of `StringSymbol`
- New discoverability pattern: `<Type>.algebra.<nd/etc>`
### Deprecated ### Deprecated
- Specialized `DoubleBufferAlgebra` - Specialized `DoubleBufferAlgebra`

View File

@ -7,13 +7,13 @@ package space.kscience.kmath.operations
import space.kscience.kmath.complex.Complex import space.kscience.kmath.complex.Complex
import space.kscience.kmath.complex.ComplexField import space.kscience.kmath.complex.ComplexField
import space.kscience.kmath.complex.nd
import space.kscience.kmath.complex.withNd import space.kscience.kmath.complex.withNd
import space.kscience.kmath.nd.StructureND import space.kscience.kmath.nd.StructureND
import space.kscience.kmath.nd.autoNd
fun main() { fun main() {
// 2d element // 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) Complex(i - j, i + j)
} }
println(element) println(element)

View File

@ -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. * Creates a complex number with real part equal to this real.

View File

@ -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<BigInt> = public inline fun Buffer.Companion.bigInt(size: Int, initializer: (Int) -> BigInt): Buffer<BigInt> =
boxing(size, initializer) boxing(size, initializer)
public inline fun BigInt.buffer(size: Int, initializer: (Int) -> BigInt): Buffer<BigInt> =
Buffer.boxing(size, initializer)
@Deprecated("Use BigInt::mutableBuffer")
public inline fun MutableBuffer.Companion.bigInt(size: Int, initializer: (Int) -> BigInt): MutableBuffer<BigInt> = public inline fun MutableBuffer.Companion.bigInt(size: Int, initializer: (Int) -> BigInt): MutableBuffer<BigInt> =
boxing(size, initializer) boxing(size, initializer)
public inline fun BigInt.mutableBuffer(size: Int, initializer: (Int) -> BigInt): Buffer<BigInt> =
Buffer.boxing(size, initializer)
public fun BigIntField.nd(vararg shape: Int): BufferedRingND<BigInt, BigIntField> = public fun BigIntField.nd(vararg shape: Int): BufferedRingND<BigInt, BigIntField> =
BufferedRingND(shape, BigIntField, Buffer.Companion::bigInt) BufferedRingND(shape, BigIntField, Buffer.Companion::bigInt)

View File

@ -107,6 +107,8 @@ public object DoubleField : ExtendedField<Double>, Norm<Double, Double>, ScaleOp
override inline fun Double.div(b: Double): Double = this / b 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. * A field for [Float] without boxing. Does not produce appropriate field element.
*/ */
@ -158,6 +160,8 @@ public object FloatField : ExtendedField<Float>, Norm<Float, Float> {
override inline fun Float.div(b: Float): Float = this / b 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. * A field for [Int] without boxing. Does not produce corresponding ring element.
*/ */
@ -180,6 +184,8 @@ public object IntRing : Ring<Int>, Norm<Int, Int>, NumericAlgebra<Int> {
override inline fun Int.times(b: Int): Int = this * b 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. * A field for [Short] without boxing. Does not produce appropriate ring element.
*/ */
@ -202,6 +208,8 @@ public object ShortRing : Ring<Short>, Norm<Short, Short>, NumericAlgebra<Short>
override inline fun Short.times(b: Short): Short = (this * b).toShort() 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. * A field for [Byte] without boxing. Does not produce appropriate ring element.
*/ */
@ -224,6 +232,8 @@ public object ByteRing : Ring<Byte>, Norm<Byte, Byte>, NumericAlgebra<Byte> {
override inline fun Byte.times(b: Byte): Byte = (this * b).toByte() 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. * A field for [Double] without boxing. Does not produce appropriate ring element.
*/ */
@ -245,3 +255,5 @@ public object LongRing : Ring<Long>, Norm<Long, Long>, NumericAlgebra<Long> {
override inline fun Long.minus(b: Long): Long = (this - b) override inline fun Long.minus(b: Long): Long = (this - b)
override inline fun Long.times(b: Long): Long = (this * b) override inline fun Long.times(b: Long): Long = (this * b)
} }
public val Long.Companion.algebra: LongRing get() = LongRing