-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduction.js
1268 lines (1019 loc) · 31.8 KB
/
reduction.js
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
/*
Fōrmulæ list package. Module for reduction.
Copyright (C) 2015-2023 Laurence R. Ugalde
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
'use strict';
export class List extends Formulae.Package {}
List.fromRange = async (range, session) => {
let left = CanonicalArithmetic.getBigInt(range.children[0]);
if (left === undefined) {
ReductionManager.setInError(range.children[0], "Expression must be an integer number");
throw new ReductionError();
}
let right = CanonicalArithmetic.getBigInt(range.children[1]);
if (right === undefined) {
ReductionManager.setInError(range.children[1], "Expression must be an integer number");
throw new ReductionError();
}
let result = Formulae.createExpression("List.List");
let i = left;
let step =left <= right ? 1n : -1n;
while (true) {
result.addChild(
CanonicalArithmetic.canonical2InternalNumber(
new CanonicalArithmetic.Integer(BigInt(i))
)
);
if (i === right) break;
i += step;
}
range.replaceBy(result);
return true;
};
List.table = async (table, session) => {
if (table.children.length != 2) return false;
if (table.children[1].getTag() !== "List.List") return false;
let cols = Utils.isMatrix(table.children[0]);
if (cols == 0) return false;
if (cols !== table.children[1].children.length) return false;
let copy = table.children[0].clone();
copy.addChildAt(0, table.children[1].clone());
table.setChild(0, copy);
table.removeChildAt(1);
return true;
};
List.createList = async (createList, session) => {
if (createList.children.length == 3) return false;
let n = createList.children.length;
let result;
if (n == 2) {
let arg = createList.children[0];
let _N = await session.reduceAndGet(createList.children[1], 1);
let N = CanonicalArithmetic.getInteger(_N);
if (N === undefined) return false;
result = Formulae.createExpression("List.List");
for (let i = 0; i < N; ++i) {
result.addChild(arg.clone());
}
createList.replaceBy(result);
await session.reduce(result);
//session.log("List created");
}
else {
// symbol
let symbol = createList.children[1];
if (symbol.getTag() !== "Symbolic.Symbol") {
return false;
}
for (let i = 2; i < n; ++i) {
await session.reduce(createList.children[i]);
}
// from
let from;
if (n >= 4) {
if (!createList.children[2].isInternalNumber()) return false;
from = createList.children[2].get("Value");
}
else {
from = new CanonicalArithmetic.Integer(1n);
}
// to
if (!createList.children[n == 3 ? 2 : 3].isInternalNumber()) return false;
let to = createList.children[n == 3 ? 2 : 3].get("Value");
// step
let step;
if (n == 5) {
if (!createList.children[4].isInternalNumber()) return false;
step = createList.children[4].get("Value");
}
else {
step = new CanonicalArithmetic.Integer(1n);
}
if (step.isZero()) return false;
// sign
let negative = step.isNegative();
result = Formulae.createExpression("List.List");
//////////////////////////////////////
result.createScope();
let scopeEntry = new ScopeEntry();
result.putIntoScope(symbol.get("Name"), scopeEntry, false);
//createList.replaceBy(result);
//session.log("List created");
let arg = createList.children[0];
let clone;
let isTable = createList.getTag() === "List.CreateTable";
let hasHeaders = isTable && Utils.isMatrix(arg) > 0;
if (isTable) {
let table = Formulae.createExpression("List.Table");
table.addChild(result);
createList.replaceBy(table);
}
else {
createList.replaceBy(result);
}
if (isTable) { // header
if (hasHeaders) {
result.addChild(arg.children[0].clone());
}
else {
result.addChild(arg.clone());
}
}
filling: while (true) {
if (negative) {
if (from.comparison(to, session) < 0) {
break filling;
}
}
else {
if (from.comparison(to, session) > 0) {
break filling;
}
}
scopeEntry.setValue(CanonicalArithmetic.canonical2InternalNumber(from));
if (hasHeaders) {
result.addChild(clone = arg.children[1].clone());
}
else {
result.addChild(clone = arg.clone());
}
//result.addChild(clone = arg.clone());
//session.log("Element created");
await session.reduce(clone);
from = from.addition(step, session);
}
result.removeScope();
}
return true;
};
List.createListList = async (createList, session) => {
if (createList.children.length != 3) return false;
// the symbol, or list of symbols
let numberOfSymbols;
let symbols;
{
let spec = createList.children[1];
switch (spec.getTag()) {
case "Symbolic.Symbol":
numberOfSymbols = 1;
symbols = [ spec.get("Name") ];
break;
case "List.List":
numberOfSymbols = spec.children.length;
symbols = Array(numberOfSymbols);
for (let s = 0; s < numberOfSymbols; ++s) {
if (spec.children[s].getTag() !== "Symbolic.Symbol") return false;
symbols[s] = spec.children[s].get("Name");
}
break;
default: return false;
}
}
// the list to iterate over
let list = await session.reduceAndGet(createList.children[2], 2);
if (list.getTag() !== "List.List") {
return false;
}
// the argument
let arg = createList.children[0];
// the resulting list
let result = Formulae.createExpression("List.List");
let isTable = createList.getTag() === "List.CreateTable";
let hasHeaders = isTable && Utils.isMatrix(arg) > 0;
if (isTable) {
let table = Formulae.createExpression("List.Table");
table.addChild(result);
createList.replaceBy(table);
}
else {
createList.replaceBy(result);
}
// the first element of the resulting list, the header (if it is a table)
if (isTable) {
if (hasHeaders) {
result.addChild(arg.children[0].clone());
}
else {
result.addChild(arg.clone());
}
}
//createList.replaceBy(result);
//session.log("List created");
result.createScope();
let scopeEntries = Array(numberOfSymbols);
for (let s = 0; s < numberOfSymbols; ++s) {
scopeEntries[s] = new ScopeEntry();
result.putIntoScope(symbols[s], scopeEntries[s], false);
}
// filling
for (let e = 0, E = list.children.length; e < E; ++e) {
if (numberOfSymbols == 1) {
scopeEntries[0].setValue(list.children[e].clone());
}
else {
for (let s = 0; s < numberOfSymbols; ++s) {
scopeEntries[s].setValue(list.children[e].children[s].clone());
}
}
if (isTable && hasHeaders) {
result.addChild(arg.children[1].clone());
}
else {
result.addChild(arg.clone());
}
//result.unlockScope();
await session.reduce(result.children[result.children.length - 1]);
//result.lockScope();
}
result.removeScope();
return true;
};
List.createCrossedTable = async (createCrossedTable, session) => {
// the two symbols
let symbolList = createCrossedTable.children[1];
let symbolName1, symbolName2;
{
if (symbolList.getTag() !== "List.List") return false;
if (symbolList.children.length !== 2) return false;
let symbol1, symbol2;
if ((symbol1 = symbolList.children[0]).getTag() !== "Symbolic.Symbol") return false;
if ((symbol2 = symbolList.children[1]).getTag() !== "Symbolic.Symbol") return false;
symbolName1 = symbol1.get("Name");
symbolName2 = symbol2.get("Name");
if (symbolName1 === symbolName2) return false;
}
// the two lists to iterate over
let list1, list2;
lists: {
let lists = await session.reduceAndGet(createCrossedTable.children[2], 2);
if (lists.getTag() !== "List.List") return false;
if (lists.children.length == 1) {
if ((list1 = list2 = lists.children[0]).getTag() !== "List.List" || list1.children.length == 0) return false;
break lists;
}
if (lists.children.length == 2) {
if ((list1 = lists.children[0]).getTag() !== "List.List" || list1.children.length == 0) return false;
if ((list2 = lists.children[1]).getTag() !== "List.List" || list2.children.length == 0) return false;
break lists;
}
return false;
}
// the argument
let arg = createCrossedTable.children[0];
// the resulting matrix
let matrix = Formulae.createExpression("List.List");
let result = Formulae.createExpression("List.Table");
result.addChild(matrix);
createCrossedTable.replaceBy(result);
// creating the scope
result.createScope();
let entry1 = new ScopeEntry();
let entry2 = new ScopeEntry();
matrix.putIntoScope(symbolName1, entry1, false);
matrix.putIntoScope(symbolName2, entry2, false);
// filling
let x, X = list2.children.length;
let y, Y = list1.children.length;
let row;
let a;
// first row
row = Formulae.createExpression("List.List");
row.addChild(symbolList.clone());
for (x = 0; x < X; ++x) {
row.addChild(list2.children[x].clone())
}
matrix.addChild(row);
// rows
for (y = 0; y < Y; ++y) {
row = Formulae.createExpression("List.List");
matrix.addChild(row);
row.addChild(list1.children[y].clone());
entry1.setValue(list1.children[y].clone());
for (x = 0; x < X; ++x) {
//console.log(y + " - " + x);
entry2.setValue(list2.children[x].clone());
row.addChild(a = arg.clone());
await session.reduce(a);
}
}
result.removeScope();
return true;
};
/*
List.negativeList = async (negativeList, session) => {
let list = negativeList.children[0];
if (list.getTag() !== "List.List") return false;
let i, n = list.children.length;
let negative;
for (i = 0; i < n; ++i) {
negative = Formulae.createExpression("Math.Arithmetic.Negative");
negative.addChild(list.children[i]);
list.setChild(i, negative);
}
negativeList.replaceBy(list);
//session.log("Negative of a list");
for (i = 0; i < n; ++i) {
await session.reduce(list.children[i]);
}
return true;
};
*/
List.additionLists = async (additionList, session) => {
let sizePivot;
let pivot;
let updated = false;
outer: for (let o = 0, O = additionList.children.length - 1; o < O; ++o) {
pivot = additionList.children[o];
if (pivot.getTag() !== "List.List") continue outer;
if ((sizePivot = pivot.children.length) > 0) { // a pivot
let test;
let found = false;
inner: for (let i = o + 1; i <= O; ++i) {
test = additionList.children[i];
if (test.getTag() !== "List.List") continue inner;
if (sizePivot == test.children.length) { // hit
updated = found = true;
let expr;
for (let e = 0; e < sizePivot; ++e) {
expr = pivot.children[e];
if (expr.getTag() !== "Math.Arithmetic.Addition") {
let sum = Formulae.createExpression("Math.Arithmetic.Addition");
expr.replaceBy(sum);
sum.addChild(expr);
expr = sum;
}
expr.addChild(test.children[e]);
}
additionList.removeChildAt(i);
--i;
--O;
}
}
if (found) {
//session.log("Addition of same size lists");
for (let i = 0; i < sizePivot; ++i) {
await session.reduce(pivot.children[i]);
}
}
}
}
if (updated) {
if (additionList.children.length == 1) {
additionList.replaceBy(additionList.children[0]);
}
return true;
}
return false;
};
List.multiplicationScalarList = async (multiplication, session) => {
if (multiplication.children.length !== 2) return false;
let scalar = multiplication.children[0];
if (!scalar.isInternalNumber()) return false;
let list = multiplication.children[1];
if (list.getTag() !== "List.List") return false;
let i, n = list.children.length;
let mult;
for (i = 0; i < n; ++i) {
mult = Formulae.createExpression("Math.Arithmetic.Multiplication");
mult.addChild(scalar.clone());
mult.addChild(list.children[i]);
list.setChild(i, mult);
}
multiplication.replaceBy(list);
//session.log("Multiplication of a sclar and a list");
for (i = 0; i < n; ++i) {
await session.reduce(list.children[i]);
}
return true;
};
List.matrixMultiplication = async (multiplication, session) => {
let left;
let colsLeft;
let updated = false;
for (
let i = 0, n_1 = multiplication.children.length - 1;
i < n_1;
++i
) {
left = multiplication.children[i];
if ((colsLeft = Utils.isMatrix(left)) > 0) {
let right = multiplication.children[i + 1];
let colsRight = Utils.isMatrix(right);
if (colsRight > 0) {
let rowsRight = right.children.length;
if (colsLeft == rowsRight) {
updated = true;
let rowsLeft = left.children.length;
let result = Formulae.createExpression("List.List");
let row, target;
let r, c, x;
let mult;
for (r = 0; r < rowsLeft; ++r) {
result.addChild(row = Formulae.createExpression("List.List"));
for (c = 0; c < colsRight; ++c) {
if (colsLeft == 1) {
target = row;
}
else {
row.addChild(
target = Formulae.createExpression("Math.Arithmetic.Addition")
);
}
for (x = 0; x < colsLeft; ++x) {
mult = Formulae.createExpression("Math.Arithmetic.Multiplication");
mult.addChild(left.children[r].children[x].clone());
mult.addChild(right.children[x].children[c].clone());
target.addChild(mult);
}
}
}
left.replaceBy(result);
multiplication.removeChildAt(i + 1);
//session.log("Multiplication of matrices");
for (r = 0; r < rowsLeft; ++r) {
for (c = 0; c < colsRight; ++c) {
await session.reduce(result.children[r].children[c]);
}
}
--i;
--n_1;
}
}
}
}
if (updated) {
if (multiplication.children.length == 1) {
multiplication.replaceBy(multiplication.children[0]);
}
return true;
}
return false;
};
List.matrixExponentiation = async (exponentiation, session) => {
let matrix = exponentiation.clone().children[0];
let cols = Utils.isMatrix(matrix);
if (cols <= 0) return false;
if (matrix.children.length != cols) return false;
let exponent = exponentiation.children[1];
let n = CanonicalArithmetic.getInteger(exponent);
if (n === undefined) return false;
////////////////////////
if (n == 1) {
exponentiation.replaceBy(matrix);
//session.log("Matrix exponentiation");
return true;
}
let result = Formulae.createExpression("Math.Arithmetic.Multiplication");
for (let i = 0; i < n; ++i) {
result.addChild(matrix.clone());
}
exponentiation.replaceBy(result);
//session.log("Matrix exponentiation");
await session.reduce(result);
return true;
};
List.matrixTranspose = async (transpose, session) => {
let matrix = transpose.children[0];
let cols = Utils.isMatrix(matrix);
if (cols > 0) {
let r, c, rows = matrix.children.length;
let row, result = Formulae.createExpression("List.List");
for (r = 0; r < cols; ++r) {
result.addChild(row = Formulae.createExpression("List.List"));
for (c = 0; c < rows; ++c) {
row.addChild(matrix.children[c].children[r].clone());
}
}
transpose.replaceBy(result);
//session.log("Transpose of a matrix");
return true;
}
return false;
};
List.matrixDeterminant = async (determinant, session) => {
let matrix = determinant.children[0];
let size = Utils.isMatrix(matrix);
// matrix ?
if (size <= 0) return false;
// square ?
if (matrix.children.length != size) return false;
// 1 x 1
if (size == 1) {
determinant.replaceBy(matrix.children[0].children[0]);
//session.log("Determinant of a matrix");
return true;
}
// 2 x 2
if (size == 2) {
let mult1 = Formulae.createExpression("Math.Arithmetic.Multiplication");
mult1.addChild(matrix.children[0].children[0]);
mult1.addChild(matrix.children[1].children[1]);
let mult2 = Formulae.createExpression("Math.Arithmetic.Multiplication");
mult2.addChild(matrix.children[0].children[1]);
mult2.addChild(matrix.children[1].children[0]);
let neg = Formulae.createExpression("Math.Arithmetic.Negative");
neg.addChild(mult2);
let result = Formulae.createExpression("Math.Arithmetic.Addition");
result.addChild(mult1);
result.addChild(neg);
determinant.replaceBy(result);
//session.log("Determinant of a matrix");
await session.reduce(result);
return true;
}
// 3 x 3 or greater
let addend, multiplication, det, minor, row;
let addition =
Formulae.createExpression("Math.Arithmetic.Addition")
;
let r, c, size_1 = size - 1;
for (let part = 0; part < size; ++part) {
multiplication = Formulae.createExpression(
"Math.Arithmetic.Multiplication"
);
det = Formulae.createExpression(
"Math.Matrix.Determinant"
);
det.addChild(
minor = Formulae.createExpression("List.List")
);
multiplication.addChild(matrix.children[0].children[part].clone());
multiplication.addChild(det);
if (part % 2 != 0) {
addend = Formulae.createExpression(
"Math.Arithmetic.Negative"
);
addend.addChild(multiplication);
}
else {
addend = multiplication;
}
for (r = 0; r < size_1; ++r) {
minor.addChild(row = Formulae.createExpression("List.List"));
for (c = 0; c < size_1; ++c) {
row.addChild(matrix.children[r + 1].children[c >= part ? c + 1 : c].clone());
}
}
addition.addChild(addend);
}
determinant.replaceBy(addition);
//session.log("Determinant of a matrix");
await session.reduce(addition);
return true;
};
List.rangeLookup = async (lookup, session) => {
let table = lookup.children[0];
// it is not a table, or it is a table with less than 2 columns
if (Utils.isMatrix(table) < 2) {
return false;
}
let value = lookup.children[1];
let pivot;
let compare;
let result;
let row;
rows: for (let r = 0, R = table.children.length; r < R; ++r) {
row = table.children[r];
pivot = row.children[0];
compare = Formulae.createExpression("Relation.Compare");
compare.addChild(value.clone());
compare.addChild(pivot.clone());
row.setChild(0, compare);
//session.log("conversion to compare");
// comparing
result = await session.reduceAndGet(compare, 0);
// restoring
row.setChild(0, pivot);
//session.log("restoring");
// there was no reduction
if (result === compare) {
return false;
}
// a reduction was performed
if (result.getTag() === "Relation.Comparison.Greater") {
if (r == R - 1) { // last row
lookup.replaceBy(row);
//session.log("Lookup succeded");
return true;
}
continue rows;
}
if (result.getTag() === "Relation.Comparison.Less") {
if (r == 0) {
return false;
}
lookup.replaceBy(table.children[r - 1]);
//session.log("Lookup succeded");
return true;
}
if (result.getTag() === "Relation.Comparison.Equals") {
lookup.replaceBy(row);
//session.log("Lookup succeded");
return true;
}
}
return false;
};
List.exactLookupReducer = async (lookup, session) => {
let table = lookup.children[0];
let value = lookup.children[1];
if (Utils.isMatrix(table) < 2) {
return false;
}
let compare;
let result;
let pivot;
let row;
for (let r = 0, R = table.children.length; r < R; ++r) {
row = table.children[r];
pivot = row.children[0];
compare = Formulae.createExpression("Relation.Compare");
compare.addChild(value.clone());
compare.addChild(pivot.clone());
row.setChild(0, compare);
//session.log("conversion to compare");
// comparing
result = await session.reduceAndGet(compare, 0);
// restoring
row.setChild(0, pivot);
//session.log("restoring");
if (result.getTag() === "Relation.Comparison.Equals") {
lookup.replaceBy(row);
//session.log("Lookup succeded");
return true;
}
}
return false;
};
List.cartesianProduct = async (expr, session) => {
let i, n = expr.children.length;
for (i = 0; i < n; ++i) {
if (expr.children[i].getTag() !== "List.List") {
return false;
}
}
let indices = Array(n).fill(0);
let max = Array(n);
let result = Formulae.createExpression("List.List");
for (i = 0; i < n; ++i) {
if ((max[i] = expr.children[i].children.length) == 0) {
expr.replaceBy(result);
//session.log("cartesian product");
return true;
}
}
let row = null;
let m;
product: while (true) {
row = Formulae.createExpression("List.List");
for (i = 0; i < n; ++i) {
row.addChild(expr.children[i].children[indices[i]].clone());
}
result.addChild(row);
for (i = 0; i < n; ++i) {
m = n - i - 1;
++indices[m];
if (indices[m] == max[m]) {
if (m == 0) {
break product;
}
indices[m] = 0;
}
else {
break;
}
}
}
expr.replaceBy(result);
//session.log("cartesian product");
return true;
};
List.cartesianExponentiation = async (expr, session) => {
if (expr.children[0].getTag() !== "List.List") {
return false;
}
let pow = CanonicalArithmetic.getInteger(expr.children[1]);
if (pow == null) {
return false;
}
let list = expr.children[0];
if (pow < 0) {
ReductionManager.setInError(expr.children[1], "Expression must be a non-negative integer number");
throw new ReductionError();
}
if (pow == 0) {
let outerList = Formulae.createExpression("List.List");
outerList.addChild(Formulae.createExpression("List.List"));
expr.replaceBy(outerList);
//session.log("cartesian exponentiation");
return true;
}
else { // pos > 0
let product = Formulae.createExpression("List.CartesianProduct");
for (let i = 0; i < pow; ++i) {
product.addChild(list.clone());
}
expr.replaceBy(product);
//session.log("cartesian exponentiation");
await session.reduce(product);
return true;
}
};
List.kroneckerProduct = async (kroneckerProduct, session) => {
let m1, m2;
let result, row;
let r1, c1, r2, c2;
let C1, R1, C2, R2;
let mult;
factors: while (true) {
m1 = kroneckerProduct.children[0];
C1 = Utils.isMatrix(m1);
if (C1 < 0) return false;
R1 = m1.children.length;
m2 = kroneckerProduct.children[1];
C2 = Utils.isMatrix(m2);
if (C2 < 0) return false;
R2 = m2.children.length;
result = Formulae.createExpression("List.List");
for (r1 = 0; r1 < R1; ++r1) {
for (r2 = 0; r2 < R2; ++r2) {
row = Formulae.createExpression("List.List");
for (c1 = 0; c1 < C1; ++c1) {
for (c2 = 0; c2 < C2; ++c2) {
row.addChild(
mult = Formulae.createExpression("Math.Arithmetic.Multiplication")
);
mult.addChild(m1.children[r1].children[c1].clone());
mult.addChild(m2.children[r2].children[c2].clone());
}
}
result.addChild(row);
}
}
if (kroneckerProduct.children.length == 2) {
break factors;
}
else {
m1.replaceBy(result);
kroneckerProduct.removeChildAt(1);
//session.log("Kronecker product");
}
}
kroneckerProduct.replaceBy(result);
//session.log("Kronecker product");
await session.reduce(result);
return true;
};
List.dotProduct = async (dotProduct, session) => {
let f1 = dotProduct.children[0];
if (f1.getTag() !== "List.List") return false;
let f2 = dotProduct.children[1];
if (f2.getTag() !== "List.List") return false;
if (f1.children.length != f2.children.length) return false;
let result;
switch (f1.children.length) {
case 0:
dotProduct.replaceBy(
CanonicalArithmetic.canonical2InternalNumber(
new CanonicalArithmetic.Integer(0n)
)
);
break;
case 1:
result = Formulae.createExpression("Math.Arithmetic.Multiplication");
result.addChild(f1.children[0].clone()),
result.addChild(f2.children[0].clone())