Feature: Polynomials and rational functions #469

Merged
lounres merged 132 commits from feature/polynomials into dev 2022-07-28 18:04:06 +03:00
5 changed files with 67 additions and 6 deletions
Showing only changes of commit 09868f090b - Show all commits

View File

@ -6,7 +6,8 @@
package space.kscience.kmath.functions package space.kscience.kmath.functions
import space.kscience.kmath.expressions.Symbol 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.InvocationKind
import kotlin.contracts.contract import kotlin.contracts.contract
import kotlin.experimental.ExperimentalTypeInference import kotlin.experimental.ExperimentalTypeInference
@ -269,6 +270,13 @@ public class LabeledPolynomialSpace<C, A : Ring<C>>(
} }
) )
/**
* Converts the integer [value] to polynomial.
*/
public override fun number(value: Int): LabeledPolynomial<C> =
if (value == 0) zero
else LabeledPolynomial(mapOf(emptyMap<Symbol, UInt>() to constantNumber(value)))
public operator fun C.plus(other: Symbol): LabeledPolynomial<C> = public operator fun C.plus(other: Symbol): LabeledPolynomial<C> =
if (isZero()) LabeledPolynomial<C>(mapOf( if (isZero()) LabeledPolynomial<C>(mapOf(
mapOf(other to 1U) to constantOne, mapOf(other to 1U) to constantOne,

View File

@ -5,7 +5,9 @@
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.ScaleOperations
import space.kscience.kmath.operations.invoke
import kotlin.contracts.InvocationKind import kotlin.contracts.InvocationKind
import kotlin.contracts.contract import kotlin.contracts.contract
import kotlin.experimental.ExperimentalTypeInference import kotlin.experimental.ExperimentalTypeInference
@ -13,6 +15,7 @@ import kotlin.jvm.JvmName
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
/** /**
* Polynomial model without fixation on specific context they are applied to. * Polynomial model without fixation on specific context they are applied to.
* *
@ -193,6 +196,13 @@ public open class ListPolynomialSpace<C, A : Ring<C>>(
} }
) )
/**
* Converts the integer [value] to polynomial.
*/
public override fun number(value: Int): ListPolynomial<C> =
if (value == 0) zero
else ListPolynomial(constantNumber(value))
/** /**
* Returns sum of the constant represented as polynomial and the polynomial. * Returns sum of the constant represented as polynomial and the polynomial.
*/ */

View File

@ -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.ScaleOperations
import kotlin.contracts.InvocationKind import kotlin.contracts.InvocationKind
import kotlin.contracts.contract import kotlin.contracts.contract
import kotlin.experimental.ExperimentalTypeInference import kotlin.experimental.ExperimentalTypeInference
@ -221,6 +222,13 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
} }
) )
/**
* Converts the integer [value] to polynomial.
*/
public override fun number(value: Int): NumberedPolynomial<C> =
if (value == 0) zero
else NumberedPolynomial(mapOf(emptyList<UInt>() to constantNumber(value)))
/** /**
* Returns sum of the constant represented as polynomial and the polynomial. * Returns sum of the constant represented as polynomial and the polynomial.
*/ */

View File

@ -61,6 +61,11 @@ public interface PolynomialSpace<C, P: Polynomial<C>> : Ring<P> {
*/ */
public operator fun Int.times(other: C): C 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. * Returns sum of the polynomial and the integer represented as polynomial.
* *
@ -99,6 +104,11 @@ public interface PolynomialSpace<C, P: Polynomial<C>> : Ring<P> {
*/ */
public operator fun Int.times(other: P): P = multiplyBySquaring(other, this) 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. * Returns the same constant.
*/ */

View File

@ -68,6 +68,11 @@ public interface RationalFunctionalSpace<C, P: Polynomial<C>, R: RationalFunctio
*/ */
public operator fun Int.times(other: C): C 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. * Returns sum of the constant and the integer represented as polynomial.
* *
@ -106,6 +111,11 @@ public interface RationalFunctionalSpace<C, P: Polynomial<C>, R: RationalFunctio
*/ */
public operator fun Int.times(other: P): P 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. * Returns sum of the rational function and the integer represented as rational function.
* *
@ -144,6 +154,11 @@ public interface RationalFunctionalSpace<C, P: Polynomial<C>, R: RationalFunctio
*/ */
public operator fun Int.times(other: R): R = multiplyBySquaring(other, this) 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. * Returns the same constant.
*/ */
@ -672,6 +687,11 @@ public interface RationalFunctionalSpaceOverPolynomialSpace<
*/ */
public override operator fun Int.times(other: P): P = polynomialRing { this@times * other } 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. * Returns the same constant.
*/ */
@ -880,7 +900,7 @@ public abstract class PolynomialSpaceOfFractions<
P: Polynomial<C>, P: Polynomial<C>,
R: RationalFunction<C, P>, R: RationalFunction<C, P>,
> : RationalFunctionalSpace<C, P, R> { > : RationalFunctionalSpace<C, P, R> {
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. * Returns sum of the rational function and the integer represented as rational function.
@ -944,6 +964,11 @@ public abstract class PolynomialSpaceOfFractions<
other.denominator 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. * 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). * 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). * 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)
} }