From 31d1cc774af7ce56a8d401691ecb08e9a24a2b1c Mon Sep 17 00:00:00 2001 From: mrFendel <novikov.ivan@phystech.edu> 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<T, out A : Ring<T>, out BA : BufferAlgebra<T, A> } } + /** + * Zip buffer with itself, but shifted + * */ + public inline fun Buffer<T>.shiftOp( + shift: Int = 1, + crossinline operation: A.(left: T, right: T) -> T + ): Buffer<T> { + val shifted = this.moveTo(this.startOffset+shift) + return zip(shifted, operation) + } + override fun Buffer<T>.unaryMinus(): Buffer<T> = map { -it } override fun add(left: Buffer<T>, right: Buffer<T>): Series<T> = left.zip(right) { l, r -> l + r } override fun multiply(left: Buffer<T>, right: Buffer<T>): Buffer<T> = left.zip(right) { l, r -> l * r } + public inline fun Buffer<T>.diff(): Buffer<T> = this.shiftOp {l, r -> r - l} + public companion object }