Add owned property to all the objects

This commit is contained in:
Iaroslav Postovalov 2021-01-30 17:35:13 +07:00
parent 955e95690d
commit e66f169655
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
12 changed files with 207 additions and 177 deletions

View File

@ -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<Buffer<$kotlinTypeName>>
@ -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)

View File

@ -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 {

View File

@ -13,20 +13,21 @@ internal fun Complex.toGsl(): CValue<gsl_complex> = cValue {
dat[1] = im
}
internal class GslComplexMatrix(override val rawNativeHandle: CPointer<gsl_matrix_complex>, scope: AutofreeScope) :
GslMatrix<Complex, gsl_matrix_complex>(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<gsl_matrix_complex>,
scope: AutofreeScope,
owned: Boolean,
) : GslMatrix<Complex, gsl_matrix_complex>(scope, owned) {
override val rowNum: Int get() = nativeHandle.pointed.size1.toInt()
override val colNum: Int get() = nativeHandle.pointed.size2.toInt()
override val rows: Buffer<Buffer<Complex>>
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<Buffer<Complex>>
@ -34,7 +35,8 @@ internal class GslComplexMatrix(override val rawNativeHandle: CPointer<gsl_matri
GslComplexVector(
gsl_matrix_complex_column(nativeHandle, c.toULong()).placeTo(scope).pointed.vector.ptr,
scope,
).apply { shouldBeFreed = false }
false,
)
}
override operator fun get(i: Int, j: Int): Complex =
@ -46,7 +48,7 @@ internal class GslComplexMatrix(override val rawNativeHandle: CPointer<gsl_matri
override fun copy(): GslComplexMatrix {
val new = checkNotNull(gsl_matrix_complex_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_complex_memcpy(new, nativeHandle)
return GslComplexMatrix(new, scope)
return GslComplexMatrix(new, scope, true)
}
override fun close(): Unit = gsl_matrix_complex_free(nativeHandle)
@ -59,11 +61,12 @@ internal class GslComplexMatrix(override val rawNativeHandle: CPointer<gsl_matri
}
}
internal class GslComplexVector(override val rawNativeHandle: CPointer<gsl_vector_complex>, scope: AutofreeScope) :
GslVector<Complex, gsl_vector_complex>(scope) {
override val size: Int
get() = nativeHandle.pointed.size.toInt()
internal class GslComplexVector(
override val rawNativeHandle: CPointer<gsl_vector_complex>,
scope: AutofreeScope,
owned: Boolean,
) : GslVector<Complex, gsl_vector_complex>(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<gsl_vecto
override fun copy(): GslComplexVector {
val new = checkNotNull(gsl_vector_complex_alloc(size.toULong()))
gsl_vector_complex_memcpy(new, nativeHandle)
return GslComplexVector(new, scope)
return GslComplexVector(new, scope, true)
}
override fun equals(other: Any?): Boolean {

View File

@ -2,15 +2,15 @@ package kscience.kmath.gsl
import kotlinx.cinterop.AutofreeScope
import kotlinx.cinterop.CStructVar
import kscience.kmath.structures.Matrix
import kscience.kmath.structures.NDStructure
import kscience.kmath.linear.Matrix
import kscience.kmath.nd.NDStructure
import kscience.kmath.structures.asSequence
/**
* Wraps gsl_matrix_* objects from GSL.
*/
public abstract class GslMatrix<T : Any, H : CStructVar> internal constructor(scope: AutofreeScope) :
GslObject<H>(scope), Matrix<T> {
public abstract class GslMatrix<T : Any, H : CStructVar> internal constructor(scope: AutofreeScope, owned: Boolean) :
GslObject<H>(scope, owned), Matrix<T> {
internal abstract operator fun set(i: Int, j: Int, value: T)
internal abstract fun copy(): GslMatrix<T, H>

View File

@ -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<Double, gsl_matrix> = GslRealMatrix(
rawNativeHandle = checkNotNull(gsl_matrix_alloc(rows.toULong(), columns.toULong())),
scope = scope,
owned = true,
)
override fun produceDirtyVector(size: Int): GslVector<Double, gsl_vector> =
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<Double>.dot(other: Matrix<Double>): GslMatrix<Double, gsl_matrix> {
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<Double>.dot(vector: Point<Double>): GslVector<Double, gsl_vector> {
@ -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<Double>.times(value: Double): GslMatrix<Double, gsl_matrix> {
@ -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<IntVar>()
@ -192,17 +197,21 @@ public class GslFloatMatrixContext(internal val scope: AutofreeScope) :
override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix<Float, gsl_matrix_float> = GslFloatMatrix(
rawNativeHandle = checkNotNull(gsl_matrix_float_alloc(rows.toULong(), columns.toULong())),
scope = scope,
owned = true,
)
override fun produceDirtyVector(size: Int): GslVector<Float, gsl_vector_float> =
GslFloatVector(rawNativeHandle = checkNotNull(value = gsl_vector_float_alloc(size.toULong())), scope = scope)
override fun produceDirtyVector(size: Int): GslVector<Float, gsl_vector_float> = GslFloatVector(
rawNativeHandle = checkNotNull(value = gsl_vector_float_alloc(size.toULong())),
scope = scope,
owned = true,
)
public override fun Matrix<Float>.dot(other: Matrix<Float>): GslMatrix<Float, gsl_matrix_float> {
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<Float>.dot(vector: Point<Float>): GslVector<Float, gsl_vector_float> {
@ -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<Float>.times(value: Float): GslMatrix<Float, gsl_matrix_float> {
@ -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<IntVar>()
@ -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()

View File

@ -13,7 +13,7 @@ import kotlinx.cinterop.DeferScope
*
* @param scope the scope where this object is declared.
*/
public abstract class GslObject<H : CStructVar> internal constructor(internal val scope: AutofreeScope) {
public abstract class GslObject<H : CStructVar> internal constructor(internal val scope: AutofreeScope, internal val owned: Boolean) {
internal abstract val rawNativeHandle: CPointer<H>
private var isClosed: Boolean = false
@ -23,13 +23,11 @@ public abstract class GslObject<H : CStructVar> internal constructor(internal va
return rawNativeHandle
}
internal var shouldBeFreed = true
init {
ensureHasGslErrorHandler()
scope.defer {
if (shouldBeFreed) close()
if (owned) close()
isClosed = true
}
}

View File

@ -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<gsl_permutation>, scope: AutofreeScope) :
GslObject<gsl_permutation>(scope) {
internal class GslPermutation(
override val rawNativeHandle: CPointer<gsl_permutation>,
scope: AutofreeScope,
owned: Boolean,
) : GslObject<gsl_permutation>(scope, owned) {
val size get() = nativeHandle.pointed.size.toInt()
operator fun get(i: Int) = gsl_permutation_get(nativeHandle, i.toULong()).toInt()

View File

@ -7,8 +7,8 @@ import kscience.kmath.linear.Point
/**
* Wraps gsl_vector_* objects from GSL.
*/
public abstract class GslVector<T, H : CStructVar> internal constructor(scope: AutofreeScope) :
GslObject<H>(scope), Point<T> {
public abstract class GslVector<T, H : CStructVar> internal constructor(scope: AutofreeScope, owned: Boolean) :
GslObject<H>(scope, owned), Point<T> {
internal abstract operator fun set(index: Int, value: T)
internal abstract fun copy(): GslVector<T, H>

View File

@ -7,7 +7,8 @@ import org.gnu.gsl.*
internal class GslRealMatrix(
override val rawNativeHandle: CPointer<gsl_matrix>,
scope: AutofreeScope,
) : GslMatrix<Double, gsl_matrix>(scope) {
owned: Boolean,
) : GslMatrix<Double, gsl_matrix>(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<Buffer<Double>>
@ -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<gsl_matrix_float>,
scope: AutofreeScope,
) : GslMatrix<Float, gsl_matrix_float>(scope) {
owned: Boolean,
) : GslMatrix<Float, gsl_matrix_float>(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<Buffer<Float>>
@ -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<gsl_matrix_short>,
scope: AutofreeScope,
) : GslMatrix<Short, gsl_matrix_short>(scope) {
owned: Boolean,
) : GslMatrix<Short, gsl_matrix_short>(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<Buffer<Short>>
@ -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<gsl_matrix_ushort>,
scope: AutofreeScope,
) : GslMatrix<UShort, gsl_matrix_ushort>(scope) {
owned: Boolean,
) : GslMatrix<UShort, gsl_matrix_ushort>(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<Buffer<UShort>>
@ -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<gsl_matrix_long>,
scope: AutofreeScope,
) : GslMatrix<Long, gsl_matrix_long>(scope) {
owned: Boolean,
) : GslMatrix<Long, gsl_matrix_long>(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<Buffer<Long>>
@ -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<gsl_matrix_ulong>,
scope: AutofreeScope,
) : GslMatrix<ULong, gsl_matrix_ulong>(scope) {
owned: Boolean,
) : GslMatrix<ULong, gsl_matrix_ulong>(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<Buffer<ULong>>
@ -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<gsl_matrix_int>,
scope: AutofreeScope,
) : GslMatrix<Int, gsl_matrix_int>(scope) {
owned: Boolean,
) : GslMatrix<Int, gsl_matrix_int>(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<Buffer<Int>>
@ -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<gsl_matrix_uint>,
scope: AutofreeScope,
) : GslMatrix<UInt, gsl_matrix_uint>(scope) {
owned: Boolean,
) : GslMatrix<UInt, gsl_matrix_uint>(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<Buffer<UInt>>
@ -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)

View File

@ -3,21 +3,19 @@ package kscience.kmath.gsl
import kotlinx.cinterop.*
import org.gnu.gsl.*
internal class GslRealVector(override val rawNativeHandle: CPointer<gsl_vector>, scope: AutofreeScope) :
GslVector<Double, gsl_vector>(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<gsl_vector>,
scope: AutofreeScope,
owned: Boolean,
) : GslVector<Double, gsl_vector>(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<gsl_vector>,
override fun close(): Unit = gsl_vector_free(nativeHandle)
}
internal class GslFloatVector(override val rawNativeHandle: CPointer<gsl_vector_float>, scope: AutofreeScope) :
GslVector<Float, gsl_vector_float>(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<gsl_vector_float>,
scope: AutofreeScope,
owned: Boolean,
) : GslVector<Float, gsl_vector_float>(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<gsl_vector_
override fun close(): Unit = gsl_vector_float_free(nativeHandle)
}
internal class GslShortVector(override val rawNativeHandle: CPointer<gsl_vector_short>, scope: AutofreeScope) :
GslVector<Short, gsl_vector_short>(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<gsl_vector_short>,
scope: AutofreeScope,
owned: Boolean,
) : GslVector<Short, gsl_vector_short>(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<gsl_vector_
override fun close(): Unit = gsl_vector_short_free(nativeHandle)
}
internal class GslUShortVector(override val rawNativeHandle: CPointer<gsl_vector_ushort>, scope: AutofreeScope) :
GslVector<UShort, gsl_vector_ushort>(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<gsl_vector_ushort>,
scope: AutofreeScope,
owned: Boolean,
) : GslVector<UShort, gsl_vector_ushort>(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<gsl_vector
override fun close(): Unit = gsl_vector_ushort_free(nativeHandle)
}
internal class GslLongVector(override val rawNativeHandle: CPointer<gsl_vector_long>, scope: AutofreeScope) :
GslVector<Long, gsl_vector_long>(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<gsl_vector_long>,
scope: AutofreeScope,
owned: Boolean,
) : GslVector<Long, gsl_vector_long>(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<gsl_vector_l
override fun close(): Unit = gsl_vector_long_free(nativeHandle)
}
internal class GslULongVector(override val rawNativeHandle: CPointer<gsl_vector_ulong>, scope: AutofreeScope) :
GslVector<ULong, gsl_vector_ulong>(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<gsl_vector_ulong>,
scope: AutofreeScope,
owned: Boolean,
) : GslVector<ULong, gsl_vector_ulong>(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<gsl_vector_
override fun close(): Unit = gsl_vector_ulong_free(nativeHandle)
}
internal class GslIntVector(override val rawNativeHandle: CPointer<gsl_vector_int>, scope: AutofreeScope) :
GslVector<Int, gsl_vector_int>(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<gsl_vector_int>,
scope: AutofreeScope,
owned: Boolean,
) : GslVector<Int, gsl_vector_int>(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<gsl_vector_in
override fun close(): Unit = gsl_vector_int_free(nativeHandle)
}
internal class GslUIntVector(override val rawNativeHandle: CPointer<gsl_vector_uint>, scope: AutofreeScope) :
GslVector<UInt, gsl_vector_uint>(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<gsl_vector_uint>,
scope: AutofreeScope,
owned: Boolean,
) : GslVector<UInt, gsl_vector_uint>(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 {

View File

@ -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

View File

@ -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