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

""" 

Reduction Theory 

""" 

from copy import deepcopy 

from sage.matrix.constructor import matrix 

from sage.functions.all import floor 

from sage.misc.mrange import mrange 

from sage.modules.free_module_element import vector 

from sage.rings.integer_ring import ZZ 

 

 

def reduced_binary_form1(self): 

r""" 

Reduce the form `ax^2 + bxy+cy^2` to satisfy the reduced condition `|b| \le 

a \le c`, with `b \ge 0` if `a = c`. This reduction occurs within the 

proper class, so all transformations are taken to have determinant 1. 

 

EXAMPLES:: 

 

sage: QuadraticForm(ZZ,2,[5,5,2]).reduced_binary_form1() 

( 

Quadratic form in 2 variables over Integer Ring with coefficients: 

[ 2 -1 ] 

[ * 2 ] , 

<BLANKLINE> 

[ 0 -1] 

[ 1 1] 

) 

""" 

if self.dim() != 2: 

raise TypeError("This must be a binary form for now...") 

 

R = self.base_ring() 

interior_reduced_flag = False 

Q = deepcopy(self) 

M = matrix(R, 2, 2, [1,0,0,1]) 

 

while not interior_reduced_flag: 

interior_reduced_flag = True 

 

## Arrange for a <= c 

if Q[0,0] > Q[1,1]: 

M_new = matrix(R,2,2,[0, -1, 1, 0]) 

Q = Q(M_new) 

M = M * M_new 

interior_reduced_flag = False 

#print "A" 

 

## Arrange for |b| <= a 

if abs(Q[0,1]) > Q[0,0]: 

r = R(floor(round(Q[0,1]/(2*Q[0,0])))) 

M_new = matrix(R,2,2,[1, -r, 0, 1]) 

Q = Q(M_new) 

M = M * M_new 

interior_reduced_flag = False 

#print "B" 

 

return Q, M 

 

 

 

 

def reduced_ternary_form__Dickson(self): 

""" 

Find the unique reduced ternary form according to the conditions 

of Dickson's "Studies in the Theory of Numbers", pp164-171. 

 

EXAMPLES:: 

 

sage: Q = DiagonalQuadraticForm(ZZ, [1, 1, 1]) 

sage: Q.reduced_ternary_form__Dickson() 

Traceback (most recent call last): 

... 

NotImplementedError: TO DO 

 

""" 

raise NotImplementedError("TO DO") 

 

 

 

def reduced_binary_form(self): 

""" 

Find a form which is reduced in the sense that no further binary 

form reductions can be done to reduce the original form. 

 

EXAMPLES:: 

 

sage: QuadraticForm(ZZ,2,[5,5,2]).reduced_binary_form() 

( 

Quadratic form in 2 variables over Integer Ring with coefficients: 

[ 2 -1 ] 

[ * 2 ] , 

<BLANKLINE> 

[ 0 -1] 

[ 1 1] 

) 

""" 

R = self.base_ring() 

n = self.dim() 

interior_reduced_flag = False 

Q = deepcopy(self) 

M = matrix(R, n, n) 

for i in range(n): 

M[i,i] = 1 

 

 

while not interior_reduced_flag: 

interior_reduced_flag = True 

 

#print Q 

 

## Arrange for (weakly) increasing diagonal entries 

for i in range(n): 

for j in range(i+1,n): 

if Q[i,i] > Q[j,j]: 

M_new = matrix(R,n,n) 

for k in range(n): 

M_new[k,k] = 1 

M_new[i,j] = -1 

M_new[j,i] = 1 

M_new[i,i] = 0 

M_new[j,j] = 1 

 

Q = Q(M_new) 

M = M * M_new 

interior_reduced_flag = False 

#print "A" 

 

## Arrange for |b| <= a 

if abs(Q[i,j]) > Q[i,i]: 

r = R(floor(round(Q[i,j]/(2*Q[i,i])))) 

 

M_new = matrix(R,n,n) 

for k in range(n): 

M_new[k,k] = 1 

M_new[i,j] = -r 

 

Q = Q(M_new) 

M = M * M_new 

interior_reduced_flag = False 

#print "B" 

 

return Q, M 

 

 

def minkowski_reduction(self): 

