Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

""" 

Benchmarks for matrices 

 

This file has many functions for computing timing benchmarks 

of various methods for random matrices with given bounds for 

the entries. The systems supported are Sage and Magma. 

 

The basic command syntax is as follows:: 

 

sage: import sage.matrix.benchmark as b 

sage: print("starting"); import sys; sys.stdout.flush(); b.report([b.det_ZZ], 'Test', systems=['sage']) 

starting... 

====================================================================== 

Test 

====================================================================== 

... 

====================================================================== 

""" 

from __future__ import print_function 

from __future__ import absolute_import 

 

from .constructor import random_matrix, Matrix 

from sage.rings.all import ZZ, QQ, GF 

from sage.misc.misc import cputime 

from cysignals.alarm import AlarmInterrupt, alarm, cancel_alarm 

 

from sage.interfaces.all import magma 

 

verbose = False 

 

timeout = 60 

 

def report(F, title, systems = ['sage', 'magma'], **kwds): 

""" 

Run benchmarks with default arguments for each function in the list F. 

 

INPUT: 

 

- ``F`` - a list of callables used for benchmarking 

- ``title`` - a string describing this report 

- ``systems`` - a list of systems (supported entries are 'sage' and 'magma') 

- ``**kwds`` - keyword arguments passed to all functions in ``F`` 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: print("starting"); import sys; sys.stdout.flush(); b.report([b.det_ZZ], 'Test', systems=['sage']) 

starting... 

====================================================================== 

Test 

====================================================================== 

... 

====================================================================== 

""" 

import os 

if len(systems) > 2: 

raise NotImplementedError("at most two systems ('sage' or 'magma')") 

print('=' * 70) 

print(' ' * 10 + title) 

print('=' * 70) 

os.system('uname -a') 

print('\n') 

for f in F: 

print("-"*70) 

print(f.__doc__.strip()) 

print(('%15s' * len(systems)) % tuple(systems)) 

w = [] 

for s in systems: 

alarm(timeout) 

try: 

t = f(system=s, **kwds) 

except AlarmInterrupt: 

t = -timeout 

cancel_alarm() 

w.append(float(t)) 

if len(w) > 1: 

if w[1] == 0: 

w.append(0.0) 

else: 

w.append(w[0]/w[1]) 

 

w = tuple(w) 

print(('%15.3f'*len(w)) % w) 

print('=' * 70) 

 

 

####################################################################### 

# Dense Benchmarks over ZZ 

####################################################################### 

 

def report_ZZ(**kwds): 

""" 

Reports all the benchmarks for integer matrices and few 

rational matrices. 

 

INPUT: 

 

- ``**kwds`` - passed through to :func:`report` 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: print("starting"); import sys; sys.stdout.flush(); b.report_ZZ(systems=['sage']) # long time (15s on sage.math, 2012) 

starting... 

====================================================================== 

Dense benchmarks over ZZ 

====================================================================== 

... 

====================================================================== 

""" 

F = [vecmat_ZZ, rank_ZZ, rank2_ZZ, charpoly_ZZ, smithform_ZZ, 

det_ZZ, det_QQ, matrix_multiply_ZZ, matrix_add_ZZ, 

matrix_add_ZZ_2, 

nullspace_ZZ] 

 

title = 'Dense benchmarks over ZZ' 

report(F, title, **kwds) 

 

# Integer Nullspace 

 

def nullspace_ZZ(n=200, min=0, max=2**32, system='sage'): 

""" 

Nullspace over ZZ: 

Given a n+1 x n matrix over ZZ with random entries 

between min and max, compute the nullspace. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``200``) 

- ``min`` - minimal value for entries of matrix (default: ``0``) 

- ``max`` - maximal value for entries of matrix (default: ``2**32``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.nullspace_ZZ(200) 

sage: tm = b.nullspace_ZZ(200, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(ZZ, n+1, n, x=min, y=max+1).change_ring(QQ) 

t = cputime() 

v = A.kernel() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := RMatrixSpace(RationalField(), n+1,n)![Random(%s,%s) : i in [1..n*(n+1)]]; 

t := Cputime(); 

K := Kernel(A); 

s := Cputime(t); 

"""%(n,min,max) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

