diff --git a/buildSrc/src/main/kotlin/kscience/kmath/gsl/codegen/matricesCodegen.kt b/buildSrc/src/main/kotlin/kscience/kmath/gsl/codegen/matricesCodegen.kt index df21a8c06..763f08814 100644 --- a/buildSrc/src/main/kotlin/kscience/kmath/gsl/codegen/matricesCodegen.kt +++ b/buildSrc/src/main/kotlin/kscience/kmath/gsl/codegen/matricesCodegen.kt @@ -21,7 +21,8 @@ private fun KtPsiFactory.createMatrixClass( @Language("kotlin") val text = """internal class $className( override val rawNativeHandle: CPointer<$structName>, scope: AutofreeScope, -) : GslMatrix<$kotlinTypeName, $structName>(scope) { + owned: Boolean, +) : GslMatrix<$kotlinTypeName, $structName>(scope, owned) { override val rowNum: Int get() = nativeHandle.pointed.size1.toInt() @@ -33,7 +34,8 @@ private fun KtPsiFactory.createMatrixClass( Gsl${kotlinTypeAlias}Vector( ${fn("gsl_matrixRrow")}(nativeHandle, r.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override val columns: Buffer> @@ -41,7 +43,8 @@ private fun KtPsiFactory.createMatrixClass( Gsl${kotlinTypeAlias}Vector( ${fn("gsl_matrixRcolumn")}(nativeHandle, c.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override operator fun get(i: Int, j: Int): $kotlinTypeName = @@ -53,7 +56,7 @@ private fun KtPsiFactory.createMatrixClass( override fun copy(): $className { val new = checkNotNull(${fn("gsl_matrixRalloc")}(rowNum.toULong(), colNum.toULong())) ${fn("gsl_matrixRmemcpy")}(new, nativeHandle) - return $className(new, scope) + return $className(new, scope, true) } override fun close(): Unit = ${fn("gsl_matrixRfree")}(nativeHandle) diff --git a/buildSrc/src/main/kotlin/kscience/kmath/gsl/codegen/vectorsCodegen.kt b/buildSrc/src/main/kotlin/kscience/kmath/gsl/codegen/vectorsCodegen.kt index c39b2b8f6..67ab77186 100644 --- a/buildSrc/src/main/kotlin/kscience/kmath/gsl/codegen/vectorsCodegen.kt +++ b/buildSrc/src/main/kotlin/kscience/kmath/gsl/codegen/vectorsCodegen.kt @@ -19,21 +19,19 @@ private fun KtPsiFactory.createVectorClass( val structName = sn("gsl_vectorR", cTypeName) @Language("kotlin") val text = - """internal class $className(override val rawNativeHandle: CPointer<$structName>, scope: AutofreeScope) : - GslVector<$kotlinTypeName, $structName>(scope) { - override val size: Int - get() = nativeHandle.pointed.size.toInt() - - override operator fun get(index: Int): $kotlinTypeName = - ${fn("gsl_vectorRget")}(nativeHandle, index.toULong()) - - override operator fun set(index: Int, value: $kotlinTypeName): Unit = - ${fn("gsl_vectorRset")}(nativeHandle, index.toULong(), value) + """internal class $className( + override val rawNativeHandle: CPointer<$structName>, + scope: AutofreeScope, + owned: Boolean, +) : GslVector<$kotlinTypeName, $structName>(scope, owned) { + override val size: Int get() = nativeHandle.pointed.size.toInt() + override operator fun get(index: Int): $kotlinTypeName = ${fn("gsl_vectorRget")}(nativeHandle, index.toULong()) + override operator fun set(index: Int, value: $kotlinTypeName): Unit = ${fn("gsl_vectorRset")}(nativeHandle, index.toULong(), value) override fun copy(): $className { val new = checkNotNull(${fn("gsl_vectorRalloc")}(size.toULong())) ${fn("gsl_vectorRmemcpy")}(new, nativeHandle) - return ${className}(new, scope) + return ${className}(new, scope, true) } override fun equals(other: Any?): Boolean { diff --git a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslComplex.kt b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslComplex.kt index 4a48e432a..c18c0dfdf 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslComplex.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslComplex.kt @@ -13,20 +13,21 @@ internal fun Complex.toGsl(): CValue = cValue { dat[1] = im } -internal class GslComplexMatrix(override val rawNativeHandle: CPointer, scope: AutofreeScope) : - GslMatrix(scope) { - override val rowNum: Int - get() = nativeHandle.pointed.size1.toInt() - - override val colNum: Int - get() = nativeHandle.pointed.size2.toInt() +internal class GslComplexMatrix( + override val rawNativeHandle: CPointer, + scope: AutofreeScope, + owned: Boolean, +) : GslMatrix(scope, owned) { + override val rowNum: Int get() = nativeHandle.pointed.size1.toInt() + override val colNum: Int get() = nativeHandle.pointed.size2.toInt() override val rows: Buffer> get() = VirtualBuffer(rowNum) { r -> GslComplexVector( gsl_matrix_complex_row(nativeHandle, r.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override val columns: Buffer> @@ -34,7 +35,8 @@ internal class GslComplexMatrix(override val rawNativeHandle: CPointer, scope: AutofreeScope) : - GslVector(scope) { - override val size: Int - get() = nativeHandle.pointed.size.toInt() - +internal class GslComplexVector( + override val rawNativeHandle: CPointer, + scope: AutofreeScope, + owned: Boolean, +) : GslVector(scope, owned) { + override val size: Int get() = nativeHandle.pointed.size.toInt() override fun get(index: Int): Complex = gsl_vector_complex_get(nativeHandle, index.toULong()).toKMath() override fun set(index: Int, value: Complex): Unit = @@ -72,7 +75,7 @@ internal class GslComplexVector(override val rawNativeHandle: CPointer internal constructor(scope: AutofreeScope) : - GslObject(scope), Matrix { +public abstract class GslMatrix internal constructor(scope: AutofreeScope, owned: Boolean) : + GslObject(scope, owned), Matrix { internal abstract operator fun set(i: Int, j: Int, value: T) internal abstract fun copy(): GslMatrix diff --git a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrixContext.kt b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrixContext.kt index 8e27df060..e86d3676e 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrixContext.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrixContext.kt @@ -6,7 +6,6 @@ import kscience.kmath.misc.UnstableKMathAPI import kscience.kmath.operations.Complex import kscience.kmath.operations.ComplexField import kscience.kmath.operations.toComplex -import kscience.kmath.structures.Matrix import org.gnu.gsl.* import kotlin.reflect.KClass import kotlin.reflect.cast @@ -65,17 +64,18 @@ public class GslRealMatrixContext(internal val scope: AutofreeScope) : override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix = GslRealMatrix( rawNativeHandle = checkNotNull(gsl_matrix_alloc(rows.toULong(), columns.toULong())), scope = scope, + owned = true, ) override fun produceDirtyVector(size: Int): GslVector = - GslRealVector(rawNativeHandle = checkNotNull(gsl_vector_alloc(size.toULong())), scope = scope) + GslRealVector(rawNativeHandle = checkNotNull(gsl_vector_alloc(size.toULong())), scope = scope, owned = true) public override fun Matrix.dot(other: Matrix): GslMatrix { val x = toGsl().nativeHandle val a = other.toGsl().nativeHandle val result = checkNotNull(gsl_matrix_calloc(a.pointed.size1, a.pointed.size2)) gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, x, a, 1.0, result) - return GslRealMatrix(result, scope = scope) + return GslRealMatrix(rawNativeHandle = result, scope = scope, owned = true) } public override fun Matrix.dot(vector: Point): GslVector { @@ -83,7 +83,7 @@ public class GslRealMatrixContext(internal val scope: AutofreeScope) : val a = vector.toGsl().nativeHandle val result = checkNotNull(gsl_vector_calloc(a.pointed.size)) gsl_blas_dgemv(CblasNoTrans, 1.0, x, a, 1.0, result) - return GslRealVector(result, scope = scope) + return GslRealVector(rawNativeHandle = result, scope = scope, owned = true) } public override fun Matrix.times(value: Double): GslMatrix { @@ -118,7 +118,12 @@ public class GslRealMatrixContext(internal val scope: AutofreeScope) : private val lups by lazy { val lu = m.toGsl().copy() val n = m.rowNum - val perm = GslPermutation(checkNotNull(gsl_permutation_alloc(n.toULong())), scope) + + val perm = GslPermutation( + rawNativeHandle = checkNotNull(gsl_permutation_alloc(n.toULong())), + scope = scope, + owned = true, + ) val signum = memScoped { val i = alloc() @@ -192,17 +197,21 @@ public class GslFloatMatrixContext(internal val scope: AutofreeScope) : override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix = GslFloatMatrix( rawNativeHandle = checkNotNull(gsl_matrix_float_alloc(rows.toULong(), columns.toULong())), scope = scope, + owned = true, ) - override fun produceDirtyVector(size: Int): GslVector = - GslFloatVector(rawNativeHandle = checkNotNull(value = gsl_vector_float_alloc(size.toULong())), scope = scope) + override fun produceDirtyVector(size: Int): GslVector = GslFloatVector( + rawNativeHandle = checkNotNull(value = gsl_vector_float_alloc(size.toULong())), + scope = scope, + owned = true, + ) public override fun Matrix.dot(other: Matrix): GslMatrix { val x = toGsl().nativeHandle val a = other.toGsl().nativeHandle val result = checkNotNull(gsl_matrix_float_calloc(a.pointed.size1, a.pointed.size2)) gsl_blas_sgemm(CblasNoTrans, CblasNoTrans, 1f, x, a, 1f, result) - return GslFloatMatrix(rawNativeHandle = result, scope = scope) + return GslFloatMatrix(rawNativeHandle = result, scope = scope, owned = true) } public override fun Matrix.dot(vector: Point): GslVector { @@ -210,7 +219,7 @@ public class GslFloatMatrixContext(internal val scope: AutofreeScope) : val a = vector.toGsl().nativeHandle val result = checkNotNull(gsl_vector_float_calloc(a.pointed.size)) gsl_blas_sgemv(CblasNoTrans, 1f, x, a, 1f, result) - return GslFloatVector(rawNativeHandle = result, scope = scope) + return GslFloatVector(rawNativeHandle = result, scope = scope, owned = true) } public override fun Matrix.times(value: Float): GslMatrix { @@ -305,7 +314,10 @@ public class GslComplexMatrixContext(internal val scope: AutofreeScope) : private val lups by lazy { val lu = m.toGsl().copy() val n = m.rowNum - val perm = GslPermutation(checkNotNull(gsl_permutation_alloc(n.toULong())), scope) + + val perm = GslPermutation(rawNativeHandle = checkNotNull(gsl_permutation_alloc(n.toULong())), + scope = scope, + owned = true) val signum = memScoped { val i = alloc() @@ -322,7 +334,9 @@ public class GslComplexMatrixContext(internal val scope: AutofreeScope) : val perm = produce(n, n) { _, _ -> 0.0.toComplex() } for (j in 0 until lups.second.size) - gsl_matrix_complex_set_col(perm.nativeHandle, j.toULong(), one.columns[lups.second[j]].toGsl().nativeHandle) + gsl_matrix_complex_set_col(perm.nativeHandle, + j.toULong(), + one.columns[lups.second[j]].toGsl().nativeHandle) perm } @@ -344,7 +358,10 @@ public class GslComplexMatrixContext(internal val scope: AutofreeScope) : ) { i, j -> if (j >= i) lups.first[i, j] else 0.0.toComplex() } + UFeature } - override val determinant by lazy { gsl_linalg_complex_LU_det(lups.first.nativeHandle, lups.third).toKMath() } + override val determinant by lazy { + gsl_linalg_complex_LU_det(lups.first.nativeHandle, + lups.third).toKMath() + } override val inverse by lazy { val inv = lups.first.copy() diff --git a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslObject.kt b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslObject.kt index 8c771a183..ff4ef96c5 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslObject.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslObject.kt @@ -13,7 +13,7 @@ import kotlinx.cinterop.DeferScope * * @param scope the scope where this object is declared. */ -public abstract class GslObject internal constructor(internal val scope: AutofreeScope) { +public abstract class GslObject internal constructor(internal val scope: AutofreeScope, internal val owned: Boolean) { internal abstract val rawNativeHandle: CPointer private var isClosed: Boolean = false @@ -23,13 +23,11 @@ public abstract class GslObject internal constructor(internal va return rawNativeHandle } - internal var shouldBeFreed = true - init { ensureHasGslErrorHandler() scope.defer { - if (shouldBeFreed) close() + if (owned) close() isClosed = true } } diff --git a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslPermutation.kt b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslPermutation.kt index 49baceb6b..6954ceee2 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslPermutation.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslPermutation.kt @@ -7,8 +7,11 @@ import org.gnu.gsl.gsl_permutation import org.gnu.gsl.gsl_permutation_free import org.gnu.gsl.gsl_permutation_get -internal class GslPermutation(override val rawNativeHandle: CPointer, scope: AutofreeScope) : - GslObject(scope) { +internal class GslPermutation( + override val rawNativeHandle: CPointer, + scope: AutofreeScope, + owned: Boolean, +) : GslObject(scope, owned) { val size get() = nativeHandle.pointed.size.toInt() operator fun get(i: Int) = gsl_permutation_get(nativeHandle, i.toULong()).toInt() diff --git a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslVector.kt b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslVector.kt index fb8ba53d7..14b956aba 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslVector.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslVector.kt @@ -7,8 +7,8 @@ import kscience.kmath.linear.Point /** * Wraps gsl_vector_* objects from GSL. */ -public abstract class GslVector internal constructor(scope: AutofreeScope) : - GslObject(scope), Point { +public abstract class GslVector internal constructor(scope: AutofreeScope, owned: Boolean) : + GslObject(scope, owned), Point { internal abstract operator fun set(index: Int, value: T) internal abstract fun copy(): GslVector diff --git a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/_Matrices.kt b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/_Matrices.kt index 7dbab66d7..a0ed0cdb0 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/_Matrices.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/_Matrices.kt @@ -7,7 +7,8 @@ import org.gnu.gsl.* internal class GslRealMatrix( override val rawNativeHandle: CPointer, scope: AutofreeScope, -) : GslMatrix(scope) { + owned: Boolean, +) : GslMatrix(scope, owned) { override val rowNum: Int get() = nativeHandle.pointed.size1.toInt() @@ -19,7 +20,8 @@ internal class GslRealMatrix( GslRealVector( gsl_matrix_row(nativeHandle, r.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override val columns: Buffer> @@ -27,7 +29,8 @@ internal class GslRealMatrix( GslRealVector( gsl_matrix_column(nativeHandle, c.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override operator fun get(i: Int, j: Int): Double = @@ -39,7 +42,7 @@ internal class GslRealMatrix( override fun copy(): GslRealMatrix { val new = checkNotNull(gsl_matrix_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_memcpy(new, nativeHandle) - return GslRealMatrix(new, scope) + return GslRealMatrix(new, scope, true) } override fun close(): Unit = gsl_matrix_free(nativeHandle) @@ -55,7 +58,8 @@ internal class GslRealMatrix( internal class GslFloatMatrix( override val rawNativeHandle: CPointer, scope: AutofreeScope, -) : GslMatrix(scope) { + owned: Boolean, +) : GslMatrix(scope, owned) { override val rowNum: Int get() = nativeHandle.pointed.size1.toInt() @@ -67,7 +71,8 @@ internal class GslFloatMatrix( GslFloatVector( gsl_matrix_float_row(nativeHandle, r.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override val columns: Buffer> @@ -75,7 +80,8 @@ internal class GslFloatMatrix( GslFloatVector( gsl_matrix_float_column(nativeHandle, c.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override operator fun get(i: Int, j: Int): Float = @@ -87,7 +93,7 @@ internal class GslFloatMatrix( override fun copy(): GslFloatMatrix { val new = checkNotNull(gsl_matrix_float_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_float_memcpy(new, nativeHandle) - return GslFloatMatrix(new, scope) + return GslFloatMatrix(new, scope, true) } override fun close(): Unit = gsl_matrix_float_free(nativeHandle) @@ -103,7 +109,8 @@ internal class GslFloatMatrix( internal class GslShortMatrix( override val rawNativeHandle: CPointer, scope: AutofreeScope, -) : GslMatrix(scope) { + owned: Boolean, +) : GslMatrix(scope, owned) { override val rowNum: Int get() = nativeHandle.pointed.size1.toInt() @@ -115,7 +122,8 @@ internal class GslShortMatrix( GslShortVector( gsl_matrix_short_row(nativeHandle, r.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override val columns: Buffer> @@ -123,7 +131,8 @@ internal class GslShortMatrix( GslShortVector( gsl_matrix_short_column(nativeHandle, c.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override operator fun get(i: Int, j: Int): Short = @@ -135,7 +144,7 @@ internal class GslShortMatrix( override fun copy(): GslShortMatrix { val new = checkNotNull(gsl_matrix_short_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_short_memcpy(new, nativeHandle) - return GslShortMatrix(new, scope) + return GslShortMatrix(new, scope, true) } override fun close(): Unit = gsl_matrix_short_free(nativeHandle) @@ -151,7 +160,8 @@ internal class GslShortMatrix( internal class GslUShortMatrix( override val rawNativeHandle: CPointer, scope: AutofreeScope, -) : GslMatrix(scope) { + owned: Boolean, +) : GslMatrix(scope, owned) { override val rowNum: Int get() = nativeHandle.pointed.size1.toInt() @@ -163,7 +173,8 @@ internal class GslUShortMatrix( GslUShortVector( gsl_matrix_ushort_row(nativeHandle, r.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override val columns: Buffer> @@ -171,7 +182,8 @@ internal class GslUShortMatrix( GslUShortVector( gsl_matrix_ushort_column(nativeHandle, c.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override operator fun get(i: Int, j: Int): UShort = @@ -183,7 +195,7 @@ internal class GslUShortMatrix( override fun copy(): GslUShortMatrix { val new = checkNotNull(gsl_matrix_ushort_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_ushort_memcpy(new, nativeHandle) - return GslUShortMatrix(new, scope) + return GslUShortMatrix(new, scope, true) } override fun close(): Unit = gsl_matrix_ushort_free(nativeHandle) @@ -199,7 +211,8 @@ internal class GslUShortMatrix( internal class GslLongMatrix( override val rawNativeHandle: CPointer, scope: AutofreeScope, -) : GslMatrix(scope) { + owned: Boolean, +) : GslMatrix(scope, owned) { override val rowNum: Int get() = nativeHandle.pointed.size1.toInt() @@ -211,7 +224,8 @@ internal class GslLongMatrix( GslLongVector( gsl_matrix_long_row(nativeHandle, r.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override val columns: Buffer> @@ -219,7 +233,8 @@ internal class GslLongMatrix( GslLongVector( gsl_matrix_long_column(nativeHandle, c.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override operator fun get(i: Int, j: Int): Long = @@ -231,7 +246,7 @@ internal class GslLongMatrix( override fun copy(): GslLongMatrix { val new = checkNotNull(gsl_matrix_long_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_long_memcpy(new, nativeHandle) - return GslLongMatrix(new, scope) + return GslLongMatrix(new, scope, true) } override fun close(): Unit = gsl_matrix_long_free(nativeHandle) @@ -247,7 +262,8 @@ internal class GslLongMatrix( internal class GslULongMatrix( override val rawNativeHandle: CPointer, scope: AutofreeScope, -) : GslMatrix(scope) { + owned: Boolean, +) : GslMatrix(scope, owned) { override val rowNum: Int get() = nativeHandle.pointed.size1.toInt() @@ -259,7 +275,8 @@ internal class GslULongMatrix( GslULongVector( gsl_matrix_ulong_row(nativeHandle, r.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override val columns: Buffer> @@ -267,7 +284,8 @@ internal class GslULongMatrix( GslULongVector( gsl_matrix_ulong_column(nativeHandle, c.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override operator fun get(i: Int, j: Int): ULong = @@ -279,7 +297,7 @@ internal class GslULongMatrix( override fun copy(): GslULongMatrix { val new = checkNotNull(gsl_matrix_ulong_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_ulong_memcpy(new, nativeHandle) - return GslULongMatrix(new, scope) + return GslULongMatrix(new, scope, true) } override fun close(): Unit = gsl_matrix_ulong_free(nativeHandle) @@ -295,7 +313,8 @@ internal class GslULongMatrix( internal class GslIntMatrix( override val rawNativeHandle: CPointer, scope: AutofreeScope, -) : GslMatrix(scope) { + owned: Boolean, +) : GslMatrix(scope, owned) { override val rowNum: Int get() = nativeHandle.pointed.size1.toInt() @@ -307,7 +326,8 @@ internal class GslIntMatrix( GslIntVector( gsl_matrix_int_row(nativeHandle, r.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override val columns: Buffer> @@ -315,7 +335,8 @@ internal class GslIntMatrix( GslIntVector( gsl_matrix_int_column(nativeHandle, c.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override operator fun get(i: Int, j: Int): Int = @@ -327,7 +348,7 @@ internal class GslIntMatrix( override fun copy(): GslIntMatrix { val new = checkNotNull(gsl_matrix_int_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_int_memcpy(new, nativeHandle) - return GslIntMatrix(new, scope) + return GslIntMatrix(new, scope, true) } override fun close(): Unit = gsl_matrix_int_free(nativeHandle) @@ -343,7 +364,8 @@ internal class GslIntMatrix( internal class GslUIntMatrix( override val rawNativeHandle: CPointer, scope: AutofreeScope, -) : GslMatrix(scope) { + owned: Boolean, +) : GslMatrix(scope, owned) { override val rowNum: Int get() = nativeHandle.pointed.size1.toInt() @@ -355,7 +377,8 @@ internal class GslUIntMatrix( GslUIntVector( gsl_matrix_uint_row(nativeHandle, r.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override val columns: Buffer> @@ -363,7 +386,8 @@ internal class GslUIntMatrix( GslUIntVector( gsl_matrix_uint_column(nativeHandle, c.toULong()).placeTo(scope).pointed.vector.ptr, scope, - ).apply { shouldBeFreed = false } + false, + ) } override operator fun get(i: Int, j: Int): UInt = @@ -375,7 +399,7 @@ internal class GslUIntMatrix( override fun copy(): GslUIntMatrix { val new = checkNotNull(gsl_matrix_uint_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_uint_memcpy(new, nativeHandle) - return GslUIntMatrix(new, scope) + return GslUIntMatrix(new, scope, true) } override fun close(): Unit = gsl_matrix_uint_free(nativeHandle) diff --git a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/_Vectors.kt b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/_Vectors.kt index 42ecb2fca..922c2ba2f 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/_Vectors.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/_Vectors.kt @@ -3,21 +3,19 @@ package kscience.kmath.gsl import kotlinx.cinterop.* import org.gnu.gsl.* -internal class GslRealVector(override val rawNativeHandle: CPointer, scope: AutofreeScope) : - GslVector(scope) { - override val size: Int - get() = nativeHandle.pointed.size.toInt() - - override operator fun get(index: Int): Double = - gsl_vector_get(nativeHandle, index.toULong()) - - override operator fun set(index: Int, value: Double): Unit = - gsl_vector_set(nativeHandle, index.toULong(), value) +internal class GslRealVector( + override val rawNativeHandle: CPointer, + scope: AutofreeScope, + owned: Boolean, +) : GslVector(scope, owned) { + override val size: Int get() = nativeHandle.pointed.size.toInt() + override operator fun get(index: Int): Double = gsl_vector_get(nativeHandle, index.toULong()) + override operator fun set(index: Int, value: Double): Unit = gsl_vector_set(nativeHandle, index.toULong(), value) override fun copy(): GslRealVector { val new = checkNotNull(gsl_vector_alloc(size.toULong())) gsl_vector_memcpy(new, nativeHandle) - return GslRealVector(new, scope) + return GslRealVector(new, scope, true) } override fun equals(other: Any?): Boolean { @@ -30,21 +28,19 @@ internal class GslRealVector(override val rawNativeHandle: CPointer, override fun close(): Unit = gsl_vector_free(nativeHandle) } -internal class GslFloatVector(override val rawNativeHandle: CPointer, scope: AutofreeScope) : - GslVector(scope) { - override val size: Int - get() = nativeHandle.pointed.size.toInt() - - override operator fun get(index: Int): Float = - gsl_vector_float_get(nativeHandle, index.toULong()) - - override operator fun set(index: Int, value: Float): Unit = - gsl_vector_float_set(nativeHandle, index.toULong(), value) +internal class GslFloatVector( + override val rawNativeHandle: CPointer, + scope: AutofreeScope, + owned: Boolean, +) : GslVector(scope, owned) { + override val size: Int get() = nativeHandle.pointed.size.toInt() + override operator fun get(index: Int): Float = gsl_vector_float_get(nativeHandle, index.toULong()) + override operator fun set(index: Int, value: Float): Unit = gsl_vector_float_set(nativeHandle, index.toULong(), value) override fun copy(): GslFloatVector { val new = checkNotNull(gsl_vector_float_alloc(size.toULong())) gsl_vector_float_memcpy(new, nativeHandle) - return GslFloatVector(new, scope) + return GslFloatVector(new, scope, true) } override fun equals(other: Any?): Boolean { @@ -57,21 +53,19 @@ internal class GslFloatVector(override val rawNativeHandle: CPointer, scope: AutofreeScope) : - GslVector(scope) { - override val size: Int - get() = nativeHandle.pointed.size.toInt() - - override operator fun get(index: Int): Short = - gsl_vector_short_get(nativeHandle, index.toULong()) - - override operator fun set(index: Int, value: Short): Unit = - gsl_vector_short_set(nativeHandle, index.toULong(), value) +internal class GslShortVector( + override val rawNativeHandle: CPointer, + scope: AutofreeScope, + owned: Boolean, +) : GslVector(scope, owned) { + override val size: Int get() = nativeHandle.pointed.size.toInt() + override operator fun get(index: Int): Short = gsl_vector_short_get(nativeHandle, index.toULong()) + override operator fun set(index: Int, value: Short): Unit = gsl_vector_short_set(nativeHandle, index.toULong(), value) override fun copy(): GslShortVector { val new = checkNotNull(gsl_vector_short_alloc(size.toULong())) gsl_vector_short_memcpy(new, nativeHandle) - return GslShortVector(new, scope) + return GslShortVector(new, scope, true) } override fun equals(other: Any?): Boolean { @@ -84,21 +78,19 @@ internal class GslShortVector(override val rawNativeHandle: CPointer, scope: AutofreeScope) : - GslVector(scope) { - override val size: Int - get() = nativeHandle.pointed.size.toInt() - - override operator fun get(index: Int): UShort = - gsl_vector_ushort_get(nativeHandle, index.toULong()) - - override operator fun set(index: Int, value: UShort): Unit = - gsl_vector_ushort_set(nativeHandle, index.toULong(), value) +internal class GslUShortVector( + override val rawNativeHandle: CPointer, + scope: AutofreeScope, + owned: Boolean, +) : GslVector(scope, owned) { + override val size: Int get() = nativeHandle.pointed.size.toInt() + override operator fun get(index: Int): UShort = gsl_vector_ushort_get(nativeHandle, index.toULong()) + override operator fun set(index: Int, value: UShort): Unit = gsl_vector_ushort_set(nativeHandle, index.toULong(), value) override fun copy(): GslUShortVector { val new = checkNotNull(gsl_vector_ushort_alloc(size.toULong())) gsl_vector_ushort_memcpy(new, nativeHandle) - return GslUShortVector(new, scope) + return GslUShortVector(new, scope, true) } override fun equals(other: Any?): Boolean { @@ -111,21 +103,19 @@ internal class GslUShortVector(override val rawNativeHandle: CPointer, scope: AutofreeScope) : - GslVector(scope) { - override val size: Int - get() = nativeHandle.pointed.size.toInt() - - override operator fun get(index: Int): Long = - gsl_vector_long_get(nativeHandle, index.toULong()) - - override operator fun set(index: Int, value: Long): Unit = - gsl_vector_long_set(nativeHandle, index.toULong(), value) +internal class GslLongVector( + override val rawNativeHandle: CPointer, + scope: AutofreeScope, + owned: Boolean, +) : GslVector(scope, owned) { + override val size: Int get() = nativeHandle.pointed.size.toInt() + override operator fun get(index: Int): Long = gsl_vector_long_get(nativeHandle, index.toULong()) + override operator fun set(index: Int, value: Long): Unit = gsl_vector_long_set(nativeHandle, index.toULong(), value) override fun copy(): GslLongVector { val new = checkNotNull(gsl_vector_long_alloc(size.toULong())) gsl_vector_long_memcpy(new, nativeHandle) - return GslLongVector(new, scope) + return GslLongVector(new, scope, true) } override fun equals(other: Any?): Boolean { @@ -138,21 +128,19 @@ internal class GslLongVector(override val rawNativeHandle: CPointer, scope: AutofreeScope) : - GslVector(scope) { - override val size: Int - get() = nativeHandle.pointed.size.toInt() - - override operator fun get(index: Int): ULong = - gsl_vector_ulong_get(nativeHandle, index.toULong()) - - override operator fun set(index: Int, value: ULong): Unit = - gsl_vector_ulong_set(nativeHandle, index.toULong(), value) +internal class GslULongVector( + override val rawNativeHandle: CPointer, + scope: AutofreeScope, + owned: Boolean, +) : GslVector(scope, owned) { + override val size: Int get() = nativeHandle.pointed.size.toInt() + override operator fun get(index: Int): ULong = gsl_vector_ulong_get(nativeHandle, index.toULong()) + override operator fun set(index: Int, value: ULong): Unit = gsl_vector_ulong_set(nativeHandle, index.toULong(), value) override fun copy(): GslULongVector { val new = checkNotNull(gsl_vector_ulong_alloc(size.toULong())) gsl_vector_ulong_memcpy(new, nativeHandle) - return GslULongVector(new, scope) + return GslULongVector(new, scope, true) } override fun equals(other: Any?): Boolean { @@ -165,21 +153,19 @@ internal class GslULongVector(override val rawNativeHandle: CPointer, scope: AutofreeScope) : - GslVector(scope) { - override val size: Int - get() = nativeHandle.pointed.size.toInt() - - override operator fun get(index: Int): Int = - gsl_vector_int_get(nativeHandle, index.toULong()) - - override operator fun set(index: Int, value: Int): Unit = - gsl_vector_int_set(nativeHandle, index.toULong(), value) +internal class GslIntVector( + override val rawNativeHandle: CPointer, + scope: AutofreeScope, + owned: Boolean, +) : GslVector(scope, owned) { + override val size: Int get() = nativeHandle.pointed.size.toInt() + override operator fun get(index: Int): Int = gsl_vector_int_get(nativeHandle, index.toULong()) + override operator fun set(index: Int, value: Int): Unit = gsl_vector_int_set(nativeHandle, index.toULong(), value) override fun copy(): GslIntVector { val new = checkNotNull(gsl_vector_int_alloc(size.toULong())) gsl_vector_int_memcpy(new, nativeHandle) - return GslIntVector(new, scope) + return GslIntVector(new, scope, true) } override fun equals(other: Any?): Boolean { @@ -192,21 +178,19 @@ internal class GslIntVector(override val rawNativeHandle: CPointer, scope: AutofreeScope) : - GslVector(scope) { - override val size: Int - get() = nativeHandle.pointed.size.toInt() - - override operator fun get(index: Int): UInt = - gsl_vector_uint_get(nativeHandle, index.toULong()) - - override operator fun set(index: Int, value: UInt): Unit = - gsl_vector_uint_set(nativeHandle, index.toULong(), value) +internal class GslUIntVector( + override val rawNativeHandle: CPointer, + scope: AutofreeScope, + owned: Boolean, +) : GslVector(scope, owned) { + override val size: Int get() = nativeHandle.pointed.size.toInt() + override operator fun get(index: Int): UInt = gsl_vector_uint_get(nativeHandle, index.toULong()) + override operator fun set(index: Int, value: UInt): Unit = gsl_vector_uint_set(nativeHandle, index.toULong(), value) override fun copy(): GslUIntVector { val new = checkNotNull(gsl_vector_uint_alloc(size.toULong())) gsl_vector_uint_memcpy(new, nativeHandle) - return GslUIntVector(new, scope) + return GslUIntVector(new, scope, true) } override fun equals(other: Any?): Boolean { diff --git a/kmath-gsl/src/nativeTest/kotlin/GslMatrixRealTest.kt b/kmath-gsl/src/nativeTest/kotlin/GslMatrixRealTest.kt index c171f6d0a..79ee7e7f1 100644 --- a/kmath-gsl/src/nativeTest/kotlin/GslMatrixRealTest.kt +++ b/kmath-gsl/src/nativeTest/kotlin/GslMatrixRealTest.kt @@ -1,8 +1,8 @@ package kscience.kmath.gsl +import kscience.kmath.linear.Matrix import kscience.kmath.linear.RealMatrixContext import kscience.kmath.operations.invoke -import kscience.kmath.structures.Matrix import kscience.kmath.structures.asIterable import kscience.kmath.structures.toList import kotlin.random.Random diff --git a/kmath-gsl/src/nativeTest/kotlin/GslRealMatrixContextTest.kt b/kmath-gsl/src/nativeTest/kotlin/GslRealMatrixContextTest.kt index c73cb2775..fa35a5346 100644 --- a/kmath-gsl/src/nativeTest/kotlin/GslRealMatrixContextTest.kt +++ b/kmath-gsl/src/nativeTest/kotlin/GslRealMatrixContextTest.kt @@ -1,8 +1,8 @@ package kscience.kmath.gsl +import kscience.kmath.linear.Matrix import kscience.kmath.linear.RealMatrixContext import kscience.kmath.operations.invoke -import kscience.kmath.structures.Matrix import kscience.kmath.structures.RealBuffer import kscience.kmath.structures.asSequence import kotlin.random.Random