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 54 additions and 25 deletions
Showing only changes of commit 87aeda84d9 - Show all commits

View File

@ -19,6 +19,10 @@ kotlin.sourceSets {
} }
} }
dependencies {
dokkaPlugin("org.jetbrains.dokka:mathjax-plugin:${versionCatalogs.named("npmlibs").findVersion("dokka").get().requiredVersion}")
}
readme { readme {
maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL
propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md")) propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md"))

View File

@ -23,8 +23,10 @@ import kotlin.math.min
@JvmInline @JvmInline
public value class Polynomial<C>( public value class Polynomial<C>(
/** /**
* List that contains coefficients of the polynomial. Every monomial `a x^d` is stored as a coefficient `a` placed * List that contains coefficients of the polynomial.
* into the list at index `d`. For example, coefficients of a polynomial `5 x^2 - 6` can be represented as *
* Every monomial \(a x^d\) is stored as a coefficient \(a\) placed
* into the list at index \(d\). For example, coefficients of a polynomial \(5 x^2 - 6\) can be represented as
* ``` * ```
* listOf( * listOf(
* -6, // -6 + * -6, // -6 +
@ -42,9 +44,11 @@ public value class Polynomial<C>(
* 0, // 0 x^4 * 0, // 0 x^4
* ) * )
* ``` * ```
* It is not prohibited to put extra zeros at end of the list (as for `0x^3` and `0x^4` in the example). But the * It is not prohibited to put extra zeros at end of the list (as for \(0x^3\) and \(0x^4\) in the example). But the
* longer the coefficients list the worse performance of arithmetical operations performed on it. Thus, it is * longer the coefficients list the worse performance of arithmetical operations performed on it. Thus, it is
* recommended not to put (or even to remove) extra (or useless) coefficients at the end of the coefficients list. * recommended not to put (or even to remove) extra (or useless) coefficients at the end of the coefficients list.
*
* @usesMathJax
*/ */
public val coefficients: List<C> public val coefficients: List<C>
) { ) {

View File

@ -20,6 +20,10 @@ kotlin.sourceSets {
} }
} }
dependencies {
dokkaPlugin("org.jetbrains.dokka:mathjax-plugin:${versionCatalogs.named("npmlibs").findVersion("dokka").get().requiredVersion}")
}
readme { readme {
maturity = ru.mipt.npm.gradle.Maturity.PROTOTYPE maturity = ru.mipt.npm.gradle.Maturity.PROTOTYPE
propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md")) propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md"))

View File