def charpoly_ZZ(n=100, min=0, max=9, system='sage'): 

""" 

Characteristic polynomial over ZZ: 

Given a n x n matrix over ZZ with random entries between min and 

max, compute the charpoly. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``100``) 

- ``min`` - minimal value for entries of matrix (default: ``0``) 

- ``max`` - maximal value for entries of matrix (default: ``9``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.charpoly_ZZ(100) 

sage: tm = b.charpoly_ZZ(100, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(ZZ, n, n, x=min, y=max+1) 

t = cputime() 

v = A.charpoly() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := MatrixAlgebra(IntegerRing(), n)![Random(%s,%s) : i in [1..n^2]]; 

t := Cputime(); 

K := CharacteristicPolynomial(A); 

s := Cputime(t); 

"""%(n,min,max) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

def rank_ZZ(n=700, min=0, max=9, system='sage'): 

""" 

Rank over ZZ: 

Given a n x (n+10) matrix over ZZ with random entries 

between min and max, compute the rank. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``700``) 

- ``min`` - minimal value for entries of matrix (default: ``0``) 

- ``max`` - maximal value for entries of matrix (default: ``9``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.rank_ZZ(300) 

sage: tm = b.rank_ZZ(300, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(ZZ, n, n+10, x=min, y=max+1) 

t = cputime() 

v = A.rank() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := RMatrixSpace(IntegerRing(), n, n+10)![Random(%s,%s) : i in [1..n*(n+10)]]; 

t := Cputime(); 

K := Rank(A); 

s := Cputime(t); 

"""%(n,min,max) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

def rank2_ZZ(n=400, min=0, max=2**64, system='sage'): 

""" 

Rank 2 over ZZ: 

Given a (n + 10) x n matrix over ZZ with random entries 

between min and max, compute the rank. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``400``) 

- ``min`` - minimal value for entries of matrix (default: ``0``) 

- ``max`` - maximal value for entries of matrix (default: ``2**64``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.rank2_ZZ(300) 

sage: tm = b.rank2_ZZ(300, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(ZZ, n+10, n, x=min, y=max+1) 

t = cputime() 

v = A.rank() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := RMatrixSpace(IntegerRing(), n+10, n)![Random(%s,%s) : i in [1..n*(n+10)]]; 

t := Cputime(); 

K := Rank(A); 

s := Cputime(t); 

"""%(n,min,max) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

# Smith Form 

 

def smithform_ZZ(n=128, min=0, max=9, system='sage'): 

""" 

Smith Form over ZZ: 

Given a n x n matrix over ZZ with random entries 

between min and max, compute the Smith normal form. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``128``) 

- ``min`` - minimal value for entries of matrix (default: ``0``) 

- ``max`` - maximal value for entries of matrix (default: ``9``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.smithform_ZZ(100) 

sage: tm = b.smithform_ZZ(100, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(ZZ, n, n, x=min, y=max+1) 

t = cputime() 

v = A.elementary_divisors() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := MatrixAlgebra(IntegerRing(), n)![Random(%s,%s) : i in [1..n^2]]; 

t := Cputime(); 

K := ElementaryDivisors(A); 

s := Cputime(t); 

"""%(n,min,max) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

def matrix_multiply_ZZ(n=300, min=-9, max=9, system='sage', times=1): 

""" 

Matrix multiplication over ZZ 

Given an n x n matrix A over ZZ with random entries 

between min and max, inclusive, compute A * (A+1). 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``300``) 

- ``min`` - minimal value for entries of matrix (default: ``-9``) 

- ``max`` - maximal value for entries of matrix (default: ``9``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

- ``times`` - number of experiments (default: ``1``) 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.matrix_multiply_ZZ(200) 

sage: tm = b.matrix_multiply_ZZ(200, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(ZZ, n, n, x=min, y=max+1) 

B = A + 1 

t = cputime() 

for z in range(times): 

v = A * B 

return cputime(t)/times 

elif system == 'magma': 

code = """ 

