v0.2.0 #206

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

View File

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

View File

@ -26,7 +26,6 @@ dependencies {
implementation(project(":kmath-viktor")) implementation(project(":kmath-viktor"))
implementation(project(":kmath-dimensions")) implementation(project(":kmath-dimensions"))
implementation(project(":kmath-ejml")) 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.jetbrains.kotlinx:kotlinx.benchmark.runtime:0.2.0-dev-20")
implementation("org.slf4j:slf4j-simple:1.7.30") implementation("org.slf4j:slf4j-simple:1.7.30")
"benchmarksImplementation"("org.jetbrains.kotlinx:kotlinx.benchmark.runtime-jvm:0.2.0-dev-8") "benchmarksImplementation"("org.jetbrains.kotlinx:kotlinx.benchmark.runtime-jvm:0.2.0-dev-8")

View File

@ -18,10 +18,11 @@ public interface MatrixContext<T : Any> : SpaceOperations<Matrix<T>> {
*/ */
public fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> T): 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> =
"dot" -> left dot right when (operation) {
else -> super.binaryOperation(operation, left, right) "dot" -> left dot right
} else -> super.binaryOperation(operation, left, right)
}
/** /**
* Computes the dot product of this matrix and another one. * Computes the dot product of this matrix and another one.

View File

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

View File

@ -8,6 +8,7 @@ kotlin {
val nativeTarget = when (System.getProperty("os.name")) { val nativeTarget = when (System.getProperty("os.name")) {
"Mac OS X" -> macosX64("native") "Mac OS X" -> macosX64("native")
"Linux" -> linuxX64("native") "Linux" -> linuxX64("native")
else -> { else -> {
logger.warn("Current OS cannot build any of kmath-gsl targets.") logger.warn("Current OS cannot build any of kmath-gsl targets.")
return@kotlin return@kotlin
@ -26,6 +27,7 @@ kotlin {
sourceSets.commonMain { sourceSets.commonMain {
dependencies { dependencies {
api(project(":kmath-core")) 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 kscience.kmath.structures.NDStructure
import org.gnu.gsl.* 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 { public override fun equals(other: Any?): Boolean {
return NDStructure.equals(this, other as? NDStructure<*> ?: return false) 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>) : internal class GslRealMatrix(
GslMatrix<Double>() { override val nativeHandle: CPointer<gsl_matrix>,
features: Set<MatrixFeature> = emptySet()
public override val rowNum: Int ) : GslMatrix<Double, gsl_matrix>() {
override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() } 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() } 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) 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) if (other is GslRealMatrix) gsl_matrix_equal(nativeHandle, other.nativeHandle)
return super.equals(other) return super.equals(other)
} }
} }
public class GslFloatMatrix( internal class GslFloatMatrix(
protected override val nativeHandle: CValues<gsl_matrix_float>, override val nativeHandle: CPointer<gsl_matrix_float>,
features: Set<MatrixFeature> features: Set<MatrixFeature> = emptySet()
) : ) : GslMatrix<Float, gsl_matrix_float>() {
GslMatrix<Float>() { override val rowNum: Int
public override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() } 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() } 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) 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) if (other is GslFloatMatrix) gsl_matrix_float_equal(nativeHandle, other.nativeHandle)
return super.equals(other) return super.equals(other)
} }
} }
public class GslIntMatrix(protected override val nativeHandle: CValues<gsl_matrix_int>, features: Set<MatrixFeature>) : internal class GslIntMatrix(
GslMatrix<Int>() { 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() } 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() } 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) 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) if (other is GslIntMatrix) gsl_matrix_int_equal(nativeHandle, other.nativeHandle)
return super.equals(other) return super.equals(other)
} }
} }
public class GslUIntMatrix( internal class GslUIntMatrix(
protected override val nativeHandle: CValues<gsl_matrix_uint>, override val nativeHandle: CPointer<gsl_matrix_uint>,
features: Set<MatrixFeature> features: Set<MatrixFeature> = emptySet()
) : GslMatrix<UInt>() { ) : GslMatrix<UInt, gsl_matrix_uint>() {
public override val rowNum: Int override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() } 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() } 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) 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) if (other is GslUIntMatrix) gsl_matrix_uint_equal(nativeHandle, other.nativeHandle)
return super.equals(other) return super.equals(other)
} }
} }
public class GslLongMatrix( internal class GslLongMatrix(
protected override val nativeHandle: CValues<gsl_matrix_long>, override val nativeHandle: CPointer<gsl_matrix_long>,
features: Set<MatrixFeature> features: Set<MatrixFeature> = emptySet()
) : ) : GslMatrix<Long, gsl_matrix_long>() {
GslMatrix<Long>() {
public override val rowNum: Int override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() } 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() } 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) 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) if (other is GslLongMatrix) gsl_matrix_long_equal(nativeHandle, other.nativeHandle)
return super.equals(other) return super.equals(other)
} }
} }
public class GslULongMatrix( internal class GslULongMatrix(
protected override val nativeHandle: CValues<gsl_matrix_ulong>, override val nativeHandle: CPointer<gsl_matrix_ulong>,
features: Set<MatrixFeature> features: Set<MatrixFeature> = emptySet()
) : GslMatrix<ULong>() { ) : GslMatrix<ULong, gsl_matrix_ulong>() {
public override val rowNum: Int override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() } 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() } 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) 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) if (other is GslULongMatrix) gsl_matrix_ulong_equal(nativeHandle, other.nativeHandle)
return super.equals(other) return super.equals(other)
} }
} }
public class GslShortMatrix( internal class GslShortMatrix(
protected override val nativeHandle: CValues<gsl_matrix_short>, override val nativeHandle: CPointer<gsl_matrix_short>,
features: Set<MatrixFeature> features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Short>() { ) : GslMatrix<Short, gsl_matrix_short>() {
public override val rowNum: Int override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() } 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() } 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) 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) if (other is GslShortMatrix) gsl_matrix_short_equal(nativeHandle, other.nativeHandle)
return super.equals(other) return super.equals(other)
} }
} }
public class GslUShortMatrix( internal class GslUShortMatrix(
protected override val nativeHandle: CValues<gsl_matrix_ushort>, override val nativeHandle: CPointer<gsl_matrix_ushort>,
features: Set<MatrixFeature> features: Set<MatrixFeature> = emptySet()
) : GslMatrix<UShort>() { ) : GslMatrix<UShort, gsl_matrix_ushort>() {
public override val rowNum: Int override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() } 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() } 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) 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) if (other is GslUShortMatrix) gsl_matrix_ushort_equal(nativeHandle, other.nativeHandle)
return super.equals(other) return super.equals(other)
} }
} }
public class GslComplexMatrix( internal class GslComplexMatrix(
protected override val nativeHandle: CValues<gsl_matrix_complex>, override val nativeHandle: CPointer<gsl_matrix_complex>,
features: Set<MatrixFeature> features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Complex>() { ) : GslMatrix<Complex, gsl_matrix_complex>() {
override val rowNum: Int
public override val rowNum: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() } 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() } 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) GslComplexMatrix(nativeHandle, this.features + features)
public override fun get(i: Int, j: Int): Complex = override operator fun get(i: Int, j: Int): Complex =
gsl_matrix_complex_get(nativeHandle, i.toULong(), j.toULong()).useContents { Complex(dat[0], dat[1]) } 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) if (other is GslComplexMatrix) gsl_matrix_complex_equal(nativeHandle, other.nativeHandle)
return super.equals(other) 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 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.linear.Point
import kscience.kmath.operations.Complex import kscience.kmath.operations.Complex
import org.gnu.gsl.* 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> { public override fun iterator(): Iterator<T> = object : Iterator<T> {
private var cursor = 0 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>() { internal class GslRealVector(override val nativeHandle: CPointer<gsl_vector>) : GslVector<Double, gsl_vector>() {
public override val size: Int override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() } 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>() { internal class GslFloatVector(override val nativeHandle: CPointer<gsl_vector_float>) :
public override val size: Int GslVector<Float, gsl_vector_float>() {
override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() } 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>() { internal class GslIntVector(override val nativeHandle: CPointer<gsl_vector_int>) : GslVector<Int, gsl_vector_int>() {
public override val size: Int override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() } 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>() { internal class GslUIntVector(override val nativeHandle: CPointer<gsl_vector_uint>) :
public override val size: Int GslVector<UInt, gsl_vector_uint>() {
override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() } 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>() { internal class GslLongVector(override val nativeHandle: CPointer<gsl_vector_long>) :
public override val size: Int GslVector<Long, gsl_vector_long>() {
override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() } 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 public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() } 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 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 public override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() } 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 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>() { internal class GslUShortVector(override val nativeHandle: CPointer<gsl_vector_ushort>) :
public override val size: Int GslVector<UShort, gsl_vector_ushort>() {
override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() } 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>() { internal class GslComplexVector(override val nativeHandle: CPointer<gsl_vector_complex>) :
public override val size: Int GslVector<Complex, gsl_vector_complex>() {
override val size: Int
get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() } get() = memScoped { nativeHandle.getPointer(this).pointed.size.toInt() }
public override fun get(index: Int): Complex = gsl_vector_complex_get(nativeHandle, index.toULong()).useContents { override fun get(index: Int): Complex = gsl_vector_complex_get(nativeHandle, index.toULong()).toKMath()
Complex(dat[0], dat[1]) 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) }
}
}
}