-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumberedUtil.kt
513 lines (471 loc) · 21.6 KB
/
numberedUtil.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* 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.
*/
package space.kscience.kmath.functions
import space.kscience.kmath.UnstableKMathAPI
import space.kscience.kmath.operations.Field
import space.kscience.kmath.operations.Ring
import space.kscience.kmath.operations.algebra
import space.kscience.kmath.operations.invoke
import space.kscience.kmath.structures.Buffer
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.jvm.JvmName
import kotlin.math.max
import kotlin.math.min
/**
* Creates a [NumberedPolynomialSpace] over a received ring.
*/
public inline val <C, A : Ring<C>> A.numberedPolynomialSpace: NumberedPolynomialSpace<C, A>
get() = NumberedPolynomialSpace(this)
/**
* Creates a [NumberedPolynomialSpace]'s scope over a received ring.
*/
public inline fun <C, A : Ring<C>, R> A.numberedPolynomialSpace(block: NumberedPolynomialSpace<C, A>.() -> R): R {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
return NumberedPolynomialSpace(this).block()
}
/**
* Creates a [NumberedRationalFunctionSpace] over a received ring.
*/
public inline val <C, A : Ring<C>> A.numberedRationalFunctionSpace: NumberedRationalFunctionSpace<C, A>
get() = NumberedRationalFunctionSpace(this)
/**
* Creates a [NumberedRationalFunctionSpace]'s scope over a received ring.
*/
public inline fun <C, A : Ring<C>, R> A.numberedRationalFunctionSpace(block: NumberedRationalFunctionSpace<C, A>.() -> R): R {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
return NumberedRationalFunctionSpace(this).block()
}
/**
* Substitutes provided Double arguments [args] into [this] Double polynomial.
*/
public fun NumberedPolynomial<Double>.substitute(args: Map<Int, Double>): NumberedPolynomial<Double> = Double.algebra {
NumberedPolynomial<Double>(
buildMap(coefficients.size) {
for ((degs, c) in coefficients) {
val newDegs = degs.mapIndexed { index, deg -> if (index !in args) deg else 0u }.cleanUp()
val newC = args.entries.fold(c) { product, (variable, substitution) ->
val deg = degs.getOrElse(variable) { 0u }
if (deg == 0u) product else product * substitution.pow(deg.toInt())
}
putOrChange(newDegs, newC) { it -> it + newC }
}
}
)
}
/**
* Substitutes provided arguments [args] into [this] polynomial.
*/
public fun <C> NumberedPolynomial<C>.substitute(ring: Ring<C>, args: Map<Int, C>): NumberedPolynomial<C> = ring {
NumberedPolynomial<C>(
buildMap(coefficients.size) {
for ((degs, c) in coefficients) {
val newDegs = degs.mapIndexed { index, deg -> if (index !in args) deg else 0u }.cleanUp()
val newC = args.entries.fold(c) { product, (variable, substitution) ->
val deg = degs.getOrElse(variable) { 0u }
if (deg == 0u) product else product * power(substitution, deg)
}
putOrChange(newDegs, newC) { it -> it + newC }
}
}
)
}
/**
* Substitutes provided arguments [args] into [this] polynomial.
*/ // TODO: To optimize boxing
@JvmName("substitutePolynomial")
public fun <C> NumberedPolynomial<C>.substitute(ring: Ring<C>, args: Map<Int, NumberedPolynomial<C>>) : NumberedPolynomial<C> =
ring.numberedPolynomialSpace {
coefficients.entries.fold(zero) { acc, (degs, c) ->
val newDegs = degs.mapIndexed { index, deg -> if (index !in args) deg else 0u }.cleanUp()
acc + args.entries.fold(NumberedPolynomial<C>(mapOf(newDegs to c))) { product, (variable, substitution) ->
val deg = degs.getOrElse(variable) { 0u }
if (deg == 0u) product else product * power(substitution, deg)
}
}
}
/**
* Substitutes provided arguments [args] into [this] polynomial.
*/ // TODO: To optimize boxing
@JvmName("substituteRationalFunction")
public fun <C> NumberedPolynomial<C>.substitute(ring: Ring<C>, args: Map<Int, NumberedRationalFunction<C>>) : NumberedRationalFunction<C> =
ring.numberedRationalFunctionSpace {
coefficients.entries.fold(zero) { acc, (degs, c) ->
val newDegs = degs.mapIndexed { index, deg -> if (index !in args) deg else 0u }.cleanUp()
acc + args.entries.fold(NumberedRationalFunction(NumberedPolynomial<C>(mapOf(newDegs to c)))) { product, (variable, substitution) ->
val deg = degs.getOrElse(variable) { 0u }
if (deg == 0u) product else product * power(substitution, deg)
}
}
}
/**
* Substitutes provided Double arguments [args] into [this] Double rational function.
*/
public fun NumberedRationalFunction<Double>.substitute(args: Map<Int, Double>): NumberedRationalFunction<Double> =
NumberedRationalFunction(numerator.substitute(args), denominator.substitute(args))
/**
* Substitutes provided arguments [args] into [this] rational function.
*/
public fun <C> NumberedRationalFunction<C>.substitute(ring: Ring<C>, args: Map<Int, C>): NumberedRationalFunction<C> =
NumberedRationalFunction(numerator.substitute(ring, args), denominator.substitute(ring, args))
/**
* Substitutes provided arguments [args] into [this] rational function.
*/ // TODO: To optimize calculation
@JvmName("substitutePolynomial")
public fun <C> NumberedRationalFunction<C>.substitute(ring: Ring<C>, args: Map<Int, NumberedPolynomial<C>>) : NumberedRationalFunction<C> =
NumberedRationalFunction(numerator.substitute(ring, args), denominator.substitute(ring, args))
/**
* Substitutes provided arguments [args] into [this] rational function.
*/ // TODO: To optimize calculation
@JvmName("substituteRationalFunction")
public fun <C> NumberedRationalFunction<C>.substitute(ring: Ring<C>, args: Map<Int, NumberedRationalFunction<C>>) : NumberedRationalFunction<C> =
ring.numberedRationalFunctionSpace {
numerator.substitute(ring, args) / denominator.substitute(ring, args)
}
/**
* Substitutes provided Double arguments [args] into [this] Double polynomial.
*/
public fun NumberedPolynomial<Double>.substitute(args: Buffer<Double>): NumberedPolynomial<Double> = Double.algebra {
val lastSubstitutionVariable = args.size - 1
NumberedPolynomial<Double>(
buildMap(coefficients.size) {
for ((degs, c) in coefficients) {
val lastDegsIndex = degs.lastIndex
val newDegs =
if (lastDegsIndex <= lastSubstitutionVariable) emptyList()
else degs.toMutableList().apply {
for (i in 0..lastSubstitutionVariable) this[i] = 0u
}
val newC = (0..min(lastDegsIndex, lastSubstitutionVariable)).fold(c) { product, variable ->
val deg = degs[variable]
if (deg == 0u) product else product * args[variable].pow(deg.toInt())
}
putOrChange(newDegs, newC) { it -> it + newC }
}
}
)
}
/**
* Substitutes provided arguments [args] into [this] polynomial.
*/
public fun <C> NumberedPolynomial<C>.substitute(ring: Ring<C>, args: Buffer<C>): NumberedPolynomial<C> = ring {
val lastSubstitutionVariable = args.size - 1
NumberedPolynomial<C>(
buildMap<List<UInt>, C>(coefficients.size) {
for ((degs, c) in coefficients) {
val lastDegsIndex = degs.lastIndex
val newDegs =
if (lastDegsIndex <= lastSubstitutionVariable) emptyList()
else degs.toMutableList().apply {
for (i in 0..lastSubstitutionVariable) this[i] = 0u
}
val newC = (0..min(lastDegsIndex, lastSubstitutionVariable)).fold(c) { product, variable ->
val deg = degs[variable]
if (deg == 0u) product else product * power(args[variable], deg)
}
putOrChange(newDegs, newC) { it -> it + newC }
}
}
)
}
/**
* Substitutes provided arguments [args] into [this] polynomial.
*/ // TODO: To optimize boxing
@JvmName("substitutePolynomial")
public fun <C> NumberedPolynomial<C>.substitute(ring: Ring<C>, args: Buffer<NumberedPolynomial<C>>) : NumberedPolynomial<C> =
ring.numberedPolynomialSpace {
val lastSubstitutionVariable = args.size - 1
coefficients.entries.fold(zero) { acc, (degs, c) ->
val lastDegsIndex = degs.lastIndex
val newDegs =
if (lastDegsIndex <= lastSubstitutionVariable) emptyList()
else degs.toMutableList().apply {
for (i in 0..lastSubstitutionVariable) this[i] = 0u
}
acc + (0..min(lastDegsIndex, lastSubstitutionVariable))
.fold(NumberedPolynomial<C>(mapOf(newDegs to c))) { product, variable ->
val deg = degs[variable]
if (deg == 0u) product else product * power(args[variable], deg)
}
}
}
/**
* Substitutes provided arguments [args] into [this] polynomial.
*/ // TODO: To optimize boxing
@JvmName("substituteRationalFunction")
public fun <C> NumberedPolynomial<C>.substitute(ring: Ring<C>, args: Buffer<NumberedRationalFunction<C>>) : NumberedRationalFunction<C> =
ring.numberedRationalFunctionSpace {
val lastSubstitutionVariable = args.size - 1
coefficients.entries.fold(zero) { acc, (degs, c) ->
val lastDegsIndex = degs.lastIndex
val newDegs =
if (lastDegsIndex <= lastSubstitutionVariable) emptyList()
else degs.toMutableList().apply {
for (i in 0..lastSubstitutionVariable) this[i] = 0u
}
acc + (0..min(lastDegsIndex, lastSubstitutionVariable))
.fold(NumberedRationalFunction(NumberedPolynomial<C>(mapOf(newDegs to c)))) { product, variable ->
val deg = degs[variable]
if (deg == 0u) product else product * power(args[variable], deg)
}
}
}
/**
* Substitutes provided Double arguments [args] into [this] Double rational function.
*/
public fun NumberedRationalFunction<Double>.substitute(args: Buffer<Double>): NumberedRationalFunction<Double> =
NumberedRationalFunction(numerator.substitute(args), denominator.substitute(args))
/**
* Substitutes provided arguments [args] into [this] rational function.
*/
public fun <C> NumberedRationalFunction<C>.substitute(ring: Ring<C>, args: Buffer<C>): NumberedRationalFunction<C> =
NumberedRationalFunction(numerator.substitute(ring, args), denominator.substitute(ring, args))
/**
* Substitutes provided arguments [args] into [this] rational function.
*/ // TODO: To optimize calculation
@JvmName("substitutePolynomial")
public fun <C> NumberedRationalFunction<C>.substitute(ring: Ring<C>, args: Buffer<NumberedPolynomial<C>>) : NumberedRationalFunction<C> =
NumberedRationalFunction(numerator.substitute(ring, args), denominator.substitute(ring, args))
/**
* Substitutes provided arguments [args] into [this] rational function.
*/ // TODO: To optimize calculation
@JvmName("substituteRationalFunction")
public fun <C> NumberedRationalFunction<C>.substitute(ring: Ring<C>, args: Buffer<NumberedRationalFunction<C>>) : NumberedRationalFunction<C> =
ring.numberedRationalFunctionSpace {
numerator.substitute(ring, args) / denominator.substitute(ring, args)
}
internal const val fullSubstitutionExceptionMessage: String = "Fully substituting buffer should cover all variables of the polynomial."
/**
* Substitutes provided Double arguments [args] into [this] Double polynomial.
*/
public fun NumberedPolynomial<Double>.substituteFully(args: Buffer<Double>): Double = Double.algebra {
val lastSubstitutionVariable = args.size - 1
require(coefficients.keys.all { it.lastIndex <= lastSubstitutionVariable }) { fullSubstitutionExceptionMessage }
coefficients.entries.fold(.0) { acc, (degs, c) ->
acc + degs.foldIndexed(c) { variable, product, deg ->
if (deg == 0u) product else product * args[variable].pow(deg.toInt())
}
}
}
/**
* Substitutes provided arguments [args] into [this] polynomial.
*/
public fun <C> NumberedPolynomial<C>.substituteFully(ring: Ring<C>, args: Buffer<C>): C = ring {
val lastSubstitutionVariable = args.size - 1
require(coefficients.keys.all { it.lastIndex <= lastSubstitutionVariable }) { fullSubstitutionExceptionMessage }
coefficients.entries.fold(zero) { acc, (degs, c) ->
acc + degs.foldIndexed(c) { variable, product, deg ->
if (deg == 0u) product else product * power(args[variable], deg)
}
}
}
/**
* Substitutes provided Double arguments [args] into [this] Double rational function.
*/
public fun NumberedRationalFunction<Double>.substituteFully(args: Buffer<Double>): Double =
numerator.substituteFully(args) / denominator.substituteFully(args)
/**
* Substitutes provided arguments [args] into [this] rational function.
*/
public fun <C> NumberedRationalFunction<C>.substituteFully(ring: Field<C>, args: Buffer<C>): C = ring {
numerator.substituteFully(ring, args) / denominator.substituteFully(ring, args)
}
/**
* Represent [this] polynomial as a regular context-less function.
*/
public fun <C, A : Ring<C>> NumberedPolynomial<C>.asFunctionOver(ring: A): (Buffer<C>) -> C = { substituteFully(ring, it) }
/**
* Represent [this] polynomial as a regular context-less function.
*/
public fun <C, A : Ring<C>> NumberedPolynomial<C>.asFunctionOfConstantOver(ring: A): (Buffer<C>) -> C = { substituteFully(ring, it) }
/**
* Represent [this] polynomial as a regular context-less function.
*/
public fun <C, A : Ring<C>> NumberedPolynomial<C>.asFunctionOfPolynomialOver(ring: A): (Buffer<NumberedPolynomial<C>>) -> NumberedPolynomial<C> = { substitute(ring, it) }
/**
* Represent [this] polynomial as a regular context-less function.
*/
public fun <C, A : Ring<C>> NumberedPolynomial<C>.asFunctionOfRationalFunctionOver(ring: A): (Buffer<NumberedRationalFunction<C>>) -> NumberedRationalFunction<C> = { substitute(ring, it) }
/**
* Represent [this] rational function as a regular context-less function.
*/
public fun <C, A : Field<C>> NumberedRationalFunction<C>.asFunctionOver(ring: A): (Buffer<C>) -> C = { substituteFully(ring, it) }
/**
* Represent [this] rational function as a regular context-less function.
*/
public fun <C, A : Field<C>> NumberedRationalFunction<C>.asFunctionOfConstantOver(ring: A): (Buffer<C>) -> C = { substituteFully(ring, it) }
/**
* Represent [this] rational function as a regular context-less function.
*/
public fun <C, A : Ring<C>> NumberedRationalFunction<C>.asFunctionOfPolynomialOver(ring: A): (Buffer<NumberedPolynomial<C>>) -> NumberedRationalFunction<C> = { substitute(ring, it) }
/**
* Represent [this] rational function as a regular context-less function.
*/
public fun <C, A : Ring<C>> NumberedRationalFunction<C>.asFunctionOfRationalFunctionOver(ring: A): (Buffer<NumberedRationalFunction<C>>) -> NumberedRationalFunction<C> = { substitute(ring, it) }
/**
* Returns algebraic derivative of received polynomial with respect to provided variable.
*/
@UnstableKMathAPI
public fun <C, A : Ring<C>> NumberedPolynomial<C>.derivativeWithRespectTo(
ring: A,
variable: Int,
): NumberedPolynomial<C> = ring {
NumberedPolynomial<C>(
buildMap(coefficients.count { it.key.getOrElse(variable) { 0u } >= 1u }) {
coefficients
.forEach { (degs, c) ->
if (degs.lastIndex < variable) return@forEach
put(
degs.mapIndexed { index, deg ->
when {
index != variable -> deg
deg > 0u -> deg - 1u
else -> return@forEach
}
}.cleanUp(),
multiplyByDoubling(c, degs[variable])
)
}
}
)
}
/**
* Returns algebraic derivative of received polynomial with respect to provided variable of specified order.
*/
@UnstableKMathAPI
public fun <C, A : Ring<C>> NumberedPolynomial<C>.nthDerivativeWithRespectTo(
ring: A,
variable: Int,
order: UInt
): NumberedPolynomial<C> = ring {
if (order == 0u) return this@nthDerivativeWithRespectTo
NumberedPolynomial<C>(
buildMap(coefficients.count { it.key.getOrElse(variable) { 0u } >= order }) {
coefficients
.forEach { (degs, c) ->
if (degs.lastIndex < variable) return@forEach
put(
degs.mapIndexed { index, deg ->
when {
index != variable -> deg
deg >= order -> deg - order
else -> return@forEach
}
}.cleanUp(),
degs[variable].let { deg ->
(deg downTo deg - order + 1u)
.fold(c) { acc, ord -> multiplyByDoubling(acc, ord) }
}
)
}
}
)
}
/**
* Returns algebraic derivative of received polynomial with respect to provided variables of specified orders.
*/
@UnstableKMathAPI
public fun <C, A : Ring<C>> NumberedPolynomial<C>.nthDerivativeWithRespectTo(
ring: A,
variablesAndOrders: Map<Int, UInt>,
): NumberedPolynomial<C> = ring {
val filteredVariablesAndOrders = variablesAndOrders.filterValues { it != 0u }
if (filteredVariablesAndOrders.isEmpty()) return this@nthDerivativeWithRespectTo
val maxRespectedVariable = filteredVariablesAndOrders.keys.maxOrNull()!!
NumberedPolynomial<C>(
buildMap(coefficients.size) {
coefficients
.forEach { (degs, c) ->
if (degs.lastIndex < maxRespectedVariable) return@forEach
put(
degs.mapIndexed { index, deg ->
if (index !in filteredVariablesAndOrders) return@mapIndexed deg
val order = filteredVariablesAndOrders[index]!!
if (deg >= order) deg - order else return@forEach
}.cleanUp(),
filteredVariablesAndOrders.entries.fold(c) { acc1, (index, order) ->
degs[index].let { deg ->
(deg downTo deg - order + 1u)
.fold(acc1) { acc2, ord -> multiplyByDoubling(acc2, ord) }
}
}
)
}
}
)
}
/**
* Returns algebraic antiderivative of received polynomial with respect to provided variable.
*/
@UnstableKMathAPI
public fun <C, A : Field<C>> NumberedPolynomial<C>.antiderivativeWithRespectTo(
ring: A,
variable: Int,
): NumberedPolynomial<C> = ring {
NumberedPolynomial<C>(
buildMap(coefficients.size) {
coefficients
.forEach { (degs, c) ->
put(
List(max(variable + 1, degs.size)) { degs.getOrElse(it) { 0u } + if (it != variable) 0u else 1u },
c / multiplyByDoubling(one, degs.getOrElse(variable) { 0u } + 1u)
)
}
}
)
}
/**
* Returns algebraic antiderivative of received polynomial with respect to provided variable of specified order.
*/
@UnstableKMathAPI
public fun <C, A : Field<C>> NumberedPolynomial<C>.nthAntiderivativeWithRespectTo(
ring: A,
variable: Int,
order: UInt
): NumberedPolynomial<C> = ring {
if (order == 0u) return this@nthAntiderivativeWithRespectTo
NumberedPolynomial<C>(
buildMap(coefficients.size) {
coefficients
.forEach { (degs, c) ->
put(
List(max(variable + 1, degs.size)) { degs.getOrElse(it) { 0u } + if (it != variable) 0u else order },
degs.getOrElse(variable) { 0u }.let { deg ->
(deg + 1u .. deg + order)
.fold(c) { acc, ord -> acc / multiplyByDoubling(one, ord) }
}
)
}
}
)
}
/**
* Returns algebraic derivative of received polynomial with respect to provided variables of specified orders.
*/
@UnstableKMathAPI
public fun <C, A : Field<C>> NumberedPolynomial<C>.nthAntiderivativeWithRespectTo(
ring: A,
variablesAndOrders: Map<Int, UInt>,
): NumberedPolynomial<C> = ring {
val filteredVariablesAndOrders = variablesAndOrders.filterValues { it != 0u }
if (filteredVariablesAndOrders.isEmpty()) return this@nthAntiderivativeWithRespectTo
val maxRespectedVariable = filteredVariablesAndOrders.keys.maxOrNull()!!
NumberedPolynomial<C>(
buildMap(coefficients.size) {
coefficients
.forEach { (degs, c) ->
put(
List(max(maxRespectedVariable + 1, degs.size)) { degs.getOrElse(it) { 0u } + filteredVariablesAndOrders.getOrElse(it) { 0u } },
filteredVariablesAndOrders.entries.fold(c) { acc1, (variable, order) ->
degs.getOrElse(variable) { 0u }.let { deg ->
(deg + 1u .. deg + order)
.fold(acc1) { acc, ord -> acc / multiplyByDoubling(one, ord) }
}
}
)
}
}
)
}