Compare commits

..

2 Commits

Author SHA1 Message Date
dba001eff3 Fix types in geometry algebras 2024-02-20 20:39:57 +03:00
49f0d1fe7d Fix types in geometry algebras 2024-02-20 19:35:00 +03:00
12 changed files with 51 additions and 46 deletions

View File

@ -52,6 +52,7 @@
- Median statistics - Median statistics
- Complex power of negative real numbers - Complex power of negative real numbers
- Add proper mutability for MutableBufferND rows and columns - Add proper mutability for MutableBufferND rows and columns
- Generic Float32 and Float64 vectors are used in geometry algebras.
## 0.3.1 - 2023-04-09 ## 0.3.1 - 2023-04-09

View File

@ -52,7 +52,7 @@ dependencies {
implementation("org.slf4j:slf4j-simple:1.7.32") implementation("org.slf4j:slf4j-simple:1.7.32")
// plotting // plotting
implementation("space.kscience:plotlykt-server:0.5.3-dev-1") implementation("space.kscience:plotlykt-server:0.7.0")
} }
kotlin { kotlin {

View File

@ -116,7 +116,7 @@ public object CMOptimizer : Optimizer<Double, FunctionOptimization<Double>> {
} }
addOptimizationData(gradientFunction) addOptimizationData(gradientFunction)
val logger = problem.attributes[OptimizationLog] // val logger = problem.attributes[OptimizationLog]
problem.attributes[CMOptimizerData]?.let { builders: Set<SymbolIndexer.() -> OptimizationData> -> problem.attributes[CMOptimizerData]?.let { builders: Set<SymbolIndexer.() -> OptimizationData> ->
builders.forEach { dataBuilder -> builders.forEach { dataBuilder ->

View File

@ -7,8 +7,7 @@ package space.kscience.kmath.geometry
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import space.kscience.kmath.geometry.euclidean2d.Float64Vector2D import space.kscience.kmath.structures.Float64
import space.kscience.kmath.geometry.euclidean3d.Float64Vector3D
/** /**
* A line formed by [start] vector of start and a [direction] vector. Direction vector is not necessarily normalized, * A line formed by [start] vector of start and a [direction] vector. Direction vector is not necessarily normalized,
@ -25,8 +24,8 @@ private data class LineImpl<out V : Any>(override val start: V, override val dir
public fun <V : Any> Line(base: V, direction: V): Line<V> = LineImpl(base, direction) public fun <V : Any> Line(base: V, direction: V): Line<V> = LineImpl(base, direction)
public typealias Line2D = Line<Float64Vector2D> public typealias Line2D = Line<Vector2D<Float64>>
public typealias Line3D = Line<Float64Vector3D> public typealias Line3D = Line<Vector3D<Float64>>
/** /**
* A directed line segment between [begin] and [end] * A directed line segment between [begin] and [end]
@ -49,5 +48,5 @@ public fun <V : Any> LineSegment<V>.line(algebra: GeometrySpace<V, *>): Line<V>
Line(begin, end - begin) Line(begin, end - begin)
} }
public typealias LineSegment2D = LineSegment<Float64Vector2D> public typealias LineSegment2D = LineSegment<Vector2D<Float64>>
public typealias LineSegment3D = LineSegment<Float64Vector3D> public typealias LineSegment3D = LineSegment<Vector3D<Float64>>

View File

@ -3,9 +3,7 @@
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
package space.kscience.kmath.geometry.euclidean2d package space.kscience.kmath.geometry
import space.kscience.kmath.geometry.Vector2D
/** /**

View File

@ -7,6 +7,7 @@ package space.kscience.kmath.geometry.euclidean2d
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import space.kscience.kmath.geometry.Vector2D import space.kscience.kmath.geometry.Vector2D
import space.kscience.kmath.structures.Float64
import kotlin.math.PI import kotlin.math.PI
@ -23,9 +24,12 @@ public val Circle2D<*>.circumference: Double get() = radius * 2 * PI
@Serializable @Serializable
public data class Float64Circle2D( public data class Float64Circle2D(
@Serializable(Float64Space2D.VectorSerializer::class) override val center: Float64Vector2D, @Serializable(Float64Space2D.VectorSerializer::class) override val center: Float64Vector2D,
override val radius: Double override val radius: Float64,
) : Circle2D<Double> ) : Circle2D<Double>
public fun Circle2D(center: Float64Vector2D, radius: Double): Circle2D<Double> = Float64Circle2D(center, radius) public fun Circle2D(center: Vector2D<Float64>, radius: Double): Circle2D<Double> = Float64Circle2D(
center as? Float64Vector2D ?: Float64Vector2D(center.x, center.y),
radius
)

View File

@ -26,7 +26,7 @@ public interface Float32Vector2D : Vector2D<Float32>{
} }
public object Float32Space2D : GeometrySpace<Float32Vector2D, Float32> { public object Float32Space2D : GeometrySpace<Vector2D<Float32>, Float32> {
@Serializable @Serializable
@SerialName("Float32Vector2D") @SerialName("Float32Vector2D")
private data class Vector2DImpl( private data class Vector2DImpl(
@ -54,21 +54,21 @@ public object Float32Space2D : GeometrySpace<Float32Vector2D, Float32> {
override val zero: Float32Vector2D by lazy { vector(0f, 0f) } override val zero: Float32Vector2D by lazy { vector(0f, 0f) }
override fun norm(arg: Float32Vector2D): Float32 = sqrt(arg.x.pow(2) + arg.y.pow(2)) override fun norm(arg: Vector2D<Float32>): Float32 = sqrt(arg.x.pow(2) + arg.y.pow(2))
public fun Float32Vector2D.norm(): Float32 = norm(this) public fun Vector2D<Float32>.norm(): Float32 = norm(this)
override fun Float32Vector2D.unaryMinus(): Float32Vector2D = vector(-x, -y) override fun Vector2D<Float32>.unaryMinus(): Float32Vector2D = vector(-x, -y)
override fun Float32Vector2D.distanceTo(other: Float32Vector2D): Float32 = (this - other).norm() override fun Vector2D<Float32>.distanceTo(other: Vector2D<Float32>): Float32 = (this - other).norm()
override fun add(left: Float32Vector2D, right: Float32Vector2D): Float32Vector2D = override fun add(left: Vector2D<Float32>, right: Vector2D<Float32>): Float32Vector2D =
vector(left.x + right.x, left.y + right.y) vector(left.x + right.x, left.y + right.y)
override fun scale(a: Float32Vector2D, value: Double): Float32Vector2D = override fun scale(a: Vector2D<Float32>, value: Double): Float32Vector2D =
vector(a.x * value, a.y * value) vector(a.x * value, a.y * value)
override fun Float32Vector2D.dot(other: Float32Vector2D): Double = override fun Vector2D<Float32>.dot(other: Vector2D<Float32>): Double =
(x * other.x + y * other.y).toDouble() (x * other.x + y * other.y).toDouble()
public val xAxis: Float32Vector2D = vector(1.0, 0.0) public val xAxis: Float32Vector2D = vector(1.0, 0.0)
@ -76,7 +76,7 @@ public object Float32Space2D : GeometrySpace<Float32Vector2D, Float32> {
override val defaultPrecision: Float32 = 1e-3f override val defaultPrecision: Float32 = 1e-3f
override val bufferFactory: MutableBufferFactory<Float32Vector2D> = MutableBufferFactory() override val bufferFactory: MutableBufferFactory<Vector2D<Float32>> = MutableBufferFactory()
} }
public fun Float32Vector2D(x: Number, y: Number): Float32Vector2D = Float32Space2D.vector(x, y) public fun Float32Vector2D(x: Number, y: Number): Float32Vector2D = Float32Space2D.vector(x, y)

View File

@ -35,7 +35,7 @@ public typealias DoubleVector2D = Float64Vector2D
/** /**
* 2D Euclidean space * 2D Euclidean space
*/ */
public object Float64Space2D : GeometrySpace<Float64Vector2D, Float64>, ScaleOperations<Float64Vector2D> { public object Float64Space2D : GeometrySpace<Vector2D<Float64>, Float64>, ScaleOperations<Vector2D<Float64>> {
@Serializable @Serializable
@ -61,23 +61,23 @@ public object Float64Space2D : GeometrySpace<Float64Vector2D, Float64>, ScaleOpe
override val zero: Float64Vector2D by lazy { vector(0.0, 0.0) } override val zero: Float64Vector2D by lazy { vector(0.0, 0.0) }
override fun norm(arg: Float64Vector2D): Double = sqrt(arg.x.pow(2) + arg.y.pow(2)) override fun norm(arg: Vector2D<Float64>): Double = sqrt(arg.x.pow(2) + arg.y.pow(2))
override fun Float64Vector2D.unaryMinus(): Float64Vector2D = vector(-x, -y) override fun Vector2D<Float64>.unaryMinus(): Float64Vector2D = vector(-x, -y)
override fun Float64Vector2D.distanceTo(other: Float64Vector2D): Double = norm(this - other) override fun Vector2D<Float64>.distanceTo(other: Vector2D<Float64>): Double = norm(this - other)
override fun add(left: Float64Vector2D, right: Float64Vector2D): Float64Vector2D = override fun add(left: Vector2D<Float64>, right: Vector2D<Float64>): Float64Vector2D =
vector(left.x + right.x, left.y + right.y) vector(left.x + right.x, left.y + right.y)
override fun scale(a: Float64Vector2D, value: Double): Float64Vector2D = vector(a.x * value, a.y * value) override fun scale(a: Vector2D<Float64>, value: Double): Float64Vector2D = vector(a.x * value, a.y * value)
override fun Float64Vector2D.dot(other: Float64Vector2D): Double = x * other.x + y * other.y override fun Vector2D<Float64>.dot(other: Vector2D<Float64>): Double = x * other.x + y * other.y
public val xAxis: Float64Vector2D = vector(1.0, 0.0) public val xAxis: Float64Vector2D = vector(1.0, 0.0)
public val yAxis: Float64Vector2D = vector(0.0, 1.0) public val yAxis: Float64Vector2D = vector(0.0, 1.0)
override val defaultPrecision: Double = 1e-6 override val defaultPrecision: Double = 1e-6
override val bufferFactory: MutableBufferFactory<Float64Vector2D> = MutableBufferFactory() override val bufferFactory: MutableBufferFactory<Vector2D<Float64>> = MutableBufferFactory()
} }
public fun Float64Vector2D(x: Number, y: Number): Float64Vector2D = Float64Space2D.vector(x, y) public fun Float64Vector2D(x: Number, y: Number): Float64Vector2D = Float64Space2D.vector(x, y)

View File

@ -26,7 +26,7 @@ public interface Float32Vector3D : Vector3D<Float>{
} }
public object Float32Space3D : GeometrySpace<Float32Vector3D, Float32> { public object Float32Space3D : GeometrySpace<Vector3D<Float32>, Float32> {
@Serializable @Serializable
@SerialName("Float32Vector3D") @SerialName("Float32Vector3D")
@ -56,21 +56,21 @@ public object Float32Space3D : GeometrySpace<Float32Vector3D, Float32> {
override val zero: Float32Vector3D by lazy { vector(0.0, 0.0, 0.0) } override val zero: Float32Vector3D by lazy { vector(0.0, 0.0, 0.0) }
override fun norm(arg: Float32Vector3D): Float32 = sqrt(arg.x.pow(2) + arg.y.pow(2) + arg.z.pow(2)) override fun norm(arg: Vector3D<Float32>): Float32 = sqrt(arg.x.pow(2) + arg.y.pow(2) + arg.z.pow(2))
public fun Float32Vector3D.norm(): Float32 = norm(this) public fun Vector3D<Float32>.norm(): Float32 = norm(this)
override fun Float32Vector3D.unaryMinus(): Float32Vector3D = vector(-x, -y, -z) override fun Vector3D<Float32>.unaryMinus(): Float32Vector3D = vector(-x, -y, -z)
override fun Float32Vector3D.distanceTo(other: Float32Vector3D): Float32 = (this - other).norm() override fun Vector3D<Float32>.distanceTo(other: Vector3D<Float32>): Float32 = (this - other).norm()
override fun add(left: Float32Vector3D, right: Float32Vector3D): Float32Vector3D = override fun add(left: Vector3D<Float32>, right: Vector3D<Float32>): Float32Vector3D =
vector(left.x + right.x, left.y + right.y, left.z + right.z) vector(left.x + right.x, left.y + right.y, left.z + right.z)
override fun scale(a: Float32Vector3D, value: Double): Float32Vector3D = override fun scale(a: Vector3D<Float32>, value: Double): Float32Vector3D =
vector(a.x * value, a.y * value, a.z * value) vector(a.x * value, a.y * value, a.z * value)
override fun Float32Vector3D.dot(other: Float32Vector3D): Double = override fun Vector3D<Float32>.dot(other: Vector3D<Float32>): Double =
(x * other.x + y * other.y + z * other.z).toDouble() (x * other.x + y * other.y + z * other.z).toDouble()
/** /**
@ -106,7 +106,7 @@ public object Float32Space3D : GeometrySpace<Float32Vector3D, Float32> {
override val defaultPrecision: Float32 = 1e-3f override val defaultPrecision: Float32 = 1e-3f
override val bufferFactory: MutableBufferFactory<Float32Vector3D> = MutableBufferFactory() override val bufferFactory: MutableBufferFactory<Vector3D<Float32>> = MutableBufferFactory()
} }
public fun Float32Vector3D(x: Number, y: Number, z: Number): Float32Vector3D = Float32Space3D.vector(x, y, z) public fun Float32Vector3D(x: Number, y: Number, z: Number): Float32Vector3D = Float32Space3D.vector(x, y, z)

View File

@ -128,7 +128,8 @@ public object Float64Space3D : GeometrySpace<Vector3D<Float64>, Double> {
override val bufferFactory: MutableBufferFactory<Vector3D<Float64>> = MutableBufferFactory() override val bufferFactory: MutableBufferFactory<Vector3D<Float64>> = MutableBufferFactory()
} }
@Suppress("UnusedReceiverParameter")
public val Float64Field.euclidean3D: Float64Space3D get() = Float64Space3D public val Float64Field.euclidean3D: Float64Space3D get() = Float64Space3D
public val Float64Vector3D.r: Double get() = Float64Space3D.norm(this) public val Vector3D<Float64>.r: Double get() = Float64Space3D.norm(this)

View File

@ -37,7 +37,8 @@ public infix fun Quaternion.dot(other: Quaternion): Double = w * other.w + x * o
/** /**
* Represent a vector as quaternion with zero a rotation angle. * Represent a vector as quaternion with zero a rotation angle.
*/ */
internal fun Float64Vector3D.asQuaternion(): Quaternion = Quaternion(0.0, x, y, z) internal fun Vector3D<Float64>.asQuaternion(): Quaternion = Quaternion(0.0, x, y, z)
/** /**
* Angle in radians denoted by this quaternion rotation * Angle in radians denoted by this quaternion rotation
@ -71,7 +72,7 @@ public val Quaternion.vector: Float64Vector3D
/** /**
* Rotate a vector in a [Float64Space3D] with [quaternion] * Rotate a vector in a [Float64Space3D] with [quaternion]
*/ */
public fun Float64Space3D.rotate(vector: Float64Vector3D, quaternion: Quaternion): Float64Vector3D = public fun Float64Space3D.rotate(vector: Vector3D<Float64>, quaternion: Quaternion): Float64Vector3D =
with(QuaternionAlgebra) { with(QuaternionAlgebra) {
val p = vector.asQuaternion() val p = vector.asQuaternion()
(quaternion * p * quaternion.reciprocal).vector (quaternion * p * quaternion.reciprocal).vector

View File

@ -9,6 +9,7 @@ import space.kscience.kmath.geometry.euclidean2d.Float64Space2D
import space.kscience.kmath.geometry.euclidean2d.Float64Vector2D import space.kscience.kmath.geometry.euclidean2d.Float64Vector2D
import space.kscience.kmath.geometry.euclidean3d.Float64Space3D import space.kscience.kmath.geometry.euclidean3d.Float64Space3D
import space.kscience.kmath.geometry.euclidean3d.Float64Vector3D import space.kscience.kmath.geometry.euclidean3d.Float64Vector3D
import space.kscience.kmath.structures.Float64
/** /**
@ -25,7 +26,7 @@ public fun <V : Any, D : Comparable<D>> V.equalsVector(
/** /**
* Vector equality using Euclidian L2 norm and given [precision] * Vector equality using Euclidian L2 norm and given [precision]
*/ */
public fun Float64Vector2D.equalsVector( public fun Vector2D<Float64>.equalsVector(
other: Float64Vector2D, other: Float64Vector2D,
precision: Double = Float64Space2D.defaultPrecision, precision: Double = Float64Space2D.defaultPrecision,
): Boolean = equalsVector(Float64Space2D, other, precision) ): Boolean = equalsVector(Float64Space2D, other, precision)
@ -33,7 +34,7 @@ public fun Float64Vector2D.equalsVector(
/** /**
* Vector equality using Euclidean L2 norm and given [precision] * Vector equality using Euclidean L2 norm and given [precision]
*/ */
public fun Float64Vector3D.equalsVector( public fun Vector3D<Float64>.equalsVector(
other: Float64Vector3D, other: Float64Vector3D,
precision: Double = Float64Space3D.defaultPrecision, precision: Double = Float64Space3D.defaultPrecision,
): Boolean = equalsVector(Float64Space3D, other, precision) ): Boolean = equalsVector(Float64Space3D, other, precision)