forked from kscience/kmath
Replace manual matrices classes building with codegen
This commit is contained in:
parent
abcde808dc
commit
0a02fd07b4
@ -2,9 +2,9 @@ plugins {
|
||||
id("ru.mipt.npm.project")
|
||||
}
|
||||
|
||||
val kmathVersion: String by extra("0.2.0-dev-2")
|
||||
val bintrayRepo: String by extra("kscience")
|
||||
val githubProject: String by extra("kmath")
|
||||
internal val kmathVersion: String by extra("0.2.0-dev-2")
|
||||
internal val bintrayRepo: String by extra("kscience")
|
||||
internal val githubProject: String by extra("kmath")
|
||||
|
||||
allprojects {
|
||||
repositories {
|
||||
|
9
buildSrc/build.gradle.kts
Normal file
9
buildSrc/build.gradle.kts
Normal file
@ -0,0 +1,9 @@
|
||||
plugins {
|
||||
`kotlin-dsl`
|
||||
}
|
||||
|
||||
repositories.jcenter()
|
||||
|
||||
dependencies {
|
||||
implementation(kotlin("compiler-embeddable"))
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package kscience.kmath.gsl.codegen
|
||||
|
||||
import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtil
|
||||
import org.jetbrains.kotlin.com.intellij.psi.PsiFile
|
||||
import org.jetbrains.kotlin.com.intellij.psi.impl.DebugUtil
|
||||
import org.jetbrains.kotlin.com.intellij.psi.impl.source.PsiFileImpl
|
||||
import org.jetbrains.kotlin.com.intellij.testFramework.LightVirtualFile
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import kotlin.math.min
|
||||
|
||||
internal object PsiTestUtil {
|
||||
fun checkFileStructure(file: KtFile) {
|
||||
compareFromAllRoots(file) { f -> DebugUtil.psiTreeToString(f, false) }
|
||||
}
|
||||
|
||||
private fun compareFromAllRoots(
|
||||
file: KtFile,
|
||||
function: (PsiFile) -> String
|
||||
) {
|
||||
val dummyFile = createDummyCopy(file)
|
||||
|
||||
val psiTree = StringUtil.join(
|
||||
file.viewProvider.allFiles,
|
||||
{ param -> function(param) },
|
||||
"\n"
|
||||
)
|
||||
|
||||
val reparsedTree = StringUtil.join(
|
||||
dummyFile.viewProvider.allFiles,
|
||||
{ param -> function(param) },
|
||||
"\n"
|
||||
)
|
||||
|
||||
assertPsiTextTreeConsistency(psiTree, reparsedTree)
|
||||
}
|
||||
|
||||
private fun assertPsiTextTreeConsistency(psiTree: String, reparsedTree: String) {
|
||||
var psiTreeMutable = psiTree
|
||||
var reparsedTreeMutable = reparsedTree
|
||||
|
||||
if (psiTreeMutable != reparsedTreeMutable) {
|
||||
val psiLines = splitByLinesDontTrim(psiTreeMutable)
|
||||
val reparsedLines = splitByLinesDontTrim(reparsedTreeMutable)
|
||||
var i = 0
|
||||
|
||||
while (true) {
|
||||
if (i >= psiLines.size || i >= reparsedLines.size || psiLines[i] != reparsedLines[i]) {
|
||||
psiLines[min(i, psiLines.size - 1)] += " // in PSI structure"
|
||||
reparsedLines[min(i, reparsedLines.size - 1)] += " // re-created from text"
|
||||
break
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
psiTreeMutable = StringUtil.join(psiLines, "\n")
|
||||
reparsedTreeMutable = StringUtil.join(reparsedLines, "\n")
|
||||
assert(reparsedTreeMutable == psiTreeMutable)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createDummyCopy(file: KtFile): PsiFile {
|
||||
val copy = LightVirtualFile(file.name, file.text)
|
||||
copy.originalFile = file.viewProvider.virtualFile
|
||||
val dummyCopy = requireNotNull(file.manager.findFile(copy))
|
||||
if (dummyCopy is PsiFileImpl) dummyCopy.originalFile = file
|
||||
return dummyCopy
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package kscience.kmath.gsl.codegen
|
||||
|
||||
import java.util.regex.Pattern
|
||||
|
||||
private val EOL_SPLIT_DONT_TRIM_PATTERN: Pattern = Pattern.compile("(\r|\n|\r\n)+")
|
||||
|
||||
internal fun splitByLinesDontTrim(string: String): Array<String> {
|
||||
return EOL_SPLIT_DONT_TRIM_PATTERN.split(string)
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
package kscience.kmath.gsl.codegen
|
||||
|
||||
import org.jetbrains.kotlin.com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.resolve.ImportPath
|
||||
import java.io.File
|
||||
|
||||
private fun fn(pattern: String, type: String): String {
|
||||
if (type == "double") return pattern.replace("R", "_")
|
||||
return pattern.replace("R", "_${type}_")
|
||||
}
|
||||
|
||||
private fun sn(pattern: String, type: String): String {
|
||||
if (type == "double") return pattern.replace("R", "")
|
||||
return pattern.replace("R", "_$type")
|
||||
}
|
||||
|
||||
private fun KtPsiFactory.createMatrixClass(
|
||||
f: KtFile,
|
||||
cTypeName: String,
|
||||
kotlinTypeName: String,
|
||||
kotlinTypeAlias: String = kotlinTypeName
|
||||
) {
|
||||
val className = "Gsl${kotlinTypeAlias}Matrix"
|
||||
val structName = sn("gsl_matrixR", cTypeName)
|
||||
|
||||
f += createClass(
|
||||
"""internal class $className(
|
||||
override val nativeHandle: CPointer<$structName>,
|
||||
features: Set<MatrixFeature> = emptySet()
|
||||
) : GslMatrix<$kotlinTypeName, $structName>() {
|
||||
override val rowNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
|
||||
|
||||
override val colNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
|
||||
|
||||
override val features: Set<MatrixFeature> = features
|
||||
|
||||
override fun suggestFeature(vararg features: MatrixFeature): $className =
|
||||
${className}(nativeHandle, this.features + features)
|
||||
|
||||
override operator fun get(i: Int, j: Int): $kotlinTypeName = ${
|
||||
fn("gsl_matrixRget", cTypeName)
|
||||
}(nativeHandle, i.toULong(), j.toULong())
|
||||
|
||||
override operator fun set(i: Int, j: Int, value: ${kotlinTypeName}): Unit =
|
||||
${fn("gsl_matrixRset", cTypeName)}(nativeHandle, i.toULong(), j.toULong(), value)
|
||||
|
||||
override fun copy(): $className = memScoped {
|
||||
val new = requireNotNull(${fn("gsl_matrixRalloc", cTypeName)}(rowNum.toULong(), colNum.toULong()))
|
||||
${fn("gsl_matrixRmemcpy", cTypeName)}(new, nativeHandle)
|
||||
$className(new, features)
|
||||
}
|
||||
|
||||
override fun close(): Unit = ${fn("gsl_matrixRfree", cTypeName)}(nativeHandle)
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other is $className) ${fn("gsl_matrixRequal", cTypeName)}(nativeHandle, other.nativeHandle)
|
||||
return super.equals(other)
|
||||
}
|
||||
}"""
|
||||
)
|
||||
|
||||
f += createNewLine(2)
|
||||
}
|
||||
|
||||
fun matricesCodegen(outputFile: String, project: Project = createProject()) {
|
||||
val f = KtPsiFactory(project, true).run {
|
||||
createFile("package kscience.kmath.gsl").also { f ->
|
||||
f += createNewLine(2)
|
||||
f += createImportDirective(ImportPath.fromString("kotlinx.cinterop.*"))
|
||||
f += createNewLine(1)
|
||||
f += createImportDirective(ImportPath.fromString("kscience.kmath.linear.*"))
|
||||
f += createNewLine(1)
|
||||
f += createImportDirective(ImportPath.fromString("kscience.kmath.operations.*"))
|
||||
f += createNewLine(1)
|
||||
f += createImportDirective(ImportPath.fromString("org.gnu.gsl.*"))
|
||||
f += createNewLine(2)
|
||||
createMatrixClass(f, "double", "Double", "Real")
|
||||
createMatrixClass(f, "float", "Float")
|
||||
createMatrixClass(f, "short", "Short")
|
||||
createMatrixClass(f, "ushort", "UShort")
|
||||
createMatrixClass(f, "long", "Long")
|
||||
createMatrixClass(f, "ulong", "ULong")
|
||||
createMatrixClass(f, "int", "Int")
|
||||
createMatrixClass(f, "uint", "UInt")
|
||||
}
|
||||
}
|
||||
|
||||
PsiTestUtil.checkFileStructure(f)
|
||||
|
||||
File(outputFile).apply {
|
||||
parentFile.mkdirs()
|
||||
writeText(f.text)
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
@file:Suppress("UNUSED_VARIABLE")
|
||||
|
||||
import kscience.kmath.gsl.codegen.matricesCodegen
|
||||
|
||||
plugins {
|
||||
id("ru.mipt.npm.mpp")
|
||||
}
|
||||
@ -24,10 +26,18 @@ kotlin {
|
||||
}
|
||||
}
|
||||
|
||||
sourceSets.commonMain {
|
||||
sourceSets {
|
||||
val nativeMain by getting {
|
||||
dependencies {
|
||||
api(project(":kmath-core"))
|
||||
api("org.jetbrains.kotlinx:kotlinx-io:0.2.0-tvis-3")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal val codegen: Task by tasks.creating {
|
||||
matricesCodegen(kotlin.sourceSets["nativeMain"].kotlin.srcDirs.first().absolutePath + "/generated/Matrices.kt")
|
||||
}
|
||||
|
||||
kotlin.sourceSets["nativeMain"].kotlin.srcDirs(files().builtBy(codegen))
|
||||
|
@ -1,340 +0,0 @@
|
||||
package kscience.kmath.gsl
|
||||
|
||||
import kotlinx.cinterop.*
|
||||
import kscience.kmath.linear.FeaturedMatrix
|
||||
import kscience.kmath.linear.MatrixFeature
|
||||
import kscience.kmath.operations.Complex
|
||||
import kscience.kmath.structures.NDStructure
|
||||
import org.gnu.gsl.*
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
public override fun hashCode(): Int {
|
||||
var result = nativeHandle.hashCode()
|
||||
result = 31 * result + features.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
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() }
|
||||
|
||||
override val colNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
|
||||
|
||||
override val features: Set<MatrixFeature> = features
|
||||
|
||||
override fun suggestFeature(vararg features: MatrixFeature): GslRealMatrix =
|
||||
GslRealMatrix(nativeHandle, this.features + features)
|
||||
|
||||
override operator fun get(i: Int, j: Int): Double = gsl_matrix_get(nativeHandle, i.toULong(), j.toULong())
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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() }
|
||||
|
||||
override val colNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
|
||||
|
||||
override val features: Set<MatrixFeature> = features
|
||||
|
||||
override fun suggestFeature(vararg features: MatrixFeature): GslFloatMatrix =
|
||||
GslFloatMatrix(nativeHandle, this.features + features)
|
||||
|
||||
override operator fun get(i: Int, j: Int): Float =
|
||||
gsl_matrix_float_get(nativeHandle, i.toULong(), j.toULong())
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
internal class GslIntMatrix(
|
||||
override val nativeHandle: CPointer<gsl_matrix_int>,
|
||||
features: Set<MatrixFeature> = emptySet()
|
||||
) : GslMatrix<Int, gsl_matrix_int>() {
|
||||
|
||||
override val rowNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
|
||||
|
||||
override val colNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
|
||||
|
||||
override val features: Set<MatrixFeature> = features
|
||||
|
||||
override fun suggestFeature(vararg features: MatrixFeature): GslIntMatrix =
|
||||
GslIntMatrix(nativeHandle, this.features + features)
|
||||
|
||||
override operator fun get(i: Int, j: Int): Int = gsl_matrix_int_get(nativeHandle, i.toULong(), j.toULong())
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
internal class GslUIntMatrix(
|
||||
override val nativeHandle: CPointer<gsl_matrix_uint>,
|
||||
features: Set<MatrixFeature> = emptySet()
|
||||
) : GslMatrix<UInt, gsl_matrix_uint>() {
|
||||
|
||||
override val rowNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
|
||||
|
||||
override val colNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
|
||||
|
||||
override val features: Set<MatrixFeature> = features
|
||||
|
||||
override fun suggestFeature(vararg features: MatrixFeature): GslUIntMatrix =
|
||||
GslUIntMatrix(nativeHandle, this.features + features)
|
||||
|
||||
override operator fun get(i: Int, j: Int): UInt = gsl_matrix_uint_get(nativeHandle, i.toULong(), j.toULong())
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
internal class GslLongMatrix(
|
||||
override val nativeHandle: CPointer<gsl_matrix_long>,
|
||||
features: Set<MatrixFeature> = emptySet()
|
||||
) : GslMatrix<Long, gsl_matrix_long>() {
|
||||
|
||||
override val rowNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
|
||||
|
||||
override val colNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
|
||||
|
||||
override val features: Set<MatrixFeature> = features
|
||||
|
||||
override fun suggestFeature(vararg features: MatrixFeature): GslLongMatrix =
|
||||
GslLongMatrix(nativeHandle, this.features + features)
|
||||
|
||||
override operator fun get(i: Int, j: Int): Long = gsl_matrix_long_get(nativeHandle, i.toULong(), j.toULong())
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
internal class GslULongMatrix(
|
||||
override val nativeHandle: CPointer<gsl_matrix_ulong>,
|
||||
features: Set<MatrixFeature> = emptySet()
|
||||
) : GslMatrix<ULong, gsl_matrix_ulong>() {
|
||||
|
||||
override val rowNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
|
||||
|
||||
override val colNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
|
||||
|
||||
override val features: Set<MatrixFeature> = features
|
||||
|
||||
override fun suggestFeature(vararg features: MatrixFeature): GslULongMatrix =
|
||||
GslULongMatrix(nativeHandle, this.features + features)
|
||||
|
||||
override operator fun get(i: Int, j: Int): ULong =
|
||||
gsl_matrix_ulong_get(nativeHandle, i.toULong(), j.toULong())
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
internal class GslShortMatrix(
|
||||
override val nativeHandle: CPointer<gsl_matrix_short>,
|
||||
features: Set<MatrixFeature> = emptySet()
|
||||
) : GslMatrix<Short, gsl_matrix_short>() {
|
||||
|
||||
override val rowNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
|
||||
|
||||
override val colNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
|
||||
|
||||
override val features: Set<MatrixFeature> = features
|
||||
|
||||
override fun suggestFeature(vararg features: MatrixFeature): GslShortMatrix =
|
||||
GslShortMatrix(nativeHandle, this.features + features)
|
||||
|
||||
override operator fun get(i: Int, j: Int): Short =
|
||||
gsl_matrix_short_get(nativeHandle, i.toULong(), j.toULong())
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
internal class GslUShortMatrix(
|
||||
override val nativeHandle: CPointer<gsl_matrix_ushort>,
|
||||
features: Set<MatrixFeature> = emptySet()
|
||||
) : GslMatrix<UShort, gsl_matrix_ushort>() {
|
||||
|
||||
override val rowNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size1.toInt() }
|
||||
|
||||
override val colNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
|
||||
|
||||
override val features: Set<MatrixFeature> = features
|
||||
|
||||
override fun suggestFeature(vararg features: MatrixFeature): GslUShortMatrix =
|
||||
GslUShortMatrix(nativeHandle, this.features + features)
|
||||
|
||||
override operator fun get(i: Int, j: Int): UShort =
|
||||
gsl_matrix_ushort_get(nativeHandle, i.toULong(), j.toULong())
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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() }
|
||||
|
||||
override val colNum: Int
|
||||
get() = memScoped { nativeHandle.getPointer(this).pointed.size2.toInt() }
|
||||
|
||||
override val features: Set<MatrixFeature> = features
|
||||
|
||||
override fun suggestFeature(vararg features: MatrixFeature): GslComplexMatrix =
|
||||
GslComplexMatrix(nativeHandle, this.features + features)
|
||||
|
||||
override operator fun get(i: Int, j: Int): Complex =
|
||||
gsl_matrix_complex_get(nativeHandle, i.toULong(), j.toULong()).toKMath()
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package kscience.kmath.gsl
|
||||
|
||||
import kotlinx.cinterop.CStructVar
|
||||
import kscience.kmath.linear.FeaturedMatrix
|
||||
import kscience.kmath.structures.NDStructure
|
||||
|
||||
public abstract class GslMatrix<T : Any, H : CStructVar> internal constructor(): 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)
|
||||
}
|
||||
|
||||
public override fun hashCode(): Int {
|
||||
var result = nativeHandle.hashCode()
|
||||
result = 31 * result + features.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ import kscience.kmath.linear.Point
|
||||
import kscience.kmath.operations.Complex
|
||||
import org.gnu.gsl.*
|
||||
|
||||
public sealed class GslVector<T, H : CStructVar> : GslMemoryHolder<H>(), Point<T> {
|
||||
public abstract class GslVector<T, H : CStructVar> internal constructor(): GslMemoryHolder<H>(), Point<T> {
|
||||
public override fun iterator(): Iterator<T> = object : Iterator<T> {
|
||||
private var cursor = 0
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
package kscience.kmath.gsl
|
||||
|
||||
import kotlinx.io.use
|
||||
import kscience.kmath.operations.invoke
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
@ -8,8 +7,10 @@ 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) }
|
||||
}
|
||||
val ma = GslRealMatrixContext.produce(10, 10) { _, _ -> 0.1 }
|
||||
val mb = (ma * 20.0)
|
||||
assertEquals(mb[0, 1], 2.0)
|
||||
mb.close()
|
||||
ma.close()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user