Added division to RFs' Spaces. Added conversion to polynomial and RFs' spaces. Added requirements for RFs' denominators' changes for case of non-integral domain. Added requirements for non-zero divisor to RFs' divisions.

This commit is contained in:
Gleb Minaev 2022-03-22 13:58:29 +03:00
parent 09868f090b
commit 2f9e504357
7 changed files with 250 additions and 61 deletions

View File

@ -273,9 +273,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
/** /**
* Converts the integer [value] to polynomial. * Converts the integer [value] to polynomial.
*/ */
public override fun number(value: Int): LabeledPolynomial<C> = public override fun number(value: Int): LabeledPolynomial<C> = number(constantNumber(value))
if (value == 0) zero
else LabeledPolynomial(mapOf(emptyMap<Symbol, UInt>() to constantNumber(value)))
public operator fun C.plus(other: Symbol): LabeledPolynomial<C> = public operator fun C.plus(other: Symbol): LabeledPolynomial<C> =
if (isZero()) LabeledPolynomial<C>(mapOf( if (isZero()) LabeledPolynomial<C>(mapOf(
@ -425,6 +423,13 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
} }
) )
/**
* Converts the constant [value] to polynomial.
*/
public override fun number(value: C): LabeledPolynomial<C> =
if (value == 0) zero
else LabeledPolynomial(mapOf(emptyMap<Symbol, UInt>() to value))
public operator fun Symbol.plus(other: Symbol): LabeledPolynomial<C> = public operator fun Symbol.plus(other: Symbol): LabeledPolynomial<C> =
if (this == other) LabeledPolynomial<C>(mapOf( if (this == other) LabeledPolynomial<C>(mapOf(
mapOf(this to 1U) to constantOne * 2 mapOf(this to 1U) to constantOne * 2

View File

@ -144,24 +144,6 @@ public class LabeledRationalFunctionSpace<C, A: Ring<C>>(
// TODO: Разобрать // TODO: Разобрать
public operator fun LabeledRationalFunction<C>.div(other: LabeledRationalFunction<C>): LabeledRationalFunction<C> =
LabeledRationalFunction(
numerator * other.denominator,
denominator * other.numerator
)
public operator fun LabeledRationalFunction<C>.div(other: LabeledPolynomial<C>): LabeledRationalFunction<C> =
LabeledRationalFunction(
numerator,
denominator * other
)
public operator fun LabeledRationalFunction<C>.div(other: C): LabeledRationalFunction<C> =
LabeledRationalFunction(
numerator,
denominator * other
)
// operator fun invoke(arg: Map<Symbol, C>): LabeledRationalFunction<C> = // operator fun invoke(arg: Map<Symbol, C>): LabeledRationalFunction<C> =
// LabeledRationalFunction( // LabeledRationalFunction(
// numerator(arg), // numerator(arg),

View File

@ -66,7 +66,7 @@ public fun <C> ListPolynomial(coefficients: List<C>, reverse: Boolean = false):
public fun <C> ListPolynomial(vararg coefficients: C, reverse: Boolean = false): ListPolynomial<C> = public fun <C> ListPolynomial(vararg coefficients: C, reverse: Boolean = false): ListPolynomial<C> =
ListPolynomial(with(coefficients) { if (reverse) reversed() else toList() }) ListPolynomial(with(coefficients) { if (reverse) reversed() else toList() })
public fun <C> C.asPolynomial() : ListPolynomial<C> = ListPolynomial(listOf(this)) public fun <C> C.asListPolynomial() : ListPolynomial<C> = ListPolynomial(listOf(this))
/** /**
* Space of univariate polynomials constructed over ring. * Space of univariate polynomials constructed over ring.
@ -199,9 +199,7 @@ public open class ListPolynomialSpace<C, A : Ring<C>>(
/** /**
* Converts the integer [value] to polynomial. * Converts the integer [value] to polynomial.
*/ */
public override fun number(value: Int): ListPolynomial<C> = public override fun number(value: Int): ListPolynomial<C> = number(constantNumber(value))
if (value == 0) zero
else ListPolynomial(constantNumber(value))
/** /**
* Returns sum of the constant represented as polynomial and the polynomial. * Returns sum of the constant represented as polynomial and the polynomial.
@ -313,6 +311,13 @@ public open class ListPolynomialSpace<C, A : Ring<C>>(
} }
) )
/**
* Converts the constant [value] to polynomial.
*/
public override fun number(value: C): ListPolynomial<C> =
if (value.isZero()) zero
else ListPolynomial(value)
/** /**
* Returns negation of the polynomial. * Returns negation of the polynomial.
*/ */

View File

@ -70,30 +70,6 @@ public class ListRationalFunctionSpace<C, A : Ring<C>> (
// TODO: Разобрать // TODO: Разобрать
public operator fun ListRationalFunction<C>.div(other: ListRationalFunction<C>): ListRationalFunction<C> =
ListRationalFunction(
numerator * other.denominator,
denominator * other.numerator
)
public operator fun ListRationalFunction<C>.div(other: ListPolynomial<C>): ListRationalFunction<C> =
ListRationalFunction(
numerator,
denominator * other
)
public operator fun ListRationalFunction<C>.div(other: C): ListRationalFunction<C> =
ListRationalFunction(
numerator,
denominator * other
)
public operator fun ListRationalFunction<C>.div(other: Int): ListRationalFunction<C> =
ListRationalFunction(
numerator,
denominator * other
)
// operator fun invoke(arg: UnivariatePolynomial<T>): RationalFunction<T> = // operator fun invoke(arg: UnivariatePolynomial<T>): RationalFunction<T> =
// RationalFunction( // RationalFunction(
// numerator(arg), // numerator(arg),

View File

@ -225,9 +225,7 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
/** /**
* Converts the integer [value] to polynomial. * Converts the integer [value] to polynomial.
*/ */
public override fun number(value: Int): NumberedPolynomial<C> = public override fun number(value: Int): NumberedPolynomial<C> = number(constantNumber(value))
if (value == 0) zero
else NumberedPolynomial(mapOf(emptyList<UInt>() to constantNumber(value)))
/** /**
* Returns sum of the constant represented as polynomial and the polynomial. * Returns sum of the constant represented as polynomial and the polynomial.
@ -331,6 +329,13 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
} }
) )
/**
* Converts the constant [value] to polynomial.
*/
public override fun number(value: C): NumberedPolynomial<C> =
if (value == 0) zero
else NumberedPolynomial(mapOf(emptyList<UInt>() to value))
/** /**
* Returns negation of the polynomial. * Returns negation of the polynomial.
*/ */

View File

@ -65,6 +65,10 @@ public interface PolynomialSpace<C, P: Polynomial<C>> : Ring<P> {
* Converts the integer [value] to constant. * Converts the integer [value] to constant.
*/ */
public fun constantNumber(value: Int): C = constantOne * value public fun constantNumber(value: Int): C = constantOne * value
/**
* Converts the integer to constant.
*/
public fun Int.asConstant(): C = constantNumber(this)
/** /**
* Returns sum of the polynomial and the integer represented as polynomial. * Returns sum of the polynomial and the integer represented as polynomial.
@ -108,6 +112,10 @@ public interface PolynomialSpace<C, P: Polynomial<C>> : Ring<P> {
* Converts the integer [value] to polynomial. * Converts the integer [value] to polynomial.
*/ */
public fun number(value: Int): P = one * value public fun number(value: Int): P = one * value
/**
* Converts the integer to polynomial.
*/
public fun Int.asPolynomial(): P = number(this)
/** /**
* Returns the same constant. * Returns the same constant.
@ -206,6 +214,15 @@ public interface PolynomialSpace<C, P: Polynomial<C>> : Ring<P> {
*/ */
public operator fun P.times(other: C): P public operator fun P.times(other: C): P
/**
* Converts the constant [value] to polynomial.
*/
public fun number(value: C): P = one * value
/**
* Converts the constant to polynomial.
*/
public fun C.asPolynomial(): P = number(this)
/** /**
* Returns the same polynomial. * Returns the same polynomial.
*/ */

View File

@ -72,6 +72,10 @@ public interface RationalFunctionalSpace<C, P: Polynomial<C>, R: RationalFunctio
* Converts the integer [value] to constant. * Converts the integer [value] to constant.
*/ */
public fun constantNumber(value: Int): C = constantOne * value public fun constantNumber(value: Int): C = constantOne * value
/**
* Converts the integer to constant.
*/
public fun Int.asConstant(): C = constantNumber(this)
/** /**
* Returns sum of the constant and the integer represented as polynomial. * Returns sum of the constant and the integer represented as polynomial.
@ -115,6 +119,10 @@ public interface RationalFunctionalSpace<C, P: Polynomial<C>, R: RationalFunctio
* Converts the integer [value] to polynomial. * Converts the integer [value] to polynomial.
*/ */
public fun polynomialNumber(value: Int): P = polynomialOne * value public fun polynomialNumber(value: Int): P = polynomialOne * value
/**
* Converts the integer to polynomial.
*/
public fun Int.asPolynomial(): P = polynomialNumber(this)
/** /**
* Returns sum of the rational function and the integer represented as rational function. * Returns sum of the rational function and the integer represented as rational function.
@ -134,6 +142,13 @@ public interface RationalFunctionalSpace<C, P: Polynomial<C>, R: RationalFunctio
* The operation is equivalent to sum of [other] copies of [this]. * The operation is equivalent to sum of [other] copies of [this].
*/ */
public operator fun R.times(other: Int): R = multiplyBySquaring(this, other) public operator fun R.times(other: Int): R = multiplyBySquaring(this, other)
/**
* Returns quotient of the rational function and the integer represented as rational function.
*
* The operation is equivalent to creating a new rational function by preserving numerator of [this] and
* multiplication denominator of [this] to [other].
*/
public operator fun R.div(other: Int): R = this / multiplyBySquaring(one, other)
/** /**
* Returns sum of the integer represented as rational function and the rational function. * Returns sum of the integer represented as rational function and the rational function.
@ -153,11 +168,22 @@ public interface RationalFunctionalSpace<C, P: Polynomial<C>, R: RationalFunctio
* The operation is equivalent to sum of [this] copies of [other]. * The operation is equivalent to sum of [this] copies of [other].
*/ */
public operator fun Int.times(other: R): R = multiplyBySquaring(other, this) public operator fun Int.times(other: R): R = multiplyBySquaring(other, this)
/**
* Returns quotient of the integer represented as rational function and the rational function.
*
* The operation is equivalent to creating a new rational function which numerator is [this] times denominator of
* [other] and which denominator is [other]'s numerator.
*/
public operator fun Int.div(other: R): R = multiplyBySquaring(one / other, this)
/** /**
* Converts the integer [value] to rational function. * Converts the integer [value] to rational function.
*/ */
public fun number(value: Int): R = one * value public fun number(value: Int): R = one * value
/**
* Converts the integer to rational function.
*/
public fun Int.asRationalFunction(): R = number(this)
/** /**
* Returns the same constant. * Returns the same constant.
@ -256,6 +282,15 @@ public interface RationalFunctionalSpace<C, P: Polynomial<C>, R: RationalFunctio
*/ */
public operator fun P.times(other: C): P public operator fun P.times(other: C): P
/**
* Converts the constant [value] to polynomial.
*/
public fun polynomialNumber(value: C): P = polynomialOne * value
/**
* Converts the constant to polynomial.
*/
public fun C.asPolynomial(): P = polynomialNumber(this)
/** /**
* Returns the same polynomial. * Returns the same polynomial.
*/ */
@ -276,6 +311,10 @@ public interface RationalFunctionalSpace<C, P: Polynomial<C>, R: RationalFunctio
* Returns product of the polynomials. * Returns product of the polynomials.
*/ */
public operator fun P.times(other: P): P public operator fun P.times(other: P): P
/**
* Returns quotient of the polynomials as rational function.
*/
public operator fun P.div(other: P): R
/** /**
* Raises [arg] to the integer power [exponent]. * Raises [arg] to the integer power [exponent].
*/ */
@ -336,19 +375,36 @@ public interface RationalFunctionalSpace<C, P: Polynomial<C>, R: RationalFunctio
* Returns product of the constant represented as polynomial and the rational function. * Returns product of the constant represented as polynomial and the rational function.
*/ */
public operator fun C.times(other: R): R public operator fun C.times(other: R): R
/**
* Returns quotient of the constant represented as polynomial and the rational function.
*/
public operator fun C.div(other: R): R
/** /**
* Returns sum of the constant represented as rational function and the rational function. * Returns sum of the rational function and the constant represented as rational function.
*/ */
public operator fun R.plus(other: C): R public operator fun R.plus(other: C): R
/** /**
* Returns difference between the constant represented as rational function and the rational function. * Returns difference between the rational function and the constant represented as rational function.
*/ */
public operator fun R.minus(other: C): R public operator fun R.minus(other: C): R
/** /**
* Returns product of the constant represented as rational function and the rational function. * Returns product of the rational function and the constant represented as rational function.
*/ */
public operator fun R.times(other: C): R public operator fun R.times(other: C): R
/**
* Returns quotient of the rational function and the constant represented as rational function.
*/
public operator fun R.div(other: C): R
/**
* Converts the constant [value] to rational function.
*/
public fun number(value: C): R = one * value
/**
* Converts the constant to rational function.
*/
public fun C.asRationalFunction(): R = number(this)
/** /**
* Returns sum of the polynomial represented as rational function and the rational function. * Returns sum of the polynomial represented as rational function and the rational function.
@ -362,19 +418,36 @@ public interface RationalFunctionalSpace<C, P: Polynomial<C>, R: RationalFunctio
* Returns product of the polynomial represented as polynomial and the rational function. * Returns product of the polynomial represented as polynomial and the rational function.
*/ */
public operator fun P.times(other: R): R public operator fun P.times(other: R): R
/**
* Returns quotient of the polynomial represented as polynomial and the rational function.
*/
public operator fun P.div(other: R): R
/** /**
* Returns sum of the polynomial represented as rational function and the rational function. * Returns sum of the rational function and the polynomial represented as rational function.
*/ */
public operator fun R.plus(other: P): R public operator fun R.plus(other: P): R
/** /**
* Returns difference between the polynomial represented as rational function and the rational function. * Returns difference between the rational function and the polynomial represented as rational function.
*/ */
public operator fun R.minus(other: P): R public operator fun R.minus(other: P): R
/** /**
* Returns product of the polynomial represented as rational function and the rational function. * Returns product of the rational function and the polynomial represented as rational function.
*/ */
public operator fun R.times(other: P): R public operator fun R.times(other: P): R
/**
* Returns quotient of the rational function and the polynomial represented as rational function.
*/
public operator fun R.div(other: P): R
/**
* Converts the polynomial [value] to rational function.
*/
public fun number(value: P): R = one * value
/**
* Converts the polynomial to rational function.
*/
public fun P.asRationalFunction(): R = number(this)
/** /**
* Returns the same rational function. * Returns the same rational function.
@ -396,6 +469,10 @@ public interface RationalFunctionalSpace<C, P: Polynomial<C>, R: RationalFunctio
* Returns product of the rational functions. * Returns product of the rational functions.
*/ */
public override operator fun R.times(other: R): R public override operator fun R.times(other: R): R
/**
* Returns quotient of the rational functions.
*/
public operator fun R.div(other: R): R
/** /**
* Raises [arg] to the integer power [exponent]. * Raises [arg] to the integer power [exponent].
*/ */
@ -649,6 +726,15 @@ public interface RationalFunctionalSpaceOverPolynomialSpace<
*/ */
public override operator fun Int.times(other: C): C = polynomialRing { this@times * other } public override operator fun Int.times(other: C): C = polynomialRing { this@times * other }
/**
* Converts the integer [value] to constant.
*/
public override fun constantNumber(value: Int): C = polynomialRing { constantNumber(value) }
/**
* Converts the integer to constant.
*/
override fun Int.asConstant(): C = polynomialRing { asConstant() }
/** /**
* Returns sum of the constant and the integer represented as polynomial. * Returns sum of the constant and the integer represented as polynomial.
* *
@ -691,6 +777,10 @@ public interface RationalFunctionalSpaceOverPolynomialSpace<
* Converts the integer [value] to polynomial. * Converts the integer [value] to polynomial.
*/ */
public override fun polynomialNumber(value: Int): P = polynomialRing { number(value) } public override fun polynomialNumber(value: Int): P = polynomialRing { number(value) }
/**
* Converts the integer to polynomial.
*/
public override fun Int.asPolynomial(): P = polynomialRing { asPolynomial() }
/** /**
* Returns the same constant. * Returns the same constant.
@ -783,6 +873,15 @@ public interface RationalFunctionalSpaceOverPolynomialSpace<
*/ */
public override operator fun P.times(other: C): P = polynomialRing { this@times * other } public override operator fun P.times(other: C): P = polynomialRing { this@times * other }
/**
* Converts the constant [value] to polynomial.
*/
public override fun polynomialNumber(value: C): P = polynomialRing { number(value) }
/**
* Converts the constant to polynomial.
*/
public override fun C.asPolynomial(): P = polynomialRing { asPolynomial() }
/** /**
* Returns the same polynomial. * Returns the same polynomial.
*/ */
@ -933,6 +1032,19 @@ public abstract class PolynomialSpaceOfFractions<
denominator denominator
) )
public override operator fun R.div(other: Int): R {
val otherAsConstant = constantNumber(other)
require(otherAsConstant.isNotZero()) { "/ by zero." }
return constructRationalFunction(
numerator,
(denominator * other).also {
check(it.isNotZero()) {
"Got zero denominator during division of rational functions to constant. It means underlying ring of polynomials is not integral domain."
}
}
)
}
/** /**
* Returns sum of the integer represented as rational function and the rational function. * Returns sum of the integer represented as rational function and the rational function.
* *
@ -964,11 +1076,24 @@ public abstract class PolynomialSpaceOfFractions<
other.denominator other.denominator
) )
public override operator fun Int.div(other: R): R {
require(other.numerator.isNotZero()) { "/ by zero." }
return constructRationalFunction(
this * other.denominator,
other.numerator
)
}
/** /**
* Converts the integer [value] to rational function. * Converts the integer [value] to rational function.
*/ */
public override fun number(value: Int): R = constructRationalFunction(polynomialNumber(value)) public override fun number(value: Int): R = constructRationalFunction(polynomialNumber(value))
/**
* Returns quotient of the polynomials as rational function.
*/
public override operator fun P.div(other: P): R = constructRationalFunction(this, other)
/** /**
* Returns sum of the constant represented as rational function and the rational function. * Returns sum of the constant represented as rational function and the rational function.
*/ */
@ -994,6 +1119,14 @@ public abstract class PolynomialSpaceOfFractions<
other.denominator other.denominator
) )
public override operator fun C.div(other: R): R {
require(other.numerator.isNotZero()) { "/ by zero." }
return constructRationalFunction(
this * other.denominator,
other.numerator
)
}
/** /**
* Returns sum of the constant represented as rational function and the rational function. * Returns sum of the constant represented as rational function and the rational function.
*/ */
@ -1019,6 +1152,23 @@ public abstract class PolynomialSpaceOfFractions<
denominator denominator
) )
public override operator fun R.div(other: C): R {
require(other.isNotZero()) { "/ by zero." }
return constructRationalFunction(
numerator,
(denominator * other).also {
check(it.isNotZero()) {
"Got zero denominator during division of rational functions to constant. It means underlying ring of polynomials is not integral domain."
}
}
)
}
/**
* Converts the constant [value] to rational function.
*/
public override fun number(value: C): R = constructRationalFunction(polynomialNumber(value))
/** /**
* Returns sum of the polynomial represented as rational function and the rational function. * Returns sum of the polynomial represented as rational function and the rational function.
*/ */
@ -1044,6 +1194,14 @@ public abstract class PolynomialSpaceOfFractions<
other.denominator other.denominator
) )
public override operator fun P.div(other: R): R {
require(other.numerator.isNotZero()) { "/ by zero." }
return constructRationalFunction(
this * other.denominator,
other.numerator
)
}
/** /**
* Returns sum of the polynomial represented as rational function and the rational function. * Returns sum of the polynomial represented as rational function and the rational function.
*/ */
@ -1069,6 +1227,23 @@ public abstract class PolynomialSpaceOfFractions<
denominator denominator
) )
public override operator fun R.div(other: P): R {
require(other.isNotZero()) { "/ by zero." }
return constructRationalFunction(
numerator,
(denominator * other).also {
require(it.isNotZero()) {
"Got zero denominator during division of rational functions to polynomial. It means underlying ring of polynomials is not integral domain."
}
}
)
}
/**
* Converts the polynomial [value] to rational function.
*/
public override fun number(value: P): R = constructRationalFunction(value)
/** /**
* Returns negation of the rational function. * Returns negation of the rational function.
*/ */
@ -1079,7 +1254,11 @@ public abstract class PolynomialSpaceOfFractions<
public override operator fun R.plus(other: R): R = public override operator fun R.plus(other: R): R =
constructRationalFunction( constructRationalFunction(
numerator * other.denominator + denominator * other.numerator, numerator * other.denominator + denominator * other.numerator,
denominator * other.denominator (denominator * other.denominator).also {
check(it.isNotZero()) {
"Got zero denominator during addition of rational functions. It means underlying ring of polynomials is not integral domain."
}
}
) )
/** /**
* Returns difference of the rational functions. * Returns difference of the rational functions.
@ -1087,7 +1266,11 @@ public abstract class PolynomialSpaceOfFractions<
public override operator fun R.minus(other: R): R = public override operator fun R.minus(other: R): R =
constructRationalFunction( constructRationalFunction(
numerator * other.denominator - denominator * other.numerator, numerator * other.denominator - denominator * other.numerator,
denominator * other.denominator (denominator * other.denominator).also {
check(it.isNotZero()) {
"Got zero denominator during subtraction of rational functions. It means underlying ring of polynomials is not integral domain."
}
}
) )
/** /**
* Returns product of the rational functions. * Returns product of the rational functions.
@ -1095,9 +1278,25 @@ public abstract class PolynomialSpaceOfFractions<
public override operator fun R.times(other: R): R = public override operator fun R.times(other: R): R =
constructRationalFunction( constructRationalFunction(
numerator * other.numerator, numerator * other.numerator,
denominator * other.denominator (denominator * other.denominator).also {
check(it.isNotZero()) {
"Got zero denominator during multiplication of rational functions. It means underlying ring of polynomials is not integral domain."
}
}
) )
public override operator fun R.div(other: R): R {
require(other.isNotZero()) { "/ by zero." }
return constructRationalFunction(
numerator * other.denominator,
(denominator * other.numerator).also {
check(it.isNotZero()) {
"Got zero denominator during division of rational functions. It means underlying ring of polynomials is not integral domain."
}
}
)
}
/** /**
* Instance of zero rational function (zero of the rational functions ring). * Instance of zero rational function (zero of the rational functions ring).
*/ */