Feature: Polynomials and rational functions #469

Merged
lounres merged 132 commits from feature/polynomials into dev 2022-07-28 18:04:06 +03:00
3 changed files with 33 additions and 93 deletions
Showing only changes of commit b3087c245f - Show all commits

View File

@ -9,7 +9,7 @@ import space.kscience.kmath.test.misc.*
import kotlin.test.*
class ListPolynomialTest { // TODO: Adapt tests to changes
class ListPolynomialTest {
@Test
fun test_Polynomial_Int_plus() {
RationalField.listPolynomial {
@ -24,7 +24,7 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
"test 2"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
ListPolynomial(Rational(-2)) + 2,
"test 3"
)
@ -64,7 +64,7 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
"test 2"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
ListPolynomial(Rational(2)) - 2,
"test 3"
)
@ -99,7 +99,7 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
"test 1"
)
assertEquals(
ListPolynomial(),
ListPolynomial(0, 0, 0, 0, 0),
ListPolynomial(7, 0, 49, 21, 14) * 15,
"test 2"
)
@ -119,7 +119,7 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
"test 2"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
2 + ListPolynomial(Rational(-2)),
"test 3"
)
@ -159,7 +159,7 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
"test 2"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
-2 - ListPolynomial(Rational(-2)),
"test 3"
)
@ -194,7 +194,7 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
"test 1"
)
assertEquals(
ListPolynomial(),
ListPolynomial(0, 0, 0, 0, 0),
15 * ListPolynomial(7, 0, 49, 21, 14),
"test 2"
)
@ -214,12 +214,12 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
"test 2"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
ListPolynomial(Rational(-2)) + Rational(2),
"test 3"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
ListPolynomial<Rational>() + Rational(0),
"test 4"
)
@ -254,12 +254,12 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
"test 2"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
ListPolynomial(Rational(2)) - Rational(2),
"test 3"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
ListPolynomial<Rational>() - Rational(0),
"test 4"
)
@ -285,12 +285,12 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
IntModuloRing(35).listPolynomial {
assertEquals(
ListPolynomial(34, 2, 1, 20, 2),
ListPolynomial(22, 26, 13, 15, 26) * number(27),
ListPolynomial(22, 26, 13, 15, 26) * 27.asConstant(),
"test 1"
)
assertEquals(
ListPolynomial(),
ListPolynomial(7, 0, 49, 21, 14) * number(15),
ListPolynomial(0, 0, 0, 0, 0),
ListPolynomial(7, 0, 49, 21, 14) * 15.asConstant(),
"test 2"
)
}
@ -309,12 +309,12 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
"test 2"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
Rational(2) + ListPolynomial(Rational(-2)),
"test 3"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
Rational(0) + ListPolynomial(),
"test 4"
)
@ -349,12 +349,12 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
"test 2"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
Rational(-2) - ListPolynomial(Rational(-2)),
"test 3"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
Rational(0) - ListPolynomial(),
"test 4"
)
@ -384,7 +384,7 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
"test 1"
)
assertEquals(
ListPolynomial(),
ListPolynomial(0, 0, 0, 0, 0),
15 * ListPolynomial(7, 0, 49, 21, 14),
"test 2"
)
@ -424,14 +424,14 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
)
// (-4/7 - 2/6 x + 0 x^2 + 0 x^3) + (-6/3 - 7/2 x + 2/3 x^2) ?= -18/7 - 23/6 x + 2/3 x^2
assertEquals(
ListPolynomial(Rational(-18, 7), Rational(-23, 6), Rational(2, 3)),
ListPolynomial(Rational(-18, 7), Rational(-23, 6), Rational(2, 3), Rational(0)),
ListPolynomial(Rational(-4, 7), Rational(-2, 6), Rational(0), Rational(0)) +
ListPolynomial(Rational(-6, 3), Rational(-7, 2), Rational(2, 3)),
"test 3"
)
// (-2/4 - 6/9 x - 4/9 x^2) + (2/4 + 6/9 x + 4/9 x^2) ?= 0
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0), Rational(0), Rational(0)),
ListPolynomial(Rational(-2, 4), Rational(-6, 9), Rational(-4, 9)) +
ListPolynomial(Rational(2, 4), Rational(6, 9), Rational(4, 9)),
"test 4"
@ -457,14 +457,14 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
)
// (-4/7 - 2/6 x + 0 x^2 + 0 x^3) - (-6/3 - 7/2 x + 2/3 x^2) ?= 10/7 + 19/6 x - 2/3 x^2
assertEquals(
ListPolynomial(Rational(10, 7), Rational(19, 6), Rational(-2, 3)),
ListPolynomial(Rational(10, 7), Rational(19, 6), Rational(-2, 3), Rational(0)),
ListPolynomial(Rational(-4, 7), Rational(-2, 6), Rational(0), Rational(0)) -
ListPolynomial(Rational(-6, 3), Rational(-7, 2), Rational(2, 3)),
"test 3"
)
// (-2/4 - 6/9 x - 4/9 x^2) - (-2/4 - 6/9 x - 4/9 x^2) ?= 0
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0), Rational(0), Rational(0)),
ListPolynomial(Rational(-2, 4), Rational(-6, 9), Rational(-4, 9)) -
ListPolynomial(Rational(-2, 4), Rational(-6, 9), Rational(-4, 9)),
"test 4"
@ -482,70 +482,10 @@ class ListPolynomialTest { // TODO: Adapt tests to changes
)
// Spoiler: 5 * 7 = 0
assertEquals(
ListPolynomial(),
ListPolynomial(0, 0, 0, 0, 0),
ListPolynomial(5, -25, 10) * ListPolynomial(21, 14, -7),
"test 2"
)
}
}
@Test
fun test_Polynomial_degree() {
RationalField.listPolynomial {
assertEquals(
2,
ListPolynomial(Rational(5, 9), Rational(-8, 9), Rational(-8, 7)).degree,
"test 1"
)
assertEquals(
-1,
ListPolynomial<Rational>().degree,
"test 2"
)
assertEquals(
-1,
ListPolynomial(Rational(0)).degree,
"test 3"
)
assertEquals(
-1,
ListPolynomial(Rational(0), Rational(0)).degree,
"test 4"
)
assertEquals(
-1,
ListPolynomial(Rational(0), Rational(0), Rational(0)).degree,
"test 5"
)
assertEquals(
0,
ListPolynomial(Rational(5, 9)).degree,
"test 6"
)
assertEquals(
0,
ListPolynomial(Rational(5, 9), Rational(0)).degree,
"test 7"
)
assertEquals(
0,
ListPolynomial(Rational(5, 9), Rational(0), Rational(0)).degree,
"test 8"
)
assertEquals(
2,
ListPolynomial(Rational(0), Rational(0), Rational(-8, 7)).degree,
"test 9"
)
assertEquals(
2,
ListPolynomial(Rational(5, 9), Rational(-8, 9), Rational(-8, 7), Rational(0), Rational(0)).degree,
"test 10"
)
assertEquals(
2,
ListPolynomial(Rational(0), Rational(0), Rational(-8, 7), Rational(0), Rational(0)).degree,
"test 11"
)
}
}
}

