Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic Toom-3 multiplication for gr_poly #2071

Merged
merged 4 commits into from
Sep 18, 2024

Conversation

fredrik-johansson
Copy link
Collaborator

Should be a bit better asymptotically than Karatsuba, for rings where we don't have FFT.

This can be tidied up a bit (avoiding zero-padding, etc). Not optimized for squaring, nor for unbalanced products.

Would be interesting to see how Bodrato's sequence (used here) compares to a completely generic Toom-(n,m).

@albinahlback
Copy link
Collaborator

Interesting!

int gr_poly_mul_toom33(gr_poly_t res, const gr_poly_t poly1, const gr_poly_t poly2, gr_ctx_t ctx);

Balanced Toom-3 multiplication with interpolation in five points,
using the Bodrato evaluation scheme. Assumes commutativity and division by 3.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is meant by "division by 3"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We require that the ring supports exact division by 3, i.e. (3x)/3 = x; in other words the characteristic should be 0 or not divisible by 3.

Actually, division by 2 is required too; I should update this comment.

@albinahlback
Copy link
Collaborator

Are there any requirements on the length, i.e. minimal length, for any of the functions?

@fredrik-johansson
Copy link
Collaborator Author

Are there any requirements on the length, i.e. minimal length, for any of the functions?

No, but this algorithm is most efficient when the operands are approximately balanced. For unbalanced products one should use an unbalanced Toom rule instead.

@fredrik-johansson
Copy link
Collaborator Author

fredrik-johansson commented Sep 17, 2024

For fun, this is the theoretical speedup of Karatsuba vs classical and Toom-3 vs Karatsuba for an n x n multiply, counting arithmetic operations (in Toom, multiplication or division by 2 or 3 counts as an arithmetic operation here):

speedupk

This is when using the brute forced optimal cutoffs classical -> Karatsuba -> Toom-3 for the recursive multiplications (in particular, Karatsuba and Toom-3 are not used at all when suboptimal). Of course, counting all arithmetic operations as having unit cost isn't realistic; this just gives a rough idea.

The curves aren't quite smooth because Karatsuba performs a bit better when n is even, and Toom performs a bit better when n is divisible by 3.

Raw number of arithmetic operations:

