Feature: Polynomials and rational functions #469

Merged
lounres merged 132 commits from feature/polynomials into dev 2022-07-28 18:04:06 +03:00
20 changed files with 2229 additions and 178 deletions
Showing only changes of commit 58d7015782 - Show all commits

View File

@ -12,11 +12,6 @@ kotlin.sourceSets {
api(project(":kmath-core")) api(project(":kmath-core"))
} }
} }
commonTest {
dependencies {
api(projects.testUtilsFunctions)
}
}
} }
dependencies { dependencies {

View File

@ -11,9 +11,9 @@ import space.kscience.kmath.operations.Ring
import space.kscience.kmath.operations.ScaleOperations import space.kscience.kmath.operations.ScaleOperations
public class IntModulo { class IntModulo {
public val residue: Int val residue: Int
public val modulus: Int val modulus: Int
@PublishedApi @PublishedApi
internal constructor(residue: Int, modulus: Int, toCheckInput: Boolean = true) { internal constructor(residue: Int, modulus: Int, toCheckInput: Boolean = true) {
@ -27,16 +27,16 @@ public class IntModulo {
} }
} }
public constructor(residue: Int, modulus: Int) : this(residue, modulus, true) constructor(residue: Int, modulus: Int) : this(residue, modulus, true)
public operator fun unaryPlus(): IntModulo = this operator fun unaryPlus(): IntModulo = this
public operator fun unaryMinus(): IntModulo = operator fun unaryMinus(): IntModulo =
IntModulo( IntModulo(
if (residue == 0) 0 else modulus - residue, if (residue == 0) 0 else modulus - residue,
modulus, modulus,
toCheckInput = false toCheckInput = false
) )
public operator fun plus(other: IntModulo): IntModulo { operator fun plus(other: IntModulo): IntModulo {
require(modulus == other.modulus) { "can not add two residue different modulo" } require(modulus == other.modulus) { "can not add two residue different modulo" }
return IntModulo( return IntModulo(
(residue + other.residue) % modulus, (residue + other.residue) % modulus,
@ -44,13 +44,13 @@ public class IntModulo {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun plus(other: Int): IntModulo = operator fun plus(other: Int): IntModulo =
IntModulo( IntModulo(
(residue + other) % modulus, (residue + other) % modulus,
modulus, modulus,
toCheckInput = false toCheckInput = false
) )
public operator fun minus(other: IntModulo): IntModulo { operator fun minus(other: IntModulo): IntModulo {
require(modulus == other.modulus) { "can not subtract two residue different modulo" } require(modulus == other.modulus) { "can not subtract two residue different modulo" }
return IntModulo( return IntModulo(
(residue - other.residue) % modulus, (residue - other.residue) % modulus,
@ -58,13 +58,13 @@ public class IntModulo {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun minus(other: Int): IntModulo = operator fun minus(other: Int): IntModulo =
IntModulo( IntModulo(
(residue - other) % modulus, (residue - other) % modulus,
modulus, modulus,
toCheckInput = false toCheckInput = false
) )
public operator fun times(other: IntModulo): IntModulo { operator fun times(other: IntModulo): IntModulo {
require(modulus == other.modulus) { "can not multiply two residue different modulo" } require(modulus == other.modulus) { "can not multiply two residue different modulo" }
return IntModulo( return IntModulo(
(residue * other.residue) % modulus, (residue * other.residue) % modulus,
@ -72,13 +72,13 @@ public class IntModulo {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun times(other: Int): IntModulo = operator fun times(other: Int): IntModulo =
IntModulo( IntModulo(
(residue * other) % modulus, (residue * other) % modulus,
modulus, modulus,
toCheckInput = false toCheckInput = false
) )
public operator fun div(other: IntModulo): IntModulo { operator fun div(other: IntModulo): IntModulo {
require(modulus == other.modulus) { "can not divide two residue different modulo" } require(modulus == other.modulus) { "can not divide two residue different modulo" }
val (reciprocalCandidate, gcdOfOtherResidueAndModulus) = bezoutIdentityWithGCD(other.residue, modulus) val (reciprocalCandidate, gcdOfOtherResidueAndModulus) = bezoutIdentityWithGCD(other.residue, modulus)
require(gcdOfOtherResidueAndModulus == 1) { "can not divide to residue that has non-trivial GCD with modulo" } require(gcdOfOtherResidueAndModulus == 1) { "can not divide to residue that has non-trivial GCD with modulo" }
@ -88,7 +88,7 @@ public class IntModulo {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun div(other: Int): IntModulo { operator fun div(other: Int): IntModulo {
val (reciprocalCandidate, gcdOfOtherResidueAndModulus) = bezoutIdentityWithGCD(other, modulus) val (reciprocalCandidate, gcdOfOtherResidueAndModulus) = bezoutIdentityWithGCD(other, modulus)
require(gcdOfOtherResidueAndModulus == 1) { "can not divide to residue that has non-trivial GCD with modulo" } require(gcdOfOtherResidueAndModulus == 1) { "can not divide to residue that has non-trivial GCD with modulo" }
return IntModulo( return IntModulo(
@ -109,11 +109,11 @@ public class IntModulo {
} }
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE")
public class IntModuloRing : Ring<IntModulo>, ScaleOperations<IntModulo> { class IntModuloRing : Ring<IntModulo>, ScaleOperations<IntModulo> {
public val modulus: Int val modulus: Int
public constructor(modulus: Int) { constructor(modulus: Int) {
require(modulus != 0) { "modulus can not be zero" } require(modulus != 0) { "modulus can not be zero" }
this.modulus = if (modulus < 0) -modulus else modulus this.modulus = if (modulus < 0) -modulus else modulus
} }
@ -121,7 +121,7 @@ public class IntModuloRing : Ring<IntModulo>, ScaleOperations<IntModulo> {
override inline val zero: IntModulo get() = IntModulo(0, modulus, toCheckInput = false) override inline val zero: IntModulo get() = IntModulo(0, modulus, toCheckInput = false)
override inline val one: IntModulo get() = IntModulo(1, modulus, toCheckInput = false) override inline val one: IntModulo get() = IntModulo(1, modulus, toCheckInput = false)
public fun number(arg: Int): IntModulo = IntModulo(arg, modulus, toCheckInput = false) fun number(arg: Int): IntModulo = IntModulo(arg, modulus, toCheckInput = false)
override inline fun add(left: IntModulo, right: IntModulo): IntModulo = left + right override inline fun add(left: IntModulo, right: IntModulo): IntModulo = left + right
override inline fun multiply(left: IntModulo, right: IntModulo): IntModulo = left * right override inline fun multiply(left: IntModulo, right: IntModulo): IntModulo = left * right
@ -130,7 +130,7 @@ public class IntModuloRing : Ring<IntModulo>, ScaleOperations<IntModulo> {
override inline fun IntModulo.plus(arg: IntModulo): IntModulo = this + arg override inline fun IntModulo.plus(arg: IntModulo): IntModulo = this + arg
override inline fun IntModulo.minus(arg: IntModulo): IntModulo = this - arg override inline fun IntModulo.minus(arg: IntModulo): IntModulo = this - arg
override inline fun IntModulo.times(arg: IntModulo): IntModulo = this * arg override inline fun IntModulo.times(arg: IntModulo): IntModulo = this * arg
public inline fun IntModulo.div(arg: IntModulo): IntModulo = this / arg inline fun IntModulo.div(arg: IntModulo): IntModulo = this / arg
override fun scale(a: IntModulo, value: Double): IntModulo = a * value.toInt() override fun scale(a: IntModulo, value: Double): IntModulo = a * value.toInt()
} }

View File

@ -9,10 +9,10 @@ import space.kscience.kmath.functions.Polynomial
import space.kscience.kmath.functions.PolynomialSpace import space.kscience.kmath.functions.PolynomialSpace
public fun PolynomialSpace<IntModulo, IntModuloRing>.Polynomial(vararg coefs: Int): Polynomial<IntModulo> = fun PolynomialSpace<IntModulo, IntModuloRing>.Polynomial(vararg coefs: Int): Polynomial<IntModulo> =
Polynomial(coefs.map { IntModulo(it, ring.modulus) }) Polynomial(coefs.map { IntModulo(it, ring.modulus) })
public fun IntModuloRing.Polynomial(vararg coefs: Int): Polynomial<IntModulo> = fun IntModuloRing.Polynomial(vararg coefs: Int): Polynomial<IntModulo> =
Polynomial(coefs.map { IntModulo(it, modulus) }) Polynomial(coefs.map { IntModulo(it, modulus) })
public fun IntModuloRing.m(arg: Int): IntModulo = IntModulo(arg, modulus) fun IntModuloRing.m(arg: Int): IntModulo = IntModulo(arg, modulus)
public fun PolynomialSpace<IntModulo, IntModuloRing>.m(arg: Int): IntModulo = IntModulo(arg, ring.modulus) fun PolynomialSpace<IntModulo, IntModuloRing>.m(arg: Int): IntModulo = IntModulo(arg, ring.modulus)

View File

@ -12,14 +12,14 @@ import space.kscience.kmath.operations.Field
import space.kscience.kmath.operations.NumbersAddOps import space.kscience.kmath.operations.NumbersAddOps
@Suppress("NAME_SHADOWING") @Suppress("NAME_SHADOWING")
public class Rational { class Rational {
public companion object { companion object {
public val ZERO: Rational = Rational(0L) val ZERO: Rational = Rational(0L)
public val ONE: Rational = Rational(1L) val ONE: Rational = Rational(1L)
} }
public val numerator: Long val numerator: Long
public val denominator: Long val denominator: Long
internal constructor(numerator: Long, denominator: Long, toCheckInput: Boolean = true) { internal constructor(numerator: Long, denominator: Long, toCheckInput: Boolean = true) {
if (toCheckInput) { if (toCheckInput) {
@ -35,16 +35,16 @@ public class Rational {
} }
} }
public constructor(numerator: Int, denominator: Int) : this(numerator.toLong(), denominator.toLong(), true) constructor(numerator: Int, denominator: Int) : this(numerator.toLong(), denominator.toLong(), true)
public constructor(numerator: Int, denominator: Long) : this(numerator.toLong(), denominator, true) constructor(numerator: Int, denominator: Long) : this(numerator.toLong(), denominator, true)
public constructor(numerator: Long, denominator: Int) : this(numerator, denominator.toLong(), true) constructor(numerator: Long, denominator: Int) : this(numerator, denominator.toLong(), true)
public constructor(numerator: Long, denominator: Long) : this(numerator, denominator, true) constructor(numerator: Long, denominator: Long) : this(numerator, denominator, true)
public constructor(numerator: Int) : this(numerator.toLong(), 1L, false) constructor(numerator: Int) : this(numerator.toLong(), 1L, false)
public constructor(numerator: Long) : this(numerator, 1L, false) constructor(numerator: Long) : this(numerator, 1L, false)
public operator fun unaryPlus(): Rational = this operator fun unaryPlus(): Rational = this
public operator fun unaryMinus(): Rational = Rational(-this.numerator, this.denominator) operator fun unaryMinus(): Rational = Rational(-this.numerator, this.denominator)
public operator fun plus(other: Rational): Rational { operator fun plus(other: Rational): Rational {
val denominatorsGcd = gcd(denominator, other.denominator) val denominatorsGcd = gcd(denominator, other.denominator)
val dividedThisDenominator = denominator / denominatorsGcd val dividedThisDenominator = denominator / denominatorsGcd
val dividedOtherDenominator = other.denominator / denominatorsGcd val dividedOtherDenominator = other.denominator / denominatorsGcd
@ -56,19 +56,19 @@ public class Rational {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun plus(other: Int): Rational = operator fun plus(other: Int): Rational =
Rational( Rational(
numerator + denominator * other.toLong(), numerator + denominator * other.toLong(),
denominator, denominator,
toCheckInput = false toCheckInput = false
) )
public operator fun plus(other: Long): Rational = operator fun plus(other: Long): Rational =
Rational( Rational(
numerator + denominator * other, numerator + denominator * other,
denominator, denominator,
toCheckInput = false toCheckInput = false
) )
public operator fun minus(other: Rational): Rational { operator fun minus(other: Rational): Rational {
val denominatorsGcd = gcd(denominator, other.denominator) val denominatorsGcd = gcd(denominator, other.denominator)
val dividedThisDenominator = denominator / denominatorsGcd val dividedThisDenominator = denominator / denominatorsGcd
val dividedOtherDenominator = other.denominator / denominatorsGcd val dividedOtherDenominator = other.denominator / denominatorsGcd
@ -80,19 +80,19 @@ public class Rational {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun minus(other: Int): Rational = operator fun minus(other: Int): Rational =
Rational( Rational(
numerator - denominator * other.toLong(), numerator - denominator * other.toLong(),
denominator, denominator,
toCheckInput = false toCheckInput = false
) )
public operator fun minus(other: Long): Rational = operator fun minus(other: Long): Rational =
Rational( Rational(
numerator - denominator * other, numerator - denominator * other,
denominator, denominator,
toCheckInput = false toCheckInput = false
) )
public operator fun times(other: Rational): Rational { operator fun times(other: Rational): Rational {
val thisDenominatorAndOtherNumeratorGcd = gcd(denominator, other.numerator) val thisDenominatorAndOtherNumeratorGcd = gcd(denominator, other.numerator)
val otherDenominatorAndThisNumeratorGcd = gcd(other.denominator, numerator) val otherDenominatorAndThisNumeratorGcd = gcd(other.denominator, numerator)
return Rational( return Rational(
@ -101,7 +101,7 @@ public class Rational {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun times(other: Int): Rational { operator fun times(other: Int): Rational {
val other = other.toLong() val other = other.toLong()
val denominatorAndOtherGcd = gcd(denominator, other) val denominatorAndOtherGcd = gcd(denominator, other)
return Rational( return Rational(
@ -110,7 +110,7 @@ public class Rational {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun times(other: Long): Rational { operator fun times(other: Long): Rational {
val denominatorAndOtherGcd = gcd(denominator, other) val denominatorAndOtherGcd = gcd(denominator, other)
return Rational( return Rational(
numerator * (other / denominatorAndOtherGcd), numerator * (other / denominatorAndOtherGcd),
@ -118,7 +118,7 @@ public class Rational {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun div(other: Rational): Rational { operator fun div(other: Rational): Rational {
val denominatorsGcd = gcd(denominator, other.denominator) val denominatorsGcd = gcd(denominator, other.denominator)
val numeratorsGcd = gcd(numerator, other.numerator) val numeratorsGcd = gcd(numerator, other.numerator)
return Rational( return Rational(
@ -126,7 +126,7 @@ public class Rational {
(denominator / denominatorsGcd) * (other.numerator / numeratorsGcd) (denominator / denominatorsGcd) * (other.numerator / numeratorsGcd)
) )
} }
public operator fun div(other: Int): Rational { operator fun div(other: Int): Rational {
val other = other.toLong() val other = other.toLong()
val numeratorAndOtherGcd = gcd(numerator, other) val numeratorAndOtherGcd = gcd(numerator, other)
return Rational( return Rational(
@ -135,7 +135,7 @@ public class Rational {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun div(other: Long): Rational { operator fun div(other: Long): Rational {
val numeratorAndOtherGcd = gcd(numerator, other) val numeratorAndOtherGcd = gcd(numerator, other)
return Rational( return Rational(
numerator / numeratorAndOtherGcd, numerator / numeratorAndOtherGcd,
@ -158,7 +158,7 @@ public class Rational {
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE")
@OptIn(UnstableKMathAPI::class) @OptIn(UnstableKMathAPI::class)
public object RationalField : Field<Rational>, NumbersAddOps<Rational> { object RationalField : Field<Rational>, NumbersAddOps<Rational> {
override inline val zero: Rational get() = Rational.ZERO override inline val zero: Rational get() = Rational.ZERO
override inline val one: Rational get() = Rational.ONE override inline val one: Rational get() = Rational.ONE

View File

@ -12,12 +12,6 @@ kotlin.sourceSets {
api(projects.kmathCore) api(projects.kmathCore)
} }
} }
commonTest {
dependencies {
api(projects.testUtilsPolynomial)
api(kotlin("test"))
}
}
} }
dependencies { dependencies {

View File

@ -0,0 +1,589 @@
/*
* 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.operations.invoke
import space.kscience.kmath.operations.Field
import kotlin.jvm.JvmInline
import kotlin.test.Test
import kotlin.test.assertEquals
@JvmInline
value class Expr(val expr: String)
object ExprRing : Field<Expr> {
override fun Expr.unaryMinus(): Expr = Expr("-${expr}")
override fun add(left: Expr, right: Expr): Expr = Expr("(${left.expr} + ${right.expr})")
override fun multiply(left: Expr, right: Expr): Expr = Expr("(${left.expr} * ${right.expr})")
override val zero: Expr = Expr("0")
override val one: Expr = Expr("1")
override fun divide(left: Expr, right: Expr): Expr = Expr("(${left.expr} / ${right.expr})")
override fun scale(a: Expr, value: Double): Expr = Expr("(${a.expr} / $value)")
}
class AlgebraicStubTest {
@Test
fun test_addMultipliedBySquaring_for_UInt() {
ExprRing {
assertEquals(
"57",
addMultipliedByDoubling(Expr("57"), Expr("179"), 0u).expr,
"tried addMultipliedBySquaring(57, 179, 0u)"
)
assertEquals(
"(57 + 179)",
addMultipliedByDoubling(Expr("57"), Expr("179"), 1u).expr,
"tried addMultipliedBySquaring(57, 179, 1u)"
)
assertEquals(
"(57 + (179 + 179))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 2u).expr,
"tried addMultipliedBySquaring(57, 179, 2u)"
)
assertEquals(
"((57 + 179) + (179 + 179))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 3u).expr,
"tried addMultipliedBySquaring(57, 179, 3u)"
)
assertEquals(
"(57 + ((179 + 179) + (179 + 179)))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 4u).expr,
"tried addMultipliedBySquaring(57, 179, 4u)"
)
assertEquals(
"((57 + 179) + ((179 + 179) + (179 + 179)))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 5u).expr,
"tried addMultipliedBySquaring(57, 179, 5u)"
)
assertEquals(
"((57 + (179 + 179)) + ((179 + 179) + (179 + 179)))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 6u).expr,
"tried addMultipliedBySquaring(57, 179, 6u)"
)
assertEquals(
"(((57 + 179) + (179 + 179)) + ((179 + 179) + (179 + 179)))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 7u).expr,
"tried addMultipliedBySquaring(57, 179, 7u)"
)
assertEquals(
"(57 + (((179 + 179) + (179 + 179)) + ((179 + 179) + (179 + 179))))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 8u).expr,
"tried addMultipliedBySquaring(57, 179, 8u)"
)
}
}
@Test
fun test_multiplyBySquaring_for_UInt() {
ExprRing {
assertEquals(
"0",
multiplyByDoubling(Expr("57"), 0u).expr,
"tried multiplyBySquaring(57, 0u)"
)
assertEquals(
"57",
multiplyByDoubling(Expr("57"), 1u).expr,
"tried multiplyBySquaring(57, 1u)"
)
assertEquals(
"(57 + 57)",
multiplyByDoubling(Expr("57"), 2u).expr,
"tried multiplyBySquaring(57, 2u)"
)
assertEquals(
"(57 + (57 + 57))",
multiplyByDoubling(Expr("57"), 3u).expr,
"tried multiplyBySquaring(57, 3u)"
)
assertEquals(
"((57 + 57) + (57 + 57))",
multiplyByDoubling(Expr("57"), 4u).expr,
"tried multiplyBySquaring(57, 4u)"
)
assertEquals(
"(57 + ((57 + 57) + (57 + 57)))",
multiplyByDoubling(Expr("57"), 5u).expr,
"tried multiplyBySquaring(57, 5u)"
)
assertEquals(
"((57 + 57) + ((57 + 57) + (57 + 57)))",
multiplyByDoubling(Expr("57"), 6u).expr,
"tried multiplyBySquaring(57, 6u)"
)
assertEquals(
"((57 + (57 + 57)) + ((57 + 57) + (57 + 57)))",
multiplyByDoubling(Expr("57"), 7u).expr,
"tried multiplyBySquaring(57, 7u)"
)
assertEquals(
"(((57 + 57) + (57 + 57)) + ((57 + 57) + (57 + 57)))",
multiplyByDoubling(Expr("57"), 8u).expr,
"tried multiplyBySquaring(57, 8u)"
)
}
}
@Test
fun test_addMultipliedBySquaring_for_Int() {
ExprRing {
assertEquals(
"57",
addMultipliedByDoubling(Expr("57"), Expr("179"), 0).expr,
"tried addMultipliedBySquaring(57, 179, 0)"
)
assertEquals(
"(57 + 179)",
addMultipliedByDoubling(Expr("57"), Expr("179"), 1).expr,
"tried addMultipliedBySquaring(57, 179, 1)"
)
assertEquals(
"(57 + -179)",
addMultipliedByDoubling(Expr("57"), Expr("179"), -1).expr,
"tried addMultipliedBySquaring(57, 179, -1)"
)
assertEquals(
"(57 + (179 + 179))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 2).expr,
"tried addMultipliedBySquaring(57, 179, 2)"
)
assertEquals(
"(57 + (-179 + -179))",
addMultipliedByDoubling(Expr("57"), Expr("179"), -2).expr,
"tried addMultipliedBySquaring(57, 179, -2)"
)
assertEquals(
"((57 + 179) + (179 + 179))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 3).expr,
"tried addMultipliedBySquaring(57, 179, 3)"
)
assertEquals(
"((57 + -179) + (-179 + -179))",
addMultipliedByDoubling(Expr("57"), Expr("179"), -3).expr,
"tried addMultipliedBySquaring(57, 179, -3)"
)
assertEquals(
"(57 + ((179 + 179) + (179 + 179)))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 4).expr,
"tried addMultipliedBySquaring(57, 179, 4)"
)
assertEquals(
"(57 + ((-179 + -179) + (-179 + -179)))",
addMultipliedByDoubling(Expr("57"), Expr("179"), -4).expr,
"tried addMultipliedBySquaring(57, 179, -4)"
)
assertEquals(
"((57 + 179) + ((179 + 179) + (179 + 179)))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 5).expr,
"tried addMultipliedBySquaring(57, 179, 5)"
)
assertEquals(
"((57 + -179) + ((-179 + -179) + (-179 + -179)))",
addMultipliedByDoubling(Expr("57"), Expr("179"), -5).expr,
"tried addMultipliedBySquaring(57, 179, -5)"
)
assertEquals(
"((57 + (179 + 179)) + ((179 + 179) + (179 + 179)))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 6).expr,
"tried addMultipliedBySquaring(57, 179, 6)"
)
assertEquals(
"((57 + (-179 + -179)) + ((-179 + -179) + (-179 + -179)))",
addMultipliedByDoubling(Expr("57"), Expr("179"), -6).expr,
"tried addMultipliedBySquaring(57, 179, -6)"
)
assertEquals(
"(((57 + 179) + (179 + 179)) + ((179 + 179) + (179 + 179)))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 7).expr,
"tried addMultipliedBySquaring(57, 179, 7)"
)
assertEquals(
"(((57 + -179) + (-179 + -179)) + ((-179 + -179) + (-179 + -179)))",
addMultipliedByDoubling(Expr("57"), Expr("179"), -7).expr,
"tried addMultipliedBySquaring(57, 179, -7)"
)
assertEquals(
"(57 + (((179 + 179) + (179 + 179)) + ((179 + 179) + (179 + 179))))",
addMultipliedByDoubling(Expr("57"), Expr("179"), 8).expr,
"tried addMultipliedBySquaring(57, 179, 8)"
)
assertEquals(
"(57 + (((-179 + -179) + (-179 + -179)) + ((-179 + -179) + (-179 + -179))))",
addMultipliedByDoubling(Expr("57"), Expr("179"), -8).expr,
"tried addMultipliedBySquaring(57, 179, -8)"
)
}
}
@Test
fun test_multiplyBySquaring_for_Int() {
ExprRing {
assertEquals(
"0",
multiplyByDoubling(Expr("57"), 0).expr,
"tried multiplyBySquaring(57, 0)"
)
assertEquals(
"57",
multiplyByDoubling(Expr("57"), 1).expr,
"tried multiplyBySquaring(57, 1)"
)
assertEquals(
"-57",
multiplyByDoubling(Expr("57"), -1).expr,
"tried multiplyBySquaring(57, -1)"
)
assertEquals(
"(57 + 57)",
multiplyByDoubling(Expr("57"), 2).expr,
"tried multiplyBySquaring(57, 2)"
)
assertEquals(
"(-57 + -57)",
multiplyByDoubling(Expr("57"), -2).expr,
"tried multiplyBySquaring(57, -2)"
)
assertEquals(
"(57 + (57 + 57))",
multiplyByDoubling(Expr("57"), 3).expr,
"tried multiplyBySquaring(57, 3)"
)
assertEquals(
"(-57 + (-57 + -57))",
multiplyByDoubling(Expr("57"), -3).expr,
"tried multiplyBySquaring(57, -3)"
)
assertEquals(
"((57 + 57) + (57 + 57))",
multiplyByDoubling(Expr("57"), 4).expr,
"tried multiplyBySquaring(57, 4)"
)
assertEquals(
"((-57 + -57) + (-57 + -57))",
multiplyByDoubling(Expr("57"), -4).expr,
"tried multiplyBySquaring(57, -4)"
)
assertEquals(
"(57 + ((57 + 57) + (57 + 57)))",
multiplyByDoubling(Expr("57"), 5).expr,
"tried multiplyBySquaring(57, 5)"
)
assertEquals(
"(-57 + ((-57 + -57) + (-57 + -57)))",
multiplyByDoubling(Expr("57"), -5).expr,
"tried multiplyBySquaring(57, -5)"
)
assertEquals(
"((57 + 57) + ((57 + 57) + (57 + 57)))",
multiplyByDoubling(Expr("57"), 6).expr,
"tried multiplyBySquaring(57, 6)"
)
assertEquals(
"((-57 + -57) + ((-57 + -57) + (-57 + -57)))",
multiplyByDoubling(Expr("57"), -6).expr,
"tried multiplyBySquaring(57, -6)"
)
assertEquals(
"((57 + (57 + 57)) + ((57 + 57) + (57 + 57)))",
multiplyByDoubling(Expr("57"), 7).expr,
"tried multiplyBySquaring(57, 7)"
)
assertEquals(
"((-57 + (-57 + -57)) + ((-57 + -57) + (-57 + -57)))",
multiplyByDoubling(Expr("57"), -7).expr,
"tried multiplyBySquaring(57, -7)"
)
assertEquals(
"(((57 + 57) + (57 + 57)) + ((57 + 57) + (57 + 57)))",
multiplyByDoubling(Expr("57"), 8).expr,
"tried multiplyBySquaring(57, 8)"
)
assertEquals(
"(((-57 + -57) + (-57 + -57)) + ((-57 + -57) + (-57 + -57)))",
multiplyByDoubling(Expr("57"), -8).expr,
"tried multiplyBySquaring(57, -8)"
)
}
}
@Test
fun test_multiplyExponentiationBySquaring_for_UInt() {
ExprRing {
assertEquals(
"57",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 0u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 0u)"
)
assertEquals(
"(57 * 179)",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 1u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 1u)"
)
assertEquals(
"(57 * (179 * 179))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 2u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 2u)"
)
assertEquals(
"((57 * 179) * (179 * 179))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 3u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 3u)"
)
assertEquals(
"(57 * ((179 * 179) * (179 * 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 4u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 4u)"
)
assertEquals(
"((57 * 179) * ((179 * 179) * (179 * 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 5u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 5u)"
)
assertEquals(
"((57 * (179 * 179)) * ((179 * 179) * (179 * 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 6u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 6u)"
)
assertEquals(
"(((57 * 179) * (179 * 179)) * ((179 * 179) * (179 * 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 7u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 7u)"
)
assertEquals(
"(57 * (((179 * 179) * (179 * 179)) * ((179 * 179) * (179 * 179))))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 8u).expr,
"tried multiplyExponentiationBySquaring(57, 179, 8u)"
)
}
}
@Test
fun test_exponentiationBySquaring_for_UInt() {
ExprRing {
assertEquals(
"0",
exponentiateBySquaring(Expr("57"), 0u).expr,
"tried exponentiationBySquaring(57, 0u)"
)
assertEquals(
"57",
exponentiateBySquaring(Expr("57"), 1u).expr,
"tried exponentiationBySquaring(57, 1u)"
)
assertEquals(
"(57 * 57)",
exponentiateBySquaring(Expr("57"), 2u).expr,
"tried exponentiationBySquaring(57, 2u)"
)
assertEquals(
"(57 * (57 * 57))",
exponentiateBySquaring(Expr("57"), 3u).expr,
"tried exponentiationBySquaring(57, 3u)"
)
assertEquals(
"((57 * 57) * (57 * 57))",
exponentiateBySquaring(Expr("57"), 4u).expr,
"tried exponentiationBySquaring(57, 4u)"
)
assertEquals(
"(57 * ((57 * 57) * (57 * 57)))",
exponentiateBySquaring(Expr("57"), 5u).expr,
"tried exponentiationBySquaring(57, 5u)"
)
assertEquals(
"((57 * 57) * ((57 * 57) * (57 * 57)))",
exponentiateBySquaring(Expr("57"), 6u).expr,
"tried exponentiationBySquaring(57, 6u)"
)
assertEquals(
"((57 * (57 * 57)) * ((57 * 57) * (57 * 57)))",
exponentiateBySquaring(Expr("57"), 7u).expr,
"tried exponentiationBySquaring(57, 7u)"
)
assertEquals(
"(((57 * 57) * (57 * 57)) * ((57 * 57) * (57 * 57)))",
exponentiateBySquaring(Expr("57"), 8u).expr,
"tried exponentiationBySquaring(57, 8u)"
)
}
}
@Test
fun test_multiplyExponentiationBySquaring_for_Int() {
ExprRing {
assertEquals(
"57",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 0).expr,
"tried multiplyExponentiationBySquaring(57, 179, 0)"
)
assertEquals(
"(57 * 179)",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 1).expr,
"tried multiplyExponentiationBySquaring(57, 179, 1)"
)
assertEquals(
"(57 * (1 / 179))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), -1).expr,
"tried multiplyExponentiationBySquaring(57, 179, -1)"
)
assertEquals(
"(57 * (179 * 179))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 2).expr,
"tried multiplyExponentiationBySquaring(57, 179, 2)"
)
assertEquals(
"(57 * ((1 / 179) * (1 / 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), -2).expr,
"tried multiplyExponentiationBySquaring(57, 179, -2)"
)
assertEquals(
"((57 * 179) * (179 * 179))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 3).expr,
"tried multiplyExponentiationBySquaring(57, 179, 3)"
)
assertEquals(
"((57 * (1 / 179)) * ((1 / 179) * (1 / 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), -3).expr,
"tried multiplyExponentiationBySquaring(57, 179, -3)"
)
assertEquals(
"(57 * ((179 * 179) * (179 * 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 4).expr,
"tried multiplyExponentiationBySquaring(57, 179, 4)"
)
assertEquals(
"(57 * (((1 / 179) * (1 / 179)) * ((1 / 179) * (1 / 179))))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), -4).expr,
"tried multiplyExponentiationBySquaring(57, 179, -4)"
)
assertEquals(
"((57 * 179) * ((179 * 179) * (179 * 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 5).expr,
"tried multiplyExponentiationBySquaring(57, 179, 5)"
)
assertEquals(
"((57 * (1 / 179)) * (((1 / 179) * (1 / 179)) * ((1 / 179) * (1 / 179))))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), -5).expr,
"tried multiplyExponentiationBySquaring(57, 179, -5)"
)
assertEquals(
"((57 * (179 * 179)) * ((179 * 179) * (179 * 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 6).expr,
"tried multiplyExponentiationBySquaring(57, 179, 6)"
)
assertEquals(
"((57 * ((1 / 179) * (1 / 179))) * (((1 / 179) * (1 / 179)) * ((1 / 179) * (1 / 179))))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), -6).expr,
"tried multiplyExponentiationBySquaring(57, 179, -6)"
)
assertEquals(
"(((57 * 179) * (179 * 179)) * ((179 * 179) * (179 * 179)))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 7).expr,
"tried multiplyExponentiationBySquaring(57, 179, 7)"
)
assertEquals(
"(((57 * (1 / 179)) * ((1 / 179) * (1 / 179))) * (((1 / 179) * (1 / 179)) * ((1 / 179) * (1 / 179))))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), -7).expr,
"tried multiplyExponentiationBySquaring(57, 179, -7)"
)
assertEquals(
"(57 * (((179 * 179) * (179 * 179)) * ((179 * 179) * (179 * 179))))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), 8).expr,
"tried multiplyExponentiationBySquaring(57, 179, 8)"
)
assertEquals(
"(57 * ((((1 / 179) * (1 / 179)) * ((1 / 179) * (1 / 179))) * (((1 / 179) * (1 / 179)) * ((1 / 179) * (1 / 179)))))",
multiplyExponentiatedBySquaring(Expr("57"), Expr("179"), -8).expr,
"tried multiplyExponentiationBySquaring(57, 179, -8)"
)
}
}
@Test
fun test_exponentiationBySquaring_for_Int() {
ExprRing {
assertEquals(
"0",
exponentiateBySquaring(Expr("57"), 0).expr,
"tried exponentiationBySquaring(57, 0)"
)
assertEquals(
"57",
exponentiateBySquaring(Expr("57"), 1).expr,
"tried exponentiationBySquaring(57, 1)"
)
assertEquals(
"(1 / 57)",
exponentiateBySquaring(Expr("57"), -1).expr,
"tried exponentiationBySquaring(57, -1)"
)
assertEquals(
"(57 * 57)",
exponentiateBySquaring(Expr("57"), 2).expr,
"tried exponentiationBySquaring(57, 2)"
)
assertEquals(
"((1 / 57) * (1 / 57))",
exponentiateBySquaring(Expr("57"), -2).expr,
"tried exponentiationBySquaring(57, -2)"
)
assertEquals(
"(57 * (57 * 57))",
exponentiateBySquaring(Expr("57"), 3).expr,
"tried exponentiationBySquaring(57, 3)"
)
assertEquals(
"((1 / 57) * ((1 / 57) * (1 / 57)))",
exponentiateBySquaring(Expr("57"), -3).expr,
"tried exponentiationBySquaring(57, -3)"
)
assertEquals(
"((57 * 57) * (57 * 57))",
exponentiateBySquaring(Expr("57"), 4).expr,
"tried exponentiationBySquaring(57, 4)"
)
assertEquals(
"(((1 / 57) * (1 / 57)) * ((1 / 57) * (1 / 57)))",
exponentiateBySquaring(Expr("57"), -4).expr,
"tried exponentiationBySquaring(57, -4)"
)
assertEquals(
"(57 * ((57 * 57) * (57 * 57)))",
exponentiateBySquaring(Expr("57"), 5).expr,
"tried exponentiationBySquaring(57, 5)"
)
assertEquals(
"((1 / 57) * (((1 / 57) * (1 / 57)) * ((1 / 57) * (1 / 57))))",
exponentiateBySquaring(Expr("57"), -5).expr,
"tried exponentiationBySquaring(57, -5)"
)
assertEquals(
"((57 * 57) * ((57 * 57) * (57 * 57)))",
exponentiateBySquaring(Expr("57"), 6).expr,
"tried exponentiationBySquaring(57, 6)"
)
assertEquals(
"(((1 / 57) * (1 / 57)) * (((1 / 57) * (1 / 57)) * ((1 / 57) * (1 / 57))))",
exponentiateBySquaring(Expr("57"), -6).expr,
"tried exponentiationBySquaring(57, -6)"
)
assertEquals(
"((57 * (57 * 57)) * ((57 * 57) * (57 * 57)))",
exponentiateBySquaring(Expr("57"), 7).expr,
"tried exponentiationBySquaring(57, 7)"
)
assertEquals(
"(((1 / 57) * ((1 / 57) * (1 / 57))) * (((1 / 57) * (1 / 57)) * ((1 / 57) * (1 / 57))))",
exponentiateBySquaring(Expr("57"), -7).expr,
"tried exponentiationBySquaring(57, -7)"
)
assertEquals(
"(((57 * 57) * (57 * 57)) * ((57 * 57) * (57 * 57)))",
exponentiateBySquaring(Expr("57"), 8).expr,
"tried exponentiationBySquaring(57, 8)"
)
assertEquals(
"((((1 / 57) * (1 / 57)) * ((1 / 57) * (1 / 57))) * (((1 / 57) * (1 / 57)) * ((1 / 57) * (1 / 57))))",
exponentiateBySquaring(Expr("57"), -8).expr,
"tried exponentiationBySquaring(57, -8)"
)
}
}
}

View File

@ -0,0 +1,544 @@
/*
* 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.
*/
@file:Suppress("LocalVariableName")
package space.kscience.kmath.functions
import space.kscience.kmath.functions.testUtils.IntModuloRing
import space.kscience.kmath.functions.testUtils.ListPolynomial
import space.kscience.kmath.functions.testUtils.Rational
import space.kscience.kmath.functions.testUtils.RationalField
import kotlin.test.*
class ListPolynomialTest {
@Test
fun test_Polynomial_Int_plus() {
RationalField.listPolynomialSpace {
assertEquals(
ListPolynomial(Rational(-22, 9), Rational(-8, 9), Rational(-8, 7)),
ListPolynomial(Rational(5, 9), Rational(-8, 9), Rational(-8, 7)) + -3,
"test 1"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(0), Rational(0)),
ListPolynomial(Rational(-2), Rational(0), Rational(0), Rational(0)) + 2,
"test 2"
)
assertEquals(
ListPolynomial(Rational(0)),
ListPolynomial(Rational(-2)) + 2,
"test 3"
)
val polynomial_4 = ListPolynomial<Rational>()
assertSame(
polynomial_4,
polynomial_4 + 0,
"test 4"
)
val polynomial_5 = ListPolynomial(Rational(-22, 9), Rational(-8, 9), Rational(-8, 7))
assertSame(
polynomial_5,
polynomial_5 + 0,
"test 5"
)
assertEquals(
ListPolynomial(Rational(-1), Rational(0), Rational(0), Rational(0)),
ListPolynomial(Rational(-2), Rational(0), Rational(0), Rational(0)) + 1,
"test 6"
)
assertEquals(
ListPolynomial(Rational(-1)),
ListPolynomial(Rational(-2)) + 1,
"test 7"
)
assertEquals(
ListPolynomial(Rational(2)),
ListPolynomial<Rational>() + 2,
"test 8"
)
}
}
@Test
fun test_Polynomial_Int_minus() {
RationalField.listPolynomialSpace {
assertEquals(
ListPolynomial(Rational(32, 9), Rational(-8, 9), Rational(-8, 7)),
ListPolynomial(Rational(5, 9), Rational(-8, 9), Rational(-8, 7)) - -3,
"test 1"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(0), Rational(0)),
ListPolynomial(Rational(2), Rational(0), Rational(0), Rational(0)) - 2,
"test 2"
)
assertEquals(
ListPolynomial(Rational(0)),
ListPolynomial(Rational(2)) - 2,
"test 3"
)
val polynomial_4 = ListPolynomial<Rational>()
assertSame(
polynomial_4,
polynomial_4 - 0,
"test 4"
)
val polynomial_5 = ListPolynomial(Rational(-22, 9), Rational(-8, 9), Rational(-8, 7))
assertEquals(
polynomial_5,
polynomial_5 - 0,
"test 5"
)
assertEquals(
ListPolynomial(Rational(1), Rational(0), Rational(0), Rational(0)),
ListPolynomial(Rational(2), Rational(0), Rational(0), Rational(0)) - 1,
"test 6"
)
assertEquals(
ListPolynomial(Rational(1)),
ListPolynomial(Rational(2)) - 1,
"test 7"
)
assertEquals(
ListPolynomial(Rational(-2)),
ListPolynomial<Rational>() - 2,
"test 8"
)
}
}
@Test
fun test_Polynomial_Int_times() {
IntModuloRing(35).listPolynomialSpace {
assertEquals(
ListPolynomial(34, 2, 1, 20, 2),
ListPolynomial(22, 26, 13, 15, 26) * 27,
"test 1"
)
assertEquals(
ListPolynomial(0, 0, 0, 0, 0),
ListPolynomial(7, 0, 49, 21, 14) * 15,
"test 2"
)
val polynomial = ListPolynomial(22, 26, 13, 15, 26)
assertSame(
zero,
polynomial * 0,
"test 3"
)
assertSame(
polynomial,
polynomial * 1,
"test 4"
)
}
}
@Test
fun test_Int_Polynomial_plus() {
RationalField.listPolynomialSpace {
assertEquals(
ListPolynomial(Rational(-22, 9), Rational(-8, 9), Rational(-8, 7)),
-3 + ListPolynomial(Rational(5, 9), Rational(-8, 9), Rational(-8, 7)),
"test 1"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(0), Rational(0)),
2 + ListPolynomial(Rational(-2), Rational(0), Rational(0), Rational(0)),
"test 2"
)
assertEquals(
ListPolynomial(Rational(0)),
2 + ListPolynomial(Rational(-2)),
"test 3"
)
val polynomial_4 = ListPolynomial<Rational>()
assertSame(
polynomial_4,
0 + polynomial_4,
"test 4"
)
val polynomial_5 = ListPolynomial<Rational>(Rational(-22, 9), Rational(-8, 9), Rational(-8, 7))
assertSame(
polynomial_5,
0 + polynomial_5,
"test 5"
)
assertEquals(
ListPolynomial(Rational(-1), Rational(0), Rational(0), Rational(0)),
1 + ListPolynomial(Rational(-2), Rational(0), Rational(0), Rational(0)),
"test 6"
)
assertEquals(
ListPolynomial(Rational(-1)),
1 + ListPolynomial(Rational(-2)),
"test 7"
)
assertEquals(
ListPolynomial(Rational(2)),
2 + ListPolynomial(),
"test 8"
)
}
}
@Test
fun test_Int_Polynomial_minus() {
RationalField.listPolynomialSpace {
assertEquals(
ListPolynomial(Rational(32, 9), Rational(-8, 9), Rational(-8, 7)),
3 - ListPolynomial(Rational(-5, 9), Rational(8, 9), Rational(8, 7)),
"test 1"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(0), Rational(0)),
-2 - ListPolynomial(Rational(-2), Rational(0), Rational(0), Rational(0)),
"test 2"
)
assertEquals(
ListPolynomial(Rational(0)),
-2 - ListPolynomial(Rational(-2)),
"test 3"
)
assertEquals(
ListPolynomial(Rational(-32, 9), Rational(-8, -9), Rational(8, 7)),
0 - ListPolynomial(Rational(32, 9), Rational(-8, 9), Rational(-8, 7)),
"test 4"
)
assertEquals(
ListPolynomial(),
0 - ListPolynomial(),
"test 5"
)
assertEquals(
ListPolynomial(Rational(1), Rational(0), Rational(0), Rational(0)),
-1 - ListPolynomial(Rational(-2), Rational(0), Rational(0), Rational(0)),
"test 6"
)
assertEquals(
ListPolynomial(Rational(1)),
-1 - ListPolynomial(Rational(-2)),
"test 7"
)
assertEquals(
ListPolynomial(Rational(-2)),
-2 - ListPolynomial(),
"test 8"
)
}
}
@Test
fun test_Int_Polynomial_times() {
IntModuloRing(35).listPolynomialSpace {
assertEquals(
ListPolynomial(34, 2, 1, 20, 2),
27 * ListPolynomial(22, 26, 13, 15, 26),
"test 1"
)
assertEquals(
ListPolynomial(0, 0, 0, 0, 0),
15 * ListPolynomial(7, 0, 49, 21, 14),
"test 2"
)
val polynomial = ListPolynomial(22, 26, 13, 15, 26)
assertSame(
zero,
0 * polynomial,
"test 3"
)
assertSame(
polynomial,
1 * polynomial,
"test 4"
)
}
}
@Test
fun test_Polynomial_Constant_plus() {
RationalField.listPolynomialSpace {
assertEquals(
ListPolynomial(Rational(-22, 9), Rational(-8, 9), Rational(-8, 7)),
ListPolynomial(Rational(5, 9), Rational(-8, 9), Rational(-8, 7)) + Rational(-3),
"test 1"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(0), Rational(0)),
ListPolynomial(Rational(-2), Rational(0), Rational(0), Rational(0)) + Rational(2),
"test 2"
)
assertEquals(
ListPolynomial(Rational(0)),
ListPolynomial(Rational(-2)) + Rational(2),
"test 3"
)
assertEquals(
ListPolynomial(Rational(0)),
ListPolynomial<Rational>() + Rational(0),
"test 4"
)
assertEquals(
ListPolynomial(Rational(-1), Rational(0), Rational(0), Rational(0)),
ListPolynomial(Rational(-2), Rational(0), Rational(0), Rational(0)) + Rational(1),
"test 5"
)
assertEquals(
ListPolynomial(Rational(-1)),
ListPolynomial(Rational(-2)) + Rational(1),
"test 6"
)
assertEquals(
ListPolynomial(Rational(2)),
ListPolynomial<Rational>() + Rational(2),
"test 7"
)
}
}
@Test
fun test_Polynomial_Constant_minus() {
RationalField.listPolynomialSpace {
assertEquals(
ListPolynomial(Rational(32, 9), Rational(-8, 9), Rational(-8, 7)),
ListPolynomial(Rational(5, 9), Rational(-8, 9), Rational(-8, 7)) - Rational(-3),
"test 1"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(0), Rational(0)),
ListPolynomial(Rational(2), Rational(0), Rational(0), Rational(0)) - Rational(2),
"test 2"
)
assertEquals(
ListPolynomial(Rational(0)),
ListPolynomial(Rational(2)) - Rational(2),
"test 3"
)
assertEquals(
ListPolynomial(Rational(0)),
ListPolynomial<Rational>() - Rational(0),
"test 4"
)
assertEquals(
ListPolynomial(Rational(1), Rational(0), Rational(0), Rational(0)),
ListPolynomial(Rational(2), Rational(0), Rational(0), Rational(0)) - Rational(1),
"test 5"
)
assertEquals(
ListPolynomial(Rational(1)),
ListPolynomial(Rational(2)) - Rational(1),
"test 6"
)
assertEquals(
ListPolynomial(Rational(-2)),
ListPolynomial<Rational>() - Rational(2),
"test 7"
)
}
}
@Test
fun test_Polynomial_Constant_times() {
IntModuloRing(35).listPolynomialSpace {
assertEquals(
ListPolynomial(34, 2, 1, 20, 2),
ListPolynomial(22, 26, 13, 15, 26) * 27.asConstant(),
"test 1"
)
assertEquals(
ListPolynomial(0, 0, 0, 0, 0),
ListPolynomial(7, 0, 49, 21, 14) * 15.asConstant(),
"test 2"
)
}
}
@Test
fun test_Constant_Polynomial_plus() {
RationalField.listPolynomialSpace {
assertEquals(
ListPolynomial(Rational(-22, 9), Rational(-8, 9), Rational(-8, 7)),
Rational(-3) + ListPolynomial(Rational(5, 9), Rational(-8, 9), Rational(-8, 7)),
"test 1"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(0), Rational(0)),
Rational(2) + ListPolynomial(Rational(-2), Rational(0), Rational(0), Rational(0)),
"test 2"
)
assertEquals(
ListPolynomial(Rational(0)),
Rational(2) + ListPolynomial(Rational(-2)),
"test 3"
)
assertEquals(
ListPolynomial(Rational(0)),
Rational(0) + ListPolynomial(),
"test 4"
)
assertEquals(
ListPolynomial(Rational(-1), Rational(0), Rational(0), Rational(0)),
Rational(1) + ListPolynomial(Rational(-2), Rational(0), Rational(0), Rational(0)),
"test 5"
)
assertEquals(
ListPolynomial(Rational(-1)),
Rational(1) + ListPolynomial(Rational(-2)),
"test 6"
)
assertEquals(
ListPolynomial(Rational(2)),
Rational(2) + ListPolynomial(),
"test 7"
)
}
}
@Test
fun test_Constant_Polynomial_minus() {
RationalField.listPolynomialSpace {
assertEquals(
ListPolynomial(Rational(32, 9), Rational(-8, 9), Rational(-8, 7)),
Rational(3) - ListPolynomial(Rational(-5, 9), Rational(8, 9), Rational(8, 7)),
"test 1"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(0), Rational(0)),
Rational(-2) - ListPolynomial(Rational(-2), Rational(0), Rational(0), Rational(0)),
"test 2"
)
assertEquals(
ListPolynomial(Rational(0)),
Rational(-2) - ListPolynomial(Rational(-2)),
"test 3"
)
assertEquals(
ListPolynomial(Rational(0)),
Rational(0) - ListPolynomial(),
"test 4"
)
assertEquals(
ListPolynomial(Rational(1), Rational(0), Rational(0), Rational(0)),
Rational(-1) - ListPolynomial(Rational(-2), Rational(0), Rational(0), Rational(0)),
"test 5"
)
assertEquals(
ListPolynomial(Rational(1)),
Rational(-1) - ListPolynomial(Rational(-2)),
"test 6"
)
assertEquals(
ListPolynomial(Rational(-2)),
Rational(-2) - ListPolynomial(),
"test 7"
)
}
}
@Test
fun test_Constant_Polynomial_times() {
IntModuloRing(35).listPolynomialSpace {
assertEquals(
ListPolynomial(34, 2, 1, 20, 2),
27 * ListPolynomial(22, 26, 13, 15, 26),
"test 1"
)
assertEquals(
ListPolynomial(0, 0, 0, 0, 0),
15 * ListPolynomial(7, 0, 49, 21, 14),
"test 2"
)
}
}
@Test
fun test_Polynomial_unaryMinus() {
RationalField.listPolynomialSpace {
assertEquals(
ListPolynomial(Rational(-5, 9), Rational(8, 9), Rational(8, 7)),
-ListPolynomial(Rational(5, 9), Rational(-8, 9), Rational(-8, 7)),
"test 1"
)
assertEquals(
ListPolynomial(Rational(-5, 9), Rational(8, 9), Rational(8, 7), Rational(0), Rational(0)),
-ListPolynomial(Rational(5, 9), Rational(-8, 9), Rational(-8, 7), Rational(0), Rational(0)),
"test 2"
)
}
}
@Test
fun test_Polynomial_Polynomial_plus() {
RationalField.listPolynomialSpace {
// (5/9 - 8/9 x - 8/7 x^2) + (-5/7 + 5/1 x + 5/8 x^2) ?= -10/63 + 37/9 x - 29/56 x^2
assertEquals(
ListPolynomial(Rational(-10, 63), Rational(37, 9), Rational(-29, 56)),
ListPolynomial(Rational(5, 9), Rational(-8, 9), Rational(-8, 7)) +
ListPolynomial(Rational(-5, 7), Rational(5, 1), Rational(5, 8)),
"test 1"
)
// (-2/9 - 8/3 x) + (0 + 9/4 x + 2/4 x^2) ?= -2/9 - 5/12 x + 2/4 x^2
assertEquals(
ListPolynomial(Rational(-2, 9), Rational(-5, 12), Rational(2, 4)),
ListPolynomial(Rational(-2, 9), Rational(-8, 3)) +
ListPolynomial(Rational(0), Rational(9, 4), Rational(2, 4)),
"test 2"
)
// (-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), 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(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"
)
}
}
@Test
fun test_Polynomial_Polynomial_minus() {
RationalField.listPolynomialSpace {
// (5/9 - 8/9 x - 8/7 x^2) - (-5/7 + 5/1 x + 5/8 x^2) ?= 80/63 - 53/9 x - 99/56 x^2
assertEquals(
ListPolynomial(Rational(80, 63), Rational(-53, 9), Rational(-99, 56)),
ListPolynomial(Rational(5, 9), Rational(-8, 9), Rational(-8, 7)) -
ListPolynomial(Rational(-5, 7), Rational(5, 1), Rational(5, 8)),
"test 1"
)
// (-2/9 - 8/3 x) - (0 + 9/4 x + 2/4 x^2) ?= -2/9 - 59/12 x - 2/4 x^2
assertEquals(
ListPolynomial(Rational(-2, 9), Rational(-59, 12), Rational(-2, 4)),
ListPolynomial(Rational(-2, 9), Rational(-8, 3)) -
ListPolynomial(Rational(0), Rational(9, 4), Rational(2, 4)),
"test 2"
)
// (-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), 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(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"
)
}
}
@Test
fun test_Polynomial_Polynomial_times() {
IntModuloRing(35).listPolynomialSpace {
// (1 + x + x^2) * (1 - x + x^2) ?= 1 + x^2 + x^4
assertEquals(
ListPolynomial(1, 0, 1, 0, 1),
ListPolynomial(1, -1, 1) * ListPolynomial(1, 1, 1),
"test 1"
)
// Spoiler: 5 * 7 = 0
assertEquals(
ListPolynomial(0, 0, 0, 0, 0),
ListPolynomial(5, -25, 10) * ListPolynomial(21, 14, -7),
"test 2"
)
}
}
}

View File

@ -0,0 +1,982 @@
/*
* 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.misc.UnstableKMathAPI
import space.kscience.kmath.functions.testUtils.Rational
import space.kscience.kmath.functions.testUtils.RationalField
import space.kscience.kmath.functions.testUtils.assertFailsWithTypeAndMessage
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
@OptIn(UnstableKMathAPI::class)
class ListPolynomialUtilTest {
@Test
fun test_Polynomial_substitute_Double() {
assertEquals(
0.0,
ListPolynomial(1.0, -2.0, 1.0).substitute(1.0),
0.001,
"test 1"
)
assertEquals(
0.0,
ListPolynomial(1.0, -2.0, 1.0).substitute(1.0),
0.001,
"test 1"
)
assertEquals(
1.1931904761904761,
ListPolynomial(0.625, 2.6666666666666665, 0.5714285714285714, 1.5).substitute(0.2),
0.001,
"test 2"
)
assertEquals(
0.5681904761904762,
ListPolynomial(0.0, 2.6666666666666665, 0.5714285714285714, 1.5).substitute(0.2),
0.001,
"test 3"
)
assertEquals(
1.1811904761904761,
ListPolynomial(0.625, 2.6666666666666665, 0.5714285714285714, 0.0).substitute(0.2),
0.001,
"test 4"
)
assertEquals(
1.1703333333333332,
ListPolynomial(0.625, 2.6666666666666665, 0.0, 1.5).substitute(0.2),
0.001,
"test 5"
)
}
@Test
fun test_Polynomial_substitute_Constant() {
assertEquals(
Rational(0),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).substitute(RationalField, Rational(1)),
"test 1"
)
assertEquals(
Rational(25057, 21000),
ListPolynomial(Rational(5, 8), Rational(8, 3), Rational(4, 7), Rational(3, 2))
.substitute(RationalField, Rational(1, 5)),
"test 2"
)
assertEquals(
Rational(2983, 5250),
ListPolynomial(Rational(0), Rational(8, 3), Rational(4, 7), Rational(3, 2))
.substitute(RationalField, Rational(1, 5)),
"test 3"
)
assertEquals(
Rational(4961, 4200),
ListPolynomial(Rational(5, 8), Rational(8, 3), Rational(4, 7), Rational(0))
.substitute(RationalField, Rational(1, 5)),
"test 4"
)
assertEquals(
Rational(3511, 3000),
ListPolynomial(Rational(5, 8), Rational(8, 3), Rational(0), Rational(3, 2))
.substitute(RationalField, Rational(1, 5)),
"test 5"
)
}
@Test
fun test_Polynomial_substitute_Polynomial() {
assertEquals(
ListPolynomial(Rational(0)),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).substitute(RationalField, ListPolynomial(Rational(1))),
"test 1"
)
assertEquals(
ListPolynomial(Rational(709, 378), Rational(155, 252), Rational(19, 525), Rational(2, 875)),
ListPolynomial(Rational(1, 7), Rational(9, 4), Rational(1, 3), Rational(2, 7))
.substitute(RationalField, ListPolynomial(Rational(6, 9), Rational(1, 5))),
"test 2"
)
assertEquals(
ListPolynomial(Rational(655, 378), Rational(155, 252), Rational(19, 525), Rational(2, 875)),
ListPolynomial(Rational(0), Rational(9, 4), Rational(1, 3), Rational(2, 7))
.substitute(RationalField, ListPolynomial(Rational(6, 9), Rational(1, 5))),
"test 3"
)
assertEquals(
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"
)
assertEquals(
ListPolynomial(Rational(653, 378), Rational(221, 420), Rational(4, 175), Rational(2, 875)),
ListPolynomial(Rational(1, 7), Rational(9, 4), Rational(0), Rational(2, 7))
.substitute(RationalField, ListPolynomial(Rational(6, 9), Rational(1, 5))),
"test 5"
)
assertEquals(
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"
)
}
@Test
@Ignore // FIXME: This tests work only for sane realisations of the substitutions. Currently, it is not.
// Sane algorithm for substitution p(q/r) (p, q, and r are polynomials) should return denominator r^deg(p),
// not r^(deg(p)(deg(p)+1)/2) as it is now.
fun test_Polynomial_substitute_RationalFunction() {
assertEquals(
ListRationalFunction(ListPolynomial(Rational(0)), ListPolynomial(Rational(1))),
ListPolynomial(Rational(1), Rational(-2), Rational(1))
.substitute(RationalField, ListRationalFunction(ListPolynomial(Rational(1)), ListPolynomial(Rational(1)))),
"test 1"
)
assertEquals(
ListRationalFunction(
ListPolynomial(
Rational(66349, 243),
Rational(-17873, 405),
Rational(173533, 3780),
Rational(-91141, 567),
Rational(5773909, 105840),
Rational(-23243, 630),
Rational(1573, 27)
),
ListPolynomial(
Rational(169, 81),
Rational(-130, 27),
Rational(115, 18),
Rational(-797, 54),
Rational(1985, 144),
Rational(-55, 6),
Rational(121, 9)
)
),
ListPolynomial(
Rational(13, 3),
Rational(-9, 5),
Rational(5, 5)
).substitute(RationalField,
ListRationalFunction(
ListPolynomial(
Rational(15, 1),
Rational(6, 9),
Rational(-3, 7)
),
ListPolynomial(
Rational(-13, 9),
Rational(10, 6),
Rational(-10, 8),
Rational(11, 3)
)
)
),
"test 2"
)
assertEquals(
ListRationalFunction(
ListPolynomial(
Rational(0, 1),
Rational(0, 1),
Rational(-14, 9),
Rational(31, 14),
Rational(-5077, 980),
Rational(99, 35)
),
ListPolynomial(
Rational(0, 1),
Rational(0, 1),
Rational(25, 9),
Rational(-25, 6),
Rational(1985, 144),
Rational(-55, 6),
Rational(121, 9)
)
),
ListPolynomial(
Rational(0),
Rational(-9, 5),
Rational(5, 5)
).substitute(RationalField,
ListRationalFunction(
ListPolynomial(
Rational(0),
Rational(6, 9),
Rational(-3, 7)
),
ListPolynomial(
Rational(0),
Rational(10, 6),
Rational(-10, 8),
Rational(11, 3)
)
)
),
"test 3"
)
assertEquals(
ListRationalFunction(
ListPolynomial(
Rational(-898, 27),
Rational(271, 45),
Rational(-65, 12) ,
Rational(0),
Rational(0),
Rational(0),
Rational(0)
),
ListPolynomial(
Rational(-13, 9),
Rational(5, 3),
Rational(-5, 4),
Rational(0),
Rational(0),
Rational(0),
Rational(0)
)
),
ListPolynomial(
Rational(13, 3),
Rational(-9, 5),
Rational(0)
).substitute(RationalField,
ListRationalFunction(
ListPolynomial(
Rational(15, 1),
Rational(6, 9),
Rational(0)
),
ListPolynomial(
Rational(-13, 9),
Rational(10, 6),
Rational(-10, 8),
Rational(0)
)
)
),
"test 4"
)
assertEquals(
ListRationalFunction(
ListPolynomial(
Rational(56872, 243),
Rational(0, 1),
Rational(-90, 7),
Rational(-3718, 81),
Rational(9, 49),
Rational(0, 1),
Rational(1573, 27)
),
ListPolynomial(
Rational(169, 81),
Rational(0, 1),
Rational(0, 1),
Rational(-286, 27),
Rational(0, 1),
Rational(0, 1),
Rational(121, 9)
)
),
ListPolynomial(
Rational(13, 3),
Rational(0),
Rational(5, 5)
).substitute(RationalField,
ListRationalFunction(
ListPolynomial(
Rational(15, 1),
Rational(0),
Rational(-3, 7)
),
ListPolynomial(
Rational(-13, 9),
Rational(0),
Rational(0),
Rational(11, 3)
)
)
),
"test 5"
)
}
@Test
fun test_RationalFunction_substitute_Double() {
assertEquals(
0.0,
ListRationalFunction(
ListPolynomial(1.0, -2.0, 1.0),
ListPolynomial(-6.302012278484357, 5.831971885376948, -9.271604788393432, 5.494387848015814, -3.7187384450880785)
).substitute(1.0),
0.001,
"test 1"
)
assertEquals(
2.693702616649797,
ListRationalFunction(
ListPolynomial(-5.848840571263625, -1.660411278951134, -3.793740946372443, -9.624569269490076),
ListPolynomial(-2.9680680215311073, -1.862973627119981, 4.776550592888336, -2.7320154512368466)
).substitute(-7.53452770353279),
0.001,
"test 2"
)
assertEquals(
2.692226268901378,
ListRationalFunction(
ListPolynomial(0.0, -1.660411278951134, -3.793740946372443, -9.624569269490076),
ListPolynomial(0.0, -1.862973627119981, 4.776550592888336, -2.7320154512368466)
).substitute(-7.53452770353279),
0.001,
"test 3"
)
assertEquals(
-0.7394904842099175,
ListRationalFunction(
ListPolynomial(-5.848840571263625, -1.660411278951134, -3.793740946372443, 0.0),
ListPolynomial(-2.9680680215311073, -1.862973627119981, 4.776550592888336, 0.0)
).substitute(-7.53452770353279),
0.001,
"test 4"
)
assertEquals(
3.526835209398159,
ListRationalFunction(
ListPolynomial(-5.848840571263625, 0.0, 0.0, -9.624569269490076),
ListPolynomial(-2.9680680215311073, 0.0, 0.0, -2.7320154512368466)
).substitute(-7.53452770353279),
0.001,
"test 5"
)
}
@Test
fun test_RationalFunction_substitute_Constant() {
assertEquals(
Rational(0),
ListRationalFunction(
ListPolynomial(Rational(1), Rational(-2), Rational(1)),
ListPolynomial(Rational(1)),
).substitute(RationalField, Rational(1)),
"test 1"
)
assertEquals(
Rational(1149615, 61306),
ListRationalFunction(
ListPolynomial(Rational(17, 7), Rational(18, 3), Rational(18, 8), Rational(9, 1)),
ListPolynomial(Rational(11, 9), Rational(-6, 5), Rational(-12, 7), Rational(2, 1)),
).substitute(RationalField, Rational(-7, 8)),
"test 2"
)
assertEquals(
Rational(3495, 586),
ListRationalFunction(
ListPolynomial(Rational(0), Rational(18, 3), Rational(18, 8), Rational(9, 1)),
ListPolynomial(Rational(0), Rational(-6, 5), Rational(-12, 7), Rational(2, 1)),
).substitute(RationalField, Rational(-7, 8)),
"test 3"
)
assertEquals(
Rational(-88605, 77392),
ListRationalFunction(
ListPolynomial(Rational(17, 7), Rational(18, 3), Rational(18, 8), Rational(0)),
ListPolynomial(Rational(11, 9), Rational(-6, 5), Rational(-12, 7), Rational(0)),
).substitute(RationalField, Rational(-7, 8)),
"test 4"
)
assertEquals(
Rational(116145, 3794),
ListRationalFunction(
ListPolynomial(Rational(17, 7), Rational(0), Rational(0), Rational(9, 1)),
ListPolynomial(Rational(11, 9), Rational(0), Rational(0), Rational(2, 1)),
).substitute(RationalField, Rational(-7, 8)),
"test 5"
)
}
@Test
fun test_RationalFunction_substitute_Polynomial() {
assertEquals(
ListRationalFunction(
ListPolynomial(Rational(0)),
ListPolynomial(Rational(1))
),
ListRationalFunction(
ListPolynomial(Rational(1), Rational(-2), Rational(1)),
ListPolynomial(Rational(1)),
).substitute(RationalField, ListPolynomial(Rational(1))),
"test 1"
)
assertEquals(
ListRationalFunction(
ListPolynomial(
Rational(-283303, 36),
Rational(-23593, 24),
Rational(368713, 192),
Rational(1455, 8),
Rational(-272171, 1536),
Rational(-2149, 192),
Rational(469, 64),
Rational(11, 48),
Rational(-11, 96)
),
ListPolynomial(
Rational(5797, 12),
Rational(595, 16),
Rational(-5285, 72),
Rational(-745, 192),
Rational(1105, 288),
Rational(5, 48),
Rational(-5, 72)
)
),
ListRationalFunction(
ListPolynomial(
Rational(2, 9),
Rational(11, 3),
Rational(-9, 4),
Rational(-6, 1),
Rational(-11, 6)
),
ListPolynomial(
Rational(-2, 3),
Rational(-15, 4),
Rational(5, 9),
Rational(-5, 9)
)
).substitute(RationalField,
ListPolynomial(
Rational(-9, 1),
Rational(-1, 4),
Rational(2, 4)
)
),
"test 2"
)
assertEquals(
ListRationalFunction(
ListPolynomial(
Rational(0, 1),
Rational(-11, 12),
Rational(325, 192),
Rational(21, 32),
Rational(-1739, 1536),
Rational(227, 192),
Rational(-59, 64),
Rational(11, 48),
Rational(-11, 96)
),
ListPolynomial(
Rational(0, 1),
Rational(15, 16),
Rational(-265, 144),
Rational(-25, 192),
Rational(25, 288),
Rational(5, 48),
Rational(-5, 72)
)
),
ListRationalFunction(
ListPolynomial(
Rational(0, 9),
Rational(11, 3),
Rational(-9, 4),
Rational(-6, 1),
Rational(-11, 6)
),
ListPolynomial(
Rational(0, 3),
Rational(-15, 4),
Rational(5, 9),
Rational(-5, 9)
)
).substitute(RationalField,
ListPolynomial(
Rational(0, 1),
Rational(-1, 4),
Rational(2, 4)
)
),
"test 3"
)
assertEquals(
ListRationalFunction(
ListPolynomial(
Rational(149723, 36),
Rational(8483, 24),
Rational(639, 64),
Rational(3, 32),
Rational(0),
Rational(0),
Rational(0),
Rational(0),
Rational(0)
),
ListPolynomial(
Rational(937, 12),
Rational(55, 16),
Rational(5, 144),
Rational(0),
Rational(0),
Rational(0),
Rational(0)
)
),
ListRationalFunction(
ListPolynomial(
Rational(2, 9),
Rational(11, 3),
Rational(-9, 4),
Rational(-6, 1),
Rational(0)
),
ListPolynomial(
Rational(-2, 3),
Rational(-15, 4),
Rational(5, 9),
Rational(0)
)
).substitute(RationalField,
ListPolynomial(
Rational(-9, 1),
Rational(-1, 4),
Rational(0)
)
),
"test 4"
)
assertEquals(
ListRationalFunction(
ListPolynomial(
Rational(-216509, 18),
Rational(0, 1),
Rational(2673, 1),
Rational(0, 1),
Rational(-891, 4),
Rational(0, 1),
Rational(33, 4),
Rational(0, 1),
Rational(-11, 96)
),
ListPolynomial(
Rational(1213, 3),
Rational(0, 1),
Rational(-135, 2),
Rational(0, 1),
Rational(15, 4),
Rational(0, 1),
Rational(-5, 72)
)
),
ListRationalFunction(
ListPolynomial(
Rational(2, 9),
Rational(0),
Rational(0),
Rational(0),
Rational(-11, 6)
),
ListPolynomial(
Rational(-2, 3),
Rational(0),
Rational(0),
Rational(-5, 9)
)
).substitute(RationalField,
ListPolynomial(
Rational(-9, 1),
Rational(0),
Rational(2, 4)
)
),
"test 5"
)
}
@Test
@Ignore // FIXME: This tests work only for sane realisations of the substitutions. Currently, it is not.
// Sane algorithm for substitution p(q/r) (p, q, and r are polynomials) should return denominator r^deg(p),
// not r^(deg(p)(deg(p)+1)/2) as it is now.
fun test_RationalFunction_substitute_RationalFunction() {
assertEquals(
ListRationalFunction(
ListPolynomial(Rational(0)),
ListPolynomial(Rational(1))
),
ListRationalFunction(
ListPolynomial(Rational(1), Rational(-2), Rational(1)),
ListPolynomial(Rational(1))
).substitute(RationalField,
ListRationalFunction(
ListPolynomial(Rational(1)),
ListPolynomial(Rational(1))
)
),
"test 1"
)
assertEquals(
ListRationalFunction(
ListPolynomial(
Rational(130087, 3888),
Rational(-2866333, 65610),
Rational(-5076229, 97200),
Rational(222136997, 3280500),
Rational(754719329, 20995200),
Rational(-12010283, 324000),
Rational(-2011967, 172800),
Rational(18607, 2880),
Rational(4705, 4096)
),
ListPolynomial(
Rational(-143820355, 3779136),
Rational(73886869, 1574640),
Rational(1440175193, 15746400),
Rational(-5308968857, 52488000),
Rational(-186910083731, 2099520000),
Rational(125043463, 1555200),
Rational(5299123, 388800),
Rational(-213757, 15360),
Rational(1380785, 147456)
)
),
ListRationalFunction(
ListPolynomial(
Rational(1, 1),
Rational(-10, 5),
Rational(18, 8),
Rational(-8, 8)
),
ListPolynomial(
Rational(-14, 8),
Rational(-14, 8),
Rational(-19, 6),
Rational(14, 3),
Rational(8, 9)
)
).substitute(RationalField,
ListRationalFunction(
ListPolynomial(
Rational(14, 9),
Rational(-2, 5),
Rational(-14, 7)
),
ListPolynomial(
Rational(-6, 4),
Rational(5, 9),
Rational(1, 8)
)
)
),
"test 2"
)
assertEquals(
ListRationalFunction(
ListPolynomial(
Rational(0, 1),
Rational(0, 1),
Rational(0, 1),
Rational(0, 1),
Rational(5173, 18225),
Rational(904291, 364500),
Rational(283127, 43200),
Rational(37189, 5760),
Rational(147, 128)
),
ListPolynomial(
Rational(0, 1),
Rational(0, 1),
Rational(0, 1),
Rational(0, 1),
Rational(-163589, 911250),
Rational(-881831, 291600),
Rational(-10722229, 777600),
Rational(-640921, 46080),
Rational(86303, 9216)
)
),
ListRationalFunction(
ListPolynomial(
Rational(0),
Rational(-10, 5),
Rational(18, 8),
Rational(-8, 8)
),
ListPolynomial(
Rational(0),
Rational(-14, 8),
Rational(-19, 6),
Rational(14, 3),
Rational(8, 9)
)
).substitute(RationalField,
ListRationalFunction(
ListPolynomial(
Rational(0),
Rational(-2, 5),
Rational(-14, 7)
),
ListPolynomial(
Rational(0),
Rational(5, 9),
Rational(1, 8)
)
)
),
"test 3"
)
assertEquals(
ListRationalFunction(
ListPolynomial(
Rational(445, 16),
Rational(-2011, 54),
Rational(1359199, 72900),
Rational(-135733, 32805),
Rational(2254, 6561),
Rational(0, 1),
Rational(0, 1),
Rational(0, 1),
Rational(0, 1)
),
ListPolynomial(
Rational(-2018387, 46656),
Rational(82316437, 1574640),
Rational(-9335047, 393660),
Rational(15765889, 3280500),
Rational(-242089, 656100),
Rational(0, 1),
Rational(0, 1),
Rational(0, 1),
Rational(0, 1)
)
),
ListRationalFunction(
ListPolynomial(
Rational(1, 1),
Rational(-10, 5),
Rational(18, 8),
Rational(0)
),
ListPolynomial(
Rational(-14, 8),
Rational(-14, 8),
Rational(-19, 6),
Rational(14, 3),
Rational(0)
)
).substitute(RationalField,
ListRationalFunction(
ListPolynomial(
Rational(14, 9),
Rational(-2, 5),
Rational(0)
),
ListPolynomial(
Rational(-6, 4),
Rational(5, 9),
Rational(0)
)
)
),
"test 4"
)
assertEquals(
ListRationalFunction(
ListPolynomial(
Rational(41635, 3888),
Rational(0, 1),
Rational(-279187, 11664),
Rational(0, 1),
Rational(103769, 3456),
Rational(0, 1),
Rational(-11017, 768),
Rational(0, 1),
Rational(4097, 4096)
),
ListPolynomial(
Rational(-13811791, 3779136),
Rational(0, 1),
Rational(-9999395, 419904),
Rational(0, 1),
Rational(6376601, 124416),
Rational(0, 1),
Rational(-3668315, 82944),
Rational(0, 1),
Rational(2097089, 147456)
)
),
ListRationalFunction(
ListPolynomial(
Rational(1, 1),
Rational(0),
Rational(0),
Rational(-8, 8)
),
ListPolynomial(
Rational(-14, 8),
Rational(0),
Rational(0),
Rational(0),
Rational(8, 9)
)
).substitute(RationalField,
ListRationalFunction(
ListPolynomial(
Rational(14, 9),
Rational(0),
Rational(-14, 7)
),
ListPolynomial(
Rational(-6, 4),
Rational(0),
Rational(1, 8)
)
)
),
"test 5"
)
}
@Test
fun test_Polynomial_derivative() {
assertEquals(
ListPolynomial(Rational(-2), Rational(2)),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).derivative(RationalField),
"test 1"
)
assertEquals(
ListPolynomial(Rational(-8, 3), Rational(8, 9), Rational(15, 7), Rational(-20, 9)),
ListPolynomial(Rational(1, 5), Rational(-8, 3), Rational(4, 9), Rational(5, 7), Rational(-5, 9)).derivative(RationalField),
"test 2"
)
assertEquals(
ListPolynomial(Rational(0), Rational(8, 9), Rational(15, 7), Rational(-20, 9)),
ListPolynomial(Rational(0), Rational(0), Rational(4, 9), Rational(5, 7), Rational(-5, 9)).derivative(RationalField),
"test 3"
)
assertEquals(
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"
)
}
@Test
fun test_Polynomial_nthDerivative() {
assertEquals(
ListPolynomial(Rational(-2), Rational(2)),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).nthDerivative(RationalField, 1),
"test 1"
)
assertFailsWithTypeAndMessage<IllegalArgumentException>(
"Order of derivative must be non-negative",
"test2"
) {
ListPolynomial(Rational(1), Rational(-2), Rational(1)).nthDerivative(RationalField, -1)
}
assertEquals(
ListPolynomial(Rational(1), Rational(-2), Rational(1)),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).nthDerivative(RationalField, 0),
"test 3"
)
assertEquals(
ListPolynomial(Rational(2)),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).nthDerivative(RationalField, 2),
"test 4"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).nthDerivative(RationalField, 3),
"test 5"
)
assertEquals(
ListPolynomial(),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).nthDerivative(RationalField, 4),
"test 6"
)
assertEquals(
ListPolynomial(Rational(8, 9), Rational(30, 7), Rational(-20, 3)),
ListPolynomial(Rational(1, 5), Rational(-8, 3), Rational(4, 9), Rational(5, 7), Rational(-5, 9)).nthDerivative(RationalField, 2),
"test 7"
)
assertEquals(
ListPolynomial(Rational(8, 9), Rational(30, 7), Rational(-20, 3)),
ListPolynomial(Rational(0), Rational(0), Rational(4, 9), Rational(5, 7), Rational(-5, 9)).nthDerivative(RationalField, 2),
"test 8"
)
assertEquals(
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"
)
}
@Test
fun test_Polynomial_antiderivative() {
assertEquals(
ListPolynomial(Rational(0), Rational(1), Rational(-1), Rational(1, 3)),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).antiderivative(RationalField),
"test 1"
)
assertEquals(
ListPolynomial(Rational(0), Rational(1, 5), Rational(-4, 3), Rational(4, 27), Rational(5, 28), Rational(-1, 9)),
ListPolynomial(Rational(1, 5), Rational(-8, 3), Rational(4, 9), Rational(5, 7), Rational(-5, 9)).antiderivative(RationalField),
"test 2"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(0), Rational(4, 27), Rational(5, 28), Rational(-1, 9)),
ListPolynomial(Rational(0), Rational(0), Rational(4, 9), Rational(5, 7), Rational(-5, 9)).antiderivative(RationalField),
"test 3"
)
assertEquals(
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"
)
}
@Test
fun test_Polynomial_nthAntiderivative() {
assertEquals(
ListPolynomial(Rational(0), Rational(1), Rational(-1), Rational(1, 3)),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).nthAntiderivative(RationalField, 1),
"test 1"
)
assertFailsWithTypeAndMessage<IllegalArgumentException>(
"Order of antiderivative must be non-negative",
"test2"
) {
ListPolynomial(Rational(1), Rational(-2), Rational(1)).nthAntiderivative(RationalField, -1)
}
assertEquals(
ListPolynomial(Rational(1), Rational(-2), Rational(1)),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).nthAntiderivative(RationalField, 0),
"test 3"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(1, 2), Rational(-1, 3), Rational(1, 12)),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).nthAntiderivative(RationalField, 2),
"test 4"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(0), Rational(1, 6), Rational(-1, 12), Rational(1, 60)),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).nthAntiderivative(RationalField, 3),
"test 5"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(0), Rational(0), Rational(1, 24), Rational(-1, 60), Rational(1, 360)),
ListPolynomial(Rational(1), Rational(-2), Rational(1)).nthAntiderivative(RationalField, 4),
"test 6"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(1, 10), Rational(-4, 9), Rational(1, 27), Rational(1, 28), Rational(-1, 54)),
ListPolynomial(Rational(1, 5), Rational(-8, 3), Rational(4, 9), Rational(5, 7), Rational(-5, 9)).nthAntiderivative(RationalField, 2),
"test 7"
)
assertEquals(
ListPolynomial(Rational(0), Rational(0), Rational(0), Rational(0), Rational(1, 27), Rational(1, 28), Rational(-1, 54)),
ListPolynomial(Rational(0), Rational(0), Rational(4, 9), Rational(5, 7), Rational(-5, 9)).nthAntiderivative(RationalField, 2),
"test 8"
)
assertEquals(
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

@ -9,4 +9,4 @@ import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.asBuffer import space.kscience.kmath.structures.asBuffer
public fun <T> bufferOf(vararg elements: T): Buffer<T> = elements.asBuffer() fun <T> bufferOf(vararg elements: T): Buffer<T> = elements.asBuffer()

View File

@ -10,9 +10,9 @@ package space.kscience.kmath.functions.testUtils
import space.kscience.kmath.operations.Ring import space.kscience.kmath.operations.Ring
public class IntModulo { class IntModulo {
public val residue: Int val residue: Int
public val modulus: Int val modulus: Int
@PublishedApi @PublishedApi
internal constructor(residue: Int, modulus: Int, toCheckInput: Boolean = true) { internal constructor(residue: Int, modulus: Int, toCheckInput: Boolean = true) {
@ -26,16 +26,16 @@ public class IntModulo {
} }
} }
public constructor(residue: Int, modulus: Int) : this(residue, modulus, true) constructor(residue: Int, modulus: Int) : this(residue, modulus, true)
public operator fun unaryPlus(): IntModulo = this operator fun unaryPlus(): IntModulo = this
public operator fun unaryMinus(): IntModulo = operator fun unaryMinus(): IntModulo =
IntModulo( IntModulo(
if (residue == 0) 0 else modulus - residue, if (residue == 0) 0 else modulus - residue,
modulus, modulus,
toCheckInput = false toCheckInput = false
) )
public operator fun plus(other: IntModulo): IntModulo { operator fun plus(other: IntModulo): IntModulo {
require(modulus == other.modulus) { "can not add two residue different modulo" } require(modulus == other.modulus) { "can not add two residue different modulo" }
return IntModulo( return IntModulo(
(residue + other.residue) % modulus, (residue + other.residue) % modulus,
@ -43,13 +43,13 @@ public class IntModulo {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun plus(other: Int): IntModulo = operator fun plus(other: Int): IntModulo =
IntModulo( IntModulo(
(residue + other) % modulus, (residue + other) % modulus,
modulus, modulus,
toCheckInput = false toCheckInput = false
) )
public operator fun minus(other: IntModulo): IntModulo { operator fun minus(other: IntModulo): IntModulo {
require(modulus == other.modulus) { "can not subtract two residue different modulo" } require(modulus == other.modulus) { "can not subtract two residue different modulo" }
return IntModulo( return IntModulo(
(residue - other.residue) % modulus, (residue - other.residue) % modulus,
@ -57,13 +57,13 @@ public class IntModulo {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun minus(other: Int): IntModulo = operator fun minus(other: Int): IntModulo =
IntModulo( IntModulo(
(residue - other) % modulus, (residue - other) % modulus,
modulus, modulus,
toCheckInput = false toCheckInput = false
) )
public operator fun times(other: IntModulo): IntModulo { operator fun times(other: IntModulo): IntModulo {
require(modulus == other.modulus) { "can not multiply two residue different modulo" } require(modulus == other.modulus) { "can not multiply two residue different modulo" }
return IntModulo( return IntModulo(
(residue * other.residue) % modulus, (residue * other.residue) % modulus,
@ -71,13 +71,13 @@ public class IntModulo {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun times(other: Int): IntModulo = operator fun times(other: Int): IntModulo =
IntModulo( IntModulo(
(residue * other) % modulus, (residue * other) % modulus,
modulus, modulus,
toCheckInput = false toCheckInput = false
) )
public operator fun div(other: IntModulo): IntModulo { operator fun div(other: IntModulo): IntModulo {
require(modulus == other.modulus) { "can not divide two residue different modulo" } require(modulus == other.modulus) { "can not divide two residue different modulo" }
val (reciprocalCandidate, gcdOfOtherResidueAndModulus) = bezoutIdentityWithGCD(other.residue, modulus) val (reciprocalCandidate, gcdOfOtherResidueAndModulus) = bezoutIdentityWithGCD(other.residue, modulus)
require(gcdOfOtherResidueAndModulus == 1) { "can not divide to residue that has non-trivial GCD with modulo" } require(gcdOfOtherResidueAndModulus == 1) { "can not divide to residue that has non-trivial GCD with modulo" }
@ -87,7 +87,7 @@ public class IntModulo {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun div(other: Int): IntModulo { operator fun div(other: Int): IntModulo {
val (reciprocalCandidate, gcdOfOtherResidueAndModulus) = bezoutIdentityWithGCD(other, modulus) val (reciprocalCandidate, gcdOfOtherResidueAndModulus) = bezoutIdentityWithGCD(other, modulus)
require(gcdOfOtherResidueAndModulus == 1) { "can not divide to residue that has non-trivial GCD with modulo" } require(gcdOfOtherResidueAndModulus == 1) { "can not divide to residue that has non-trivial GCD with modulo" }
return IntModulo( return IntModulo(
@ -108,11 +108,11 @@ public class IntModulo {
} }
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE")
public class IntModuloRing : Ring<IntModulo> { class IntModuloRing : Ring<IntModulo> {
public val modulus: Int val modulus: Int
public constructor(modulus: Int) { constructor(modulus: Int) {
require(modulus != 0) { "modulus can not be zero" } require(modulus != 0) { "modulus can not be zero" }
this.modulus = if (modulus < 0) -modulus else modulus this.modulus = if (modulus < 0) -modulus else modulus
} }
@ -120,7 +120,7 @@ public class IntModuloRing : Ring<IntModulo> {
override inline val zero: IntModulo get() = IntModulo(0, modulus, toCheckInput = false) override inline val zero: IntModulo get() = IntModulo(0, modulus, toCheckInput = false)
override inline val one: IntModulo get() = IntModulo(1, modulus, toCheckInput = false) override inline val one: IntModulo get() = IntModulo(1, modulus, toCheckInput = false)
public fun number(arg: Int): IntModulo = IntModulo(arg, modulus, toCheckInput = false) fun number(arg: Int): IntModulo = IntModulo(arg, modulus, toCheckInput = false)
override inline fun add(left: IntModulo, right: IntModulo): IntModulo = left + right override inline fun add(left: IntModulo, right: IntModulo): IntModulo = left + right
override inline fun multiply(left: IntModulo, right: IntModulo): IntModulo = left * right override inline fun multiply(left: IntModulo, right: IntModulo): IntModulo = left * right
@ -129,5 +129,5 @@ public class IntModuloRing : Ring<IntModulo> {
override inline fun IntModulo.plus(arg: IntModulo): IntModulo = this + arg override inline fun IntModulo.plus(arg: IntModulo): IntModulo = this + arg
override inline fun IntModulo.minus(arg: IntModulo): IntModulo = this - arg override inline fun IntModulo.minus(arg: IntModulo): IntModulo = this - arg
override inline fun IntModulo.times(arg: IntModulo): IntModulo = this * arg override inline fun IntModulo.times(arg: IntModulo): IntModulo = this * arg
public inline fun IntModulo.div(arg: IntModulo): IntModulo = this / arg inline fun IntModulo.div(arg: IntModulo): IntModulo = this / arg
} }

View File

@ -6,15 +6,14 @@
package space.kscience.kmath.functions.testUtils package space.kscience.kmath.functions.testUtils
import space.kscience.kmath.functions.ListPolynomial import space.kscience.kmath.functions.ListPolynomial
import space.kscience.kmath.functions.Polynomial
import space.kscience.kmath.functions.ListPolynomialSpace import space.kscience.kmath.functions.ListPolynomialSpace
import space.kscience.kmath.functions.PolynomialSpaceOverRing import space.kscience.kmath.functions.PolynomialSpaceOverRing
public fun ListPolynomialSpace<IntModulo, IntModuloRing>.ListPolynomial(vararg coefs: Int): ListPolynomial<IntModulo> = fun ListPolynomialSpace<IntModulo, IntModuloRing>.ListPolynomial(vararg coefs: Int): ListPolynomial<IntModulo> =
ListPolynomial(coefs.map { IntModulo(it, ring.modulus) }) ListPolynomial(coefs.map { IntModulo(it, ring.modulus) })
public fun IntModuloRing.ListPolynomial(vararg coefs: Int): ListPolynomial<IntModulo> = fun IntModuloRing.ListPolynomial(vararg coefs: Int): ListPolynomial<IntModulo> =
ListPolynomial(coefs.map { IntModulo(it, modulus) }) ListPolynomial(coefs.map { IntModulo(it, modulus) })
public fun IntModuloRing.m(arg: Int): IntModulo = IntModulo(arg, modulus) fun IntModuloRing.m(arg: Int): IntModulo = IntModulo(arg, modulus)
public fun PolynomialSpaceOverRing<IntModulo, *, IntModuloRing>.m(arg: Int): IntModulo = IntModulo(arg, ring.modulus) fun PolynomialSpaceOverRing<IntModulo, *, IntModuloRing>.m(arg: Int): IntModulo = IntModulo(arg, ring.modulus)

View File

@ -12,14 +12,14 @@ import space.kscience.kmath.operations.Field
import space.kscience.kmath.operations.NumbersAddOps import space.kscience.kmath.operations.NumbersAddOps
@Suppress("NAME_SHADOWING") @Suppress("NAME_SHADOWING")
public class Rational { class Rational {
public companion object { companion object {
public val ZERO: Rational = Rational(0L) val ZERO: Rational = Rational(0L)
public val ONE: Rational = Rational(1L) val ONE: Rational = Rational(1L)
} }
public val numerator: Long val numerator: Long
public val denominator: Long val denominator: Long
internal constructor(numerator: Long, denominator: Long, toCheckInput: Boolean = true) { internal constructor(numerator: Long, denominator: Long, toCheckInput: Boolean = true) {
if (toCheckInput) { if (toCheckInput) {
@ -35,16 +35,16 @@ public class Rational {
} }
} }
public constructor(numerator: Int, denominator: Int) : this(numerator.toLong(), denominator.toLong(), true) constructor(numerator: Int, denominator: Int) : this(numerator.toLong(), denominator.toLong(), true)
public constructor(numerator: Int, denominator: Long) : this(numerator.toLong(), denominator, true) constructor(numerator: Int, denominator: Long) : this(numerator.toLong(), denominator, true)
public constructor(numerator: Long, denominator: Int) : this(numerator, denominator.toLong(), true) constructor(numerator: Long, denominator: Int) : this(numerator, denominator.toLong(), true)
public constructor(numerator: Long, denominator: Long) : this(numerator, denominator, true) constructor(numerator: Long, denominator: Long) : this(numerator, denominator, true)
public constructor(numerator: Int) : this(numerator.toLong(), 1L, false) constructor(numerator: Int) : this(numerator.toLong(), 1L, false)
public constructor(numerator: Long) : this(numerator, 1L, false) constructor(numerator: Long) : this(numerator, 1L, false)
public operator fun unaryPlus(): Rational = this operator fun unaryPlus(): Rational = this
public operator fun unaryMinus(): Rational = Rational(-this.numerator, this.denominator) operator fun unaryMinus(): Rational = Rational(-this.numerator, this.denominator)
public operator fun plus(other: Rational): Rational { operator fun plus(other: Rational): Rational {
val denominatorsGcd = gcd(denominator, other.denominator) val denominatorsGcd = gcd(denominator, other.denominator)
val dividedThisDenominator = denominator / denominatorsGcd val dividedThisDenominator = denominator / denominatorsGcd
val dividedOtherDenominator = other.denominator / denominatorsGcd val dividedOtherDenominator = other.denominator / denominatorsGcd
@ -56,19 +56,19 @@ public class Rational {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun plus(other: Int): Rational = operator fun plus(other: Int): Rational =
Rational( Rational(
numerator + denominator * other.toLong(), numerator + denominator * other.toLong(),
denominator, denominator,
toCheckInput = false toCheckInput = false
) )
public operator fun plus(other: Long): Rational = operator fun plus(other: Long): Rational =
Rational( Rational(
numerator + denominator * other, numerator + denominator * other,
denominator, denominator,
toCheckInput = false toCheckInput = false
) )
public operator fun minus(other: Rational): Rational { operator fun minus(other: Rational): Rational {
val denominatorsGcd = gcd(denominator, other.denominator) val denominatorsGcd = gcd(denominator, other.denominator)
val dividedThisDenominator = denominator / denominatorsGcd val dividedThisDenominator = denominator / denominatorsGcd
val dividedOtherDenominator = other.denominator / denominatorsGcd val dividedOtherDenominator = other.denominator / denominatorsGcd
@ -80,19 +80,19 @@ public class Rational {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun minus(other: Int): Rational = operator fun minus(other: Int): Rational =
Rational( Rational(
numerator - denominator * other.toLong(), numerator - denominator * other.toLong(),
denominator, denominator,
toCheckInput = false toCheckInput = false
) )
public operator fun minus(other: Long): Rational = operator fun minus(other: Long): Rational =
Rational( Rational(
numerator - denominator * other, numerator - denominator * other,
denominator, denominator,
toCheckInput = false toCheckInput = false
) )
public operator fun times(other: Rational): Rational { operator fun times(other: Rational): Rational {
val thisDenominatorAndOtherNumeratorGcd = gcd(denominator, other.numerator) val thisDenominatorAndOtherNumeratorGcd = gcd(denominator, other.numerator)
val otherDenominatorAndThisNumeratorGcd = gcd(other.denominator, numerator) val otherDenominatorAndThisNumeratorGcd = gcd(other.denominator, numerator)
return Rational( return Rational(
@ -101,7 +101,7 @@ public class Rational {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun times(other: Int): Rational { operator fun times(other: Int): Rational {
val other = other.toLong() val other = other.toLong()
val denominatorAndOtherGcd = gcd(denominator, other) val denominatorAndOtherGcd = gcd(denominator, other)
return Rational( return Rational(
@ -110,7 +110,7 @@ public class Rational {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun times(other: Long): Rational { operator fun times(other: Long): Rational {
val denominatorAndOtherGcd = gcd(denominator, other) val denominatorAndOtherGcd = gcd(denominator, other)
return Rational( return Rational(
numerator * (other / denominatorAndOtherGcd), numerator * (other / denominatorAndOtherGcd),
@ -118,7 +118,7 @@ public class Rational {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun div(other: Rational): Rational { operator fun div(other: Rational): Rational {
val denominatorsGcd = gcd(denominator, other.denominator) val denominatorsGcd = gcd(denominator, other.denominator)
val numeratorsGcd = gcd(numerator, other.numerator) val numeratorsGcd = gcd(numerator, other.numerator)
return Rational( return Rational(
@ -126,7 +126,7 @@ public class Rational {
(denominator / denominatorsGcd) * (other.numerator / numeratorsGcd) (denominator / denominatorsGcd) * (other.numerator / numeratorsGcd)
) )
} }
public operator fun div(other: Int): Rational { operator fun div(other: Int): Rational {
val other = other.toLong() val other = other.toLong()
val numeratorAndOtherGcd = gcd(numerator, other) val numeratorAndOtherGcd = gcd(numerator, other)
return Rational( return Rational(
@ -135,7 +135,7 @@ public class Rational {
toCheckInput = false toCheckInput = false
) )
} }
public operator fun div(other: Long): Rational { operator fun div(other: Long): Rational {
val numeratorAndOtherGcd = gcd(numerator, other) val numeratorAndOtherGcd = gcd(numerator, other)
return Rational( return Rational(
numerator / numeratorAndOtherGcd, numerator / numeratorAndOtherGcd,
@ -158,7 +158,7 @@ public class Rational {
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE")
@OptIn(UnstableKMathAPI::class) @OptIn(UnstableKMathAPI::class)
public object RationalField : Field<Rational>, NumbersAddOps<Rational> { object RationalField : Field<Rational>, NumbersAddOps<Rational> {
override inline val zero: Rational get() = Rational.ZERO override inline val zero: Rational get() = Rational.ZERO
override inline val one: Rational get() = Rational.ONE override inline val one: Rational get() = Rational.ONE

View File

@ -13,7 +13,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertFailsWith import kotlin.test.assertFailsWith
public fun <T> assertContentEquals( fun <T> assertContentEquals(
expected: Map<T, Double>, expected: Map<T, Double>,
actual: Map<T, Double>, actual: Map<T, Double>,
absoluteTolerance: Double, absoluteTolerance: Double,
@ -23,7 +23,7 @@ public fun <T> assertContentEquals(
for ((key, expectedValue) in expected) assertEquals(expectedValue, actual[key]!!, absoluteTolerance, message) for ((key, expectedValue) in expected) assertEquals(expectedValue, actual[key]!!, absoluteTolerance, message)
} }
public fun assertEquals( fun assertEquals(
expected: NumberedPolynomial<Double>, expected: NumberedPolynomial<Double>,
actual: NumberedPolynomial<Double>, actual: NumberedPolynomial<Double>,
absoluteTolerance: Double, absoluteTolerance: Double,
@ -37,7 +37,7 @@ public fun assertEquals(
) )
} }
public fun assertEquals( fun assertEquals(
expected: LabeledPolynomial<Double>, expected: LabeledPolynomial<Double>,
actual: LabeledPolynomial<Double>, actual: LabeledPolynomial<Double>,
absoluteTolerance: Double, absoluteTolerance: Double,
@ -51,7 +51,7 @@ public fun assertEquals(
) )
} }
public fun assertEquals( fun assertEquals(
expected: NumberedRationalFunction<Double>, expected: NumberedRationalFunction<Double>,
actual: NumberedRationalFunction<Double>, actual: NumberedRationalFunction<Double>,
absoluteTolerance: Double, absoluteTolerance: Double,
@ -71,7 +71,7 @@ public fun assertEquals(
) )
} }
public fun assertEquals( fun assertEquals(
expected: LabeledRationalFunction<Double>, expected: LabeledRationalFunction<Double>,
actual: LabeledRationalFunction<Double>, actual: LabeledRationalFunction<Double>,
absoluteTolerance: Double, absoluteTolerance: Double,
@ -91,7 +91,7 @@ public fun assertEquals(
) )
} }
public inline fun <reified T : Throwable> assertFailsWithTypeAndMessage( inline fun <reified T : Throwable> assertFailsWithTypeAndMessage(
expectedMessage: String? = null, expectedMessage: String? = null,
assertionMessage: String? = null, assertionMessage: String? = null,
block: () -> Unit block: () -> Unit

View File

@ -9,11 +9,11 @@ import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.expressions.symbol import space.kscience.kmath.expressions.symbol
public val o: Rational = Rational(0) val o: Rational = Rational(0)
public val x: Symbol by symbol val x: Symbol by symbol
public val y: Symbol by symbol val y: Symbol by symbol
public val z: Symbol by symbol val z: Symbol by symbol
public val t: Symbol by symbol val t: Symbol by symbol
public val s: Symbol by symbol val s: Symbol by symbol
public val iota: Symbol by symbol val iota: Symbol by symbol

View File

@ -26,9 +26,7 @@ include(
":kmath-core", ":kmath-core",
":kmath-coroutines", ":kmath-coroutines",
":kmath-functions", ":kmath-functions",
":test-utils-functions",
":kmath-polynomial", ":kmath-polynomial",
":test-utils-polynomial",
":kmath-histograms", ":kmath-histograms",
":kmath-commons", ":kmath-commons",
":kmath-viktor", ":kmath-viktor",

View File

@ -1,14 +0,0 @@
plugins {
id("ru.mipt.npm.gradle.mpp")
id("ru.mipt.npm.gradle.native")
}
kotlin.sourceSets {
commonMain {
dependencies {
api(projects.kmathCore)
api(projects.kmathFunctions)
api(kotlin("test"))
}
}
}

View File

@ -1,22 +0,0 @@
/*
* 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.testUtils
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
public inline fun <reified T : Throwable> assertFailsWithTypeAndMessage(
expectedMessage: String? = null,
assertionMessage: String? = null,
block: () -> Unit
) {
assertEquals(
expectedMessage,
assertFailsWith(T::class, assertionMessage, block).message,
assertionMessage
)
}

View File

@ -1,14 +0,0 @@
plugins {
id("ru.mipt.npm.gradle.mpp")
id("ru.mipt.npm.gradle.native")
}
kotlin.sourceSets {
commonMain {
dependencies {
api(projects.kmathCore)
api(projects.kmathPolynomial)
api(kotlin("test"))
}
}
}