From f1b319f73de90b13adf88491ae6cf4a838f84822 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Tue, 19 Jan 2021 00:14:12 +0700 Subject: [PATCH] Minor: micro-optimize getFeature function, reformat --- .../kotlin/kscience/kmath/linear/FeaturedMatrix.kt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/FeaturedMatrix.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/FeaturedMatrix.kt index f154a6115..61dbdf721 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/FeaturedMatrix.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/FeaturedMatrix.kt @@ -60,8 +60,9 @@ public inline fun Matrix<*>.hasFeature(): Boolean = /** * Get the first feature matching given class. Does not guarantee that matrix has only one feature matching the criteria */ +@Suppress("UNCHECKED_CAST") public inline fun Matrix<*>.getFeature(): T? = - features.filterIsInstance().firstOrNull() + features.find { it is T }?.let { it as T } /** * Diagonal matrix of ones. The matrix is virtual no actual matrix is created @@ -78,7 +79,12 @@ public fun > GenericMatrixContext.one(rows: Int, c public fun > GenericMatrixContext.zero(rows: Int, columns: Int): FeaturedMatrix = VirtualMatrix(rows, columns) { _, _ -> elementContext.zero } -public class TransposedFeature(public val original: Matrix) : MatrixFeature +/** + * Matrices with this feature were transposed previosly and hold the reference to their original. + * + * @property original the matrix before transposition. + */ +public inline class TransposedFeature(public val original: Matrix) : MatrixFeature /** * Create a virtual transposed matrix without copying anything. `A.transpose().transpose() === A`. @@ -105,5 +111,5 @@ public fun Matrix.transposeConjugate(): Matrix = transpose() @JvmName("transposeConjugateComplex") public fun Matrix.transposeConjugate(): Matrix { val t = transpose() - return VirtualMatrix(t.rowNum, t.colNum) { i, j -> t[i,j].conjugate } + return VirtualMatrix(t.rowNum, t.colNum) { i, j -> t[i, j].conjugate } }