-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGAS.pck.st
More file actions
1583 lines (1453 loc) · 64.8 KB
/
GAS.pck.st
File metadata and controls
1583 lines (1453 loc) · 64.8 KB
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
'From Cuis7.3 [latest update: #7158] on 13 July 2025 at 5:45:58 pm'!
'Description General Algebra Subrutines. This package provides low-level support for algebra on arrays.'!
!provides: 'GAS' 1 24!
!requires: 'Collections-Extras' 1 14 nil!
!SequenceableCollection methodsFor: '*gas' stamp: 'len 1/10/2025 10:09:58'!
· aCollection
| answer |
self flag: #deprecated. "but used in Buchberger algorithm"
self isEmpty ifTrue: [^ aCollection first zero].
aCollection isEmpty ifTrue: [^ self first zero].
answer := self first * aCollection first.
2 to: (self size min: aCollection size) do: [:i| answer := (self at: i) * (aCollection at: i) + answer].
^ answer! !
!SequenceableCollection methodsFor: '*gas' stamp: 'len 3/23/2016 05:04'!
at: anInteger add: anObject
^ self at: anInteger put: (self at: anInteger) + anObject! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/5/2023 16:35:18'!
* anObject
| n |
self species = anObject species
ifTrue: [^ (self species new: (n _ self size min: anObject size)) replaceFrom: 1 to: n with: self startingAt: 1 times: anObject startingAt: 1].
anObject isCollection ifTrue: [^ super * anObject].
^ (self species new: (n _ self size)) replaceFrom: 1 to: n with: self startingAt: 1 timesScalar: anObject! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/5/2023 16:37:02'!
*= anObject
self isEmpty ifTrue: [^ self].
anObject species = self species
ifTrue: [ self replaceFrom: 1 to: self size with: self startingAt: 1 times: anObject startingAt: 1]
ifFalse: [self replaceFrom: 1 to: self size with: self startingAt: 1 timesScalar: anObject]! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/11/2023 17:42:47'!
+ anObject
| n m answer |
anObject species = self species ifFalse:
[anObject isCollection ifTrue: [^ super + anObject].
^ self collect: [:each| each + anObject]].
n _ self size.
m _ anObject size.
answer _ self species new: (n max: m).
answer replaceFrom: 1 to: (n min: m) with: self startingAt: 1 plus: anObject startingAt: 1.
m > n
ifTrue: [answer replaceFrom: n+1 to: m with: anObject startingAt: n+1]
ifFalse: [m < n ifTrue: [answer replaceFrom: m+1 to: n with: self startingAt: m+1]].
^ answer! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 2/1/2024 19:56:47'!
+= anObject
anObject species = self species ifFalse:
[anObject isCollection ifTrue: [^ super += anObject].
^ self replace: [:each| each + anObject]].
self replaceFrom: 1 to: self size plus: anObject startingAt: 1! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/6/2023 16:09:15'!
, anObject
anObject isCollection ifTrue: [^ super , anObject].
^ self copyWith: anObject! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/11/2023 17:43:04'!
- anObject
| n m answer |
anObject species = self species ifFalse:
[anObject isCollection ifTrue: [^ super - anObject].
^ self collect: [:each| each - anObject]].
n _ self size.
m _ anObject size.
answer _ self species new: (n max: m).
answer replaceFrom: 1 to: (n min: m) with: self startingAt: 1 minus: anObject startingAt: 1.
m > n
ifTrue: [answer replaceFrom: n+1 to: m withNegated: anObject startingAt: n+1]
ifFalse: [m < n ifTrue: [answer replaceFrom: m+1 to: n with: self startingAt: m+1]].
^ answer! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 2/1/2024 20:13:26'!
-= anObject
anObject species = self species ifFalse:
[anObject isCollection ifTrue: [^ super -= anObject].
^ self replace: [:each| each - anObject]].
self replaceFrom: 1 to: self size minus: anObject startingAt: 1! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/5/2023 16:42:56'!
/ anObject
anObject isCollection ifTrue: [^ super / anObject].
^ self collect: [:each| each / anObject]! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/5/2023 16:43:26'!
// anObject
anObject isCollection ifTrue: [^ super // anObject].
^ self collect: [:each| each // anObject]! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/5/2023 15:53:37'!
<< anInteger
"Answer the left shift of the receiver by anInteger places.
The answer is a new array of the same size as the receiver."
^ self >> anInteger negated! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/5/2023 13:15:55'!
<<< anInteger
"Answer the circular left shift of the receiver by anInteger places.
The answer is a new array of the same size as the receiver."
^ self >>> anInteger negated! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/9/2023 18:50:04'!
>> anInteger
"Answer the right shift of the receiver by anInteger places.
The answer is a new array of the same size as the receiver."
| answer zero n m |
(anInteger = 0 or: [(m _ self size) = 0]) ifTrue: [^ self].
answer _ self species new: m.
zero _ self first zero.
n _ m min: anInteger max: m negated.
n > 0
ifTrue:
[answer from: 1 to: n put: zero.
answer replaceFrom: 1+n to: m with: self startingAt: 1]
ifFalse:
[answer from: m+n+1 to: answer size put: zero.
answer replaceFrom: 1 to: m+n with: self startingAt: 1-n].
^ answer! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/9/2023 18:50:18'!
>>> anInteger
"Answer the circular right shift of the receiver by anInteger places.
The answer is a new array of the same size as the receiver."
| n m |
(anInteger = 0 or: [(m _ self size) = 0]) ifTrue: [^ self].
n _ anInteger \\ m.
^ (self species new: m)
replaceFrom: 1 to: n with: self startingAt: m-n+1;
replaceFrom: 1+n to: m with: self startingAt: 1! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/5/2023 16:43:36'!
\\ anObject
anObject isCollection ifTrue: [^ super \\ anObject].
^ self collect: [:each| each \\ anObject]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 5/20/2023 23:58:36'!
· anArray
| n i answer |
answer := self first zero.
n := self size min: anArray size.
i := 1.
[i <= n] whileTrue:
[answer := (self at: i) * (anArray at: i) + answer.
i := i + 1].
^ answer! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 12/9/2022 07:57:27'!
autoconvolution
self isEmpty ifTrue: [^ self].
^ (self species new: self size * 2 - 1)
replaceFrom: 1 withAutoconvolution: self from: 1 to: self size! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 12/9/2022 07:56:19'!
convolution: anArray
self isEmpty ifTrue: [^ self].
anArray isEmpty ifTrue: [^ anArray].
^ (self species new: self size + anArray size - 1)
replaceFrom: 1 with: self from: 1 to: self size convolution: anArray from: 1 to: anArray size! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 2/3/2024 16:45:34'!
dot: anArray modulo: modulus
| answer n i minusOne |
n := self size min: anArray size.
answer := 0.
i := 1.
modulus squared * n sqrt < 16r40000000
ifTrue: "delayed reduction"
[[i <= n] whileTrue:
[answer := (self at: i) * (anArray at: i) + answer.
i := i + 1].
answer := answer \\ modulus]
ifFalse:
[((minusOne := modulus - 1) bitAnd: modulus) = 0
ifTrue: "modulus is power of 2"
[[i <= n] whileTrue:
[answer := (self at: i) * (anArray at: i) + answer bitAnd: minusOne.
i := i + 1]]
ifFalse:
[[i <= n] whileTrue:
[answer := (self at: i) * (anArray at: i) + answer \\ modulus.
i := i + 1]]].
^ answer! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 6/6/2023 13:16:08'!
evaluateFrom: start to: stop at: anElement
"Evaluate the receiver (as an univariate polynomial from start to stop) at the given argument using ranged Horner's method."
| answer i |
answer := anElement zero.
i := stop.
[i >= start] whileTrue:
[answer := anElement * answer + (self at: i).
i := i - 1].
^ answer! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 2/3/2024 16:45:43'!
evaluateFrom: start to: stop at: anInteger modulo: modulus
"Evaluate the receiver (as an univariate polynomial from start to stop) at the given argument using ranged Horner's method."
| answer i minusOne |
stop < start ifTrue: [^ 0].
anInteger = 0 ifTrue: [^ self at: start].
anInteger = 1 ifTrue: [^ self sumFrom: start to: stop modulo: modulus].
answer := 0.
i := stop.
((minusOne := modulus - 1) bitAnd: modulus) = 0
ifTrue: "power of 2"
[[i >= start] whileTrue:
[answer := anInteger * answer + (self at: i) bitAnd: minusOne.
i := i - 1]]
ifFalse:
[[i >= start] whileTrue:
[answer := anInteger * answer + (self at: i) \\ modulus.
i := i - 1]].
^ answer! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 6/21/2023 23:03:56'!
exponentiate: anArray
| answer next |
1 to: anArray size do: [:i|
next := (self at: i) ^ (anArray at: i).
answer := answer ifNil: [next] ifNotNil: [next * answer]].
^ answer! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 6/21/2023 23:05:51'!
exponentiate: anArray modulo: modulus
| answer |
answer := 1.
1 to: anArray size do: [:i|
answer := answer * ((self at: i) raisedTo: (anArray at: i) modulo: modulus) \\ modulus].
^ answer! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 5/20/2022 11:36:55'!
findFirstNonzeroFrom: start to: stop
"Answer the index of the first nonzero entry between start and stop, or nil."
start to: stop do: [:i| (self isZeroAt: i) ifFalse: [^ i]].
^ nil! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 12/27/2022 11:24:59'!
findLastNonzeroFrom: start to: stop
"Answer the index of the last nonzero entry between start and stop, or nil."
| i |
i _ stop.
[i >= start] whileTrue:
[(self isZeroAt: i) ifFalse: [^ i].
i _ i - 1].
^ nil! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/20/2022 11:37:09'!
firstNonzeroIndex
"Answer the index of the first nonzero entry, or nil."
^ self findFirstNonzeroFrom: 1 to: self size! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/20/2022 20:57:37'!
isZero
^ (self findFirstNonzeroFrom: 1 to: self size) isNil! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 1/10/2025 10:05:32'!
isZeroAt: anInteger
^ (self at: anInteger) isZero! !
!ArrayedCollection methodsFor: '*gas-low level-karatsuba' stamp: 'len 5/14/2023 22:52:02'!
karatsubaSplit: length startingAt: start
^ length + 1 // 2! !
!ArrayedCollection methodsFor: '*gas-low level-karatsuba' stamp: 'len 5/14/2023 11:36:51'!
karatsubaThreshold
^ 32! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/20/2022 11:37:29'!
lastNonzeroIndex
"Answer the index of the last nonzero entry, or nil."
^ self findLastNonzeroFrom: 1 to: self size! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/5/2023 16:01:45'!
negated
^ (self species new: self size) replaceFrom: 1 to: self size withNegated: self startingAt: 1! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/23/2023 10:34:56'!
partiallyReduceBy: g
self partiallyReduceBy: g quotient: nil! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 2/2/2024 10:56:43'!
partiallyReduceBy: g quotient: q
| n m lead |
m := g lastNonzeroIndex ifNil: [^ self error: 'division by zero'].
lead := g at: m.
[(n := self lastNonzeroIndex ifNil: [0]) >= m]
whileTrue:
[| i c |
i := n-m+1.
c := ((self at: n) // lead) negated.
c isZero ifTrue: [^ self].
q ifNotNil: [q at: i put: (q at: i) - c].
self replaceFrom: i to: i+m-1 plus: g startingAt: 1 timesScalar: c]! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 6/2/2023 18:55:09'!
partiallyReduceBy: g quotient: q modulo: modulus
| n m lead |
m := g lastNonzeroIndex ifNil: [^ self error: 'division by zero'].
lead := g at: m.
[(n := self lastNonzeroIndex ifNil: [0]) >= m]
whileTrue:
[| i c |
i := n-m+1.
c := ((self at: n) // lead) negated.
c = 0 ifTrue: [^ self].
q ifNotNil: [q at: i put: (q at: i) - c \\ modulus].
self replaceFrom: i to: i+m-1 with: self startingAt: i plus: g startingAt: 1 timesScalar: c modulo: modulus]! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 1/10/2025 10:13:15'!
permutedBy: aPermutation
| answer |
answer := self species new: self size.
1 to: self size do: [:i| answer at: (aPermutation at: i) put: (self at: i)].
^ answer! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 6/6/2023 08:47:56'!
polynomialDivisionBy: anArray
| q r |
q := self zeros.
r := self copy reduceBy: anArray quotient: q.
^ {q. r}! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 6/6/2023 08:48:24'!
polynomialPartialDivisionBy: anArray
| q r |
q := self zeros.
r := self copy partiallyReduceBy: anArray quotient: q.
^ {q. r}! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 6/6/2023 00:17:56'!
polynomialPartialQuotientBy: anArray
| q r k m n |
n := self size - 1.
m := anArray size - 1.
(n > m and: [(k := n - m * 2) < n]) ifFalse: [^ (self polynomialPartialDivisionBy: anArray) first].
r := self copyFrom: n - k + 1 to: n + 1.
q := (self species new: n - m + 1) atAllPut: self first zero.
r partiallyReduceBy: (anArray copyFrom: n - k + 1 to: m + 1) quotient: q.
^ q! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 6/6/2023 00:18:19'!
polynomialQuotientBy: anArray
| q r k m n |
n := self size - 1.
m := anArray size - 1.
(n > m and: [(k := n - m * 2) < n]) ifFalse: [^ (self polynomialDivisionBy: anArray) first].
r := self copyFrom: n - k + 1 to: n + 1.
q := (self species new: n - m + 1) atAllPut: self first zero.
r reduceBy: (anArray copyFrom: n - k + 1 to: m + 1) quotient: q.
^ q! !
!ArrayedCollection methodsFor: '*gas' stamp: 'len 5/23/2023 10:40:54'!
reduceBy: g
self reduceBy: g quotient: nil! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 11/7/2023 11:21:53'!
reduceBy: g leadingInverse: u quotient: q modulo: modulus
| n m minusU |
m := g size "lastNonzeroIndex".
"inv _ (g at: m) inverse."
minusU := modulus - u.
n := self size + 1.
[(n := self findLastNonzeroFrom: m to: n-1) notNil]
whileTrue:
[| i c |
i := n-m+1.
c := (self at: n) * minusU \\ modulus.
q ifNotNil: [q at: i put: (q at: i) - c \\ modulus].
self replaceFrom: i to: n with: self startingAt: i plus: g startingAt: 1 timesScalar: c modulo: modulus]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 2/2/2024 10:56:55'!
reduceBy: g quotient: q
| n m minusU |
m := g size "lastNonzeroIndex".
minusU := (g at: m) inverse negated.
n := self size + 1.
[(n := self findLastNonzeroFrom: m to: n-1) notNil]
whileTrue:
[| i c |
i := n-m+1.
c := (self at: n) * minusU.
q ifNotNil: [q at: i put: (q at: i) - c].
self replaceFrom: i to: n plus: g startingAt: 1 timesScalar: c]! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 1/1/2025 18:10:55'!
replaceFrom: start by: step blockSize: blockSize plusMatrix: anArray1 startingAt: start1 by: step1 width: width1 height: height timesMatrix: anArray2 startingAt: start2 by: step2 width: width modulo: modulus
"Blocked matrix multiplication with accumulation.
A choice of block size that minimizes cache misses should provide a considerable speedup. Although cache misses don't seem to be the bottleneck in Smalltalk, blocking still improves performance for some modulii because it can enable per-block delayed reduction."
| value s t u blockBottom blockRight |
blockSize < (16r40000000 // modulus) squared
ifTrue: "delayed reduction"
[0 to: height - 1 // blockSize * blockSize by: blockSize do: [:k₀|
blockBottom := k₀ + blockSize - 1 min: width1 - 1.
0 to: width - 1 // blockSize * blockSize by: blockSize do: [:j₀|
blockRight := j₀ + blockSize - 1 min: width - 1.
0 to: height - 1 do: [:i|
s := i*step + start.
t := i*step1 + start1.
j₀ to: blockRight do: [:j|
value := self at: s + j.
u := start2 + j.
k₀ to: blockBottom do: [:k| value := (anArray1 at: t + k) * (anArray2 at: k*step2 + u) + value].
self at: s + j put: value \\ modulus]]]]]
ifFalse:
[0 to: height - 1 // blockSize * blockSize by: blockSize do: [:k₀|
blockBottom := k₀ + blockSize - 1 min: width1 - 1.
0 to: width - 1 // blockSize * blockSize by: blockSize do: [:j₀|
blockRight := j₀ + blockSize - 1 min: width - 1.
0 to: height - 1 do: [:i|
s := i*step + start.
t := i*step1 + start1.
j₀ to: blockRight do: [:j|
value := self at: s + j.
u := start2 + j.
k₀ to: blockBottom do: [:k| value := (anArray1 at: t + k) * (anArray2 at: k*step2 + u) + value \\ modulus].
self at: s + j put: value]]]]]
"
n := 1000.
a := WordArray new: n*n.
p := 16r0FFFFFFF atRandom nextPrime.
b := a copy.
c := a copy.
[:aRandom| 1 to: a size do: [:i| a at: i put: (aRandom nextBits: 32) \\ p. b at: i put: (aRandom nextBits: 32) \\ p]] value: Random new.
c atAllPut: 0.
[c replaceFrom: 1 by: n blockSize: 25 plusMatrix: a startingAt: 1 by: n width: n height: n timesMatrix: b startingAt: 1 by: n width: n modulo: p] timeToRun
"! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 1/6/2025 09:40:48'!
replaceFrom: start by: step minusMatrix: anArray1 startingAt: start1 by: step1 width: width1 height: height timesMatrix: anArray2 startingAt: start2 by: step2 width: width
"C ← C - AB."
| i j |
i := start.
j := start1.
height timesRepeat:
[| k value |
k := start2.
i to: i + width - 1 do: [:s|
value := self at: s.
j to: j + width1 - 1 do: [:t|
| u |
u := t - j * step2 + k.
value := value - ((anArray1 at: t) * (anArray2 at: u))].
self at: s put: value.
k := k + 1].
i := i + step.
j := j + step1]! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 1/1/2025 18:14:41'!
replaceFrom: start by: step minusMatrix: anArray1 startingAt: start1 by: step1 width: width1 height: height timesMatrix: anArray2 startingAt: start2 by: step2 width: width modulo: modulus
| i j k value minusOne |
" self class isBits ifTrue: [^ self replaceFrom: start by: step blockSize: 128 // self bytesPerElement plusMatrix: anArray1 startingAt: start1 by: step1 width: width1 height: height timesMatrix: anArray2 startingAt: start2 by: step2 width: width modulo: modulus]. "
i := start.
j := start1.
modulus * width1 sqrt < 16r40000000
ifTrue: "delayed reduction"
[height timesRepeat:
[k := start2.
i to: i + width - 1 do: [:s|
value := self at: s.
j to: j + width1 - 1 do: [:t| value := value - ((anArray1 at: t) * (anArray2 at: t - j * step2 + k))].
self at: s put: value \\ modulus.
k := k + 1].
i := i + step.
j := j + step1]]
ifFalse:
[((minusOne := modulus - 1) bitAnd: modulus) = 0
ifTrue: "power of 2"
[height timesRepeat:
[k := start2.
i to: i + width - 1 do: [:s|
value := self at: s.
j to: j + width1 - 1 do: [:t| value := value - ((anArray1 at: t) * (anArray2 at: t - j * step2 + k)) bitAnd: minusOne].
self at: s put: value.
k := k + 1].
i := i + step.
j := j + step1]]
ifFalse:
[height timesRepeat:
[k := start2.
i to: i + width - 1 do: [:s|
value := self at: s.
j to: j + width1 - 1 do: [:t| value := value - ((anArray1 at: t) * (anArray2 at: t - j * step2 + k)) \\ modulus].
self at: s put: value.
k := k + 1].
i := i + step.
j := j + step1]]]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 1/6/2025 09:43:00'!
replaceFrom: start by: step plusMatrix: anArray1 startingAt: start1 by: step1 width: width height: height
"C ← C + A."
| i j |
i := start.
j := start1.
height timesRepeat:
[self replaceFrom: i to: i + width - 1 plus: anArray1 startingAt: j.
i := i + step.
j := j + step1]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 1/6/2025 09:41:02'!
replaceFrom: start by: step plusMatrix: anArray1 startingAt: start1 by: step1 width: width1 height: height timesMatrix: anArray2 startingAt: start2 by: step2 width: width
"C ← C + AB."
| i j |
i := start.
j := start1.
height timesRepeat:
[| k value |
k := start2.
i to: i + width - 1 do: [:s|
value := self at: s.
j to: j + width1 - 1 do: [:t|
| u |
u := t - j * step2 + k.
value := (anArray1 at: t) * (anArray2 at: u) + value].
self at: s put: value.
k := k + 1].
i := i + step.
j := j + step1]! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 1/27/2025 14:39:33'!
replaceFrom: start by: step plusMatrix: anArray1 startingAt: start1 by: step1 width: width1 height: height timesMatrix: anArray2 startingAt: start2 by: step2 width: width modulo: modulus
| i j k value minusOne |
self class isBits ifTrue: [^ self replaceFrom: start by: step blockSize: 128 // self class bytesPerBasicElement plusMatrix: anArray1 startingAt: start1 by: step1 width: width1 height: height timesMatrix: anArray2 startingAt: start2 by: step2 width: width modulo: modulus].
i := start.
j := start1.
modulus * width1 sqrt < 16r40000000
ifTrue: "delayed reduction"
[height timesRepeat:
[k := start2.
i to: i + width - 1 do: [:s|
value := self at: s.
j to: j + width1 - 1 do: [:t| value := (anArray1 at: t) * (anArray2 at: t - j * step2 + k) + value].
self at: s put: value \\ modulus.
k := k + 1].
i := i + step.
j := j + step1]]
ifFalse:
[((minusOne := modulus - 1) bitAnd: modulus) = 0
ifTrue: "power of 2"
[height timesRepeat:
[k := start2.
i to: i + width - 1 do: [:s|
value := self at: s.
j to: j + width1 - 1 do: [:t| value := (anArray1 at: t) * (anArray2 at: t - j * step2 + k) + value bitAnd: minusOne].
self at: s put: value.
k := k + 1].
i := i + step.
j := j + step1]]
ifFalse:
[height timesRepeat:
[k := start2.
i to: i + width - 1 do: [:s|
value := self at: s.
j to: j + width1 - 1 do: [:t| value := (anArray1 at: t) * (anArray2 at: t - j * step2 + k) + value \\ modulus].
self at: s put: value.
k := k + 1].
i := i + step.
j := j + step1]]]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 6/23/2025 15:21:49'!
replaceFrom: start by: step width: width preSolveLowerTriangularMatrix: anArray1 startingAt: start1 by: step1 width: width1 unitDiagonal: unitDiagonal
self notYetImplemented! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 6/23/2025 15:21:24'!
replaceFrom: start by: step width: width preSolveUpperTriangularMatrix: anArray1 startingAt: start1 by: step1 width: width1 unitDiagonal: unitDiagonal
self notYetImplemented! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 6/23/2025 15:20:33'!
replaceFrom: start by: step width: width solveLowerTriangularMatrix: anArray1 startingAt: start1 by: step1 width: width1 unitDiagonal: unitDiagonal
" i := start.
j := start1.
width timesRepeat:
[i :=
1 to: width do: [:j| (
anArray at: start1
i := i + step]
1 to: width do: [:i|
x := self at: start + i - 1.
1 to: i-1 do: [:j| x := (self at: start +
width timesRepeat:
[i+1 to: i+width-1 do: [:
λ := (anArray1 at: j) inverse.
self replaceFrom: i to: i + width - 1 with: self timesScalar: λ.
i := i + step
]
"! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 6/23/2025 15:20:53'!
replaceFrom: start by: step width: width solveUpperTriangularMatrix: anArray1 startingAt: start1 by: step1 width: width1 unitDiagonal: unitDiagonal
self notYetImplemented! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 2/3/2024 16:30:33'!
replaceFrom: start by: step withMatrix: anArray startingAt: start1 by: step1 width: width height: height
| i j |
i := start.
j := start1.
height timesRepeat:
[self replaceFrom: i to: i + width - 1 with: anArray startingAt: j.
i := i + step.
j := j + step1]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 1/6/2025 09:44:02'!
replaceFrom: start by: step withMatrix: anArray1 startingAt: start1 by: step1 width: width height: height plusMatrix: anArray2 startingAt: start2 by: step2 timesScalar: anElement
"C ← A + B * b."
| i j k |
i := start.
j := start1.
k := start2.
height timesRepeat:
[self replaceFrom: i to: i + width - 1 with: anArray1 startingAt: j plus: anArray2 startingAt: k timesScalar: anElement.
i := i + step.
j := j + step1.
k := k + step2]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 2/2/2024 10:27:00'!
replaceFrom: start plus: anArray1 from: start1 to: stop1 convolution: anArray2 from: start2 to: stop2
self replaceFrom: start plus: anArray1 from: start1 to: stop1 convolutionLong: anArray2 from: start2 to: stop2! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 2/3/2024 16:46:22'!
replaceFrom: start plus: anArray1 from: start1 to: stop1 convolutionKronecker: anArray2 from: start2 to: stop2 modulo: modulus
| n₁ n₂ minusOne ℓ₀ ℓ z₁ z₂ z j |
self flag: #fixme. "this seems to be slower than convolutionLong"
n₁ := stop1 - start1 + 1.
n₂ := stop2 - start2 + 1.
ℓ₀ := (minusOne := modulus - 1) highBit.
ℓ := ℓ₀ * 2 + (n₁ min: n₂) highBit.
z₁ := LargePositiveInteger new: n₁ * ℓ + 7 // 8.
j := 1.
start1 to: stop1 do: [:i| z₁ bits: ℓ₀ at: j xor: (anArray1 at: i). j := j + ℓ].
z₂ := LargePositiveInteger new: n₂ * ℓ + 7 // 8.
j := 1.
start2 to: stop2 do: [:i| z₂ bits: ℓ₀ at: j xor: (anArray2 at: i). j := j + ℓ].
z := z₁*z₂.
j := 1.
(minusOne bitAnd: modulus) = 0
ifTrue: "modulus is power of 2"
[start to: start + n₁ + n₂ - 2 do: [:i| self at: i put: ((self at: i) + (z bits: ℓ₀ at: j) bitAnd: minusOne). j := j + ℓ]]
ifFalse:
[start to: start + n₁ + n₂ - 2 do: [:i| self at: i put: (self at: i) + (z bits: ℓ at: j) \\ modulus. j := j + ℓ]]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 2/2/2024 11:00:02'!
replaceFrom: start plus: anArray1 from: start1 to: stop1 convolutionLong: anArray2 from: start2 to: stop2
start2 to: stop2 do: [:i|
(anArray2 isZeroAt: i) ifFalse:
[| k |
k := i-start2+start.
self replaceFrom: k to: k+stop1-start1
plus: anArray1 startingAt: start1
timesScalar: (anArray2 at: i)]]! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 2/2/2024 10:28:42'!
replaceFrom: start plus: anArray1 from: start1 to: stop1 convolutionLong: anArray2 from: start2 to: stop2 modulo: modulus
start2 to: stop2 do: [:i| | x |
(x := anArray2 at: i) = 0 ifFalse:
[| index |
index := i-start2+start.
self replaceFrom: index to: index+stop1-start1
with: self startingAt: index
plus: anArray1 startingAt: start1 timesScalar: x
modulo: modulus]]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 2/2/2024 10:27:48'!
replaceFrom: start plusAutoconvolution: anArray1 from: start1 to: stop1
self replaceFrom: start plusAutoconvolutionLong: anArray1 from: start1 to: stop1! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 2/2/2024 10:27:48'!
replaceFrom: start plusAutoconvolutionLong: anArray1 from: start1 to: stop1
self replaceFrom: start plus: anArray1 from: start1 to: stop1 convolutionLong: anArray1 from: start1 to: stop1! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 2/1/2024 20:12:08'!
replaceFrom: start to: stop minus: anArray1 startingAt: start1
^ self replaceFrom: start to: stop with: self startingAt: start minus: anArray1 startingAt: start1! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 2/1/2024 19:55:27'!
replaceFrom: start to: stop plus: anArray1 startingAt: start1
^ self replaceFrom: start to: stop with: self startingAt: start plus: anArray1 startingAt: start1! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 2/1/2024 19:56:11'!
replaceFrom: start to: stop plus: anArray1 startingAt: start1 timesScalar: anElement
^ self replaceFrom: start to: stop with: self startingAt: start plus: anArray1 startingAt: start1 timesScalar: anElement! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 2/3/2024 17:11:45'!
replaceFrom: start to: stop plusMatrix: anArray1 startingAt: start1 by: step1 width: width1 timesVector: anArray2 startingAt: start2
| j |
width1 = 0 ifTrue: [^ self].
j := start1.
start to: stop do: [:i|
| value |
value := self at: i.
j to: j + width1 - 1 do: [:k| value := (anArray1 at: k) * (anArray2 at: k - j + start2) + value].
self at: i put: value.
j := j + step1]! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 2/3/2024 17:11:00'!
replaceFrom: start to: stop plusMatrix: anArray1 startingAt: start1 by: step1 width: width1 timesVector: anArray2 startingAt: start2 modulo: modulus
| j value minusOne |
width1 = 0 ifTrue: [^ self].
j := start1.
modulus * width1 sqrt < 16r40000000
ifTrue: "delayed reduction"
[start to: stop do: [:i|
value := self at: i.
j to: j + width1 - 1 do: [:k| value := (anArray1 at: k) * (anArray2 at: k - j + 1) + value].
self at: i put: value \\ modulus.
j := j + step1]]
ifFalse:
[((minusOne := modulus - 1) bitAnd: modulus) = 0
ifTrue: "power of 2"
[start to: stop do: [:i|
value := self at: i.
j to: j + width1 - 1 do: [:k| value := (anArray1 at: k) * (anArray2 at: k - j + 1) + value bitAnd: minusOne].
self at: i put: value.
j := j + step1]]
ifFalse:
[start to: stop do: [:i|
value := self at: i.
j to: j + width1 - 1 do: [:k| value := (anArray1 at: k) * (anArray2 at: k - j + 1) + value \\ modulus].
self at: i put: value.
j := j + step1]]]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 4/25/2022 07:55:30'!
replaceFrom: start to: stop with: anArray1 startingAt: start1 minus: anArray2 startingAt: start2
| j1 j2 |
j1 _ start1.
j2 _ start2.
start to: stop do: [:i|
self at: i put: (anArray1 at: j1) - (anArray2 at: j2).
j1 _ j1 + 1.
j2 _ j2 + 1]! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 12/27/2022 18:02:28'!
replaceFrom: start to: stop with: anArray1 startingAt: start1 minus: anArray2 startingAt: start2 modulo: modulus
| j1 j2 |
j1 _ start1.
j2 _ start2.
start to: stop do: [:i| | x |
x _ anArray2 at: j2.
x = 0 ifTrue: [x _ anArray1 at: j1] ifFalse: [x _ (anArray1 at: j1) + modulus - x. x >= modulus ifTrue: [x _ x - modulus]].
self at: i put: x.
j1 _ j1 + 1.
j2 _ j2 + 1]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 4/25/2022 07:56:38'!
replaceFrom: start to: stop with: anArray1 startingAt: start1 plus: anArray2 startingAt: start2
| j1 j2 |
j1 _ start1.
j2 _ start2.
start to: stop do: [:i|
self at: i put: (anArray1 at: j1) + (anArray2 at: j2).
j1 _ j1 + 1.
j2 _ j2 + 1]! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 5/1/2022 12:20:44'!
replaceFrom: start to: stop with: anArray1 startingAt: start1 plus: anArray2 startingAt: start2 modulo: modulus
| j1 j2 |
j1 _ start1.
j2 _ start2.
start to: stop do: [:i| | x |
x _ (anArray1 at: j1) + (anArray2 at: j2).
x >= modulus ifTrue: [x _ x - modulus].
self at: i put: x.
j1 _ j1 + 1.
j2 _ j2 + 1]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 5/30/2023 17:40:32'!
replaceFrom: start to: stop with: anArray1 startingAt: start1 plus: anArray2 startingAt: start2 timesScalar: anElement
| j1 j2 |
(self == anArray1 and: [start = start1]) ifTrue: "this optimization makes reduction by a sparse modulus much faster"
[j2 := start2.
start to: stop do: [:i|
| x |
(x := anArray2 at: j2) isZero
ifFalse: [self at: i put: (self at: i) + (x * anElement)].
j2 := j2 + 1].
^ self].
j1 := start1.
j2 := start2.
start to: stop do: [:i|
self at: i put: (anArray1 at: j1) + ((anArray2 at: j2) * anElement).
j1 := j1 + 1.
j2 := j2 + 1].
^ self! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 5/30/2023 17:39:01'!
replaceFrom: start to: stop with: anArray1 startingAt: start1 plus: anArray2 startingAt: start2 timesScalar: anInteger modulo: modulus
| j1 j2 |
anInteger = 0 ifTrue: [^ self replaceFrom: start to: stop with: anArray1 startingAt: start1].
anInteger = 1 ifTrue: [^ self replaceFrom: start to: stop with: anArray1 startingAt: start1 plus: anArray2 startingAt: start2 modulo: modulus].
(self == anArray1 and: [start = start1]) ifTrue: "this optimization makes reduction by a sparse modulus much faster"
[j2 := start2.
start to: stop do: [:i|
| x |
(x := anArray2 at: j2) = 0
ifFalse: [self at: i put: (self at: i) + (x * anInteger) \\ modulus].
j2 := j2 + 1].
^ self].
j1 := start1.
j2 := start2.
start to: stop do: [:i|
self at: i put: (anArray1 at: j1) + ((anArray2 at: j2) * anInteger) \\ modulus.
j1 := j1 + 1.
j2 := j2 + 1]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 4/25/2022 07:58:39'!
replaceFrom: start to: stop with: anArray1 startingAt: start1 times: anArray2 startingAt: start2
| j1 j2 |
j1 _ start1.
j2 _ start2.
start to: stop do: [:i|
self at: i put: (anArray1 at: j1) * (anArray2 at: j2).
j1 _ j1 + 1.
j2 _ j2 + 1]! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 2/3/2024 16:46:42'!
replaceFrom: start to: stop with: anArray1 startingAt: start1 times: anArray2 startingAt: start2 modulo: modulus
| j1 j2 minusOne |
j1 := start1.
j2 := start2.
((minusOne := modulus - 1) bitAnd: modulus) = 0
ifTrue: "modulus is power of 2"
[start to: stop do: [:i|
self at: i put: ((anArray1 at: j1) * (anArray2 at: j2) bitAnd: minusOne).
j1 := j1 + 1.
j2 := j2 + 1]]
ifFalse:
[start to: stop do: [:i|
self at: i put: (anArray1 at: j1) * (anArray2 at: j2) \\ modulus.
j1 := j1 + 1.
j2 := j2 + 1]]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 6/11/2023 23:20:44'!
replaceFrom: start to: stop with: anArray1 startingAt: start1 timesScalar: anElement
| j |
j := start1.
start to: stop do: [:i|
self at: i put: (anArray1 at: j) * anElement.
j := j + 1]! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 2/3/2024 16:46:54'!
replaceFrom: start to: stop with: anArray startingAt: srcStart timesScalar: anInteger modulo: modulus
| j minusOne |
anInteger = 0 ifTrue: [^ self from: start to: stop put: 0].
anInteger = 1 ifTrue: [^ self replaceFrom: start to: stop with: anArray startingAt: srcStart].
j := srcStart.
((minusOne := modulus - 1) bitAnd: modulus) = 0
ifTrue: "modulus is power of 2"
[start to: stop do: [:i|
self at: i put: ((anArray at: j) * anInteger bitAnd: minusOne).
j := j + 1]]
ifFalse:
[start to: stop do: [:i|
self at: i put: (anArray at: j) * anInteger \\ modulus.
j := j + 1]]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 3/14/2025 08:07:46'!
replaceFrom: start to: stop withNegated: anArray startingAt: srcStart
| j |
j := srcStart.
start to: stop do: [:i|
self at: i put: (anArray at: j) negated.
j := j + 1]! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 5/1/2022 12:24:57'!
replaceFrom: start to: stop withNegated: anArray startingAt: srcStart modulo: modulus
| j |
j _ srcStart.
start to: stop do: [:i| | x |
x _ anArray at: j.
x = 0 ifFalse: [x _ modulus - x].
self at: i put: x.
j _ j + 1]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 6/13/2023 22:39:00'!
replaceFrom: start with: anArray1 from: start1 to: stop1 convolution: anArray2 from: start2 to: stop2
| s1 s2 k tempArray tempSize depth m |
s1 := stop1 - start1 + 1.
s2 := stop2 - start2 + 1.
"Note: this assumes commutativity. It is fine, because the convolution is only used for multiplication of polynomials, and the coefficients ring is always commutative."
s1 < s2 ifTrue: [^ self replaceFrom: start with: anArray2 from: start2 to: stop2 convolution: anArray1 from: start1 to: stop1].
k := self karatsubaThreshold.
s1 < k ifTrue: [^ self replaceFrom: start with: anArray1 from: start1 to: stop1 convolutionLong: anArray2 from: start2 to: stop2].
tempSize := 0.
depth := 0.
[m := s1 + 1 // 2.
tempSize := m * 2 - 1 + tempSize.
s1 := m.
depth := depth + 1.
s1 >= k] whileTrue.
tempArray := self species new: tempSize.
self replaceFrom: start with: anArray1 from: start1 to: stop1 convolutionKaratsuba: anArray2 from: start2 to: stop2 temporary: tempArray startingAt: 1! !
!ArrayedCollection methodsFor: '*gas-low level-karatsuba' stamp: 'len 7/6/2025 15:58:02'!
replaceFrom: start with: anArray1 from: start1 to: stop1 convolutionKaratsuba: anArray2 from: start2 to: stop2 temporary: tempArray startingAt: tempStart
| s1 s2 m m2 r1 r2 nextTempStart |
s1 := stop1 - start1 + 1.
s2 := stop2 - start2 + 1.
(s1 > 0 and: [s2 > 0]) ifFalse: [^ self].
s1 < s2 ifTrue: [^ self replaceFrom: start with: anArray2 from: start2 to: stop2 convolutionKaratsuba: anArray1 from: start1 to: stop1 temporary: tempArray startingAt: tempStart].
"basecase:"
s1 < self karatsubaThreshold ifTrue: [^ self replaceFrom: start with: anArray1 from: start1 to: stop1 convolutionLong: anArray2 from: start2 to: stop2].
m := self karatsubaSplit: s1 startingAt: start1.
m2 := m * 2.
m < s2 ifFalse: "degenerate case:"
[nextTempStart := tempStart + m + s2 - 1.
"recursively compute f_hi * g into high part of result:"
self replaceFrom: start + m with: anArray1 from: start1 + m to: stop1 convolutionKaratsuba: anArray2 from: start2 to: stop2 temporary: tempArray startingAt: nextTempStart.
"recursively compute f_lo * g into temporary space:"
tempArray replaceFrom: tempStart with: anArray1 from: start1 to: start1 + m - 1 convolutionKaratsuba: anArray2 from: start2 to: stop2 temporary: tempArray startingAt: nextTempStart.
"add temporary into result:"
self replaceFrom: start to: start + m - 1 with: tempArray startingAt: tempStart.
^ self replaceFrom: start + m to: start + m + s2 - 2 plus: tempArray startingAt: tempStart + m ].
nextTempStart := tempStart + m2 - 1.
r1 := s1 - m.
r2 := s2 - m.
"compute t1 = f_lo + f_hi:"
self replaceFrom: start to: start + r1 - 1 with: anArray1 startingAt: start1 plus: anArray1 startingAt: start1 + m.
self replaceFrom: start + r1 to: start + m - 1 with: anArray1 startingAt: start1 + r1.
"compute t2 = g_lo + g_hi:"
self replaceFrom: start + m to: start + m + r2 - 1 with: anArray2 startingAt: start2 plus: anArray2 startingAt: start2 + m.
self replaceFrom: start + m + r2 to: start + m2 - 1 with: anArray2 startingAt: start2 + r2.
"recursively compute t3 = t1 * t2:"
tempArray replaceFrom: tempStart with: self from: start to: start + m - 1 convolutionKaratsuba: self from: start + m to: start + m2 - 1 temporary: tempArray startingAt: nextTempStart.
"recursively compute f_hi * g_hi into high part of result and subtract from t3:"
self replaceFrom: start + m2 with: anArray1 from: start1 + m to: stop1 convolutionKaratsuba: anArray2 from: start2 + m to: stop2 temporary: tempArray startingAt: nextTempStart.
tempArray replaceFrom: tempStart to: tempStart + r1 + r2 - 2 minus: self startingAt: start + m2.
"recursively compute f_lo * g_lo into low part of result and subtract from t3:"
self replaceFrom: start with: anArray1 from: start1 to: start1 + m - 1 convolutionKaratsuba: anArray2 from: start2 to: start2 + m - 1 temporary: tempArray startingAt: nextTempStart.
tempArray replaceFrom: tempStart to: tempStart + m2 - 2 minus: self startingAt: start.
self at: start + m2 - 1 put: (anArray1 at: start1) zero.
"finally add t3 * x^m to result:"
self replaceFrom: start + m to: start + m + m2 - 2 plus: tempArray startingAt: tempStart! !
!ArrayedCollection methodsFor: '*gas-low level-modular' stamp: 'len 2/3/2024 16:47:04'!
replaceFrom: start with: anArray1 from: start1 to: stop1 convolutionKronecker: anArray2 from: start2 to: stop2 modulo: modulus
| n₁ n₂ minusOne ℓ₀ ℓ z₁ z₂ z j |
n₁ := stop1 - start1 + 1.
n₂ := stop2 - start2 + 1.
ℓ₀ := (minusOne := modulus - 1) highBit.
ℓ := ℓ₀ * 2 + (n₁ min: n₂) highBit.
z₁ := LargePositiveInteger new: n₁ * ℓ + 7 // 8.
j := 1.
start1 to: stop1 do: [:i| z₁ bits: ℓ₀ at: j xor: (anArray1 at: i). j := j + ℓ].
z₂ := LargePositiveInteger new: n₂ * ℓ + 7 // 8.
j := 1.
start2 to: stop2 do: [:i| z₂ bits: ℓ₀ at: j xor: (anArray2 at: i). j := j + ℓ].
z := z₁*z₂.
j := 1.
(minusOne bitAnd: modulus) = 0
ifTrue: "modulus is power of 2"
[start to: start + n₁ + n₂ - 2 do: [:i| self at: i put: (z bits: ℓ₀ at: j). j := j + ℓ]]
ifFalse:
[start to: start + n₁ + n₂ - 2 do: [:i| self at: i put: (z bits: ℓ at: j) \\ modulus. j := j + ℓ]]! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 2/2/2024 10:27:00'!
replaceFrom: start with: anArray1 from: start1 to: stop1 convolutionLong: anArray2 from: start2 to: stop2
self from: start to: start + stop1 - start1 + stop2 - start2 put: (anArray1 at: start1) zero.
self replaceFrom: start plus: anArray1 from: start1 to: stop1 convolutionLong: anArray2 from: start2 to: stop2! !
!ArrayedCollection methodsFor: '*gas-low level' stamp: 'len 12/9/2022 07:47:04'!
replaceFrom: start withAutoconvolution: anArray1 from: start1 to: stop1
| s k tempArray tempSize depth m |
s _ stop1 - start1 + 1.
k _ self karatsubaThreshold.
s < k ifTrue: [^ self replaceFrom: start withAutoconvolutionLong: anArray1 from: start1 to: stop1].
tempSize _ 0.
depth _ 0.
[m _ s + 1 // 2.
tempSize _ m * 2 - 1 + tempSize.
s _ m.
depth _ depth + 1.
s >= k] whileTrue.
tempArray _ self species new: tempSize.
self replaceFrom: start withAutoconvolutionKaratsuba: anArray1 from: start1 to: stop1 temporary: tempArray startingAt: 1! !
!ArrayedCollection methodsFor: '*gas-low level-karatsuba' stamp: 'len 7/6/2025 15:58:14'!
replaceFrom: start withAutoconvolutionKaratsuba: anArray1 from: start1 to: stop1 temporary: tempArray startingAt: tempStart
| s m m2 r nextTempStart |
s := stop1 - start1 + 1.
s > 0 ifFalse: [^ self].
"basecase:"
s < self karatsubaThreshold ifTrue: [^ self replaceFrom: start withAutoconvolutionLong: anArray1 from: start1 to: stop1].
m := self karatsubaSplit: s startingAt: start1.
m2 := m * 2.
nextTempStart := tempStart + m2 - 1.
r := s - m.
"compute t1 = f_lo + f_hi:"
self replaceFrom: start to: start + r - 1 with: anArray1 startingAt: start1 plus: anArray1 startingAt: start1 + m.
self replaceFrom: start + r to: start + m - 1 with: anArray1 startingAt: start1 + r.
self replaceFrom: start + m to: start + m2 - 1 with: self startingAt: start.
"recursively compute t2 = t1^2:"
tempArray replaceFrom: tempStart withAutoconvolutionKaratsuba: self from: start to: start + m - 1 temporary: tempArray startingAt: nextTempStart.