diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedPolynomial.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedPolynomial.kt index 307b78f29..37c0c96f0 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedPolynomial.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedPolynomial.kt @@ -432,12 +432,12 @@ public open class NumberedPolynomialSpace>( 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.countOfVariables: Int - get() = coefficients.entries.maxOfOrNull { (degs, c) -> if (c.isZero()) 0 else degs.size } ?: 0 + public val NumberedPolynomial.lastVariable: Int + 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 * zero, degree is -1. @@ -449,18 +449,30 @@ public open class NumberedPolynomialSpace>( * 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. - * And size of the list is [countOfVariables]. + * And last index of the list is [lastVariable]. */ public val NumberedPolynomial.degrees: List get() = - buildList(countOfVariables) { - repeat(countOfVariables) { add(0U) } + MutableList(lastVariable + 1) { 0u }.apply { coefficients.entries.forEach { (degs, c) -> if (c.isNotZero()) degs.forEachIndexed { 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.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. diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedRationalFunction.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedRationalFunction.kt index 8e3b3b1c5..6542bc89b 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedRationalFunction.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedRationalFunction.kt @@ -277,12 +277,12 @@ public class NumberedRationalFunctionSpace> ( 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 thisDenominatorDegrees = this.denominator.degrees val otherNumeratorDegrees = other.numerator.degrees val otherDenominatorDegrees = other.denominator.degrees - for (variable in 0 until countOfVariables) + for (variable in 0 .. countOfVariables) if ( thisNumeratorDegrees.getOrElse(variable) { 0u } + otherDenominatorDegrees.getOrElse(variable) { 0u } != thisDenominatorDegrees.getOrElse(variable) { 0u } + otherNumeratorDegrees.getOrElse(variable) { 0u } @@ -292,9 +292,10 @@ public class NumberedRationalFunctionSpace> ( } /** - * 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.countOfVariables: Int get() = polynomialRing { countOfVariables } + public val NumberedPolynomial.lastVariable: Int get() = polynomialRing { lastVariable } /** * 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. @@ -303,12 +304,35 @@ public class NumberedRationalFunctionSpace> ( * And size of the list is [countOfVariables]. */ public val NumberedPolynomial.degrees: List 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.countOfVariables: Int get() = polynomialRing { countOfVariables } /** * Count of all variables that appear in the polynomial in positive exponents. */ + public val NumberedRationalFunction.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.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: Разобрать