From bd178d77babd7fe6a21786860aeb7fdbe584b8ad Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Sun, 17 Jan 2021 02:38:48 +0700 Subject: [PATCH] Add transposeConjugate function for Complex and Double (conjugate values are not cached). Minor refactoring of MatrixContext and API reference changes --- .../kscience/kmath/linear/FeaturedMatrix.kt | 35 ++++++++++++++----- .../kscience/kmath/linear/MatrixContext.kt | 16 ++++++--- .../kscience/kmath/linear/VirtualMatrix.kt | 9 +++-- .../kscience/kmath/structures/Structure2D.kt | 2 +- 4 files changed, 46 insertions(+), 16 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 119f5d844..bbe2a0b61 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/FeaturedMatrix.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/FeaturedMatrix.kt @@ -1,6 +1,8 @@ package kscience.kmath.linear +import kscience.kmath.operations.Complex import kscience.kmath.operations.Ring +import kscience.kmath.operations.conjugate import kscience.kmath.structures.Matrix import kscience.kmath.structures.Structure2D import kscience.kmath.structures.asBuffer @@ -70,7 +72,7 @@ public fun > GenericMatrixContext.one(rows: Int, c /** - * A virtual matrix of zeroes + * Returns virtual matrix of zeroes. */ public fun > GenericMatrixContext.zero(rows: Int, columns: Int): FeaturedMatrix = VirtualMatrix(rows, columns) { _, _ -> elementContext.zero } @@ -78,12 +80,27 @@ public fun > GenericMatrixContext.zero(rows: Int, public class TransposedFeature(public val original: Matrix) : 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`. */ -public fun Matrix.transpose(): Matrix { - return getFeature>()?.original ?: VirtualMatrix( - colNum, - rowNum, - setOf(TransposedFeature(this)) - ) { i, j -> get(j, i) } -} \ No newline at end of file +public fun Matrix.transpose(): Matrix = getFeature>()?.original ?: VirtualMatrix( + colNum, + rowNum, + setOf(TransposedFeature(this)), +) { i, j -> get(j, i) } + +/** + * Returns Hermitian conjugate of this matrix (i.e., just transposes it). + * + * + */ +public fun Matrix.transposeConjugate(): Matrix = transpose() + +/** + * Returns Hermitian conjugate of this matrix (i.e., transposes it and replaces each element with its conjugate). + * + * @return the Hermitian conjugate of this matrix. + */ +public fun Matrix.transposeConjugate(): Matrix { + val t = transpose() + return VirtualMatrix(t.rowNum, t.colNum) { i, j -> t[i,j].conjugate } +} diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/MatrixContext.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/MatrixContext.kt index 8c28a240f..c61eb7eb9 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/MatrixContext.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/MatrixContext.kt @@ -10,7 +10,10 @@ import kscience.kmath.structures.Matrix import kscience.kmath.structures.asSequence /** - * Basic operations on matrices. Operates on [Matrix] + * Basic operations on matrices. Operates on [Matrix]. + * + * @param T the type of items in the matrices. + * @param M the type of operated matrices. */ public interface MatrixContext> : SpaceOperations> { /** @@ -84,9 +87,16 @@ public interface MatrixContext> : SpaceOperations, out M : Matrix> : MatrixContext { /** - * The ring context for matrix elements + * The ring over matrix elements. */ public val elementContext: R @@ -133,8 +143,6 @@ public interface GenericMatrixContext, out M : Matrix> : public override fun multiply(a: Matrix, k: Number): M = produce(a.rowNum, a.colNum) { i, j -> elementContext { a[i, j] * k } } - public operator fun Number.times(matrix: FeaturedMatrix): M = multiply(matrix, this) - public override operator fun Matrix.times(value: T): M = produce(rowNum, colNum) { i, j -> elementContext { get(i, j) * value } } } diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/VirtualMatrix.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/VirtualMatrix.kt index e0a1d0026..8b7eaa3e7 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/VirtualMatrix.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/VirtualMatrix.kt @@ -2,17 +2,22 @@ package kscience.kmath.linear import kscience.kmath.structures.Matrix +/** + * The matrix where each element is evaluated each time when is being accessed. + * + * @property generator the function that provides elements. + */ public class VirtualMatrix( override val rowNum: Int, override val colNum: Int, override val features: Set = emptySet(), - public val generator: (i: Int, j: Int) -> T + public val generator: (i: Int, j: Int) -> T, ) : FeaturedMatrix { public constructor( rowNum: Int, colNum: Int, vararg features: MatrixFeature, - generator: (i: Int, j: Int) -> T + generator: (i: Int, j: Int) -> T, ) : this( rowNum, colNum, diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Structure2D.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Structure2D.kt index bac7d3389..7131a02e0 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Structure2D.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Structure2D.kt @@ -72,6 +72,6 @@ else /** * Alias for [Structure2D] with more familiar name. * - * @param T the type of items. + * @param T the type of items in the matrix. */ public typealias Matrix = Structure2D