-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNumberedPolynomial.kt
350 lines (334 loc) · 15 KB
/
NumberedPolynomial.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright 2018-2022 KMath contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:Suppress("NOTHING_TO_INLINE", "KotlinRedundantDiagnosticSuppress")
package space.kscience.kmath.functions
import space.kscience.kmath.operations.Ring
import space.kscience.kmath.structures.Buffer
import kotlin.jvm.JvmName
import kotlin.math.max
/**
* Represents multivariate polynomial that stores its coefficients in a [Map] and terms' signatures in a [List].
*
* @param C the type of constants.
*/
public data class NumberedPolynomial<out C>
@PublishedApi
internal constructor(
/**
* Map that contains coefficients of the polynomial.
*
* Every monomial \(a x_1^{d_1} ... x_n^{d_n}\) is stored as a pair "key-value" in the map, where the value is the
* coefficient \(a\) and the key is a list that associates index of every variable in the monomial with their degree
* in the monomial. For example, coefficients of a polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be represented as
* ```
* mapOf(
* listOf(2, 0, 3) to 5, // 5 x_1^2 x_3^3 +
* listOf(0, 1) to (-6), // (-6) x_2^1
* )
* ```
* and also as
* ```
* mapOf(
* listOf(2, 0, 3) to 5, // 5 x_1^2 x_3^3 +
* listOf(0, 1) to (-6), // (-6) x_2^1
* listOf(0, 1, 1) to 0, // 0 x_2^1 x_3^1
* )
* ```
* It is not prohibited to put extra zero monomials into the map (as for \(0 x_2 x_3\) in the example). But the
* bigger the coefficients map the worse performance of arithmetical operations performed on it. Thus, it is
* recommended not to put (or even to remove) extra (or useless) monomials in the coefficients map.
* @usesMathJax
*/
public val coefficients: Map<List<UInt>, C>
) {
override fun toString(): String = "NumberedPolynomial$coefficients"
}
/**
* Arithmetic context for multivariate polynomials with coefficients stored as a [Map] and terms' signatures stored as a
* [List] constructed with the provided [ring] of constants.
*
* @param C the type of constants. Polynomials have them a coefficients in their terms.
* @param A type of provided underlying ring of constants. It's [Ring] of [C].
* @param ring underlying ring of constants of type [A].
*/
public class NumberedPolynomialSpace<C, out A : Ring<C>>(
public override val ring: A,
) : PolynomialSpaceOverRing<C, NumberedPolynomial<C>, A> {
/**
* Returns sum of the polynomial and the integer represented as a polynomial.
*
* The operation is equivalent to adding [other] copies of unit polynomial to [this].
*/
public override operator fun NumberedPolynomial<C>.plus(other: Int): NumberedPolynomial<C> =
if (other == 0) this
else NumberedPolynomialAsIs(
coefficients.withPutOrChanged(emptyList(), other.asConstant()) { it -> it + other }
)
/**
* Returns difference between the polynomial and the integer represented as a polynomial.
*
* The operation is equivalent to subtraction [other] copies of unit polynomial from [this].
*/
public override operator fun NumberedPolynomial<C>.minus(other: Int): NumberedPolynomial<C> =
if (other == 0) this
else NumberedPolynomialAsIs(
coefficients.withPutOrChanged(emptyList(), (-other).asConstant()) { it -> it - other }
)
/**
* Returns product of the polynomial and the integer represented as a polynomial.
*
* The operation is equivalent to sum of [other] copies of [this].
*/
public override operator fun NumberedPolynomial<C>.times(other: Int): NumberedPolynomial<C> =
when (other) {
0 -> zero
1 -> this
else -> NumberedPolynomialAsIs(
coefficients.mapValues { it.value * other }
)
}
/**
* Returns sum of the integer represented as a polynomial and the polynomial.
*
* The operation is equivalent to adding [this] copies of unit polynomial to [other].
*/
public override operator fun Int.plus(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
if (this == 0) other
else NumberedPolynomialAsIs(
other.coefficients.withPutOrChanged(emptyList(), [email protected]()) { it -> this@plus + it }
)
/**
* Returns difference between the integer represented as a polynomial and the polynomial.
*
* The operation is equivalent to subtraction [this] copies of unit polynomial from [other].
*/
public override operator fun Int.minus(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
when {
this == 0 -> -other
other.coefficients.isEmpty() -> this.asPolynomial()
else -> NumberedPolynomialAsIs(
buildMap(other.coefficients.size + 1) {
put(emptyList(), other.coefficients.computeOnOrElse(emptyList(), { [email protected]() }, { it -> this@minus - it}))
other.coefficients.copyMapToBy(this, { _, c -> -c }) { currentC, _ -> currentC }
}
)
}
/**
* Returns product of the integer represented as a polynomial and the polynomial.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
public override operator fun Int.times(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
when (this) {
0 -> zero
1 -> other
else -> NumberedPolynomialAsIs(
other.coefficients.mapValues { this@times * it.value }
)
}
/**
* Returns sum of the constant represented as a polynomial and the polynomial.
*/
override operator fun C.plus(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
if (other.coefficients.isEmpty()) [email protected]()
else NumberedPolynomialAsIs(
other.coefficients.withPutOrChanged(emptyList(), this@plus) { it -> this@plus + it }
)
/**
* Returns difference between the constant represented as a polynomial and the polynomial.
*/
override operator fun C.minus(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
if (other.coefficients.isEmpty()) [email protected]()
else NumberedPolynomialAsIs(
buildMap(other.coefficients.size) {
put(emptyList(), other.coefficients.computeOnOrElse(emptyList(), this@minus) { it -> this@minus - it })
other.coefficients.copyMapToBy(this, { _, c -> -c }, { currentC, _ -> currentC })
}
)
/**
* Returns product of the constant represented as a polynomial and the polynomial.
*/
override operator fun C.times(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
NumberedPolynomialAsIs(
other.coefficients.mapValues { this@times * it.value }
)
/**
* Returns sum of the constant represented as a polynomial and the polynomial.
*/
override operator fun NumberedPolynomial<C>.plus(other: C): NumberedPolynomial<C> =
if (coefficients.isEmpty()) other.asPolynomial()
else NumberedPolynomialAsIs(
coefficients.withPutOrChanged(emptyList(), other) { it -> it + other }
)
/**
* Returns difference between the constant represented as a polynomial and the polynomial.
*/
override operator fun NumberedPolynomial<C>.minus(other: C): NumberedPolynomial<C> =
if (coefficients.isEmpty()) other.asPolynomial()
else NumberedPolynomialAsIs(
coefficients.withPutOrChanged(emptyList(), -other) { it -> it - other }
)
/**
* Returns product of the constant represented as a polynomial and the polynomial.
*/
override operator fun NumberedPolynomial<C>.times(other: C): NumberedPolynomial<C> =
NumberedPolynomialAsIs(
coefficients.mapValues { it.value * other }
)
/**
* Converts the constant [value] to polynomial.
*/
public override fun number(value: C): NumberedPolynomial<C> =
NumberedPolynomialAsIs(mapOf(emptyList<UInt>() to value))
/**
* Returns negation of the polynomial.
*/
override fun NumberedPolynomial<C>.unaryMinus(): NumberedPolynomial<C> =
NumberedPolynomialAsIs(
coefficients.mapValues { -it.value }
)
/**
* Returns sum of the polynomials.
*/
override operator fun NumberedPolynomial<C>.plus(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
NumberedPolynomialAsIs(
mergeBy(coefficients, other.coefficients) { c1, c2 -> c1 + c2 }
)
/**
* Returns difference of the polynomials.
*/
override operator fun NumberedPolynomial<C>.minus(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
NumberedPolynomialAsIs(
buildMap(coefficients.size + other.coefficients.size) {
coefficients.copyTo(this)
other.coefficients.copyMapToBy(this, { _, c -> -c }, { currentC, newC -> currentC - newC })
}
)
/**
* Returns product of the polynomials.
*/
override operator fun NumberedPolynomial<C>.times(other: NumberedPolynomial<C>): NumberedPolynomial<C> =
NumberedPolynomialAsIs(
buildMap(coefficients.size * other.coefficients.size) {
for ((degs1, c1) in coefficients) for ((degs2, c2) in other.coefficients) {
val degs =
(0..max(degs1.lastIndex, degs2.lastIndex))
.map { degs1.getOrElse(it) { 0U } + degs2.getOrElse(it) { 0U } }
val c = c1 * c2
putOrChange(degs, c) { it -> it + c }
}
}
)
/**
* Raises [arg] to the integer power [exponent].
*/ // TODO: To optimize boxing
override fun power(arg: NumberedPolynomial<C>, exponent: UInt): NumberedPolynomial<C> = super.power(arg, exponent)
/**
* Instance of zero polynomial (zero of the polynomial ring).
*/
override val zero: NumberedPolynomial<C> = NumberedPolynomialAsIs(emptyMap())
/**
* Instance of unit polynomial (unit of the polynomial ring).
*/
override val one: NumberedPolynomial<C> by lazy { NumberedPolynomialAsIs(mapOf(emptyList<UInt>() to constantOne)) }
/**
* Maximal index (ID) of variable occurring in the polynomial with positive power. If there is no such variable,
* the result is -1.
*/
public val NumberedPolynomial<C>.lastVariable: Int
get() = coefficients.keys.maxOfOrNull { degs -> degs.lastIndex } ?: -1
/**
* Degree of the polynomial, [see also](https://en.wikipedia.org/wiki/Degree_of_a_polynomial). If the polynomial is
* zero, degree is -1.
*/
override val NumberedPolynomial<C>.degree: Int
get() = coefficients.keys.maxOfOrNull { degs -> degs.sum().toInt() } ?: -1
/**
* List that associates indices of variables (that appear in the polynomial in positive exponents) with their most
* exponents in which the variables are appeared in the polynomial.
*
* As consequence all values in the list are non-negative integers. Also, if the polynomial is constant, the list is empty.
* And last index of the list is [lastVariable].
*/
public val NumberedPolynomial<C>.degrees: List<UInt>
get() =
MutableList(lastVariable + 1) { 0u }.apply {
coefficients.keys.forEach { degs ->
degs.forEachIndexed { index, deg ->
this[index] = max(this[index], deg)
}
}
}
/**
* Counts degree of the polynomial by the specified [variable].
*/
public fun NumberedPolynomial<C>.degreeBy(variable: Int): UInt =
coefficients.keys.maxOfOrNull { degs -> degs.getOrElse(variable) { 0u } } ?: 0u
/**
* Counts degree of the polynomial by the specified [variables].
*/
public fun NumberedPolynomial<C>.degreeBy(variables: Collection<Int>): UInt =
coefficients.keys.maxOfOrNull { degs ->
degs.withIndex().fold(0u) { acc, (index, value) -> if (index in variables) acc + value else acc }
} ?: 0u
/**
* Count of variables occurring in the polynomial with positive power. If there is no such variable,
* the result is 0.
*/
public val NumberedPolynomial<C>.countOfVariables: Int
get() =
MutableList(lastVariable + 1) { false }.apply {
coefficients.entries.forEach { (degs, _) ->
degs.forEachIndexed { index, deg ->
if (deg != 0u) this[index] = true
}
}
}.count { it }
// TODO: When context receivers will be ready move all of this substitutions and invocations to utilities with
// [ListPolynomialSpace] as a context receiver
/**
* Substitutes provided arguments [arguments] into [this] polynomial.
*/
public inline fun NumberedPolynomial<C>.substitute(arguments: Map<Int, C>): NumberedPolynomial<C> = substitute(ring, arguments)
/**
* Substitutes provided arguments [arguments] into [this] polynomial.
*/
@JvmName("substitutePolynomial")
public inline fun NumberedPolynomial<C>.substitute(arguments: Map<Int, NumberedPolynomial<C>>) : NumberedPolynomial<C> = substitute(ring, arguments)
/**
* Substitutes provided arguments [arguments] into [this] polynomial.
*/
public inline fun NumberedPolynomial<C>.substitute(arguments: Buffer<C>): NumberedPolynomial<C> = substitute(ring, arguments)
/**
* Substitutes provided arguments [arguments] into [this] polynomial.
*/
@JvmName("substitutePolynomial")
public inline fun NumberedPolynomial<C>.substitute(arguments: Buffer<NumberedPolynomial<C>>) : NumberedPolynomial<C> = substitute(ring, arguments)
/**
* Substitutes provided arguments [arguments] into [this] polynomial.
*/
public inline fun NumberedPolynomial<C>.substituteFully(arguments: Buffer<C>): C = this.substituteFully(ring, arguments)
/**
* Represent [this] polynomial as a regular context-less function.
*/
public inline fun NumberedPolynomial<C>.asFunction(): (Buffer<C>) -> C = asFunctionOver(ring)
/**
* Represent [this] polynomial as a regular context-less function.
*/
public inline fun NumberedPolynomial<C>.asFunctionOfConstant(): (Buffer<C>) -> C = asFunctionOfConstantOver(ring)
/**
* Represent [this] polynomial as a regular context-less function.
*/
public inline fun NumberedPolynomial<C>.asFunctionOfPolynomial(): (Buffer<NumberedPolynomial<C>>) -> NumberedPolynomial<C> = asFunctionOfPolynomialOver(ring)
/**
* Evaluates value of [this] polynomial on provided [arguments].
*/
public inline operator fun NumberedPolynomial<C>.invoke(arguments: Buffer<C>): C = substituteFully(ring, arguments)
/**
* Substitutes provided [arguments] into [this] polynomial.
*/
@JvmName("invokePolynomial")
public inline operator fun NumberedPolynomial<C>.invoke(arguments: Buffer<NumberedPolynomial<C>>): NumberedPolynomial<C> = substitute(ring, arguments)
}