Feature: Polynomials and rational functions #469

Merged
lounres merged 132 commits from feature/polynomials into dev 2022-07-28 18:04:06 +03:00
8 changed files with 271 additions and 35 deletions
Showing only changes of commit 1754ae0695 - Show all commits

View File

@ -18,7 +18,7 @@ public interface AbstractPolynomial<C>
/** /**
* Abstraction of ring of polynomials of type [P] over ring of constants of type [C]. * Abstraction of ring of polynomials of type [P] over ring of constants of type [C].
* *
* @param C the type of constants. Polynomials have them as a coefficients in their terms. * @param C the type of constants. Polynomials have them as coefficients in their terms.
* @param P the type of polynomials. * @param P the type of polynomials.
*/ */
@Suppress("INAPPLICABLE_JVM_NAME", "PARAMETER_NAME_CHANGED_ON_OVERRIDE") @Suppress("INAPPLICABLE_JVM_NAME", "PARAMETER_NAME_CHANGED_ON_OVERRIDE")
@ -313,10 +313,12 @@ public interface AbstractPolynomialSpace<C, P: AbstractPolynomial<C>> : Ring<P>
} }
/** /**
* Abstraction of ring of polynomials of type [P] over ring of constants of type [C]. * Abstraction of ring of polynomials of type [P] over ring of constants of type [C]. It also assumes that there is
* provided [ring] (of type [A]), that provides constant-wise operations.
* *
* @param C the type of constants. Polynomials have them as a coefficients in their terms. * @param C the type of constants. Polynomials have them as coefficients in their terms.
* @param P the type of polynomials. * @param P the type of polynomials.
* @param A the type of algebraic structure (precisely, of ring) provided for constants.
*/ */
@Suppress("INAPPLICABLE_JVM_NAME") @Suppress("INAPPLICABLE_JVM_NAME")
public interface AbstractPolynomialSpaceOverRing<C, P: AbstractPolynomial<C>, A: Ring<C>> : AbstractPolynomialSpace<C, P> { public interface AbstractPolynomialSpaceOverRing<C, P: AbstractPolynomial<C>, A: Ring<C>> : AbstractPolynomialSpace<C, P> {

View File

@ -20,6 +20,14 @@ public interface AbstractRationalFunction<C, P: AbstractPolynomial<C>> {
public operator fun component2(): P = denominator public operator fun component2(): P = denominator
} }
/**
* Abstraction of field of rational functions of type [R] with respect to polynomials of type [P] and constants of type
* [C].
*
* @param C the type of constants. Polynomials have them as coefficients in their terms.
* @param P the type of polynomials. Rational functions have them as numerators and denominators in them.
* @param R the type of rational functions.
*/ // TODO: Add support of field
@Suppress("INAPPLICABLE_JVM_NAME", "PARAMETER_NAME_CHANGED_ON_OVERRIDE") @Suppress("INAPPLICABLE_JVM_NAME", "PARAMETER_NAME_CHANGED_ON_OVERRIDE")
public interface AbstractRationalFunctionalSpace<C, P: AbstractPolynomial<C>, R: AbstractRationalFunction<C, P>> : Ring<R> { public interface AbstractRationalFunctionalSpace<C, P: AbstractPolynomial<C>, R: AbstractRationalFunction<C, P>> : Ring<R> {
// region Constant-integer relation // region Constant-integer relation
@ -508,6 +516,15 @@ public interface AbstractRationalFunctionalSpace<C, P: AbstractPolynomial<C>, R:
// endregion // endregion
} }
/**
* Abstraction of field of rational functions of type [R] with respect to polynomials of type [P] and constants of type
* [C]. It also assumes that there is provided [ring] (of type [A]), that provides constant-wise operations.
*
* @param C the type of constants. Polynomials have them as coefficients in their terms.
* @param P the type of polynomials. Rational functions have them as numerators and denominators in them.
* @param R the type of rational functions.
* @param A the type of algebraic structure (precisely, of ring) provided for constants.
*/ // TODO: Add support of field
@Suppress("INAPPLICABLE_JVM_NAME") @Suppress("INAPPLICABLE_JVM_NAME")
public interface AbstractRationalFunctionalSpaceOverRing<C, P: AbstractPolynomial<C>, R: AbstractRationalFunction<C, P>, A: Ring<C>> : AbstractRationalFunctionalSpace<C, P, R> { public interface AbstractRationalFunctionalSpaceOverRing<C, P: AbstractPolynomial<C>, R: AbstractRationalFunction<C, P>, A: Ring<C>> : AbstractRationalFunctionalSpace<C, P, R> {
@ -593,10 +610,25 @@ public interface AbstractRationalFunctionalSpaceOverRing<C, P: AbstractPolynomia
// endregion // endregion
} }
/**
* Abstraction of field of rational functions of type [R] with respect to polynomials of type [P] and constants of type
* [C]. It also assumes that there is provided [polynomialRing] (of type [AP]), that provides constant- and
* polynomial-wise operations.
*
* @param C the type of constants. Polynomials have them as coefficients in their terms.
* @param P the type of polynomials. Rational functions have them as numerators and denominators in them.
* @param R the type of rational functions.
* @param AP the type of algebraic structure (precisely, of ring) provided for polynomials.
*/ // TODO: Add support of field
@Suppress("INAPPLICABLE_JVM_NAME") @Suppress("INAPPLICABLE_JVM_NAME")
public interface AbstractRationalFunctionalSpaceOverPolynomialSpace<C, P: AbstractPolynomial<C>, R: AbstractRationalFunction<C, P>, A: Ring<C>> : AbstractRationalFunctionalSpace<C, P, R> { public interface AbstractRationalFunctionalSpaceOverPolynomialSpace<
C,
P: AbstractPolynomial<C>,
R: AbstractRationalFunction<C, P>,
AP: AbstractPolynomialSpace<C, P>,
> : AbstractRationalFunctionalSpace<C, P, R> {
public val polynomialRing: AbstractPolynomialSpace<C, P> public val polynomialRing: AP
// region Constant-integer relation // region Constant-integer relation
/** /**

View File

@ -384,6 +384,11 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
// endregion // endregion
// region Polynomial-integer relation // region Polynomial-integer relation
/**
* Returns sum of the polynomial and the integer represented as polynomial.
*
* The operation is equivalent to adding [other] copies of unit polynomial to [this].
*/
public override operator fun LabeledPolynomial<C>.plus(other: Int): LabeledPolynomial<C> = public override operator fun LabeledPolynomial<C>.plus(other: Int): LabeledPolynomial<C> =
if (other == 0) this if (other == 0) this
else else
@ -399,6 +404,11 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
else this[degs] = result else this[degs] = result
} }
) )
/**
* Returns difference between the polynomial and the integer represented as polynomial.
*
* The operation is equivalent to subtraction [other] copies of unit polynomial from [this].
*/
public override operator fun LabeledPolynomial<C>.minus(other: Int): LabeledPolynomial<C> = public override operator fun LabeledPolynomial<C>.minus(other: Int): LabeledPolynomial<C> =
if (other == 0) this if (other == 0) this
else else
@ -414,6 +424,11 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
else this[degs] = result else this[degs] = result
} }
) )
/**
* Returns product of the polynomial and the integer represented as polynomial.
*
* The operation is equivalent to sum of [other] copies of [this].
*/
public override operator fun LabeledPolynomial<C>.times(other: Int): LabeledPolynomial<C> = public override operator fun LabeledPolynomial<C>.times(other: Int): LabeledPolynomial<C> =
if (other == 0) zero if (other == 0) zero
else LabeledPolynomial( else LabeledPolynomial(
@ -425,6 +440,11 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
// endregion // endregion
// region Integer-polynomial relation // region Integer-polynomial relation
/**
* Returns sum of the integer represented as polynomial and the polynomial.
*
* The operation is equivalent to adding [this] copies of unit polynomial to [other].
*/
public override operator fun Int.plus(other: LabeledPolynomial<C>): LabeledPolynomial<C> = public override operator fun Int.plus(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
if (this == 0) other if (this == 0) other
else else
@ -440,6 +460,11 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
else this[degs] = result else this[degs] = result
} }
) )
/**
* Returns difference between the integer represented as polynomial and the polynomial.
*
* The operation is equivalent to subtraction [this] copies of unit polynomial from [other].
*/
public override operator fun Int.minus(other: LabeledPolynomial<C>): LabeledPolynomial<C> = public override operator fun Int.minus(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
if (this == 0) other if (this == 0) other
else else
@ -455,6 +480,11 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
else this[degs] = result else this[degs] = result
} }
) )
/**
* Returns product of the integer represented as polynomial and the polynomial.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
public override operator fun Int.times(other: LabeledPolynomial<C>): LabeledPolynomial<C> = public override operator fun Int.times(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
if (this == 0) zero if (this == 0) zero
else LabeledPolynomial( else LabeledPolynomial(
@ -514,6 +544,9 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
// endregion // endregion
// region Constant-polynomial relation // region Constant-polynomial relation
/**
* Returns sum of the constant represented as polynomial and the polynomial.
*/
override operator fun C.plus(other: LabeledPolynomial<C>): LabeledPolynomial<C> = override operator fun C.plus(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
if (this.isZero()) other if (this.isZero()) other
else with(other.coefficients) { else with(other.coefficients) {
@ -530,6 +563,9 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
} }
) )
} }
/**
* Returns difference between the constant represented as polynomial and the polynomial.
*/
override operator fun C.minus(other: LabeledPolynomial<C>): LabeledPolynomial<C> = override operator fun C.minus(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
if (this.isZero()) other if (this.isZero()) other
else with(other.coefficients) { else with(other.coefficients) {
@ -548,6 +584,9 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
} }
) )
} }
/**
* Returns product of the constant represented as polynomial and the polynomial.
*/
override operator fun C.times(other: LabeledPolynomial<C>): LabeledPolynomial<C> = override operator fun C.times(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
if (this.isZero()) zero if (this.isZero()) zero
else LabeledPolynomial<C>( else LabeledPolynomial<C>(
@ -560,7 +599,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
// region Polynomial-constant relation // region Polynomial-constant relation
/** /**
* Returns sum of the polynomials. [other] is interpreted as [UnivariatePolynomial]. * Returns sum of the constant represented as polynomial and the polynomial.
*/ */
override operator fun LabeledPolynomial<C>.plus(other: C): LabeledPolynomial<C> = override operator fun LabeledPolynomial<C>.plus(other: C): LabeledPolynomial<C> =
if (other.isZero()) this if (other.isZero()) this
@ -579,7 +618,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
) )
} }
/** /**
* Returns difference of the polynomials. [other] is interpreted as [UnivariatePolynomial]. * Returns difference between the constant represented as polynomial and the polynomial.
*/ */
override operator fun LabeledPolynomial<C>.minus(other: C): LabeledPolynomial<C> = override operator fun LabeledPolynomial<C>.minus(other: C): LabeledPolynomial<C> =
if (other.isZero()) this if (other.isZero()) this
@ -600,7 +639,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
) )
} }
/** /**
* Returns product of the polynomials. [other] is interpreted as [UnivariatePolynomial]. * Returns product of the constant represented as polynomial and the polynomial.
*/ */
override operator fun LabeledPolynomial<C>.times(other: C): LabeledPolynomial<C> = override operator fun LabeledPolynomial<C>.times(other: C): LabeledPolynomial<C> =
if (other.isZero()) zero if (other.isZero()) zero
@ -763,10 +802,18 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
) )
} }
/**
* Instance of zero polynomial (zero of the polynomial ring).
*/
override val zero: LabeledPolynomial<C> = LabeledPolynomial<C>(mapOf(emptyMap<Variable, UInt>() to constantZero)) override val zero: LabeledPolynomial<C> = LabeledPolynomial<C>(mapOf(emptyMap<Variable, UInt>() to constantZero))
/**
* Instance of unit polynomial (unit of the polynomial ring).
*/
override val one: LabeledPolynomial<C> = LabeledPolynomial<C>(mapOf(emptyMap<Variable, UInt>() to constantOne)) override val one: LabeledPolynomial<C> = LabeledPolynomial<C>(mapOf(emptyMap<Variable, UInt>() to constantOne))
// TODO: Docs /**
* Checks equality of the polynomials.
*/
override infix fun LabeledPolynomial<C>.equalsTo(other: LabeledPolynomial<C>): Boolean = override infix fun LabeledPolynomial<C>.equalsTo(other: LabeledPolynomial<C>): Boolean =
when { when {
this === other -> true this === other -> true
@ -832,7 +879,10 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
} }
foundAbsoluteTermAndItIsNotZero foundAbsoluteTermAndItIsNotZero
} }
/**
* If polynomial is a constant polynomial represents and returns it as constant.
* Otherwise, (when the polynomial is not constant polynomial) returns `null`.
*/
override fun LabeledPolynomial<C>.asConstantOrNull(): C? = override fun LabeledPolynomial<C>.asConstantOrNull(): C? =
with(coefficients) { with(coefficients) {
if(isConstant()) getOrElse(emptyMap()) { constantZero } if(isConstant()) getOrElse(emptyMap()) { constantZero }

View File

@ -85,7 +85,12 @@ internal fun labeledRationalFunctionError(message: Any): Nothing = throw Labeled
public class LabeledRationalFunctionSpace<C, A: Ring<C>>( public class LabeledRationalFunctionSpace<C, A: Ring<C>>(
public val ring: A, public val ring: A,
) : AbstractRationalFunctionalSpaceOverPolynomialSpace<C, LabeledPolynomial<C>, LabeledRationalFunction<C>, A> { ) : AbstractRationalFunctionalSpaceOverPolynomialSpace<
C,
LabeledPolynomial<C>,
LabeledRationalFunction<C>,
LabeledPolynomialSpace<C, A>,
> {
override val polynomialRing : LabeledPolynomialSpace<C, A> = LabeledPolynomialSpace(ring) override val polynomialRing : LabeledPolynomialSpace<C, A> = LabeledPolynomialSpace(ring)

View File

@ -267,6 +267,11 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
public final override val ring: A, public final override val ring: A,
) : AbstractPolynomialSpaceOverRing<C, NumberedPolynomial<C>, A> { ) : AbstractPolynomialSpaceOverRing<C, NumberedPolynomial<C>, A> {
// region Polynomial-integer relation // region Polynomial-integer relation
/**
* Returns sum of the polynomial and the integer represented as polynomial.
*
* The operation is equivalent to adding [other] copies of unit polynomial to [this].
*/
public override operator fun NumberedPolynomial<C>.plus(other: Int): NumberedPolynomial<C> = public override operator fun NumberedPolynomial<C>.plus(other: Int): NumberedPolynomial<C> =
if (other == 0) this if (other == 0) this
else else
@ -282,6 +287,11 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
else this[degs] = result else this[degs] = result
} }
) )
/**
* Returns difference between the polynomial and the integer represented as polynomial.
*
* The operation is equivalent to subtraction [other] copies of unit polynomial from [this].
*/
public override operator fun NumberedPolynomial<C>.minus(other: Int): NumberedPolynomial<C> = public override operator fun NumberedPolynomial<C>.minus(other: Int): NumberedPolynomial<C> =
if (other == 0) this if (other == 0) this
else else
@ -297,6 +307,11 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
else this[degs] = result else this[degs] = result
} }
) )
/**
* Returns product of the polynomial and the integer represented as polynomial.
*
* The operation is equivalent to sum of [other] copies of [this].
*/
public override operator fun NumberedPolynomial<C>.times(other: Int): NumberedPolynomial<C> = public override operator fun NumberedPolynomial<C>.times(other: Int): NumberedPolynomial<C> =
if (other == 0) zero if (other == 0) zero
else NumberedPolynomial( else NumberedPolynomial(
@ -308,6 +323,11 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
// endregion // endregion
// region Integer-polynomial relation // region Integer-polynomial relation
/**
* Returns sum of the integer represented as polynomial and the polynomial.
*
* The operation is equivalent to adding [this] copies of unit polynomial to [other].
*/
public override operator fun Int.plus(other: NumberedPolynomial<C>): NumberedPolynomial<C> = public override operator fun Int.plus(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
if (this == 0) other if (this == 0) other
else else
@ -323,6 +343,11 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
else this[degs] = result else this[degs] = result
} }
) )
/**
* Returns difference between the integer represented as polynomial and the polynomial.
*
* The operation is equivalent to subtraction [this] copies of unit polynomial from [other].
*/
public override operator fun Int.minus(other: NumberedPolynomial<C>): NumberedPolynomial<C> = public override operator fun Int.minus(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
if (this == 0) other if (this == 0) other
else else
@ -338,6 +363,11 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
else this[degs] = result else this[degs] = result
} }
) )
/**
* Returns product of the integer represented as polynomial and the polynomial.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
public override operator fun Int.times(other: NumberedPolynomial<C>): NumberedPolynomial<C> = public override operator fun Int.times(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
if (this == 0) zero if (this == 0) zero
else NumberedPolynomial( else NumberedPolynomial(
@ -349,6 +379,9 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
// endregion // endregion
// region Constant-polynomial relation // region Constant-polynomial relation
/**
* Returns sum of the constant represented as polynomial and the polynomial.
*/
override operator fun C.plus(other: NumberedPolynomial<C>): NumberedPolynomial<C> = override operator fun C.plus(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
if (this.isZero()) other if (this.isZero()) other
else with(other.coefficients) { else with(other.coefficients) {
@ -365,6 +398,9 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
} }
) )
} }
/**
* Returns difference between the constant represented as polynomial and the polynomial.
*/
override operator fun C.minus(other: NumberedPolynomial<C>): NumberedPolynomial<C> = override operator fun C.minus(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
if (this.isZero()) -other if (this.isZero()) -other
else with(other.coefficients) { else with(other.coefficients) {
@ -383,6 +419,9 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
} }
) )
} }
/**
* Returns product of the constant represented as polynomial and the polynomial.
*/
override operator fun C.times(other: NumberedPolynomial<C>): NumberedPolynomial<C> = override operator fun C.times(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
if (this.isZero()) zero if (this.isZero()) zero
else NumberedPolynomial<C>( else NumberedPolynomial<C>(
@ -395,7 +434,7 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
// region Polynomial-constant relation // region Polynomial-constant relation
/** /**
* Returns sum of the polynomials. [other] is interpreted as [NumberedPolynomial]. * Returns sum of the constant represented as polynomial and the polynomial.
*/ */
override operator fun NumberedPolynomial<C>.plus(other: C): NumberedPolynomial<C> = override operator fun NumberedPolynomial<C>.plus(other: C): NumberedPolynomial<C> =
if (other.isZero()) this if (other.isZero()) this
@ -414,7 +453,7 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
) )
} }
/** /**
* Returns difference of the polynomials. [other] is interpreted as [NumberedPolynomial]. * Returns difference between the constant represented as polynomial and the polynomial.
*/ */
override operator fun NumberedPolynomial<C>.minus(other: C): NumberedPolynomial<C> = override operator fun NumberedPolynomial<C>.minus(other: C): NumberedPolynomial<C> =
if (other.isZero()) this if (other.isZero()) this
@ -433,7 +472,7 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
) )
} }
/** /**
* Returns product of the polynomials. [other] is interpreted as [NumberedPolynomial]. * Returns product of the constant represented as polynomial and the polynomial.
*/ */
override operator fun NumberedPolynomial<C>.times(other: C): NumberedPolynomial<C> = override operator fun NumberedPolynomial<C>.times(other: C): NumberedPolynomial<C> =
if (other.isZero()) zero if (other.isZero()) zero
@ -496,7 +535,13 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
) )
} }
/**
* Check if the instant is zero polynomial.
*/
public override fun NumberedPolynomial<C>.isZero(): Boolean = coefficients.values.all { it.isZero() } public override fun NumberedPolynomial<C>.isZero(): Boolean = coefficients.values.all { it.isZero() }
/**
* Check if the instant is unit polynomial.
*/
public override fun NumberedPolynomial<C>.isOne(): Boolean = public override fun NumberedPolynomial<C>.isOne(): Boolean =
with(coefficients) { with(coefficients) {
var foundAbsoluteTermAndItIsOne = false var foundAbsoluteTermAndItIsOne = false
@ -509,6 +554,9 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
} }
foundAbsoluteTermAndItIsOne foundAbsoluteTermAndItIsOne
} }
/**
* Check if the instant is minus unit polynomial.
*/
public override fun NumberedPolynomial<C>.isMinusOne(): Boolean = public override fun NumberedPolynomial<C>.isMinusOne(): Boolean =
with(coefficients) { with(coefficients) {
var foundAbsoluteTermAndItIsMinusOne = false var foundAbsoluteTermAndItIsMinusOne = false
@ -522,7 +570,13 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
foundAbsoluteTermAndItIsMinusOne foundAbsoluteTermAndItIsMinusOne
} }
/**
* Instance of zero polynomial (zero of the polynomial ring).
*/
override val zero: NumberedPolynomial<C> = NumberedPolynomial<C>(emptyMap()) override val zero: NumberedPolynomial<C> = NumberedPolynomial<C>(emptyMap())
/**
* Instance of unit polynomial (unit of the polynomial ring).
*/
override val one: NumberedPolynomial<C> = override val one: NumberedPolynomial<C> =
NumberedPolynomial<C>( NumberedPolynomial<C>(
mapOf( mapOf(
@ -530,7 +584,9 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
) )
) )
// TODO: Docs /**
* Checks equality of the polynomials.
*/
override infix fun NumberedPolynomial<C>.equalsTo(other: NumberedPolynomial<C>): Boolean = override infix fun NumberedPolynomial<C>.equalsTo(other: NumberedPolynomial<C>): Boolean =
when { when {
this === other -> true this === other -> true
@ -591,7 +647,10 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
} }
foundAbsoluteTermAndItIsNotZero foundAbsoluteTermAndItIsNotZero
} }
/**
* If polynomial is a constant polynomial represents and returns it as constant.
* Otherwise, (when the polynomial is not constant polynomial) returns `null`.
*/
override fun NumberedPolynomial<C>.asConstantOrNull(): C? = override fun NumberedPolynomial<C>.asConstantOrNull(): C? =
with(coefficients) { with(coefficients) {
if(isConstant()) getOrElse(emptyList()) { constantZero } if(isConstant()) getOrElse(emptyList()) { constantZero }

View File

@ -82,7 +82,12 @@ internal fun numberedRationalFunctionError(message: Any): Nothing = throw Number
public class NumberedRationalFunctionSpace<C, A: Ring<C>> ( public class NumberedRationalFunctionSpace<C, A: Ring<C>> (
public val ring: A, public val ring: A,
) : AbstractRationalFunctionalSpaceOverPolynomialSpace<C, NumberedPolynomial<C>, NumberedRationalFunction<C>, A> { ) : AbstractRationalFunctionalSpaceOverPolynomialSpace<
C,
NumberedPolynomial<C>,
NumberedRationalFunction<C>,
NumberedPolynomialSpace<C, A>,
> {
override val polynomialRing : NumberedPolynomialSpace<C, A> = NumberedPolynomialSpace(ring) override val polynomialRing : NumberedPolynomialSpace<C, A> = NumberedPolynomialSpace(ring)

View File

@ -68,10 +68,15 @@ public fun <T> T.asPolynomial() : Polynomial<T> = Polynomial(listOf(this))
*/ */
//@Suppress("INAPPLICABLE_JVM_NAME") // TODO: KT-31420 //@Suppress("INAPPLICABLE_JVM_NAME") // TODO: KT-31420
public open class PolynomialSpace<C, A : Ring<C>>( public open class PolynomialSpace<C, A : Ring<C>>(
public final override val ring: A, public override val ring: A,
) : AbstractPolynomialSpaceOverRing<C, Polynomial<C>, A> { ) : AbstractPolynomialSpaceOverRing<C, Polynomial<C>, A> {
// region Polynomial-integer relation // region Polynomial-integer relation
/**
* Returns sum of the polynomial and the integer represented as polynomial.
*
* The operation is equivalent to adding [other] copies of unit polynomial to [this].
*/
public override operator fun Polynomial<C>.plus(other: Int): Polynomial<C> = public override operator fun Polynomial<C>.plus(other: Int): Polynomial<C> =
if (other == 0) this if (other == 0) this
else else
@ -89,6 +94,11 @@ public open class PolynomialSpace<C, A : Ring<C>>(
} }
} }
) )
/**
* Returns difference between the polynomial and the integer represented as polynomial.
*
* The operation is equivalent to subtraction [other] copies of unit polynomial from [this].
*/
public override operator fun Polynomial<C>.minus(other: Int): Polynomial<C> = public override operator fun Polynomial<C>.minus(other: Int): Polynomial<C> =
if (other == 0) this if (other == 0) this
else else
@ -106,6 +116,11 @@ public open class PolynomialSpace<C, A : Ring<C>>(
} }
} }
) )
/**
* Returns product of the polynomial and the integer represented as polynomial.
*
* The operation is equivalent to sum of [other] copies of [this].
*/
public override operator fun Polynomial<C>.times(other: Int): Polynomial<C> = public override operator fun Polynomial<C>.times(other: Int): Polynomial<C> =
if (other == 0) zero if (other == 0) zero
else Polynomial( else Polynomial(
@ -116,6 +131,11 @@ public open class PolynomialSpace<C, A : Ring<C>>(
// endregion // endregion
// region Integer-polynomial relation // region Integer-polynomial relation
/**
* Returns sum of the integer represented as polynomial and the polynomial.
*
* The operation is equivalent to adding [this] copies of unit polynomial to [other].
*/
public override operator fun Int.plus(other: Polynomial<C>): Polynomial<C> = public override operator fun Int.plus(other: Polynomial<C>): Polynomial<C> =
if (this == 0) other if (this == 0) other
else else
@ -133,6 +153,11 @@ public open class PolynomialSpace<C, A : Ring<C>>(
} }
} }
) )
/**
* Returns difference between the integer represented as polynomial and the polynomial.
*
* The operation is equivalent to subtraction [this] copies of unit polynomial from [other].
*/
public override operator fun Int.minus(other: Polynomial<C>): Polynomial<C> = public override operator fun Int.minus(other: Polynomial<C>): Polynomial<C> =
if (this == 0) other if (this == 0) other
else else
@ -152,6 +177,11 @@ public open class PolynomialSpace<C, A : Ring<C>>(
} }
} }
) )
/**
* Returns product of the integer represented as polynomial and the polynomial.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
public override operator fun Int.times(other: Polynomial<C>): Polynomial<C> = public override operator fun Int.times(other: Polynomial<C>): Polynomial<C> =
if (this == 0) zero if (this == 0) zero
else Polynomial( else Polynomial(
@ -162,6 +192,9 @@ public open class PolynomialSpace<C, A : Ring<C>>(
// endregion // endregion
// region Constant-polynomial relation // region Constant-polynomial relation
/**
* Returns sum of the constant represented as polynomial and the polynomial.
*/
public override operator fun C.plus(other: Polynomial<C>): Polynomial<C> = public override operator fun C.plus(other: Polynomial<C>): Polynomial<C> =
if (this.isZero()) other if (this.isZero()) other
else with(other.coefficients) { else with(other.coefficients) {
@ -180,9 +213,9 @@ public open class PolynomialSpace<C, A : Ring<C>>(
} }
) )
} }
// if (degree == -1) UnivariatePolynomial(other) else UnivariatePolynomial( /**
// listOf(coefficients[0] + other) + coefficients.subList(1, degree + 1) * Returns difference between the constant represented as polynomial and the polynomial.
// ) */
public override operator fun C.minus(other: Polynomial<C>): Polynomial<C> = public override operator fun C.minus(other: Polynomial<C>): Polynomial<C> =
if (this.isZero()) other if (this.isZero()) other
else with(other.coefficients) { else with(other.coefficients) {
@ -203,9 +236,9 @@ public open class PolynomialSpace<C, A : Ring<C>>(
} }
) )
} }
// if (degree == -1) UnivariatePolynomial(other) else UnivariatePolynomial( /**
// listOf(coefficients[0] + other) + coefficients.subList(1, degree + 1) * Returns product of the constant represented as polynomial and the polynomial.
// ) */
public override operator fun C.times(other: Polynomial<C>): Polynomial<C> = public override operator fun C.times(other: Polynomial<C>): Polynomial<C> =
if (this.isZero()) other if (this.isZero()) other
else Polynomial( else Polynomial(
@ -216,6 +249,9 @@ public open class PolynomialSpace<C, A : Ring<C>>(
// endregion // endregion
// region Polynomial-constant relation // region Polynomial-constant relation
/**
* Returns sum of the constant represented as polynomial and the polynomial.
*/
public override operator fun Polynomial<C>.plus(other: C): Polynomial<C> = public override operator fun Polynomial<C>.plus(other: C): Polynomial<C> =
if (other.isZero()) this if (other.isZero()) this
else with(coefficients) { else with(coefficients) {
@ -234,9 +270,9 @@ public open class PolynomialSpace<C, A : Ring<C>>(
} }
) )
} }
// if (degree == -1) UnivariatePolynomial(other) else UnivariatePolynomial( /**
// listOf(coefficients[0] + other) + coefficients.subList(1, degree + 1) * Returns difference between the constant represented as polynomial and the polynomial.
// ) */
public override operator fun Polynomial<C>.minus(other: C): Polynomial<C> = public override operator fun Polynomial<C>.minus(other: C): Polynomial<C> =
if (other.isZero()) this if (other.isZero()) this
else with(coefficients) { else with(coefficients) {
@ -255,9 +291,9 @@ public open class PolynomialSpace<C, A : Ring<C>>(
} }
) )
} }
// if (degree == -1) UnivariatePolynomial(-other) else UnivariatePolynomial( /**
// listOf(coefficients[0] - other) + coefficients.subList(1, degree + 1) * Returns product of the constant represented as polynomial and the polynomial.
// ) */
public override operator fun Polynomial<C>.times(other: C): Polynomial<C> = public override operator fun Polynomial<C>.times(other: C): Polynomial<C> =
if (other.isZero()) this if (other.isZero()) this
else Polynomial( else Polynomial(
@ -268,8 +304,14 @@ public open class PolynomialSpace<C, A : Ring<C>>(
// endregion // endregion
// region Polynomial-polynomial relation // region Polynomial-polynomial relation
/**
* Returns negation of the polynomial.
*/
public override operator fun Polynomial<C>.unaryMinus(): Polynomial<C> = public override operator fun Polynomial<C>.unaryMinus(): Polynomial<C> =
Polynomial(coefficients.map { -it }) Polynomial(coefficients.map { -it })
/**
* Returns sum of the polynomials.
*/
public override operator fun Polynomial<C>.plus(other: Polynomial<C>): Polynomial<C> = public override operator fun Polynomial<C>.plus(other: Polynomial<C>): Polynomial<C> =
Polynomial( Polynomial(
(0..max(degree, other.degree)) (0..max(degree, other.degree))
@ -282,6 +324,9 @@ public open class PolynomialSpace<C, A : Ring<C>>(
} }
.ifEmpty { listOf(constantZero) } .ifEmpty { listOf(constantZero) }
) )
/**
* Returns difference of the polynomials.
*/
public override operator fun Polynomial<C>.minus(other: Polynomial<C>): Polynomial<C> = public override operator fun Polynomial<C>.minus(other: Polynomial<C>): Polynomial<C> =
Polynomial( Polynomial(
(0..max(degree, other.degree)) (0..max(degree, other.degree))
@ -294,6 +339,9 @@ public open class PolynomialSpace<C, A : Ring<C>>(
} }
.ifEmpty { listOf(constantZero) } .ifEmpty { listOf(constantZero) }
) )
/**
* Returns product of the polynomials.
*/
public override operator fun Polynomial<C>.times(other: Polynomial<C>): Polynomial<C> { public override operator fun Polynomial<C>.times(other: Polynomial<C>): Polynomial<C> {
val thisDegree = degree val thisDegree = degree
val otherDegree = other.degree val otherDegree = other.degree
@ -313,15 +361,39 @@ public open class PolynomialSpace<C, A : Ring<C>>(
} }
} }
/**
* Check if the instant is zero polynomial.
*/
public override fun Polynomial<C>.isZero(): Boolean = coefficients.all { it.isZero() } public override fun Polynomial<C>.isZero(): Boolean = coefficients.all { it.isZero() }
/**
* Check if the instant is unit polynomial.
*/
public override fun Polynomial<C>.isOne(): Boolean = public override fun Polynomial<C>.isOne(): Boolean =
with(coefficients) { isNotEmpty() && asSequence().withIndex().any { (index, c) -> if (index == 0) c.isOne() else c.isZero() } } // TODO: It's better to write new methods like `anyIndexed`. But what's better way to do it? with(coefficients) {
isNotEmpty() &&
asSequence().withIndex().any { (index, c) -> if (index == 0) c.isOne() else c.isZero() } // TODO: It's better to write new methods like `anyIndexed`. But what's better way to do it?
}
/**
* Check if the instant is minus unit polynomial.
*/
public override fun Polynomial<C>.isMinusOne(): Boolean = public override fun Polynomial<C>.isMinusOne(): Boolean =
with(coefficients) { isNotEmpty() && asSequence().withIndex().any { (index, c) -> if (index == 0) c.isMinusOne() else c.isZero() } } // TODO: It's better to write new methods like `anyIndexed`. But what's better way to do it? with(coefficients) {
isNotEmpty() &&
asSequence().withIndex().any { (index, c) -> if (index == 0) c.isMinusOne() else c.isZero() } // TODO: It's better to write new methods like `anyIndexed`. But what's better way to do it?
}
/**
* Instance of zero polynomial (zero of the polynomial ring).
*/
override val zero: Polynomial<C> = Polynomial(emptyList()) override val zero: Polynomial<C> = Polynomial(emptyList())
/**
* Instance of unit constant (unit of the underlying ring).
*/
override val one: Polynomial<C> = Polynomial(listOf(constantZero)) override val one: Polynomial<C> = Polynomial(listOf(constantZero))
/**
* Checks equality of the polynomials.
*/
public override infix fun Polynomial<C>.equalsTo(other: Polynomial<C>): Boolean = public override infix fun Polynomial<C>.equalsTo(other: Polynomial<C>): Boolean =
when { when {
this === other -> true this === other -> true
@ -334,9 +406,16 @@ public open class PolynomialSpace<C, A : Ring<C>>(
// endregion // endregion
// region Polynomial properties // region Polynomial properties
/**
* Degree of the polynomial, [see also](https://en.wikipedia.org/wiki/Degree_of_a_polynomial). If the polynomial is
* zero, degree is -1.
*/
public override val Polynomial<C>.degree: Int get() = coefficients.indexOfLast { it != constantZero } public override val Polynomial<C>.degree: Int get() = coefficients.indexOfLast { it != constantZero }
/**
* If polynomial is a constant polynomial represents and returns it as constant.
* Otherwise, (when the polynomial is not constant polynomial) returns `null`.
*/
public override fun Polynomial<C>.asConstantOrNull(): C? = public override fun Polynomial<C>.asConstantOrNull(): C? =
with(coefficients) { with(coefficients) {
when { when {
@ -345,7 +424,6 @@ public open class PolynomialSpace<C, A : Ring<C>>(
else -> first() else -> first()
} }
} }
public override fun Polynomial<C>.asConstant(): C = asConstantOrNull() ?: error("Can not represent non-constant polynomial as a constant")
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
public inline fun Polynomial<C>.substitute(argument: C): C = this.substitute(ring, argument) public inline fun Polynomial<C>.substitute(argument: C): C = this.substitute(ring, argument)

View File

@ -67,7 +67,12 @@ internal fun rationalFunctionError(message: Any): Nothing = throw RationalFuncti
public class RationalFunctionSpace<C, A : Ring<C>> ( public class RationalFunctionSpace<C, A : Ring<C>> (
public val ring: A, public val ring: A,
) : AbstractRationalFunctionalSpaceOverPolynomialSpace<C, Polynomial<C>, RationalFunction<C>, A> { ) : AbstractRationalFunctionalSpaceOverPolynomialSpace<
C,
Polynomial<C>,
RationalFunction<C>,
PolynomialSpace<C, A>,
> {
override val polynomialRing : PolynomialSpace<C, A> = PolynomialSpace(ring) override val polynomialRing : PolynomialSpace<C, A> = PolynomialSpace(ring)