diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/LabeledPolynomial.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/LabeledPolynomial.kt index 968f77b20..3b4971d9c 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/LabeledPolynomial.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/LabeledPolynomial.kt @@ -6,7 +6,8 @@ package space.kscience.kmath.functions import space.kscience.kmath.expressions.Symbol -import space.kscience.kmath.operations.* +import space.kscience.kmath.operations.Ring +import space.kscience.kmath.operations.ScaleOperations import kotlin.contracts.InvocationKind import kotlin.contracts.contract import kotlin.experimental.ExperimentalTypeInference @@ -269,6 +270,13 @@ public class LabeledPolynomialSpace>( } ) + /** + * Converts the integer [value] to polynomial. + */ + public override fun number(value: Int): LabeledPolynomial = + if (value == 0) zero + else LabeledPolynomial(mapOf(emptyMap() to constantNumber(value))) + public operator fun C.plus(other: Symbol): LabeledPolynomial = if (isZero()) LabeledPolynomial(mapOf( mapOf(other to 1U) to constantOne, diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/ListPolynomial.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/ListPolynomial.kt index b97c1e0b4..f886ca19f 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/ListPolynomial.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/ListPolynomial.kt @@ -5,7 +5,9 @@ package space.kscience.kmath.functions -import space.kscience.kmath.operations.* +import space.kscience.kmath.operations.Ring +import space.kscience.kmath.operations.ScaleOperations +import space.kscience.kmath.operations.invoke import kotlin.contracts.InvocationKind import kotlin.contracts.contract import kotlin.experimental.ExperimentalTypeInference @@ -13,6 +15,7 @@ import kotlin.jvm.JvmName import kotlin.math.max import kotlin.math.min + /** * Polynomial model without fixation on specific context they are applied to. * @@ -193,6 +196,13 @@ public open class ListPolynomialSpace>( } ) + /** + * Converts the integer [value] to polynomial. + */ + public override fun number(value: Int): ListPolynomial = + if (value == 0) zero + else ListPolynomial(constantNumber(value)) + /** * Returns sum of the constant represented as polynomial and the polynomial. */ diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedPolynomial.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedPolynomial.kt index 3b9b907b7..387b6841d 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedPolynomial.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedPolynomial.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.ScaleOperations import kotlin.contracts.InvocationKind import kotlin.contracts.contract import kotlin.experimental.ExperimentalTypeInference @@ -221,6 +222,13 @@ public open class NumberedPolynomialSpace>( } ) + /** + * Converts the integer [value] to polynomial. + */ + public override fun number(value: Int): NumberedPolynomial = + if (value == 0) zero + else NumberedPolynomial(mapOf(emptyList() to constantNumber(value))) + /** * Returns sum of the constant represented as polynomial and the polynomial. */ diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Polynomial.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Polynomial.kt index b26a57a34..dc46c868a 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Polynomial.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Polynomial.kt @@ -61,6 +61,11 @@ public interface PolynomialSpace> : Ring

{ */ public operator fun Int.times(other: C): C + /** + * Converts the integer [value] to constant. + */ + public fun constantNumber(value: Int): C = constantOne * value + /** * Returns sum of the polynomial and the integer represented as polynomial. * @@ -99,6 +104,11 @@ public interface PolynomialSpace> : Ring

{ */ public operator fun Int.times(other: P): P = multiplyBySquaring(other, this) + /** + * Converts the integer [value] to polynomial. + */ + public fun number(value: Int): P = one * value + /** * Returns the same constant. */ diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/RationalFunction.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/RationalFunction.kt index 635661b8a..724d88963 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/RationalFunction.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/RationalFunction.kt @@ -68,6 +68,11 @@ public interface RationalFunctionalSpace, R: RationalFunctio */ public operator fun Int.times(other: C): C + /** + * Converts the integer [value] to constant. + */ + public fun constantNumber(value: Int): C = constantOne * value + /** * Returns sum of the constant and the integer represented as polynomial. * @@ -106,6 +111,11 @@ public interface RationalFunctionalSpace, R: RationalFunctio */ public operator fun Int.times(other: P): P + /** + * Converts the integer [value] to polynomial. + */ + public fun polynomialNumber(value: Int): P = polynomialOne * value + /** * Returns sum of the rational function and the integer represented as rational function. * @@ -144,6 +154,11 @@ public interface RationalFunctionalSpace, R: RationalFunctio */ public operator fun Int.times(other: R): R = multiplyBySquaring(other, this) + /** + * Converts the integer [value] to rational function. + */ + public fun number(value: Int): R = one * value + /** * Returns the same constant. */ @@ -672,6 +687,11 @@ public interface RationalFunctionalSpaceOverPolynomialSpace< */ public override operator fun Int.times(other: P): P = polynomialRing { this@times * other } + /** + * Converts the integer [value] to polynomial. + */ + public override fun polynomialNumber(value: Int): P = polynomialRing { number(value) } + /** * Returns the same constant. */ @@ -880,7 +900,7 @@ public abstract class PolynomialSpaceOfFractions< P: Polynomial, R: RationalFunction, > : RationalFunctionalSpace { - protected abstract fun constructRationalFunction(numerator: P, denominator: P) : R + protected abstract fun constructRationalFunction(numerator: P, denominator: P = polynomialOne) : R /** * Returns sum of the rational function and the integer represented as rational function. @@ -944,6 +964,11 @@ public abstract class PolynomialSpaceOfFractions< other.denominator ) + /** + * Converts the integer [value] to rational function. + */ + public override fun number(value: Int): R = constructRationalFunction(polynomialNumber(value)) + /** * Returns sum of the constant represented as rational function and the rational function. */ @@ -1076,10 +1101,10 @@ public abstract class PolynomialSpaceOfFractions< /** * Instance of zero rational function (zero of the rational functions ring). */ - public override val zero: R get() = constructRationalFunction(polynomialZero, polynomialOne) + public override val zero: R get() = constructRationalFunction(polynomialZero) /** * Instance of unit polynomial (unit of the rational functions ring). */ - public override val one: R get() = constructRationalFunction(polynomialOne, polynomialOne) + public override val one: R get() = constructRationalFunction(polynomialOne) } \ No newline at end of file