From 2503cb69d5840f14c0ab91cf65ff0a4ccc590b96 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 31 May 2019 12:32:22 +0300 Subject: [PATCH] removed unnecessary type-parameter in matrix --- .../scientifik/kmath/linear/FeaturedMatrix.kt | 2 +- .../scientifik/kmath/operations/AutoDiff.kt | 12 +++++----- .../scientifik/kmath/linear/MatrixTest.kt | 22 +++++++++++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/FeaturedMatrix.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/FeaturedMatrix.kt index 0886b3ce5..9f6b9f600 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/FeaturedMatrix.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/FeaturedMatrix.kt @@ -85,7 +85,7 @@ class TransposedFeature(val original: Matrix) : MatrixFeature /** * Create a virtual transposed matrix without copying anything. `A.transpose().transpose() === A` */ -fun > Matrix.transpose(): Matrix { +fun Matrix.transpose(): Matrix { return this.getFeature>()?.original ?: VirtualMatrix( this.colNum, this.rowNum, diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/AutoDiff.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/AutoDiff.kt index cd50815e3..9714630c1 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/AutoDiff.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/AutoDiff.kt @@ -73,7 +73,8 @@ abstract class AutoDiffField : Field { abstract fun derive(value: R, block: (R) -> Unit): R /** - * A variable accessing inner state of derivatives. Use only in extensions + * A variable accessing inner state of derivatives. + * Use this function in inner builders to avoid creating additional derivative bindings */ abstract var Variable.d: Double @@ -111,16 +112,17 @@ private class AutoDiffContext : AutoDiffField() { /** * A variable coupled with its derivative. For internal use only */ - class VariableWithDeriv(x: Double, var d: Double = 0.0): Variable(x) + class VariableWithDeriv(x: Double, var d: Double = 0.0) : Variable(x) - override fun variable(value: Double): Variable = VariableWithDeriv(value) + + override fun variable(value: Double): Variable = VariableWithDeriv(value) override var Variable.d: Double get() = (this as? VariableWithDeriv)?.d ?: derivatives[this] ?: 0.0 set(value) { - if(this is VariableWithDeriv){ + if (this is VariableWithDeriv) { d = value - }else { + } else { derivatives[this] = value } } diff --git a/kmath-core/src/commonTest/kotlin/scientifik/kmath/linear/MatrixTest.kt b/kmath-core/src/commonTest/kotlin/scientifik/kmath/linear/MatrixTest.kt index f7d603dab..5370ae960 100644 --- a/kmath-core/src/commonTest/kotlin/scientifik/kmath/linear/MatrixTest.kt +++ b/kmath-core/src/commonTest/kotlin/scientifik/kmath/linear/MatrixTest.kt @@ -52,4 +52,26 @@ class MatrixTest { assertEquals(2.0, matrix[1, 2]) } + + @Test + fun testMatrixExtension() { + val transitionMatrix: Matrix = VirtualMatrix(6, 6) { row, col -> + when { + col == 0 -> .50 + row + 1 == col -> .50 + row == 5 && col == 5 -> 1.0 + else -> 0.0 + } + } + + infix fun Matrix.pow(power: Int): Matrix { + var res = this + repeat(power - 1) { + res = res dot this + } + return res + } + + val toTenthPower = transitionMatrix pow 10 + } } \ No newline at end of file