removed unnecessary type-parameter in matrix

This commit is contained in:
Alexander Nozik 2019-05-31 12:32:22 +03:00
parent c3f0dbe161
commit 2503cb69d5
3 changed files with 30 additions and 6 deletions

View File

@ -85,7 +85,7 @@ class TransposedFeature<T : Any>(val original: Matrix<T>) : MatrixFeature
/** /**
* Create a virtual transposed matrix without copying anything. `A.transpose().transpose() === A` * Create a virtual transposed matrix without copying anything. `A.transpose().transpose() === A`
*/ */
fun <T : Any, R : Ring<T>> Matrix<T>.transpose(): Matrix<T> { fun <T : Any> Matrix<T>.transpose(): Matrix<T> {
return this.getFeature<TransposedFeature<T>>()?.original ?: VirtualMatrix( return this.getFeature<TransposedFeature<T>>()?.original ?: VirtualMatrix(
this.colNum, this.colNum,
this.rowNum, this.rowNum,

View File

@ -73,7 +73,8 @@ abstract class AutoDiffField : Field<Variable> {
abstract fun <R> derive(value: R, block: (R) -> Unit): R abstract fun <R> 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 abstract var Variable.d: Double
@ -111,16 +112,17 @@ private class AutoDiffContext : AutoDiffField() {
/** /**
* A variable coupled with its derivative. For internal use only * 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 override var Variable.d: Double
get() = (this as? VariableWithDeriv)?.d ?: derivatives[this] ?: 0.0 get() = (this as? VariableWithDeriv)?.d ?: derivatives[this] ?: 0.0
set(value) { set(value) {
if(this is VariableWithDeriv){ if (this is VariableWithDeriv) {
d = value d = value
}else { } else {
derivatives[this] = value derivatives[this] = value
} }
} }

View File

@ -52,4 +52,26 @@ class MatrixTest {
assertEquals(2.0, matrix[1, 2]) assertEquals(2.0, matrix[1, 2])
} }
@Test
fun testMatrixExtension() {
val transitionMatrix: Matrix<Double> = 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<Double>.pow(power: Int): Matrix<Double> {
var res = this
repeat(power - 1) {
res = res dot this
}
return res
}
val toTenthPower = transitionMatrix pow 10
}
} }