From d17dbd365c5bf77f76d0c40ebd3850ddf92ce90d Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Wed, 20 Jan 2021 01:03:55 +0700 Subject: [PATCH] Fix issues related to merge --- .../kmath/gsl/codegen/matricesCodegen.kt | 8 +-- kmath-gsl/build.gradle.kts | 10 +-- .../src/nativeInterop/cinterop/libgsl.def | 2 +- .../kotlin/kscience/kmath/gsl/GslComplex.kt | 15 +---- .../kotlin/kscience/kmath/gsl/GslMatrix.kt | 5 +- .../kscience/kmath/gsl/GslMatrixContext.kt | 64 ++++++++++++------- .../kotlin/kscience/kmath/gsl/_Matrices.kt | 64 +++---------------- 7 files changed, 60 insertions(+), 108 deletions(-) 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 668ecb625..0dd880f7b 100644 --- a/buildSrc/src/main/kotlin/kscience/kmath/gsl/codegen/matricesCodegen.kt +++ b/buildSrc/src/main/kotlin/kscience/kmath/gsl/codegen/matricesCodegen.kt @@ -19,7 +19,6 @@ private fun KtPsiFactory.createMatrixClass( @Language("kotlin") val text = """internal class $className( override val nativeHandle: CPointer<$structName>, - features: Set = emptySet(), scope: DeferScope ) : GslMatrix<$kotlinTypeName, $structName>(scope) { override val rowNum: Int @@ -28,11 +27,6 @@ private fun KtPsiFactory.createMatrixClass( override val colNum: Int get() = nativeHandleChecked().pointed.size2.toInt() - override val features: Set = features - - override fun suggestFeature(vararg features: MatrixFeature): $className = - ${className}(nativeHandleChecked(), this.features + features, scope) - override operator fun get(i: Int, j: Int): $kotlinTypeName = ${fn("gsl_matrixRget", cTypeName)}(nativeHandleChecked(), i.toULong(), j.toULong()) @@ -42,7 +36,7 @@ private fun KtPsiFactory.createMatrixClass( override fun copy(): $className { val new = requireNotNull(${fn("gsl_matrixRalloc", cTypeName)}(rowNum.toULong(), colNum.toULong())) ${fn("gsl_matrixRmemcpy", cTypeName)}(new, nativeHandleChecked()) - return $className(new, features, scope) + return $className(new, scope) } override fun close(): Unit = ${fn("gsl_matrixRfree", cTypeName)}(nativeHandleChecked()) diff --git a/kmath-gsl/build.gradle.kts b/kmath-gsl/build.gradle.kts index df405326f..83859130c 100644 --- a/kmath-gsl/build.gradle.kts +++ b/kmath-gsl/build.gradle.kts @@ -12,7 +12,7 @@ kotlin { val nativeTarget = when (System.getProperty("os.name")) { // "Mac OS X" -> macosX64() - "Linux" -> linuxX64() + "Linux" -> linuxX64("native") else -> { logger.warn("Current OS cannot build any of kmath-gsl targets.") @@ -29,7 +29,7 @@ kotlin { val test by nativeTarget.compilations.getting sourceSets { - val nativeMain by creating { + val nativeMain by getting { val codegen by tasks.creating { matricesCodegen(kotlin.srcDirs.first().absolutePath + "/kscience/kmath/gsl/_Matrices.kt") vectorsCodegen(kotlin.srcDirs.first().absolutePath + "/kscience/kmath/gsl/_Vectors.kt") @@ -42,11 +42,11 @@ kotlin { } } - val nativeTest by creating { + val nativeTest by getting { dependsOn(nativeMain) } - main.defaultSourceSet.dependsOn(nativeMain) - test.defaultSourceSet.dependsOn(nativeTest) +// main.defaultSourceSet.dependsOn(nativeMain) +// test.defaultSourceSet.dependsOn(nativeTest) } } diff --git a/kmath-gsl/src/nativeInterop/cinterop/libgsl.def b/kmath-gsl/src/nativeInterop/cinterop/libgsl.def index 23ceebf02..bd3a861f0 100644 --- a/kmath-gsl/src/nativeInterop/cinterop/libgsl.def +++ b/kmath-gsl/src/nativeInterop/cinterop/libgsl.def @@ -1,5 +1,5 @@ package=org.gnu.gsl -headers=gsl/gsl_blas.h +headers=gsl/gsl_blas.h gsl/gsl_linalg.h compilerOpts.linux=-I/usr/include compilerOpts.osx=-I/usr/local -I/usr/local/include linkerOpts.linux=-L/usr/lib64 -L/usr/lib/x86_64-linux-gnu -lgsl 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 3041ad718..0233ed8bc 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslComplex.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslComplex.kt @@ -1,7 +1,6 @@ package kscience.kmath.gsl import kotlinx.cinterop.* -import kscience.kmath.linear.MatrixFeature import kscience.kmath.operations.Complex import org.gnu.gsl.* @@ -12,22 +11,14 @@ internal fun Complex.toGsl(): CValue = cValue { dat[1] = im } -internal class GslComplexMatrix( - override val nativeHandle: CPointer, - features: Set = emptySet(), - scope: DeferScope, -) : GslMatrix(scope) { +internal class GslComplexMatrix(override val nativeHandle: CPointer, scope: DeferScope) : + GslMatrix(scope) { override val rowNum: Int get() = nativeHandleChecked().pointed.size1.toInt() override val colNum: Int get() = nativeHandleChecked().pointed.size2.toInt() - override val features: Set = features - - override fun suggestFeature(vararg features: MatrixFeature): GslComplexMatrix = - GslComplexMatrix(nativeHandleChecked(), this.features + features, scope) - override operator fun get(i: Int, j: Int): Complex = gsl_matrix_complex_get(nativeHandleChecked(), i.toULong(), j.toULong()).toKMath() @@ -37,7 +28,7 @@ internal class GslComplexMatrix( override fun copy(): GslComplexMatrix { val new = requireNotNull(gsl_matrix_complex_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_complex_memcpy(new, nativeHandleChecked()) - return GslComplexMatrix(new, features, scope) + return GslComplexMatrix(new, scope) } override fun close(): Unit = gsl_matrix_complex_free(nativeHandleChecked()) diff --git a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrix.kt b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrix.kt index e08c0c0d1..13283946e 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrix.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrix.kt @@ -2,15 +2,14 @@ package kscience.kmath.gsl import kotlinx.cinterop.CStructVar import kotlinx.cinterop.DeferScope -import kscience.kmath.linear.FeaturedMatrix +import kscience.kmath.structures.Matrix import kscience.kmath.structures.NDStructure /** * Wraps gsl_matrix_* objects from GSL. */ public abstract class GslMatrix internal constructor(scope: DeferScope) : - GslMemoryHolder(scope), - FeaturedMatrix { + GslMemoryHolder(scope), 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 2b0412752..95437c8c7 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrixContext.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/GslMatrixContext.kt @@ -34,10 +34,10 @@ public abstract class GslMatrixContext.toGsl(): GslMatrix = (if (this is GslMatrix<*, *>) + public fun Matrix.toGsl(): GslMatrix = if (this is GslMatrix<*, *>) this as GslMatrix else - produce(rowNum, colNum) { i, j -> this[i, j] }).copy() + produce(rowNum, colNum) { i, j -> this[i, j] } /** * Converts this point to GSL one. @@ -61,10 +61,7 @@ public abstract class GslMatrixContext(scope) { override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix = - GslRealMatrix( - nativeHandle = requireNotNull(gsl_matrix_alloc(rows.toULong(), columns.toULong())), - scope = scope - ) + GslRealMatrix(nativeHandle = requireNotNull(gsl_matrix_alloc(rows.toULong(), columns.toULong())), scope = scope) override fun produceDirtyVector(size: Int): GslVector = GslRealVector(nativeHandle = requireNotNull(gsl_vector_alloc(size.toULong())), scope = scope) @@ -86,22 +83,28 @@ public class GslRealMatrixContext(scope: DeferScope) : GslMatrixContext.times(value: Double): GslMatrix { - val g1 = toGsl() + val g1 = toGsl().copy() gsl_matrix_scale(g1.nativeHandle, value) return g1 } public override fun add(a: Matrix, b: Matrix): GslMatrix { - val g1 = a.toGsl() + val g1 = a.toGsl().copy() gsl_matrix_add(g1.nativeHandle, b.toGsl().nativeHandle) return g1 } public override fun multiply(a: Matrix, k: Number): GslMatrix { - val g1 = a.toGsl() + val g1 = a.toGsl().copy() gsl_matrix_scale(g1.nativeHandle, k.toDouble()) return g1 } + + public override fun Matrix.minus(b: Matrix): Matrix { + val g1 = toGsl().copy() + gsl_matrix_sub(g1.nativeHandle, b.toGsl().nativeHandle) + return g1 + } } /** @@ -115,11 +118,13 @@ public fun GslRealMatrixContext(block: GslRealMatrixContext.() -> R): R = */ public class GslFloatMatrixContext(scope: DeferScope) : GslMatrixContext(scope) { - override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix = - GslFloatMatrix(requireNotNull(gsl_matrix_float_alloc(rows.toULong(), columns.toULong())), scope = scope) + override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix = GslFloatMatrix( + nativeHandle = requireNotNull(gsl_matrix_float_alloc(rows.toULong(), columns.toULong())), + scope = scope, + ) override fun produceDirtyVector(size: Int): GslVector = - GslFloatVector(requireNotNull(gsl_vector_float_alloc(size.toULong())), scope) + GslFloatVector(nativeHandle = requireNotNull(value = gsl_vector_float_alloc(size.toULong())), scope = scope) public override fun Matrix.dot(other: Matrix): GslMatrix { val x = toGsl().nativeHandle @@ -138,22 +143,28 @@ public class GslFloatMatrixContext(scope: DeferScope) : } public override fun Matrix.times(value: Float): GslMatrix { - val g1 = toGsl() + val g1 = toGsl().copy() gsl_matrix_float_scale(g1.nativeHandle, value.toDouble()) return g1 } public override fun add(a: Matrix, b: Matrix): GslMatrix { - val g1 = a.toGsl() + val g1 = a.toGsl().copy() gsl_matrix_float_add(g1.nativeHandle, b.toGsl().nativeHandle) return g1 } public override fun multiply(a: Matrix, k: Number): GslMatrix { - val g1 = a.toGsl() + val g1 = a.toGsl().copy() gsl_matrix_float_scale(g1.nativeHandle, k.toDouble()) return g1 } + + public override fun Matrix.minus(b: Matrix): Matrix { + val g1 = toGsl().copy() + gsl_matrix_float_sub(g1.nativeHandle, b.toGsl().nativeHandle) + return g1 + } } /** @@ -167,14 +178,13 @@ public fun GslFloatMatrixContext(block: GslFloatMatrixContext.() -> R): R = */ public class GslComplexMatrixContext(scope: DeferScope) : GslMatrixContext(scope) { - override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix = - GslComplexMatrix( - nativeHandle = requireNotNull(gsl_matrix_complex_alloc(rows.toULong(), columns.toULong())), - scope = scope - ) + override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix = GslComplexMatrix( + nativeHandle = requireNotNull(gsl_matrix_complex_alloc(rows.toULong(), columns.toULong())), + scope = scope, + ) override fun produceDirtyVector(size: Int): GslVector = - GslComplexVector(requireNotNull(gsl_vector_complex_alloc(size.toULong())), scope) + GslComplexVector(nativeHandle = requireNotNull(gsl_vector_complex_alloc(size.toULong())), scope = scope) public override fun Matrix.dot(other: Matrix): GslMatrix { val x = toGsl().nativeHandle @@ -193,22 +203,28 @@ public class GslComplexMatrixContext(scope: DeferScope) : } public override fun Matrix.times(value: Complex): GslMatrix { - val g1 = toGsl() + val g1 = toGsl().copy() gsl_matrix_complex_scale(g1.nativeHandle, value.toGsl()) return g1 } public override fun add(a: Matrix, b: Matrix): GslMatrix { - val g1 = a.toGsl() + val g1 = a.toGsl().copy() gsl_matrix_complex_add(g1.nativeHandle, b.toGsl().nativeHandle) return g1 } public override fun multiply(a: Matrix, k: Number): GslMatrix { - val g1 = a.toGsl() + val g1 = a.toGsl().copy() gsl_matrix_complex_scale(g1.nativeHandle, k.toComplex().toGsl()) return g1 } + + public override fun Matrix.minus(b: Matrix): Matrix { + val g1 = toGsl().copy() + gsl_matrix_complex_sub(g1.nativeHandle, b.toGsl().nativeHandle) + return g1 + } } /** 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 6219dc7b6..07bc8af79 100644 --- a/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/_Matrices.kt +++ b/kmath-gsl/src/nativeMain/kotlin/kscience/kmath/gsl/_Matrices.kt @@ -6,7 +6,6 @@ import org.gnu.gsl.* internal class GslRealMatrix( override val nativeHandle: CPointer, - features: Set = emptySet(), scope: DeferScope ) : GslMatrix(scope) { override val rowNum: Int @@ -15,11 +14,6 @@ internal class GslRealMatrix( override val colNum: Int get() = nativeHandleChecked().pointed.size2.toInt() - override val features: Set = features - - override fun suggestFeature(vararg features: MatrixFeature): GslRealMatrix = - GslRealMatrix(nativeHandleChecked(), this.features + features, scope) - override operator fun get(i: Int, j: Int): Double = gsl_matrix_get(nativeHandleChecked(), i.toULong(), j.toULong()) @@ -29,7 +23,7 @@ internal class GslRealMatrix( override fun copy(): GslRealMatrix { val new = requireNotNull(gsl_matrix_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_memcpy(new, nativeHandleChecked()) - return GslRealMatrix(new, features, scope) + return GslRealMatrix(new, scope) } override fun close(): Unit = gsl_matrix_free(nativeHandleChecked()) @@ -44,7 +38,6 @@ internal class GslRealMatrix( internal class GslFloatMatrix( override val nativeHandle: CPointer, - features: Set = emptySet(), scope: DeferScope ) : GslMatrix(scope) { override val rowNum: Int @@ -53,11 +46,6 @@ internal class GslFloatMatrix( override val colNum: Int get() = nativeHandleChecked().pointed.size2.toInt() - override val features: Set = features - - override fun suggestFeature(vararg features: MatrixFeature): GslFloatMatrix = - GslFloatMatrix(nativeHandleChecked(), this.features + features, scope) - override operator fun get(i: Int, j: Int): Float = gsl_matrix_float_get(nativeHandleChecked(), i.toULong(), j.toULong()) @@ -67,7 +55,7 @@ internal class GslFloatMatrix( override fun copy(): GslFloatMatrix { val new = requireNotNull(gsl_matrix_float_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_float_memcpy(new, nativeHandleChecked()) - return GslFloatMatrix(new, features, scope) + return GslFloatMatrix(new, scope) } override fun close(): Unit = gsl_matrix_float_free(nativeHandleChecked()) @@ -82,7 +70,6 @@ internal class GslFloatMatrix( internal class GslShortMatrix( override val nativeHandle: CPointer, - features: Set = emptySet(), scope: DeferScope ) : GslMatrix(scope) { override val rowNum: Int @@ -91,11 +78,6 @@ internal class GslShortMatrix( override val colNum: Int get() = nativeHandleChecked().pointed.size2.toInt() - override val features: Set = features - - override fun suggestFeature(vararg features: MatrixFeature): GslShortMatrix = - GslShortMatrix(nativeHandleChecked(), this.features + features, scope) - override operator fun get(i: Int, j: Int): Short = gsl_matrix_short_get(nativeHandleChecked(), i.toULong(), j.toULong()) @@ -105,7 +87,7 @@ internal class GslShortMatrix( override fun copy(): GslShortMatrix { val new = requireNotNull(gsl_matrix_short_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_short_memcpy(new, nativeHandleChecked()) - return GslShortMatrix(new, features, scope) + return GslShortMatrix(new, scope) } override fun close(): Unit = gsl_matrix_short_free(nativeHandleChecked()) @@ -120,7 +102,6 @@ internal class GslShortMatrix( internal class GslUShortMatrix( override val nativeHandle: CPointer, - features: Set = emptySet(), scope: DeferScope ) : GslMatrix(scope) { override val rowNum: Int @@ -129,11 +110,6 @@ internal class GslUShortMatrix( override val colNum: Int get() = nativeHandleChecked().pointed.size2.toInt() - override val features: Set = features - - override fun suggestFeature(vararg features: MatrixFeature): GslUShortMatrix = - GslUShortMatrix(nativeHandleChecked(), this.features + features, scope) - override operator fun get(i: Int, j: Int): UShort = gsl_matrix_ushort_get(nativeHandleChecked(), i.toULong(), j.toULong()) @@ -143,7 +119,7 @@ internal class GslUShortMatrix( override fun copy(): GslUShortMatrix { val new = requireNotNull(gsl_matrix_ushort_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_ushort_memcpy(new, nativeHandleChecked()) - return GslUShortMatrix(new, features, scope) + return GslUShortMatrix(new, scope) } override fun close(): Unit = gsl_matrix_ushort_free(nativeHandleChecked()) @@ -158,7 +134,6 @@ internal class GslUShortMatrix( internal class GslLongMatrix( override val nativeHandle: CPointer, - features: Set = emptySet(), scope: DeferScope ) : GslMatrix(scope) { override val rowNum: Int @@ -167,11 +142,6 @@ internal class GslLongMatrix( override val colNum: Int get() = nativeHandleChecked().pointed.size2.toInt() - override val features: Set = features - - override fun suggestFeature(vararg features: MatrixFeature): GslLongMatrix = - GslLongMatrix(nativeHandleChecked(), this.features + features, scope) - override operator fun get(i: Int, j: Int): Long = gsl_matrix_long_get(nativeHandleChecked(), i.toULong(), j.toULong()) @@ -181,7 +151,7 @@ internal class GslLongMatrix( override fun copy(): GslLongMatrix { val new = requireNotNull(gsl_matrix_long_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_long_memcpy(new, nativeHandleChecked()) - return GslLongMatrix(new, features, scope) + return GslLongMatrix(new, scope) } override fun close(): Unit = gsl_matrix_long_free(nativeHandleChecked()) @@ -196,7 +166,6 @@ internal class GslLongMatrix( internal class GslULongMatrix( override val nativeHandle: CPointer, - features: Set = emptySet(), scope: DeferScope ) : GslMatrix(scope) { override val rowNum: Int @@ -205,11 +174,6 @@ internal class GslULongMatrix( override val colNum: Int get() = nativeHandleChecked().pointed.size2.toInt() - override val features: Set = features - - override fun suggestFeature(vararg features: MatrixFeature): GslULongMatrix = - GslULongMatrix(nativeHandleChecked(), this.features + features, scope) - override operator fun get(i: Int, j: Int): ULong = gsl_matrix_ulong_get(nativeHandleChecked(), i.toULong(), j.toULong()) @@ -219,7 +183,7 @@ internal class GslULongMatrix( override fun copy(): GslULongMatrix { val new = requireNotNull(gsl_matrix_ulong_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_ulong_memcpy(new, nativeHandleChecked()) - return GslULongMatrix(new, features, scope) + return GslULongMatrix(new, scope) } override fun close(): Unit = gsl_matrix_ulong_free(nativeHandleChecked()) @@ -234,7 +198,6 @@ internal class GslULongMatrix( internal class GslIntMatrix( override val nativeHandle: CPointer, - features: Set = emptySet(), scope: DeferScope ) : GslMatrix(scope) { override val rowNum: Int @@ -243,11 +206,6 @@ internal class GslIntMatrix( override val colNum: Int get() = nativeHandleChecked().pointed.size2.toInt() - override val features: Set = features - - override fun suggestFeature(vararg features: MatrixFeature): GslIntMatrix = - GslIntMatrix(nativeHandleChecked(), this.features + features, scope) - override operator fun get(i: Int, j: Int): Int = gsl_matrix_int_get(nativeHandleChecked(), i.toULong(), j.toULong()) @@ -257,7 +215,7 @@ internal class GslIntMatrix( override fun copy(): GslIntMatrix { val new = requireNotNull(gsl_matrix_int_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_int_memcpy(new, nativeHandleChecked()) - return GslIntMatrix(new, features, scope) + return GslIntMatrix(new, scope) } override fun close(): Unit = gsl_matrix_int_free(nativeHandleChecked()) @@ -272,7 +230,6 @@ internal class GslIntMatrix( internal class GslUIntMatrix( override val nativeHandle: CPointer, - features: Set = emptySet(), scope: DeferScope ) : GslMatrix(scope) { override val rowNum: Int @@ -281,11 +238,6 @@ internal class GslUIntMatrix( override val colNum: Int get() = nativeHandleChecked().pointed.size2.toInt() - override val features: Set = features - - override fun suggestFeature(vararg features: MatrixFeature): GslUIntMatrix = - GslUIntMatrix(nativeHandleChecked(), this.features + features, scope) - override operator fun get(i: Int, j: Int): UInt = gsl_matrix_uint_get(nativeHandleChecked(), i.toULong(), j.toULong()) @@ -295,7 +247,7 @@ internal class GslUIntMatrix( override fun copy(): GslUIntMatrix { val new = requireNotNull(gsl_matrix_uint_alloc(rowNum.toULong(), colNum.toULong())) gsl_matrix_uint_memcpy(new, nativeHandleChecked()) - return GslUIntMatrix(new, features, scope) + return GslUIntMatrix(new, scope) } override fun close(): Unit = gsl_matrix_uint_free(nativeHandleChecked())