Feature: Polynomials and rational functions #469

Merged
lounres merged 132 commits from feature/polynomials into dev 2022-07-28 18:04:06 +03:00
3 changed files with 122 additions and 375 deletions
Showing only changes of commit 1f9d8d34f5 - Show all commits

View File

@ -73,254 +73,59 @@ internal fun Map<Variable, UInt>.cleanUp() = filterValues { it > 0U }
// region Constructors and converters // region Constructors and converters
///** //context(LabeledPolynomialSpace<C, Ring<C>>)
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from keys of received //@Suppress("FunctionName")
// * map, sums up proportional monomials, removes aero monomials, and if result is zero map adds only element in it. //internal fun <C> LabeledPolynomial(coefs: Map<Map<Variable, UInt>, C>, toCheckInput: Boolean = false) : LabeledPolynomial<C> {
// * // if (!toCheckInput) return LabeledPolynomial(coefs)
// * @param pairs Collection of pairs that represent monomials.
// * @param toCheckInput If it's `true` cleaning of [coefficients] is executed otherwise it is not.
// *
// * @throws LabeledPolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(A)
//internal fun <C, A: Ring<C>> LabeledPolynomial(coefs: Map<Map<Variable, UInt>, C>, toCheckInput: Boolean = true): LabeledPolynomial<C> {
// if (!toCheckInput) return LabeledPolynomial<C>(coefs)
// //
// // Map for cleaned coefficients.
// val fixedCoefs = mutableMapOf<Map<Variable, UInt>, C>() // val fixedCoefs = mutableMapOf<Map<Variable, UInt>, C>()
// //
// // Cleaning the degrees, summing monomials of the same degrees.
// for (entry in coefs) { // for (entry in coefs) {
// val key = entry.key.cleanUp() // val key = entry.key.cleanUp()
// val value = entry.value // val value = entry.value
// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value // fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value
// } // }
// //
// // Removing zero monomials. // return LabeledPolynomial(
// return LabeledPolynomial<C>( // fixedCoefs.filterValues { it.isNotZero() }
// fixedCoefs
// .filter { it.value.isNotZero() }
// ) // )
//} //}
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from keys of received
// * map, sums up proportional monomials, removes aero monomials, and if result is zero map adds only element in it.
// *
// * @param pairs Collection of pairs that represent monomials.
// * @param toCheckInput If it's `true` cleaning of [coefficients] is executed otherwise it is not.
// *
// * @throws LabeledPolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(A)
//internal fun <C, A: Ring<C>> LabeledPolynomial(pairs: Collection<Pair<Map<Variable, UInt>, C>>, toCheckInput: Boolean): LabeledPolynomial<C> {
// if (!toCheckInput) return LabeledPolynomial<C>(pairs.toMap())
// //
// // Map for cleaned coefficients. //context(LabeledPolynomialSpace<C, Ring<C>>)
//@Suppress("FunctionName")
//internal fun <C> LabeledPolynomial(pairs: Collection<Pair<Map<Variable, UInt>, C>>, toCheckInput: Boolean = false) : LabeledPolynomial<C> {
// if (!toCheckInput) return LabeledPolynomial(pairs.toMap())
//
// val fixedCoefs = mutableMapOf<Map<Variable, UInt>, C>() // val fixedCoefs = mutableMapOf<Map<Variable, UInt>, C>()
// //
// // Cleaning the degrees, summing monomials of the same degrees.
// for (entry in pairs) { // for (entry in pairs) {
// val key = entry.first.cleanUp() // val key = entry.first.cleanUp()
// val value = entry.second // val value = entry.second
// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value // fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value
// } // }
// //
// // Removing zero monomials. // return LabeledPolynomial(
// return LabeledPolynomial<C>(
// fixedCoefs.filterValues { it.isNotZero() } // fixedCoefs.filterValues { it.isNotZero() }
// ) // )
//} //}
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from keys of received
// * map, sums up proportional monomials, removes aero monomials, and if result is zero map adds only element in it.
// *
// * @param pairs Collection of pairs that represents monomials.
// * @param toCheckInput If it's `true` cleaning of [coefficients] is executed otherwise it is not.
// *
// * @throws LabeledPolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(A)
//internal fun <C, A: Ring<C>> LabeledPolynomial(vararg pairs: Pair<Map<Variable, UInt>, C>, toCheckInput: Boolean): LabeledPolynomial<C> {
// if (!toCheckInput) return LabeledPolynomial<C>(pairs.toMap())
// //
// // Map for cleaned coefficients. //// TODO: Do not know how to make it without context receivers
// val fixedCoefs = mutableMapOf<Map<Variable, UInt>, C>() //context(LabeledPolynomialSpace<C, Ring<C>>)
//@Suppress("FunctionName")
//public fun <C> LabeledPolynomial(coefs: Map<Map<Variable, UInt>, C>) : LabeledPolynomial<C> = LabeledPolynomial(coefs, toCheckInput = true)
// //
// // Cleaning the degrees, summing monomials of the same degrees. //context(LabeledPolynomialSpace<C, Ring<C>>)
// for (entry in pairs) { //@Suppress("FunctionName")
// val key = entry.first.cleanUp() //public fun <C> LabeledPolynomial(pairs: Collection<Pair<Map<Variable, UInt>, C>>) : LabeledPolynomial<C> = LabeledPolynomial(pairs, toCheckInput = true)
// val value = entry.second
// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value
// }
// //
// // Removing zero monomials. //context(LabeledPolynomialSpace<C, Ring<C>>)
// return LabeledPolynomial<C>( //@Suppress("FunctionName")
// fixedCoefs.filterValues { it.isNotZero() } //public fun <C> LabeledPolynomial(vararg pairs: Pair<Map<Variable, UInt>, C>) : LabeledPolynomial<C> = LabeledPolynomial(pairs.toList(), toCheckInput = true)
// )
//}
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from keys of received
// * map, sums up proportional monomials, removes aero monomials, and if result is zero map adds only element in it.
// *
// * @param coefs Coefficients of the instants.
// *
// * @throws LabeledPolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(A)
//fun <C, A: Ring<C>> LabeledPolynomial(coefs: Map<Map<Variable, UInt>, C>): LabeledPolynomial<C> = LabeledPolynomial(coefs, toCheckInput = true)
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from keys of received
// * map, sums up proportional monomials, removes aero monomials, and if result is zero map adds only element in it.
// *
// * @param pairs Collection of pairs that represents monomials.
// *
// * @throws LabeledPolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(A)
//fun <C, A: Ring<C>> LabeledPolynomial(pairs: Collection<Pair<Map<Variable, UInt>, C>>): LabeledPolynomial<C> = LabeledPolynomial(pairs, toCheckInput = true)
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from keys of received
// * map, sums up proportional monomials, removes aero monomials, and if result is zero map adds only element in it.
// *
// * @param pairs Collection of pairs that represents monomials.
// *
// * @throws LabeledPolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(A)
//fun <C, A: Ring<C>> LabeledPolynomial(vararg pairs: Pair<Map<Variable, UInt>, C>): LabeledPolynomial<C> = LabeledPolynomial(*pairs, toCheckInput = true)
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from keys of received
// * map, sums up proportional monomials, removes aero monomials, and if result is zero map adds only element in it.
// *
// * @param pairs Collection of pairs that represent monomials.
// * @param toCheckInput If it's `true` cleaning of [coefficients] is executed otherwise it is not.
// *
// * @throws LabeledPolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(LabeledPolynomialSpace<C, A>)
//internal fun <C, A: Ring<C>> LabeledPolynomial(coefs: Map<Map<Variable, UInt>, C>, toCheckInput: Boolean = true): LabeledPolynomial<C> {
// if (!toCheckInput) return LabeledPolynomial<C>(coefs)
// //
// // Map for cleaned coefficients. //context(LabeledPolynomialSpace<C, Ring<C>>)
// val fixedCoefs = mutableMapOf<Map<Variable, UInt>, C>() //public fun <C> Variable.asLabeledPolynomial() : LabeledPolynomial<C> = LabeledPolynomial(mapOf(mapOf(this to 1u) to constantOne))
//
// // Cleaning the degrees, summing monomials of the same degrees. public fun <C> C.asLabeledPolynomial() : LabeledPolynomial<C> = LabeledPolynomial(mapOf(emptyMap<Variable, UInt>() to this))
// for (entry in coefs) {
// val key = entry.key.cleanUp()
// val value = entry.value
// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value
// }
//
// // Removing zero monomials.
// return LabeledPolynomial<C>(
// fixedCoefs
// .filter { it.value.isNotZero() }
// )
//}
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from keys of received
// * map, sums up proportional monomials, removes aero monomials, and if result is zero map adds only element in it.
// *
// * @param pairs Collection of pairs that represent monomials.
// * @param toCheckInput If it's `true` cleaning of [coefficients] is executed otherwise it is not.
// *
// * @throws LabeledPolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(LabeledPolynomialSpace<C, A>)
//internal fun <C, A: Ring<C>> LabeledPolynomial(pairs: Collection<Pair<Map<Variable, UInt>, C>>, toCheckInput: Boolean): LabeledPolynomial<C> {
// if (!toCheckInput) return LabeledPolynomial<C>(pairs.toMap())
//
// // Map for cleaned coefficients.
// val fixedCoefs = mutableMapOf<Map<Variable, UInt>, C>()
//
// // Cleaning the degrees, summing monomials of the same degrees.
// for (entry in pairs) {
// val key = entry.first.cleanUp()
// val value = entry.second
// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value
// }
//
// // Removing zero monomials.
// return LabeledPolynomial<C>(
// fixedCoefs.filterValues { it.isNotZero() }
// )
//}
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from keys of received
// * map, sums up proportional monomials, removes aero monomials, and if result is zero map adds only element in it.
// *
// * @param pairs Collection of pairs that represents monomials.
// * @param toCheckInput If it's `true` cleaning of [coefficients] is executed otherwise it is not.
// *
// * @throws LabeledPolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(LabeledPolynomialSpace<C, A>)
//internal fun <C, A: Ring<C>> LabeledPolynomial(vararg pairs: Pair<Map<Variable, UInt>, C>, toCheckInput: Boolean): LabeledPolynomial<C> {
// if (!toCheckInput) return LabeledPolynomial<C>(pairs.toMap())
//
// // Map for cleaned coefficients.
// val fixedCoefs = mutableMapOf<Map<Variable, UInt>, C>()
//
// // Cleaning the degrees, summing monomials of the same degrees.
// for (entry in pairs) {
// val key = entry.first.cleanUp()
// val value = entry.second
// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value
// }
//
// // Removing zero monomials.
// return LabeledPolynomial<C>(
// fixedCoefs.filterValues { it.isNotZero() }
// )
//}
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from keys of received
// * map, sums up proportional monomials, removes aero monomials, and if result is zero map adds only element in it.
// *
// * @param coefs Coefficients of the instants.
// *
// * @throws LabeledPolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(LabeledPolynomialSpace<C, A>)
//fun <C, A: Ring<C>> LabeledPolynomial(coefs: Map<Map<Variable, UInt>, C>): LabeledPolynomial<C> = LabeledPolynomial(coefs, toCheckInput = true)
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from keys of received
// * map, sums up proportional monomials, removes aero monomials, and if result is zero map adds only element in it.
// *
// * @param pairs Collection of pairs that represents monomials.
// *
// * @throws LabeledPolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(LabeledPolynomialSpace<C, A>)
//fun <C, A: Ring<C>> LabeledPolynomial(pairs: Collection<Pair<Map<Variable, UInt>, C>>): LabeledPolynomial<C> = LabeledPolynomial(pairs, toCheckInput = true)
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from keys of received
// * map, sums up proportional monomials, removes aero monomials, and if result is zero map adds only element in it.
// *
// * @param pairs Collection of pairs that represents monomials.
// *
// * @throws LabeledPolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(LabeledPolynomialSpace<C, A>)
//fun <C, A: Ring<C>> LabeledPolynomial(vararg pairs: Pair<Map<Variable, UInt>, C>): LabeledPolynomial<C> = LabeledPolynomial(*pairs, toCheckInput = true)
//
//fun <C> C.asLabeledPolynomial() : LabeledPolynomial<C> = LabeledPolynomial<C>(mapOf(emptyMap<Variable, UInt>() to this))
//
//context(A)
//fun <C, A: Ring<C>> Variable.asLabeledPolynomial() : LabeledPolynomial<C> = LabeledPolynomial<C>(mapOf(mapOf<Variable, UInt>(this to 1U) to one))
//
//context(LabeledPolynomialSpace<C, A>)
//fun <C, A: Ring<C>> Variable.asLabeledPolynomial() : LabeledPolynomial<C> = LabeledPolynomial<C>(mapOf(mapOf<Variable, UInt>(this to 1U) to constantOne))
//
//context(A)
//fun <C, A: Ring<C>> Variable.asLabeledPolynomial(c: C) : LabeledPolynomial<C> =
// if(c.isZero()) LabeledPolynomial<C>(emptyMap())
// else LabeledPolynomial<C>(mapOf(mapOf<Variable, UInt>(this to 1U) to c))
//
//context(LabeledPolynomialSpace<C, A>)
//fun <C, A: Ring<C>> Variable.asLabeledPolynomial(c: C) : LabeledPolynomial<C> =
// if(c.isZero()) zero
// else LabeledPolynomial<C>(mapOf(mapOf<Variable, UInt>(this to 1U) to c))
// endregion // endregion

