Added some docs

This commit is contained in:
Gleb Minaev 2022-03-15 16:43:22 +03:00
parent 79736a0a9b
commit 1754ae0695
8 changed files with 271 additions and 35 deletions

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].
*
* @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.
*/
@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 A the type of algebraic structure (precisely, of ring) provided for constants.
*/
@Suppress("INAPPLICABLE_JVM_NAME")
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
}
/**
* 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")
public interface AbstractRationalFunctionalSpace<C, P: AbstractPolynomial<C>, R: AbstractRationalFunction<C, P>> : Ring<R> {
// region Constant-integer relation
@ -508,6 +516,15 @@ public interface AbstractRationalFunctionalSpace<C, P: AbstractPolynomial<C>, R:
// 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")
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
}
/**
* 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")
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
/**

View File

@ -384,6 +384,11 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
// endregion
// 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> =
if (other == 0) this
else
@ -399,6 +404,11 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
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> =
if (other == 0) this
else
@ -414,6 +424,11 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
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> =
if (other == 0) zero
else LabeledPolynomial(
@ -425,6 +440,11 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
// endregion
// 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> =
if (this == 0) other
else
@ -440,6 +460,11 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
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> =
if (this == 0) other
else
@ -455,6 +480,11 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
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> =
if (this == 0) zero
else LabeledPolynomial(
@ -514,6 +544,9 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
// endregion
// 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> =
if (this.isZero()) other
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> =
if (this.isZero()) other
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> =
if (this.isZero()) zero
else LabeledPolynomial<C>(
@ -560,7 +599,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
// 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> =
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> =
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> =
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))
/**
* Instance of unit polynomial (unit of the polynomial ring).
*/
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 =
when {
this === other -> true
@ -832,7 +879,10 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
}
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? =
with(coefficients) {
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 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)

View File

@ -267,6 +267,11 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
public final override val ring: A,
) : AbstractPolynomialSpaceOverRing<C, NumberedPolynomial<C>, A> {
// 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> =
if (other == 0) this
else
@ -282,6 +287,11 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
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> =
if (other == 0) this
else
@ -297,6 +307,11 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
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> =
if (other == 0) zero
else NumberedPolynomial(
@ -308,6 +323,11 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
// endregion
// 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> =
if (this == 0) other
else
@ -323,6 +343,11 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
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> =
if (this == 0) other
else
@ -338,6 +363,11 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
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> =
if (this == 0) zero
else NumberedPolynomial(
@ -349,6 +379,9 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
// endregion
// 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> =
if (this.isZero()) other
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> =
if (this.isZero()) -other
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> =
if (this.isZero()) zero
else NumberedPolynomial<C>(
@ -395,7 +434,7 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
// 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> =
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> =
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> =
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() }
/**
* Check if the instant is unit polynomial.
*/
public override fun NumberedPolynomial<C>.isOne(): Boolean =
with(coefficients) {
var foundAbsoluteTermAndItIsOne = false
@ -509,6 +554,9 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
}
foundAbsoluteTermAndItIsOne
}
/**
* Check if the instant is minus unit polynomial.
*/
public override fun NumberedPolynomial<C>.isMinusOne(): Boolean =
with(coefficients) {
var foundAbsoluteTermAndItIsMinusOne = false
@ -522,7 +570,13 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
foundAbsoluteTermAndItIsMinusOne
}
/**
* Instance of zero polynomial (zero of the polynomial ring).
*/
override val zero: NumberedPolynomial<C> = NumberedPolynomial<C>(emptyMap())
/**
* Instance of unit polynomial (unit of the polynomial ring).
*/
override val one: NumberedPolynomial<C> =
NumberedPolynomial<C>(
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 =
when {
this === other -> true
@ -591,7 +647,10 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
}
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? =
with(coefficients) {
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 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)

View File

@ -68,10 +68,15 @@ public fun <T> T.asPolynomial() : Polynomial<T> = Polynomial(listOf(this))
*/
//@Suppress("INAPPLICABLE_JVM_NAME") // TODO: KT-31420
public open class PolynomialSpace<C, A : Ring<C>>(
public final override val ring: A,
public override val ring: A,
) : AbstractPolynomialSpaceOverRing<C, Polynomial<C>, A> {
// 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> =
if (other == 0) this
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> =
if (other == 0) this
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> =
if (other == 0) zero
else Polynomial(
@ -116,6 +131,11 @@ public open class PolynomialSpace<C, A : Ring<C>>(
// endregion
// 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> =
if (this == 0) other
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> =
if (this == 0) other
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> =
if (this == 0) zero
else Polynomial(
@ -162,6 +192,9 @@ public open class PolynomialSpace<C, A : Ring<C>>(
// endregion
// 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> =
if (this.isZero()) other
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> =
if (this.isZero()) other
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> =
if (this.isZero()) other
else Polynomial(
@ -216,6 +249,9 @@ public open class PolynomialSpace<C, A : Ring<C>>(
// endregion
// 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> =
if (other.isZero()) this
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> =
if (other.isZero()) this
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> =
if (other.isZero()) this
else Polynomial(
@ -268,8 +304,14 @@ public open class PolynomialSpace<C, A : Ring<C>>(
// endregion
// region Polynomial-polynomial relation
/**
* Returns negation of the polynomial.
*/
public override operator fun Polynomial<C>.unaryMinus(): Polynomial<C> =
Polynomial(coefficients.map { -it })
/**
* Returns sum of the polynomials.
*/
public override operator fun Polynomial<C>.plus(other: Polynomial<C>): Polynomial<C> =
Polynomial(
(0..max(degree, other.degree))
@ -282,6 +324,9 @@ public open class PolynomialSpace<C, A : Ring<C>>(
}
.ifEmpty { listOf(constantZero) }
)
/**
* Returns difference of the polynomials.
*/
public override operator fun Polynomial<C>.minus(other: Polynomial<C>): Polynomial<C> =
Polynomial(
(0..max(degree, other.degree))
@ -294,6 +339,9 @@ public open class PolynomialSpace<C, A : Ring<C>>(
}
.ifEmpty { listOf(constantZero) }
)
/**
* Returns product of the polynomials.
*/
public override operator fun Polynomial<C>.times(other: Polynomial<C>): Polynomial<C> {
val thisDegree = 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() }
/**
* Check if the instant is unit polynomial.
*/
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 =
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())
/**
* Instance of unit constant (unit of the underlying ring).
*/
override val one: Polynomial<C> = Polynomial(listOf(constantZero))
/**
* Checks equality of the polynomials.
*/
public override infix fun Polynomial<C>.equalsTo(other: Polynomial<C>): Boolean =
when {
this === other -> true
@ -334,9 +406,16 @@ public open class PolynomialSpace<C, A : Ring<C>>(
// endregion
// 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 }
/**
* 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? =
with(coefficients) {
when {
@ -345,7 +424,6 @@ public open class PolynomialSpace<C, A : Ring<C>>(
else -> first()
}
}
public override fun Polynomial<C>.asConstant(): C = asConstantOrNull() ?: error("Can not represent non-constant polynomial as a constant")
@Suppress("NOTHING_TO_INLINE")
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 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)