n := %s; 

A := MatrixAlgebra(IntegerRing(), n)![Random(%s,%s) : i in [1..n^2]]; 

B := A + 1; 

t := Cputime(); 

for z in [1..%s] do 

K := A * B; 

end for; 

s := Cputime(t); 

"""%(n,min,max,times) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s'))/times 

else: 

raise ValueError('unknown system "%s"'%system) 

 

def matrix_add_ZZ(n=200, min=-9, max=9, system='sage', times=50): 

""" 

Matrix addition over ZZ 

Given an n x n matrix A and B over ZZ with random entries between 

``min`` and ``max``, inclusive, compute A + B ``times`` times. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``200``) 

- ``min`` - minimal value for entries of matrix (default: ``-9``) 

- ``max`` - maximal value for entries of matrix (default: ``9``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

- ``times`` - number of experiments (default: ``50``) 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.matrix_add_ZZ(200) 

sage: tm = b.matrix_add_ZZ(200, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(ZZ, n, n, x=min, y=max+1) 

B = random_matrix(ZZ, n, n, x=min, y=max+1) 

t = cputime() 

for z in range(times): 

v = A + B 

return cputime(t)/times 

elif system == 'magma': 

code = """ 

n := %s; 

min := %s; 

max := %s; 

A := MatrixAlgebra(IntegerRing(), n)![Random(min,max) : i in [1..n^2]]; 

B := MatrixAlgebra(IntegerRing(), n)![Random(min,max) : i in [1..n^2]]; 

t := Cputime(); 

for z in [1..%s] do 

K := A + B; 

end for; 

s := Cputime(t); 

"""%(n,min,max,times) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s'))/times 

else: 

raise ValueError('unknown system "%s"'%system) 

 

def matrix_add_ZZ_2(n=200, bits=16, system='sage', times=50): 

""" 

Matrix addition over ZZ. 

Given an n x n matrix A and B over ZZ with random ``bits``-bit 

entries, compute A + B. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``200``) 

- ``bits`` - bitsize of entries 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

- ``times`` - number of experiments (default: ``50``) 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.matrix_add_ZZ_2(200) 

sage: tm = b.matrix_add_ZZ_2(200, system='magma') # optional - magma 

""" 

b = 2**bits 

return matrix_add_ZZ(n=n, min=-b, max=b,system=system, times=times) 

 

def det_ZZ(n=200, min=1, max=100, system='sage'): 

""" 

Dense integer determinant over ZZ. 

Given an n x n matrix A over ZZ with random entries 

between min and max, inclusive, compute det(A). 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``200``) 

- ``min`` - minimal value for entries of matrix (default: ``1``) 

- ``max`` - maximal value for entries of matrix (default: ``100``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.det_ZZ(200) 

sage: tm = b.det_ZZ(200, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(ZZ, n, n, x=min, y=max+1) 

t = cputime() 

d = A.determinant() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := MatrixAlgebra(IntegerRing(), n)![Random(%s,%s) : i in [1..n^2]]; 

t := Cputime(); 

d := Determinant(A); 

s := Cputime(t); 

"""%(n,min,max) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

def det_QQ(n=300, num_bound=10, den_bound=10, system='sage'): 

""" 

Dense rational determinant over QQ. 

Given an n x n matrix A over QQ with random entries 

with numerator bound and denominator bound, compute det(A). 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``200``) 

- ``num_bound`` - numerator bound, inclusive (default: ``10``) 

- ``den_bound`` - denominator bound, inclusive (default: ``10``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.det_QQ(200) 

sage: ts = b.det_QQ(10, num_bound=100000, den_bound=10000) 

sage: tm = b.det_QQ(200, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(QQ, n, n, num_bound=num_bound, den_bound=den_bound) 

t = cputime() 

d = A.determinant() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := MatrixAlgebra(RationalField(), n)![Random(%s,%s)/Random(1,%s) : i in [1..n^2]]; 

t := Cputime(); 

d := Determinant(A); 

s := Cputime(t); 

"""%(n,-num_bound, num_bound, den_bound) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

def vecmat_ZZ(n=300, min=-9, max=9, system='sage', times=200): 

""" 

Vector matrix multiplication over ZZ. 

 

Given an n x n matrix A over ZZ with random entries 

between min and max, inclusive, and v the first row of A, 

compute the product v * A. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``300``) 

- ``min`` - minimal value for entries of matrix (default: ``-9``) 

- ``max`` - maximal value for entries of matrix (default: ``9``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

- ``times`` - number of runs (default: ``200``) 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.vecmat_ZZ(300) # long time 

sage: tm = b.vecmat_ZZ(300, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(ZZ, n, n, x=min, y=max+1) 

v = A.row(0) 

t = cputime() 

for z in range(times): 

w = v * A 

return cputime(t)/times 

elif system == 'magma': 

code = """ 

n := %s; 

A := MatrixAlgebra(IntegerRing(), n)![Random(%s,%s) : i in [1..n^2]]; 

v := A[1]; 

t := Cputime(); 

for z in [1..%s] do 

K := v * A; 

end for; 

s := Cputime(t); 

"""%(n,min,max,times) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s'))/times 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

 

####################################################################### 

# Dense Benchmarks over GF(p), for small p. 

####################################################################### 

 

def report_GF(p=16411, **kwds): 

""" 

Runs all the reports for finite field matrix operations, for 

prime p=16411. 

 

INPUT: 

 

- ``p`` - ignored 

- ``**kwds`` - passed through to :func:`report` 

 

.. note:: 

 

right now, even though p is an input, it is being ignored! If 

you need to check the performance for other primes, you can 

call individual benchmark functions. 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: print("starting"); import sys; sys.stdout.flush(); b.report_GF(systems=['sage']) 

starting... 

====================================================================== 

Dense benchmarks over GF with prime 16411 

====================================================================== 

... 

====================================================================== 

""" 

F = [rank_GF, rank2_GF, nullspace_GF, charpoly_GF, 

matrix_multiply_GF, det_GF] 

title = 'Dense benchmarks over GF with prime %i' % p 

report(F, title, **kwds) 

 

# Nullspace over GF 

 

def nullspace_GF(n=300, p=16411, system='sage'): 

""" 

Given a n+1 x n matrix over GF(p) with random 

entries, compute the nullspace. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: 300) 

- ``p`` - prime number (default: ``16411``) 

- ``system`` - either 'magma' or 'sage' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.nullspace_GF(300) 

sage: tm = b.nullspace_GF(300, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(GF(p), n, n+1) 

t = cputime() 

v = A.kernel() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := Random(RMatrixSpace(GF(%s), n, n+1)); 

t := Cputime(); 

K := Kernel(A); 

s := Cputime(t); 

"""%(n,p) 

if verbose: print(code) 

magma.eval(code) 

return magma.eval('s') 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

# Characteristic Polynomial over GF 

 

def charpoly_GF(n=100, p=16411, system='sage'): 

""" 

Given a n x n matrix over GF with random entries, compute the 

charpoly. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: 100) 

- ``p`` - prime number (default: ``16411``) 

- ``system`` - either 'magma' or 'sage' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.charpoly_GF(100) 

sage: tm = b.charpoly_GF(100, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(GF(p), n, n) 

t = cputime() 

v = A.charpoly() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := Random(MatrixAlgebra(GF(%s), n)); 

t := Cputime(); 

K := CharacteristicPolynomial(A); 

s := Cputime(t); 

"""%(n,p) 

if verbose: print(code) 

magma.eval(code) 

return magma.eval('s') 

else: 

raise ValueError('unknown system "%s"'%system) 

 

def matrix_add_GF(n=1000, p=16411, system='sage',times=100): 

""" 

Given two n x n matrix over GF(p) with random entries, add them. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: 300) 

- ``p`` - prime number (default: ``16411``) 

- ``system`` - either 'magma' or 'sage' (default: 'sage') 

- ``times`` - number of experiments (default: ``100``) 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.matrix_add_GF(500, p=19) 

sage: tm = b.matrix_add_GF(500, p=19, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(GF(p), n, n) 

B = random_matrix(GF(p), n, n) 

t = cputime() 

for n in range(times): 

v = A + B 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := Random(MatrixAlgebra(GF(%s), n)); 

B := Random(MatrixAlgebra(GF(%s), n)); 

t := Cputime(); 

for z in [1..%s] do 

K := A + B; 

end for; 

s := Cputime(t); 

"""%(n,p,p,times) 

if verbose: print(code) 

magma.eval(code) 

return magma.eval('s') 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

 

# Matrix multiplication over GF(p) 

 

def matrix_multiply_GF(n=100, p=16411, system='sage', times=3): 

""" 

Given an n x n matrix A over GF(p) with random entries, compute 

A * (A+1). 

 

INPUT: 

 

- ``n`` - matrix dimension (default: 100) 

- ``p`` - prime number (default: ``16411``) 

- ``system`` - either 'magma' or 'sage' (default: 'sage') 

- ``times`` - number of experiments (default: ``3``) 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.matrix_multiply_GF(100, p=19) 

sage: tm = b.matrix_multiply_GF(100, p=19, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(GF(p), n) 

B = A + 1 

t = cputime() 

for n in range(times): 

v = A * B 

return cputime(t) / times 

elif system == 'magma': 

code = """ 

n := %s; 

A := Random(MatrixAlgebra(GF(%s), n)); 

B := A + 1; 

t := Cputime(); 

for z in [1..%s] do 

K := A * B; 

end for; 

s := Cputime(t); 

"""%(n,p,times) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s'))/times 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

def rank_GF(n=500, p=16411, system='sage'): 

""" 

Rank over GF(p): 

Given a n x (n+10) matrix over GF(p) with random entries, compute the rank. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: 300) 

- ``p`` - prime number (default: ``16411``) 

- ``system`` - either 'magma' or 'sage' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.rank_GF(1000) 

sage: tm = b.rank_GF(1000, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(GF(p), n, n+10) 

t = cputime() 

v = A.rank() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := Random(MatrixAlgebra(GF(%s), n)); 

t := Cputime(); 

K := Rank(A); 

s := Cputime(t); 

"""%(n,p) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

def rank2_GF(n=500, p=16411, system='sage'): 

""" 

Rank over GF(p): Given a (n + 10) x n matrix over GF(p) with 

random entries, compute the rank. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: 300) 

- ``p`` - prime number (default: ``16411``) 

- ``system`` - either 'magma' or 'sage' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.rank2_GF(500) 

sage: tm = b.rank2_GF(500, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(GF(p), n+10, n) 

t = cputime() 

v = A.rank() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := Random(MatrixAlgebra(GF(%s), n)); 

t := Cputime(); 

K := Rank(A); 

s := Cputime(t); 

"""%(n,p) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

def det_GF(n=400, p=16411 , system='sage'): 

""" 

Dense determinant over GF(p). 

Given an n x n matrix A over GF with random entries compute 

det(A). 

 

INPUT: 

 

- ``n`` - matrix dimension (default: 300) 

- ``p`` - prime number (default: ``16411``) 

- ``system`` - either 'magma' or 'sage' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.det_GF(1000) 

sage: tm = b.det_GF(1000, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(GF(p), n, n) 

t = cputime() 

d = A.determinant() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := Random(MatrixAlgebra(GF(%s), n)); 

t := Cputime(); 

d := Determinant(A); 

s := Cputime(t); 

"""%(n,p) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

####################################################################### 

# Dense Benchmarks over QQ 

####################################################################### 

 

def hilbert_matrix(n): 

""" 

Returns the Hilbert matrix of size n over rationals. 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: b.hilbert_matrix(3) 

[ 1 1/2 1/3] 

[1/2 1/3 1/4] 

[1/3 1/4 1/5] 

""" 

A = Matrix(QQ,n,n) 

for i in range(A.nrows()): 

for j in range(A.ncols()): 

A[i,j] = QQ(1)/((i+1)+(j+1)-1) 

return A 

 

# Reduced row echelon form over QQ 

 

def echelon_QQ(n=100, min=0, max=9, system='sage'): 

""" 

Given a n x (2*n) matrix over QQ with random integer entries 

between min and max, compute the reduced row echelon form. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``300``) 

- ``min`` - minimal value for entries of matrix (default: ``-9``) 

- ``max`` - maximal value for entries of matrix (default: ``9``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.echelon_QQ(100) 

sage: tm = b.echelon_QQ(100, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(ZZ, n, 2*n, x=min, y=max+1).change_ring(QQ) 

t = cputime() 

v = A.echelon_form() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := RMatrixSpace(RationalField(), n, 2*n)![Random(%s,%s) : i in [1..n*2*n]]; 

t := Cputime(); 

K := EchelonForm(A); 

s := Cputime(t); 

"""%(n,min,max) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

# Invert a matrix over QQ. 

 

def inverse_QQ(n=100, min=0, max=9, system='sage'): 

""" 

Given a n x n matrix over QQ with random integer entries 

between min and max, compute the reduced row echelon form. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``300``) 

- ``min`` - minimal value for entries of matrix (default: ``-9``) 

- ``max`` - maximal value for entries of matrix (default: ``9``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.inverse_QQ(100) 

sage: tm = b.inverse_QQ(100, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(ZZ, n, n, x=min, y=max+1).change_ring(QQ) 

t = cputime() 

v = ~A 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := MatrixAlgebra(RationalField(), n)![Random(%s,%s) : i in [1..n*n]]; 

t := Cputime(); 

K := A^(-1); 

s := Cputime(t); 

"""%(n,min,max) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

# Matrix multiplication over QQ 

def matrix_multiply_QQ(n=100, bnd=2, system='sage', times=1): 

""" 

Given an n x n matrix A over QQ with random entries 

whose numerators and denominators are bounded by bnd, 

compute A * (A+1). 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``300``) 

- ``bnd`` - numerator and denominator bound (default: ``bnd``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

- ``times`` - number of experiments (default: ``1``) 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.matrix_multiply_QQ(100) 

sage: tm = b.matrix_multiply_QQ(100, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = random_matrix(QQ, n, n, num_bound=bnd, den_bound=bnd) 

B = A + 1 

t = cputime() 

for z in range(times): 

v = A * B 

return cputime(t)/times 

elif system == 'magma': 

A = magma(random_matrix(QQ, n, n, num_bound=bnd, den_bound=bnd)) 

code = """ 

n := %s; 

A := %s; 

B := A + 1; 

t := Cputime(); 

for z in [1..%s] do 

K := A * B; 

end for; 

s := Cputime(t); 

"""%(n, A.name(), times) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s'))/times 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

# Determinant of Hilbert matrix 

def det_hilbert_QQ(n=80, system='sage'): 

""" 

Runs the benchmark for calculating the determinant of the hilbert 

matrix over rationals of dimension n. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``300``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.det_hilbert_QQ(50) 

sage: tm = b.det_hilbert_QQ(50, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = hilbert_matrix(n) 

t = cputime() 

d = A.determinant() 

return cputime(t) 

elif system == 'magma': 

code = """ 

h := HilbertMatrix(%s); 

tinit := Cputime(); 

d := Determinant(h); 

s := Cputime(tinit); 

delete h; 

"""%n 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

 

# inverse of Hilbert matrix 

def invert_hilbert_QQ(n=40, system='sage'): 

""" 

Runs the benchmark for calculating the inverse of the hilbert 

matrix over rationals of dimension n. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``300``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.invert_hilbert_QQ(30) 

sage: tm = b.invert_hilbert_QQ(30, system='magma') # optional - magma 

""" 

if system == 'sage': 

A = hilbert_matrix(n) 

t = cputime() 

d = A**(-1) 

return cputime(t) 

elif system == 'magma': 

code = """ 

h := HilbertMatrix(%s); 

tinit := Cputime(); 

d := h^(-1); 

s := Cputime(tinit); 

delete h; 

"""%n 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

 

def MatrixVector_QQ(n=1000,h=100,system='sage',times=1): 

""" 

Compute product of square ``n`` matrix by random vector with num and 

denom bounded by ``h`` the given number of ``times``. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``300``) 

- ``h`` - numerator and denominator bound (default: ``bnd``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

- ``times`` - number of experiments (default: ``1``) 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.MatrixVector_QQ(500) 

sage: tm = b.MatrixVector_QQ(500, system='magma') # optional - magma 

""" 

if system=='sage': 

V=QQ**n 

v=V.random_element(h) 

M=random_matrix(QQ,n) 

t=cputime() 

for i in range(times): 

w=M*v 

return cputime(t) 

elif system == 'magma': 

code = """ 

n:=%s; 

h:=%s; 

times:=%s; 

v:=VectorSpace(RationalField(),n)![Random(h)/(Random(h)+1) : i in [1..n]]; 

M:=MatrixAlgebra(RationalField(),n)![Random(h)/(Random(h)+1) : i in [1..n^2]]; 

t := Cputime(); 

for z in [1..times] do 

W:=v*M; 

end for; 

s := Cputime(t); 

"""%(n,h,times) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

####################################################################### 

# Dense Benchmarks over machine reals 

# Note that the precision in reals for MAGMA is base 10, while in 

# sage it is in base 2 

####################################################################### 

 

# Real Nullspace 

 

def nullspace_RR(n=300, min=0, max=10, system='sage'): 

""" 

Nullspace over RR: 

Given a n+1 x n matrix over RR with random entries 

between min and max, compute the nullspace. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``300``) 

- ``min`` - minimal value for entries of matrix (default: ``0``) 

- ``max`` - maximal value for entries of matrix (default: ``10``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.nullspace_RR(100) 

sage: tm = b.nullspace_RR(100, system='magma') # optional - magma 

""" 

if system == 'sage': 

from sage.rings.real_mpfr import RR 

A = random_matrix(ZZ, n+1, n, x=min, y=max+1).change_ring(RR) 

t = cputime() 

v = A.kernel() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := RMatrixSpace(RealField(16), n+1,n)![Random(%s,%s) : i in [1..n*(n+1)]]; 

t := Cputime(); 

K := Kernel(A); 

s := Cputime(t); 

"""%(n,min,max) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system) 

 

 

def nullspace_RDF(n=300, min=0, max=10, system='sage'): 

""" 

Nullspace over RDF: 

Given a n+1 x n matrix over RDF with random entries 

between min and max, compute the nullspace. 

 

INPUT: 

 

- ``n`` - matrix dimension (default: ``300``) 

- ``min`` - minimal value for entries of matrix (default: ``0``) 

- ``max`` - maximal value for entries of matrix (default: `10``) 

- ``system`` - either 'sage' or 'magma' (default: 'sage') 

 

EXAMPLES:: 

 

sage: import sage.matrix.benchmark as b 

sage: ts = b.nullspace_RDF(100) # long time 

sage: tm = b.nullspace_RDF(100, system='magma') # optional - magma 

""" 

if system == 'sage': 

from sage.rings.real_double import RDF 

A = random_matrix(ZZ, n+1, n, x=min, y=max+1).change_ring(RDF) 

t = cputime() 

v = A.kernel() 

return cputime(t) 

elif system == 'magma': 

code = """ 

n := %s; 

A := RMatrixSpace(RealField(16), n+1,n)![Random(%s,%s) : i in [1..n*(n+1)]]; 

t := Cputime(); 

K := Kernel(A); 

s := Cputime(t); 

"""%(n,min,max) 

if verbose: print(code) 

magma.eval(code) 

return float(magma.eval('s')) 

else: 

raise ValueError('unknown system "%s"'%system)