Apply suggestions from code review

Co-authored-by: Iaroslav Postovalov <38042667+CommanderTvis@users.noreply.github.com>
This commit is contained in:
Ven 2021-08-17 21:17:02 +03:00 committed by GitHub
parent 603db46eb8
commit f271ded526
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 31 additions and 31 deletions

View File

@ -1,7 +1,7 @@
package space.kscience.kmath.geometry package space.kscience.kmath.geometry
/** /**
* Project vector to a line * Project vector onto a line.
* @param vector to project * @param vector to project
* @param line line to which vector should be projected * @param line line to which vector should be projected
*/ */
@ -10,8 +10,8 @@ public fun <V : Vector> GeometrySpace<V>.projectToLine(vector: V, line: Line<V>)
} }
/** /**
* Project vector to a hyper-plane, which is defined by a normal and base * Project vector onto a hyperplane, which is defined by a normal and base.
* In 2d case it is projection to a line, in 3d case it is projection to a plane * In 2D case it is the projection to a line, in 3d case it is the one to a plane.
* @param vector to project * @param vector to project
* @param normal normal (perpendicular) vector to a hyper-plane to which vector should be projected * @param normal normal (perpendicular) vector to a hyper-plane to which vector should be projected
* @param base point belonging to a hyper-plane to which vector should be projected * @param base point belonging to a hyper-plane to which vector should be projected

View File

@ -6,7 +6,7 @@ import kotlin.test.assertEquals
internal class Euclidean2DSpaceTest { internal class Euclidean2DSpaceTest {
@Test @Test
fun getZero() { fun zero() {
assertVectorEquals(Vector2D(0.0, 0.0), Euclidean2DSpace.zero) assertVectorEquals(Vector2D(0.0, 0.0), Euclidean2DSpace.zero)
} }
@ -59,4 +59,4 @@ internal class Euclidean2DSpaceTest {
assertVectorEquals(Vector2D(300.0, 0.0003), Vector2D(100.0, 0.0001) * 3) assertVectorEquals(Vector2D(300.0, 0.0003), Vector2D(100.0, 0.0001) * 3)
} }
} }
} }

View File

@ -5,7 +5,7 @@ import kotlin.test.assertEquals
internal class Euclidean3DSpaceTest { internal class Euclidean3DSpaceTest {
@Test @Test
fun getZero() { fun zero() {
assertVectorEquals(Vector3D(0.0, 0.0, 0.0), Euclidean3DSpace.zero) assertVectorEquals(Vector3D(0.0, 0.0, 0.0), Euclidean3DSpace.zero)
} }
@ -71,4 +71,4 @@ internal class Euclidean3DSpaceTest {
assertVectorEquals(Vector3D(2.0, -4.0, 0.0), Vector3D(1.0, -2.0, 0.0) * 2) assertVectorEquals(Vector3D(2.0, -4.0, 0.0), Vector3D(1.0, -2.0, 0.0) * 2)
} }
} }
} }

View File

@ -58,4 +58,4 @@ internal class ProjectionAlongTest {
} }
} }
} }
} }

View File

@ -80,4 +80,4 @@ internal class ProjectionOntoLineTest {
} }
} }
} }
} }

View File

@ -9,7 +9,7 @@ internal class Vector2DTest {
private val vector = Vector2D(1.0, -7.999) private val vector = Vector2D(1.0, -7.999)
@Test @Test
fun getSize() { fun size() {
assertEquals(2, vector.size) assertEquals(2, vector.size)
} }
@ -20,17 +20,17 @@ internal class Vector2DTest {
} }
@Test @Test
operator fun iterator() { fun iterator() {
assertEquals(listOf(1.0, -7.999), vector.toList()) assertEquals(listOf(1.0, -7.999), vector.toList())
} }
@Test @Test
fun getX() { fun x() {
assertEquals(1.0, vector.x) assertEquals(1.0, vector.x)
} }
@Test @Test
fun getY() { fun y() {
assertEquals(-7.999, vector.y) assertEquals(-7.999, vector.y)
} }
} }

View File

@ -8,7 +8,7 @@ internal class Vector3DTest {
private val vector = Vector3D(1.0, -7.999, 0.001) private val vector = Vector3D(1.0, -7.999, 0.001)
@Test @Test
fun getSize() { fun size() {
assertEquals(3, vector.size) assertEquals(3, vector.size)
} }
@ -20,22 +20,22 @@ internal class Vector3DTest {
} }
@Test @Test
operator fun iterator() { fun iterator() {
assertEquals(listOf(1.0, -7.999, 0.001), vector.toList()) assertEquals(listOf(1.0, -7.999, 0.001), vector.toList())
} }
@Test @Test
fun getX() { fun x() {
assertEquals(1.0, vector.x) assertEquals(1.0, vector.x)
} }
@Test @Test
fun getY() { fun y() {
assertEquals(-7.999, vector.y) assertEquals(-7.999, vector.y)
} }
@Test @Test
fun getZ() { fun z() {
assertEquals(0.001, vector.z) assertEquals(0.001, vector.z)
} }
} }

View File

@ -20,22 +20,22 @@ fun grid(
return xs.flatMap { x -> ys.map { y -> x to y } } return xs.flatMap { x -> ys.map { y -> x to y } }
} }
fun assertVectorEquals(expected: Vector2D, actual: Vector2D, eps: Double = 1e-6) { fun assertVectorEquals(expected: Vector2D, actual: Vector2D, absoluteTolerance: Double = 1e-6) {
assertEquals(expected.x, actual.x, eps) assertEquals(expected.x, actual.x, absoluteTolerance)
assertEquals(expected.y, actual.y, eps) assertEquals(expected.y, actual.y, absoluteTolerance)
} }
fun assertVectorEquals(expected: Vector3D, actual: Vector3D, eps: Double = 1e-6) { fun assertVectorEquals(expected: Vector3D, actual: Vector3D, absoluteTolerance: Double = 1e-6) {
assertEquals(expected.x, actual.x, eps) assertEquals(expected.x, actual.x, absoluteTolerance)
assertEquals(expected.y, actual.y, eps) assertEquals(expected.y, actual.y, absoluteTolerance)
assertEquals(expected.z, actual.z, eps) assertEquals(expected.z, actual.z, absoluteTolerance)
} }
fun <V : Vector> GeometrySpace<V>.isCollinear(a: V, b: V, eps: Double = 1e-6): Boolean { fun <V : Vector> GeometrySpace<V>.isCollinear(a: V, b: V, absoluteTolerance: Double = 1e-6): Boolean {
val aDist = a.distanceTo(zero) val aDist = a.distanceTo(zero)
val bDist = b.distanceTo(zero) val bDist = b.distanceTo(zero)
return abs(aDist) < eps || abs(bDist) < eps || abs(abs((a dot b) / (aDist * bDist)) - 1) < eps return abs(aDist) < absoluteTolerance || abs(bDist) < absoluteTolerance || abs(abs((a dot b) / (aDist * bDist)) - 1) < absoluteTolerance
} }
fun <V : Vector> GeometrySpace<V>.isOrthogonal(a: V, b: V, eps: Double = 1e-6): Boolean = fun <V : Vector> GeometrySpace<V>.isOrthogonal(a: V, b: V, absoluteTolerance: Double = 1e-6): Boolean =
abs(a dot b) < eps abs(a dot b) < absoluteTolerance