View File

@ -72,100 +72,10 @@ internal fun List<UInt>.cleanUp() = subList(0, indexOfLast { it != 0U } + 1)
// endregion // endregion
// region Constructors and converters // region Constructors and converters
// Waiting for context receivers :( TODO: Replace with context receivers when they will be available
//context(A) //context(NumberedPolynomialSpace<C, Ring<C>>)
//@Suppress("FunctionName") //@Suppress("FunctionName")
//internal fun <C, A: Ring<C>> NumberedPolynomial(coefs: Map<List<UInt>, C>, toCheckInput: Boolean): NumberedPolynomial<C> { //internal fun <C> NumberedPolynomial(coefs: Map<List<UInt>, C>, toCheckInput: Boolean = false) : NumberedPolynomial<C> {
// if (!toCheckInput) return NumberedPolynomial<C>(coefs)
//
// val fixedCoefs = mutableMapOf<List<UInt>, C>()
//
// for (entry in coefs) {
// val key = entry.key.cleanUp()
// val value = entry.value
// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value
// }
//
// return NumberedPolynomial<C>(
// fixedCoefs
// .filter { it.value.isNotZero() }
// )
//}
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from end of received
// * lists, sums up proportional monomials, removes zero monomials, and if result is empty map adds only element in it.
// *
// * @param pairs Collection of pairs that represent monomials.
// * @param toCheckInput If it's `true` cleaning of [coefficients] is executed otherwise it is not.
// *
// * @throws PolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(A)
//@Suppress("FunctionName")
//internal fun <C, A: Ring<C>> NumberedPolynomial(pairs: Collection<Pair<List<UInt>, C>>, toCheckInput: Boolean): NumberedPolynomial<C> {
// if (!toCheckInput) return NumberedPolynomial(pairs.toMap())
//
// val fixedCoefs = mutableMapOf<List<UInt>, C>()
//
// for (entry in pairs) {
// val key = entry.first.cleanUp()
// val value = entry.second
// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value
// }
//
// return NumberedPolynomial<C>(
// fixedCoefs
// .filter { it.value.isNotZero() }
// )
//}
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from end of received
// * lists, sums up proportional monomials, removes zero monomials, and if result is empty map adds only element in it.
// *
// * @param pairs Collection of pairs that represent monomials.
// * @param toCheckInput If it's `true` cleaning of [coefficients] is executed otherwise it is not.
// *
// * @throws PolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(A)
//@Suppress("FunctionName")
//internal fun <C, A: Ring<C>> NumberedPolynomial(vararg pairs: Pair<List<UInt>, C>, toCheckInput: Boolean): NumberedPolynomial<C> =
// NumberedPolynomial(pairs.toMap(), toCheckInput)
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from end of received
// * lists, sums up proportional monomials, removes zero monomials, and if result is empty map adds only element in it.
// *
// * @param coefs Coefficients of the instants.
// *
// * @throws PolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(A)
//public fun <C, A: Ring<C>> NumberedPolynomial(coefs: Map<List<UInt>, C>) = NumberedPolynomial(coefs, toCheckInput = true)
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from end of received
// * lists, sums up proportional monomials, removes zero monomials, and if result is empty map adds only element in it.
// *
// * @param pairs Collection of pairs that represent monomials.
// *
// * @throws PolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(A)
//public fun <C, A: Ring<C>> NumberedPolynomial(pairs: Collection<Pair<List<UInt>, C>>) = NumberedPolynomial(pairs, toCheckInput = true)
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from end of received
// * lists, sums up proportional monomials, removes zero monomials, and if result is empty map adds only element in it.
// *
// * @param pairs Collection of pairs that represent monomials.
// *
// * @throws PolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(A)
//public fun <C, A: Ring<C>> NumberedPolynomial(vararg pairs: Pair<List<UInt>, C>) = NumberedPolynomial(*pairs, toCheckInput = true)
//
//context(NumberedPolynomialSpace<C, A>)
//@Suppress("FunctionName")
//internal fun <C, A: Ring<C>> NumberedPolynomial(coefs: Map<List<UInt>, C>, toCheckInput: Boolean): NumberedPolynomial<C> {
// if (!toCheckInput) return NumberedPolynomial(coefs) // if (!toCheckInput) return NumberedPolynomial(coefs)
// //
// val fixedCoefs = mutableMapOf<List<UInt>, C>() // val fixedCoefs = mutableMapOf<List<UInt>, C>()
@ -176,23 +86,14 @@ internal fun List<UInt>.cleanUp() = subList(0, indexOfLast { it != 0U } + 1)
// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value // fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value
// } // }
// //
// return NumberedPolynomial<C>( // return NumberedPolynomial(
// fixedCoefs // fixedCoefs.filterValues { it.isNotZero() }
// .filter { it.value.isNotZero() }
// ) // )
//} //}
///** //
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from end of received //context(NumberedPolynomialSpace<C, Ring<C>>)
// * lists, sums up proportional monomials, removes zero monomials, and if result is empty map adds only element in it.
// *
// * @param pairs Collection of pairs that represent monomials.
// * @param toCheckInput If it's `true` cleaning of [coefficients] is executed otherwise it is not.
// *
// * @throws PolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(NumberedPolynomialSpace<C, A>)
//@Suppress("FunctionName") //@Suppress("FunctionName")
//internal fun <C, A: Ring<C>> NumberedPolynomial(pairs: Collection<Pair<List<UInt>, C>>, toCheckInput: Boolean): NumberedPolynomial<C> { //internal fun <C> NumberedPolynomial(pairs: Collection<Pair<List<UInt>, C>>, toCheckInput: Boolean = false) : NumberedPolynomial<C> {
// if (!toCheckInput) return NumberedPolynomial(pairs.toMap()) // if (!toCheckInput) return NumberedPolynomial(pairs.toMap())
// //
// val fixedCoefs = mutableMapOf<List<UInt>, C>() // val fixedCoefs = mutableMapOf<List<UInt>, C>()
@ -203,56 +104,25 @@ internal fun List<UInt>.cleanUp() = subList(0, indexOfLast { it != 0U } + 1)
// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value // fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value
// } // }
// //
// return NumberedPolynomial<C>( // return NumberedPolynomial(
// fixedCoefs // fixedCoefs.filterValues { it.isNotZero() }
// .filter { it.value.isNotZero() }
// ) // )
//} //}
///** //
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from end of received //// TODO: Do not know how to make it without context receivers
// * lists, sums up proportional monomials, removes zero monomials, and if result is empty map adds only element in it. //context(NumberedPolynomialSpace<C, Ring<C>>)
// *
// * @param pairs Collection of pairs that represent monomials.
// * @param toCheckInput If it's `true` cleaning of [coefficients] is executed otherwise it is not.
// *
// * @throws PolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(NumberedPolynomialSpace<C, A>)
//@Suppress("FunctionName") //@Suppress("FunctionName")
//internal fun <C, A: Ring<C>> NumberedPolynomial(vararg pairs: Pair<List<UInt>, C>, toCheckInput: Boolean): NumberedPolynomial<C> = //public fun <C> NumberedPolynomial(coefs: Map<List<UInt>, C>) : NumberedPolynomial<C> = NumberedPolynomial(coefs, toCheckInput = true)
// NumberedPolynomial(pairs.toList(), toCheckInput) //
///** //context(NumberedPolynomialSpace<C, Ring<C>>)
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from end of received //@Suppress("FunctionName")
// * lists, sums up proportional monomials, removes zero monomials, and if result is empty map adds only element in it. //public fun <C> NumberedPolynomial(pairs: Collection<Pair<List<UInt>, C>>) : NumberedPolynomial<C> = NumberedPolynomial(pairs, toCheckInput = true)
// * //
// * @param coefs Coefficients of the instants. //context(NumberedPolynomialSpace<C, Ring<C>>)
// * //@Suppress("FunctionName")
// * @throws PolynomialError If no coefficient received or if any of degrees in any monomial is negative. //public fun <C> NumberedPolynomial(vararg pairs: Pair<List<UInt>, C>) : NumberedPolynomial<C> = NumberedPolynomial(pairs.toList(), toCheckInput = true)
// */
//context(NumberedPolynomialSpace<C, A>)
//public fun <C, A: Ring<C>> NumberedPolynomial(coefs: Map<List<UInt>, C>) = NumberedPolynomial(coefs, toCheckInput = true)
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from end of received
// * lists, sums up proportional monomials, removes zero monomials, and if result is empty map adds only element in it.
// *
// * @param pairs Collection of pairs that represent monomials.
// *
// * @throws PolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(NumberedPolynomialSpace<C, A>)
//public fun <C, A: Ring<C>> NumberedPolynomial(pairs: Collection<Pair<List<UInt>, C>>) = NumberedPolynomial(pairs, toCheckInput = true)
///**
// * Gets the coefficients in format of [coefficients] field and cleans it: removes zero degrees from end of received
// * lists, sums up proportional monomials, removes zero monomials, and if result is empty map adds only element in it.
// *
// * @param pairs Collection of pairs that represent monomials.
// *
// * @throws PolynomialError If no coefficient received or if any of degrees in any monomial is negative.
// */
//context(NumberedPolynomialSpace<C, A>)
//public fun <C, A: Ring<C>> NumberedPolynomial(vararg pairs: Pair<List<UInt>, C>) = NumberedPolynomial(*pairs, toCheckInput = true)
public fun <C, A: Ring<C>> C.asNumberedPolynomial() : NumberedPolynomial<C> = NumberedPolynomial<C>(mapOf(emptyList<UInt>() to this)) public fun <C> C.asNumberedPolynomial() : NumberedPolynomial<C> = NumberedPolynomial(mapOf(emptyList<UInt>() to this))
// endregion // endregion
@ -314,7 +184,7 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
*/ */
public override operator fun NumberedPolynomial<C>.times(other: Int): NumberedPolynomial<C> = public override operator fun NumberedPolynomial<C>.times(other: Int): NumberedPolynomial<C> =
if (other == 0) zero if (other == 0) zero
else NumberedPolynomial( else NumberedPolynomial<C>(
coefficients coefficients
.applyAndRemoveZeros { .applyAndRemoveZeros {
mapValues { (_, c) -> c * other } mapValues { (_, c) -> c * other }
@ -707,4 +577,51 @@ public open class NumberedPolynomialSpace<C, A : Ring<C>>(
} }
} }
// endregion // endregion
// region Constructors and converters
@Suppress("FunctionName")
internal fun NumberedPolynomial(coefs: Map<List<UInt>, C>, toCheckInput: Boolean = false) : NumberedPolynomial<C> {
if (!toCheckInput) return NumberedPolynomial<C>(coefs)
val fixedCoefs = mutableMapOf<List<UInt>, C>()
for (entry in coefs) {
val key = entry.key.cleanUp()
val value = entry.value
fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value
}
return NumberedPolynomial(
fixedCoefs.filterValues { it.isNotZero() }
)
}
@Suppress("FunctionName")
internal fun NumberedPolynomial(pairs: Collection<Pair<List<UInt>, C>>, toCheckInput: Boolean = false) : NumberedPolynomial<C> {
if (!toCheckInput) return NumberedPolynomial<C>(pairs.toMap())
val fixedCoefs = mutableMapOf<List<UInt>, C>()
for (entry in pairs) {
val key = entry.first.cleanUp()
val value = entry.second
fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value
}
return NumberedPolynomial(
fixedCoefs.filterValues { it.isNotZero() }
)
}
@Suppress("FunctionName")
public fun NumberedPolynomial(coefs: Map<List<UInt>, C>) : NumberedPolynomial<C> = NumberedPolynomial(coefs, toCheckInput = true)
@Suppress("FunctionName")
public fun NumberedPolynomial(pairs: Collection<Pair<List<UInt>, C>>) : NumberedPolynomial<C> = NumberedPolynomial(pairs, toCheckInput = true)
@Suppress("FunctionName")
public fun NumberedPolynomial(vararg pairs: Pair<List<UInt>, C>) : NumberedPolynomial<C> = NumberedPolynomial(pairs.toList(), toCheckInput = true)
// endregion
} }

View File

@ -14,7 +14,32 @@ import kotlin.math.min
* *
* @param coefficients constant is the leftmost coefficient. * @param coefficients constant is the leftmost coefficient.
*/ */
public data class Polynomial<C>(public val coefficients: List<C>) : AbstractPolynomial<C> { public data class Polynomial<C>(
/**
* List that collects coefficients of the polynomial. Every monomial `a x^d` is represented as a coefficients
* `a` placed into the list with index `d`. For example coefficients of polynomial `5 x^2 - 6` can be represented as
* ```
* listOf(
* -6, // -6 +
* 0, // 0 x +
* 5, // 5 x^2
* )
* ```
* and also as
* ```
* listOf(
* -6, // -6 +
* 0, // 0 x +
* 5, // 5 x^2
* 0, // 0 x^3
* 0, // 0 x^4
* )
* ```
* It is recommended not to put extra zeros at end of the list (as for `0x^3` and `0x^4` in the example), but is not
* prohibited.
*/
public val coefficients: List<C>
) : AbstractPolynomial<C> {
override fun toString(): String = "Polynomial$coefficients" override fun toString(): String = "Polynomial$coefficients"
} }