diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/LabeledRationalFunction.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/LabeledRationalFunction.kt index 735e04a48..599660b52 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/LabeledRationalFunction.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/LabeledRationalFunction.kt @@ -19,48 +19,46 @@ public class LabeledRationalFunction( // Waiting for context receivers :( TODO: Replace with context receivers when they will be available -//context(RationalFunctionSpace) -//@Suppress("FunctionName") -//internal fun > RationalFunction(numerator: Polynomial, denominator: Polynomial): RationalFunction = -// if (denominator.isZero()) throw ArithmeticException("/ by zero") -// else RationalFunction(numerator, denominator) -//context(RationalFunctionSpace) -//@Suppress("FunctionName") -//public fun > RationalFunction(numeratorCoefficients: List, denominatorCoefficients: List, reverse: Boolean = false): RationalFunction = -// RationalFunction( -// Polynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ), -// Polynomial( with(denominatorCoefficients) { if (reverse) reversed() else this } ).also { if (it.isZero()) } -// ) -//context(RationalFunctionSpace) -//@Suppress("FunctionName") -//public fun > RationalFunction(numerator: Polynomial): RationalFunction = -// RationalFunction(numerator, onePolynomial) -//context(RationalFunctionSpace) -//@Suppress("FunctionName") -//public fun > RationalFunction(numeratorCoefficients: List, reverse: Boolean = false): RationalFunction = -// RationalFunction( -// Polynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ) -// ) - -// TODO: Rewrite former constructors as fabrics -//constructor(numeratorCoefficients: Map, C>, denominatorCoefficients: Map, C>) : this( -//LabeledPolynomial(numeratorCoefficients), -//LabeledPolynomial(denominatorCoefficients) -//) -// -//constructor(numeratorCoefficients: Collection, C>>, denominatorCoefficients: Collection, C>>) : this( -//LabeledPolynomial(numeratorCoefficients), -//LabeledPolynomial(denominatorCoefficients) -//) -// -//constructor(numerator: LabeledPolynomial) : this(numerator, numerator.getOne()) -//constructor(numeratorCoefficients: Map, C>) : this( -//LabeledPolynomial(numeratorCoefficients) -//) -// -//constructor(numeratorCoefficients: Collection, C>>) : this( -//LabeledPolynomial(numeratorCoefficients) -//) +@Suppress("FunctionName") +internal fun > LabeledRationalFunctionSpace.LabeledRationalFunction(numerator: LabeledPolynomial, denominator: LabeledPolynomial): LabeledRationalFunction = + if (denominator.isZero()) throw ArithmeticException("/ by zero") + else LabeledRationalFunction(numerator, denominator) +@Suppress("FunctionName") +internal fun > A.LabeledRationalFunction(numerator: LabeledPolynomial, denominator: LabeledPolynomial): LabeledRationalFunction = + if (denominator.coefficients.values.all { it == zero }) throw ArithmeticException("/ by zero") + else LabeledRationalFunction(numerator, denominator) +@Suppress("FunctionName") +public fun > LabeledRationalFunctionSpace.LabeledRationalFunction(numeratorCoefficients: Map, C>, denominatorCoefficients: Map, C>): LabeledRationalFunction = + if (denominatorCoefficients.values.all { it == zero }) throw ArithmeticException("/ by zero") + else LabeledRationalFunction( + LabeledPolynomial(numeratorCoefficients), + LabeledPolynomial(denominatorCoefficients) + ) +@Suppress("FunctionName") +public fun > A.LabeledRationalFunction(numeratorCoefficients: Map, C>, denominatorCoefficients: Map, C>): LabeledRationalFunction = + if (denominatorCoefficients.values.all { it == zero }) throw ArithmeticException("/ by zero") + else LabeledRationalFunction( + LabeledPolynomial(numeratorCoefficients), + LabeledPolynomial(denominatorCoefficients) + ) +@Suppress("FunctionName") +public fun > LabeledRationalFunctionSpace.LabeledRationalFunction(numerator: LabeledPolynomial): LabeledRationalFunction = + LabeledRationalFunction(numerator, polynomialOne) +@Suppress("FunctionName") +public fun > A.LabeledRationalFunction(numerator: LabeledPolynomial): LabeledRationalFunction = + LabeledRationalFunction(numerator, LabeledPolynomial(mapOf(emptyMap() to one))) +@Suppress("FunctionName") +public fun > LabeledRationalFunctionSpace.LabeledRationalFunction(numeratorCoefficients: Map, C>): LabeledRationalFunction = + LabeledRationalFunction( + LabeledPolynomial(numeratorCoefficients), + polynomialOne + ) +@Suppress("FunctionName") +public fun > A.LabeledRationalFunction(numeratorCoefficients: Map, C>): LabeledRationalFunction = + LabeledRationalFunction( + LabeledPolynomial(numeratorCoefficients), + LabeledPolynomial(mapOf(emptyMap() to one)) + ) public class LabeledRationalFunctionSpace>( public val ring: A, diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/ListRationalFunction.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/ListRationalFunction.kt index 8bd925c46..67c7e9fa2 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/ListRationalFunction.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/ListRationalFunction.kt @@ -17,28 +17,46 @@ public data class ListRationalFunction internal constructor ( // Waiting for context receivers :( TODO: Replace with context receivers when they will be available -//context(RationalFunctionSpace) -//@Suppress("FunctionName") -//internal fun > RationalFunction(numerator: Polynomial, denominator: Polynomial): RationalFunction = -// if (denominator.isZero()) throw ArithmeticException("/ by zero") -// else RationalFunction(numerator, denominator) -//context(RationalFunctionSpace) -//@Suppress("FunctionName") -//public fun > RationalFunction(numeratorCoefficients: List, denominatorCoefficients: List, reverse: Boolean = false): RationalFunction = -// RationalFunction( -// Polynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ), -// Polynomial( with(denominatorCoefficients) { if (reverse) reversed() else this } ).also { if (it.isZero()) } -// ) -//context(RationalFunctionSpace) -//@Suppress("FunctionName") -//public fun > RationalFunction(numerator: Polynomial): RationalFunction = -// RationalFunction(numerator, onePolynomial) -//context(RationalFunctionSpace) -//@Suppress("FunctionName") -//public fun > RationalFunction(numeratorCoefficients: List, reverse: Boolean = false): RationalFunction = -// RationalFunction( -// Polynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ) -// ) +@Suppress("FunctionName") +internal fun > ListRationalFunctionSpace.ListRationalFunction(numerator: ListPolynomial, denominator: ListPolynomial): ListRationalFunction = + if (denominator.isZero()) throw ArithmeticException("/ by zero") + else ListRationalFunction(numerator, denominator) +@Suppress("FunctionName") +internal fun > A.ListRationalFunction(numerator: ListPolynomial, denominator: ListPolynomial): ListRationalFunction = + if (denominator.coefficients.all { it == zero }) throw ArithmeticException("/ by zero") + else ListRationalFunction(numerator, denominator) +@Suppress("FunctionName") +public fun > ListRationalFunctionSpace.ListRationalFunction(numeratorCoefficients: List, denominatorCoefficients: List, reverse: Boolean = false): ListRationalFunction = + if (denominatorCoefficients.all { it == zero }) throw ArithmeticException("/ by zero") + else ListRationalFunction( + ListPolynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ), + ListPolynomial( with(denominatorCoefficients) { if (reverse) reversed() else this } ) + ) +@Suppress("FunctionName") +public fun > A.ListRationalFunction(numeratorCoefficients: List, denominatorCoefficients: List, reverse: Boolean = false): ListRationalFunction = + if (denominatorCoefficients.all { it == zero }) throw ArithmeticException("/ by zero") + else ListRationalFunction( + ListPolynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ), + ListPolynomial( with(denominatorCoefficients) { if (reverse) reversed() else this } ) + ) +@Suppress("FunctionName") +public fun > ListRationalFunctionSpace.ListRationalFunction(numerator: ListPolynomial): ListRationalFunction = + ListRationalFunction(numerator, polynomialOne) +@Suppress("FunctionName") +public fun > A.ListRationalFunction(numerator: ListPolynomial): ListRationalFunction = + ListRationalFunction(numerator, ListPolynomial(listOf(one))) +@Suppress("FunctionName") +public fun > ListRationalFunctionSpace.ListRationalFunction(numeratorCoefficients: List, reverse: Boolean = false): ListRationalFunction = + ListRationalFunction( + ListPolynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ), + polynomialOne + ) +@Suppress("FunctionName") +public fun > A.ListRationalFunction(numeratorCoefficients: List, reverse: Boolean = false): ListRationalFunction = + ListRationalFunction( + ListPolynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ), + ListPolynomial(listOf(one)) + ) public class ListRationalFunctionSpace> ( public val ring: A, diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedRationalFunction.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedRationalFunction.kt index f3b90b5c5..b32f01f2a 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedRationalFunction.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedRationalFunction.kt @@ -5,7 +5,8 @@ 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 @@ -18,45 +19,46 @@ public class NumberedRationalFunction internal constructor( // Waiting for context receivers :( TODO: Replace with context receivers when they will be available -//context(RationalFunctionSpace) -//@Suppress("FunctionName") -//internal fun > RationalFunction(numerator: Polynomial, denominator: Polynomial): RationalFunction = -// if (denominator.isZero()) throw ArithmeticException("/ by zero") -// else RationalFunction(numerator, denominator) -//context(RationalFunctionSpace) -//@Suppress("FunctionName") -//public fun > RationalFunction(numeratorCoefficients: List, denominatorCoefficients: List, reverse: Boolean = false): RationalFunction = -// RationalFunction( -// Polynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ), -// Polynomial( with(denominatorCoefficients) { if (reverse) reversed() else this } ).also { if (it.isZero()) } -// ) -//context(RationalFunctionSpace) -//@Suppress("FunctionName") -//public fun > RationalFunction(numerator: Polynomial): RationalFunction = -// RationalFunction(numerator, onePolynomial) -//context(RationalFunctionSpace) -//@Suppress("FunctionName") -//public fun > RationalFunction(numeratorCoefficients: List, reverse: Boolean = false): RationalFunction = -// RationalFunction( -// Polynomial( with(numeratorCoefficients) { if (reverse) reversed() else this } ) -// ) - -// TODO: Rewrite former constructors as fabrics -//constructor(numeratorCoefficients: Map, C>, denominatorCoefficients: Map, C>) : this( -//Polynomial(numeratorCoefficients), -//Polynomial(denominatorCoefficients) -//) -//constructor(numeratorCoefficients: Collection, C>>, denominatorCoefficients: Collection, C>>) : this( -//Polynomial(numeratorCoefficients), -//Polynomial(denominatorCoefficients) -//) -//constructor(numerator: Polynomial) : this(numerator, numerator.getOne()) -//constructor(numeratorCoefficients: Map, C>) : this( -//Polynomial(numeratorCoefficients) -//) -//constructor(numeratorCoefficients: Collection, C>>) : this( -//Polynomial(numeratorCoefficients) -//) +@Suppress("FunctionName") +internal fun > NumberedRationalFunctionSpace.NumberedRationalFunction(numerator: NumberedPolynomial, denominator: NumberedPolynomial): NumberedRationalFunction = + if (denominator.isZero()) throw ArithmeticException("/ by zero") + else NumberedRationalFunction(numerator, denominator) +@Suppress("FunctionName") +internal fun > A.NumberedRationalFunction(numerator: NumberedPolynomial, denominator: NumberedPolynomial): NumberedRationalFunction = + if (denominator.coefficients.values.all { it == zero }) throw ArithmeticException("/ by zero") + else NumberedRationalFunction(numerator, denominator) +@Suppress("FunctionName") +public fun > NumberedRationalFunctionSpace.NumberedRationalFunction(numeratorCoefficients: Map, C>, denominatorCoefficients: Map, C>): NumberedRationalFunction = + if (denominatorCoefficients.values.all { it == zero }) throw ArithmeticException("/ by zero") + else NumberedRationalFunction( + NumberedPolynomial(numeratorCoefficients), + NumberedPolynomial(denominatorCoefficients) + ) +@Suppress("FunctionName") +public fun > A.NumberedRationalFunction(numeratorCoefficients: Map, C>, denominatorCoefficients: Map, C>): NumberedRationalFunction = + if (denominatorCoefficients.values.all { it == zero }) throw ArithmeticException("/ by zero") + else NumberedRationalFunction( + NumberedPolynomial(numeratorCoefficients), + NumberedPolynomial(denominatorCoefficients) + ) +@Suppress("FunctionName") +public fun > NumberedRationalFunctionSpace.NumberedRationalFunction(numerator: NumberedPolynomial): NumberedRationalFunction = + NumberedRationalFunction(numerator, polynomialOne) +@Suppress("FunctionName") +public fun > A.NumberedRationalFunction(numerator: NumberedPolynomial): NumberedRationalFunction = + NumberedRationalFunction(numerator, NumberedPolynomial(mapOf(emptyList() to one))) +@Suppress("FunctionName") +public fun > NumberedRationalFunctionSpace.NumberedRationalFunction(numeratorCoefficients: Map, C>): NumberedRationalFunction = + NumberedRationalFunction( + NumberedPolynomial(numeratorCoefficients), + polynomialOne + ) +@Suppress("FunctionName") +public fun > A.NumberedRationalFunction(numeratorCoefficients: Map, C>): NumberedRationalFunction = + NumberedRationalFunction( + NumberedPolynomial(numeratorCoefficients), + NumberedPolynomial(mapOf(emptyList() to one)) + ) public class NumberedRationalFunctionSpace> ( public val ring: A, @@ -156,24 +158,6 @@ public class NumberedRationalFunctionSpace> ( // TODO: Разобрать - public operator fun NumberedRationalFunction.div(other: NumberedRationalFunction): NumberedRationalFunction = - NumberedRationalFunction( - numerator * other.denominator, - denominator * other.numerator - ) - - public operator fun NumberedRationalFunction.div(other: NumberedPolynomial): NumberedRationalFunction = - NumberedRationalFunction( - numerator, - denominator * other - ) - - public operator fun NumberedRationalFunction.div(other: C): NumberedRationalFunction = - NumberedRationalFunction( - numerator, - denominator * other - ) - // operator fun invoke(arg: Map): NumberedRationalFunction = // NumberedRationalFunction( // numerator(arg),