From 6fb3c03e87bb272bb09de237df3bf25491fb1054 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 30 Nov 2020 01:19:27 +0700 Subject: [PATCH] Fix broken APIs in kmath-for-real --- .../kotlin/kscience/kmath/real/RealVector.kt | 10 ++++++++++ .../kotlin/kaceince/kmath/real/RealVectorTest.kt | 1 + 2 files changed, 11 insertions(+) diff --git a/kmath-for-real/src/commonMain/kotlin/kscience/kmath/real/RealVector.kt b/kmath-for-real/src/commonMain/kotlin/kscience/kmath/real/RealVector.kt index 33684fc8c..1182d871c 100644 --- a/kmath-for-real/src/commonMain/kotlin/kscience/kmath/real/RealVector.kt +++ b/kmath-for-real/src/commonMain/kotlin/kscience/kmath/real/RealVector.kt @@ -3,16 +3,26 @@ package kscience.kmath.real import kscience.kmath.linear.Point import kscience.kmath.operations.Norm import kscience.kmath.structures.Buffer +import kscience.kmath.structures.RealBuffer +import kscience.kmath.structures.asBuffer import kscience.kmath.structures.asIterable import kotlin.math.pow import kotlin.math.sqrt public typealias RealVector = Point +public inline fun RealVector(size: Int, init: (Int) -> Double): RealVector = RealBuffer(size, init) +public fun RealVector(vararg doubles: Double): RealVector = RealBuffer(doubles) + +public fun DoubleArray.asVector(): RealVector = asBuffer() +public fun List.asVector(): RealVector = asBuffer() + + public object VectorL2Norm : Norm, Double> { override fun norm(arg: Point): Double = sqrt(arg.asIterable().sumByDouble(Number::toDouble)) } + /** * Fill the vector of given [size] with given [value] */ diff --git a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealVectorTest.kt b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealVectorTest.kt index 8a9f7a443..002db935c 100644 --- a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealVectorTest.kt +++ b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealVectorTest.kt @@ -5,6 +5,7 @@ import kscience.kmath.linear.asMatrix import kscience.kmath.linear.transpose import kscience.kmath.operations.invoke import kscience.kmath.real.RealVector +import kscience.kmath.real.plus import kotlin.test.Test import kotlin.test.assertEquals