Feature: Polynomials and rational functions #469

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

View File

@ -432,12 +432,12 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
coefficients.all { (key, value) -> with(other.coefficients) { key in this && this[key] == value } } coefficients.all { (key, value) -> with(other.coefficients) { key in this && this[key] == value } }
} }
// TODO: Replace `countOfVariables` with `lastVariable` and create new `countOfVariables`
/** /**
* Count of all variables that appear in the polynomial in positive exponents. * Maximal index (ID) of variable occurring in the polynomial with positive power. If there is no such variable,
* the result is `-1`.
*/ */
public val NumberedPolynomial<C>.countOfVariables: Int public val NumberedPolynomial<C>.lastVariable: Int
get() = coefficients.entries.maxOfOrNull { (degs, c) -> if (c.isZero()) 0 else degs.size } ?: 0 get() = coefficients.entries.maxOfOrNull { (degs, c) -> if (c.isZero()) -1 else degs.lastIndex } ?: -1
/** /**
* Degree of the polynomial, [see also](https://en.wikipedia.org/wiki/Degree_of_a_polynomial). If the polynomial is * Degree of the polynomial, [see also](https://en.wikipedia.org/wiki/Degree_of_a_polynomial). If the polynomial is
* zero, degree is -1. * zero, degree is -1.
@ -449,18 +449,30 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
* exponents in which the variables are appeared in the polynomial. * exponents in which the variables are appeared in the polynomial.
* *
* As consequence all values in the list are non-negative integers. Also, if the polynomial is constant, the list is empty. * As consequence all values in the list are non-negative integers. Also, if the polynomial is constant, the list is empty.
* And size of the list is [countOfVariables]. * And last index of the list is [lastVariable].
*/ */
public val NumberedPolynomial<C>.degrees: List<UInt> public val NumberedPolynomial<C>.degrees: List<UInt>
get() = get() =
buildList(countOfVariables) { MutableList(lastVariable + 1) { 0u }.apply {
repeat(countOfVariables) { add(0U) }
coefficients.entries.forEach { (degs, c) -> coefficients.entries.forEach { (degs, c) ->
if (c.isNotZero()) degs.forEachIndexed { index, deg -> if (c.isNotZero()) degs.forEachIndexed { index, deg ->
this[index] = max(this[index], deg) this[index] = max(this[index], deg)
} }
} }
} }
/**
* Count of variables occurring in the polynomial with positive power. If there is no such variable,
* the result is `0`.
*/
public val NumberedPolynomial<C>.countOfVariables: Int
get() =
MutableList(lastVariable + 1) { false }.apply {
coefficients.entries.forEach { (degs, c) ->
if (c.isNotZero()) degs.forEachIndexed { index, deg ->
if (deg != 0u) this[index] = true
}
}
}.count { it }
/** /**
* Checks if the instant is constant polynomial (of degree no more than 0) over considered ring. * Checks if the instant is constant polynomial (of degree no more than 0) over considered ring.

View File

@ -277,12 +277,12 @@ public class NumberedRationalFunctionSpace<C, A: Ring<C>> (
if ( !(numerator.isZero() xor other.numerator.isZero()) ) return false if ( !(numerator.isZero() xor other.numerator.isZero()) ) return false
val countOfVariables = max(this.countOfVariables, other.countOfVariables) val countOfVariables = max(this.lastVariable, other.lastVariable)
val thisNumeratorDegrees = this.numerator.degrees val thisNumeratorDegrees = this.numerator.degrees
val thisDenominatorDegrees = this.denominator.degrees val thisDenominatorDegrees = this.denominator.degrees
val otherNumeratorDegrees = other.numerator.degrees val otherNumeratorDegrees = other.numerator.degrees
val otherDenominatorDegrees = other.denominator.degrees val otherDenominatorDegrees = other.denominator.degrees
for (variable in 0 until countOfVariables) for (variable in 0 .. countOfVariables)
if ( if (
thisNumeratorDegrees.getOrElse(variable) { 0u } + otherDenominatorDegrees.getOrElse(variable) { 0u } thisNumeratorDegrees.getOrElse(variable) { 0u } + otherDenominatorDegrees.getOrElse(variable) { 0u }
!= thisDenominatorDegrees.getOrElse(variable) { 0u } + otherNumeratorDegrees.getOrElse(variable) { 0u } != thisDenominatorDegrees.getOrElse(variable) { 0u } + otherNumeratorDegrees.getOrElse(variable) { 0u }
@ -292,9 +292,10 @@ public class NumberedRationalFunctionSpace<C, A: Ring<C>> (
} }
/** /**
* Count of all variables that appear in the polynomial in positive exponents. * Maximal index (ID) of variable occurring in the polynomial with positive power. If there is no such variable,
* the result is `-1`.
*/ */
public val NumberedPolynomial<C>.countOfVariables: Int get() = polynomialRing { countOfVariables } public val NumberedPolynomial<C>.lastVariable: Int get() = polynomialRing { lastVariable }
/** /**
* List that associates indices of variables (that appear in the polynomial in positive exponents) with their most * List that associates indices of variables (that appear in the polynomial in positive exponents) with their most
* exponents in which the variables are appeared in the polynomial. * exponents in which the variables are appeared in the polynomial.
@ -303,12 +304,35 @@ public class NumberedRationalFunctionSpace<C, A: Ring<C>> (
* And size of the list is [countOfVariables]. * And size of the list is [countOfVariables].
*/ */
public val NumberedPolynomial<C>.degrees: List<UInt> get() = polynomialRing { degrees } public val NumberedPolynomial<C>.degrees: List<UInt> get() = polynomialRing { degrees }
/**
* Count of variables occurring in the polynomial with positive power. If there is no such variable,
* the result is `0`.
*/
public val NumberedPolynomial<C>.countOfVariables: Int get() = polynomialRing { countOfVariables }
/** /**
* Count of all variables that appear in the polynomial in positive exponents. * Count of all variables that appear in the polynomial in positive exponents.
*/ */
public val NumberedRationalFunction<C>.lastVariable: Int
get() = polynomialRing { max(numerator.lastVariable, denominator.lastVariable) }
/**
* Count of variables occurring in the rational function with positive power. If there is no such variable,
* the result is `0`.
*/
public val NumberedRationalFunction<C>.countOfVariables: Int public val NumberedRationalFunction<C>.countOfVariables: Int
get() = polynomialRing { max(numerator.countOfVariables, denominator.countOfVariables) } get() =
MutableList(lastVariable + 1) { false }.apply {
numerator.coefficients.entries.forEach { (degs, c) ->
if (c.isNotZero()) degs.forEachIndexed { index, deg ->
if (deg != 0u) this[index] = true
}
}
denominator.coefficients.entries.forEach { (degs, c) ->
if (c.isNotZero()) degs.forEachIndexed { index, deg ->
if (deg != 0u) this[index] = true
}
}
}.count { it }
// TODO: Разобрать // TODO: Разобрать