Added multivariate abstractions.

This commit is contained in:
Gleb Minaev 2022-03-22 15:28:34 +03:00
parent 39ce855075
commit b44c99c265
4 changed files with 303 additions and 55 deletions

View File

@ -117,8 +117,8 @@ public fun <C> C.asLabeledPolynomial() : LabeledPolynomial<C> = LabeledPolynomia
*/
public class LabeledPolynomialSpace<C, A : Ring<C>>(
public override val ring: A,
) : PolynomialSpaceOverRing<C, LabeledPolynomial<C>, A> {
public operator fun Symbol.plus(other: Int): LabeledPolynomial<C> =
) : MultivariatePolynomialSpace<C, Symbol, LabeledPolynomial<C>>, PolynomialSpaceOverRing<C, LabeledPolynomial<C>, A> {
public override operator fun Symbol.plus(other: Int): LabeledPolynomial<C> =
if (other == 0) LabeledPolynomial<C>(mapOf(
mapOf(this@plus to 1U) to constantOne,
))
@ -126,7 +126,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
mapOf(this@plus to 1U) to constantOne,
emptyMap<Symbol, UInt>() to constantOne * other,
))
public operator fun Symbol.minus(other: Int): LabeledPolynomial<C> =
public override operator fun Symbol.minus(other: Int): LabeledPolynomial<C> =
if (other == 0) LabeledPolynomial<C>(mapOf(
mapOf(this@minus to 1U) to -constantOne,
))
@ -134,13 +134,13 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
mapOf(this@minus to 1U) to -constantOne,
emptyMap<Symbol, UInt>() to constantOne * other,
))
public operator fun Symbol.times(other: Int): LabeledPolynomial<C> =
public override operator fun Symbol.times(other: Int): LabeledPolynomial<C> =
if (other == 0) zero
else LabeledPolynomial<C>(mapOf(
mapOf(this to 1U) to constantOne * other,
))
public operator fun Int.plus(other: Symbol): LabeledPolynomial<C> =
public override operator fun Int.plus(other: Symbol): LabeledPolynomial<C> =
if (this == 0) LabeledPolynomial<C>(mapOf(
mapOf(other to 1U) to constantOne,
))
@ -148,7 +148,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
mapOf(other to 1U) to constantOne,
emptyMap<Symbol, UInt>() to constantOne * this@plus,
))
public operator fun Int.minus(other: Symbol): LabeledPolynomial<C> =
public override operator fun Int.minus(other: Symbol): LabeledPolynomial<C> =
if (this == 0) LabeledPolynomial<C>(mapOf(
mapOf(other to 1U) to -constantOne,
))
@ -156,7 +156,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
mapOf(other to 1U) to -constantOne,
emptyMap<Symbol, UInt>() to constantOne * this@minus,
))
public operator fun Int.times(other: Symbol): LabeledPolynomial<C> =
public override operator fun Int.times(other: Symbol): LabeledPolynomial<C> =
if (this == 0) zero
else LabeledPolynomial<C>(mapOf(
mapOf(other to 1U) to constantOne * this@times,
@ -275,7 +275,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
*/
public override fun number(value: Int): LabeledPolynomial<C> = number(constantNumber(value))
public operator fun C.plus(other: Symbol): LabeledPolynomial<C> =
public override operator fun C.plus(other: Symbol): LabeledPolynomial<C> =
if (isZero()) LabeledPolynomial<C>(mapOf(
mapOf(other to 1U) to constantOne,
))
@ -283,7 +283,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
mapOf(other to 1U) to constantOne,
emptyMap<Symbol, UInt>() to this@plus,
))
public operator fun C.minus(other: Symbol): LabeledPolynomial<C> =
public override operator fun C.minus(other: Symbol): LabeledPolynomial<C> =
if (isZero()) LabeledPolynomial<C>(mapOf(
mapOf(other to 1U) to -constantOne,
))
@ -291,13 +291,13 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
mapOf(other to 1U) to -constantOne,
emptyMap<Symbol, UInt>() to this@minus,
))
public operator fun C.times(other: Symbol): LabeledPolynomial<C> =
public override operator fun C.times(other: Symbol): LabeledPolynomial<C> =
if (isZero()) zero
else LabeledPolynomial<C>(mapOf(
mapOf(other to 1U) to this@times,
))
public operator fun Symbol.plus(other: C): LabeledPolynomial<C> =
public override operator fun Symbol.plus(other: C): LabeledPolynomial<C> =
if (other.isZero()) LabeledPolynomial<C>(mapOf(
mapOf(this@plus to 1U) to constantOne,
))
@ -305,7 +305,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
mapOf(this@plus to 1U) to constantOne,
emptyMap<Symbol, UInt>() to other,
))
public operator fun Symbol.minus(other: C): LabeledPolynomial<C> =
public override operator fun Symbol.minus(other: C): LabeledPolynomial<C> =
if (other.isZero()) LabeledPolynomial<C>(mapOf(
mapOf(this@minus to 1U) to -constantOne,
))
@ -313,7 +313,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
mapOf(this@minus to 1U) to -constantOne,
emptyMap<Symbol, UInt>() to other,
))
public operator fun Symbol.times(other: C): LabeledPolynomial<C> =
public override operator fun Symbol.times(other: C): LabeledPolynomial<C> =
if (other.isZero()) zero
else LabeledPolynomial<C>(mapOf(
mapOf(this@times to 1U) to other,
@ -430,7 +430,15 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
if (value == 0) zero
else LabeledPolynomial(mapOf(emptyMap<Symbol, UInt>() to value))
public operator fun Symbol.plus(other: Symbol): LabeledPolynomial<C> =
public override operator fun Symbol.unaryPlus(): LabeledPolynomial<C> =
LabeledPolynomial<C>(mapOf(
mapOf(this to 1U) to constantOne,
))
public override operator fun Symbol.unaryMinus(): LabeledPolynomial<C> =
LabeledPolynomial<C>(mapOf(
mapOf(this to 1U) to -constantOne,
))
public override operator fun Symbol.plus(other: Symbol): LabeledPolynomial<C> =
if (this == other) LabeledPolynomial<C>(mapOf(
mapOf(this to 1U) to constantOne * 2
))
@ -438,13 +446,13 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
mapOf(this to 1U) to constantOne,
mapOf(other to 1U) to constantOne,
))
public operator fun Symbol.minus(other: Symbol): LabeledPolynomial<C> =
public override operator fun Symbol.minus(other: Symbol): LabeledPolynomial<C> =
if (this == other) zero
else LabeledPolynomial<C>(mapOf(
mapOf(this to 1U) to constantOne,
mapOf(other to 1U) to -constantOne,
))
public operator fun Symbol.times(other: Symbol): LabeledPolynomial<C> =
public override operator fun Symbol.times(other: Symbol): LabeledPolynomial<C> =
if (this == other) LabeledPolynomial<C>(mapOf(
mapOf(this to 2U) to constantOne
))
@ -452,7 +460,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
mapOf(this to 1U, other to 1U) to constantOne,
))
public operator fun Symbol.plus(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
public override operator fun Symbol.plus(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
with(other.coefficients) {
if (isEmpty()) LabeledPolynomial<C>(mapOf(mapOf(this@plus to 1u) to constantOne))
else LabeledPolynomial<C>(
@ -467,7 +475,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
}
)
}
public operator fun Symbol.minus(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
public override operator fun Symbol.minus(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
with(other.coefficients) {
if (isEmpty()) LabeledPolynomial<C>(mapOf(mapOf(this@minus to 1u) to constantOne))
else LabeledPolynomial<C>(
@ -484,13 +492,13 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
}
)
}
public operator fun Symbol.times(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
public override operator fun Symbol.times(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
LabeledPolynomial<C>(
other.coefficients
.mapKeys { (degs, _) -> degs.toMutableMap().also{ it[this] = if (this in it) it[this]!! + 1U else 1U } }
)
public operator fun LabeledPolynomial<C>.plus(other: Symbol): LabeledPolynomial<C> =
public override operator fun LabeledPolynomial<C>.plus(other: Symbol): LabeledPolynomial<C> =
with(coefficients) {
if (isEmpty()) LabeledPolynomial<C>(mapOf(mapOf(other to 1u) to constantOne))
else LabeledPolynomial<C>(
@ -505,7 +513,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
}
)
}
public operator fun LabeledPolynomial<C>.minus(other: Symbol): LabeledPolynomial<C> =
public override operator fun LabeledPolynomial<C>.minus(other: Symbol): LabeledPolynomial<C> =
with(coefficients) {
if (isEmpty()) LabeledPolynomial<C>(mapOf(mapOf(other to 1u) to constantOne))
else LabeledPolynomial<C>(
@ -520,7 +528,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
}
)
}
public operator fun LabeledPolynomial<C>.times(other: Symbol): LabeledPolynomial<C> =
public override operator fun LabeledPolynomial<C>.times(other: Symbol): LabeledPolynomial<C> =
LabeledPolynomial<C>(
coefficients
.mapKeys { (degs, _) -> degs.toMutableMap().also{ it[other] = if (other in it) it[other]!! + 1U else 1U } }
@ -604,7 +612,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
* As consequence all values in the map are positive integers. Also, if the polynomial is constant, the map is empty.
* And keys of the map is the same as in [variables].
*/
public val LabeledPolynomial<C>.degrees: Map<Symbol, UInt>
public override val LabeledPolynomial<C>.degrees: Map<Symbol, UInt>
get() =
buildMap {
coefficients.entries.forEach { (degs, c) ->
@ -613,10 +621,20 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
}
}
}
/**
* Counts degree of the polynomial by the specified [variable].
*/
public override fun LabeledPolynomial<C>.degreeBy(variable: Symbol): UInt =
coefficients.entries.maxOfOrNull { (degs, c) -> if (c.isZero()) 0u else degs.getOrElse(variable) { 0u } } ?: 0u
/**
* Counts degree of the polynomial by the specified [variables].
*/
public override fun LabeledPolynomial<C>.degreeBy(variables: Collection<Symbol>): UInt =
coefficients.entries.maxOfOrNull { (degs, c) -> if (c.isZero()) 0u else degs.filterKeys { it in variables }.values.sum() } ?: 0u
/**
* Set of all variables that appear in the polynomial in positive exponents.
*/
public val LabeledPolynomial<C>.variables: Set<Symbol>
public override val LabeledPolynomial<C>.variables: Set<Symbol>
get() =
buildSet {
coefficients.entries.forEach { (degs, c) -> if (c.isNotZero()) addAll(degs.keys) }
@ -624,7 +642,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
/**
* Count of all variables that appear in the polynomial in positive exponents.
*/
public val LabeledPolynomial<C>.countOfVariables: Int get() = variables.size
public override val LabeledPolynomial<C>.countOfVariables: Int get() = variables.size
/**
* Checks if the instant is constant polynomial (of degree no more than 0) over considered ring.

View File

@ -63,14 +63,16 @@ public fun <C, A: Ring<C>> A.LabeledRationalFunction(numeratorCoefficients: Map<
public class LabeledRationalFunctionSpace<C, A: Ring<C>>(
public val ring: A,
) :
RationalFunctionalSpaceOverPolynomialSpace<
MultivariateRationalFunctionalSpaceOverMultivariatePolynomialSpace<
C,
Symbol,
LabeledPolynomial<C>,
LabeledRationalFunction<C>,
LabeledPolynomialSpace<C, A>,
>,
PolynomialSpaceOfFractions<
MultivariatePolynomialSpaceOfFractions<
C,
Symbol,
LabeledPolynomial<C>,
LabeledRationalFunction<C>,
>() {
@ -113,33 +115,6 @@ public class LabeledRationalFunctionSpace<C, A: Ring<C>>(
return numerator * other.denominator equalsTo other.numerator * denominator
}
/**
* Map that associates variables (that appear in the polynomial in positive exponents) with their most exponents
* in which they are appeared in the polynomial.
*
* As consequence all values in the map are positive integers. Also, if the polynomial is constant, the map is empty.
* And keys of the map is the same as in [variables].
*/
public val LabeledPolynomial<C>.degrees: Map<Symbol, UInt> get() = polynomialRing { degrees }
/**
* Set of all variables that appear in the polynomial in positive exponents.
*/
public val LabeledPolynomial<C>.variables: Set<Symbol> get() = polynomialRing { variables }
/**
* Count of all variables that appear in the polynomial in positive exponents.
*/
public val LabeledPolynomial<C>.countOfVariables: Int get() = polynomialRing { countOfVariables }
/**
* Count of all variables that appear in the polynomial in positive exponents.
*/
public val LabeledRationalFunction<C>.variables: Set<Symbol>
get() = numerator.variables union denominator.variables
/**
* Count of all variables that appear in the polynomial in positive exponents.
*/
public val LabeledRationalFunction<C>.countOfVariables: Int get() = variables.size
// TODO: Разобрать
// operator fun invoke(arg: Map<Symbol, C>): LabeledRationalFunction<C> =

View File

@ -5,7 +5,8 @@
package space.kscience.kmath.functions
import space.kscience.kmath.operations.*
import space.kscience.kmath.operations.Ring
import space.kscience.kmath.operations.invoke
import kotlin.js.JsName
import kotlin.jvm.JvmName
@ -413,4 +414,61 @@ public interface PolynomialSpaceOverRing<C, P: Polynomial<C>, A: Ring<C>> : Poly
* Instance of unit constant (unit of the underlying ring).
*/
public override val constantOne: C get() = ring.one
}
public interface MultivariatePolynomialSpace<C, V, P: Polynomial<C>>: PolynomialSpace<C, P> {
public operator fun V.plus(other: Int): P
public operator fun V.minus(other: Int): P
public operator fun V.times(other: Int): P
public operator fun Int.plus(other: V): P
public operator fun Int.minus(other: V): P
public operator fun Int.times(other: V): P
public operator fun C.plus(other: V): P
public operator fun C.minus(other: V): P
public operator fun C.times(other: V): P
public operator fun V.plus(other: C): P
public operator fun V.minus(other: C): P
public operator fun V.times(other: C): P
public operator fun V.unaryPlus(): P
public operator fun V.unaryMinus(): P
public operator fun V.plus(other: V): P
public operator fun V.minus(other: V): P
public operator fun V.times(other: V): P
public operator fun V.plus(other: P): P
public operator fun V.minus(other: P): P
public operator fun V.times(other: P): P
public operator fun P.plus(other: V): P
public operator fun P.minus(other: V): P
public operator fun P.times(other: V): P
/**
* Map that associates variables (that appear in the polynomial in positive exponents) with their most exponents
* in which they are appeared in the polynomial.
*
* As consequence all values in the map are positive integers. Also, if the polynomial is constant, the map is empty.
* And keys of the map is the same as in [variables].
*/
public val P.degrees: Map<V, UInt>
/**
* Counts degree of the polynomial by the specified [variable].
*/
public fun P.degreeBy(variable: V): UInt = degrees.getOrElse(variable) { 0u }
/**
* Counts degree of the polynomial by the specified [variables].
*/
public fun P.degreeBy(variables: Collection<V>): UInt
/**
* Set of all variables that appear in the polynomial in positive exponents.
*/
public val P.variables: Set<V> get() = degrees.keys
/**
* Count of all variables that appear in the polynomial in positive exponents.
*/
public val P.countOfVariables: Int get() = variables.size
}

View File

@ -1306,4 +1306,201 @@ public abstract class PolynomialSpaceOfFractions<
* Instance of unit polynomial (unit of the rational functions ring).
*/
public override val one: R get() = constructRationalFunction(polynomialOne)
}
public interface MultivariateRationalFunctionalSpace<
C,
V,
P: Polynomial<C>,
R: RationalFunction<C, P>
>: RationalFunctionalSpace<C, P, R> {
public operator fun V.plus(other: Int): P
public operator fun V.minus(other: Int): P
public operator fun V.times(other: Int): P
public operator fun Int.plus(other: V): P
public operator fun Int.minus(other: V): P
public operator fun Int.times(other: V): P
public operator fun C.plus(other: V): P
public operator fun C.minus(other: V): P
public operator fun C.times(other: V): P
public operator fun V.plus(other: C): P
public operator fun V.minus(other: C): P
public operator fun V.times(other: C): P
public operator fun V.unaryPlus(): P
public operator fun V.unaryMinus(): P
public operator fun V.plus(other: V): P
public operator fun V.minus(other: V): P
public operator fun V.times(other: V): P
public operator fun V.plus(other: P): P
public operator fun V.minus(other: P): P
public operator fun V.times(other: P): P
public operator fun P.plus(other: V): P
public operator fun P.minus(other: V): P
public operator fun P.times(other: V): P
public operator fun V.plus(other: R): R
public operator fun V.minus(other: R): R
public operator fun V.times(other: R): R
public operator fun R.plus(other: V): R
public operator fun R.minus(other: V): R
public operator fun R.times(other: V): R
/**
* Map that associates variables (that appear in the polynomial in positive exponents) with their most exponents
* in which they are appeared in the polynomial.
*
* As consequence all values in the map are positive integers. Also, if the polynomial is constant, the map is empty.
* And keys of the map is the same as in [variables].
*/
public val P.degrees: Map<V, UInt>
/**
* Counts degree of the polynomial by the specified [variable].
*/
public fun P.degreeBy(variable: V): UInt = degrees.getOrElse(variable) { 0u }
/**
* Counts degree of the polynomial by the specified [variables].
*/
public fun P.degreeBy(variables: Collection<V>): UInt
/**
* Set of all variables that appear in the polynomial in positive exponents.
*/
public val P.variables: Set<V> get() = degrees.keys
/**
* Count of all variables that appear in the polynomial in positive exponents.
*/
public val P.countOfVariables: Int get() = variables.size
/**
* Set of all variables that appear in the polynomial in positive exponents.
*/
public val R.variables: Set<V> get() = numerator.variables union denominator.variables
/**
* Count of all variables that appear in the polynomial in positive exponents.
*/
public val R.countOfVariables: Int get() = variables.size
}
public interface MultivariateRationalFunctionalSpaceOverRing<
C,
V,
P: Polynomial<C>,
R: RationalFunction<C, P>,
A: Ring<C>
> : RationalFunctionalSpaceOverRing<C, P, R, A>, MultivariateRationalFunctionalSpace<C, V, P, R>
public interface MultivariateRationalFunctionalSpaceOverPolynomialSpace<
C,
V,
P: Polynomial<C>,
R: RationalFunction<C, P>,
AP: PolynomialSpace<C, P>,
> : RationalFunctionalSpaceOverPolynomialSpace<C, P, R, AP>, MultivariateRationalFunctionalSpace<C, V, P, R>
public interface MultivariateRationalFunctionalSpaceOverMultivariatePolynomialSpace<
C,
V,
P: Polynomial<C>,
R: RationalFunction<C, P>,
AP: MultivariatePolynomialSpace<C, V, P>,
> : MultivariateRationalFunctionalSpaceOverPolynomialSpace<C, V, P, R, AP> {
public override operator fun V.plus(other: Int): P = polynomialRing { this@plus + other }
public override operator fun V.minus(other: Int): P = polynomialRing { this@minus - other }
public override operator fun V.times(other: Int): P = polynomialRing { this@times * other }
public override operator fun Int.plus(other: V): P = polynomialRing { this@plus + other }
public override operator fun Int.minus(other: V): P = polynomialRing { this@minus - other }
public override operator fun Int.times(other: V): P = polynomialRing { this@times * other }
public override operator fun C.plus(other: V): P = polynomialRing { this@plus + other }
public override operator fun C.minus(other: V): P = polynomialRing { this@minus - other }
public override operator fun C.times(other: V): P = polynomialRing { this@times * other }
public override operator fun V.plus(other: C): P = polynomialRing { this@plus + other }
public override operator fun V.minus(other: C): P = polynomialRing { this@minus - other }
public override operator fun V.times(other: C): P = polynomialRing { this@times * other }
public override operator fun V.unaryPlus(): P = polynomialRing { +this@unaryPlus }
public override operator fun V.unaryMinus(): P = polynomialRing { -this@unaryMinus }
public override operator fun V.plus(other: V): P = polynomialRing { this@plus + other }
public override operator fun V.minus(other: V): P = polynomialRing { this@minus - other }
public override operator fun V.times(other: V): P = polynomialRing { this@times * other }
public override operator fun V.plus(other: P): P = polynomialRing { this@plus + other }
public override operator fun V.minus(other: P): P = polynomialRing { this@minus - other }
public override operator fun V.times(other: P): P = polynomialRing { this@times * other }
public override operator fun P.plus(other: V): P = polynomialRing { this@plus + other }
public override operator fun P.minus(other: V): P = polynomialRing { this@minus - other }
public override operator fun P.times(other: V): P = polynomialRing { this@times * other }
/**
* Map that associates variables (that appear in the polynomial in positive exponents) with their most exponents
* in which they are appeared in the polynomial.
*
* As consequence all values in the map are positive integers. Also, if the polynomial is constant, the map is empty.
* And keys of the map is the same as in [variables].
*/
public override val P.degrees: Map<V, UInt> get() = polynomialRing { degrees }
/**
* Counts degree of the polynomial by the specified [variable].
*/
public override fun P.degreeBy(variable: V): UInt = polynomialRing { degreeBy(variable) }
/**
* Counts degree of the polynomial by the specified [variables].
*/
public override fun P.degreeBy(variables: Collection<V>): UInt = polynomialRing { degreeBy(variables) }
/**
* Set of all variables that appear in the polynomial in positive exponents.
*/
public override val P.variables: Set<V> get() = polynomialRing { variables }
/**
* Count of all variables that appear in the polynomial in positive exponents.
*/
public override val P.countOfVariables: Int get() = polynomialRing { countOfVariables }
}
public abstract class MultivariatePolynomialSpaceOfFractions<
C,
V,
P: Polynomial<C>,
R: RationalFunction<C, P>,
> : MultivariateRationalFunctionalSpace<C, V, P, R>, PolynomialSpaceOfFractions<C, P, R>() {
public override operator fun V.plus(other: R): R =
constructRationalFunction(
this * other.denominator + other.numerator,
other.denominator
)
public override operator fun V.minus(other: R): R =
constructRationalFunction(
this * other.denominator - other.numerator,
other.denominator
)
public override operator fun V.times(other: R): R =
constructRationalFunction(
this * other.numerator,
other.denominator
)
public override operator fun R.plus(other: V): R =
constructRationalFunction(
numerator + denominator * other,
denominator
)
public override operator fun R.minus(other: V): R =
constructRationalFunction(
numerator - denominator * other,
denominator
)
public override operator fun R.times(other: V): R =
constructRationalFunction(
numerator * other,
denominator
)
}