Added Rings for Int and Short

This commit is contained in:
Alexander Nozik 2019-01-07 17:44:10 +03:00
parent 83e24acdc5
commit 2bf34f2a07
2 changed files with 18 additions and 10 deletions

View File

@ -32,7 +32,7 @@ inline class Real(val value: Double) : FieldElement<Double, Real, RealField> {
/** /**
* A field for double without boxing. Does not produce appropriate field element * A field for double without boxing. Does not produce appropriate field element
*/ */
object RealField : AbstractField<Double>(),ExtendedField<Double>, Norm<Double, Double> { object RealField : AbstractField<Double>(), ExtendedField<Double>, Norm<Double, Double> {
override val zero: Double = 0.0 override val zero: Double = 0.0
override fun add(a: Double, b: Double): Double = a + b override fun add(a: Double, b: Double): Double = a + b
override fun multiply(a: Double, @Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE") b: Double): Double = a * b override fun multiply(a: Double, @Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE") b: Double): Double = a * b
@ -52,15 +52,25 @@ object RealField : AbstractField<Double>(),ExtendedField<Double>, Norm<Double, D
} }
/** /**
* A field for double without boxing. Does not produce appropriate field element * A field for [Int] without boxing. Does not produce corresponding field element
*/ */
object IntField : Field<Int> { object IntRing : Ring<Int> {
override val zero: Int = 0 override val zero: Int = 0
override fun add(a: Int, b: Int): Int = a + b override fun add(a: Int, b: Int): Int = a + b
override fun multiply(a: Int, b: Int): Int = a * b override fun multiply(a: Int, b: Int): Int = a * b
override fun multiply(a: Int, k: Double): Int = (k * a).toInt() override fun multiply(a: Int, k: Double): Int = (k * a).toInt()
override val one: Int = 1 override val one: Int = 1
override fun divide(a: Int, b: Int): Int = a / b }
/**
* A field for [Short] without boxing. Does not produce appropriate field element
*/
object ShortRing : Ring<Short> {
override val zero: Short = 0
override fun add(a: Short, b: Short): Short = (a + b).toShort()
override fun multiply(a: Short, b: Short): Short = (a * b).toShort()
override fun multiply(a: Short, k: Double): Short = (a * k).toShort()
override val one: Short = 1
} }
//interface FieldAdapter<T, R> : Field<R> { //interface FieldAdapter<T, R> : Field<R> {

View File

@ -32,7 +32,6 @@ interface NDField<T, F : Field<T>, N : NDStructure<T>> : Field<N> {
* Create a nd-field for [Double] values * Create a nd-field for [Double] values
*/ */
fun real(shape: IntArray) = RealNDField(shape) fun real(shape: IntArray) = RealNDField(shape)
/** /**
* Create a nd-field with boxing generic buffer * Create a nd-field with boxing generic buffer
*/ */
@ -42,12 +41,11 @@ interface NDField<T, F : Field<T>, N : NDStructure<T>> : Field<N> {
/** /**
* Create a most suitable implementation for nd-field using reified class. * Create a most suitable implementation for nd-field using reified class.
*/ */
@Suppress("UNCHECKED_CAST")
inline fun <reified T : Any, F : Field<T>> auto(shape: IntArray, field: F): StridedNDField<T, F> = inline fun <reified T : Any, F : Field<T>> auto(shape: IntArray, field: F): StridedNDField<T, F> =
if (T::class == Double::class) { when {
@Suppress("UNCHECKED_CAST") T::class == Double::class -> real(shape) as StridedNDField<T, F>
real(shape) as StridedNDField<T, F> else -> BufferNDField(shape, field, Buffer.Companion::auto)
} else {
BufferNDField(shape, field, Buffer.Companion::auto)
} }
} }
} }