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> 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>( 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, public val ring: A,
) : Ring<Polynomial<C>>, ScaleOperations<Polynomial<C>> where A : Ring<C>, A : ScaleOperations<C> { ) : 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. * Returns negation of the polynomial.
*/ */
public override operator fun Polynomial<C>.unaryMinus(): Polynomial<C> = public override operator fun Polynomial<C>.unaryMinus(): Polynomial<C> = ring {
with(ring) {
Polynomial(coefficients.map { -it }) Polynomial(coefficients.map { -it })
} }
/** /**
* Returns sum of the polynomials. * Returns sum of the polynomials.
*/ */
public override operator fun Polynomial<C>.plus(other: Polynomial<C>): Polynomial<C> { public override operator fun Polynomial<C>.plus(other: Polynomial<C>): Polynomial<C> = ring {
with(ring) {
val thisDegree = degree val thisDegree = degree
val otherDegree = other.degree val otherDegree = other.degree
return Polynomial( return Polynomial(
@ -212,12 +210,10 @@ public open class PolynomialSpace<C, A>(
} }
) )
} }
}
/** /**
* Returns difference of the polynomials. * Returns difference of the polynomials.
*/ */
public override operator fun Polynomial<C>.minus(other: Polynomial<C>): Polynomial<C> { public override operator fun Polynomial<C>.minus(other: Polynomial<C>): Polynomial<C> = ring {
with(ring) {
val thisDegree = degree val thisDegree = degree
val otherDegree = other.degree val otherDegree = other.degree
return Polynomial( return Polynomial(
@ -230,12 +226,10 @@ public open class PolynomialSpace<C, A>(
} }
) )
} }
}
/** /**
* Returns product of the polynomials. * Returns product of the polynomials.
*/ */
public override operator fun Polynomial<C>.times(other: Polynomial<C>): Polynomial<C> { public override operator fun Polynomial<C>.times(other: Polynomial<C>): Polynomial<C> = ring {
with(ring) {
val thisDegree = degree val thisDegree = degree
val otherDegree = other.degree val otherDegree = other.degree
return Polynomial( return Polynomial(
@ -246,7 +240,6 @@ public open class PolynomialSpace<C, A>(
} }
) )
} }
}
/** /**
* Instance of zero polynomial (zero of the polynomial ring). * 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( public fun <C : Comparable<C>> Polynomial<C>.integrate(
ring: Field<C>, ring: Field<C>,
range: ClosedRange<C>, range: ClosedRange<C>,
): C { ): C = ring {
val antiderivative = integrate(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)
} }