Feature: Polynomials and rational functions #469

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

View File

@ -29,21 +29,18 @@ public interface AbstractPolynomialSpace<C, P: AbstractPolynomial<C>> : Ring<P>
*
* The operation is equivalent to adding [other] copies of unit of underlying ring to [this].
*/
@JvmName("constantIntPlus")
public operator fun C.plus(other: Int): C
/**
* Returns difference between the constant and the integer represented as constant (member of underlying ring).
*
* The operation is equivalent to subtraction [other] copies of unit of underlying ring from [this].
*/
@JvmName("constantIntMinus")
public operator fun C.minus(other: Int): C
/**
* Returns product of the constant and the integer represented as constant (member of underlying ring).
*
* The operation is equivalent to sum of [other] copies of [this].
*/
@JvmName("constantIntTimes")
public operator fun C.times(other: Int): C
// endregion
@ -53,21 +50,18 @@ public interface AbstractPolynomialSpace<C, P: AbstractPolynomial<C>> : Ring<P>
*
* The operation is equivalent to adding [this] copies of unit of underlying ring to [other].
*/
@JvmName("intConstantPlus")
public operator fun Int.plus(other: C): C
/**
* Returns difference between the integer represented as constant (member of underlying ring) and the constant.
*
* The operation is equivalent to subtraction [this] copies of unit of underlying ring from [other].
*/
@JvmName("intConstantMinus")
public operator fun Int.minus(other: C): C
/**
* Returns product of the integer represented as constant (member of underlying ring) and the constant.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
@JvmName("intConstantTimes")
public operator fun Int.times(other: C): C
// endregion
@ -148,32 +142,26 @@ public interface AbstractPolynomialSpace<C, P: AbstractPolynomial<C>> : Ring<P>
/**
* Check if the instant is zero constant.
*/
@JvmName("constantIsZero")
public fun C.isZero(): Boolean = this == constantZero
/**
* Check if the instant is NOT zero constant.
*/
@JvmName("constantIsNotZero")
public fun C.isNotZero(): Boolean = !isZero()
/**
* Check if the instant is unit constant.
*/
@JvmName("constantIsOne")
public fun C.isOne(): Boolean = this == constantOne
/**
* Check if the instant is NOT unit constant.
*/
@JvmName("constantIsNotOne")
public fun C.isNotOne(): Boolean = !isOne()
/**
* Check if the instant is minus unit constant.
*/
@JvmName("constantIsMinusOne")
public fun C.isMinusOne(): Boolean = this == -constantOne
/**
* Check if the instant is NOT minus unit constant.
*/
@JvmName("constantIsNotMinusOne")
public fun C.isNotMinusOne(): Boolean = !isMinusOne()
/**
@ -330,7 +318,7 @@ public interface AbstractPolynomialSpace<C, P: AbstractPolynomial<C>> : Ring<P>
* @param C the type of constants. Polynomials have them as a coefficients in their terms.
* @param P the type of polynomials.
*/
@Suppress("INAPPLICABLE_JVM_NAME", "PARAMETER_NAME_CHANGED_ON_OVERRIDE")
@Suppress("INAPPLICABLE_JVM_NAME")
public interface AbstractPolynomialSpaceOverRing<C, P: AbstractPolynomial<C>, A: Ring<C>> : AbstractPolynomialSpace<C, P> {
public val ring: A
@ -341,21 +329,18 @@ public interface AbstractPolynomialSpaceOverRing<C, P: AbstractPolynomial<C>, A:
*
* The operation is equivalent to adding [other] copies of unit of underlying ring to [this].
*/
@JvmName("constantIntPlus")
public override operator fun C.plus(other: Int): C = ring { optimizedAddMultiplied(this@plus, one, other) }
/**
* Returns difference between the constant and the integer represented as constant (member of underlying ring).
*
* The operation is equivalent to subtraction [other] copies of unit of underlying ring from [this].
*/
@JvmName("constantIntMinus")
public override operator fun C.minus(other: Int): C = ring { optimizedAddMultiplied(this@minus, one, -other) }
/**
* Returns product of the constant and the integer represented as constant (member of underlying ring).
*
* The operation is equivalent to sum of [other] copies of [this].
*/
@JvmName("constantIntTimes")
public override operator fun C.times(other: Int): C = ring { optimizedMultiply(this@times, other) }
// endregion
@ -365,21 +350,18 @@ public interface AbstractPolynomialSpaceOverRing<C, P: AbstractPolynomial<C>, A:
*
* The operation is equivalent to adding [this] copies of unit of underlying ring to [other].
*/
@JvmName("intConstantPlus")
public override operator fun Int.plus(other: C): C = ring { optimizedAddMultiplied(other, one, this@plus) }
/**
* Returns difference between the integer represented as constant (member of underlying ring) and the constant.
*
* The operation is equivalent to subtraction [this] copies of unit of underlying ring from [other].
*/
@JvmName("intConstantMinus")
public override operator fun Int.minus(other: C): C = ring { optimizedAddMultiplied(-other, one, this@minus) }
/**
* Returns product of the integer represented as constant (member of underlying ring) and the constant.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
@JvmName("intConstantTimes")
public override operator fun Int.times(other: C): C = ring { optimizedMultiply(other, this@times) }
// endregion

View File

@ -28,21 +28,18 @@ public interface AbstractRationalFunctionalSpace<C, P: AbstractPolynomial<C>, R:
*
* The operation is equivalent to adding [other] copies of unit of underlying ring to [this].
*/
@JvmName("constantIntPlus")
public operator fun C.plus(other: Int): C
/**
* Returns difference between the constant and the integer represented as constant (member of underlying ring).
*
* The operation is equivalent to subtraction [other] copies of unit of underlying ring from [this].
*/
@JvmName("constantIntMinus")
public operator fun C.minus(other: Int): C
/**
* Returns product of the constant and the integer represented as constant (member of underlying ring).
*
* The operation is equivalent to sum of [other] copies of [this].
*/
@JvmName("constantIntTimes")
public operator fun C.times(other: Int): C
// endregion
@ -52,21 +49,18 @@ public interface AbstractRationalFunctionalSpace<C, P: AbstractPolynomial<C>, R:
*
* The operation is equivalent to adding [this] copies of unit of underlying ring to [other].
*/
@JvmName("intConstantPlus")
public operator fun Int.plus(other: C): C
/**
* Returns difference between the integer represented as constant (member of underlying ring) and the constant.
*
* The operation is equivalent to subtraction [this] copies of unit of underlying ring from [other].
*/
@JvmName("intConstantMinus")
public operator fun Int.minus(other: C): C
/**
* Returns product of the integer represented as constant (member of underlying ring) and the constant.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
@JvmName("intConstantTimes")
public operator fun Int.times(other: C): C
// endregion
@ -189,32 +183,26 @@ public interface AbstractRationalFunctionalSpace<C, P: AbstractPolynomial<C>, R:
/**
* Check if the instant is zero constant.
*/
@JvmName("constantIsZero")
public fun C.isZero(): Boolean = this == constantZero
/**
* Check if the instant is NOT zero constant.
*/
@JvmName("constantIsNotZero")
public fun C.isNotZero(): Boolean = !isZero()
/**
* Check if the instant is unit constant.
*/
@JvmName("constantIsOne")
public fun C.isOne(): Boolean = this == constantOne
/**
* Check if the instant is NOT unit constant.
*/
@JvmName("constantIsNotOne")
public fun C.isNotOne(): Boolean = !isOne()
/**
* Check if the instant is minus unit constant.
*/
@JvmName("constantIsMinusOne")
public fun C.isMinusOne(): Boolean = this == -constantOne
/**
* Check if the instant is NOT minus unit constant.
*/
@JvmName("constantIsNotMinusOne")
public fun C.isNotMinusOne(): Boolean = !isMinusOne()
/**
@ -520,7 +508,7 @@ public interface AbstractRationalFunctionalSpace<C, P: AbstractPolynomial<C>, R:
// endregion
}
@Suppress("INAPPLICABLE_JVM_NAME", "PARAMETER_NAME_CHANGED_ON_OVERRIDE")
@Suppress("INAPPLICABLE_JVM_NAME")
public interface AbstractRationalFunctionalSpaceOverRing<C, P: AbstractPolynomial<C>, R: AbstractRationalFunction<C, P>, A: Ring<C>> : AbstractRationalFunctionalSpace<C, P, R> {
public val ring: A
@ -531,21 +519,18 @@ public interface AbstractRationalFunctionalSpaceOverRing<C, P: AbstractPolynomia
*
* The operation is equivalent to adding [other] copies of unit of underlying ring to [this].
*/
@JvmName("constantIntPlus")
public override operator fun C.plus(other: Int): C = ring { optimizedAddMultiplied(this@plus, one, other) }
/**
* Returns difference between the constant and the integer represented as constant (member of underlying ring).
*
* The operation is equivalent to subtraction [other] copies of unit of underlying ring from [this].
*/
@JvmName("constantIntMinus")
public override operator fun C.minus(other: Int): C = ring { optimizedAddMultiplied(this@minus, one, -other) }
/**
* Returns product of the constant and the integer represented as constant (member of underlying ring).
*
* The operation is equivalent to sum of [other] copies of [this].
*/
@JvmName("constantIntTimes")
public override operator fun C.times(other: Int): C = ring { optimizedMultiply(this@times, other) }
// endregion
@ -555,21 +540,18 @@ public interface AbstractRationalFunctionalSpaceOverRing<C, P: AbstractPolynomia
*
* The operation is equivalent to adding [this] copies of unit of underlying ring to [other].
*/
@JvmName("intConstantPlus")
public override operator fun Int.plus(other: C): C = ring { optimizedAddMultiplied(other, one, this@plus) }
/**
* Returns difference between the integer represented as constant (member of underlying ring) and the constant.
*
* The operation is equivalent to subtraction [this] copies of unit of underlying ring from [other].
*/
@JvmName("intConstantMinus")
public override operator fun Int.minus(other: C): C = ring { optimizedAddMultiplied(-other, one, this@minus) }
/**
* Returns product of the integer represented as constant (member of underlying ring) and the constant.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
@JvmName("intConstantTimes")
public override operator fun Int.times(other: C): C = ring { optimizedMultiply(other, this@times) }
// endregion
@ -611,7 +593,7 @@ public interface AbstractRationalFunctionalSpaceOverRing<C, P: AbstractPolynomia
// endregion
}
@Suppress("INAPPLICABLE_JVM_NAME", "PARAMETER_NAME_CHANGED_ON_OVERRIDE")
@Suppress("INAPPLICABLE_JVM_NAME")
public interface AbstractRationalFunctionalSpaceOverPolynomialSpace<C, P: AbstractPolynomial<C>, R: AbstractRationalFunction<C, P>, A: Ring<C>> : AbstractRationalFunctionalSpace<C, P, R> {
public val polynomialRing: AbstractPolynomialSpace<C, P>
@ -622,21 +604,18 @@ public interface AbstractRationalFunctionalSpaceOverPolynomialSpace<C, P: Abstra
*
* The operation is equivalent to adding [other] copies of unit of underlying ring to [this].
*/
@JvmName("constantIntPlus")
public override operator fun C.plus(other: Int): C = polynomialRing { this@plus + other }
/**
* Returns difference between the constant and the integer represented as constant (member of underlying ring).
*
* The operation is equivalent to subtraction [other] copies of unit of underlying ring from [this].
*/
@JvmName("constantIntMinus")
public override operator fun C.minus(other: Int): C = polynomialRing { this@minus - other }
/**
* Returns product of the constant and the integer represented as constant (member of underlying ring).
*
* The operation is equivalent to sum of [other] copies of [this].
*/
@JvmName("constantIntTimes")
public override operator fun C.times(other: Int): C = polynomialRing { this@times * other }
// endregion
@ -646,21 +625,18 @@ public interface AbstractRationalFunctionalSpaceOverPolynomialSpace<C, P: Abstra
*
* The operation is equivalent to adding [this] copies of unit of underlying ring to [other].
*/
@JvmName("intConstantPlus")
public override operator fun Int.plus(other: C): C = polynomialRing { this@plus + other }
/**
* Returns difference between the integer represented as constant (member of underlying ring) and the constant.
*
* The operation is equivalent to subtraction [this] copies of unit of underlying ring from [other].
*/
@JvmName("intConstantMinus")
public override operator fun Int.minus(other: C): C = polynomialRing { this@minus - other }
/**
* Returns product of the integer represented as constant (member of underlying ring) and the constant.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
@JvmName("intConstantTimes")
public override operator fun Int.times(other: C): C = polynomialRing { this@times * other }
// endregion
@ -736,32 +712,26 @@ public interface AbstractRationalFunctionalSpaceOverPolynomialSpace<C, P: Abstra
/**
* Check if the instant is zero constant.
*/
@JvmName("constantIsZero")
public override fun C.isZero(): Boolean = polynomialRing { this@isZero.isZero() }
/**
* Check if the instant is NOT zero constant.
*/
@JvmName("constantIsNotZero")
public override fun C.isNotZero(): Boolean = polynomialRing { this@isNotZero.isNotZero() }
/**
* Check if the instant is unit constant.
*/
@JvmName("constantIsOne")
public override fun C.isOne(): Boolean = polynomialRing { this@isOne.isOne() }
/**
* Check if the instant is NOT unit constant.
*/
@JvmName("constantIsNotOne")
public override fun C.isNotOne(): Boolean = polynomialRing { this@isNotOne.isNotOne() }
/**
* Check if the instant is minus unit constant.
*/
@JvmName("constantIsMinusOne")
public override fun C.isMinusOne(): Boolean = polynomialRing { this@isMinusOne.isMinusOne() }
/**
* Check if the instant is NOT minus unit constant.
*/
@JvmName("constantIsNotMinusOne")
public override fun C.isNotMinusOne(): Boolean = polynomialRing { this@isNotMinusOne.isNotMinusOne() }
/**

View File

@ -331,7 +331,6 @@ internal fun Map<Variable, UInt>.cleanUp() = filterValues { it > 0U }
* @param A the intersection of [Ring] of [C] and [ScaleOperations] of [C].
* @param ring the [A] instance.
*/
@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE", "INAPPLICABLE_JVM_NAME")
public class LabeledPolynomialSpace<C, A : Ring<C>>(
public override val ring: A,
) : AbstractPolynomialSpaceOverRing<C, LabeledPolynomial<C>, A> {
@ -768,7 +767,6 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
override val one: LabeledPolynomial<C> = LabeledPolynomial<C>(mapOf(emptyMap<Variable, UInt>() to constantOne))
// TODO: Docs
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "CovariantEquals")
override infix fun LabeledPolynomial<C>.equalsTo(other: LabeledPolynomial<C>): Boolean =
when {
this === other -> true

View File

@ -305,7 +305,6 @@ public class LabeledRationalFunctionSpace<C, A: Ring<C>>(
/**
* Checks equality of the rational functions.
*/
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "CovariantEquals")
public override infix fun LabeledRationalFunction<C>.equalsTo(other: LabeledRationalFunction<C>): Boolean {
if (this === other) return true

View File

@ -263,7 +263,6 @@ public fun <C, A: Ring<C>> C.asNumberedPolynomial() : NumberedPolynomial<C> = Nu
* @param A the intersection of [Ring] of [C] and [ScaleOperations] of [C].
* @param ring the [A] instance.
*/
@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE", "INAPPLICABLE_JVM_NAME")
public open class NumberedPolynomialSpace<C, A : Ring<C>>(
public final override val ring: A,
) : AbstractPolynomialSpaceOverRing<C, NumberedPolynomial<C>, A> {
@ -532,7 +531,6 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
)
// TODO: Docs
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "CovariantEquals")
override infix fun NumberedPolynomial<C>.equalsTo(other: NumberedPolynomial<C>): Boolean =
when {
this === other -> true

View File

@ -302,7 +302,6 @@ public class NumberedRationalFunctionSpace<C, A: Ring<C>> (
/**
* Checks equality of the rational functions.
*/
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "CovariantEquals")
public override infix fun NumberedRationalFunction<C>.equalsTo(other: NumberedRationalFunction<C>): Boolean {
if (this === other) return true

View File

@ -66,7 +66,7 @@ public fun <T> T.asPolynomial() : Polynomial<T> = Polynomial(listOf(this))
* @param A type of underlying ring of constants. It's [Ring] of [C].
* @param ring underlying ring of constants of type [A].
*/
@Suppress("INAPPLICABLE_JVM_NAME") // TODO: KT-31420
//@Suppress("INAPPLICABLE_JVM_NAME") // TODO: KT-31420
public open class PolynomialSpace<C, A : Ring<C>>(
public final override val ring: A,
) : AbstractPolynomialSpaceOverRing<C, Polynomial<C>, A> {
@ -322,7 +322,6 @@ public open class PolynomialSpace<C, A : Ring<C>>(
override val zero: Polynomial<C> = Polynomial(emptyList())
override val one: Polynomial<C> = Polynomial(listOf(constantZero))
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "CovariantEquals")
public override infix fun Polynomial<C>.equalsTo(other: Polynomial<C>): Boolean =
when {
this === other -> true

View File

@ -287,7 +287,6 @@ public class RationalFunctionSpace<C, A : Ring<C>> (
/**
* Checks equality of the rational functions.
*/
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "CovariantEquals")
public override infix fun RationalFunction<C>.equalsTo(other: RationalFunction<C>): Boolean =
when {
this === other -> true

View File

@ -6,6 +6,7 @@ import kotlin.contracts.*
import kotlin.jvm.JvmName
// TODO: Docs
// region Sort of legacy
@ -485,8 +486,8 @@ public fun NumberedPolynomial<Double>.substitute(args: Map<Int, Double>): Number
val deg = degs.getOrElse(variable) { 0u }
if (deg == 0u) product else product * substitutor.pow(deg.toInt())
}
if (newDegs !in acc) acc[newDegs] = c
else acc[newDegs] = acc[newDegs]!! + c
if (newDegs !in acc) acc[newDegs] = newC
else acc[newDegs] = acc[newDegs]!! + newC
}
return NumberedPolynomial<Double>(acc)
}
@ -504,8 +505,8 @@ public fun <C> NumberedPolynomial<C>.substitute(ring: Ring<C>, args: Map<Int, C>
val deg = degs.getOrElse(variable) { 0u }
if (deg == 0u) product else product * power(substitutor, deg)
}
if (newDegs !in acc) acc[newDegs] = c
else acc[newDegs] = acc[newDegs]!! + c
if (newDegs !in acc) acc[newDegs] = newC
else acc[newDegs] = acc[newDegs]!! + newC
}
return NumberedPolynomial<C>(acc)
}