v0.2.0 #206

Merged
altavir merged 210 commits from dev into master 2021-02-21 16:33:25 +03:00
7 changed files with 60 additions and 108 deletions
Showing only changes of commit d17dbd365c - Show all commits

View File

@ -19,7 +19,6 @@ private fun KtPsiFactory.createMatrixClass(
@Language("kotlin") val text = """internal class $className(
override val nativeHandle: CPointer<$structName>,
features: Set<MatrixFeature> = 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<MatrixFeature> = 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())

View File

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

View File

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

View File

@ -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<gsl_complex> = cValue {
dat[1] = im
}
internal class GslComplexMatrix(
override val nativeHandle: CPointer<gsl_matrix_complex>,
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope,
) : GslMatrix<Complex, gsl_matrix_complex>(scope) {
internal class GslComplexMatrix(override val nativeHandle: CPointer<gsl_matrix_complex>, scope: DeferScope) :
GslMatrix<Complex, gsl_matrix_complex>(scope) {
override val rowNum: Int
get() = nativeHandleChecked().pointed.size1.toInt()
override val colNum: Int
get() = nativeHandleChecked().pointed.size2.toInt()
override val features: Set<MatrixFeature> = 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())

View File

@ -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<T : Any, H : CStructVar> internal constructor(scope: DeferScope) :
GslMemoryHolder<H>(scope),
FeaturedMatrix<T> {
GslMemoryHolder<H>(scope), Matrix<T> {
internal abstract operator fun set(i: Int, j: Int, value: T)
internal abstract fun copy(): GslMatrix<T, H>

View File

@ -34,10 +34,10 @@ public abstract class GslMatrixContext<T : Any, H1 : CStructVar, H2 : CStructVar
* Converts this matrix to GSL one.
*/
@Suppress("UNCHECKED_CAST")
public fun Matrix<T>.toGsl(): GslMatrix<T, H1> = (if (this is GslMatrix<*, *>)
public fun Matrix<T>.toGsl(): GslMatrix<T, H1> = if (this is GslMatrix<*, *>)
this as GslMatrix<T, H1>
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<T : Any, H1 : CStructVar, H2 : CStructVar
*/
public class GslRealMatrixContext(scope: DeferScope) : GslMatrixContext<Double, gsl_matrix, gsl_vector>(scope) {
override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix<Double, gsl_matrix> =
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<Double, gsl_vector> =
GslRealVector(nativeHandle = requireNotNull(gsl_vector_alloc(size.toULong())), scope = scope)
@ -86,22 +83,28 @@ public class GslRealMatrixContext(scope: DeferScope) : GslMatrixContext<Double,
}
public override fun Matrix<Double>.times(value: Double): GslMatrix<Double, gsl_matrix> {
val g1 = toGsl()
val g1 = toGsl().copy()
gsl_matrix_scale(g1.nativeHandle, value)
return g1
}
public override fun add(a: Matrix<Double>, b: Matrix<Double>): GslMatrix<Double, gsl_matrix> {
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<Double>, k: Number): GslMatrix<Double, gsl_matrix> {
val g1 = a.toGsl()
val g1 = a.toGsl().copy()
gsl_matrix_scale(g1.nativeHandle, k.toDouble())
return g1
}
public override fun Matrix<Double>.minus(b: Matrix<Double>): Matrix<Double> {
val g1 = toGsl().copy()
gsl_matrix_sub(g1.nativeHandle, b.toGsl().nativeHandle)
return g1
}
}
/**
@ -115,11 +118,13 @@ public fun <R> GslRealMatrixContext(block: GslRealMatrixContext.() -> R): R =
*/
public class GslFloatMatrixContext(scope: DeferScope) :
GslMatrixContext<Float, gsl_matrix_float, gsl_vector_float>(scope) {
override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix<Float, gsl_matrix_float> =
GslFloatMatrix(requireNotNull(gsl_matrix_float_alloc(rows.toULong(), columns.toULong())), scope = scope)
override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix<Float, gsl_matrix_float> = GslFloatMatrix(
nativeHandle = requireNotNull(gsl_matrix_float_alloc(rows.toULong(), columns.toULong())),
scope = scope,
)
override fun produceDirtyVector(size: Int): GslVector<Float, gsl_vector_float> =
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<Float>.dot(other: Matrix<Float>): GslMatrix<Float, gsl_matrix_float> {
val x = toGsl().nativeHandle
@ -138,22 +143,28 @@ public class GslFloatMatrixContext(scope: DeferScope) :
}
public override fun Matrix<Float>.times(value: Float): GslMatrix<Float, gsl_matrix_float> {
val g1 = toGsl()
val g1 = toGsl().copy()
gsl_matrix_float_scale(g1.nativeHandle, value.toDouble())
return g1
}
public override fun add(a: Matrix<Float>, b: Matrix<Float>): GslMatrix<Float, gsl_matrix_float> {
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<Float>, k: Number): GslMatrix<Float, gsl_matrix_float> {
val g1 = a.toGsl()
val g1 = a.toGsl().copy()
gsl_matrix_float_scale(g1.nativeHandle, k.toDouble())
return g1
}
public override fun Matrix<Float>.minus(b: Matrix<Float>): Matrix<Float> {
val g1 = toGsl().copy()
gsl_matrix_float_sub(g1.nativeHandle, b.toGsl().nativeHandle)
return g1
}
}
/**
@ -167,14 +178,13 @@ public fun <R> GslFloatMatrixContext(block: GslFloatMatrixContext.() -> R): R =
*/
public class GslComplexMatrixContext(scope: DeferScope) :
GslMatrixContext<Complex, gsl_matrix_complex, gsl_vector_complex>(scope) {
override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix<Complex, gsl_matrix_complex> =
GslComplexMatrix(
override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix<Complex, gsl_matrix_complex> = GslComplexMatrix(
nativeHandle = requireNotNull(gsl_matrix_complex_alloc(rows.toULong(), columns.toULong())),
scope = scope
scope = scope,
)
override fun produceDirtyVector(size: Int): GslVector<Complex, gsl_vector_complex> =
GslComplexVector(requireNotNull(gsl_vector_complex_alloc(size.toULong())), scope)
GslComplexVector(nativeHandle = requireNotNull(gsl_vector_complex_alloc(size.toULong())), scope = scope)
public override fun Matrix<Complex>.dot(other: Matrix<Complex>): GslMatrix<Complex, gsl_matrix_complex> {
val x = toGsl().nativeHandle
@ -193,22 +203,28 @@ public class GslComplexMatrixContext(scope: DeferScope) :
}
public override fun Matrix<Complex>.times(value: Complex): GslMatrix<Complex, gsl_matrix_complex> {
val g1 = toGsl()
val g1 = toGsl().copy()
gsl_matrix_complex_scale(g1.nativeHandle, value.toGsl())
return g1
}
public override fun add(a: Matrix<Complex>, b: Matrix<Complex>): GslMatrix<Complex, gsl_matrix_complex> {
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<Complex>, k: Number): GslMatrix<Complex, gsl_matrix_complex> {
val g1 = a.toGsl()
val g1 = a.toGsl().copy()
gsl_matrix_complex_scale(g1.nativeHandle, k.toComplex().toGsl())
return g1
}
public override fun Matrix<Complex>.minus(b: Matrix<Complex>): Matrix<Complex> {
val g1 = toGsl().copy()
gsl_matrix_complex_sub(g1.nativeHandle, b.toGsl().nativeHandle)
return g1
}
}
/**

View File

@ -6,7 +6,6 @@ import org.gnu.gsl.*
internal class GslRealMatrix(
override val nativeHandle: CPointer<gsl_matrix>,
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<Double, gsl_matrix>(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<MatrixFeature> = 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<gsl_matrix_float>,
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<Float, gsl_matrix_float>(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<MatrixFeature> = 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<gsl_matrix_short>,
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<Short, gsl_matrix_short>(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<MatrixFeature> = 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<gsl_matrix_ushort>,
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<UShort, gsl_matrix_ushort>(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<MatrixFeature> = 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<gsl_matrix_long>,
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<Long, gsl_matrix_long>(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<MatrixFeature> = 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<gsl_matrix_ulong>,
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<ULong, gsl_matrix_ulong>(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<MatrixFeature> = 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<gsl_matrix_int>,
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<Int, gsl_matrix_int>(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<MatrixFeature> = 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<gsl_matrix_uint>,
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<UInt, gsl_matrix_uint>(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<MatrixFeature> = 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())