n classical karatsuba toom3
1  1  1  1
2  5  5  5
3  13  13  13
4  25  25  25
5  41  41  41
6  61  60  60
7  85  85  85
8  113  104  104
9  145  140  140
10  181  160  160
11  221  202  202
12  265  225  225
13  313  279  279
14  365  308  308
15  421  350  350
16  481  373  373
17  545  449  449
18  613  489  489
19  685  533  533
20  761  557  557
21  841  645  645
22  925  691  691
23  1013  741  741
24  1105  768  768
25  1201  880  880
26  1301  938  938
27  1405  1000  1000
28  1513  1033  1033
29  1625  1121  1121
30  1741  1167  1167
31  1861  1217  1217
32  1985  1244  1244
33  2113  1400  1400
34  2245  1480  1480
35  2381  1564  1560
36  2521  1608  1593
37  2665  1700  1700
38  2813  1748  1748
39  2965  1800  1800
40  3121  1828  1828
41  3281  2008  2008
42  3445  2100  2088
43  3613  2196  2196
44  3785  2246  2246
45  3961  2350  2338
46  4141  2404  2404
47  4325  2462  2460
48  4513  2493  2493
49  4705  2721  2721
50  4901  2837  2827
51  5101  2957  2913
52  5305  3019  3017
53  5513  3147  3103
54  5725  3213  3153
55  5941  3283  3283
56  6161  3320  3320
57  6385  3500  3413
58  6613  3592  3485
59  6845  3688  3539
60  7081  3738  3573
61  7321  3842  3842
62  7565  3896  3896
63  7813  3954  3954
64  8065  3985  3985
65  8321  4301  4267
66  8581  4461  4323
67  8845  4625  4497
68  9113  4709  4553
69  9385  4881  4613
70  9661  4969  4691
71  9941  5061  4751
72  10225  5109  4788
73  10513  5297  5229
74  10805  5393  5266
75  11101  5493  5388
76  11401  5545  5528
77  11705  5653  5650
78  12013  5709  5709
79  12325  5769  5769
80  12641  5801  5801
81  12961  6165  6068
82  13285  6349  6158
83  13613  6537  6230
84  13945  6633  6273
85  14281  6829  6612
86  14621  6929  6655
87  14965  7033  6753
88  15313  7087  6869
89  15665  7299  6967
90  16021  7407  7023
91  16381  7519  7197
92  16745  7577  7253
93  17113  7697  7313
94  17485  7759  7391
95  17861  7825  7451
96  18241  7860  7488
97  18625  8320  8105
98  19013  8552  8142
99  19405  8788  8308
100  19801  8908  8492
101  20201  9152  8658
102  20605  9276  8748
103  21013  9404  9008
104  21425  9470  9098
105  21841  9730  9188
106  22261  9862  9260
107  22685  9998  9350
108  23113  10068  9393
109  23545  10212  9808
110  23981  10286  9851
111  24421  10364  9968
112  24865  10405  10073
113  25313  10769  10190
114  25765  10953  10248
115  26221  11141  10428
116  26681  11237  10486
117  27145  11433  10548
118  27613  11533  10628
119  28085  11637  10690
120  28561  11691  10728
121  29041  11903  11440
122  29525  12011  11478
123  30013  12123  11668
124  30505  12181  11828
125  31001  12301  12018
126  31501  12363  12108
127  32005  12429  12429
128  32513  12464  12464
129  33025  13100  12688
130  33541  13420  12800
131  34061  13744  12918
132  34585  13908  12978
133  35113  14240  13316
134  35645  14408  13376
135  36181  14580  13478
136  36721  14668  13670
137  37265  15016  13772
138  37813  15192  13848
139  38365  15372  14026
140  38921  15464  14102
141  39481  15652  14168
142  40045  15748  14264
143  40613  15848  14330
144  41185  15900  14373
145  41761  16280  15272
146  42341  16472  15315
147  42925  16668  15553
148  43513  16768  15769
149  44105  16972  16007
150  44701  17076  16123
151  45301  17184  16381
152  45905  17240  16497
153  46513  17460  16593
154  47125  17572  16943
155  47741  17688  17039
156  48361  17748  17153
157  48985  17872  17413
158  49613  17936  17527
159  50245  18004  17623
160  50881  18040  17757
161  51521  18772  17853
162  52165  19140  17913
163  52813  19512  18403
164  53465  19700  18463
165  54121  20080  18603
166  54781  20272  18641
167  55445  20468  18781
168  56113  20568  18828
169  56785  20964  19183
170  57461  21164  19230
171  58141  21368  19333
172  58825  21472  19548
173  59513  21684  19651
174  60205  21792  19733
175  60901  21904  19897
176  61601  21962  19979
177  62305  22390  20043
178  63013  22606  20145
179  63725  22826  20209
180  64441  22938  20253
181  65161  23166  21315
182  65885  23282  21359
183  66613  23402  21638
184  67345  23464  21605
185  68081  23708  21884
186  68821  23832  21948
187  69565  23960  22146
188  70313  24026  22210
189  71065  24162  22278
190  71821  24232  22364
191  72581  24306  22432
192  73345  24345  22473
193  74113  25269  23590
194  74885  25733  23631
195  75661  26201  23923
196  76441  26437  23885
197  77225  26913  24177
198  78013  27153  24243
199  78805  27397  24903
200  79601  27521  24969
202  81205  28261  25223
204  82825  28641  25473
206  84461  29033  25743
208  86113  29239  26085
210  87781  30027  26243
212  89465  30431  26513
214  91165  30847  26691
216  92881  31065  26808
218  94613  31505  28602
220  96361  31735  28780
222  98125  31977  29278
224  99905  32108  29796
226  101701  33208  30386
228  103513  33768  30668
230  105341  34340  31186
232  107185  34636  31452
234  109045  35232  31653
236  110921  35540  31923
238  112813  35860  32081
240  114721  36030  32193
242  116645  36674  33291
244  118585  37006  33681
246  120541  37350  34058
248  122513  37532  34376
250  124501  37900  34578
252  126505  38094  34713
254  128525  38300  36099
256  130561  38413  36301
258  132613  40329  36703
260  134681  41297  37125
262  136765  42277  37619
264  138865  42777  37853
266  140981  43781  38275
268  143113  44293  38529
270  145261  44817  38703
272  147425  45089  39429
274  149605  46141  39683
276  151801  46677  39933
278  154013  47225  40203
280  156241  47509  40545
282  158485  48081  40703
284  160745  48377  40973
286  163021  48685  41151
288  165313  48849  41268
290  167621  49997  43766
292  169945  50581  43944
294  172285  51177  44618
296  174641  51485  45312
298  177013  52105  46078
300  179401  52425  46448
303  183013  52873  47318
306  186661  53601  47808
309  190345  54181  49148
312  194065  54489  49638
315  197821  55001  50128
318  201613  55281  50528
321  205441  56865  51018
324  209305  58713  51273
327  213205  60217  53388
330  217141  61557  53643
333  221113  62537  54268
336  225121  63045  54833
339  229165  64645  55458
342  233245  65469  55788
345  237361  66217  56728
348  241513  66765  57058
351  245701  67229  57408
354  249925  68583  57848
357  254185  69683  58198
360  258481  70251  58428
363  262813  71179  62028
366  267181  71667  62258
369  271585  72353  63248
372  276025  72981  64088
375  280501  73509  65078
378  285013  73995  65568
381  289561  74365  67213
384  294145  74568  67428
387  298765  78280  68588
390  303421  80160  69188
393  308113  81832  69818
396  312841  83040  70158
399  317605  84032  71888
402  322405  85644  72228
406  328861  87160  73464
410  335381  88340  74216
414  341965  89160  74748
418  348613  90958  75824
422  355325  92570  76392
426  362101  93822  76988
430  368941  94690  77484
434  375845  95804  81239
438  382813  96720  82403
442  389845  97450  84279
446  396941  97970  85735
450  404101  100317  86603
454  411325  102553  88159
458  418613  104273  88967
462  425965  105453  90863
466  433381  106957  91763
470  440861  108185  93063
474  448405  109149  93943
478  456013  109825  94923
482  463685  111299  95587
486  471421  112623  96033
490  479221  113659  98293
494  487085  114383  99453
498  495013  115317  99833
502  503005  116089  100641
507  513085  116855  102663
512  523265  117284  103380
517  533545  124340  104825
522  543925  127932  105613
527  554405  130268  106831
532  564985  133468  107647
537  575665  135720  108193
542  586445  137156  112731
547  597325  140256  115155
552  608305  142236  115373
557  619385  144276  117094
562  630565  145912  118276
567  641845  147296  118938
572  653225  148340  119710
577  664705  150380  124480
582  676285  153480  125903
587  687965  155676  127281
592  699745  156820  128745
597  711625  159124  132463
602  723605  160340  133599
608  738113  161228  134949
614  752765  163940  136359
620  767561  165380  137253
626  782501  166724  139043
632  797585  167732  140361
638  812813  168620  141343
644  828185  175376  142313
650  843701  180980  149686
656  859361  183848  153552
662  875165  188480  154522
668  891113  190880  157172
674  907205  194216  158486
680  923441  197264  161516
686  939821  199784  163950
692  956345  202064  165676
698  973013  203768  167086
704  989825  204686  168447
711  1009621  210338  169433
718  1029613  213274  170421
725  1049801  216198  176071
732  1070185  217926  178153
739  1090765  220508  180910
746  1111541  222704  182146
753  1132513  224298  183328
760  1153681  225676  189237
767  1175045  226726  191870
774  1196605  237933  193823
781  1218361  244553  196412
788  1240313  250085  199025
795  1262461  254269  201333
802  1284805  258649  203079
810  1310581  263949  204303
818  1336613  267497  208779
826  1362901  270361  210423
834  1389445  274623  212123
842  1416245  280259  214223
850  1443301  284023  215923
858  1470613  287055  217183
866  1498181  289985  227682
874  1526005  293185  231190
882  1554085  295383  234838
890  1582421  297197  239122
898  1611013  302332  243838
906  1639861  310152  247778
915  1672621  316092  250728
924  1705705  320052  256698
933  1739113  325408  260118
942  1772845  328956  262688
951  1806901  332132  264898
960  1841281  333858  267378
969  1875985  340626  269008
978  1911013  344190  277843
987  1946365  346970  281098
996  1982041  349932  283973
1005  2018041  352556  286978
1015  2058421  354770  290576
1025  2099201  361061  292530
1035  2140381  378453  297428
1045  2181961  389293  299990
1055  2223941  395365  302210
1065  2266321  406029  304368
1075  2309101  412161  306046
1085  2352281  416181  320940
1095  2395861  425857  325638
1105  2439841  432589  330720
1116  2488681  437865  335308
1127  2538005  443797  341168
1138  2587813  447985  348264
1149  2638105  450841  352223
1160  2688881  461561  358164
1171  2740141  470097  362946
1182  2791885  474861  365548
1193  2844113  481713  373528
1204  2896825  485833  376770
1216  2954881  488545  382730
1228  3013513  496729  387010
1240  3072721  501097  390070
1252  3132505  505177  395474
1264  3192865  508249  398514
1276  3253801  510961  401766
1288  3315313  531277  404386
1300  3377401  548137  419897
1313  3445313  559857  429460
1326  3513901  571929  439063
1339  3583165  579645  447289
1352  3653105  591677  454777
1365  3723721  601821  460323
1378  3795013  608437  468923
1391  3866981  614673  474563
1404  3939625  619089  478673
1418  4018613  634031  487723
1432  4098385  643471  493137
1446  4178941  652239  497203
1460  4260281  658475  507303
1474  4342405  664429  513393
1488  4425313  671697  518003
1502  4509005  677009  522733
1517  4599545  682707  531651
1532  4690985  686033  536648
1547  4783325  719048  543433
1562  4876565  739904  548310
1577  4970705  758504  554119
1592  5065745  769928  557559
1607  5161685  786360  561901
1623  5265013  800404  585013
1639  5369365  811176  596477
1655  5474741  818840  598946
1671  5581141  834794  607738
1687  5688565  849718  614622
1703  5797013  861138  618572
1720  5913361  869398  622254
1737  6030865  880796  651438
1754  6149525  888968  660091
1771  6269341  896094  667099
1788  6390313  900444  682793
1805  6512441  929041  691977
1823  6643013  946153  698963
1841  6774881  963697  706257
1859  6908045  976797  710967
1877  7042505  991385  719951
1895  7178261  1001065  726965
1913  7315313  1008025  732115
1932  7461385  1024923  737313
1951  7608901  1036087  781409
1970  7757861  1047287  795862
1989  7908265  1056493  801423
2008  8060113  1064869  813500
2028  8221513  1071963  828768
2048  8384513  1076024  838596
2068  8549113  1139728  852620
2088  8715313  1172256  860468
2108  8883113  1193480  869183
2129  9061025  1225228  875064
2150  9240701  1245080  880093
2171  9422141  1257608  904831
2192  9605345  1287416  919081
2213  9790313  1310304  929978
2235  9985981  1324120  939968
2257  10183585  1342608  946506
2279  10383125  1354752  977664
2301  10584601  1362332  990018
2324  10797305  1401020  1005865
2347  11012125  1424140  1014072
2370  11229061  1440036  1029383
2393  11448113  1458860  1041777
2416  11669281  1470304  1050821
2440  11902321  1487500  1068569
2464  12137665  1503292  1079389
2488  12375313  1516708  1090017
2512  12615265  1529152  1100125
2537  12867665  1539392  1107635
2562  13122565  1561980  1116763
2587  13379965  1630280  1123225
2612  13639865  1672028  1196832
2638  13912813  1710976  1205670
2664  14188465  1739076  1227448
2690  14466821  1762844  1251762
2716  14747881  1800652  1274384
2743  15042613  1825260  1289798
2770  15340261  1848928  1319328
2797  15640825  1864476  1337468
2824  15944305  1893694  1350678
2852  16262105  1931594  1362398
2880  16583041  1951458  1375278
2908  16907113  1979206  1384226
2937  17246065  1994766  1436698
2966  17588381  2022596  1445803
2995  17934061  2040120  1462968
3024  18283105  2055282  1478273
3054  18647725  2067720  1497063
3084  19015945  2138601  1517938
3114  19387765  2216373  1530648
3145  19775761  2273029  1544576
3176  20167601  2315669  1556746
3207  20563285  2358909  1568128
3239  20975765  2407409  1575760
3271  21392341  2439645  1658328
3303  21813013  2465989  1692368
3336  22251121  2504955  1715508
3369  22693585  2558203  1744848
3402  23140405  2593599  1760088
3436  23605321  2620543  1798258
3470  24074861  2651717  1831082
3504  24549025  2677977  1852468
3539  25041965  2701047  1873008
3574  25539805  2715319  1908368
3609  26042545  2800056  1932198
3645  26564761  2852640  1948978
3681  27092161  2904216  1981158
3717  27624745  2943652  1998638
3754  28177525  2989168  2026804
3791  28735781  3018804  2042952
3828  29299513  3040128  2059858
3866  29884181  3092906  2073812
3904  30474625  3124246  2166117
3943  31086613  3159634  2214852
3982  31704685  3188440  2252491
4021  32328841  3213764  2292029
4061  32975321  3233842  2331105
4101  33628201  3300789  2358953
4142  34304045  3451397  2403563
4183  34986613  3550769  2432797
4224  35675905  3602097  2471373
4266  36388981  3707253  2500183
4308  37109113  3762165  2528313
4351  37853701  3793421  2549617
4394  38605685  3903629  2614493
4437  39365065  3962529  2642263
4481  40149761  3999849  2655045
4525  40942201  4056429  2680405
4570  41760661  4090357  2725130
4615  42587221  4146381  2751203
4661  43440521  4241201  2791693
4707  44302285  4304753  2822463
4754  45191525  4363061  2844809
4801  46089601  4405885  2865485
4849  47015905  4441173  2882137
4897  47951425  4504389  3019005
4945  48896161  4551201  3056982
4994  49870085  4581869  3097419
5043  50853613  4621065  3132808
5093  51867113  4646537  3152454
5143  52890613  4811381  3174714
5194  53944885  4973833  3275239
5245  55009561  5062541  3361500
5297  56105825  5192741  3394171
5349  57212905  5264425  3469723
5402  58352405  5372993  3523599
5456  59524961  5463341  3559189
5510  60709181  5528429  3595719
5565  61927321  5588177  3625703
5620  63157561  5630329  3668561
5676  64422601  5770983  3705033
5732  65700185  5856863  3734661
5789  67013465  5933275  3761793
5846  68339741  5990831  3942722
5904  69702625  6052353  4048648
5963  71102813  6123369  4084511
6022  72516925  6168763  4148810
6082  73969285  6212473  4223046
6142  75436045  6239395  4274270
6203  76942013  6574948  4345516
6265  78487921  6749916  4391842
6327  80049205  6924688  4434088
6390  81651421  7020312  4460953
6453  83269513  7198384  4486943
6517  84929545  7298540  4628219
6582  86632285  7397592  4685763
6647  88351925  7453396  4754978
6713  90115313  7645386  4792168
6780  91923241  7750854  4826768
6847  93749125  7856530  5007666
6915  95620621  7931984  5071098
6984  97538545  8040228  5131473
7053  99475513  8107678  5194663
7123  101460013  8162568  5247329
7194  103492885  8348349  5315063
7265  105545921  8548893  5358947
7337  107648465  8708529  5457799
7410  109801381  8801445  5512823
7484  112005545  8954057  5561269
7558  114231613  9054985  5616151
7633  116510113  9136265  5662485
7709  118841945  9263511  5699901
7786  121228021  9390463  5919470
7863  123637813  9487179  6071283
7941  126103081  9560377  6204193
8020  128624761  9657817  6302172
8100  131203801  9721923  6393628
8181  133841161  9764205  6508258
8262  136504765  10297320  6575388
8344  139227985  10615840  6751138
8427  142011805  10821756  6837088
8511  144857221  11087072  6899098
8596  147765241  11285260  6964190
8681  150702161  11403332  7016640
8767  153703045  11674040  7235010
8854  156768925  11885728  7287871
8942  159900845  12007712  7404883
9031  163099861  12180172  7489096
9121  166367041  12289352  7577218
9212  169703465  12355436  7636361
9304  173110225  12723556  7760678
9397  176588425  12924044  7826330
9490  180101221  13087804  7891900
9584  183686945  13235720  7949710
9679  187346725  13348748  7997178
9775  191081701  13521752  8417958
9872  194893025  13667252  8496716
9970  198781861  13762816  8669738

@fredrik-johansson fredrik-johansson merged commit 9500186 into flintlib:main Sep 18, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants