Sort classes, add vectors support

This commit is contained in:
Iaroslav Postovalov 2020-10-04 14:35:51 +07:00
parent 5724558760
commit 90bdf00f8b
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
3 changed files with 166 additions and 77 deletions

View File

@ -7,10 +7,8 @@ import kscience.kmath.operations.Complex
import kscience.kmath.structures.NDStructure import kscience.kmath.structures.NDStructure
import org.gnu.gsl.* import org.gnu.gsl.*
public sealed class GslMatrix<T : Any> : FeaturedMatrix<T> { public sealed class GslMatrix<T : Any> : StructHolder(), FeaturedMatrix<T> {
protected abstract val nativeHandle: CValues<out CStructVar> public override fun equals(other: Any?): Boolean {
override fun equals(other: Any?): Boolean {
return NDStructure.equals(this, other as? NDStructure<*> ?: return false) return NDStructure.equals(this, other as? NDStructure<*> ?: return false)
} }
@ -43,6 +41,30 @@ public class GslRealMatrix(protected override val nativeHandle: CValues<gsl_matr
} }
} }
public class GslFloatMatrix(
protected override val nativeHandle: CValues<gsl_matrix_float>,
features: Set<MatrixFeature>
) :
GslMatrix<Float>() {
public override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslFloatMatrix =
GslFloatMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): Float = gsl_matrix_float_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
if (other is GslFloatMatrix) gsl_matrix_float_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslIntMatrix(protected override val nativeHandle: CValues<gsl_matrix_int>, features: Set<MatrixFeature>) : public class GslIntMatrix(protected override val nativeHandle: CValues<gsl_matrix_int>, features: Set<MatrixFeature>) :
GslMatrix<Int>() { GslMatrix<Int>() {
@ -65,6 +87,30 @@ public class GslIntMatrix(protected override val nativeHandle: CValues<gsl_matri
} }
} }
public class GslUIntMatrix(
protected override val nativeHandle: CValues<gsl_matrix_uint>,
features: Set<MatrixFeature>
) : GslMatrix<UInt>() {
public override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslUIntMatrix =
GslUIntMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): UInt = gsl_matrix_uint_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
if (other is GslUIntMatrix) gsl_matrix_uint_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslLongMatrix( public class GslLongMatrix(
protected override val nativeHandle: CValues<gsl_matrix_long>, protected override val nativeHandle: CValues<gsl_matrix_long>,
features: Set<MatrixFeature> features: Set<MatrixFeature>
@ -90,55 +136,6 @@ public class GslLongMatrix(
} }
} }
public class GslFloatMatrix(
protected override val nativeHandle: CValues<gsl_matrix_float>,
features: Set<MatrixFeature>
) :
GslMatrix<Float>() {
public override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslFloatMatrix =
GslFloatMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): Float = gsl_matrix_float_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
if (other is GslFloatMatrix) gsl_matrix_float_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslUIntMatrix(
protected override val nativeHandle: CValues<gsl_matrix_uint>,
features: Set<MatrixFeature>
) : GslMatrix<UInt>() {
public override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslUIntMatrix =
GslUIntMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): UInt = gsl_matrix_uint_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
if (other is GslUIntMatrix) gsl_matrix_uint_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslULongMatrix( public class GslULongMatrix(
protected override val nativeHandle: CValues<gsl_matrix_ulong>, protected override val nativeHandle: CValues<gsl_matrix_ulong>,
features: Set<MatrixFeature> features: Set<MatrixFeature>
@ -163,30 +160,6 @@ public class GslULongMatrix(
} }
} }
public class GslUShortMatrix(
protected override val nativeHandle: CValues<gsl_matrix_ushort>,
features: Set<MatrixFeature>
) : GslMatrix<UShort>() {
public override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslUShortMatrix =
GslUShortMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): UShort = gsl_matrix_ushort_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
if (other is GslUShortMatrix) gsl_matrix_ushort_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslShortMatrix( public class GslShortMatrix(
protected override val nativeHandle: CValues<gsl_matrix_short>, protected override val nativeHandle: CValues<gsl_matrix_short>,
features: Set<MatrixFeature> features: Set<MatrixFeature>
@ -211,6 +184,30 @@ public class GslShortMatrix(
} }
} }
public class GslUShortMatrix(
protected override val nativeHandle: CValues<gsl_matrix_ushort>,
features: Set<MatrixFeature>
) : GslMatrix<UShort>() {
public override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslUShortMatrix =
GslUShortMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): UShort = gsl_matrix_ushort_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
if (other is GslUShortMatrix) gsl_matrix_ushort_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslComplexMatrix( public class GslComplexMatrix(
protected override val nativeHandle: CValues<gsl_matrix_complex>, protected override val nativeHandle: CValues<gsl_matrix_complex>,
features: Set<MatrixFeature> features: Set<MatrixFeature>

View File

@ -0,0 +1,84 @@
package kscience.kmath.gsl
import kotlinx.cinterop.*
import kscience.kmath.linear.Point
import kscience.kmath.operations.Complex
import org.gnu.gsl.*
public sealed class GslVector<T> : StructHolder(), Point<T> {
public override fun iterator(): Iterator<T> = object : Iterator<T> {
private var cursor = 0
override fun hasNext(): Boolean = cursor < size
override fun next(): T {
cursor++
return this@GslVector[cursor - 1]
}
}
}
public class GslRealVector(override val nativeHandle: CValues<gsl_vector>) : GslVector<Double>() {
public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Double = gsl_vector_get(nativeHandle, index.toULong())
}
public class GslFloatVector(override val nativeHandle: CValues<gsl_vector_float>) : GslVector<Float>() {
public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Float = gsl_vector_float_get(nativeHandle, index.toULong())
}
public class GslIntVector(override val nativeHandle: CValues<gsl_vector_int>) : GslVector<Int>() {
public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Int = gsl_vector_int_get(nativeHandle, index.toULong())
}
public class GslUIntVector(override val nativeHandle: CValues<gsl_vector_uint>) : GslVector<UInt>() {
public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): UInt = gsl_vector_uint_get(nativeHandle, index.toULong())
}
public class GslLongVector(override val nativeHandle: CValues<gsl_vector_long>) : GslVector<Long>() {
public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Long = gsl_vector_long_get(nativeHandle, index.toULong())
}
public class GslULongVector(override val nativeHandle: CValues<gsl_vector_ulong>) : GslVector<ULong>() {
public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): ULong = gsl_vector_ulong_get(nativeHandle, index.toULong())
}
public class GslShortVector(override val nativeHandle: CValues<gsl_vector_short>) : GslVector<Short>() {
public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Short = gsl_vector_short_get(nativeHandle, index.toULong())
}
public class GslUShortVector(override val nativeHandle: CValues<gsl_vector_ushort>) : GslVector<UShort>() {
public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): UShort = gsl_vector_ushort_get(nativeHandle, index.toULong())
}
public class GslComplexVector(override val nativeHandle: CValues<gsl_vector_complex>) : GslVector<Complex>() {
public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Complex = gsl_vector_complex_get(nativeHandle, index.toULong()).useContents {
Complex(dat[0], dat[1])
}
}

View File

@ -0,0 +1,8 @@
package kscience.kmath.gsl
import kotlinx.cinterop.CStructVar
import kotlinx.cinterop.CValues
public abstract class StructHolder internal constructor() {
protected abstract val nativeHandle: CValues<out CStructVar>
}