Fix vector product

This commit is contained in:
Alexander Nozik 2023-03-16 09:37:03 +03:00
parent cd2ade881a
commit ef336af87d
8 changed files with 40 additions and 15 deletions

3
.gitignore vendored
View File

@ -18,4 +18,5 @@ out/
!/.idea/copyright/ !/.idea/copyright/
!/.idea/scopes/ !/.idea/scopes/
/kotlin-js-store/yarn.lock /gradle/yarn.lock

View File

@ -2,6 +2,7 @@
## [Unreleased] ## [Unreleased]
### Added ### Added
- Generic builders for `BufferND` and `MutableBufferND`
- `NamedMatrix` - matrix with symbol-based indexing - `NamedMatrix` - matrix with symbol-based indexing
- `Expression` with default arguments - `Expression` with default arguments
- Type-aliases for numbers like `Float64` - Type-aliases for numbers like `Float64`

View File

@ -55,17 +55,19 @@ dependencies {
implementation("space.kscience:plotlykt-server:0.5.0") implementation("space.kscience:plotlykt-server:0.5.0")
} }
kotlin.sourceSets.all { kotlin {
jvmToolchain(11)
sourceSets.all {
with(languageSettings) { with(languageSettings) {
optIn("kotlin.contracts.ExperimentalContracts") optIn("kotlin.contracts.ExperimentalContracts")
optIn("kotlin.ExperimentalUnsignedTypes") optIn("kotlin.ExperimentalUnsignedTypes")
optIn("space.kscience.kmath.misc.UnstableKMathAPI") optIn("space.kscience.kmath.misc.UnstableKMathAPI")
} }
}
} }
tasks.withType<KotlinJvmCompile> { tasks.withType<KotlinJvmCompile> {
kotlinOptions { kotlinOptions {
jvmTarget = "11"
freeCompilerArgs = freeCompilerArgs + "-Xjvm-default=all" + "-Xopt-in=kotlin.RequiresOptIn" + "-Xlambdas=indy" freeCompilerArgs = freeCompilerArgs + "-Xjvm-default=all" + "-Xopt-in=kotlin.RequiresOptIn" + "-Xlambdas=indy"
} }
} }

View File

@ -9,7 +9,7 @@ kotlin.native.ignoreDisabledTargets=true
org.gradle.configureondemand=true org.gradle.configureondemand=true
org.gradle.jvmargs=-Xmx4096m org.gradle.jvmargs=-Xmx4096m
toolsVersion=0.14.3-kotlin-1.8.20-RC toolsVersion=0.14.4-kotlin-1.8.20-RC
org.gradle.parallel=true org.gradle.parallel=true

View File

@ -7,7 +7,9 @@ package space.kscience.kmath.nd
import space.kscience.kmath.misc.PerformancePitfall import space.kscience.kmath.misc.PerformancePitfall
import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.BufferFactory
import space.kscience.kmath.structures.MutableBuffer import space.kscience.kmath.structures.MutableBuffer
import space.kscience.kmath.structures.MutableBufferFactory
/** /**
* Represents [StructureND] over [Buffer]. * Represents [StructureND] over [Buffer].
@ -29,6 +31,18 @@ public open class BufferND<out T>(
override fun toString(): String = StructureND.toString(this) override fun toString(): String = StructureND.toString(this)
} }
/**
* Create a generic [BufferND] using provided [initializer]
*/
public fun <T> BufferND(
shape: ShapeND,
bufferFactory: BufferFactory<T> = BufferFactory.boxing(),
initializer: (IntArray) -> T,
): BufferND<T> {
val strides = Strides(shape)
return BufferND(strides, bufferFactory(strides.linearSize) { initializer(strides.index(it)) })
}
///** ///**
// * Transform structure to a new structure using provided [BufferFactory] and optimizing if argument is [BufferND] // * Transform structure to a new structure using provided [BufferFactory] and optimizing if argument is [BufferND]
// */ // */
@ -67,6 +81,18 @@ public open class MutableBufferND<T>(
} }
} }
/**
* Create a generic [BufferND] using provided [initializer]
*/
public fun <T> MutableBufferND(
shape: ShapeND,
bufferFactory: MutableBufferFactory<T> = MutableBufferFactory.boxing(),
initializer: (IntArray) -> T,
): MutableBufferND<T> {
val strides = Strides(shape)
return MutableBufferND(strides, bufferFactory(strides.linearSize) { initializer(strides.index(it)) })
}
///** ///**
// * Transform structure to a new structure using provided [MutableBufferFactory] and optimizing if argument is [MutableBufferND] // * Transform structure to a new structure using provided [MutableBufferFactory] and optimizing if argument is [MutableBufferND]
// */ // */

View File

@ -9,7 +9,6 @@ import space.kscience.kmath.operations.mapToBuffer
import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.DoubleBuffer import space.kscience.kmath.structures.DoubleBuffer
import space.kscience.kmath.structures.asBuffer import space.kscience.kmath.structures.asBuffer
import kotlin.jvm.Synchronized
import kotlin.math.ulp import kotlin.math.ulp
import kotlin.native.concurrent.ThreadLocal import kotlin.native.concurrent.ThreadLocal
@ -57,7 +56,6 @@ public object GaussLegendreRuleFactory : GaussIntegratorRuleFactory {
private val cache = HashMap<Int, Pair<Buffer<Double>, Buffer<Double>>>() private val cache = HashMap<Int, Pair<Buffer<Double>, Buffer<Double>>>()
@Synchronized
private fun getOrBuildRule(numPoints: Int): Pair<Buffer<Double>, Buffer<Double>> = private fun getOrBuildRule(numPoints: Int): Pair<Buffer<Double>, Buffer<Double>> =
cache.getOrPut(numPoints) { buildRule(numPoints) } cache.getOrPut(numPoints) { buildRule(numPoints) }

View File

@ -117,13 +117,11 @@ public object Euclidean3DSpace : GeometrySpace<DoubleVector3D>, ScaleOperations<
} }
/** /**
* Compute vector product of [first] and [second]. The basis assumed to be right-handed if [rightBasis] is true and * Compute vector product of [first] and [second]. The basis assumed to be right-handed.
* left-handed otherwise
*/ */
public fun vectorProduct( public fun vectorProduct(
first: DoubleVector3D, first: DoubleVector3D,
second: DoubleVector3D, second: DoubleVector3D,
rightBasis: Boolean = true,
): DoubleVector3D { ): DoubleVector3D {
var x = 0.0 var x = 0.0
var y = 0.0 var y = 0.0
@ -137,7 +135,7 @@ public object Euclidean3DSpace : GeometrySpace<DoubleVector3D>, ScaleOperations<
} }
} }
return vector(x, y, z) * (if (rightBasis) 1 else -1) return vector(x, y, z)
} }
/** /**

View File

@ -78,7 +78,6 @@ internal class Euclidean3DSpaceTest {
assertVectorEquals(zAxis, vectorProduct(xAxis, yAxis)) assertVectorEquals(zAxis, vectorProduct(xAxis, yAxis))
assertVectorEquals(zAxis, xAxis cross yAxis) assertVectorEquals(zAxis, xAxis cross yAxis)
assertVectorEquals(-zAxis, vectorProduct(yAxis, xAxis)) assertVectorEquals(-zAxis, vectorProduct(yAxis, xAxis))
assertVectorEquals(zAxis, vectorProduct(yAxis, xAxis, rightBasis = false))
} }
@Test @Test