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