-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRationalFunction.kt
1689 lines (1597 loc) · 63.9 KB
/
RationalFunction.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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.operations.Ring
import space.kscience.kmath.operations.invoke
import kotlin.js.JsName
import kotlin.jvm.JvmName
/**
* Abstraction of rational function.
*/
public interface RationalFunction<C, P> {
public val numerator: P
public val denominator: P
public operator fun component1(): P = numerator
public operator fun component2(): P = denominator
}
/**
* Abstraction of field of rational functions of type [R] with respect to polynomials of type [P] and constants of type
* [C].
*
* @param C the type of constants. Polynomials have them as coefficients in their terms.
* @param P the type of polynomials. Rational functions have them as numerators and denominators.
* @param R the type of rational functions.
*/
@Suppress("INAPPLICABLE_JVM_NAME", "PARAMETER_NAME_CHANGED_ON_OVERRIDE") // FIXME: Waiting for KT-31420
public interface RationalFunctionSpace<C, P, R: RationalFunction<C, P>> : Ring<R> {
/**
* Returns sum of the constant and the integer represented as a constant (member of underlying ring).
*
* The operation is equivalent to adding [other] copies of unit of underlying ring to [this].
*/
@JvmName("plusConstantInt")
public operator fun C.plus(other: Int): C
/**
* Returns difference between the constant and the integer represented as a constant (member of underlying ring).
*
* The operation is equivalent to subtraction [other] copies of unit of underlying ring from [this].
*/
@JvmName("minusConstantInt")
public operator fun C.minus(other: Int): C
/**
* Returns product of the constant and the integer represented as a constant (member of underlying ring).
*
* The operation is equivalent to sum of [other] copies of [this].
*/
@JvmName("timesConstantInt")
public operator fun C.times(other: Int): C
/**
* Returns sum of the integer represented as a constant (member of underlying ring) and the constant.
*
* The operation is equivalent to adding [this] copies of unit of underlying ring to [other].
*/
@JvmName("plusIntConstant")
public operator fun Int.plus(other: C): C
/**
* Returns difference between the integer represented as a constant (member of underlying ring) and the constant.
*
* The operation is equivalent to subtraction [this] copies of unit of underlying ring from [other].
*/
@JvmName("minusIntConstant")
public operator fun Int.minus(other: C): C
/**
* Returns product of the integer represented as a constant (member of underlying ring) and the constant.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
@JvmName("timesIntConstant")
public operator fun Int.times(other: C): C
/**
* Converts the integer [value] to constant.
*/
public fun constantNumber(value: Int): C = constantOne * value
/**
* Converts the integer to constant.
*/
public fun Int.asConstant(): C = constantNumber(this)
/**
* Returns sum of the constant and the integer represented as a polynomial.
*
* The operation is equivalent to adding [other] copies of unit polynomial to [this].
*/
@JvmName("plusPolynomialInt")
public operator fun P.plus(other: Int): P
/**
* Returns difference between the constant and the integer represented as a polynomial.
*
* The operation is equivalent to subtraction [other] copies of unit polynomial from [this].
*/
@JvmName("minusPolynomialInt")
public operator fun P.minus(other: Int): P
/**
* Returns product of the constant and the integer represented as a polynomial.
*
* The operation is equivalent to sum of [other] copies of [this].
*/
@JvmName("timesPolynomialInt")
public operator fun P.times(other: Int): P
/**
* Returns sum of the integer represented as a polynomial and the constant.
*
* The operation is equivalent to adding [this] copies of unit polynomial to [other].
*/
@JvmName("plusIntPolynomial")
public operator fun Int.plus(other: P): P
/**
* Returns difference between the integer represented as a polynomial and the constant.
*
* The operation is equivalent to subtraction [this] copies of unit polynomial from [other].
*/
@JvmName("minusIntPolynomial")
public operator fun Int.minus(other: P): P
/**
* Returns product of the integer represented as a polynomial and the constant.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
@JvmName("timesIntPolynomial")
public operator fun Int.times(other: P): P
/**
* Converts the integer [value] to polynomial.
*/
public fun polynomialNumber(value: Int): P = polynomialOne * value
/**
* Converts the integer to polynomial.
*/
public fun Int.asPolynomial(): P = polynomialNumber(this)
/**
* Returns sum of the rational function and the integer represented as a rational function.
*
* The operation is equivalent to adding [other] copies of unit polynomial to [this].
*/
public operator fun R.plus(other: Int): R = addMultipliedByDoubling(this, one, other)
/**
* Returns difference between the rational function and the integer represented as a rational function.
*
* The operation is equivalent to subtraction [other] copies of unit polynomial from [this].
*/
public operator fun R.minus(other: Int): R = addMultipliedByDoubling(this, one, -other)
/**
* Returns product of the rational function and the integer represented as a rational function.
*
* The operation is equivalent to sum of [other] copies of [this].
*/
public operator fun R.times(other: Int): R = multiplyByDoubling(this, other)
/**
* Returns quotient of the rational function and the integer represented as a rational function.
*
* The operation is equivalent to creating a new rational function by preserving numerator of [this] and
* multiplication denominator of [this] to [other].
*/
public operator fun R.div(other: Int): R = this / multiplyByDoubling(one, other)
/**
* Returns sum of the integer represented as a rational function and the rational function.
*
* The operation is equivalent to adding [this] copies of unit polynomial to [other].
*/
public operator fun Int.plus(other: R): R = addMultipliedByDoubling(other, one, this)
/**
* Returns difference between the integer represented as a rational function and the rational function.
*
* The operation is equivalent to subtraction [this] copies of unit polynomial from [other].
*/
public operator fun Int.minus(other: R): R = addMultipliedByDoubling(-other, one, this)
/**
* Returns product of the integer represented as a rational function and the rational function.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
public operator fun Int.times(other: R): R = multiplyByDoubling(other, this)
/**
* Returns quotient of the integer represented as a rational function and the rational function.
*
* The operation is equivalent to creating a new rational function which numerator is [this] times denominator of
* [other] and which denominator is [other]'s numerator.
*/
public operator fun Int.div(other: R): R = multiplyByDoubling(one / other, this)
/**
* Converts the integer [value] to rational function.
*/
public fun number(value: Int): R = one * value
/**
* Converts the integer to rational function.
*/
public fun Int.asRationalFunction(): R = number(this)
/**
* Returns the same constant.
*/
@JvmName("unaryPlusConstant")
@JsName("unaryPlusConstant")
public operator fun C.unaryPlus(): C = this
/**
* Returns negation of the constant.
*/
@JvmName("unaryMinusConstant")
@JsName("unaryMinusConstant")
public operator fun C.unaryMinus(): C
/**
* Returns sum of the constants.
*/
@JvmName("plusConstantConstant")
@JsName("plusConstantConstant")
public operator fun C.plus(other: C): C
/**
* Returns difference of the constants.
*/
@JvmName("minusConstantConstant")
@JsName("minusConstantConstant")
public operator fun C.minus(other: C): C
/**
* Returns product of the constants.
*/
@JvmName("timesConstantConstant")
@JsName("timesConstantConstant")
public operator fun C.times(other: C): C
/**
* Raises [arg] to the integer power [exponent].
*/
@JvmName("powerConstant")
@JsName("powerConstant")
public fun power(arg: C, exponent: UInt) : C
/**
* Instance of zero constant (zero of the underlying ring).
*/
public val constantZero: C
/**
* Instance of unit constant (unit of the underlying ring).
*/
public val constantOne: C
/**
* Returns sum of the constant represented as a polynomial and the polynomial.
*/
@JvmName("plusConstantPolynomial")
public operator fun C.plus(other: P): P
/**
* Returns difference between the constant represented as a polynomial and the polynomial.
*/
@JvmName("minusConstantPolynomial")
public operator fun C.minus(other: P): P
/**
* Returns product of the constant represented as a polynomial and the polynomial.
*/
@JvmName("timesConstantPolynomial")
public operator fun C.times(other: P): P
/**
* Returns sum of the constant represented as a polynomial and the polynomial.
*/
@JvmName("plusPolynomialConstant")
public operator fun P.plus(other: C): P
/**
* Returns difference between the constant represented as a polynomial and the polynomial.
*/
@JvmName("minusPolynomialConstant")
public operator fun P.minus(other: C): P
/**
* Returns product of the constant represented as a polynomial and the polynomial.
*/
@JvmName("timesPolynomialConstant")
public operator fun P.times(other: C): P
/**
* Converts the constant [value] to polynomial.
*/
public fun polynomialNumber(value: C): P = polynomialOne * value
/**
* Converts the constant to polynomial.
*/
public fun C.asPolynomial(): P = polynomialNumber(this)
/**
* Returns the same polynomial.
*/
@JvmName("unaryPlusPolynomial")
public operator fun P.unaryPlus(): P = this
/**
* Returns negation of the polynomial.
*/
@JvmName("unaryMinusPolynomial")
public operator fun P.unaryMinus(): P
/**
* Returns sum of the polynomials.
*/
@JvmName("plusPolynomialPolynomial")
public operator fun P.plus(other: P): P
/**
* Returns difference of the polynomials.
*/
@JvmName("minusPolynomialPolynomial")
public operator fun P.minus(other: P): P
/**
* Returns product of the polynomials.
*/
@JvmName("timesPolynomialPolynomial")
public operator fun P.times(other: P): P
/**
* Returns quotient of the polynomials as rational function.
*/
@JvmName("divPolynomialPolynomial")
public operator fun P.div(other: P): R
/**
* Raises [arg] to the integer power [exponent].
*/
@JvmName("powerPolynomial")
public fun power(arg: P, exponent: UInt) : P
/**
* Instance of zero polynomial (zero of the polynomial ring).
*/
public val polynomialZero: P
/**
* Instance of unit polynomial (unit of the polynomial ring).
*/
public val polynomialOne: P
/**
* Returns sum of the constant represented as a rational function and the rational function.
*/
@JvmName("plusConstantRational")
public operator fun C.plus(other: R): R
/**
* Returns difference between the constant represented as a polynomial and the rational function.
*/
@JvmName("minusConstantRational")
public operator fun C.minus(other: R): R
/**
* Returns product of the constant represented as a polynomial and the rational function.
*/
@JvmName("timesConstantRational")
public operator fun C.times(other: R): R
/**
* Returns quotient of the constant represented as a polynomial and the rational function.
*/
@JvmName("divConstantRational")
public operator fun C.div(other: R): R
/**
* Returns sum of the rational function and the constant represented as a rational function.
*/
@JvmName("plusRationalConstant")
public operator fun R.plus(other: C): R
/**
* Returns difference between the rational function and the constant represented as a rational function.
*/
@JvmName("minusRationalConstant")
public operator fun R.minus(other: C): R
/**
* Returns product of the rational function and the constant represented as a rational function.
*/
@JvmName("timesRationalConstant")
public operator fun R.times(other: C): R
/**
* Returns quotient of the rational function and the constant represented as a rational function.
*/
@JvmName("divRationalConstant")
public operator fun R.div(other: C): R
/**
* Converts the constant [value] to rational function.
*/
@JvmName("numberConstant")
public fun number(value: C): R = one * value
/**
* Converts the constant to rational function.
*/
@JvmName("asRationalFunctionConstant")
public fun C.asRationalFunction(): R = number(this)
/**
* Returns sum of the polynomial represented as a rational function and the rational function.
*/
@JvmName("plusPolynomialRational")
public operator fun P.plus(other: R): R
/**
* Returns difference between the polynomial represented as a polynomial and the rational function.
*/
@JvmName("minusPolynomialRational")
public operator fun P.minus(other: R): R
/**
* Returns product of the polynomial represented as a polynomial and the rational function.
*/
@JvmName("timesPolynomialRational")
public operator fun P.times(other: R): R
/**
* Returns quotient of the polynomial represented as a polynomial and the rational function.
*/
@JvmName("divPolynomialRational")
public operator fun P.div(other: R): R
/**
* Returns sum of the rational function and the polynomial represented as a rational function.
*/
@JvmName("plusRationalPolynomial")
public operator fun R.plus(other: P): R
/**
* Returns difference between the rational function and the polynomial represented as a rational function.
*/
@JvmName("minusRationalPolynomial")
public operator fun R.minus(other: P): R
/**
* Returns product of the rational function and the polynomial represented as a rational function.
*/
@JvmName("timesRationalPolynomial")
public operator fun R.times(other: P): R
/**
* Returns quotient of the rational function and the polynomial represented as a rational function.
*/
@JvmName("divRationalPolynomial")
public operator fun R.div(other: P): R
/**
* Converts the polynomial [value] to rational function.
*/
@JvmName("numberPolynomial")
public fun number(value: P): R = one * value
/**
* Converts the polynomial to rational function.
*/
@JvmName("asRationalFunctionPolynomial")
public fun P.asRationalFunction(): R = number(this)
/**
* Returns the same rational function.
*/
public override operator fun R.unaryPlus(): R = this
/**
* Returns negation of the rational function.
*/
public override operator fun R.unaryMinus(): R
/**
* Returns sum of the rational functions.
*/
public override operator fun R.plus(other: R): R
/**
* Returns difference of the rational functions.
*/
public override operator fun R.minus(other: R): R
/**
* Returns product of the rational functions.
*/
public override operator fun R.times(other: R): R
/**
* Returns quotient of the rational functions.
*/
public operator fun R.div(other: R): R
/**
* Raises [arg] to the integer power [exponent].
*/
public override fun power(arg: R, exponent: UInt) : R = exponentiateBySquaring(arg, exponent)
/**
* Instance of zero rational function (zero of the rational functions ring).
*/
public override val zero: R
/**
* Instance of unit polynomial (unit of the rational functions ring).
*/
public override val one: R
/**
* Degree of the polynomial, [see also](https://en.wikipedia.org/wiki/Degree_of_a_polynomial). If the polynomial is
* zero, degree is -1.
*/
public val P.degree: Int
/**
* Degree of the polynomial, [see also](https://en.wikipedia.org/wiki/Degree_of_a_polynomial). If the polynomial is
* zero, degree is -1.
*/
public val R.numeratorDegree: Int get() = numerator.degree
/**
* Degree of the polynomial, [see also](https://en.wikipedia.org/wiki/Degree_of_a_polynomial). If the polynomial is
* zero, degree is -1.
*/
public val R.denominatorDegree: Int get() = denominator.degree
override fun add(left: R, right: R): R = left + right
override fun multiply(left: R, right: R): R = left * right
}
/**
* Abstraction of field of rational functions of type [R] with respect to polynomials of type [P] and constants of type
* [C]. It also assumes that there is provided [ring] (of type [A]), that provides constant-wise operations.
*
* @param C the type of constants. Polynomials have them as coefficients in their terms.
* @param P the type of polynomials. Rational functions have them as numerators and denominators in them.
* @param R the type of rational functions.
* @param A the type of algebraic structure (precisely, of ring) provided for constants.
*/
@Suppress("INAPPLICABLE_JVM_NAME") // FIXME: Waiting for KT-31420
public interface RationalFunctionSpaceOverRing<
C,
P,
R: RationalFunction<C, P>,
out A: Ring<C>
> : RationalFunctionSpace<C, P, R> {
/**
* Underlying ring of constants. Its operations on constants are inherited by local operations on constants.
*/
public val ring: A
/**
* Returns sum of the constant and the integer represented as a constant (member of underlying ring).
*
* The operation is equivalent to adding [other] copies of unit of underlying ring to [this].
*/
@JvmName("plusConstantInt")
public override operator fun C.plus(other: Int): C = ring { addMultipliedByDoubling(this@plus, one, other) }
/**
* Returns difference between the constant and the integer represented as a constant (member of underlying ring).
*
* The operation is equivalent to subtraction [other] copies of unit of underlying ring from [this].
*/
@JvmName("minusConstantInt")
public override operator fun C.minus(other: Int): C = ring { addMultipliedByDoubling(this@minus, one, -other) }
/**
* Returns product of the constant and the integer represented as a constant (member of underlying ring).
*
* The operation is equivalent to sum of [other] copies of [this].
*/
@JvmName("timesConstantInt")
public override operator fun C.times(other: Int): C = ring { multiplyByDoubling(this@times, other) }
/**
* Returns sum of the integer represented as a constant (member of underlying ring) and the constant.
*
* The operation is equivalent to adding [this] copies of unit of underlying ring to [other].
*/
@JvmName("plusIntConstant")
public override operator fun Int.plus(other: C): C = ring { addMultipliedByDoubling(other, one, this@plus) }
/**
* Returns difference between the integer represented as a constant (member of underlying ring) and the constant.
*
* The operation is equivalent to subtraction [this] copies of unit of underlying ring from [other].
*/
@JvmName("minusIntConstant")
public override operator fun Int.minus(other: C): C = ring { addMultipliedByDoubling(-other, one, this@minus) }
/**
* Returns product of the integer represented as a constant (member of underlying ring) and the constant.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
@JvmName("timesIntConstant")
public override operator fun Int.times(other: C): C = ring { multiplyByDoubling(other, this@times) }
/**
* Returns the same constant.
*/
@JvmName("unaryPlusConstant")
public override operator fun C.unaryPlus(): C = ring { +this@unaryPlus }
/**
* Returns negation of the constant.
*/
@JvmName("unaryMinusConstant")
public override operator fun C.unaryMinus(): C = ring { -this@unaryMinus }
/**
* Returns sum of the constants.
*/
@JvmName("plusConstantConstant")
public override operator fun C.plus(other: C): C = ring { this@plus + other }
/**
* Returns difference of the constants.
*/
@JvmName("minusConstantConstant")
public override operator fun C.minus(other: C): C = ring { this@minus - other }
/**
* Returns product of the constants.
*/
@JvmName("timesConstantConstant")
public override operator fun C.times(other: C): C = ring { this@times * other }
/**
* Raises [arg] to the integer power [exponent].
*/
@JvmName("powerConstant")
public override fun power(arg: C, exponent: UInt) : C = ring { power(arg, exponent) }
/**
* Instance of zero constant (zero of the underlying ring).
*/
public override val constantZero: C get() = ring.zero
/**
* Instance of unit constant (unit of the underlying ring).
*/
public override val constantOne: C get() = ring.one
}
/**
* Abstraction of field of rational functions of type [R] with respect to polynomials of type [P] and constants of type
* [C]. It also assumes that there is provided [polynomialRing] (of type [AP]), that provides constant- and
* polynomial-wise operations.
*
* @param C the type of constants. Polynomials have them as coefficients in their terms.
* @param P the type of polynomials. Rational functions have them as numerators and denominators in them.
* @param R the type of rational functions.
* @param AP the type of algebraic structure (precisely, of ring) provided for polynomials.
*/
@Suppress("INAPPLICABLE_JVM_NAME") // FIXME: Waiting for KT-31420
public interface RationalFunctionSpaceOverPolynomialSpace<
C,
P,
R: RationalFunction<C, P>,
out AP: PolynomialSpace<C, P>,
> : RationalFunctionSpace<C, P, R> {
/**
* Underlying polynomial ring. Its polynomial operations are inherited by local polynomial operations.
*/
public val polynomialRing: AP
/**
* Returns sum of the constant and the integer represented as a constant (member of underlying ring).
*
* The operation is equivalent to adding [other] copies of unit of underlying ring to [this].
*/
@JvmName("plusConstantInt")
public override operator fun C.plus(other: Int): C = polynomialRing { this@plus + other }
/**
* Returns difference between the constant and the integer represented as a constant (member of underlying ring).
*
* The operation is equivalent to subtraction [other] copies of unit of underlying ring from [this].
*/
@JvmName("minusConstantInt")
public override operator fun C.minus(other: Int): C = polynomialRing { this@minus - other }
/**
* Returns product of the constant and the integer represented as a constant (member of underlying ring).
*
* The operation is equivalent to sum of [other] copies of [this].
*/
@JvmName("timesConstantInt")
public override operator fun C.times(other: Int): C = polynomialRing { this@times * other }
/**
* Returns sum of the integer represented as a constant (member of underlying ring) and the constant.
*
* The operation is equivalent to adding [this] copies of unit of underlying ring to [other].
*/
@JvmName("plusIntConstant")
public override operator fun Int.plus(other: C): C = polynomialRing { this@plus + other }
/**
* Returns difference between the integer represented as a constant (member of underlying ring) and the constant.
*
* The operation is equivalent to subtraction [this] copies of unit of underlying ring from [other].
*/
@JvmName("minusIntConstant")
public override operator fun Int.minus(other: C): C = polynomialRing { this@minus - other }
/**
* Returns product of the integer represented as a constant (member of underlying ring) and the constant.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
@JvmName("timesIntConstant")
public override operator fun Int.times(other: C): C = polynomialRing { this@times * other }
/**
* Converts the integer [value] to constant.
*/
public override fun constantNumber(value: Int): C = polynomialRing { constantNumber(value) }
/**
* Converts the integer to constant.
*/
override fun Int.asConstant(): C = polynomialRing { asConstant() }
/**
* Returns sum of the constant and the integer represented as a polynomial.
*
* The operation is equivalent to adding [other] copies of unit polynomial to [this].
*/
@JvmName("plusPolynomialInt")
public override operator fun P.plus(other: Int): P = polynomialRing { this@plus + other }
/**
* Returns difference between the constant and the integer represented as a polynomial.
*
* The operation is equivalent to subtraction [other] copies of unit polynomial from [this].
*/
@JvmName("minusPolynomialInt")
public override operator fun P.minus(other: Int): P = polynomialRing { this@minus - other }
/**
* Returns product of the constant and the integer represented as a polynomial.
*
* The operation is equivalent to sum of [other] copies of [this].
*/
@JvmName("timesPolynomialInt")
public override operator fun P.times(other: Int): P = polynomialRing { this@times * other }
/**
* Returns sum of the integer represented as a polynomial and the constant.
*
* The operation is equivalent to adding [this] copies of unit polynomial to [other].
*/
@JvmName("plusIntPolynomial")
public override operator fun Int.plus(other: P): P = polynomialRing { this@plus + other }
/**
* Returns difference between the integer represented as a polynomial and the constant.
*
* The operation is equivalent to subtraction [this] copies of unit polynomial from [other].
*/
@JvmName("minusIntPolynomial")
public override operator fun Int.minus(other: P): P = polynomialRing { this@minus - other }
/**
* Returns product of the integer represented as a polynomial and the constant.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
@JvmName("timesIntPolynomial")
public override operator fun Int.times(other: P): P = polynomialRing { this@times * other }
/**
* Converts the integer [value] to polynomial.
*/
public override fun polynomialNumber(value: Int): P = polynomialRing { number(value) }
/**
* Converts the integer to polynomial.
*/
public override fun Int.asPolynomial(): P = polynomialRing { asPolynomial() }
/**
* Returns the same constant.
*/
@JvmName("unaryPlusConstant")
public override operator fun C.unaryPlus(): C = polynomialRing { +this@unaryPlus }
/**
* Returns negation of the constant.
*/
@JvmName("unaryMinusConstant")
public override operator fun C.unaryMinus(): C = polynomialRing { -this@unaryMinus }
/**
* Returns sum of the constants.
*/
@JvmName("plusConstantConstant")
public override operator fun C.plus(other: C): C = polynomialRing { this@plus + other }
/**
* Returns difference of the constants.
*/
@JvmName("minusConstantConstant")
public override operator fun C.minus(other: C): C = polynomialRing { this@minus - other }
/**
* Returns product of the constants.
*/
@JvmName("timesConstantConstant")
public override operator fun C.times(other: C): C = polynomialRing { this@times * other }
/**
* Raises [arg] to the integer power [exponent].
*/
@JvmName("powerConstant")
public override fun power(arg: C, exponent: UInt) : C = polynomialRing { power(arg, exponent) }
/**
* Instance of zero constant (zero of the underlying ring).
*/
public override val constantZero: C get() = polynomialRing.constantZero
/**
* Instance of unit constant (unit of the underlying ring).
*/
public override val constantOne: C get() = polynomialRing.constantOne
/**
* Returns sum of the constant represented as a polynomial and the polynomial.
*/
@JvmName("plusConstantPolynomial")
public override operator fun C.plus(other: P): P = polynomialRing { this@plus + other }
/**
* Returns difference between the constant represented as a polynomial and the polynomial.
*/
@JvmName("minusConstantPolynomial")
public override operator fun C.minus(other: P): P = polynomialRing { this@minus - other }
/**
* Returns product of the constant represented as a polynomial and the polynomial.
*/
@JvmName("timesConstantPolynomial")
public override operator fun C.times(other: P): P = polynomialRing { this@times * other }
/**
* Returns sum of the constant represented as a polynomial and the polynomial.
*/
@JvmName("plusPolynomialConstant")
public override operator fun P.plus(other: C): P = polynomialRing { this@plus + other }
/**
* Returns difference between the constant represented as a polynomial and the polynomial.
*/
@JvmName("minusPolynomialConstant")
public override operator fun P.minus(other: C): P = polynomialRing { this@minus - other }
/**
* Returns product of the constant represented as a polynomial and the polynomial.
*/
@JvmName("timesPolynomialConstant")
public override operator fun P.times(other: C): P = polynomialRing { this@times * other }
/**
* Converts the constant [value] to polynomial.
*/
public override fun polynomialNumber(value: C): P = polynomialRing { number(value) }
/**
* Converts the constant to polynomial.
*/
public override fun C.asPolynomial(): P = polynomialRing { asPolynomial() }
/**
* Returns the same polynomial.
*/
@JvmName("unaryPlusPolynomial")
public override operator fun P.unaryPlus(): P = polynomialRing { +this@unaryPlus }
/**
* Returns negation of the polynomial.
*/
@JvmName("unaryMinusPolynomial")
public override operator fun P.unaryMinus(): P = polynomialRing { -this@unaryMinus }
/**
* Returns sum of the polynomials.
*/
@JvmName("plusPolynomialPolynomial")
public override operator fun P.plus(other: P): P = polynomialRing { this@plus + other }
/**
* Returns difference of the polynomials.
*/
@JvmName("minusPolynomialPolynomial")
public override operator fun P.minus(other: P): P = polynomialRing { this@minus - other }
/**
* Returns product of the polynomials.
*/
@JvmName("timesPolynomialPolynomial")
public override operator fun P.times(other: P): P = polynomialRing { this@times * other }
/**
* Raises [arg] to the integer power [exponent].
*/
@JvmName("powerPolynomial")
public override fun power(arg: P, exponent: UInt) : P = polynomialRing { power(arg, exponent) }
/**
* Instance of zero polynomial (zero of the polynomial ring).
*/
public override val polynomialZero: P get() = polynomialRing.zero
/**
* Instance of unit polynomial (unit of the polynomial ring).
*/
public override val polynomialOne: P get() = polynomialRing.one
/**
* Degree of the polynomial, [see also](https://en.wikipedia.org/wiki/Degree_of_a_polynomial). If the polynomial is
* zero, degree is -1.
*/
public override val P.degree: Int get() = polynomialRing { [email protected] }
}
/**
* Abstraction of field of rational functions of type [R] with respect to polynomials of type [P] and constants of type
* [C]. It also assumes that there is provided constructor [constructRationalFunction] of rational functions from
* polynomial numerator and denominator.
*
* @param C the type of constants. Polynomials have them as coefficients in their terms.
* @param P the type of polynomials. Rational functions have them as numerators and denominators in them.
* @param R the type of rational functions.
*/
@Suppress("INAPPLICABLE_JVM_NAME") // FIXME: Waiting for KT-31420
public abstract class PolynomialSpaceOfFractions<
C,
P,
R: RationalFunction<C, P>,
> : RationalFunctionSpace<C, P, R> {
/**
* Constructor of rational functions (of type [R]) from numerator and denominator (of type [P]).
*/
protected abstract fun constructRationalFunction(numerator: P, denominator: P = polynomialOne) : R
/**
* Returns sum of the rational function and the integer represented as a rational function.
*
* The operation is equivalent to adding [other] copies of unit polynomial to [this].
*/
public override operator fun R.plus(other: Int): R =
constructRationalFunction(
numerator + denominator * other,
denominator
)
/**
* Returns difference between the rational function and the integer represented as a rational function.
*
* The operation is equivalent to subtraction [other] copies of unit polynomial from [this].
*/
public override operator fun R.minus(other: Int): R =
constructRationalFunction(
numerator - denominator * other,
denominator
)
/**
* Returns product of the rational function and the integer represented as a rational function.
*
* The operation is equivalent to sum of [other] copies of [this].
*/
public override operator fun R.times(other: Int): R =
constructRationalFunction(
numerator * other,
denominator
)
/**
* Returns quotient of the rational function and the integer represented as a rational function.
*
* The operation is equivalent to creating a new rational function by preserving numerator of [this] and
* multiplication denominator of [this] to [other].
*/
public override operator fun R.div(other: Int): R =
constructRationalFunction(
numerator,
denominator * other
)
/**
* Returns sum of the integer represented as a rational function and the rational function.
*
* The operation is equivalent to adding [this] copies of unit polynomial to [other].
*/
public override operator fun Int.plus(other: R): R =
constructRationalFunction(
other.denominator * this + other.numerator,
other.denominator
)
/**
* Returns difference between the integer represented as a rational function and the rational function.
*
* The operation is equivalent to subtraction [this] copies of unit polynomial from [other].
*/
public override operator fun Int.minus(other: R): R =
constructRationalFunction(
other.denominator * this - other.numerator,
other.denominator
)
/**
* Returns product of the integer represented as a rational function and the rational function.
*
* The operation is equivalent to sum of [this] copies of [other].
*/
public override operator fun Int.times(other: R): R =
constructRationalFunction(
this * other.numerator,
other.denominator
)
/**
* Returns quotient of the integer represented as a rational function and the rational function.
*
* The operation is equivalent to creating a new rational function which numerator is [this] times denominator of
* [other] and which denominator is [other]'s numerator.
*/
public override operator fun Int.div(other: R): R =
constructRationalFunction(
this * other.denominator,
other.numerator
)
/**
* Converts the integer [value] to rational function.
*/
public override fun number(value: Int): R = constructRationalFunction(polynomialNumber(value))
/**
* Returns quotient of the polynomials as rational function.
*/
@JvmName("divPolynomialPolynomial")
public override operator fun P.div(other: P): R = constructRationalFunction(this, other)
/**
* Returns sum of the constant represented as a rational function and the rational function.
*/
@JvmName("plusConstantRational")
public override operator fun C.plus(other: R): R =
constructRationalFunction(
other.denominator * this + other.numerator,
other.denominator
)
/**
* Returns difference between the constant represented as a polynomial and the rational function.
*/
@JvmName("minusConstantRational")
public override operator fun C.minus(other: R): R =
constructRationalFunction(
other.denominator * this - other.numerator,
other.denominator
)
/**
* Returns product of the constant represented as a polynomial and the rational function.
*/
@JvmName("timesConstantRational")
public override operator fun C.times(other: R): R =