Add first working test, use kotlinx-io fork, major rework of GSL API

This commit is contained in:
Iaroslav Postovalov 2020-10-04 20:17:44 +07:00
parent eedfcf3932
commit abcde808dc
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
12 changed files with 348 additions and 136 deletions

View File

@ -12,6 +12,7 @@ allprojects {
maven("https://dl.bintray.com/kotlin/kotlin-eap")
maven("https://dl.bintray.com/kotlin/kotlinx")
maven("https://dl.bintray.com/hotkeytlt/maven")
maven("https://dl.bintray.com/commandertvis/kotlinx-io/")
}
group = "kscience.kmath"

View File

@ -26,7 +26,6 @@ dependencies {
implementation(project(":kmath-viktor"))
implementation(project(":kmath-dimensions"))
implementation(project(":kmath-ejml"))
implementation("org.jetbrains.kotlinx:kotlinx-io:0.2.0-npm-dev-11")
implementation("org.jetbrains.kotlinx:kotlinx.benchmark.runtime:0.2.0-dev-20")
implementation("org.slf4j:slf4j-simple:1.7.30")
"benchmarksImplementation"("org.jetbrains.kotlinx:kotlinx.benchmark.runtime-jvm:0.2.0-dev-8")

View File

@ -18,7 +18,8 @@ public interface MatrixContext<T : Any> : SpaceOperations<Matrix<T>> {
*/
public fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> T): Matrix<T>
public override fun binaryOperation(operation: String, left: Matrix<T>, right: Matrix<T>): Matrix<T> = when (operation) {
public override fun binaryOperation(operation: String, left: Matrix<T>, right: Matrix<T>): Matrix<T> =
when (operation) {
"dot" -> left dot right
else -> super.binaryOperation(operation, left, right)
}

View File

@ -1,11 +1,11 @@
package kscience.kmath.ejml
import org.ejml.simple.SimpleMatrix
import kscience.kmath.linear.MatrixContext
import kscience.kmath.linear.Point
import kscience.kmath.operations.Space
import kscience.kmath.operations.invoke
import kscience.kmath.structures.Matrix
import org.ejml.simple.SimpleMatrix
/**
* Represents context of basic operations operating with [EjmlMatrix].
@ -29,8 +29,8 @@ public class EjmlMatrixContext(private val space: Space<Double>) : MatrixContext
override fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> Double): EjmlMatrix =
EjmlMatrix(SimpleMatrix(rows, columns).also {
(0 until it.numRows()).forEach { row ->
(0 until it.numCols()).forEach { col -> it[row, col] = initializer(row, col) }
(0 until rows).forEach { row ->
(0 until columns).forEach { col -> it[row, col] = initializer(row, col) }
}
})
@ -49,7 +49,8 @@ public class EjmlMatrixContext(private val space: Space<Double>) : MatrixContext
public override fun multiply(a: Matrix<Double>, k: Number): EjmlMatrix =
produce(a.rowNum, a.colNum) { i, j -> space { a[i, j] * k } }
public override operator fun Matrix<Double>.times(value: Double): EjmlMatrix = EjmlMatrix(toEjml().origin.scale(value))
public override operator fun Matrix<Double>.times(value: Double): EjmlMatrix =
EjmlMatrix(toEjml().origin.scale(value))
public companion object
}

View File

@ -8,6 +8,7 @@ kotlin {
val nativeTarget = when (System.getProperty("os.name")) {
"Mac OS X" -> macosX64("native")
"Linux" -> linuxX64("native")
else -> {
logger.warn("Current OS cannot build any of kmath-gsl targets.")
return@kotlin
@ -26,6 +27,7 @@ kotlin {
sourceSets.commonMain {
dependencies {
api(project(":kmath-core"))
api("org.jetbrains.kotlinx:kotlinx-io:0.2.0-tvis-3")
}
}
}

View File

@ -0,0 +1,12 @@
package kscience.kmath.gsl
import kotlinx.cinterop.*
import kscience.kmath.operations.Complex
import org.gnu.gsl.gsl_complex
internal fun CValue<gsl_complex>.toKMath(): Complex = useContents { Complex(dat[0], dat[1]) }
internal fun Complex.toGsl(): CValue<gsl_complex> = cValue {
dat[0] = re
dat[1] = im
}

View File

@ -7,7 +7,10 @@ import kscience.kmath.operations.Complex
import kscience.kmath.structures.NDStructure
import org.gnu.gsl.*
public sealed class GslMatrix<T : Any> : StructHolder(), FeaturedMatrix<T> {
public sealed class GslMatrix<T : Any, H : CStructVar> : GslMemoryHolder<H>(), FeaturedMatrix<T> {
internal abstract operator fun set(i: Int, j: Int, value: T)
internal abstract fun copy(): GslMatrix<T, H>
public override fun equals(other: Any?): Boolean {
return NDStructure.equals(this, other as? NDStructure<*> ?: return false)
}
@ -19,215 +22,318 @@ public sealed class GslMatrix<T : Any> : StructHolder(), FeaturedMatrix<T> {
}
}
public class GslRealMatrix(protected override val nativeHandle: CValues<gsl_matrix>, features: Set<MatrixFeature>) :
GslMatrix<Double>() {
public override val rowNum: Int
internal class GslRealMatrix(
override val nativeHandle: CPointer<gsl_matrix>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Double, gsl_matrix>() {
override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslRealMatrix =
override fun suggestFeature(vararg features: MatrixFeature): GslRealMatrix =
GslRealMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): Double = gsl_matrix_get(nativeHandle, i.toULong(), j.toULong())
override operator fun get(i: Int, j: Int): Double = gsl_matrix_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
override operator fun set(i: Int, j: Int, value: Double): Unit =
gsl_matrix_set(nativeHandle, i.toULong(), j.toULong(), value)
override fun copy(): GslRealMatrix = memScoped {
val new = requireNotNull(gsl_matrix_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_memcpy(new, nativeHandle)
GslRealMatrix(new, features)
}
override fun close(): Unit = gsl_matrix_free(nativeHandle)
override fun equals(other: Any?): Boolean {
if (other is GslRealMatrix) gsl_matrix_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslFloatMatrix(
protected override val nativeHandle: CValues<gsl_matrix_float>,
features: Set<MatrixFeature>
) :
GslMatrix<Float>() {
public override val rowNum: Int
internal class GslFloatMatrix(
override val nativeHandle: CPointer<gsl_matrix_float>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Float, gsl_matrix_float>() {
override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslFloatMatrix =
override fun suggestFeature(vararg features: MatrixFeature): GslFloatMatrix =
GslFloatMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): Float = gsl_matrix_float_get(nativeHandle, i.toULong(), j.toULong())
override operator fun get(i: Int, j: Int): Float =
gsl_matrix_float_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
override operator fun set(i: Int, j: Int, value: Float): Unit =
gsl_matrix_float_set(nativeHandle, i.toULong(), j.toULong(), value)
override fun copy(): GslFloatMatrix = memScoped {
val new = requireNotNull(gsl_matrix_float_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_float_memcpy(new, nativeHandle)
GslFloatMatrix(new, features)
}
override fun close(): Unit = gsl_matrix_float_free(nativeHandle)
override fun equals(other: Any?): Boolean {
if (other is GslFloatMatrix) gsl_matrix_float_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslIntMatrix(protected override val nativeHandle: CValues<gsl_matrix_int>, features: Set<MatrixFeature>) :
GslMatrix<Int>() {
internal class GslIntMatrix(
override val nativeHandle: CPointer<gsl_matrix_int>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Int, gsl_matrix_int>() {
public override val rowNum: Int
override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslIntMatrix =
override fun suggestFeature(vararg features: MatrixFeature): GslIntMatrix =
GslIntMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): Int = gsl_matrix_int_get(nativeHandle, i.toULong(), j.toULong())
override operator fun get(i: Int, j: Int): Int = gsl_matrix_int_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
override operator fun set(i: Int, j: Int, value: Int): Unit =
gsl_matrix_int_set(nativeHandle, i.toULong(), j.toULong(), value)
override fun copy(): GslIntMatrix = memScoped {
val new = requireNotNull(gsl_matrix_int_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_int_memcpy(new, nativeHandle)
GslIntMatrix(new, features)
}
override fun close(): Unit = gsl_matrix_int_free(nativeHandle)
override fun equals(other: Any?): Boolean {
if (other is GslIntMatrix) gsl_matrix_int_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslUIntMatrix(
protected override val nativeHandle: CValues<gsl_matrix_uint>,
features: Set<MatrixFeature>
) : GslMatrix<UInt>() {
internal class GslUIntMatrix(
override val nativeHandle: CPointer<gsl_matrix_uint>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<UInt, gsl_matrix_uint>() {
public override val rowNum: Int
override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslUIntMatrix =
override fun suggestFeature(vararg features: MatrixFeature): GslUIntMatrix =
GslUIntMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): UInt = gsl_matrix_uint_get(nativeHandle, i.toULong(), j.toULong())
override operator fun get(i: Int, j: Int): UInt = gsl_matrix_uint_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
override operator fun set(i: Int, j: Int, value: UInt): Unit =
gsl_matrix_uint_set(nativeHandle, i.toULong(), j.toULong(), value)
override fun copy(): GslUIntMatrix = memScoped {
val new = requireNotNull(gsl_matrix_uint_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_uint_memcpy(new, nativeHandle)
GslUIntMatrix(new, features)
}
override fun close(): Unit = gsl_matrix_uint_free(nativeHandle)
override fun equals(other: Any?): Boolean {
if (other is GslUIntMatrix) gsl_matrix_uint_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslLongMatrix(
protected override val nativeHandle: CValues<gsl_matrix_long>,
features: Set<MatrixFeature>
) :
GslMatrix<Long>() {
internal class GslLongMatrix(
override val nativeHandle: CPointer<gsl_matrix_long>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Long, gsl_matrix_long>() {
public override val rowNum: Int
override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslLongMatrix =
override fun suggestFeature(vararg features: MatrixFeature): GslLongMatrix =
GslLongMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): Long = gsl_matrix_long_get(nativeHandle, i.toULong(), j.toULong())
override operator fun get(i: Int, j: Int): Long = gsl_matrix_long_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
override operator fun set(i: Int, j: Int, value: Long): Unit =
gsl_matrix_long_set(nativeHandle, i.toULong(), j.toULong(), value)
override fun copy(): GslLongMatrix = memScoped {
val new = requireNotNull(gsl_matrix_long_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_long_memcpy(new, nativeHandle)
GslLongMatrix(new, features)
}
override fun close(): Unit = gsl_matrix_long_free(nativeHandle)
override fun equals(other: Any?): Boolean {
if (other is GslLongMatrix) gsl_matrix_long_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslULongMatrix(
protected override val nativeHandle: CValues<gsl_matrix_ulong>,
features: Set<MatrixFeature>
) : GslMatrix<ULong>() {
internal class GslULongMatrix(
override val nativeHandle: CPointer<gsl_matrix_ulong>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<ULong, gsl_matrix_ulong>() {
public override val rowNum: Int
override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslULongMatrix =
override fun suggestFeature(vararg features: MatrixFeature): GslULongMatrix =
GslULongMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): ULong = gsl_matrix_ulong_get(nativeHandle, i.toULong(), j.toULong())
override operator fun get(i: Int, j: Int): ULong =
gsl_matrix_ulong_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
override operator fun set(i: Int, j: Int, value: ULong): Unit =
gsl_matrix_ulong_set(nativeHandle, i.toULong(), j.toULong(), value)
override fun copy(): GslULongMatrix = memScoped {
val new = requireNotNull(gsl_matrix_ulong_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_ulong_memcpy(new, nativeHandle)
GslULongMatrix(new, features)
}
override fun close(): Unit = gsl_matrix_ulong_free(nativeHandle)
override fun equals(other: Any?): Boolean {
if (other is GslULongMatrix) gsl_matrix_ulong_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslShortMatrix(
protected override val nativeHandle: CValues<gsl_matrix_short>,
features: Set<MatrixFeature>
) : GslMatrix<Short>() {
internal class GslShortMatrix(
override val nativeHandle: CPointer<gsl_matrix_short>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Short, gsl_matrix_short>() {
public override val rowNum: Int
override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslShortMatrix =
override fun suggestFeature(vararg features: MatrixFeature): GslShortMatrix =
GslShortMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): Short = gsl_matrix_short_get(nativeHandle, i.toULong(), j.toULong())
override operator fun get(i: Int, j: Int): Short =
gsl_matrix_short_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
override operator fun set(i: Int, j: Int, value: Short): Unit =
gsl_matrix_short_set(nativeHandle, i.toULong(), j.toULong(), value)
override fun copy(): GslShortMatrix = memScoped {
val new = requireNotNull(gsl_matrix_short_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_short_memcpy(new, nativeHandle)
GslShortMatrix(new, features)
}
override fun close(): Unit = gsl_matrix_short_free(nativeHandle)
override fun equals(other: Any?): Boolean {
if (other is GslShortMatrix) gsl_matrix_short_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslUShortMatrix(
protected override val nativeHandle: CValues<gsl_matrix_ushort>,
features: Set<MatrixFeature>
) : GslMatrix<UShort>() {
internal class GslUShortMatrix(
override val nativeHandle: CPointer<gsl_matrix_ushort>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<UShort, gsl_matrix_ushort>() {
public override val rowNum: Int
override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslUShortMatrix =
override fun suggestFeature(vararg features: MatrixFeature): GslUShortMatrix =
GslUShortMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): UShort = gsl_matrix_ushort_get(nativeHandle, i.toULong(), j.toULong())
override operator fun get(i: Int, j: Int): UShort =
gsl_matrix_ushort_get(nativeHandle, i.toULong(), j.toULong())
public override fun equals(other: Any?): Boolean {
override operator fun set(i: Int, j: Int, value: UShort): Unit =
gsl_matrix_ushort_set(nativeHandle, i.toULong(), j.toULong(), value)
override fun copy(): GslUShortMatrix = memScoped {
val new = requireNotNull(gsl_matrix_ushort_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_ushort_memcpy(new, nativeHandle)
GslUShortMatrix(new, features)
}
override fun close(): Unit = gsl_matrix_ushort_free(nativeHandle)
override fun equals(other: Any?): Boolean {
if (other is GslUShortMatrix) gsl_matrix_ushort_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}
}
public class GslComplexMatrix(
protected override val nativeHandle: CValues<gsl_matrix_complex>,
features: Set<MatrixFeature>
) : GslMatrix<Complex>() {
public override val rowNum: Int
internal class GslComplexMatrix(
override val nativeHandle: CPointer<gsl_matrix_complex>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Complex, gsl_matrix_complex>() {
override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
public override val colNum: Int
override val colNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
public override val features: Set<MatrixFeature> = features
override val features: Set<MatrixFeature> = features
public override fun suggestFeature(vararg features: MatrixFeature): GslComplexMatrix =
override fun suggestFeature(vararg features: MatrixFeature): GslComplexMatrix =
GslComplexMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): Complex =
gsl_matrix_complex_get(nativeHandle, i.toULong(), j.toULong()).useContents { Complex(dat[0], dat[1]) }
override operator fun get(i: Int, j: Int): Complex =
gsl_matrix_complex_get(nativeHandle, i.toULong(), j.toULong()).toKMath()
public override fun equals(other: Any?): Boolean {
override operator fun set(i: Int, j: Int, value: Complex): Unit =
gsl_matrix_complex_set(nativeHandle, i.toULong(), j.toULong(), value.toGsl())
override fun copy(): GslComplexMatrix = memScoped {
val new = requireNotNull(gsl_matrix_complex_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_complex_memcpy(new, nativeHandle)
GslComplexMatrix(new, features)
}
override fun close(): Unit = gsl_matrix_complex_free(nativeHandle)
override fun equals(other: Any?): Boolean {
if (other is GslComplexMatrix) gsl_matrix_complex_equal(nativeHandle, other.nativeHandle)
return super.equals(other)
}

View File

@ -0,0 +1,57 @@
package kscience.kmath.gsl
import kotlinx.cinterop.CStructVar
import kscience.kmath.linear.MatrixContext
import kscience.kmath.linear.Point
import kscience.kmath.operations.invoke
import kscience.kmath.structures.Matrix
import org.gnu.gsl.*
private inline fun <T : Any, H : CStructVar> GslMatrix<T, H>.fill(initializer: (Int, Int) -> T): GslMatrix<T, H> =
apply {
(0 until rowNum).forEach { row -> (0 until colNum).forEach { col -> this[row, col] = initializer(row, col) } }
}
public sealed class GslMatrixContext<T : Any, H : CStructVar> : MatrixContext<T> {
@Suppress("UNCHECKED_CAST")
public fun Matrix<T>.toGsl(): GslMatrix<T, H> =
(if (this is GslMatrix<*, *>) this as GslMatrix<T, H> else produce(rowNum, colNum) { i, j -> get(i, j) }).copy()
internal abstract fun produceDirty(rows: Int, columns: Int): GslMatrix<T, H>
public override fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> T): GslMatrix<T, H> =
produceDirty(rows, columns).fill(initializer)
}
public object GslRealMatrixContext : GslMatrixContext<Double, gsl_matrix>() {
public override fun produceDirty(rows: Int, columns: Int): GslMatrix<Double, gsl_matrix> =
GslRealMatrix(requireNotNull(gsl_matrix_alloc(rows.toULong(), columns.toULong())))
public override fun Matrix<Double>.dot(other: Matrix<Double>): GslMatrix<Double, gsl_matrix> {
val g1 = toGsl()
gsl_matrix_mul_elements(g1.nativeHandle, other.toGsl().nativeHandle)
return g1
}
public override fun Matrix<Double>.dot(vector: Point<Double>): GslVector<Double, gsl_vector> {
TODO()
}
public override fun Matrix<Double>.times(value: Double): GslMatrix<Double, gsl_matrix> {
val g1 = toGsl()
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()
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()
gsl_matrix_scale(g1.nativeHandle, k.toDouble())
return g1
}
}

View File

@ -0,0 +1,9 @@
package kscience.kmath.gsl
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.CStructVar
import kotlinx.io.Closeable
public abstract class GslMemoryHolder<H : CStructVar> internal constructor() : Closeable {
internal abstract val nativeHandle: CPointer<H>
}

View File

@ -1,11 +1,14 @@
package kscience.kmath.gsl
import kotlinx.cinterop.*
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.CStructVar
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.pointed
import kscience.kmath.linear.Point
import kscience.kmath.operations.Complex
import org.gnu.gsl.*
public sealed class GslVector<T> : StructHolder(), Point<T> {
public sealed class GslVector<T, H : CStructVar> : GslMemoryHolder<H>(), Point<T> {
public override fun iterator(): Iterator<T> = object : Iterator<T> {
private var cursor = 0
@ -18,67 +21,81 @@ public sealed class GslVector<T> : StructHolder(), Point<T> {
}
}
public class GslRealVector(override val nativeHandle: CValues<gsl_vector>) : GslVector<Double>() {
public override val size: Int
internal class GslRealVector(override val nativeHandle: CPointer<gsl_vector>) : GslVector<Double, gsl_vector>() {
override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Double = gsl_vector_get(nativeHandle, index.toULong())
override fun get(index: Int): Double = gsl_vector_get(nativeHandle, index.toULong())
override fun close(): Unit = gsl_vector_free(nativeHandle)
}
public class GslFloatVector(override val nativeHandle: CValues<gsl_vector_float>) : GslVector<Float>() {
public override val size: Int
internal class GslFloatVector(override val nativeHandle: CPointer<gsl_vector_float>) :
GslVector<Float, gsl_vector_float>() {
override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Float = gsl_vector_float_get(nativeHandle, index.toULong())
override fun get(index: Int): Float = gsl_vector_float_get(nativeHandle, index.toULong())
override fun close(): Unit = gsl_vector_float_free(nativeHandle)
}
public class GslIntVector(override val nativeHandle: CValues<gsl_vector_int>) : GslVector<Int>() {
public override val size: Int
internal class GslIntVector(override val nativeHandle: CPointer<gsl_vector_int>) : GslVector<Int, gsl_vector_int>() {
override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Int = gsl_vector_int_get(nativeHandle, index.toULong())
override fun get(index: Int): Int = gsl_vector_int_get(nativeHandle, index.toULong())
override fun close(): Unit = gsl_vector_int_free(nativeHandle)
}
public class GslUIntVector(override val nativeHandle: CValues<gsl_vector_uint>) : GslVector<UInt>() {
public override val size: Int
internal class GslUIntVector(override val nativeHandle: CPointer<gsl_vector_uint>) :
GslVector<UInt, gsl_vector_uint>() {
override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): UInt = gsl_vector_uint_get(nativeHandle, index.toULong())
override fun get(index: Int): UInt = gsl_vector_uint_get(nativeHandle, index.toULong())
override fun close(): Unit = gsl_vector_uint_free(nativeHandle)
}
public class GslLongVector(override val nativeHandle: CValues<gsl_vector_long>) : GslVector<Long>() {
public override val size: Int
internal class GslLongVector(override val nativeHandle: CPointer<gsl_vector_long>) :
GslVector<Long, gsl_vector_long>() {
override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Long = gsl_vector_long_get(nativeHandle, index.toULong())
override fun get(index: Int): Long = gsl_vector_long_get(nativeHandle, index.toULong())
override fun close(): Unit = gsl_vector_long_free(nativeHandle)
}
public class GslULongVector(override val nativeHandle: CValues<gsl_vector_ulong>) : GslVector<ULong>() {
internal class GslULongVector(override val nativeHandle: CPointer<gsl_vector_ulong>) :
GslVector<ULong, gsl_vector_ulong>() {
public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): ULong = gsl_vector_ulong_get(nativeHandle, index.toULong())
public override fun close(): Unit = gsl_vector_ulong_free(nativeHandle)
}
public class GslShortVector(override val nativeHandle: CValues<gsl_vector_short>) : GslVector<Short>() {
internal class GslShortVector(override val nativeHandle: CPointer<gsl_vector_short>) :
GslVector<Short, gsl_vector_short>() {
public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Short = gsl_vector_short_get(nativeHandle, index.toULong())
public override fun close(): Unit = gsl_vector_short_free(nativeHandle)
}
public class GslUShortVector(override val nativeHandle: CValues<gsl_vector_ushort>) : GslVector<UShort>() {
public override val size: Int
internal class GslUShortVector(override val nativeHandle: CPointer<gsl_vector_ushort>) :
GslVector<UShort, gsl_vector_ushort>() {
override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): UShort = gsl_vector_ushort_get(nativeHandle, index.toULong())
override fun get(index: Int): UShort = gsl_vector_ushort_get(nativeHandle, index.toULong())
override fun close(): Unit = gsl_vector_ushort_free(nativeHandle)
}
public class GslComplexVector(override val nativeHandle: CValues<gsl_vector_complex>) : GslVector<Complex>() {
public override val size: Int
internal class GslComplexVector(override val nativeHandle: CPointer<gsl_vector_complex>) :
GslVector<Complex, gsl_vector_complex>() {
override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Complex = gsl_vector_complex_get(nativeHandle, index.toULong()).useContents {
Complex(dat[0], dat[1])
}
override fun get(index: Int): Complex = gsl_vector_complex_get(nativeHandle, index.toULong()).toKMath()
override fun close(): Unit = gsl_vector_complex_free(nativeHandle)
}

View File

@ -1,8 +0,0 @@
package kscience.kmath.gsl
import kotlinx.cinterop.CStructVar
import kotlinx.cinterop.CValues
public abstract class StructHolder internal constructor() {
protected abstract val nativeHandle: CValues<out CStructVar>
}

View File

@ -0,0 +1,15 @@
package kscience.kmath.gsl
import kotlinx.io.use
import kscience.kmath.operations.invoke
import kotlin.test.Test
import kotlin.test.assertEquals
internal class RealTest {
@Test
fun testScale() = GslRealMatrixContext {
GslRealMatrixContext.produce(10, 10) { _, _ -> 0.1 }.use { ma ->
(ma * 20.0).use { mb -> assertEquals(mb[0, 1], 2.0) }
}
}
}