""" 

Find a Minkowski-reduced form equivalent to the given one. 

This means that 

 

.. MATH:: 

 

Q(v_k) <= Q(s_1 * v_1 + ... + s_n * v_n) 

 

for all `s_i` where GCD`(s_k, ... s_n) = 1`. 

 

Note: When Q has dim <= 4 we can take all `s_i` in {1, 0, -1}. 

 

References: 

Schulze-Pillot's paper on "An algorithm for computing genera 

of ternary and quaternary quadratic forms", p138. 

Donaldson's 1979 paper "Minkowski Reduction of Integral 

Matrices", p203. 

 

EXAMPLES:: 

 

sage: Q = QuadraticForm(ZZ,4,[30,17,11,12,29,25,62,64,25,110]) 

sage: Q 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 30 17 11 12 ] 

[ * 29 25 62 ] 

[ * * 64 25 ] 

[ * * * 110 ] 

sage: Q.minkowski_reduction() 

( 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 30 17 11 -5 ] 

[ * 29 25 4 ] 

[ * * 64 0 ] 

[ * * * 77 ] , 

<BLANKLINE> 

[ 1 0 0 0] 

[ 0 1 0 -1] 

[ 0 0 1 0] 

[ 0 0 0 1] 

) 

""" 

R = self.base_ring() 

n = self.dim() 

interior_reduced_flag = False 

Q = deepcopy(self) 

M = matrix(R, n, n) 

for i in range(n): 

M[i,i] = 1 

 

 

## Begin the reduction 

done_flag = False 

while not done_flag: 

 

## Loop through possible shorted vectors until 

done_flag = True 

#print " j_range = ", range(n-1, -1, -1) 

for j in range(n-1, -1, -1): 

for a_first in mrange([2 for i in range(j)]): 

y = [x-1 for x in a_first] + [1] + [0 for k in range(n-1-j)] 

e_j = [0 for k in range(n)] 

e_j[j] = 1 

#print "j = ", j 

 

## Reduce if a shorter vector is found 

#print "y = ", y, " e_j = ", e_j, "\n" 

if Q(y) < Q(e_j): 

 

## Create the transformation matrix 

M_new = matrix(R, n, n) 

for k in range(n): 

M_new[k,k] = 1 

for k in range(n): 

M_new[k,j] = y[k] 

 

## Perform the reduction and restart the loop 

#print "Q_before = ", Q 

Q = Q(M_new) 

M = M * M_new 

done_flag = False 

 

## DIAGNOSTIC 

#print "Q(y) = ", Q(y) 

#print "Q(e_j) = ", Q(e_j) 

#print "M_new = ", M_new 

#print "Q_after = ", Q 

#print 

 

if not done_flag: 

break 

 

if not done_flag: 

break 

 

## Return the results 

return Q, M 

 

 

 

 

def minkowski_reduction_for_4vars__SP(self): 

""" 

Find a Minkowski-reduced form equivalent to the given one. 

This means that 

 

Q(`v_k`) <= Q(`s_1 * v_1 + ... + s_n * v_n`) 

 

for all `s_i` where GCD(`s_k, ... s_n`) = 1. 

 

Note: When Q has dim <= 4 we can take all `s_i` in {1, 0, -1}. 

 

References: 

Schulze-Pillot's paper on "An algorithm for computing genera 

of ternary and quaternary quadratic forms", p138. 

Donaldson's 1979 paper "Minkowski Reduction of Integral 

Matrices", p203. 

 

EXAMPLES:: 

 

sage: Q = QuadraticForm(ZZ,4,[30,17,11,12,29,25,62,64,25,110]) 

sage: Q 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 30 17 11 12 ] 

[ * 29 25 62 ] 

[ * * 64 25 ] 

[ * * * 110 ] 

sage: Q.minkowski_reduction_for_4vars__SP() 

( 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 29 -17 25 4 ] 

[ * 30 -11 5 ] 

[ * * 64 0 ] 

[ * * * 77 ] , 

<BLANKLINE> 

[ 0 1 0 0] 

[ 1 0 0 -1] 

[ 0 0 1 0] 

[ 0 0 0 1] 

) 

""" 

R = self.base_ring() 

n = self.dim() 

interior_reduced_flag = False 

Q = deepcopy(self) 

M = matrix(R, n, n) 

for i in range(n): 

M[i,i] = 1 

 

## Only allow 4-variable forms 

if n != 4: 

raise TypeError("Oops! The given quadratic form has " + str(n) + \ 

" != 4 variables. =|") 

 

 

## Step 1: Begin the reduction 

done_flag = False 

while not done_flag: 

 

## Loop through possible shorter vectors 

done_flag = True 

#print " j_range = ", range(n-1, -1, -1) 

for j in range(n-1, -1, -1): 

for a_first in mrange([2 for i in range(j)]): 

y = [x-1 for x in a_first] + [1] + [0 for k in range(n-1-j)] 

e_j = [0 for k in range(n)] 

e_j[j] = 1 

#print "j = ", j 

 

## Reduce if a shorter vector is found 

#print "y = ", y, " e_j = ", e_j, "\n" 

if Q(y) < Q(e_j): 

 

## Further n=4 computations 

B_y_vec = Q.matrix() * vector(ZZ, y) 

## SP's B = our self.matrix()/2 

## SP's A = coeff matrix of his B 