View File

@ -12,7 +12,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class ListPolynomialUtilTest { // TODO: Adapt tests to changes
class ListPolynomialUtilTest {
@Test
fun test_substitute_Double() {
assertEquals(
@ -81,7 +81,7 @@ class ListPolynomialUtilTest { // TODO: Adapt tests to changes
@Test
fun test_substitute_Polynomial() {
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(0)),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).substitute(RationalField, ListPolynomial(Rational(1))),
"test 1"
)
@ -98,7 +98,7 @@ class ListPolynomialUtilTest { // TODO: Adapt tests to changes
"test 3"
)
assertEquals(
ListPolynomial(Rational(677, 378), Rational(97, 180), Rational(1, 75)),
ListPolynomial(Rational(677, 378), Rational(97, 180), Rational(1, 75), Rational(0)),
ListPolynomial(Rational(1, 7), Rational(9, 4), Rational(1, 3), Rational(0))
.substitute(RationalField, ListPolynomial(Rational(6, 9), Rational(1, 5))),
"test 4"
@ -110,7 +110,7 @@ class ListPolynomialUtilTest { // TODO: Adapt tests to changes
"test 5"
)
assertEquals(
ListPolynomial(Rational(89, 54)),
ListPolynomial(Rational(89, 54), Rational(0), Rational(0), Rational(0)),
ListPolynomial(Rational(0), Rational(9, 4), Rational(1, 3), Rational(0))
.substitute(RationalField, ListPolynomial(Rational(6, 9), Rational(0))),
"test 6"
@ -134,7 +134,7 @@ class ListPolynomialUtilTest { // TODO: Adapt tests to changes
"test 3"
)
assertEquals(
ListPolynomial(Rational(-8, 3), Rational(8, 9), Rational(15, 7)),
ListPolynomial(Rational(-8, 3), Rational(8, 9), Rational(15, 7), Rational(0)),
ListPolynomial(Rational(1, 5), Rational(-8, 3), Rational(4, 9), Rational(5, 7), Rational(0)).derivative(RationalField),
"test 4"
)
@ -180,7 +180,7 @@ class ListPolynomialUtilTest { // TODO: Adapt tests to changes
"test 8"
)
assertEquals(
ListPolynomial(Rational(8, 9), Rational(30, 7)),
ListPolynomial(Rational(8, 9), Rational(30, 7), Rational(0)),
ListPolynomial(Rational(1, 5), Rational(-8, 3), Rational(4, 9), Rational(5, 7), Rational(0)).nthDerivative(RationalField, 2),
"test 9"
)
@ -203,7 +203,7 @@ class ListPolynomialUtilTest { // TODO: Adapt tests to changes
"test 3"
)
assertEquals(
ListPolynomial(Rational(0), Rational(1, 5), Rational(-4, 3), Rational(4, 27), Rational(5, 28)),
ListPolynomial(Rational(0), Rational(1, 5), Rational(-4, 3), Rational(4, 27), Rational(5, 28), Rational(0)),
ListPolynomial(Rational(1, 5), Rational(-8, 3), Rational(4, 9), Rational(5, 7), Rational(0)).antiderivative(RationalField),
"test 4"
)
@ -249,7 +249,7 @@ class ListPolynomialUtilTest { // TODO: Adapt tests to changes
"test 8"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(1, 10), Rational(-4, 9), Rational(1, 27), Rational(1, 28)),
ListPolynomial(Rational(0), Rational(0), Rational(1, 10), Rational(-4, 9), Rational(1, 27), Rational(1, 28), Rational(0)),
ListPolynomial(Rational(1, 5), Rational(-8, 3), Rational(4, 9), Rational(5, 7), Rational(0)).nthAntiderivative(RationalField, 2),
"test 9"
)

View File

@ -120,7 +120,7 @@ class IntModuloRing : Ring<IntModulo> {
}
override inline val zero: IntModulo get() = IntModulo(0, modulus, toCheckInput = false)
override inline val one: IntModulo get() = IntModulo(0, modulus, toCheckInput = false)
override inline val one: IntModulo get() = IntModulo(1, modulus, toCheckInput = false)
fun number(arg: Int) = IntModulo(arg, modulus, toCheckInput = false)