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 39 additions and 46 deletions
Showing only changes of commit f48e4483cc - Show all commits

View File

@ -50,7 +50,7 @@ public data class Polynomial<out C>(
*/
public val coefficients: List<C>
) {
override fun toString(): String = "ListPolynomial$coefficients"
override fun toString(): String = "Polynomial$coefficients"
}
/**
@ -63,7 +63,7 @@ public data class Polynomial<out C>(
*/
public open class PolynomialSpace<C, A>(
/**
* Underlying ring of constants. Its operations on constants are inherited by local operations on constants.
* Underlying ring of constants. Its operations on constants are used by local operations on constants and polynomials.
*/
public val ring: A,
) : Ring<Polynomial<C>>, ScaleOperations<Polynomial<C>> where A : Ring<C>, A : ScaleOperations<C> {
@ -191,15 +191,13 @@ public open class PolynomialSpace<C, A>(
/**
* Returns negation of the polynomial.
*/
public override operator fun Polynomial<C>.unaryMinus(): Polynomial<C> =
with(ring) {
public override operator fun Polynomial<C>.unaryMinus(): Polynomial<C> = ring {
Polynomial(coefficients.map { -it })
}
/**
* Returns sum of the polynomials.
*/
public override operator fun Polynomial<C>.plus(other: Polynomial<C>): Polynomial<C> {
with(ring) {
public override operator fun Polynomial<C>.plus(other: Polynomial<C>): Polynomial<C> = ring {
val thisDegree = degree
val otherDegree = other.degree
return Polynomial(
@ -212,12 +210,10 @@ public open class PolynomialSpace<C, A>(
}
)
}
}
/**
* Returns difference of the polynomials.
*/
public override operator fun Polynomial<C>.minus(other: Polynomial<C>): Polynomial<C> {
with(ring) {
public override operator fun Polynomial<C>.minus(other: Polynomial<C>): Polynomial<C> = ring {
val thisDegree = degree
val otherDegree = other.degree
return Polynomial(
@ -230,12 +226,10 @@ public open class PolynomialSpace<C, A>(
}
)
}
}
/**
* Returns product of the polynomials.
*/
public override operator fun Polynomial<C>.times(other: Polynomial<C>): Polynomial<C> {
with(ring) {
public override operator fun Polynomial<C>.times(other: Polynomial<C>): Polynomial<C> = ring {
val thisDegree = degree
val otherDegree = other.degree
return Polynomial(
@ -246,7 +240,6 @@ public open class PolynomialSpace<C, A>(
}
)
}
}
/**
* Instance of zero polynomial (zero of the polynomial ring).

View File

@ -91,7 +91,7 @@ public fun <C, A> Polynomial<C>.integrate(
public fun <C : Comparable<C>> Polynomial<C>.integrate(
ring: Field<C>,
range: ClosedRange<C>,
): C {
): C = ring {
val antiderivative = integrate(ring)
return ring { antiderivative.value(ring, range.endInclusive) - antiderivative.value(ring, range.start) }
return antiderivative.value(ring, range.endInclusive) - antiderivative.value(ring, range.start)
}