## Here we compute the double of both and compare. 

B_sum = sum([abs(B_y_vec[i]) for i in range(4) if i != j]) 

A_sum = sum([abs(Q[i,j]) for i in range(4) if i != j]) 

B_max = max([abs(B_y_vec[i]) for i in range(4) if i != j]) 

A_max = max([abs(Q[i,j]) for i in range(4) if i != j]) 

 

if (B_sum < A_sum) or ((B_sum == A_sum) and (B_max < A_max)): 

 

## Create the transformation matrix 

M_new = matrix(R, n, n) 

for k in range(n): 

M_new[k,k] = 1 

for k in range(n): 

M_new[k,j] = y[k] 

 

## Perform the reduction and restart the loop 

#print "Q_before = ", Q 

Q = Q(M_new) 

M = M * M_new 

done_flag = False 

 

## DIAGNOSTIC 

#print "Q(y) = ", Q(y) 

#print "Q(e_j) = ", Q(e_j) 

#print "M_new = ", M_new 

#print "Q_after = ", Q 

#print 

 

if not done_flag: 

break 

 

if not done_flag: 

break 

 

## Step 2: Order A by certain criteria 

for i in range(4): 

for j in range(i+1,4): 

 

## Condition (a) 

if (Q[i,i] > Q[j,j]): 

Q.swap_variables(i,j,in_place=True) 

M_new = matrix(R,n,n) 

M_new[i,j] = -1 

M_new[j,i] = 1 

for r in range(4): 

if (r == i) or (r == j): 

M_new[r,r] = 0 

else: 

M_new[r,r] = 1 

M = M * M_new 

 

elif (Q[i,i] == Q[j,j]): 

i_sum = sum([abs(Q[i,k]) for k in range(4) if k != i]) 

j_sum = sum([abs(Q[j,k]) for k in range(4) if k != j]) 

 

## Condition (b) 

if (i_sum > j_sum): 

Q.swap_variables(i,j,in_place=True) 

M_new = matrix(R,n,n) 

M_new[i,j] = -1 

M_new[j,i] = 1 

for r in range(4): 

if (r == i) or (r == j): 

M_new[r,r] = 0 

else: 

M_new[r,r] = 1 

M = M * M_new 

 

elif (i_sum == j_sum): 

for k in [2,1,0]: ## TO DO: These steps are a little redundant... 

Q1 = Q.matrix() 

 

c_flag = True 

for l in range(k+1,4): 

c_flag = c_flag and (abs(Q1[i,l]) == abs(Q1[j,l])) 

 

## Condition (c) 

if c_flag and (abs(Q1[i,k]) > abs(Q1[j,k])): 

Q.swap_variables(i,j,in_place=True) 

M_new = matrix(R,n,n) 

M_new[i,j] = -1 

M_new[j,i] = 1 

for r in range(4): 

if (r == i) or (r == j): 

M_new[r,r] = 0 

else: 

M_new[r,r] = 1 

M = M * M_new 

 

 

## Step 3: Order the signs 

for i in range(4): 

if Q[i,3] < 0: 

Q.multiply_variable(-1, i, in_place=True) 

M_new = matrix(R,n,n) 

for r in range(4): 

if r == i: 

M_new[r,r] = -1 

else: 

M_new[r,r] = 1 

M = M * M_new 

 

for i in range(4): 

j = 3 

while (Q[i,j] == 0): 

j += -1 

if (Q[i,j] < 0): 

Q.multiply_variable(-1, i, in_place=True) 

M_new = matrix(R,n,n) 

for r in range(4): 

if r == i: 

M_new[r,r] = -1 

else: 

M_new[r,r] = 1 

M = M * M_new 

 

if Q[1,2] < 0: 

## Test a row 1 sign change 

if (Q[1,3] <= 0 and \ 

((Q[1,3] < 0) or (Q[1,3] == 0 and Q[1,2] < 0) \ 

or (Q[1,3] == 0 and Q[1,2] == 0 and Q[1,1] < 0))): 

Q.multiply_variable(-1, i, in_place=True) 

M_new = matrix(R,n,n) 

for r in range(4): 

if r == i: 

M_new[r,r] = -1 

else: 

M_new[r,r] = 1 

M = M * M_new 

 

elif (Q[2,3] <= 0 and \ 

((Q[2,3] < 0) or (Q[2,3] == 0 and Q[2,2] < 0) \ 

or (Q[2,3] == 0 and Q[2,2] == 0 and Q[2,1] < 0))): 

Q.multiply_variable(-1, i, in_place=True) 

M_new = matrix(R,n,n) 

for r in range(4): 

if r == i: 

M_new[r,r] = -1 

else: 

M_new[r,r] = 1 

M = M * M_new 

 

 

## Return the results 

return Q, M