-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabeledConstructors.kt
779 lines (695 loc) · 31.7 KB
/
labeledConstructors.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/*
* 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("FunctionName", "NOTHING_TO_INLINE", "KotlinRedundantDiagnosticSuppress")
package space.kscience.kmath.functions
import space.kscience.kmath.UnstableKMathAPI
import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.operations.Ring
import space.kscience.kmath.operations.invoke
/**
* Returns the same degrees' description of the monomial, but without zero degrees.
*/
internal fun Map<Symbol, UInt>.cleanUp() = filterValues { it > 0U }
/**
* Constructs [LabeledPolynomial] with provided coefficients map [coefs]. The map is used as is.
*/
@PublishedApi
internal inline fun <C> LabeledPolynomialAsIs(coefs: Map<Map<Symbol, UInt>, C>) : LabeledPolynomial<C> = LabeledPolynomial<C>(coefs)
/**
* Constructs [LabeledPolynomial] with provided collection of [pairs] of pairs "term's signature — term's coefficient".
* The collections will be transformed to map with [toMap] and then will be used as is.
*/
@PublishedApi
internal inline fun <C> LabeledPolynomialAsIs(pairs: Collection<Pair<Map<Symbol, UInt>, C>>) : LabeledPolynomial<C> = LabeledPolynomial<C>(pairs.toMap())
/**
* Constructs [LabeledPolynomial] with provided array of [pairs] of pairs "term's signature — term's coefficient".
* The array will be transformed to map with [toMap] and then will be used as is.
*/
@PublishedApi
internal inline fun <C> LabeledPolynomialAsIs(vararg pairs: Pair<Map<Symbol, UInt>, C>) : LabeledPolynomial<C> = LabeledPolynomial<C>(pairs.toMap())
/**
* Constructs [LabeledPolynomial] with provided coefficients map [coefs]. The map is used as is.
*
* **Be sure you read description of [LabeledPolynomial.coefficients]. Otherwise, you may make a mistake that will
* cause wrong computation result or even runtime error.**
*/
@DelicatePolynomialAPI
public inline fun <C> LabeledPolynomialWithoutCheck(coefs: Map<Map<Symbol, UInt>, C>) : LabeledPolynomial<C> = LabeledPolynomial<C>(coefs)
/**
* Constructs [LabeledPolynomial] with provided collection of [pairs] of pairs "term's signature — term's coefficient".
* The collections will be transformed to map with [toMap] and then will be used as is.
*
* **Be sure you read description of [LabeledPolynomial.coefficients]. Otherwise, you may make a mistake that will
* cause wrong computation result or even runtime error.**
*/
@DelicatePolynomialAPI
public inline fun <C> LabeledPolynomialWithoutCheck(pairs: Collection<Pair<Map<Symbol, UInt>, C>>) : LabeledPolynomial<C> = LabeledPolynomial<C>(pairs.toMap())
/**
* Constructs [LabeledPolynomial] with provided array of [pairs] of pairs "term's signature — term's coefficient".
* The array will be transformed to map with [toMap] and then will be used as is.
*
* **Be sure you read description of [LabeledPolynomial.coefficients]. Otherwise, you may make a mistake that will
* cause wrong computation result or even runtime error.**
*/
@DelicatePolynomialAPI
public inline fun <C> LabeledPolynomialWithoutCheck(vararg pairs: Pair<Map<Symbol, UInt>, C>) : LabeledPolynomial<C> = LabeledPolynomial<C>(pairs.toMap())
/**
* Constructs [LabeledPolynomial] with provided coefficients map [coefs].
*
* [coefs] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [coefs] keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*
* @see LabeledPolynomialWithoutCheck
*/
public fun <C> LabeledPolynomial(coefs: Map<Map<Symbol, UInt>, C>, add: (C, C) -> C) : LabeledPolynomial<C> =
LabeledPolynomialAsIs(
coefs.mapKeys({ key, _ -> key.cleanUp() }, add)
)
/**
* Constructs [LabeledPolynomial] with provided collection of [pairs] of pairs "term's signature — term's coefficient".
*
* [pairs] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [pairs] keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*
* @see LabeledPolynomialWithoutCheck
*/
public fun <C> LabeledPolynomial(pairs: Collection<Pair<Map<Symbol, UInt>, C>>, add: (C, C) -> C) : LabeledPolynomial<C> =
LabeledPolynomialAsIs(
pairs.associateBy({ it.first.cleanUp() }, { it.second }, add)
)
/**
* Constructs [LabeledPolynomial] with provided array [pairs] of pairs "term's signature — term's coefficient".
*
* [pairs] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [pairs] keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*
* @see LabeledPolynomialWithoutCheck
*/
public fun <C> LabeledPolynomial(vararg pairs: Pair<Map<Symbol, UInt>, C>, add: (C, C) -> C) : LabeledPolynomial<C> =
LabeledPolynomialAsIs(
pairs.asIterable().associateBy({ it.first.cleanUp() }, { it.second }, add)
)
// Waiting for context receivers :( FIXME: Replace with context receivers when they will be available
/**
* Constructs [LabeledPolynomial] with provided coefficients map [coefs].
*
* [coefs] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [coefs] keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*
* @see LabeledPolynomialWithoutCheck
*/
public inline fun <C, A: Ring<C>> A.LabeledPolynomial(coefs: Map<Map<Symbol, UInt>, C>) : LabeledPolynomial<C> = LabeledPolynomial(coefs, ::add)
/**
* Constructs [LabeledPolynomial] with provided coefficients map [coefs].
*
* [coefs] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [coefs] keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*
* @see LabeledPolynomialWithoutCheck
*/
public inline fun <C, A: Ring<C>> LabeledPolynomialSpace<C, A>.LabeledPolynomial(coefs: Map<Map<Symbol, UInt>, C>) : LabeledPolynomial<C> = LabeledPolynomial(coefs) { left: C, right: C -> left + right }
/**
* Constructs [LabeledPolynomial] with provided coefficients map [coefs].
*
* [coefs] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [coefs] keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*
* @see LabeledPolynomialWithoutCheck
*/
public inline fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledPolynomial(coefs: Map<Map<Symbol, UInt>, C>) : LabeledPolynomial<C> = LabeledPolynomial(coefs) { left: C, right: C -> left + right }
/**
* Constructs [LabeledPolynomial] with provided collection of [pairs] of pairs "term's signature — term's coefficient".
*
* [pairs] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [pairs] keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*
* @see LabeledPolynomialWithoutCheck
*/
public inline fun <C, A: Ring<C>> A.LabeledPolynomial(pairs: Collection<Pair<Map<Symbol, UInt>, C>>) : LabeledPolynomial<C> = LabeledPolynomial(pairs, ::add)
/**
* Constructs [LabeledPolynomial] with provided collection of [pairs] of pairs "term's signature — term's coefficient".
*
* [pairs] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [pairs] keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*
* @see LabeledPolynomialWithoutCheck
*/
public inline fun <C, A: Ring<C>> LabeledPolynomialSpace<C, A>.LabeledPolynomial(pairs: Collection<Pair<Map<Symbol, UInt>, C>>) : LabeledPolynomial<C> = LabeledPolynomial(pairs) { left: C, right: C -> left + right }
/**
* Constructs [LabeledPolynomial] with provided collection of [pairs] of pairs "term's signature — term's coefficient".
*
* [pairs] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [pairs] keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*
* @see LabeledPolynomialWithoutCheck
*/
public inline fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledPolynomial(pairs: Collection<Pair<Map<Symbol, UInt>, C>>) : LabeledPolynomial<C> = LabeledPolynomial(pairs) { left: C, right: C -> left + right }
/**
* Constructs [LabeledPolynomial] with provided array [pairs] of pairs "term's signature — term's coefficient".
*
* [pairs] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [pairs] keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*
* @see LabeledPolynomialWithoutCheck
*/
public inline fun <C, A: Ring<C>> A.LabeledPolynomial(vararg pairs: Pair<Map<Symbol, UInt>, C>) : LabeledPolynomial<C> = LabeledPolynomial(*pairs) { left: C, right: C -> left + right }
/**
* Constructs [LabeledPolynomial] with provided array [pairs] of pairs "term's signature — term's coefficient".
*
* [pairs] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [pairs] keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*
* @see LabeledPolynomialWithoutCheck
*/
public inline fun <C, A: Ring<C>> LabeledPolynomialSpace<C, A>.LabeledPolynomial(vararg pairs: Pair<Map<Symbol, UInt>, C>) : LabeledPolynomial<C> = LabeledPolynomial(*pairs) { left: C, right: C -> left + right }
/**
* Constructs [LabeledPolynomial] with provided array [pairs] of pairs "term's signature — term's coefficient".
*
* [pairs] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [pairs] keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*
* @see LabeledPolynomialWithoutCheck
*/
public inline fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledPolynomial(vararg pairs: Pair<Map<Symbol, UInt>, C>) : LabeledPolynomial<C> = LabeledPolynomial(*pairs) { left: C, right: C -> left + right }
/**
* Converts [this] constant to [LabeledPolynomial].
*/
public inline fun <C> C.asLabeledPolynomial() : LabeledPolynomial<C> = LabeledPolynomialAsIs(mapOf(emptyMap<Symbol, UInt>() to this))
///**
//// * Converts [this] variable to [LabeledPolynomial].
//// */
//context(A)
//public inline fun <C, A: Ring<C>> Symbol.asLabeledPolynomial() : LabeledPolynomial<C> = LabeledPolynomial<C>(mapOf(mapOf(this to 1u) to one))
///**
// * Converts [this] variable to [LabeledPolynomial].
// */
//context(LabeledPolynomialSpace<C, A>)
//public inline fun <C, A: Ring<C>> Symbol.asLabeledPolynomial() : LabeledPolynomial<C> = LabeledPolynomial<C>(mapOf(mapOf(this to 1u) to constantOne))
///**
// * Converts [this] variable to [LabeledPolynomial].
// */
//context(LabeledRationalFunctionSpace<C, A>)
//public inline fun <C, A: Ring<C>> Symbol.asLabeledPolynomial() : LabeledPolynomial<C> = LabeledPolynomial<C>(mapOf(mapOf(this to 1u) to constantOne))
/**
* Marks DSL that allows to more simply create [LabeledPolynomial]s with good performance.
*
* For example, polynomial \(5 a^2 c^3 - 6 b\) can be described as
* ```
* Int.algebra {
* val labeledPolynomial : LabeledPolynomial<Int> = LabeledPolynomialDSL1 {
* 5 { a inPowerOf 2u; c inPowerOf 3u } // 5 a^2 c^3 +
* (-6) { b inPowerOf 1u } // (-6) b^1
* }
* }
* ```
* @usesMathJax
*/
@DslMarker
@UnstableKMathAPI
internal annotation class LabeledPolynomialConstructorDSL1
/**
* Builder of [LabeledPolynomial] signature. It should be used as an implicit context for lambdas that describe term signature.
*/
@UnstableKMathAPI
@LabeledPolynomialConstructorDSL1
public class DSL1LabeledPolynomialTermSignatureBuilder {
/**
* Signature storage. Any declaration of any variable's power updates the storage by increasing corresponding value.
* Afterward the storage will be used as a resulting signature.
*/
private val signature: MutableMap<Symbol, UInt> = LinkedHashMap()
/**
* Builds the resulting signature.
*
* In fact, it just returns [signature] as regular signature of type `List<UInt>`.
*/
@PublishedApi
internal fun build(): Map<Symbol, UInt> = signature
/**
* Declares power of [this] variable of degree [deg].
*
* Declaring another power of the same variable will increase its degree by received degree.
*/
public infix fun Symbol.inPowerOf(deg: UInt) {
if (deg == 0u) return
signature.putOrChange(this, deg) { it -> it + deg }
}
/**
* Declares power of [this] variable of degree [deg].
*
* Declaring another power of the same variable will increase its degree by received degree.
*/
public inline infix fun Symbol.pow(deg: UInt): Unit = this inPowerOf deg
/**
* Declares power of [this] variable of degree [deg].
*
* Declaring another power of the same variable will increase its degree by received degree.
*/
public inline infix fun Symbol.`in`(deg: UInt): Unit = this inPowerOf deg
/**
* Declares power of [this] variable of degree [deg].
*
* Declaring another power of the same variable will increase its degree by received degree.
*/
public inline infix fun Symbol.of(deg: UInt): Unit = this inPowerOf deg
}
/**
* Builder of [LabeledPolynomial]. It should be used as an implicit context for lambdas that describe [LabeledPolynomial].
*/
@UnstableKMathAPI
@LabeledPolynomialConstructorDSL1
public class DSL1LabeledPolynomialBuilder<C>(
/**
* Summation operation that will be used to sum coefficients of monomials of same signatures.
*/
private val add: (C, C) -> C,
/**
* Initial capacity of coefficients map.
*/
initialCapacity: Int? = null
) {
/**
* Coefficients storage. Any declaration of any monomial updates the storage.
* Afterward the storage will be used as a resulting coefficients map.
*/
private val coefficients: MutableMap<Map<Symbol, UInt>, C> = if (initialCapacity != null) LinkedHashMap(initialCapacity) else LinkedHashMap()
/**
* Builds the resulting coefficients map.
*
* In fact, it just returns [coefficients] as regular coefficients map of type `Map<Map<Symbol, UInt>, C>`.
*/
@PublishedApi
internal fun build(): LabeledPolynomial<C> = LabeledPolynomial<C>(coefficients)
/**
* Declares monomial with [this] coefficient and provided [signature].
*
* Declaring another monomial with the same signature will add [this] coefficient to existing one. If the sum of such
* coefficients is zero at any moment the monomial won't be removed but will be left as it is.
*/
public infix fun C.with(signature: Map<Symbol, UInt>) {
coefficients.putOrChange(signature, this@with, add)
}
/**
* Declares monomial with [this] coefficient and signature constructed by [block].
*
* Declaring another monomial with the same signature will add [this] coefficient to existing one. If the sum of such
* coefficients is zero at any moment the monomial won't be removed but will be left as it is.
*/
public inline infix fun C.with(noinline block: DSL1LabeledPolynomialTermSignatureBuilder.() -> Unit): Unit = this.invoke(block)
/**
* Declares monomial with [this] coefficient and signature constructed by [block].
*
* Declaring another monomial with the same signature will add [this] coefficient to existing one. If the sum of such
* coefficients is zero at any moment the monomial won't be removed but will be left as it is.
*/
public inline operator fun C.invoke(block: DSL1LabeledPolynomialTermSignatureBuilder.() -> Unit): Unit =
this with DSL1LabeledPolynomialTermSignatureBuilder().apply(block).build()
}
// Waiting for context receivers :( FIXME: Replace with context receivers when they will be available
///**
// * Creates [LabeledPolynomial] with lambda [block] in context of [this] ring of constants.
// *
// * For example, polynomial \(5 a^2 c^3 - 6 b\) can be described as
// * ```
// * Int.algebra {
// * val labeledPolynomial : LabeledPolynomial<Int> = LabeledPolynomialDSL1 {
// * 5 { a inPowerOf 2u; c inPowerOf 3u } // 5 a^2 c^3 +
// * (-6) { b inPowerOf 1u } // (-6) b^1
// * }
// * }
// * ```
// * @usesMathJax
// */
// FIXME: For now this fabric does not let next two fabrics work. (See KT-52803.) Possible feature solutions:
// 1. `LowPriorityInOverloadResolution` becomes public. Then it should be applied to this function.
// 2. Union types are implemented. Then all three functions should be rewritten
// as one with single union type as a (context) receiver.
//@UnstableKMathAPI
//public inline fun <C, A: Ring<C>> A.LabeledPolynomialDSL1(initialCapacity: Int? = null, block: LabeledPolynomialBuilder<C>.() -> Unit) : LabeledPolynomial<C> = LabeledPolynomialBuilder(::add, initialCapacity).apply(block).build()
/**
* Creates [LabeledPolynomial] with lambda [block] in context of [this] ring of [LabeledPolynomial]s.
*
* For example, polynomial \(5 a^2 c^3 - 6 b\) can be described as
* ```
* Int.algebra {
* val labeledPolynomial : LabeledPolynomial<Int> = LabeledPolynomialDSL1 {
* 5 { a inPowerOf 2u; c inPowerOf 3u } // 5 a^2 c^3 +
* (-6) { b inPowerOf 1u } // (-6) b^1
* }
* }
* ```
* @usesMathJax
*/
@UnstableKMathAPI
public inline fun <C, A: Ring<C>> LabeledPolynomialSpace<C, A>.LabeledPolynomialDSL1(initialCapacity: Int? = null, block: DSL1LabeledPolynomialBuilder<C>.() -> Unit) : LabeledPolynomial<C> = DSL1LabeledPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build()
/**
* Creates [LabeledPolynomial] with lambda [block] in context of [this] field of [LabeledRationalFunction]s.
*
* For example, polynomial \(5 a^2 c^3 - 6 b\) can be described as
* ```
* Int.algebra {
* val labeledPolynomial : LabeledPolynomial<Int> = LabeledPolynomialDSL1 {
* 5 { a inPowerOf 2u; c inPowerOf 3u } // 5 a^2 c^3 +
* (-6) { b inPowerOf 1u } // (-6) b^1
* }
* }
* ```
* @usesMathJax
*/
@UnstableKMathAPI
public inline fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledPolynomialDSL1(initialCapacity: Int? = null, block: DSL1LabeledPolynomialBuilder<C>.() -> Unit) : LabeledPolynomial<C> = DSL1LabeledPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build()
/**
* Marks DSL that allows to more simply create [LabeledPolynomial]s with good performance.
*
* For example, polynomial \(5 a^2 c^3 - 6 b\) can be described as
* ```
* Int.algebra {
* val numberedPolynomial : NumberedPolynomial<Int> = NumberedPolynomial {
* 5 { a inPowerOf 2u; c inPowerOf 3u } // 5 a^2 c^3 +
* (-6) { b inPowerOf 1u } // (-6) b^1
* }
* }
* ```
* @usesMathJax
*/
@DslMarker
@UnstableKMathAPI
internal annotation class LabeledPolynomialBuilderDSL2
/**
* Builder of [LabeledPolynomial]. It should be used as an implicit context for lambdas that describe [LabeledPolynomial].
*/
@UnstableKMathAPI
@LabeledPolynomialBuilderDSL2
public class DSL2LabeledPolynomialBuilder<C>(
private val ring: Ring<C>,
/**
* Initial capacity of coefficients map.
*/
initialCapacity: Int? = null
) {
/**
* Coefficients storage. Any declaration of any monomial updates the storage.
* Afterward the storage will be used as a resulting coefficients map.
*/
private val coefficients: MutableMap<Map<Symbol, UInt>, C> = if (initialCapacity != null) LinkedHashMap(initialCapacity) else LinkedHashMap()
/**
* Builds the resulting coefficients map.
*
* In fact, it just returns [coefficients] as regular coefficients map of type `Map<Map<Symbol, UInt>, C>`.
*/
@PublishedApi
internal fun build(): LabeledPolynomial<C> = LabeledPolynomial<C>(coefficients)
public inner class Term internal constructor(
internal val signature: Map<Symbol, UInt> = HashMap(),
internal val coefficient: C
)
private inline fun submit(signature: Map<Symbol, UInt>, onPut: Ring<C>.() -> C, onChange: Ring<C>.(C) -> C) {
coefficients.putOrChange<_, C>(signature, { ring.onPut() }, { ring.onChange(it) })
}
private inline fun submit(signature: Map<Symbol, UInt>, lazyCoefficient: Ring<C>.() -> C) {
submit(signature, lazyCoefficient) { it + lazyCoefficient() }
}
private fun submit(signature: Map<Symbol, UInt>, coefficient: C) {
submit(signature) { coefficient }
}
// TODO: `@submit` will be resolved differently. Change it to `@C`.
private fun C.submitSelf() = submit(emptyMap()) { this@submitSelf }
private fun Symbol.submit() = submit(mapOf(this to 1u)) { one }
private fun Term.submit(): Submit {
submit(signature, coefficient)
return Submit
}
public object Submit
public operator fun C.unaryPlus(): Submit {
submitSelf()
return Submit
}
public operator fun C.unaryMinus(): Submit {
submit(emptyMap(), { -this@unaryMinus }, { it - this@unaryMinus })
return Submit
}
public operator fun C.plus(other: C): Submit {
submit(emptyMap()) { this@plus + other }
return Submit
}
public operator fun C.minus(other: C): Submit {
submit(emptyMap()) { this@minus - other }
return Submit
}
public operator fun C.times(other: C): C = ring { this@times * other }
public operator fun C.plus(other: Symbol): Submit {
submit(emptyMap(), this)
submit(mapOf(other to 1u), ring.one)
return Submit
}
public operator fun C.minus(other: Symbol): Submit {
submit(emptyMap(), this)
submit(mapOf(other to 1u), { -one }, { it - one })
return Submit
}
public operator fun C.times(other: Symbol): Term = Term(mapOf(other to 1u), this)
public operator fun C.plus(other: Term): Submit {
submit(emptyMap(), this)
other.submit()
return Submit
}
public operator fun C.minus(other: Term): Submit {
submit(emptyMap(), this)
submit(other.signature, { -other.coefficient }, { it - other.coefficient })
return Submit
}
public operator fun C.times(other: Term): Term = Term(other.signature, ring { this@times * other.coefficient })
public operator fun Symbol.plus(other: C): Submit {
this.submit()
other.submitSelf()
return Submit
}
public operator fun Symbol.minus(other: C): Submit {
this.submit()
submit(emptyMap(), { -other }, { it - other })
return Submit
}
public operator fun Symbol.times(other: C): Term = Term(mapOf(this to 1u), other)
public operator fun Symbol.unaryPlus(): Submit {
this.submit()
return Submit
}
public operator fun Symbol.unaryMinus(): Submit {
submit(mapOf(this to 1u), { -one }, { it - one })
return Submit
}
public operator fun Symbol.plus(other: Symbol): Submit {
this.submit()
other.submit()
return Submit
}
public operator fun Symbol.minus(other: Symbol): Submit {
this.submit()
submit(mapOf(other to 1u), { -one }, { it - one })
return Submit
}
public operator fun Symbol.times(other: Symbol): Term =
if (this == other) Term(mapOf(this to 2u), ring.one)
else Term(mapOf(this to 1u, other to 1u), ring.one)
public operator fun Symbol.plus(other: Term): Submit {
this.submit()
other.submit()
return Submit
}
public operator fun Symbol.minus(other: Term): Submit {
this.submit()
submit(other.signature, { -other.coefficient }, { it - other.coefficient })
return Submit
}
public operator fun Symbol.times(other: Term): Term =
Term(
other.signature.withPutOrChanged(this, 1u) { it -> it + 1u },
other.coefficient
)
public operator fun Term.plus(other: C): Submit {
this.submit()
other.submitSelf()
return Submit
}
public operator fun Term.minus(other: C): Submit {
this.submit()
submit(emptyMap(), { -other }, { it - other })
return Submit
}
public operator fun Term.times(other: C): Term =
Term(
signature,
ring { coefficient * other }
)
public operator fun Term.plus(other: Symbol): Submit {
this.submit()
other.submit()
return Submit
}
public operator fun Term.minus(other: Symbol): Submit {
this.submit()
submit(mapOf(other to 1u), { -one }, { it - one })
return Submit
}
public operator fun Term.times(other: Symbol): Term =
Term(
signature.withPutOrChanged(other, 1u) { it -> it + 1u },
coefficient
)
public operator fun Term.unaryPlus(): Submit {
this.submit()
return Submit
}
public operator fun Term.unaryMinus(): Submit {
submit(signature, { -coefficient }, { it - coefficient })
return Submit
}
public operator fun Term.plus(other: Term): Submit {
this.submit()
other.submit()
return Submit
}
public operator fun Term.minus(other: Term): Submit {
this.submit()
submit(other.signature, { -other.coefficient }, { it - other.coefficient })
return Submit
}
public operator fun Term.times(other: Term): Term =
Term(
mergeBy(signature, other.signature) { deg1, deg2 -> deg1 + deg2 },
ring { coefficient * other.coefficient }
)
}
//@UnstableKMathAPI
//public fun <C> Ring<C>.LabeledPolynomialDSL2(initialCapacity: Int? = null, block: DSL2LabeledPolynomialBuilder<C>.() -> Unit): LabeledPolynomial<C> = DSL2LabeledPolynomialBuilder(this, initialCapacity).apply(block).build()
@UnstableKMathAPI
public fun <C, A: Ring<C>> LabeledPolynomialSpace<C, A>.LabeledPolynomialDSL2(initialCapacity: Int? = null, block: DSL2LabeledPolynomialBuilder<C>.() -> Unit): LabeledPolynomial<C> = DSL2LabeledPolynomialBuilder(ring, initialCapacity).apply(block).build()
@UnstableKMathAPI
public fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledPolynomialDSL2(initialCapacity: Int? = null, block: DSL2LabeledPolynomialBuilder<C>.() -> Unit): LabeledPolynomial<C> = DSL2LabeledPolynomialBuilder(ring, initialCapacity).apply(block).build()
// Waiting for context receivers :( FIXME: Replace with context receivers when they will be available
/**
* Constructs [LabeledRationalFunction] with provided coefficients maps [numeratorCoefficients] and [denominatorCoefficients].
*
* The maps will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. the maps' keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*/
public fun <C, A: Ring<C>> A.LabeledRationalFunction(numeratorCoefficients: Map<Map<Symbol, UInt>, C>, denominatorCoefficients: Map<Map<Symbol, UInt>, C>): LabeledRationalFunction<C> =
LabeledRationalFunction<C>(
LabeledPolynomial(numeratorCoefficients),
LabeledPolynomial(denominatorCoefficients)
)
/**
* Constructs [LabeledRationalFunction] with provided coefficients maps [numeratorCoefficients] and [denominatorCoefficients].
*
* The maps will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. the maps' keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*/
public fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledRationalFunction(numeratorCoefficients: Map<Map<Symbol, UInt>, C>, denominatorCoefficients: Map<Map<Symbol, UInt>, C>): LabeledRationalFunction<C> =
LabeledRationalFunction<C>(
LabeledPolynomial(numeratorCoefficients),
LabeledPolynomial(denominatorCoefficients)
)
/**
* Constructs [LabeledRationalFunction] with provided [numerator] and unit denominator.
*/
public fun <C, A: Ring<C>> A.LabeledRationalFunction(numerator: LabeledPolynomial<C>): LabeledRationalFunction<C> =
LabeledRationalFunction<C>(numerator, LabeledPolynomial(mapOf(emptyMap<Symbol, UInt>() to one)))
/**
* Constructs [LabeledRationalFunction] with provided [numerator] and unit denominator.
*/
public fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledRationalFunction(numerator: LabeledPolynomial<C>): LabeledRationalFunction<C> =
LabeledRationalFunction<C>(numerator, polynomialOne)
/**
* Constructs [LabeledRationalFunction] with provided coefficients map [numeratorCoefficients] for numerator and unit
* denominator.
*
* [numeratorCoefficients] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [numeratorCoefficients]'s keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*/
public fun <C, A: Ring<C>> LabeledRationalFunctionSpace<C, A>.LabeledRationalFunction(numeratorCoefficients: Map<Map<Symbol, UInt>, C>): LabeledRationalFunction<C> =
LabeledRationalFunction<C>(
LabeledPolynomial(numeratorCoefficients),
polynomialOne
)
/**
* Constructs [LabeledRationalFunction] with provided coefficients map [numeratorCoefficients] for numerator and unit
* denominator.
*
* [numeratorCoefficients] will be "cleaned up":
* 1. Zeros at the ends of terms' signatures (e.g. [numeratorCoefficients]'s keys) will be removed. (See [cleanUp].)
* 1. Terms that happen to have the same signature will be summed up.
* 1. New map will be formed of resulting terms.
*/
public fun <C, A: Ring<C>> A.LabeledRationalFunction(numeratorCoefficients: Map<Map<Symbol, UInt>, C>): LabeledRationalFunction<C> =
LabeledRationalFunction<C>(
LabeledPolynomial(numeratorCoefficients),
LabeledPolynomialAsIs(mapOf(emptyMap<Symbol, UInt>() to one))
)
///**
// * Converts [this] constant to [LabeledRationalFunction].
// */
//context(A)
//public fun <C, A: Ring<C>> C.asLabeledRationalFunction() : LabeledRationalFunction<C> =
// LabeledRationalFunction(
// LabeledPolynomialAsIs(mapOf(emptyMap<Symbol, UInt>() to this)),
// LabeledPolynomialAsIs(mapOf(emptyMap<Symbol, UInt>() to one))
// )
///**
// * Converts [this] constant to [LabeledRationalFunction].
// */
//context(LabeledRationalFunctionSpace<C, A>)
//public fun <C, A: Ring<C>> C.asLabeledRationalFunction() : LabeledRationalFunction<C> =
// LabeledRationalFunction(
// LabeledPolynomialAsIs(mapOf(emptyMap<Symbol, UInt>() to this)),
// LabeledPolynomialAsIs(mapOf(emptyMap<Symbol, UInt>() to constantOne))
// )
///**
// * Converts [this] variable to [LabeledRationalFunction].
// */
//context(A)
//public fun <C, A: Ring<C>> Symbol.asLabeledRationalFunction() : LabeledRationalFunction<C> =
// LabeledRationalFunction(
// LabeledPolynomialAsIs(mapOf(mapOf(this to 1u) to one)),
// LabeledPolynomialAsIs(mapOf(emptyMap<Symbol, UInt>() to one))
// )
///**
// * Converts [this] variable to [LabeledRationalFunction].
// */
//context(LabeledRationalFunctionSpace<C, A>)
//public fun <C, A: Ring<C>> Symbol.asLabeledRationalFunction() : LabeledRationalFunction<C> =
// LabeledRationalFunction(
// LabeledPolynomialAsIs(mapOf(mapOf(this to 1u) to constantOne)),
// LabeledPolynomialAsIs(mapOf(emptyMap<Symbol, UInt>() to constantOne))
// )