Feature: Polynomials and rational functions #469
@ -19,48 +19,46 @@ public class LabeledRationalFunction<C>(
|
|||||||
|
|
||||||
// Waiting for context receivers :( TODO: Replace with context receivers when they will be available
|
// Waiting for context receivers :( TODO: Replace with context receivers when they will be available
|
||||||
|
|
||||||
//context(RationalFunctionSpace<C, A>)
|
@Suppress("FunctionName")
|
||||||
//@Suppress("FunctionName")
|
internal fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledRationalFunction(numerator: LabeledPolynomial<C>, denominator: LabeledPolynomial<C>): LabeledRationalFunction<C> =
|
||||||
//internal fun <C, A: Ring<C>> RationalFunction(numerator: Polynomial<C>, denominator: Polynomial<C>): RationalFunction<C> =
|
if (denominator.isZero()) throw ArithmeticException("/ by zero")
|
||||||
// if (denominator.isZero()) throw ArithmeticException("/ by zero")
|
else LabeledRationalFunction<C>(numerator, denominator)
|
||||||
// else RationalFunction<C>(numerator, denominator)
|
@Suppress("FunctionName")
|
||||||
//context(RationalFunctionSpace<C, A>)
|
internal fun <C, A: Ring<C>> A.LabeledRationalFunction(numerator: LabeledPolynomial<C>, denominator: LabeledPolynomial<C>): LabeledRationalFunction<C> =
|
||||||
//@Suppress("FunctionName")
|
if (denominator.coefficients.values.all { it == zero }) throw ArithmeticException("/ by zero")
|
||||||
//public fun <C, A: Ring<C>> RationalFunction(numeratorCoefficients: List<C>, denominatorCoefficients: List<C>, reverse: Boolean = false): RationalFunction<C> =
|
else LabeledRationalFunction<C>(numerator, denominator)
|
||||||
// RationalFunction<C>(
|
@Suppress("FunctionName")
|
||||||
// Polynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ),
|
public fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledRationalFunction(numeratorCoefficients: Map<Map<Symbol, UInt>, C>, denominatorCoefficients: Map<Map<Symbol, UInt>, C>): LabeledRationalFunction<C> =
|
||||||
// Polynomial( with(denominatorCoefficients) { if (reverse) reversed() else this } ).also { if (it.isZero()) }
|
if (denominatorCoefficients.values.all { it == zero }) throw ArithmeticException("/ by zero")
|
||||||
// )
|
else LabeledRationalFunction<C>(
|
||||||
//context(RationalFunctionSpace<C, A>)
|
LabeledPolynomial(numeratorCoefficients),
|
||||||
//@Suppress("FunctionName")
|
LabeledPolynomial(denominatorCoefficients)
|
||||||
//public fun <C, A: Ring<C>> RationalFunction(numerator: Polynomial<C>): RationalFunction<C> =
|
)
|
||||||
// RationalFunction(numerator, onePolynomial)
|
@Suppress("FunctionName")
|
||||||
//context(RationalFunctionSpace<C, A>)
|
public fun <C, A: Ring<C>> A.LabeledRationalFunction(numeratorCoefficients: Map<Map<Symbol, UInt>, C>, denominatorCoefficients: Map<Map<Symbol, UInt>, C>): LabeledRationalFunction<C> =
|
||||||
//@Suppress("FunctionName")
|
if (denominatorCoefficients.values.all { it == zero }) throw ArithmeticException("/ by zero")
|
||||||
//public fun <C, A: Ring<C>> RationalFunction(numeratorCoefficients: List<C>, reverse: Boolean = false): RationalFunction<C> =
|
else LabeledRationalFunction<C>(
|
||||||
// RationalFunction(
|
LabeledPolynomial(numeratorCoefficients),
|
||||||
// Polynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } )
|
LabeledPolynomial(denominatorCoefficients)
|
||||||
// )
|
)
|
||||||
|
@Suppress("FunctionName")
|
||||||
// TODO: Rewrite former constructors as fabrics
|
public fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledRationalFunction(numerator: LabeledPolynomial<C>): LabeledRationalFunction<C> =
|
||||||
//constructor(numeratorCoefficients: Map<Map<Symbol, Int>, C>, denominatorCoefficients: Map<Map<Symbol, Int>, C>) : this(
|
LabeledRationalFunction<C>(numerator, polynomialOne)
|
||||||
//LabeledPolynomial(numeratorCoefficients),
|
@Suppress("FunctionName")
|
||||||
//LabeledPolynomial(denominatorCoefficients)
|
public fun <C, A: Ring<C>> A.LabeledRationalFunction(numerator: LabeledPolynomial<C>): LabeledRationalFunction<C> =
|
||||||
//)
|
LabeledRationalFunction<C>(numerator, LabeledPolynomial(mapOf(emptyMap<Symbol, UInt>() to one)))
|
||||||
//
|
@Suppress("FunctionName")
|
||||||
//constructor(numeratorCoefficients: Collection<Pair<Map<Symbol, Int>, C>>, denominatorCoefficients: Collection<Pair<Map<Symbol, Int>, C>>) : this(
|
public fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledRationalFunction(numeratorCoefficients: Map<Map<Symbol, UInt>, C>): LabeledRationalFunction<C> =
|
||||||
//LabeledPolynomial(numeratorCoefficients),
|
LabeledRationalFunction<C>(
|
||||||
//LabeledPolynomial(denominatorCoefficients)
|
LabeledPolynomial(numeratorCoefficients),
|
||||||
//)
|
polynomialOne
|
||||||
//
|
)
|
||||||
//constructor(numerator: LabeledPolynomial<C>) : this(numerator, numerator.getOne())
|
@Suppress("FunctionName")
|
||||||
//constructor(numeratorCoefficients: Map<Map<Symbol, Int>, C>) : this(
|
public fun <C, A: Ring<C>> A.LabeledRationalFunction(numeratorCoefficients: Map<Map<Symbol, UInt>, C>): LabeledRationalFunction<C> =
|
||||||
//LabeledPolynomial(numeratorCoefficients)
|
LabeledRationalFunction<C>(
|
||||||
//)
|
LabeledPolynomial(numeratorCoefficients),
|
||||||
//
|
LabeledPolynomial(mapOf(emptyMap<Symbol, UInt>() to one))
|
||||||
//constructor(numeratorCoefficients: Collection<Pair<Map<Symbol, Int>, C>>) : this(
|
)
|
||||||
//LabeledPolynomial(numeratorCoefficients)
|
|
||||||
//)
|
|
||||||
|
|
||||||
public class LabeledRationalFunctionSpace<C, A: Ring<C>>(
|
public class LabeledRationalFunctionSpace<C, A: Ring<C>>(
|
||||||
public val ring: A,
|
public val ring: A,
|
||||||
|
@ -17,28 +17,46 @@ public data class ListRationalFunction<C> internal constructor (
|
|||||||
|
|
||||||
// Waiting for context receivers :( TODO: Replace with context receivers when they will be available
|
// Waiting for context receivers :( TODO: Replace with context receivers when they will be available
|
||||||
|
|
||||||
//context(RationalFunctionSpace<C, A>)
|
@Suppress("FunctionName")
|
||||||
//@Suppress("FunctionName")
|
internal fun <C, A: Ring<C>> ListRationalFunctionSpace<C, A>.ListRationalFunction(numerator: ListPolynomial<C>, denominator: ListPolynomial<C>): ListRationalFunction<C> =
|
||||||
//internal fun <C, A: Ring<C>> RationalFunction(numerator: Polynomial<C>, denominator: Polynomial<C>): RationalFunction<C> =
|
if (denominator.isZero()) throw ArithmeticException("/ by zero")
|
||||||
// if (denominator.isZero()) throw ArithmeticException("/ by zero")
|
else ListRationalFunction<C>(numerator, denominator)
|
||||||
// else RationalFunction<C>(numerator, denominator)
|
@Suppress("FunctionName")
|
||||||
//context(RationalFunctionSpace<C, A>)
|
internal fun <C, A: Ring<C>> A.ListRationalFunction(numerator: ListPolynomial<C>, denominator: ListPolynomial<C>): ListRationalFunction<C> =
|
||||||
//@Suppress("FunctionName")
|
if (denominator.coefficients.all { it == zero }) throw ArithmeticException("/ by zero")
|
||||||
//public fun <C, A: Ring<C>> RationalFunction(numeratorCoefficients: List<C>, denominatorCoefficients: List<C>, reverse: Boolean = false): RationalFunction<C> =
|
else ListRationalFunction<C>(numerator, denominator)
|
||||||
// RationalFunction<C>(
|
@Suppress("FunctionName")
|
||||||
// Polynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ),
|
public fun <C, A: Ring<C>> ListRationalFunctionSpace<C, A>.ListRationalFunction(numeratorCoefficients: List<C>, denominatorCoefficients: List<C>, reverse: Boolean = false): ListRationalFunction<C> =
|
||||||
// Polynomial( with(denominatorCoefficients) { if (reverse) reversed() else this } ).also { if (it.isZero()) }
|
if (denominatorCoefficients.all { it == zero }) throw ArithmeticException("/ by zero")
|
||||||
// )
|
else ListRationalFunction<C>(
|
||||||
//context(RationalFunctionSpace<C, A>)
|
ListPolynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ),
|
||||||
//@Suppress("FunctionName")
|
ListPolynomial( with(denominatorCoefficients) { if (reverse) reversed() else this } )
|
||||||
//public fun <C, A: Ring<C>> RationalFunction(numerator: Polynomial<C>): RationalFunction<C> =
|
)
|
||||||
// RationalFunction(numerator, onePolynomial)
|
@Suppress("FunctionName")
|
||||||
//context(RationalFunctionSpace<C, A>)
|
public fun <C, A: Ring<C>> A.ListRationalFunction(numeratorCoefficients: List<C>, denominatorCoefficients: List<C>, reverse: Boolean = false): ListRationalFunction<C> =
|
||||||
//@Suppress("FunctionName")
|
if (denominatorCoefficients.all { it == zero }) throw ArithmeticException("/ by zero")
|
||||||
//public fun <C, A: Ring<C>> RationalFunction(numeratorCoefficients: List<C>, reverse: Boolean = false): RationalFunction<C> =
|
else ListRationalFunction<C>(
|
||||||
// RationalFunction(
|
ListPolynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ),
|
||||||
// Polynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } )
|
ListPolynomial( with(denominatorCoefficients) { if (reverse) reversed() else this } )
|
||||||
// )
|
)
|
||||||
|
@Suppress("FunctionName")
|
||||||
|
public fun <C, A: Ring<C>> ListRationalFunctionSpace<C, A>.ListRationalFunction(numerator: ListPolynomial<C>): ListRationalFunction<C> =
|
||||||
|
ListRationalFunction<C>(numerator, polynomialOne)
|
||||||
|
@Suppress("FunctionName")
|
||||||
|
public fun <C, A: Ring<C>> A.ListRationalFunction(numerator: ListPolynomial<C>): ListRationalFunction<C> =
|
||||||
|
ListRationalFunction<C>(numerator, ListPolynomial(listOf(one)))
|
||||||
|
@Suppress("FunctionName")
|
||||||
|
public fun <C, A: Ring<C>> ListRationalFunctionSpace<C, A>.ListRationalFunction(numeratorCoefficients: List<C>, reverse: Boolean = false): ListRationalFunction<C> =
|
||||||
|
ListRationalFunction<C>(
|
||||||
|
ListPolynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ),
|
||||||
|
polynomialOne
|
||||||
|
)
|
||||||
|
@Suppress("FunctionName")
|
||||||
|
public fun <C, A: Ring<C>> A.ListRationalFunction(numeratorCoefficients: List<C>, reverse: Boolean = false): ListRationalFunction<C> =
|
||||||
|
ListRationalFunction<C>(
|
||||||
|
ListPolynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ),
|
||||||
|
ListPolynomial(listOf(one))
|
||||||
|
)
|
||||||
|
|
||||||
public class ListRationalFunctionSpace<C, A : Ring<C>> (
|
public class ListRationalFunctionSpace<C, A : Ring<C>> (
|
||||||
public val ring: A,
|
public val ring: A,
|
||||||
|
@ -5,7 +5,8 @@
|
|||||||
|
|
||||||
package space.kscience.kmath.functions
|
package space.kscience.kmath.functions
|
||||||
|
|
||||||
import space.kscience.kmath.operations.*
|
import space.kscience.kmath.operations.Ring
|
||||||
|
import space.kscience.kmath.operations.invoke
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
|
||||||
|
|
||||||
@ -18,45 +19,46 @@ public class NumberedRationalFunction<C> internal constructor(
|
|||||||
|
|
||||||
// Waiting for context receivers :( TODO: Replace with context receivers when they will be available
|
// Waiting for context receivers :( TODO: Replace with context receivers when they will be available
|
||||||
|
|
||||||
//context(RationalFunctionSpace<C, A>)
|
@Suppress("FunctionName")
|
||||||
//@Suppress("FunctionName")
|
internal fun <C, A: Ring<C>> NumberedRationalFunctionSpace<C, A>.NumberedRationalFunction(numerator: NumberedPolynomial<C>, denominator: NumberedPolynomial<C>): NumberedRationalFunction<C> =
|
||||||
//internal fun <C, A: Ring<C>> RationalFunction(numerator: Polynomial<C>, denominator: Polynomial<C>): RationalFunction<C> =
|
if (denominator.isZero()) throw ArithmeticException("/ by zero")
|
||||||
// if (denominator.isZero()) throw ArithmeticException("/ by zero")
|
else NumberedRationalFunction<C>(numerator, denominator)
|
||||||
// else RationalFunction<C>(numerator, denominator)
|
@Suppress("FunctionName")
|
||||||
//context(RationalFunctionSpace<C, A>)
|
internal fun <C, A: Ring<C>> A.NumberedRationalFunction(numerator: NumberedPolynomial<C>, denominator: NumberedPolynomial<C>): NumberedRationalFunction<C> =
|
||||||
//@Suppress("FunctionName")
|
if (denominator.coefficients.values.all { it == zero }) throw ArithmeticException("/ by zero")
|
||||||
//public fun <C, A: Ring<C>> RationalFunction(numeratorCoefficients: List<C>, denominatorCoefficients: List<C>, reverse: Boolean = false): RationalFunction<C> =
|
else NumberedRationalFunction<C>(numerator, denominator)
|
||||||
// RationalFunction<C>(
|
@Suppress("FunctionName")
|
||||||
// Polynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ),
|
public fun <C, A: Ring<C>> NumberedRationalFunctionSpace<C, A>.NumberedRationalFunction(numeratorCoefficients: Map<List<UInt>, C>, denominatorCoefficients: Map<List<UInt>, C>): NumberedRationalFunction<C> =
|
||||||
// Polynomial( with(denominatorCoefficients) { if (reverse) reversed() else this } ).also { if (it.isZero()) }
|
if (denominatorCoefficients.values.all { it == zero }) throw ArithmeticException("/ by zero")
|
||||||
// )
|
else NumberedRationalFunction<C>(
|
||||||
//context(RationalFunctionSpace<C, A>)
|
NumberedPolynomial(numeratorCoefficients),
|
||||||
//@Suppress("FunctionName")
|
NumberedPolynomial(denominatorCoefficients)
|
||||||
//public fun <C, A: Ring<C>> RationalFunction(numerator: Polynomial<C>): RationalFunction<C> =
|
)
|
||||||
// RationalFunction(numerator, onePolynomial)
|
@Suppress("FunctionName")
|
||||||
//context(RationalFunctionSpace<C, A>)
|
public fun <C, A: Ring<C>> A.NumberedRationalFunction(numeratorCoefficients: Map<List<UInt>, C>, denominatorCoefficients: Map<List<UInt>, C>): NumberedRationalFunction<C> =
|
||||||
//@Suppress("FunctionName")
|
if (denominatorCoefficients.values.all { it == zero }) throw ArithmeticException("/ by zero")
|
||||||
//public fun <C, A: Ring<C>> RationalFunction(numeratorCoefficients: List<C>, reverse: Boolean = false): RationalFunction<C> =
|
else NumberedRationalFunction<C>(
|
||||||
// RationalFunction(
|
NumberedPolynomial(numeratorCoefficients),
|
||||||
// Polynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } )
|
NumberedPolynomial(denominatorCoefficients)
|
||||||
// )
|
)
|
||||||
|
@Suppress("FunctionName")
|
||||||
// TODO: Rewrite former constructors as fabrics
|
public fun <C, A: Ring<C>> NumberedRationalFunctionSpace<C, A>.NumberedRationalFunction(numerator: NumberedPolynomial<C>): NumberedRationalFunction<C> =
|
||||||
//constructor(numeratorCoefficients: Map<List<Int>, C>, denominatorCoefficients: Map<List<Int>, C>) : this(
|
NumberedRationalFunction<C>(numerator, polynomialOne)
|
||||||
//Polynomial(numeratorCoefficients),
|
@Suppress("FunctionName")
|
||||||
//Polynomial(denominatorCoefficients)
|
public fun <C, A: Ring<C>> A.NumberedRationalFunction(numerator: NumberedPolynomial<C>): NumberedRationalFunction<C> =
|
||||||
//)
|
NumberedRationalFunction<C>(numerator, NumberedPolynomial(mapOf(emptyList<UInt>() to one)))
|
||||||
//constructor(numeratorCoefficients: Collection<Pair<List<Int>, C>>, denominatorCoefficients: Collection<Pair<List<Int>, C>>) : this(
|
@Suppress("FunctionName")
|
||||||
//Polynomial(numeratorCoefficients),
|
public fun <C, A: Ring<C>> NumberedRationalFunctionSpace<C, A>.NumberedRationalFunction(numeratorCoefficients: Map<List<UInt>, C>): NumberedRationalFunction<C> =
|
||||||
//Polynomial(denominatorCoefficients)
|
NumberedRationalFunction<C>(
|
||||||
//)
|
NumberedPolynomial(numeratorCoefficients),
|
||||||
//constructor(numerator: Polynomial<C>) : this(numerator, numerator.getOne())
|
polynomialOne
|
||||||
//constructor(numeratorCoefficients: Map<List<Int>, C>) : this(
|
)
|
||||||
//Polynomial(numeratorCoefficients)
|
@Suppress("FunctionName")
|
||||||
//)
|
public fun <C, A: Ring<C>> A.NumberedRationalFunction(numeratorCoefficients: Map<List<UInt>, C>): NumberedRationalFunction<C> =
|
||||||
//constructor(numeratorCoefficients: Collection<Pair<List<Int>, C>>) : this(
|
NumberedRationalFunction<C>(
|
||||||
//Polynomial(numeratorCoefficients)
|
NumberedPolynomial(numeratorCoefficients),
|
||||||
//)
|
NumberedPolynomial(mapOf(emptyList<UInt>() to one))
|
||||||
|
)
|
||||||
|
|
||||||
public class NumberedRationalFunctionSpace<C, A: Ring<C>> (
|
public class NumberedRationalFunctionSpace<C, A: Ring<C>> (
|
||||||
public val ring: A,
|
public val ring: A,
|
||||||
@ -156,24 +158,6 @@ public class NumberedRationalFunctionSpace<C, A: Ring<C>> (
|
|||||||
|
|
||||||
// TODO: Разобрать
|
// TODO: Разобрать
|
||||||
|
|
||||||
public operator fun NumberedRationalFunction<C>.div(other: NumberedRationalFunction<C>): NumberedRationalFunction<C> =
|
|
||||||
NumberedRationalFunction(
|
|
||||||
numerator * other.denominator,
|
|
||||||
denominator * other.numerator
|
|
||||||
)
|
|
||||||
|
|
||||||
public operator fun NumberedRationalFunction<C>.div(other: NumberedPolynomial<C>): NumberedRationalFunction<C> =
|
|
||||||
NumberedRationalFunction(
|
|
||||||
numerator,
|
|
||||||
denominator * other
|
|
||||||
)
|
|
||||||
|
|
||||||
public operator fun NumberedRationalFunction<C>.div(other: C): NumberedRationalFunction<C> =
|
|
||||||
NumberedRationalFunction(
|
|
||||||
numerator,
|
|
||||||
denominator * other
|
|
||||||
)
|
|
||||||
|
|
||||||
// operator fun invoke(arg: Map<Int, C>): NumberedRationalFunction<C> =
|
// operator fun invoke(arg: Map<Int, C>): NumberedRationalFunction<C> =
|
||||||
// NumberedRationalFunction(
|
// NumberedRationalFunction(
|
||||||
// numerator(arg),
|
// numerator(arg),
|
||||||
|
Loading…
Reference in New Issue
Block a user