Feature: Polynomials and rational functions #469
@ -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,61 +191,54 @@ 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(
|
List(max(thisDegree, otherDegree) + 1) {
|
||||||
List(max(thisDegree, otherDegree) + 1) {
|
when {
|
||||||
when {
|
it > thisDegree -> other.coefficients[it]
|
||||||
it > thisDegree -> other.coefficients[it]
|
it > otherDegree -> coefficients[it]
|
||||||
it > otherDegree -> coefficients[it]
|
else -> coefficients[it] + other.coefficients[it]
|
||||||
else -> coefficients[it] + other.coefficients[it]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
}
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 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(
|
List(max(thisDegree, otherDegree) + 1) {
|
||||||
List(max(thisDegree, otherDegree) + 1) {
|
when {
|
||||||
when {
|
it > thisDegree -> -other.coefficients[it]
|
||||||
it > thisDegree -> -other.coefficients[it]
|
it > otherDegree -> coefficients[it]
|
||||||
it > otherDegree -> coefficients[it]
|
else -> coefficients[it] - other.coefficients[it]
|
||||||
else -> coefficients[it] - other.coefficients[it]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
}
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 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(
|
List(thisDegree + otherDegree + 1) { d ->
|
||||||
List(thisDegree + otherDegree + 1) { d ->
|
(max(0, d - otherDegree)..min(thisDegree, d))
|
||||||
(max(0, d - otherDegree)..min(thisDegree, d))
|
.map { coefficients[it] * other.coefficients[d - it] }
|
||||||
.map { coefficients[it] * other.coefficients[d - it] }
|
.reduce { acc, rational -> acc + rational }
|
||||||
.reduce { acc, rational -> acc + rational }
|
}
|
||||||
}
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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)
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user