diff --git a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrices.kt b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrices.kt index 54c062f14..95f918209 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrices.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrices.kt @@ -7,10 +7,8 @@ import kscience.kmath.operations.Complex import kscience.kmath.structures.NDStructure import org.gnu.gsl.* -public sealed class GslMatrix : FeaturedMatrix { - protected abstract val nativeHandle: CValues - - override fun equals(other: Any?): Boolean { +public sealed class GslMatrix : StructHolder(), FeaturedMatrix { + public override fun equals(other: Any?): Boolean { return NDStructure.equals(this, other as? NDStructure<*> ?: return false) } @@ -43,6 +41,30 @@ public class GslRealMatrix(protected override val nativeHandle: CValues, + features: Set +) : + GslMatrix() { + 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 = 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, features: Set) : GslMatrix() { @@ -65,6 +87,30 @@ public class GslIntMatrix(protected override val nativeHandle: CValues, + features: Set +) : GslMatrix() { + + 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 = 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( protected override val nativeHandle: CValues, features: Set @@ -90,55 +136,6 @@ public class GslLongMatrix( } } -public class GslFloatMatrix( - protected override val nativeHandle: CValues, - features: Set -) : - GslMatrix() { - - 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 = 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, - features: Set -) : GslMatrix() { - - 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 = 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( protected override val nativeHandle: CValues, features: Set @@ -163,30 +160,6 @@ public class GslULongMatrix( } } -public class GslUShortMatrix( - protected override val nativeHandle: CValues, - features: Set -) : GslMatrix() { - - 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 = 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( protected override val nativeHandle: CValues, features: Set @@ -211,6 +184,30 @@ public class GslShortMatrix( } } +public class GslUShortMatrix( + protected override val nativeHandle: CValues, + features: Set +) : GslMatrix() { + + 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 = 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( protected override val nativeHandle: CValues, features: Set diff --git a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslVectors.kt b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslVectors.kt new file mode 100644 index 000000000..fc6e8d329 --- /dev/null +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslVectors.kt @@ -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 : StructHolder(), Point { + public override fun iterator(): Iterator = object : Iterator { + 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) : GslVector() { + 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) : GslVector() { + 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) : GslVector() { + 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) : GslVector() { + 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) : GslVector() { + 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) : GslVector() { + 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) : GslVector() { + 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) : GslVector() { + 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) : GslVector() { + 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]) + } +} diff --git a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/nativeUtilities.kt b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/nativeUtilities.kt new file mode 100644 index 000000000..fed31dcaa --- /dev/null +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/nativeUtilities.kt @@ -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 +}