Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/tests/benchmark.py
4045 views
1
"""
2
Benchmarks
3
4
5
COMMENTS:
6
7
Taken as a whole these benchmarks suggest that by far the fastest math
8
software is MAGMA, Mathematica, and Sage (with appropriate tuning and
9
choices -- it can also be very slow !). Maxima is very slow, at least
10
in the form that comes with Sage (perhaps this is because of using
11
clisp, at least partly). GP is slow at some thing and very fast at
12
others.
13
14
TESTS:
15
sage: import sage.tests.benchmark
16
17
"""
18
19
from sage.all import * # QQ, alarm, ModularSymbols, gp, pari, cputime, EllipticCurve
20
import sage.libs.linbox.linbox as linbox
21
22
def avg(X):
23
"""
24
Return the average of the list X.
25
26
EXAMPLE:
27
sage: from sage.tests.benchmark import avg
28
sage: avg([1,2,3])
29
2.0
30
31
"""
32
s = sum(X,0)
33
return s/float(len(X))
34
35
36
STD_SYSTEMS = ['sage', 'maxima', 'gap', 'gp', 'pari', 'python']
37
OPT_SYSTEMS = ['magma', 'macaulay2', 'maple', 'mathematica']
38
39
class Benchmark:
40
"""
41
A class for running specific benchmarks against different systems.
42
43
In order to implement an extension of this class, one must write
44
functions named after the different systems one wants to test. These
45
functions must perform the same task for each function. Calling
46
the run command with a list of systems will then show the timings.
47
48
EXAMPLE:
49
sage: from sage.tests.benchmark import Benchmark
50
sage: B = Benchmark()
51
sage: def python():
52
... t = cputime()
53
... n = 2+2
54
... return cputime(t)
55
sage: B.python = python
56
sage: B.run(systems=['python'])
57
sage.tests.benchmark.Benchmark instance
58
System min avg max trials cpu or wall
59
* python ...
60
61
"""
62
def run(self, systems=None, timeout=60, trials=1, sort=False, optional=False):
63
"""
64
Run the benchmarking functions for the current benchmark on the systems
65
given.
66
67
INPUT:
68
systems -- list of strings of which systems to run tests on
69
if None, runs the standard systems
70
timeout -- how long (in seconds) to run each test for
71
trials -- integer, number of trials
72
sort -- whether to sort system names
73
optional -- if systems is None, whether to test optional systems
74
75
EXAMPLES:
76
sage: from sage.tests.benchmark import PolyFactor
77
sage: PolyFactor(10, QQ).run()
78
Factor a product of 2 polynomials of degree 10 over Rational Field.
79
System min avg max trials cpu or wall
80
* sage ...
81
* gp ...
82
83
"""
84
if sort:
85
systems.sort()
86
print '\n\n\n' + str(self)
87
#print "Timeout: %s seconds"%timeout
88
print ' %-12s%-12s%-12s%-12s%-12s%15s'%('System', 'min',
89
'avg', 'max', 'trials', 'cpu or wall')
90
if systems is None:
91
systems = STD_SYSTEMS
92
if optional:
93
systems += OPT_SYSTEMS
94
for S in systems:
95
try:
96
X = []
97
wall = False
98
for i in range(trials):
99
alarm(timeout)
100
t = getattr(self, S)()
101
alarm(0)
102
if isinstance(t, tuple):
103
wall = True
104
t = t[1]
105
X.append(t)
106
mn = min(X)
107
mx = max(X)
108
av = avg(X)
109
s = '* %-12s%-12f%-12f%-12f%-12s'%(S, mn, av,
110
mx, trials)
111
if wall:
112
s += '%15fw'%t
113
else:
114
s += '%15fc'%t
115
print s
116
except KeyboardInterrupt:
117
print '%-12sinterrupted (timeout: %s seconds wall time)'%(
118
S, timeout)
119
except AttributeError:
120
pass
121
except Exception, msg:
122
print msg
123
124
bench = run
125
126
def __repr__(self):
127
"""
128
Print representation of self, simply coming from self.repr_str.
129
130
EXAMPLE:
131
sage: from sage.tests.benchmark import Benchmark
132
sage: B = Benchmark()
133
sage: B.repr_str = 'spam'
134
sage: B
135
spam
136
"""
137
try:
138
return self.repr_str
139
except:
140
return 'sage.tests.benchmark.Benchmark instance'
141
142
class Divpoly(Benchmark):
143
def __init__(self, n):
144
"""
145
Class for benchmarking computation of the division polynomial
146
of the following elliptic curve.
147
148
EXAMPLES:
149
sage: E = EllipticCurve([1,2,3,4,5])
150
sage: E.division_polynomial(3)
151
3*x^4 + 9*x^3 + 33*x^2 + 87*x + 35
152
sage: E.division_polynomial(5)
153
5*x^12 + 45*x^11 + 422*x^10 + ... - 426371
154
155
sage: from sage.tests.benchmark import Divpoly
156
sage: B = Divpoly(99)
157
sage: B
158
99-Division polynomial
159
"""
160
self.__n = n
161
self.repr_str = "%s-Division polynomial"%self.__n
162
163
def sage(self):
164
"""
165
Time the computation in Sage.
166
167
EXAMPLE:
168
sage: from sage.tests.benchmark import Divpoly
169
sage: B = Divpoly(3)
170
sage: isinstance(B.sage(), float)
171
True
172
173
"""
174
n = self.__n
175
t = cputime()
176
E = EllipticCurve([1,2,3,4,5])
177
f = E.division_polynomial(n)
178
return cputime(t)
179
180
def magma(self):
181
"""
182
Time the computation in Magma.
183
184
EXAMPLE:
185
sage: from sage.tests.benchmark import Divpoly
186
sage: B = Divpoly(3)
187
sage: isinstance(B.magma(), float) # optional
188
True
189
190
"""
191
n = self.__n
192
t = magma.cputime()
193
m = magma('DivisionPolynomial(EllipticCurve([1,2,3,4,5]), %s)'%n)
194
return magma.cputime(t)
195
196
class PolySquare(Benchmark):
197
def __init__(self, n, R):
198
self.__n = n
199
self.__R = R
200
self.repr_str = 'Square a polynomial of degree %s over %s'%(self.__n, self.__R)
201
202
def sage(self):
203
"""
204
Time the computation in Sage.
205
206
EXAMPLE:
207
sage: from sage.tests.benchmark import PolySquare
208
sage: B = PolySquare(3, QQ)
209
sage: isinstance(B.sage(), float)
210
True
211
212
"""
213
R = self.__R
214
n = self.__n
215
f = R['x'](range(1,n+1))
216
t = cputime()
217
g = f**2
218
return cputime(t)
219
220
def magma(self):
221
"""
222
Time the computation in Magma.
223
224
EXAMPLE:
225
sage: from sage.tests.benchmark import PolySquare
226
sage: B = PolySquare(3, QQ)
227
sage: isinstance(B.magma(), float) # optional
228
True
229
230
"""
231
R = magma(self.__R)
232
f = magma('PolynomialRing(%s)![1..%s]'%(R.name(),self.__n))
233
t = magma.cputime()
234
g = f*f
235
return magma.cputime(t)
236
237
def maple(self):
238
"""
239
Time the computation in Maple.
240
241
EXAMPLE:
242
sage: from sage.tests.benchmark import PolySquare
243
sage: B = PolySquare(3, QQ)
244
sage: isinstance(B.maple()[1], float) # optional
245
True
246
247
"""
248
R = self.__R
249
if not (R == ZZ or R == QQ):
250
raise NotImplementedError
251
n = self.__n
252
f = maple(str(R['x'](range(1,n+1))))
253
t = walltime()
254
g = f*f
255
return False, walltime(t)
256
257
class MPolynomialPower(Benchmark):
258
def __init__(self, nvars=2, exp=10, base=QQ, allow_singular=True):
259
self.nvars = nvars
260
self.exp = exp
261
self.base = base
262
self.allow_singular = allow_singular
263
s = 'Compute (x_0 + ... + x_%s)^%s over %s'%(
264
self.nvars - 1, self.exp, self.base)
265
if self.allow_singular:
266
s += ' (use singular for Sage mult.)'
267
self.repr_str = s
268
269
def sage(self):
270
"""
271
Time the computation in Sage.
272
273
EXAMPLE:
274
sage: from sage.tests.benchmark import MPolynomialPower
275
sage: B = MPolynomialPower()
276
sage: isinstance(B.sage()[1], float)
277
True
278
279
"""
280
R = PolynomialRing(self.base, self.nvars, 'x')
281
z = sum(R.gens())
282
if self.allow_singular:
283
z = singular(sum(R.gens()))
284
t = walltime()
285
w = z**self.exp
286
return False, walltime(t)
287
t = cputime()
288
w = z**self.exp
289
return cputime(t)
290
291
def macaulay2(self):
292
"""
293
Time the computation in Macaulay2.
294
295
EXAMPLE:
296
sage: from sage.tests.benchmark import MPolynomialPower
297
sage: B = MPolynomialPower()
298
sage: isinstance(B.macaulay2()[1], float) # optional
299
True
300
301
"""
302
R = PolynomialRing(self.base, self.nvars, 'x')
303
z = macaulay2(sum(R.gens()))
304
t = walltime()
305
w = z**self.exp
306
return False, walltime(t)
307
308
def maxima(self):
309
"""
310
Time the computation in Maxima.
311
312
EXAMPLE:
313
sage: from sage.tests.benchmark import MPolynomialPower
314
sage: B = MPolynomialPower()
315
sage: isinstance(B.maxima()[1], float)
316
True
317
318
"""
319
R = PolynomialRing(self.base, self.nvars, 'x')
320
z = maxima(str(sum(R.gens())))
321
w = walltime()
322
f = (z**self.exp).expand()
323
return False, walltime(w)
324
325
def maple(self):
326
"""
327
Time the computation in Maple.
328
329
EXAMPLE:
330
sage: from sage.tests.benchmark import MPolynomialPower
331
sage: B = MPolynomialPower()
332
sage: isinstance(B.maple()[1], float) #optional -- requires Maple
333
True
334
335
"""
336
R = PolynomialRing(self.base, self.nvars, 'x')
337
z = maple(str(sum(R.gens())))
338
w = walltime()
339
f = (z**self.exp).expand()
340
return False, walltime(w)
341
342
def mathematica(self):
343
"""
344
Time the computation in Mathematica.
345
346
EXAMPLE:
347
sage: from sage.tests.benchmark import MPolynomialPower
348
sage: B = MPolynomialPower()
349
sage: isinstance(B.mathematica()[1], float) # optional
350
True
351
352
"""
353
R = PolynomialRing(self.base, self.nvars, 'x')
354
z = mathematica(str(sum(R.gens())))
355
w = walltime()
356
f = (z**self.exp).Expand()
357
return False, walltime(w)
358
359
## this doesn't really expand out -- pari has no function to do so,
360
## as far as I know.
361
## def gp(self):
362
## R = PolynomialRing(self.base, self.nvars)
363
## z = gp(str(sum(R.gens())))
364
## gp.eval('gettime')
365
## f = z**self.exp
366
## return float(gp.eval('gettime/1000.0'))
367
368
369
def magma(self):
370
"""
371
Time the computation in Magma.
372
373
EXAMPLE:
374
sage: from sage.tests.benchmark import MPolynomialPower
375
sage: B = MPolynomialPower()
376
sage: isinstance(B.magma(), float) # optional
377
True
378
379
"""
380
R = magma.PolynomialRing(self.base, self.nvars)
381
z = R.gen(1)
382
for i in range(2,self.nvars+1):
383
z += R.gen(i)
384
t = magma.cputime()
385
w = z**magma(self.exp)
386
return magma.cputime(t)
387
388
389
390
class MPolynomialMult(Benchmark):
391
def __init__(self, nvars=2, base=QQ, allow_singular=True):
392
if nvars%2:
393
nvars += 1
394
self.nvars = nvars
395
self.base = base
396
self.allow_singular = allow_singular
397
s = 'Compute (x_0 + ... + x_%s) * (x_%s + ... + x_%s) over %s'%(
398
self.nvars/2 - 1, self.nvars/2, self.nvars, self.base)
399
if self.allow_singular:
400
s += ' (use singular for Sage mult.)'
401
self.repr_str = s
402
403
def maxima(self):
404
"""
405
Time the computation in Maxima.
406
407
EXAMPLE:
408
sage: from sage.tests.benchmark import MPolynomialMult
409
sage: B = MPolynomialMult()
410
sage: isinstance(B.maxima()[1], float)
411
True
412
413
"""
414
R = PolynomialRing(self.base, self.nvars, 'x')
415
k = int(self.nvars/2)
416
z0 = maxima(str(sum(R.gens()[:k])))
417
z1 = maxima(str(sum(R.gens()[k:])))
418
w = walltime()
419
f = (z0*z1).expand()
420
return False, walltime(w)
421
422
def maple(self):
423
"""
424
Time the computation in Maple.
425
426
EXAMPLE:
427
sage: from sage.tests.benchmark import MPolynomialMult
428
sage: B = MPolynomialMult()
429
sage: isinstance(B.maple()[1], float) #optional -- requires Maple
430
True
431
432
"""
433
R = PolynomialRing(self.base, self.nvars, 'x')
434
k = int(self.nvars/2)
435
z0 = maple(str(sum(R.gens()[:k])))
436
z1 = maple(str(sum(R.gens()[k:])))
437
w = walltime()
438
f = (z0*z1).expand()
439
return False, walltime(w)
440
441
def mathematica(self):
442
"""
443
Time the computation in Mathematica.
444
445
EXAMPLE:
446
sage: from sage.tests.benchmark import MPolynomialMult
447
sage: B = MPolynomialMult()
448
sage: isinstance(B.mathematica()[1], float) # optional
449
True
450
451
"""
452
R = PolynomialRing(self.base, self.nvars, 'x')
453
k = int(self.nvars/2)
454
z0 = mathematica(str(sum(R.gens()[:k])))
455
z1 = mathematica(str(sum(R.gens()[k:])))
456
w = walltime()
457
f = (z0*z1).Expand()
458
return False, walltime(w)
459
460
## def gp(self):
461
## R = PolynomialRing(self.base, self.nvars)
462
## k = int(self.nvars/2)
463
## z0 = gp(str(sum(R.gens()[:k])))
464
## z1 = gp(str(sum(R.gens()[k:])))
465
## gp.eval('gettime')
466
## f = z0*z1
467
## return float(gp.eval('gettime/1000.0'))
468
469
def sage(self):
470
"""
471
Time the computation in Sage.
472
473
EXAMPLE:
474
sage: from sage.tests.benchmark import MPolynomialMult
475
sage: B = MPolynomialMult()
476
sage: isinstance(B.sage()[1], float)
477
True
478
479
"""
480
R = PolynomialRing(self.base, self.nvars, 'x')
481
k = int(self.nvars/2)
482
z0 = sum(R.gens()[:k])
483
z1 = sum(R.gens()[k:])
484
if self.allow_singular:
485
z0 = singular(z0)
486
z1 = singular(z1)
487
t = walltime()
488
w = z0*z1
489
return False, walltime(t)
490
else:
491
t = cputime()
492
w = z0 * z1
493
return cputime(t)
494
495
def macaulay2(self):
496
"""
497
Time the computation in Macaulay2.
498
499
EXAMPLE:
500
sage: from sage.tests.benchmark import MPolynomialMult
501
sage: B = MPolynomialMult()
502
sage: isinstance(B.macaulay2()[1], float) # optional
503
True
504
505
"""
506
R = PolynomialRing(self.base, self.nvars, 'x')
507
k = int(self.nvars/2)
508
z0 = macaulay2(sum(R.gens()[:k]))
509
z1 = macaulay2(sum(R.gens()[k:]))
510
t = walltime()
511
w = z0*z1
512
return False, walltime(t)
513
514
def magma(self):
515
"""
516
Time the computation in Magma.
517
518
EXAMPLE:
519
sage: from sage.tests.benchmark import MPolynomialMult
520
sage: B = MPolynomialMult()
521
sage: isinstance(B.magma(), float) # optional
522
True
523
524
"""
525
R = magma.PolynomialRing(self.base, self.nvars)
526
z0 = R.gen(1)
527
k = int(self.nvars/2)
528
for i in range(2,k+1):
529
z0 += R.gen(i)
530
z1 = R.gen(k + 1)
531
for i in range(k+1, self.nvars + 1):
532
z1 += R.gen(i)
533
t = magma.cputime()
534
w = z0 * z1
535
return magma.cputime(t)
536
537
class MPolynomialMult2(Benchmark):
538
def __init__(self, nvars=2, base=QQ, allow_singular=True):
539
if nvars%2:
540
nvars += 1
541
self.nvars = nvars
542
self.base = base
543
self.allow_singular = allow_singular
544
s = 'Compute (x_1 + 2*x_2 + 3*x_3 + ... + %s*x_%s) * (%s * x_%s + ... + %s*x_%s) over %s'%(
545
self.nvars/2, self.nvars/2, self.nvars/2+1, self.nvars/2+1,
546
self.nvars+1, self.nvars+1, self.base)
547
if self.allow_singular:
548
s += ' (use singular for Sage mult.)'
549
self.repr_str = s
550
551
## def gp(self):
552
## R = PolynomialRing(self.base, self.nvars)
553
## k = int(self.nvars/2)
554
## z0 = R(0)
555
## z1 = R(0)
556
## for i in range(k):
557
## z0 += (i+1)*R.gen(i)
558
## for i in range(k,self.nvars):
559
## z1 += (i+1)*R.gen(i)
560
## z0 = gp(str(z0))
561
## z1 = gp(str(z1))
562
## gp.eval('gettime')
563
## f = z0*z1
564
## print f
565
## return float(gp.eval('gettime/1000.0'))
566
567
def maxima(self):
568
"""
569
Time the computation in Maxima.
570
571
EXAMPLE:
572
sage: from sage.tests.benchmark import MPolynomialMult2
573
sage: B = MPolynomialMult2()
574
sage: isinstance(B.maxima()[1], float)
575
True
576
577
"""
578
R = PolynomialRing(self.base, self.nvars, 'x')
579
k = int(self.nvars/2)
580
z0 = R(0)
581
z1 = R(0)
582
for i in range(k):
583
z0 += (i+1)*R.gen(i)
584
for i in range(k,self.nvars):
585
z1 += (i+1)*R.gen(i)
586
z0 = maxima(str(z0))
587
z1 = maxima(str(z1))
588
w = walltime()
589
f = (z0*z1).expand()
590
return False, walltime(w)
591
592
def macaulay2(self):
593
"""
594
Time the computation in Macaulay2.
595
596
EXAMPLE:
597
sage: from sage.tests.benchmark import MPolynomialMult2
598
sage: B = MPolynomialMult2()
599
sage: isinstance(B.macaulay2()[1], float) # optional
600
True
601
602
"""
603
R = PolynomialRing(self.base, self.nvars, 'x')
604
k = int(self.nvars/2)
605
z0 = R(0)
606
z1 = R(0)
607
for i in range(k):
608
z0 += (i+1)*R.gen(i)
609
for i in range(k,self.nvars):
610
z1 += (i+1)*R.gen(i)
611
z0 = macaulay2(z0)
612
z1 = macaulay2(z1)
613
t = walltime()
614
w = z0*z1
615
return False, walltime(t)
616
617
def maple(self):
618
"""
619
Time the computation in Maple.
620
621
EXAMPLE:
622
sage: from sage.tests.benchmark import MPolynomialMult2
623
sage: B = MPolynomialMult2()
624
sage: isinstance(B.maple()[1], float) # optional -- requires maple
625
True
626
627
"""
628
R = PolynomialRing(self.base, self.nvars, 'x')
629
k = int(self.nvars/2)
630
z0 = R(0)
631
z1 = R(0)
632
for i in range(k):
633
z0 += (i+1)*R.gen(i)
634
for i in range(k,self.nvars):
635
z1 += (i+1)*R.gen(i)
636
z0 = maple(str(z0))
637
z1 = maple(str(z1))
638
w = walltime()
639
f = (z0*z1).expand()
640
return False, walltime(w)
641
642
def mathematica(self):
643
"""
644
Time the computation in Mathematica.
645
646
EXAMPLE:
647
sage: from sage.tests.benchmark import MPolynomialMult2
648
sage: B = MPolynomialMult2()
649
sage: isinstance(B.mathematica()[1], float) # optional
650
True
651
652
"""
653
R = PolynomialRing(self.base, self.nvars, 'x')
654
k = int(self.nvars/2)
655
z0 = R(0)
656
z1 = R(0)
657
for i in range(k):
658
z0 += (i+1)*R.gen(i)
659
for i in range(k,self.nvars):
660
z1 += (i+1)*R.gen(i)
661
z0 = mathematica(str(z0))
662
z1 = mathematica(str(z1))
663
w = walltime()
664
f = (z0*z1).Expand()
665
return False, walltime(w)
666
667
def sage(self):
668
"""
669
Time the computation in Sage.
670
671
EXAMPLE:
672
sage: from sage.tests.benchmark import MPolynomialMult2
673
sage: B = MPolynomialMult2()
674
sage: isinstance(B.sage()[1], float)
675
True
676
677
"""
678
R = PolynomialRing(self.base, self.nvars, 'x')
679
k = int(self.nvars/2)
680
z0 = R(0)
681
z1 = R(0)
682
for i in range(k):
683
z0 += (i+1)*R.gen(i)
684
for i in range(k,self.nvars):
685
z1 += (i+1)*R.gen(i)
686
if self.allow_singular:
687
z0 = singular(z0)
688
z1 = singular(z1)
689
t = walltime()
690
w = z0*z1
691
return False, walltime(t)
692
else:
693
t = cputime()
694
w = z0 * z1
695
return cputime(t)
696
697
def magma(self):
698
"""
699
Time the computation in Magma.
700
701
EXAMPLE:
702
sage: from sage.tests.benchmark import MPolynomialMult2
703
sage: B = MPolynomialMult2()
704
sage: isinstance(B.magma(), float) # optional
705
True
706
707
"""
708
R = magma.PolynomialRing(self.base, self.nvars)
709
z0 = R.gen(1)
710
k = int(self.nvars/2)
711
for i in range(2,k+1):
712
z0 += magma(i)*R.gen(i)
713
z1 = R.gen(k + 1)
714
for i in range(k+1, self.nvars + 1):
715
z1 += magma(i)*R.gen(i)
716
t = magma.cputime()
717
w = z0 * z1
718
return magma.cputime(t)
719
720
721
722
class CharPolyTp(Benchmark):
723
def __init__(self, N=37,k=2,p=2,sign=1):
724
self.N = N
725
self.k = k
726
self.p = p
727
self.sign = sign
728
self.repr_str = "Compute the charpoly (given the matrix) of T_%s on S_%s(Gamma_0(%s)) with sign %s."%(self.p, self.k, self.N, self.sign)
729
730
def matrix(self):
731
try:
732
return self._matrix
733
except AttributeError:
734
self._matrix = ModularSymbols(group=self.N, weight=self.k, sign=self.sign).T(self.p).matrix()
735
return self._matrix
736
737
def sage(self):
738
"""
739
Time the computation in Sage.
740
741
EXAMPLE:
742
sage: from sage.tests.benchmark import CharPolyTp
743
sage: B = CharPolyTp()
744
sage: isinstance(B.sage(), float)
745
True
746
747
"""
748
m = self.matrix()
749
t = cputime()
750
f = m.charpoly('x')
751
return cputime(t)
752
753
def gp(self):
754
"""
755
Time the computation in GP.
756
757
EXAMPLE:
758
sage: from sage.tests.benchmark import CharPolyTp
759
sage: B = CharPolyTp()
760
sage: isinstance(B.gp(), float)
761
True
762
763
"""
764
m = gp(self.matrix())
765
gp.eval('gettime')
766
f = m.charpoly('x')
767
return float(gp.eval('gettime/1000.0'))
768
769
def pari(self):
770
"""
771
Time the computation in Pari.
772
773
EXAMPLE:
774
sage: from sage.tests.benchmark import CharPolyTp
775
sage: B = CharPolyTp()
776
sage: isinstance(B.pari(), float)
777
True
778
779
"""
780
m = pari(self.matrix())
781
t = cputime()
782
f = m.charpoly('x')
783
return cputime(t)
784
785
def magma(self):
786
"""
787
Time the computation in Magma.
788
789
EXAMPLE:
790
sage: from sage.tests.benchmark import CharPolyTp
791
sage: B = CharPolyTp()
792
sage: isinstance(B.magma(), float) # optional
793
True
794
795
"""
796
m = magma(self.matrix())
797
t = magma.cputime()
798
f = m.CharacteristicPolynomial()
799
return magma.cputime(t)
800
801
802
803
804
class PolyFactor(Benchmark):
805
def __init__(self, n, R):
806
self.__n = n
807
self.__R = R
808
self.repr_str = "Factor a product of 2 polynomials of degree %s over %s."%(self.__n, self.__R)
809
810
def sage(self):
811
"""
812
Time the computation in Sage.
813
814
EXAMPLE:
815
sage: from sage.tests.benchmark import PolyFactor
816
sage: B = PolyFactor(3, QQ)
817
sage: isinstance(B.sage(), float)
818
True
819
820
"""
821
R = PolynomialRing(self.__R, 'x')
822
f = R(range(1,self.__n+1))
823
g = R(range(self.__n+1,2*(self.__n+1)))
824
h = f*g
825
t = cputime()
826
h.factor()
827
return cputime(t)
828
829
def magma(self):
830
"""
831
Time the computation in Magma.
832
833
EXAMPLE:
834
sage: from sage.tests.benchmark import PolyFactor
835
sage: B = PolyFactor(3, QQ)
836
sage: isinstance(B.magma(), float) # optional
837
True
838
839
"""
840
R = magma(self.__R)
841
f = magma('PolynomialRing(%s)![1..%s]'%(R.name(),self.__n))
842
g = magma('PolynomialRing(%s)![%s+1..2*(%s+1)]'%(
843
R.name(),self.__n,self.__n))
844
h = f*g
845
t = magma.cputime()
846
h.Factorization()
847
return magma.cputime(t)
848
849
def gp(self):
850
"""
851
Time the computation in GP.
852
853
EXAMPLE:
854
sage: from sage.tests.benchmark import PolyFactor
855
sage: B = PolyFactor(3, QQ)
856
sage: isinstance(B.gp(), float)
857
True
858
859
"""
860
R = PolynomialRing(self.__R, 'x')
861
f = R(range(1,self.__n+1))
862
g = R(range(self.__n+1,2*(self.__n+1)))
863
h = f*g
864
f = gp(h)
865
gp.eval('gettime')
866
f.factor()
867
return float(gp.eval('gettime/1000.0'))
868
869
870
871
class SquareInts(Benchmark):
872
def __init__(self, base=10, ndigits=10**5):
873
self.__ndigits = ndigits
874
self.base =base
875
self.repr_str = "Square the integer %s^%s"%(self.base, self.__ndigits)
876
877
def sage(self):
878
"""
879
Time the computation in Sage.
880
881
EXAMPLE:
882
sage: from sage.tests.benchmark import SquareInts
883
sage: B = SquareInts()
884
sage: isinstance(B.sage(), float)
885
True
886
887
"""
888
n = Integer(self.base)**self.__ndigits
889
t = cputime()
890
m = n**2
891
return cputime(t)
892
893
def gp(self):
894
"""
895
Time the computation in GP.
896
897
EXAMPLE:
898
sage: from sage.tests.benchmark import SquareInts
899
sage: B = SquareInts()
900
sage: isinstance(B.gp(), float)
901
True
902
903
"""
904
n = gp('%s^%s'%(self.base,self.__ndigits))
905
gp.eval('gettime')
906
m = n**2
907
return float(gp.eval('gettime/1000.0'))
908
909
def maxima(self):
910
"""
911
Time the computation in Maxima.
912
913
EXAMPLE:
914
sage: from sage.tests.benchmark import SquareInts
915
sage: B = SquareInts()
916
sage: isinstance(B.maxima()[1], float)
917
True
918
919
"""
920
n = maxima('%s^%s'%(self.base,self.__ndigits))
921
t = walltime()
922
m = n**2
923
return False, walltime(t)
924
925
def magma(self):
926
"""
927
Time the computation in Magma.
928
929
EXAMPLE:
930
sage: from sage.tests.benchmark import SquareInts
931
sage: B = SquareInts()
932
sage: isinstance(B.magma(), float) # optional
933
True
934
935
"""
936
n = magma('%s^%s'%(self.base,self.__ndigits))
937
t = magma.cputime()
938
m = n**2
939
return magma.cputime(t)
940
941
def python(self):
942
"""
943
Time the computation in Python.
944
945
EXAMPLE:
946
sage: from sage.tests.benchmark import SquareInts
947
sage: B = SquareInts()
948
sage: isinstance(B.python(), float)
949
True
950
951
"""
952
n = self.base**self.__ndigits
953
t = cputime()
954
m = n**2
955
return cputime(t)
956
957
def maple(self):
958
"""
959
Time the computation in Maple.
960
961
EXAMPLE:
962
sage: from sage.tests.benchmark import SquareInts
963
sage: B = SquareInts()
964
sage: isinstance(B.maple()[1], float) # optional -- requires maple
965
True
966
967
"""
968
n = maple('%s^%s'%(self.base,self.__ndigits))
969
t = walltime()
970
m = n**2
971
return False, walltime(t)
972
973
def gap(self):
974
"""
975
Time the computation in GAP.
976
977
EXAMPLE:
978
sage: from sage.tests.benchmark import SquareInts
979
sage: B = SquareInts()
980
sage: isinstance(B.gap()[1], float)
981
True
982
983
"""
984
n = gap('%s^%s'%(self.base,self.__ndigits))
985
t = walltime()
986
m = n**2
987
return False, walltime(t)
988
989
def mathematica(self):
990
"""
991
Time the computation in Mathematica.
992
993
EXAMPLE:
994
sage: from sage.tests.benchmark import SquareInts
995
sage: B = SquareInts()
996
sage: isinstance(B.mathematica()[1], float) # optional
997
True
998
999
"""
1000
n = mathematica('%s^%s'%(self.base,self.__ndigits))
1001
t = walltime()
1002
m = n**2
1003
return False, walltime(t)
1004
1005
1006
class MatrixSquare(Benchmark):
1007
def __init__(self, n, R):
1008
self.__n = n
1009
self.__R = R
1010
self.repr_str = 'Square a matrix of degree %s over %s'%(self.__n, self.__R)
1011
1012
def sage(self):
1013
"""
1014
Time the computation in Sage.
1015
1016
EXAMPLE:
1017
sage: from sage.tests.benchmark import MatrixSquare
1018
sage: B = MatrixSquare(3, QQ)
1019
sage: isinstance(B.sage(), float)
1020
True
1021
1022
"""
1023
R = self.__R
1024
n = self.__n
1025
f = MatrixSpace(R,n)(range(n*n))
1026
t = cputime()
1027
g = f**2
1028
return cputime(t)
1029
1030
def magma(self):
1031
"""
1032
Time the computation in Magma.
1033
1034
EXAMPLE:
1035
sage: from sage.tests.benchmark import MatrixSquare
1036
sage: B = MatrixSquare(3, QQ)
1037
sage: isinstance(B.magma(), float) # optional
1038
True
1039
1040
"""
1041
R = magma(self.__R)
1042
f = magma('MatrixAlgebra(%s, %s)![0..%s^2-1]'%(
1043
R.name(),self.__n, self.__n))
1044
t = magma.cputime()
1045
g = f*f
1046
return magma.cputime(t)
1047
1048
def gp(self):
1049
"""
1050
Time the computation in GP.
1051
1052
EXAMPLE:
1053
sage: from sage.tests.benchmark import MatrixSquare
1054
sage: B = MatrixSquare(3, QQ)
1055
sage: isinstance(B.gp(), float)
1056
True
1057
1058
"""
1059
n = self.__n
1060
m = gp('matrix(%s,%s,m,n,%s*(m-1)+(n-1))'%(n,n,n))
1061
gp('gettime')
1062
n = m*m
1063
return float(gp.eval('gettime/1000.0'))
1064
1065
def gap(self):
1066
"""
1067
Time the computation in GAP.
1068
1069
EXAMPLE:
1070
sage: from sage.tests.benchmark import MatrixSquare
1071
sage: B = MatrixSquare(3, QQ)
1072
sage: isinstance(B.gap()[1], float)
1073
True
1074
1075
"""
1076
n = self.__n
1077
m = gap(str([range(n*k,n*(k+1)) for k in range(n)]))
1078
t = walltime()
1079
j = m*m
1080
return False, walltime(t)
1081
1082
1083
class Factorial(Benchmark):
1084
def __init__(self, n):
1085
self.__n = n
1086
self.repr_str = "Compute the factorial of %s"%self.__n
1087
1088
def sage(self):
1089
"""
1090
Time the computation in Sage.
1091
1092
EXAMPLE:
1093
sage: from sage.tests.benchmark import Factorial
1094
sage: B = Factorial(10)
1095
sage: isinstance(B.sage(), float)
1096
True
1097
1098
"""
1099
t = cputime()
1100
n = factorial(self.__n)
1101
return cputime(t)
1102
1103
def magma(self):
1104
"""
1105
Time the computation in Magma.
1106
1107
EXAMPLE:
1108
sage: from sage.tests.benchmark import Factorial
1109
sage: B = Factorial(10)
1110
sage: isinstance(B.magma(), float) # optional
1111
True
1112
1113
"""
1114
t = magma.cputime()
1115
n = magma('&*[1..%s]'%self.__n) # &* is way better than Factorial!!
1116
return magma.cputime(t)
1117
1118
def maple(self):
1119
"""
1120
Time the computation in Maple.
1121
1122
EXAMPLE:
1123
sage: from sage.tests.benchmark import Factorial
1124
sage: B = Factorial(10)
1125
sage: isinstance(B.maple()[1], float) # optional -- requires maple
1126
True
1127
1128
"""
1129
n = maple(self.__n)
1130
t = walltime()
1131
m = n.factorial()
1132
return False, walltime(t)
1133
1134
def gp(self):
1135
"""
1136
Time the computation in GP.
1137
1138
EXAMPLE:
1139
sage: from sage.tests.benchmark import Factorial
1140
sage: B = Factorial(10)
1141
sage: isinstance(B.gp(), float)
1142
True
1143
1144
"""
1145
gp.eval('gettime')
1146
n = gp('%s!'%self.__n)
1147
return float(gp.eval('gettime/1000.0'))
1148
1149
class Fibonacci(Benchmark):
1150
def __init__(self, n):
1151
self.__n = n
1152
self.repr_str = "Compute the %s-th Fibonacci number"%self.__n
1153
1154
def sage(self):
1155
"""
1156
Time the computation in Sage.
1157
1158
EXAMPLE:
1159
sage: from sage.tests.benchmark import Fibonacci
1160
sage: B = Fibonacci(10)
1161
sage: isinstance(B.sage(), float)
1162
True
1163
1164
"""
1165
t = cputime()
1166
n = fibonacci(self.__n)
1167
return cputime(t)
1168
1169
def magma(self):
1170
"""
1171
Time the computation in Magma.
1172
1173
EXAMPLE:
1174
sage: from sage.tests.benchmark import Fibonacci
1175
sage: B = Fibonacci(10)
1176
sage: isinstance(B.magma(), float) # optional
1177
True
1178
1179
"""
1180
t = magma.cputime()
1181
n = magma('Fibonacci(%s)'%self.__n)
1182
return magma.cputime(t)
1183
1184
def gap(self):
1185
"""
1186
Time the computation in GAP.
1187
1188
EXAMPLE:
1189
sage: from sage.tests.benchmark import Fibonacci
1190
sage: B = Fibonacci(10)
1191
sage: isinstance(B.gap()[1], float)
1192
True
1193
1194
"""
1195
n = gap(self.__n)
1196
t = walltime()
1197
m = n.Fibonacci()
1198
return False, walltime(t)
1199
1200
def mathematica(self):
1201
"""
1202
Time the computation in Mathematica.
1203
1204
EXAMPLE:
1205
sage: from sage.tests.benchmark import Fibonacci
1206
sage: B = Fibonacci(10)
1207
sage: isinstance(B.mathematica()[1], float) # optional
1208
True
1209
1210
"""
1211
n = mathematica(self.__n)
1212
t = walltime()
1213
m = n.Fibonacci()
1214
return False, walltime(t)
1215
1216
def gp(self):
1217
"""
1218
Time the computation in GP.
1219
1220
EXAMPLE:
1221
sage: from sage.tests.benchmark import Fibonacci
1222
sage: B = Fibonacci(10)
1223
sage: isinstance(B.gp(), float)
1224
True
1225
1226
"""
1227
gp.eval('gettime')
1228
n = gp('fibonacci(%s)'%self.__n)
1229
return float(gp.eval('gettime/1000.0'))
1230
1231
1232
1233
class SEA(Benchmark):
1234
def __init__(self, p):
1235
self.__p = p
1236
self.repr_str = "Do SEA on an elliptic curve over GF(%s)"%self.__p
1237
1238
def sage(self):
1239
"""
1240
Time the computation in Sage.
1241
1242
EXAMPLE:
1243
sage: from sage.tests.benchmark import SEA
1244
sage: B = SEA(5)
1245
sage: isinstance(B.sage()[1], float)
1246
True
1247
1248
"""
1249
E = EllipticCurve([1,2,3,4,5])
1250
t = walltime()
1251
# Note that from pari 2.4.3, the SEA algorithm is used by the
1252
# pari library, but only for large primes, so for a better
1253
# test a prime > 2^30 should be used and not 5. In fact
1254
# next_prime(2^100) works fine (<<1s).
1255
n = E.change_ring(GF(self.__p)).cardinality_pari()
1256
return False, walltime(t)
1257
1258
def magma(self):
1259
"""
1260
Time the computation in Magma.
1261
1262
EXAMPLE:
1263
sage: from sage.tests.benchmark import SEA
1264
sage: B = SEA(5)
1265
sage: isinstance(B.magma(), float) # optional
1266
True
1267
1268
"""
1269
magma(0)
1270
t = magma.cputime()
1271
m = magma('#EllipticCurve([GF(%s)|1,2,3,4,5])'%(self.__p))
1272
return magma.cputime(t)
1273
1274
class MatrixKernel(Benchmark):
1275
def __init__(self, n, R):
1276
self.__n = n
1277
self.__R = R
1278
self.repr_str = 'Kernel of a matrix of degree %s over %s'%(self.__n, self.__R)
1279
1280
def sage(self):
1281
"""
1282
Time the computation in Sage.
1283
1284
EXAMPLE:
1285
sage: from sage.tests.benchmark import MatrixKernel
1286
sage: B = MatrixKernel(3, QQ)
1287
sage: isinstance(B.sage(), float)
1288
True
1289
1290
"""
1291
R = self.__R
1292
n = self.__n
1293
f = MatrixSpace(R,n,2*n)(range(n*(2*n)))
1294
t = cputime()
1295
g = f.kernel()
1296
return cputime(t)
1297
1298
def magma(self):
1299
"""
1300
Time the computation in Magma.
1301
1302
EXAMPLE:
1303
sage: from sage.tests.benchmark import MatrixKernel
1304
sage: B = MatrixKernel(3, QQ)
1305
sage: isinstance(B.magma(), float) # optional
1306
True
1307
1308
"""
1309
R = magma(self.__R)
1310
f = magma('RMatrixSpace(%s, %s, %s)![0..(%s*2*%s)-1]'%(
1311
R.name(),self.__n, 2*self.__n, self.__n, self.__n))
1312
t = magma.cputime()
1313
g = f.Kernel()
1314
return magma.cputime(t)
1315
1316
def gp(self):
1317
"""
1318
Time the computation in GP.
1319
1320
EXAMPLE:
1321
sage: from sage.tests.benchmark import MatrixKernel
1322
sage: B = MatrixKernel(3, QQ)
1323
sage: isinstance(B.gp(), float)
1324
True
1325
1326
"""
1327
n = self.__n
1328
m = gp('matrix(%s,%s,m,n,%s*(m-1)+(n-1))'%(n,2*n,n))
1329
gp('gettime')
1330
n = m.matker()
1331
return float(gp.eval('gettime/1000.0'))
1332
1333
class ComplexMultiply(Benchmark):
1334
def __init__(self, bits_prec, times):
1335
self.__bits_prec = bits_prec
1336
self.__times = times
1337
self.repr_str = "List of multiplies of two complex numbers with %s bits of precision %s times"%(self.__bits_prec, self.__times)
1338
1339
def sage(self):
1340
"""
1341
Time the computation in Sage.
1342
1343
EXAMPLE:
1344
sage: from sage.tests.benchmark import ComplexMultiply
1345
sage: B = ComplexMultiply(28, 2)
1346
sage: isinstance(B.sage(), float)
1347
True
1348
1349
"""
1350
CC = ComplexField(self.__bits_prec)
1351
s = CC(2).sqrt() + (CC.gen()*2).sqrt()
1352
t = cputime()
1353
v = [s*s for _ in range(self.__times)]
1354
return cputime(t)
1355
1356
def magma(self):
1357
"""
1358
Time the computation in Magma.
1359
1360
EXAMPLE:
1361
sage: from sage.tests.benchmark import ComplexMultiply
1362
sage: B = ComplexMultiply(28, 2)
1363
sage: isinstance(B.magma(), float) # optional
1364
True
1365
1366
NOTES:
1367
decimal digits (despite magma docs that say bits!!)
1368
1369
"""
1370
n = int(self.__bits_prec/log(10,2)) + 1
1371
CC = magma.ComplexField(n)
1372
s = CC(2).Sqrt() + CC.gen(1).Sqrt()
1373
t = magma.cputime()
1374
magma.eval('s := %s;'%s.name())
1375
v = magma('[s*s : i in [1..%s]]'%self.__times)
1376
return magma.cputime(t)
1377
1378
def gp(self):
1379
"""
1380
Time the computation in GP.
1381
1382
EXAMPLE:
1383
sage: from sage.tests.benchmark import ComplexMultiply
1384
sage: B = ComplexMultiply(28, 2)
1385
sage: isinstance(B.gp(), float)
1386
True
1387
1388
"""
1389
n = int(self.__bits_prec/log(10,2)) + 1
1390
gp.set_real_precision(n)
1391
gp.eval('s = sqrt(2) + sqrt(2*I);')
1392
gp.eval('gettime;')
1393
v = gp('vector(%s,i,s*s)'%self.__times)
1394
return float(gp.eval('gettime/1000.0'))
1395
1396
class ModularSymbols1(Benchmark):
1397
def __init__(self, N, k=2):
1398
self.__N = N
1399
self.__k = k
1400
self.repr_str = 'Presentation for modular symbols on Gamma_0(%s) of weight %s'%(self.__N, self.__k)
1401
1402
def sage(self):
1403
"""
1404
Time the computation in Sage.
1405
1406
EXAMPLE:
1407
sage: from sage.tests.benchmark import ModularSymbols1
1408
sage: B = ModularSymbols1(11)
1409
sage: isinstance(B.sage(), float)
1410
True
1411
1412
"""
1413
t = cputime()
1414
M = ModularSymbols(self.__N, self.__k)
1415
return cputime(t)
1416
1417
def magma(self):
1418
"""
1419
Time the computation in Magma.
1420
1421
EXAMPLE:
1422
sage: from sage.tests.benchmark import ModularSymbols1
1423
sage: B = ModularSymbols1(11)
1424
sage: isinstance(B.magma(), float) # optional
1425
True
1426
1427
"""
1428
magma = Magma() # new instance since otherwise modsyms are cached, and cache can't be cleared
1429
t = magma.cputime()
1430
M = magma('ModularSymbols(%s, %s)'%(self.__N, self.__k))
1431
return magma.cputime(t)
1432
1433
class ModularSymbolsDecomp1(Benchmark):
1434
def __init__(self, N, k=2, sign=1, bnd=10):
1435
self.N = N
1436
self.k = k
1437
self.sign = sign
1438
self.bnd = bnd
1439
self.repr_str = 'Decomposition of modular symbols on Gamma_0(%s) of weight %s and sign %s'%(self.N, self.k, self.sign)
1440
1441
def sage(self):
1442
"""
1443
Time the computation in Sage.
1444
1445
EXAMPLE:
1446
sage: from sage.tests.benchmark import ModularSymbolsDecomp1
1447
sage: B = ModularSymbolsDecomp1(11)
1448
sage: isinstance(B.sage(), float)
1449
True
1450
1451
"""
1452
t = cputime()
1453
M = ModularSymbols(self.N, self.k, sign=self.sign, use_cache=False)
1454
D = M.decomposition(self.bnd)
1455
return cputime(t)
1456
1457
def magma(self):
1458
"""
1459
Time the computation in Magma.
1460
1461
EXAMPLE:
1462
sage: from sage.tests.benchmark import ModularSymbolsDecomp1
1463
sage: B = ModularSymbolsDecomp1(11)
1464
sage: isinstance(B.magma(), float) # optional
1465
True
1466
1467
"""
1468
m = Magma() # new instance since otherwise modsyms are cached, and cache can't be cleared
1469
t = m.cputime()
1470
D = m.eval('Decomposition(ModularSymbols(%s, %s, %s),%s);'%(
1471
self.N, self.k, self.sign, self.bnd))
1472
return m.cputime(t)
1473
1474
class EllipticCurveTraces(Benchmark):
1475
def __init__(self, B):
1476
self.B = B
1477
self.repr_str = "Compute all a_p for the elliptic curve [1,2,3,4,5], for p < %s"%self.B
1478
1479
def sage(self):
1480
"""
1481
Time the computation in Sage.
1482
1483
EXAMPLE:
1484
sage: from sage.tests.benchmark import EllipticCurveTraces
1485
sage: B = EllipticCurveTraces(11)
1486
sage: isinstance(B.sage(), float)
1487
Traceback (most recent call last):
1488
...
1489
TypeError: anlist() got an unexpected keyword argument 'pari_ints'
1490
1491
"""
1492
E = EllipticCurve([1,2,3,4,5])
1493
t = cputime()
1494
v = E.anlist(self.B, pari_ints=True)
1495
return cputime(t)
1496
1497
def magma(self):
1498
"""
1499
Time the computation in Magma.
1500
1501
EXAMPLE:
1502
sage: from sage.tests.benchmark import EllipticCurveTraces
1503
sage: B = EllipticCurveTraces(11)
1504
sage: isinstance(B.magma(), float) # optional
1505
True
1506
1507
"""
1508
E = magma.EllipticCurve([1,2,3,4,5])
1509
t = magma.cputime()
1510
v = E.TracesOfFrobenius(self.B)
1511
return magma.cputime(t)
1512
1513
class EllipticCurvePointMul(Benchmark):
1514
def __init__(self, n):
1515
self.n = n
1516
self.repr_str = "Compute %s*(0,0) on the elliptic curve [0, 0, 1, -1, 0] over QQ"%self.n
1517
1518
def sage(self):
1519
"""
1520
Time the computation in Sage.
1521
1522
EXAMPLE:
1523
sage: from sage.tests.benchmark import EllipticCurvePointMul
1524
sage: B = EllipticCurvePointMul(11)
1525
sage: isinstance(B.sage(), float)
1526
True
1527
1528
"""
1529
E = EllipticCurve([0, 0, 1, -1, 0])
1530
P = E([0,0])
1531
t = cputime()
1532
Q = self.n * P
1533
return cputime(t)
1534
1535
def magma(self):
1536
"""
1537
Time the computation in Magma.
1538
1539
EXAMPLE:
1540
sage: from sage.tests.benchmark import EllipticCurvePointMul
1541
sage: B = EllipticCurvePointMul(11)
1542
sage: isinstance(B.magma(), float) # optional
1543
True
1544
1545
"""
1546
E = magma.EllipticCurve('[0, 0, 1, -1, 0]')
1547
P = E('[0,0]')
1548
t = magma.cputime()
1549
Q = magma(self.n) * P
1550
return magma.cputime(t)
1551
1552
def gp(self):
1553
"""
1554
Time the computation in GP.
1555
1556
EXAMPLE:
1557
sage: from sage.tests.benchmark import EllipticCurvePointMul
1558
sage: B = EllipticCurvePointMul(11)
1559
sage: isinstance(B.gp(), float)
1560
True
1561
1562
"""
1563
E = gp.ellinit('[0, 0, 1, -1, 0]')
1564
gp.eval('gettime')
1565
P = gp([0,0])
1566
Q = E.ellpow(P, self.n)
1567
return float(gp.eval('gettime/1000.0'))
1568
1569
def pari(self):
1570
"""
1571
Time the computation in Pari.
1572
1573
EXAMPLE:
1574
sage: from sage.tests.benchmark import EllipticCurvePointMul
1575
sage: B = EllipticCurvePointMul(11)
1576
sage: isinstance(B.pari(), float)
1577
True
1578
1579
"""
1580
E = pari('ellinit([0, 0, 1, -1, 0])')
1581
pari('gettime')
1582
P = pari([0,0])
1583
Q = E.ellpow(P, self.n)
1584
return float(pari('gettime/1000.0'))
1585
1586
class EllipticCurveMW(Benchmark):
1587
def __init__(self, ainvs):
1588
self.ainvs = ainvs
1589
self.repr_str = "Compute generators for the Mordell-Weil group of the elliptic curve %s over QQ"%self.ainvs
1590
1591
def sage(self):
1592
"""
1593
Time the computation in Sage.
1594
1595
EXAMPLE:
1596
sage: from sage.tests.benchmark import EllipticCurveMW
1597
sage: B = EllipticCurveMW([1,2,3,4,5])
1598
sage: isinstance(B.sage()[1], float)
1599
True
1600
1601
"""
1602
E = EllipticCurve(self.ainvs)
1603
t = walltime()
1604
G = E.gens()
1605
return False, walltime(t)
1606
1607
def magma(self):
1608
"""
1609
Time the computation in Magma.
1610
1611
EXAMPLE:
1612
sage: from sage.tests.benchmark import EllipticCurveMW
1613
sage: B = EllipticCurveMW([1,2,3,4,5])
1614
sage: isinstance(B.magma(), float) # optional
1615
True
1616
1617
"""
1618
E = magma.EllipticCurve(str(self.ainvs))
1619
t = magma.cputime()
1620
G = E.Generators()
1621
return magma.cputime(t)
1622
1623
class FiniteExtFieldMult(Benchmark):
1624
def __init__(self,field,times):
1625
self.__times = times
1626
self.field = field
1627
self.e = field.gen()**(field.cardinality()/3)
1628
self.f = field.gen()**(2*field.cardinality()/3)
1629
self.repr_str = "Multiply a^(#K/3) with a^(2*#K/3) where a == K.gen()"
1630
1631
def sage(self):
1632
"""
1633
Time the computation in Sage.
1634
1635
EXAMPLE:
1636
sage: from sage.tests.benchmark import FiniteExtFieldMult
1637
sage: B = FiniteExtFieldMult(GF(9, 'x'), 2)
1638
sage: isinstance(B.sage(), float)
1639
True
1640
1641
"""
1642
e = self.e
1643
f = self.f
1644
t = cputime()
1645
v = [e*f for _ in range(self.__times)]
1646
return cputime(t)
1647
1648
def pari(self):
1649
"""
1650
Time the computation in Pari.
1651
1652
EXAMPLE:
1653
sage: from sage.tests.benchmark import FiniteExtFieldMult
1654
sage: B = FiniteExtFieldMult(GF(9, 'x'), 2)
1655
sage: isinstance(B.pari(), float)
1656
True
1657
1658
"""
1659
e = self.e._pari_()
1660
f = self.f._pari_()
1661
t = cputime()
1662
v = [e*f for _ in range(self.__times)]
1663
return cputime(t)
1664
1665
def givaro(self):
1666
"""
1667
Time the computation in Givaro.
1668
1669
EXAMPLE:
1670
sage: from sage.tests.benchmark import FiniteExtFieldMult
1671
sage: B = FiniteExtFieldMult(GF(9, 'x'), 2)
1672
sage: isinstance(B.givaro(), float)
1673
Traceback (most recent call last):
1674
...
1675
AttributeError: 'module' object has no attribute 'GFq'
1676
1677
"""
1678
k = linbox.GFq(self.field.cardinality())
1679
e = k(self.e)
1680
f = k(self.f)
1681
t = cputime()
1682
v = [e*f for _ in range(self.__times)]
1683
return cputime(t)
1684
1685
def givaro_nck(self):
1686
"""
1687
Time the computation in Givaro.
1688
1689
TODO: nck?
1690
1691
EXAMPLE:
1692
sage: from sage.tests.benchmark import FiniteExtFieldMult
1693
sage: B = FiniteExtFieldMult(GF(9, 'x'), 2)
1694
sage: isinstance(B.givaro_nck(), float)
1695
Traceback (most recent call last):
1696
...
1697
AttributeError: 'module' object has no attribute 'GFq'
1698
1699
"""
1700
k = linbox.GFq(self.field.cardinality())
1701
e = k(self.e)
1702
f = k(self.f)
1703
t = cputime()
1704
v = [e.mul(f) for _ in range(self.__times)]
1705
return cputime(t)
1706
1707
def givaro_raw(self):
1708
"""
1709
Time the computation in Givaro.
1710
1711
TODO: raw?
1712
1713
EXAMPLE:
1714
sage: from sage.tests.benchmark import FiniteExtFieldMult
1715
sage: B = FiniteExtFieldMult(GF(9, 'x'), 2)
1716
sage: isinstance(B.givaro_raw(), float)
1717
Traceback (most recent call last):
1718
...
1719
AttributeError: 'module' object has no attribute 'GFq'
1720
1721
"""
1722
k = linbox.GFq(self.field.cardinality())
1723
e = k(self.e).logint()
1724
f = k(self.f).logint()
1725
t = cputime()
1726
v = [k._mul(e,f) for _ in range(self.__times)]
1727
return cputime(t)
1728
1729
def magma(self):
1730
"""
1731
Time the computation in Magma.
1732
1733
EXAMPLE:
1734
sage: from sage.tests.benchmark import FiniteExtFieldMult
1735
sage: B = FiniteExtFieldMult(GF(9, 'x'), 2)
1736
sage: isinstance(B.magma(), float) # optional
1737
True
1738
1739
"""
1740
magma.eval('F<a> := GF(%s)'%(self.field.cardinality()))
1741
magma.eval('e := a^Floor(%s/3);'%(self.field.cardinality()))
1742
magma.eval('f := a^Floor(2*%s/3);'%(self.field.cardinality()))
1743
t = magma.cputime()
1744
v = magma('[e*f : i in [1..%s]]'%self.__times)
1745
return magma.cputime(t)
1746
1747
class FiniteExtFieldAdd(Benchmark):
1748
def __init__(self,field,times):
1749
self.__times = times
1750
self.field = field
1751
self.e = field.gen()**(field.cardinality()/3)
1752
self.f = field.gen()**(2*field.cardinality()/3)
1753
self.repr_str = "Add a^(#K/3) to a^(2*#K/3) where a == K.gen()"
1754
1755
def sage(self):
1756
"""
1757
Time the computation in Sage.
1758
1759
EXAMPLE:
1760
sage: from sage.tests.benchmark import FiniteExtFieldAdd
1761
sage: B = FiniteExtFieldAdd(GF(9,'x'), 2)
1762
sage: isinstance(B.sage(), float)
1763
True
1764
1765
"""
1766
e = self.e
1767
f = self.f
1768
t = cputime()
1769
v = [e+f for _ in range(self.__times)]
1770
return cputime(t)
1771
1772
def pari(self):
1773
"""
1774
Time the computation in Pari.
1775
1776
EXAMPLE:
1777
sage: from sage.tests.benchmark import FiniteExtFieldAdd
1778
sage: B = FiniteExtFieldAdd(GF(9,'x'), 2)
1779
sage: isinstance(B.pari(), float)
1780
True
1781
1782
"""
1783
e = self.e._pari_()
1784
f = self.f._pari_()
1785
t = cputime()
1786
v = [e+f for _ in range(self.__times)]
1787
return cputime(t)
1788
1789
def givaro(self):
1790
"""
1791
Time the computation in Givaro.
1792
1793
EXAMPLE:
1794
sage: from sage.tests.benchmark import FiniteExtFieldAdd
1795
sage: B = FiniteExtFieldAdd(GF(9,'x'), 2)
1796
sage: isinstance(B.givaro(), float)
1797
Traceback (most recent call last):
1798
...
1799
AttributeError: 'module' object has no attribute 'GFq'
1800
1801
"""
1802
k = linbox.GFq(self.field.cardinality())
1803
e = k(self.e)
1804
f = k(self.f)
1805
t = cputime()
1806
v = [e+f for _ in range(self.__times)]
1807
return cputime(t)
1808
1809
def givaro_nck(self):
1810
"""
1811
Time the computation in Givaro.
1812
1813
TODO: nck?
1814
1815
EXAMPLE:
1816
sage: from sage.tests.benchmark import FiniteExtFieldAdd
1817
sage: B = FiniteExtFieldAdd(GF(9,'x'), 2)
1818
sage: isinstance(B.givaro_nck(), float)
1819
Traceback (most recent call last):
1820
...
1821
AttributeError: 'module' object has no attribute 'GFq'
1822
1823
"""
1824
k = linbox.GFq(self.field.cardinality())
1825
e = k(self.e)
1826
f = k(self.f)
1827
t = cputime()
1828
v = [e.add(f) for _ in range(self.__times)]
1829
return cputime(t)
1830
1831
def givaro_raw(self):
1832
"""
1833
Time the computation in Givaro.
1834
1835
TODO: raw?
1836
1837
EXAMPLE:
1838
sage: from sage.tests.benchmark import FiniteExtFieldAdd
1839
sage: B = FiniteExtFieldAdd(GF(9, 'x'), 2)
1840
sage: isinstance(B.givaro_raw(), float)
1841
Traceback (most recent call last):
1842
...
1843
AttributeError: 'module' object has no attribute 'GFq'
1844
1845
"""
1846
k = linbox.GFq(self.field.cardinality())
1847
e = k(self.e).logint()
1848
f = k(self.f).logint()
1849
t = cputime()
1850
v = [k._add(e,f) for _ in range(self.__times)]
1851
return cputime(t)
1852
1853
def magma(self):
1854
"""
1855
Time the computation in Magma.
1856
1857
EXAMPLE:
1858
sage: from sage.tests.benchmark import FiniteExtFieldAdd
1859
sage: B = FiniteExtFieldAdd(GF(9,'x'), 2)
1860
sage: isinstance(B.magma(), float) # optional
1861
True
1862
1863
"""
1864
magma.eval('F<a> := GF(%s)'%(self.field.cardinality()))
1865
magma.eval('e := a^Floor(%s/3);'%(self.field.cardinality()))
1866
magma.eval('f := a^Floor(2*%s/3);'%(self.field.cardinality()))
1867
t = magma.cputime()
1868
v = magma('[e+f : i in [1..%s]]'%self.__times)
1869
return magma.cputime(t)
1870
1871
1872
"""
1873
TODO:
1874
* multiply reals
1875
* modular degree
1876
* anlist
1877
* MW group
1878
* symbolic det
1879
* poly factor
1880
* multivariate poly factor
1881
1882
"""
1883
1884
1885
1886
1887
def suite1():
1888
PolySquare(10000,QQ).run()
1889
PolySquare(20000,ZZ).run()
1890
PolySquare(50000,GF(5)).run()
1891
PolySquare(20000,Integers(8)).run()
1892
1893
SquareInts(10,2000000).run()
1894
1895
MatrixSquare(200,QQ).run()
1896
MatrixSquare(50,ZZ).run()
1897
1898
SquareInts(10,150000).run()
1899
1900
Factorial(2*10**6).run(systems = ['sage', 'magma'])
1901
Fibonacci(10**6).run()
1902
Fibonacci(2*10^7).run(systems=["sage", "magma", "mathematica"])
1903
1904
MatrixKernel(150,QQ).run()
1905
1906
ComplexMultiply(100000,1000)
1907
ComplexMultiply(100,100000)
1908
ComplexMultiply(53,100000)
1909
1910
PolyFactor(300,ZZ)
1911
PolyFactor(300,GF(19))
1912
PolyFactor(700,GF(19))
1913
1914
PolyFactor(500,GF(49,'a'))
1915
PolyFactor(100,GF(10007^3,'a'))
1916
1917
CharPolyTp(54,4).run()
1918
CharPolyTp(389,2).run()
1919
CharPolyTp(389,2,sign=0,p=3).run()
1920
CharPolyTp(1000,2,sign=1,p=2).run(systems=['sage','magma'])
1921
CharPolyTp(1,100,sign=1,p=5).run(systems=['sage','magma']) # Sage's multimodular really sucks here! (GP is way better, even)
1922
CharPolyTp(512,sign=1,p=3).run(systems=['sage','magma','gp'])
1923
CharPolyTp(512,sign=0,p=3).run(systems=['sage','magma','gp'])
1924
CharPolyTp(1024,sign=1,p=3).run(systems=['sage','magma','gp'])
1925
CharPolyTp(2006,sign=1,p=2).run(systems=['sage','magma','gp'])
1926
CharPolyTp(2006,sign=1,p=2).run(systems=['sage','magma']) # gp takes > 1 minute.
1927
1928
def mpoly():
1929
# This includes a maxima benchmark. Note that
1930
# maxima is *shockingly* slow in comparison to Singular or MAGMA.
1931
# It is so slow as to be useless, basically, i.e., factor
1932
# of 5000 slower than Singular on this example!
1933
MPolynomialPower(nvars=6,exp=10).run()
1934
1935
main = ['sage', 'magma'] # just the main competitors
1936
MPolynomialPower(nvars=2,exp=200, allow_singular=False).run(main)
1937
MPolynomialPower(nvars=5,exp=10, allow_singular=False).run(main)
1938
MPolynomialPower(nvars=5,exp=30, allow_singular=True).run(main)
1939
MPolynomialPower(nvars=2,exp=1000, allow_singular=True).run(main)
1940
MPolynomialPower(nvars=10,exp=10, allow_singular=True).run(main)
1941
MPolynomialPower(nvars=4,exp=350, base=GF(7), allow_singular=True).run(main)
1942
MPolynomialMult(200, allow_singular=False).run(main)
1943
MPolynomialMult(400, allow_singular=True).run(main)
1944
MPolynomialMult(800, allow_singular=True).run(main)
1945
MPolynomialMult2(500, allow_singular=True).run(main)
1946
1947
1948
def mpoly_all(include_maple=False):
1949
"""
1950
Runs benchmarks for multipoly arithmetic on all systems (except
1951
Maxima, since it is very very slow). You must have mathematica,
1952
maple, and magma.
1953
1954
NOTES:
1955
* maple is depressingly slow on these benchmarks.
1956
* Singular (i.e., Sage) does shockingly well.
1957
* mathematica is sometimes amazing.
1958
* macaulay2 is also quite bad (though not as bad as maple).
1959
"""
1960
systems = ['sage', 'magma', 'mathematica', 'macaulay2']
1961
if include_maple:
1962
systems.append('maple')
1963
MPolynomialMult(200).run(systems=systems)
1964
MPolynomialMult(400).run(systems=systems)
1965
MPolynomialMult2(256).run(systems=systems)
1966
MPolynomialMult2(512).run(systems=systems)
1967
MPolynomialPower(nvars=4,exp=50).run(systems=systems) # mathematica wins
1968
MPolynomialPower(nvars=10,exp=10).run(systems=systems)
1969
1970
def modsym_present():
1971
ModularSymbols1(2006,2)
1972
ModularSymbols1(1,50)
1973
ModularSymbols1(1,100)
1974
ModularSymbols1(1,150)
1975
ModularSymbols1(30,8)
1976
ModularSymbols1(225,4)
1977
ModularSymbols1(2,50)
1978
ModularSymbols1(2,100)
1979
1980
def modsym_decomp():
1981
ModularSymbolsDecomp1(1,24).run()
1982
ModularSymbolsDecomp1(125,2).run()
1983
ModularSymbolsDecomp1(389,2).run()
1984
ModularSymbolsDecomp1(1,100).run()
1985
ModularSymbolsDecomp1(54,4).run()
1986
1987
def elliptic_curve():
1988
EllipticCurveTraces(100000).run()
1989
EllipticCurveTraces(500000).run()
1990
Divpoly(59).run()
1991
EllipticCurvePointMul(1000).run()
1992
EllipticCurvePointMul(2000).run()
1993
EllipticCurvePointMul(2500).run() # sage is clearly using the wrong algorithm -- maybe need a balanced rep!?
1994
1995
# NOTE -- Sage can also do these using Simon's program, which is
1996
# *way* *way* faster than MAGMA...
1997
EllipticCurveMW([5,6,7,8,9]).run()
1998
EllipticCurveMW([50,6,7,8,9]).run()
1999
EllipticCurveMW([1, -1, 0, -79, 289]).run(trials=1) # rank 4
2000
EllipticCurveMW([0, 0, 1, -79, 342]).run(trials=1) # rank 5 (Sage wins)
2001
2002