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 20 additions and 128 deletions
Showing only changes of commit 44febbdd73 - Show all commits

View File

@ -6,7 +6,6 @@
package space.kscience.kmath.functions
import space.kscience.kmath.operations.*
import kotlin.jvm.JvmName
import kotlin.math.max
import kotlin.math.min

View File

@ -1,130 +1,23 @@
package math.polynomials
/*
* Copyright 2018-2021 KMath contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
import math.ringsAndFields.*
import space.kscience.kmath.functions.LabeledRationalFunction
package space.kscience.kmath.functions
fun <T: Ring<T>> T.toLabeledRationalFunction() = LabeledRationalFunction(this.toLabeledPolynomial())
// region Operator extensions
// region Field case
fun <T: Field<T>> LabeledRationalFunction<T>.reduced(): LabeledRationalFunction<T> {
val greatestCommonDivider = polynomialGCD(numerator, denominator)
return LabeledRationalFunction(
numerator / greatestCommonDivider,
denominator / greatestCommonDivider
)
}
// endregion
// region Constants
operator fun <T: Ring<T>> T.plus(other: LabeledRationalFunction<T>) = other + this
operator fun <T: Ring<T>> Integer.plus(other: LabeledRationalFunction<T>) = other + this
operator fun <T: Ring<T>> Int.plus(other: LabeledRationalFunction<T>) = other + this
operator fun <T: Ring<T>> Long.plus(other: LabeledRationalFunction<T>) = other + this
operator fun <T: Ring<T>> T.minus(other: LabeledRationalFunction<T>) = -other + this
operator fun <T: Ring<T>> Integer.minus(other: LabeledRationalFunction<T>) = -other + this
operator fun <T: Ring<T>> Int.minus(other: LabeledRationalFunction<T>) = -other + this
operator fun <T: Ring<T>> Long.minus(other: LabeledRationalFunction<T>) = -other + this
operator fun <T: Ring<T>> T.times(other: LabeledRationalFunction<T>) = other * this
operator fun <T: Ring<T>> Integer.times(other: LabeledRationalFunction<T>) = other * this
operator fun <T: Ring<T>> Int.times(other: LabeledRationalFunction<T>) = other * this
operator fun <T: Ring<T>> Long.times(other: LabeledRationalFunction<T>) = other * this
// endregion
// region Polynomials
operator fun <T: Ring<T>> LabeledRationalFunction<T>.plus(other: UnivariatePolynomial<T>) =
LabeledRationalFunction(
numerator + denominator * other.toLabeledPolynomial(),
denominator
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.plus(other: UnivariateRationalFunction<T>) =
LabeledRationalFunction(
numerator * other.denominator.toLabeledPolynomial() + denominator * other.numerator.toLabeledPolynomial(),
denominator * other.denominator.toLabeledPolynomial()
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.plus(other: Polynomial<T>) =
LabeledRationalFunction(
numerator + denominator * other.toLabeledPolynomial(),
denominator
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.plus(other: NumberedRationalFunction<T>) =
LabeledRationalFunction(
numerator * other.denominator.toLabeledPolynomial() + denominator * other.numerator.toLabeledPolynomial(),
denominator * other.denominator.toLabeledPolynomial()
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.minus(other: UnivariatePolynomial<T>) =
LabeledRationalFunction(
numerator - denominator * other.toLabeledPolynomial(),
denominator
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.minus(other: UnivariateRationalFunction<T>) =
LabeledRationalFunction(
numerator * other.denominator.toLabeledPolynomial() - denominator * other.numerator.toLabeledPolynomial(),
denominator * other.denominator.toLabeledPolynomial()
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.minus(other: Polynomial<T>) =
LabeledRationalFunction(
numerator - denominator * other.toLabeledPolynomial(),
denominator
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.minus(other: NumberedRationalFunction<T>) =
LabeledRationalFunction(
numerator * other.denominator.toLabeledPolynomial() - denominator * other.numerator.toLabeledPolynomial(),
denominator * other.denominator.toLabeledPolynomial()
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.times(other: UnivariatePolynomial<T>) =
LabeledRationalFunction(
numerator * other.toLabeledPolynomial(),
denominator
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.times(other: UnivariateRationalFunction<T>) =
LabeledRationalFunction(
numerator * other.numerator.toLabeledPolynomial(),
denominator * other.denominator.toLabeledPolynomial()
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.times(other: Polynomial<T>) =
LabeledRationalFunction(
numerator * other.toLabeledPolynomial(),
denominator
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.times(other: NumberedRationalFunction<T>) =
LabeledRationalFunction(
numerator * other.numerator.toLabeledPolynomial(),
denominator * other.denominator.toLabeledPolynomial()
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.div(other: UnivariatePolynomial<T>) =
LabeledRationalFunction(
numerator,
denominator * other.toLabeledPolynomial()
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.div(other: UnivariateRationalFunction<T>) =
LabeledRationalFunction(
numerator * other.denominator.toLabeledPolynomial(),
denominator * other.numerator.toLabeledPolynomial()
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.div(other: Polynomial<T>) =
LabeledRationalFunction(
numerator,
denominator * other.toLabeledPolynomial()
)
operator fun <T: Ring<T>> LabeledRationalFunction<T>.div(other: NumberedRationalFunction<T>) =
LabeledRationalFunction(
numerator * other.denominator.toLabeledPolynomial(),
denominator * other.numerator.toLabeledPolynomial()
)
// endregion
// endregion
//// region Operator extensions
//
//// region Field case
//
//fun <T: Field<T>> LabeledRationalFunction<T>.reduced(): LabeledRationalFunction<T> {
// val greatestCommonDivider = polynomialGCD(numerator, denominator)
// return LabeledRationalFunction(
// numerator / greatestCommonDivider,
// denominator / greatestCommonDivider
// )
//}
//
//// endregion
//
//// endregion