Adapt NumberedPolynomial tests to LabeledPolynomial tests.

This commit is contained in:
Gleb Minaev 2022-07-06 17:13:50 +03:00
parent 5834fad938
commit 923c52737d
7 changed files with 10296 additions and 24 deletions

View File

@ -177,14 +177,17 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
* The operation is equivalent to sum of [other] copies of [this].
*/
public override operator fun LabeledPolynomial<C>.times(other: Int): LabeledPolynomial<C> =
if (other == 0) zero
else LabeledPolynomial(
when(other) {
0 -> zero
1 -> this
else -> LabeledPolynomial(
coefficients
.toMutableMap()
.apply {
for (degs in keys) this[degs] = this[degs]!! * other
}
)
}
/**
* Returns sum of the integer represented as a polynomial and the polynomial.
@ -210,7 +213,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
* The operation is equivalent to subtraction [this] copies of unit polynomial from [other].
*/
public override operator fun Int.minus(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
if (this == 0) other
if (this == 0) -other
else with(other.coefficients) {
if (isEmpty()) LabeledPolynomialAsIs(mapOf(emptyMap<Symbol, UInt>() to this@minus.asConstant()))
else LabeledPolynomialAsIs(
@ -230,14 +233,17 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
* The operation is equivalent to sum of [this] copies of [other].
*/
public override operator fun Int.times(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
if (this == 0) zero
else LabeledPolynomial(
when(this) {
0 -> zero
1 -> other
else -> LabeledPolynomial(
other.coefficients
.toMutableMap()
.apply {
for (degs in keys) this[degs] = this@times * this[degs]!!
}
)
}
/**
* Converts the integer [value] to polynomial.
@ -360,8 +366,6 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
else LabeledPolynomialAsIs(
toMutableMap()
.apply {
forEach { (degs, c) -> if(degs.isNotEmpty()) this[degs] = -c }
val degs = emptyMap<Symbol, UInt>()
this[degs] = getOrElse(degs) { constantZero } - other

View File

@ -302,6 +302,7 @@ public class DSL1LabeledPolynomialTermSignatureBuilder {
* Declaring another power of the same variable will increase its degree by received degree.
*/
public infix fun Symbol.inPowerOf(deg: UInt) {
if (deg == 0u) return
signature[this] = signature.getOrElse(this) { 0u } + deg
}
/**

View File

@ -0,0 +1,129 @@
/*
* Copyright 2018-2021 KMath contributors.
* 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.functions
import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.expressions.symbol
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.algebra
import space.kscience.kmath.operations.invoke
import kotlin.test.Test
import kotlin.test.assertEquals
class LabeledConstructorsTest {
val x by symbol
val y by symbol
val z by symbol
val t by symbol
@Test
@UnstableKMathAPI
fun testDSL1() {
assertEquals(
LabeledPolynomialAsIs(
mapOf(x to 2u, z to 3u) to 5,
mapOf(y to 1u) to -6,
),
Int.algebra.labeledPolynomialSpace {
LabeledPolynomialDSL1 {
5 { x pow 2u; z pow 3u }
(-6) { y pow 1u }
}
},
"test 1"
)
assertEquals(
LabeledPolynomialAsIs(
mapOf<Symbol, UInt>() to -1,
),
Int.algebra.labeledPolynomialSpace {
LabeledPolynomialDSL1 {
5 { }
(-6) { }
}
},
"test 2"
)
assertEquals(
LabeledPolynomialAsIs(
mapOf(x to 2u) to -1,
),
Int.algebra.labeledPolynomialSpace {
LabeledPolynomialDSL1 {
5 { x pow 1u; x pow 1u }
(-6) { x pow 2u }
}
},
"test 3"
)
assertEquals(
LabeledPolynomialAsIs(
mapOf(x to 2u) to -1,
),
Int.algebra.labeledPolynomialSpace {
LabeledPolynomialDSL1 {
5 { x pow 1u; x pow 1u }
(-6) { x pow 2u; z pow 0u }
}
},
"test 3"
)
}
@Test
@UnstableKMathAPI
fun testFabric() {
assertEquals(
LabeledPolynomialAsIs(
mapOf(x to 2u, z to 3u) to 5,
mapOf(y to 1u) to -6,
),
Int.algebra {
LabeledPolynomial(
mapOf(x to 2u, z to 3u) to 5,
mapOf(y to 1u) to -6,
)
},
"test 1"
)
assertEquals(
LabeledPolynomialAsIs(
mapOf(x to 2u, z to 3u) to 5,
mapOf(y to 1u) to -6,
),
Int.algebra {
LabeledPolynomial(
mapOf(x to 2u, y to 0u, z to 3u, t to 0u) to 5,
mapOf(x to 0u, y to 1u, z to 0u, t to 0u) to -6,
)
},
"test 2"
)
assertEquals(
LabeledPolynomialAsIs(
mapOf<Symbol, UInt>() to -1,
),
Int.algebra {
LabeledPolynomial(
mapOf(x to 0u) to 5,
mapOf(y to 0u, z to 0u) to -6,
)
},
"test 3"
)
assertEquals(
LabeledPolynomialAsIs(
mapOf<Symbol, UInt>() to 0,
),
Int.algebra {
LabeledPolynomial(
mapOf(x to 0u) to 5,
mapOf(z to 0u, t to 0u) to -5,
)
},
"test 4"
)
}
}

View File

@ -7,14 +7,19 @@
package space.kscience.kmath.functions
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.test.misc.*
import kotlin.test.*
import space.kscience.kmath.test.misc.IntModuloRing
import space.kscience.kmath.test.misc.Rational
import space.kscience.kmath.test.misc.RationalField
import space.kscience.kmath.test.misc.m
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertSame
import kotlin.test.fail
@UnstableKMathAPI
class NumberedPolynomialTest {
private val o = Rational(0)
val o = Rational(0)
@Test
fun test_Polynomial_Int_plus() {
RationalField.numberedPolynomialSpace {
@ -557,6 +562,7 @@ class NumberedPolynomialTest {
)
assertEquals(
NumberedPolynomial(
listOf<UInt>() to Rational(0),
listOf(3u) to Rational(-8, 9),
listOf(0u, 4u) to Rational(-8, 7),
),
@ -650,6 +656,7 @@ class NumberedPolynomialTest {
)
assertEquals(
NumberedPolynomial(
listOf<UInt>() to Rational(0),
listOf(3u) to Rational(-8, 9),
listOf(0u, 4u) to Rational(-8, 7),
),
@ -816,6 +823,7 @@ class NumberedPolynomialTest {
)
assertEquals(
NumberedPolynomial(
listOf<UInt>() to Rational(0),
listOf(3u) to Rational(-8, 9),
listOf(0u, 4u) to Rational(-8, 7),
),

View File

@ -5,6 +5,8 @@
package space.kscience.kmath.test.misc
import space.kscience.kmath.functions.LabeledPolynomial
import space.kscience.kmath.functions.LabeledRationalFunction
import space.kscience.kmath.functions.NumberedPolynomial
import space.kscience.kmath.functions.NumberedRationalFunction
import kotlin.test.assertEquals
@ -28,6 +30,18 @@ fun assertEquals(
message
)
fun assertEquals(
expected: LabeledPolynomial<Double>,
actual: LabeledPolynomial<Double>,
absoluteTolerance: Double,
message: String? = null
) = assertContentEquals(
expected.coefficients,
actual.coefficients,
absoluteTolerance,
message
)
fun assertEquals(
expected: NumberedRationalFunction<Double>,
actual: NumberedRationalFunction<Double>,
@ -48,6 +62,26 @@ fun assertEquals(
)
}
fun assertEquals(
expected: LabeledRationalFunction<Double>,
actual: LabeledRationalFunction<Double>,
absoluteTolerance: Double,
message: String? = null
) {
assertEquals(
expected.numerator,
actual.numerator,
absoluteTolerance,
message
)
assertEquals(
expected.denominator,
actual.denominator,
absoluteTolerance,
message
)
}
inline fun <reified T : Throwable> assertFailsWithTypeAndMessage(
expectedMessage: String? = null,
assertionMessage: String? = null,