Fix scalar multiplication in BigInt, implement testers for fields, rings and spaces and apply them to ComplexFieldTest, BigIntAlgebraTest and RealFieldTest
This commit is contained in:
parent
5b215833ca
commit
1e4692d5cf
@ -22,8 +22,9 @@ object BigIntField : Field<BigInt> {
|
||||
override val one: BigInt = BigInt.ONE
|
||||
|
||||
override fun add(a: BigInt, b: BigInt): BigInt = a.plus(b)
|
||||
override fun number(value: Number): BigInt = value.toLong().toBigInt()
|
||||
|
||||
override fun multiply(a: BigInt, k: Number): BigInt = a.times(k.toLong())
|
||||
override fun multiply(a: BigInt, k: Number): BigInt = a.times(number(k))
|
||||
|
||||
override fun multiply(a: BigInt, b: BigInt): BigInt = a.times(b)
|
||||
|
||||
|
@ -1,9 +1,13 @@
|
||||
package scientifik.kmath.operations
|
||||
|
||||
import scientifik.kmath.operations.internal.RingVerifier
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class BigIntAlgebraTest {
|
||||
internal class BigIntAlgebraTest {
|
||||
@Test
|
||||
fun verify() = BigIntField { RingVerifier(this, +"42", +"10", +"-12", 10).verify() }
|
||||
|
||||
@Test
|
||||
fun testKBigIntegerRingSum() {
|
||||
val res = BigIntField {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package scientifik.kmath.operations
|
||||
|
||||
import scientifik.kmath.operations.internal.FieldVerifier
|
||||
import kotlin.math.PI
|
||||
import kotlin.math.abs
|
||||
import kotlin.test.Test
|
||||
@ -7,6 +8,9 @@ import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
internal class ComplexFieldTest {
|
||||
@Test
|
||||
fun verify() = ComplexField { FieldVerifier(this, 42.0 * i, 66.0 + 28 * i, 2.0 + 0 * i, 5).verify() }
|
||||
|
||||
@Test
|
||||
fun testAddition() {
|
||||
assertEquals(Complex(42, 42), ComplexField { Complex(16, 16) + Complex(26, 26) })
|
||||
|
@ -1,14 +1,16 @@
|
||||
package scientifik.kmath.operations
|
||||
|
||||
import scientifik.kmath.operations.internal.FieldVerifier
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class RealFieldTest {
|
||||
internal class RealFieldTest {
|
||||
@Test
|
||||
fun verify() = FieldVerifier(RealField, 42.0, 66.0, 2.0, 5).verify()
|
||||
|
||||
@Test
|
||||
fun testSqrt() {
|
||||
val sqrt = RealField {
|
||||
sqrt(25 * one)
|
||||
}
|
||||
val sqrt = RealField { sqrt(25 * one) }
|
||||
assertEquals(5.0, sqrt)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
package scientifik.kmath.operations.internal
|
||||
|
||||
import scientifik.kmath.operations.Algebra
|
||||
|
||||
internal interface AlgebraicVerifier<T, out A> where A : Algebra<T> {
|
||||
val algebra: A
|
||||
|
||||
fun verify()
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package scientifik.kmath.operations.internal
|
||||
|
||||
import scientifik.kmath.operations.Field
|
||||
import scientifik.kmath.operations.invoke
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotEquals
|
||||
|
||||
internal class FieldVerifier<T>(override val algebra: Field<T>, a: T, b: T, c: T, x: Number) :
|
||||
RingVerifier<T>(algebra, a, b, c, x) {
|
||||
|
||||
override fun verify() {
|
||||
super.verify()
|
||||
|
||||
algebra {
|
||||
assertNotEquals(a / b, b / a, "Division in $algebra is not anti-commutative.")
|
||||
assertNotEquals((a / b) / c, a / (b / c), "Division in $algebra is associative.")
|
||||
assertEquals((a + b) / c, (a / c) + (b / c), "Division in $algebra is not right-distributive.")
|
||||
assertEquals(a, a / one, "$one in $algebra is not neutral division element.")
|
||||
assertEquals(one, one / a * a, "$algebra does not provide single reciprocal element.")
|
||||
assertEquals(zero / a, zero, "$zero in $algebra is not left neutral element for division.")
|
||||
assertEquals(-one, a / (-a), "Division by sign reversal element in $algebra does not give ${-one}.")
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package scientifik.kmath.operations.internal
|
||||
|
||||
import scientifik.kmath.operations.Ring
|
||||
import scientifik.kmath.operations.invoke
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
internal open class RingVerifier<T>(override val algebra: Ring<T>, a: T, b: T, c: T, x: Number) :
|
||||
SpaceVerifier<T>(algebra, a, b, c, x) {
|
||||
override fun verify() {
|
||||
super.verify()
|
||||
|
||||
algebra {
|
||||
assertEquals(a * b, a * b, "Multiplication in $algebra is not commutative.")
|
||||
assertEquals(a * b * c, a * (b * c), "Multiplication in $algebra is not associative.")
|
||||
assertEquals(c * (a + b), (c * a) + (c * b), "Multiplication in $algebra is not distributive.")
|
||||
assertEquals(a * one, one * a, "$one in $algebra is not a neutral multiplication element.")
|
||||
assertEquals(a, one * a, "$one in $algebra is not a neutral multiplication element.")
|
||||
assertEquals(a, a * one, "$one in $algebra is not a neutral multiplication element.")
|
||||
assertEquals(a, one * a, "$one in $algebra is not a neutral multiplication element.")
|
||||
assertEquals(a, a * one * one, "Multiplication by $one in $algebra is not idempotent.")
|
||||
assertEquals(a, a * one * one * one, "Multiplication by $one in $algebra is not idempotent.")
|
||||
assertEquals(a, a * one * one * one * one, "Multiplication by $one in $algebra is not idempotent.")
|
||||
assertEquals(zero, a * zero, "Multiplication by $zero in $algebra doesn't give $zero.")
|
||||
assertEquals(zero, zero * a, "Multiplication by $zero in $algebra doesn't give $zero.")
|
||||
assertEquals(a * zero, a * zero, "Multiplication by $zero in $algebra doesn't give $zero.")
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package scientifik.kmath.operations.internal
|
||||
|
||||
import scientifik.kmath.operations.Space
|
||||
import scientifik.kmath.operations.invoke
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotEquals
|
||||
|
||||
internal open class SpaceVerifier<T>(
|
||||
override val algebra: Space<T>,
|
||||
val a: T,
|
||||
val b: T,
|
||||
val c: T,
|
||||
val x: Number
|
||||
) :
|
||||
AlgebraicVerifier<T, Space<T>> {
|
||||
override fun verify() {
|
||||
algebra {
|
||||
assertEquals(a + b, b + a, "Addition in $algebra is not commutative.")
|
||||
assertEquals(a + b + c, a + (b + c), "Addition in $algebra is not associative.")
|
||||
assertEquals(x * (a + b), x * a + x * b, "Addition in $algebra is not distributive.")
|
||||
assertEquals((a + b) * x, a * x + b * x, "Addition in $algebra is not distributive.")
|
||||
assertEquals(a + zero, zero + a, "$zero in $algebra is not a neutral addition element.")
|
||||
assertEquals(a, a + zero, "$zero in $algebra is not a neutral addition element.")
|
||||
assertEquals(a, zero + a, "$zero in $algebra is not a neutral addition element.")
|
||||
assertEquals(a - b, -(b - a), "Subtraction in $algebra is not anti-commutative.")
|
||||
assertNotEquals(a - b - c, a - (b - c), "Subtraction in $algebra is associative.")
|
||||
assertEquals(x * (a - b), x * a - x * b, "Subtraction in $algebra is not distributive.")
|
||||
assertEquals(a, a - zero, "$zero in $algebra is not a neutral addition element.")
|
||||
assertEquals(a * x, x * a, "Multiplication by scalar in $algebra is not commutative.")
|
||||
assertEquals(x * (a + b), (x * a) + (x * b), "Multiplication by scalar in $algebra is not distributive.")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user