@ -25,9 +25,9 @@ internal constructor(
/** /**
* Map that contains coefficients of the polynomial. * Map that contains coefficients of the polynomial.
* *
* Every monomial `a x_1^{d_1} ... x_n^{d_n}` is stored as a pair "key-value" in the map, where the value is the * Every monomial \(a x_1^{d_1} ... x_n^{d_n}\) is stored as a pair "key-value" in the map, where the value is the
* coefficient `a` and the key is a map that associates variables in the monomial with their degree in the monomial. * coefficient \(a\) and the key is a map that associates variables in the monomial with their degree in the monomial.
* For example, coefficients of a polynomial `5 a^2 c^3 - 6 b` can be represented as * For example, coefficients of a polynomial \(5 a^2 c^3 - 6 b\) can be represented as
* ``` * ```
* mapOf( * mapOf(
* mapOf( * mapOf(
@ -55,7 +55,12 @@ internal constructor(
* ) to 0 * ) to 0
* ) * )
* ``` * ```
* where `a`, `b` and `c` are corresponding [Symbol] objects. * where \(a\), \(b\) and \(c\) are corresponding [Symbol] objects.
*
* It is not prohibited to put extra zero monomials into the map (as for \(0 b c\) in the example). But the
* bigger the coefficients map the worse performance of arithmetical operations performed on it. Thus, it is
* recommended not to put (or even to remove) extra (or useless) monomials in the coefficients map.
* @usesMathJax
*/ */
public val coefficients: Map<Map<Symbol, UInt>, C> public val coefficients: Map<Map<Symbol, UInt>, C>
) : Polynomial<C> { ) : Polynomial<C> {

View File

@ -21,8 +21,10 @@ import kotlin.math.min
*/ */
public data class ListPolynomial<C>( public data class ListPolynomial<C>(
/** /**
* List that contains coefficients of the polynomial. Every monomial `a x^d` is stored as a coefficient `a` placed * List that contains coefficients of the polynomial.
* into the list at index `d`. For example, coefficients of a polynomial `5 x^2 - 6` can be represented as *
* Every monomial \(a x^d\) is stored as a coefficient \(a\) placed
* into the list at index \(d\). For example, coefficients of a polynomial \(5 x^2 - 6\) can be represented as
* ``` * ```
* listOf( * listOf(
* -6, // -6 + * -6, // -6 +
@ -40,9 +42,10 @@ public data class ListPolynomial<C>(
* 0, // 0 x^4 * 0, // 0 x^4
* ) * )
* ``` * ```
* It is not prohibited to put extra zeros at end of the list (as for `0x^3` and `0x^4` in the example). But the * It is not prohibited to put extra zeros at end of the list (as for \(0x^3\) and \(0x^4\) in the example). But the
* longer the coefficients list the worse performance of arithmetical operations performed on it. Thus, it is * longer the coefficients list the worse performance of arithmetical operations performed on it. Thus, it is
* recommended not to put (or even to remove) extra (or useless) coefficients at the end of the coefficients list. * recommended not to put (or even to remove) extra (or useless) coefficients at the end of the coefficients list.
* @usesMathJax
*/ */
public val coefficients: List<C> public val coefficients: List<C>
) : Polynomial<C> { ) : Polynomial<C> {

View File

@ -24,9 +24,9 @@ internal constructor(
/** /**
* Map that contains coefficients of the polynomial. * Map that contains coefficients of the polynomial.
* *
* Every monomial `a x_1^{d_1} ... x_n^{d_n}` is stored as a pair "key-value" in the map, where the value is the * Every monomial \(a x_1^{d_1} ... x_n^{d_n}\) is stored as a pair "key-value" in the map, where the value is the
* coefficient `a` and the key is a list that associates index of every variable in the monomial with their degree * coefficient \(a\) and the key is a list that associates index of every variable in the monomial with their degree
* in the monomial. For example, coefficients of a polynomial `5 x_1^2 x_3^3 - 6 x_2` can be represented as * in the monomial. For example, coefficients of a polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be represented as
* ``` * ```
* mapOf( * mapOf(
* listOf(2, 0, 3) to 5, // 5 x_1^2 x_3^3 + * listOf(2, 0, 3) to 5, // 5 x_1^2 x_3^3 +
@ -41,9 +41,10 @@ internal constructor(
* listOf(0, 1, 1) to 0, // 0 x_2^1 x_3^1 * listOf(0, 1, 1) to 0, // 0 x_2^1 x_3^1
* ) * )
* ``` * ```
* It is not prohibited to put extra zero monomials into the map (as for `0 x_2 x_3` in the example). But the * It is not prohibited to put extra zero monomials into the map (as for \(0 x_2 x_3\) in the example). But the
* bigger the coefficients map the worse performance of arithmetical operations performed on it. Thus, it is * bigger the coefficients map the worse performance of arithmetical operations performed on it. Thus, it is
* recommended not to put (or even to remove) extra (or useless) monomials in the coefficients map. * recommended not to put (or even to remove) extra (or useless) monomials in the coefficients map.
* @usesMathJax
*/ */
public val coefficients: Map<List<UInt>, C> public val coefficients: Map<List<UInt>, C>
) : Polynomial<C> { ) : Polynomial<C> {
@ -325,7 +326,7 @@ public class NumberedPolynomialSpace<C, A : Ring<C>>(
/** /**
* Maximal index (ID) of variable occurring in the polynomial with positive power. If there is no such variable, * Maximal index (ID) of variable occurring in the polynomial with positive power. If there is no such variable,
* the result is `-1`. * the result is -1.
*/ */
public val NumberedPolynomial<C>.lastVariable: Int public val NumberedPolynomial<C>.lastVariable: Int
get() = coefficients.keys.maxOfOrNull { degs -> degs.lastIndex } ?: -1 get() = coefficients.keys.maxOfOrNull { degs -> degs.lastIndex } ?: -1
@ -365,7 +366,7 @@ public class NumberedPolynomialSpace<C, A : Ring<C>>(
} ?: 0u } ?: 0u
/** /**
* Count of variables occurring in the polynomial with positive power. If there is no such variable, * Count of variables occurring in the polynomial with positive power. If there is no such variable,
* the result is `0`. * the result is 0.
*/ */
public val NumberedPolynomial<C>.countOfVariables: Int public val NumberedPolynomial<C>.countOfVariables: Int
get() = get() =

View File

@ -262,7 +262,7 @@ public inline fun <C> C.asLabeledPolynomial() : LabeledPolynomial<C> = LabeledPo
/** /**
* Marks DSL that allows to more simply create [LabeledPolynomial]s with good performance. * Marks DSL that allows to more simply create [LabeledPolynomial]s with good performance.
* *
* For example, polynomial `5 a^2 c^3 - 6 b` can be described as * For example, polynomial \(5 a^2 c^3 - 6 b\) can be described as
* ``` * ```
* Int.algebra { * Int.algebra {
* val numberedPolynomial : NumberedPolynomial<Int> = NumberedPolynomial { * val numberedPolynomial : NumberedPolynomial<Int> = NumberedPolynomial {
@ -271,6 +271,7 @@ public inline fun <C> C.asLabeledPolynomial() : LabeledPolynomial<C> = LabeledPo
* } * }
* } * }
* ``` * ```
* @usesMathJax
*/ */
@DslMarker @DslMarker
@UnstableKMathAPI @UnstableKMathAPI
@ -385,7 +386,7 @@ public class DSL1LabeledPolynomialBuilder<C>(
///** ///**
// * Creates [LabeledPolynomial] with lambda [block] in context of [this] ring of constants. // * Creates [LabeledPolynomial] with lambda [block] in context of [this] ring of constants.
// * // *
// * For example, polynomial `5 x_1^2 x_3^3 - 6 x_2` can be described as // * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as
// * ``` // * ```
// * Int.algebra { // * Int.algebra {
// * val LabeledPolynomial : LabeledPolynomial<Int> = LabeledPolynomial { // * val LabeledPolynomial : LabeledPolynomial<Int> = LabeledPolynomial {
@ -394,6 +395,7 @@ public class DSL1LabeledPolynomialBuilder<C>(
// * } // * }
// * } // * }
// * ``` // * ```
// * @usesMathJax
// */ // */
// FIXME: For now this fabric does not let next two fabrics work. (See KT-52803.) Possible feature solutions: // FIXME: For now this fabric does not let next two fabrics work. (See KT-52803.) Possible feature solutions:
// 1. `LowPriorityInOverloadResolution` becomes public. Then it should be applied to this function. // 1. `LowPriorityInOverloadResolution` becomes public. Then it should be applied to this function.
@ -404,7 +406,7 @@ public class DSL1LabeledPolynomialBuilder<C>(
/** /**
* Creates [LabeledPolynomial] with lambda [block] in context of [this] ring of [LabeledPolynomial]s. * Creates [LabeledPolynomial] with lambda [block] in context of [this] ring of [LabeledPolynomial]s.
* *
* For example, polynomial `5 x_1^2 x_3^3 - 6 x_2` can be described as * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as
* ``` * ```
* Int.algebra { * Int.algebra {
* val LabeledPolynomial : LabeledPolynomial<Int> = LabeledPolynomial { * val LabeledPolynomial : LabeledPolynomial<Int> = LabeledPolynomial {
@ -413,14 +415,15 @@ public class DSL1LabeledPolynomialBuilder<C>(
* } * }
* } * }
* ``` * ```
* @usesMathJax
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public inline fun <C, A: Ring<C>> LabeledPolynomialSpace<C, A>.LabeledPolynomialDSL1(initialCapacity: Int = 0, block: DSL1LabeledPolynomialBuilder<C>.() -> Unit) : LabeledPolynomial<C> = DSL1LabeledPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build() public inline fun <C, A: Ring<C>> LabeledPolynomialSpace<C, A>.LabeledPolynomialDSL1(initialCapacity: Int = 0, block: DSL1LabeledPolynomialBuilder<C>.() -> Unit) : LabeledPolynomial<C> = DSL1LabeledPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build()
/** /**
* Creates [LabeledPolynomial] with lambda [block] in context of [this] field of [LabeledRationalFunction]s. * Creates [LabeledPolynomial] with lambda [block] in context of [this] field of [LabeledRationalFunction]s.
* *
* For example, polynomial `5 x_1^2 x_3^3 - 6 x_2` can be described as * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as
* ``` * ``
* Int.algebra { * Int.algebra {
* val LabeledPolynomial : LabeledPolynomial<Int> = LabeledPolynomial { * val LabeledPolynomial : LabeledPolynomial<Int> = LabeledPolynomial {
* 5 { 1 inPowerOf 2u; 3 inPowerOf 3u } // 5 x_1^2 x_3^3 + * 5 { 1 inPowerOf 2u; 3 inPowerOf 3u } // 5 x_1^2 x_3^3 +
@ -428,6 +431,7 @@ public inline fun <C, A: Ring<C>> LabeledPolynomialSpace<C, A>.LabeledPolynomial
* } * }
* } * }
* ``` * ```
* @usesMathJax
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public inline fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledPolynomialDSL1(initialCapacity: Int = 0, block: DSL1LabeledPolynomialBuilder<C>.() -> Unit) : LabeledPolynomial<C> = DSL1LabeledPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build() public inline fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledPolynomialDSL1(initialCapacity: Int = 0, block: DSL1LabeledPolynomialBuilder<C>.() -> Unit) : LabeledPolynomial<C> = DSL1LabeledPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build()

View File

@ -243,7 +243,7 @@ public inline fun <C> C.asNumberedPolynomial() : NumberedPolynomial<C> = Numbere
/** /**
* Marks DSL that allows to more simply create [NumberedPolynomial]s with good performance. * Marks DSL that allows to more simply create [NumberedPolynomial]s with good performance.
* *
* For example, polynomial `5 x_1^2 x_3^3 - 6 x_2` can be described as * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as
* ``` * ```
* Int.algebra { * Int.algebra {
* val numberedPolynomial : NumberedPolynomial<Int> = NumberedPolynomial { * val numberedPolynomial : NumberedPolynomial<Int> = NumberedPolynomial {
@ -252,6 +252,7 @@ public inline fun <C> C.asNumberedPolynomial() : NumberedPolynomial<C> = Numbere
* } * }
* } * }
* ``` * ```
* @usesMathJax
*/ */
@DslMarker @DslMarker
@UnstableKMathAPI @UnstableKMathAPI
@ -372,7 +373,7 @@ public class DSL1NumberedPolynomialBuilder<C>(
///** ///**
// * Creates [NumberedPolynomial] with lambda [block] in context of [this] ring of constants. // * Creates [NumberedPolynomial] with lambda [block] in context of [this] ring of constants.
// * // *
// * For example, polynomial `5 x_1^2 x_3^3 - 6 x_2` can be described as // * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as
// * ``` // * ```
// * Int.algebra { // * Int.algebra {
// * val numberedPolynomial : NumberedPolynomial<Int> = NumberedPolynomial { // * val numberedPolynomial : NumberedPolynomial<Int> = NumberedPolynomial {
@ -381,6 +382,7 @@ public class DSL1NumberedPolynomialBuilder<C>(
// * } // * }
// * } // * }
// * ``` // * ```
// * @usesMathJax
// */ // */
// FIXME: For now this fabric does not let next two fabrics work. (See KT-52803.) Possible feature solutions: // FIXME: For now this fabric does not let next two fabrics work. (See KT-52803.) Possible feature solutions:
// 1. `LowPriorityInOverloadResolution` becomes public. Then it should be applied to this function. // 1. `LowPriorityInOverloadResolution` becomes public. Then it should be applied to this function.
@ -391,7 +393,7 @@ public class DSL1NumberedPolynomialBuilder<C>(
/** /**
* Creates [NumberedPolynomial] with lambda [block] in context of [this] ring of [NumberedPolynomial]s. * Creates [NumberedPolynomial] with lambda [block] in context of [this] ring of [NumberedPolynomial]s.
* *
* For example, polynomial `5 x_1^2 x_3^3 - 6 x_2` can be described as * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as
* ``` * ```
* Int.algebra { * Int.algebra {
* val numberedPolynomial : NumberedPolynomial<Int> = NumberedPolynomial { * val numberedPolynomial : NumberedPolynomial<Int> = NumberedPolynomial {
@ -400,13 +402,14 @@ public class DSL1NumberedPolynomialBuilder<C>(
* } * }
* } * }
* ``` * ```
* @usesMathJax
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public inline fun <C, A: Ring<C>> NumberedPolynomialSpace<C, A>.NumberedPolynomialDSL1(initialCapacity: Int = 0, block: DSL1NumberedPolynomialBuilder<C>.() -> Unit) : NumberedPolynomial<C> = DSL1NumberedPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build() public inline fun <C, A: Ring<C>> NumberedPolynomialSpace<C, A>.NumberedPolynomialDSL1(initialCapacity: Int = 0, block: DSL1NumberedPolynomialBuilder<C>.() -> Unit) : NumberedPolynomial<C> = DSL1NumberedPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build()
/** /**
* Creates [NumberedPolynomial] with lambda [block] in context of [this] field of [NumberedRationalFunction]s. * Creates [NumberedPolynomial] with lambda [block] in context of [this] field of [NumberedRationalFunction]s.
* *
* For example, polynomial `5 x_1^2 x_3^3 - 6 x_2` can be described as * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as
* ``` * ```
* Int.algebra { * Int.algebra {
* val numberedPolynomial : NumberedPolynomial<Int> = NumberedPolynomial { * val numberedPolynomial : NumberedPolynomial<Int> = NumberedPolynomial {
@ -415,6 +418,7 @@ public inline fun <C, A: Ring<C>> NumberedPolynomialSpace<C, A>.NumberedPolynomi
* } * }
* } * }
* ``` * ```
* @usesMathJax
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public inline fun <C, A: Ring<C>> NumberedRationalFunctionSpace<C, A>.NumberedPolynomialDSL1(initialCapacity: Int = 0, block: DSL1NumberedPolynomialBuilder<C>.() -> Unit) : NumberedPolynomial<C> = DSL1NumberedPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build() public inline fun <C, A: Ring<C>> NumberedRationalFunctionSpace<C, A>.NumberedPolynomialDSL1(initialCapacity: Int = 0, block: DSL1NumberedPolynomialBuilder<C>.() -> Unit) : NumberedPolynomial<C> = DSL1NumberedPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build()