From 31d1cc774af7ce56a8d401691ecb08e9a24a2b1c Mon Sep 17 00:00:00 2001 From: mrFendel Date: Tue, 11 Apr 2023 20:31:04 +0300 Subject: [PATCH] added shiftOperartion and diff --- .../space/kscience/kmath/series/SeriesAlgebra.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/SeriesAlgebra.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/SeriesAlgebra.kt index 3ccbca5a1..4b7f8b83a 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/SeriesAlgebra.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/SeriesAlgebra.kt @@ -199,12 +199,25 @@ public open class SeriesAlgebra, out BA : BufferAlgebra } } + /** + * Zip buffer with itself, but shifted + * */ + public inline fun Buffer.shiftOp( + shift: Int = 1, + crossinline operation: A.(left: T, right: T) -> T + ): Buffer { + val shifted = this.moveTo(this.startOffset+shift) + return zip(shifted, operation) + } + override fun Buffer.unaryMinus(): Buffer = map { -it } override fun add(left: Buffer, right: Buffer): Series = left.zip(right) { l, r -> l + r } override fun multiply(left: Buffer, right: Buffer): Buffer = left.zip(right) { l, r -> l * r } + public inline fun Buffer.diff(): Buffer = this.shiftOp {l, r -> r - l} + public companion object }