Feature: Polynomials and rational functions #469
@ -211,7 +211,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
|
||||
else LabeledPolynomial(
|
||||
coefficients
|
||||
.applyAndRemoveZeros {
|
||||
mapValues { (_, c) -> c * other }
|
||||
for (degs in keys) this[degs] = this[degs]!! * other
|
||||
}
|
||||
)
|
||||
|
||||
@ -265,7 +265,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
|
||||
else LabeledPolynomial(
|
||||
other.coefficients
|
||||
.applyAndRemoveZeros {
|
||||
mapValues { (_, c) -> this@times * c }
|
||||
for (degs in keys) this[degs] = this@times * this[degs]!!
|
||||
}
|
||||
)
|
||||
|
||||
@ -361,7 +361,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
|
||||
else LabeledPolynomial<C>(
|
||||
other.coefficients
|
||||
.applyAndRemoveZeros {
|
||||
mapValues { (_, c) -> this@times * c }
|
||||
for (degs in keys) this[degs] = this@times * this[degs]!!
|
||||
}
|
||||
)
|
||||
|
||||
@ -413,7 +413,7 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
|
||||
else LabeledPolynomial<C>(
|
||||
coefficients
|
||||
.applyAndRemoveZeros {
|
||||
mapValues { (_, c) -> c * other }
|
||||
for (degs in keys) this[degs] = this[degs]!! * other
|
||||
}
|
||||
)
|
||||
|
||||
@ -525,10 +525,9 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
|
||||
*/
|
||||
override operator fun LabeledPolynomial<C>.plus(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
|
||||
LabeledPolynomial<C>(
|
||||
coefficients
|
||||
.applyAndRemoveZeros {
|
||||
other.coefficients
|
||||
.mapValuesTo(this) { (key, value) -> if (key in this) this[key]!! + value else value }
|
||||
buildCoefficients(coefficients.size + other.coefficients.size) {
|
||||
other.coefficients.mapValuesTo(this) { it.value }
|
||||
other.coefficients.mapValuesTo(this) { (key, value) -> if (key in this) this[key]!! + value else value }
|
||||
}
|
||||
)
|
||||
/**
|
||||
@ -536,10 +535,9 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
|
||||
*/
|
||||
override operator fun LabeledPolynomial<C>.minus(other: LabeledPolynomial<C>): LabeledPolynomial<C> =
|
||||
LabeledPolynomial<C>(
|
||||
coefficients
|
||||
.applyAndRemoveZeros {
|
||||
other.coefficients
|
||||
.mapValuesTo(this) { (key, value) -> if (key in this) this[key]!! - value else -value }
|
||||
buildCoefficients(coefficients.size + other.coefficients.size) {
|
||||
other.coefficients.mapValuesTo(this) { it.value }
|
||||
other.coefficients.mapValuesTo(this) { (key, value) -> if (key in this) this[key]!! - value else -value }
|
||||
}
|
||||
)
|
||||
/**
|
||||
|
@ -163,7 +163,7 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
|
||||
else NumberedPolynomial<C>(
|
||||
coefficients
|
||||
.applyAndRemoveZeros {
|
||||
mapValues { (_, c) -> c * other }
|
||||
for (degs in keys) this[degs] = this[degs]!! * other
|
||||
}
|
||||
)
|
||||
|
||||
@ -217,7 +217,7 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
|
||||
else NumberedPolynomial(
|
||||
other.coefficients
|
||||
.applyAndRemoveZeros {
|
||||
mapValues { (_, c) -> this@times * c }
|
||||
for (degs in keys) this[degs] = this@times * this[degs]!!
|
||||
}
|
||||
)
|
||||
|
||||
@ -269,7 +269,7 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
|
||||
else NumberedPolynomial<C>(
|
||||
other.coefficients
|
||||
.applyAndRemoveZeros {
|
||||
mapValues { (_, c) -> this@times * c }
|
||||
for (degs in keys) this[degs] = this@times * this[degs]!!
|
||||
}
|
||||
)
|
||||
|
||||
@ -319,7 +319,7 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
|
||||
else NumberedPolynomial<C>(
|
||||
coefficients
|
||||
.applyAndRemoveZeros {
|
||||
mapValues { (_, c) -> c * other }
|
||||
for (degs in keys) this[degs] = this[degs]!! * other
|
||||
}
|
||||
)
|
||||
|
||||
@ -335,10 +335,9 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
|
||||
*/
|
||||
override operator fun NumberedPolynomial<C>.plus(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
|
||||
NumberedPolynomial<C>(
|
||||
coefficients
|
||||
.applyAndRemoveZeros {
|
||||
other.coefficients
|
||||
.mapValuesTo(this) { (key, value) -> if (key in this) this[key]!! + value else value }
|
||||
buildCoefficients(coefficients.size + other.coefficients.size) {
|
||||
other.coefficients.mapValuesTo(this) { it.value }
|
||||
other.coefficients.mapValuesTo(this) { (key, value) -> if (key in this) this[key]!! + value else value }
|
||||
}
|
||||
)
|
||||
/**
|
||||
@ -346,10 +345,9 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
|
||||
*/
|
||||
override operator fun NumberedPolynomial<C>.minus(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
|
||||
NumberedPolynomial<C>(
|
||||
coefficients
|
||||
.applyAndRemoveZeros {
|
||||
other.coefficients
|
||||
.mapValuesTo(this) { (key, value) -> if (key in this) this[key]!! - value else -value }
|
||||
buildCoefficients(coefficients.size + other.coefficients.size) {
|
||||
other.coefficients.mapValuesTo(this) { it.value }
|
||||
other.coefficients.mapValuesTo(this) { (key, value) -> if (key in this) this[key]!! - value else -value }
|
||||
}
|
||||
)
|
||||
/**
|
||||
|
@ -129,7 +129,7 @@ public open class PolynomialSpace<C, A : Ring<C>>(
|
||||
else Polynomial(
|
||||
coefficients
|
||||
.applyAndRemoveZeros {
|
||||
map { it * other }
|
||||
for (deg in indices) this[deg] = this[deg] * other
|
||||
}
|
||||
)
|
||||
|
||||
@ -189,7 +189,7 @@ public open class PolynomialSpace<C, A : Ring<C>>(
|
||||
else Polynomial(
|
||||
other.coefficients
|
||||
.applyAndRemoveZeros {
|
||||
map { it * this@times }
|
||||
for (deg in indices) this[deg] = this@times * this[deg]
|
||||
}
|
||||
)
|
||||
|
||||
@ -245,7 +245,7 @@ public open class PolynomialSpace<C, A : Ring<C>>(
|
||||
else Polynomial(
|
||||
other.coefficients
|
||||
.applyAndRemoveZeros {
|
||||
map { it * this@times }
|
||||
for (deg in indices) this[deg] = this@times * this[deg]
|
||||
}
|
||||
)
|
||||
|
||||
@ -299,7 +299,7 @@ public open class PolynomialSpace<C, A : Ring<C>>(
|
||||
else Polynomial(
|
||||
coefficients
|
||||
.applyAndRemoveZeros {
|
||||
map { it * other }
|
||||
for (deg in indices) this[deg] = this[deg] * other
|
||||
}
|
||||
)
|
||||
|
||||
@ -452,12 +452,14 @@ public open class PolynomialSpace<C, A : Ring<C>>(
|
||||
}
|
||||
internal inline fun List<C>.applyAndRemoveZeros(block: MutableList<C>.() -> Unit) : List<C> =
|
||||
toMutableList().applyAndRemoveZeros(block)
|
||||
@Suppress("FunctionName")
|
||||
internal inline fun MutableCoefficients(size: Int, init: (index: Int) -> C): MutableList<C> {
|
||||
val list = ArrayList<C>(size)
|
||||
repeat(size) { index -> list.add(init(index)) }
|
||||
with(list) { while (elementAt(lastIndex).isZero()) removeAt(lastIndex) }
|
||||
return list
|
||||
}
|
||||
@Suppress("FunctionName")
|
||||
internal inline fun Coefficients(size: Int, init: (index: Int) -> C): List<C> = MutableCoefficients(size, init)
|
||||
@OptIn(ExperimentalTypeInference::class)
|
||||
internal inline fun buildCoefficients(@BuilderInference builderAction: MutableList<C>.() -> Unit): List<C> {
|
||||
|
@ -96,7 +96,6 @@ internal inline fun <C> multiplyAddingTo(
|
||||
target[d] += multiplicand[k] * multiplier[d - k]
|
||||
}
|
||||
|
||||
// TODO: May be apply Horner's method too?
|
||||
/**
|
||||
* Evaluates the value of the given double polynomial for given double argument.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user