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

""" 

Local Normal Form 

""" 

 

#***************************************************************************** 

# Copyright (C) 2007 William Stein and Jonathan Hanke 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

import copy 

from sage.rings.infinity import Infinity 

from sage.rings.integer_ring import IntegerRing, ZZ 

from sage.rings.rational_field import QQ 

from sage.arith.all import GCD, valuation, is_prime 

 

 

def find_entry_with_minimal_scale_at_prime(self, p): 

""" 

Finds the entry of the quadratic form with minimal scale at the 

prime p, preferring diagonal entries in case of a tie. (I.e. If 

we write the quadratic form as a symmetric matrix M, then this 

entry M[i,j] has the minimal valuation at the prime p.) 

 

Note: This answer is independent of the kind of matrix (Gram or 

Hessian) associated to the form. 

 

INPUT: 

 

`p` -- a prime number > 0 

 

OUTPUT: 

 

a pair of integers >= 0 

 

EXAMPLES:: 

 

sage: Q = QuadraticForm(ZZ, 2, [6, 2, 20]); Q 

Quadratic form in 2 variables over Integer Ring with coefficients: 

[ 6 2 ] 

[ * 20 ] 

sage: Q.find_entry_with_minimal_scale_at_prime(2) 

(0, 1) 

sage: Q.find_entry_with_minimal_scale_at_prime(3) 

(1, 1) 

sage: Q.find_entry_with_minimal_scale_at_prime(5) 

(0, 0) 

 

""" 

n = self.dim() 

min_val = Infinity 

ij_index = None 

val_2 = valuation(2, p) 

for d in range(n): ## d = difference j-i 

for e in range(n - d): ## e is the length of the diagonal with value d. 

 

## Compute the valuation of the entry 

if d == 0: 

tmp_val = valuation(self[e, e+d], p) 

else: 

tmp_val = valuation(self[e, e+d], p) - val_2 

 

## Check if it's any smaller than what we have 

if tmp_val < min_val: 

ij_index = (e,e+d) 

min_val = tmp_val 

 

## Return the result 

return ij_index 

 

 

 

 

def local_normal_form(self, p): 

""" 

Returns the a locally integrally equivalent quadratic form over 

the p-adic integers Z_p which gives the Jordan decomposition. The 

Jordan components are written as sums of blocks of size <= 2 and 

are arranged by increasing scale, and then by increasing norm. 

(This is equivalent to saying that we put the 1x1 blocks before 

the 2x2 blocks in each Jordan component.) 

 

INPUT: 

 

`p` -- a positive prime number. 

 

OUTPUT: 

 

a quadratic form over ZZ 

 

WARNING: Currently this only works for quadratic forms defined over ZZ. 

 

EXAMPLES:: 

 

sage: Q = QuadraticForm(ZZ, 2, [10,4,1]) 

sage: Q.local_normal_form(5) 

Quadratic form in 2 variables over Integer Ring with coefficients: 

[ 1 0 ] 

[ * 6 ] 

 

:: 

 

sage: Q.local_normal_form(3) 

Quadratic form in 2 variables over Integer Ring with coefficients: 

[ 10 0 ] 

[ * 15 ] 

 

sage: Q.local_normal_form(2) 

Quadratic form in 2 variables over Integer Ring with coefficients: 

[ 1 0 ] 

[ * 6 ] 

 

""" 

## Sanity Checks 

if (self.base_ring() != IntegerRing()): 

raise NotImplementedError("Oops! This currently only works for quadratic forms defined over IntegerRing(). =(") 

if not ((p>=2) and is_prime(p)): 

raise TypeError("Oops! p is not a positive prime number. =(") 

 

## Some useful local variables 

Q = copy.deepcopy(self) 

Q.__init__(self.base_ring(), self.dim(), self.coefficients()) 

 

## Prepare the final form to return 

Q_Jordan = copy.deepcopy(self) 

Q_Jordan.__init__(self.base_ring(), 0) 

 

 

while Q.dim() > 0: 

n = Q.dim() 

 

## Step 1: Find the minimally p-divisible matrix entry, preferring diagonals 

## ------------------------------------------------------------------------- 

(min_i, min_j) = Q.find_entry_with_minimal_scale_at_prime(p) 

if min_i == min_j: 

min_val = valuation(2 * Q[min_i, min_j], p) 

else: 

min_val = valuation(Q[min_i, min_j], p) 

 

## Error if we still haven't seen non-zero coefficients! 

if (min_val == Infinity): 

raise RuntimeError("Oops! The original matrix is degenerate. =(") 

 

 

## Step 2: Arrange for the upper leftmost entry to have minimal valuation 

## ---------------------------------------------------------------------- 

if (min_i == min_j): 

block_size = 1 

Q.swap_variables(0, min_i, in_place = True) 

else: 

## Work in the upper-left 2x2 block, and replace it by its 2-adic equivalent form 

Q.swap_variables(0, min_i, in_place = True) 

Q.swap_variables(1, min_j, in_place = True) 

 

## 1x1 => make upper left the smallest 

if (p != 2): 

block_size = 1; 

Q.add_symmetric(1, 0, 1, in_place = True) 

## 2x2 => replace it with the appropriate 2x2 matrix 

else: 

block_size = 2 

 

## DIAGNOSTIC 

#print "\n Finished Step 2 \n"; 

#print "\n Q is: \n" + str(Q) + "\n"; 

#print " p is: " + str(p) 

#print " min_val is: " + str( min_val) 

#print " block_size is: " + str(block_size) 

#print "\n Starting Step 3 \n" 

 

## Step 3: Clear out the remaining entries 

## --------------------------------------- 

min_scale = p ** min_val ## This is the minimal valuation of the Hessian matrix entries. 

 

##DIAGNOSTIC 

#print "Starting Step 3:" 

#print "----------------" 

#print " min_scale is: " + str(min_scale) 

 

 

## Perform cancellation over Z by ensuring divisibility 

if (block_size == 1): 

a = 2 * Q[0,0] 

for j in range(block_size, n): 

b = Q[0, j] 

g = GCD(a, b) 

 

## DIAGNSOTIC 

#print "Cancelling from a 1x1 block:" 

#print "----------------------------" 

#print " Cancelling entry with index (" + str(upper_left) + ", " + str(j) + ")" 

#print " entry = " + str(b) 

#print " gcd = " + str(g) 

#print " a = " + str(a) 

#print " b = " + str(b) 

#print " a/g = " + str(a/g) + " (used for stretching)" 

#print " -b/g = " + str(-b/g) + " (used for cancelling)" 

 

## Sanity Check: a/g is a p-unit 

if valuation (g, p) != valuation(a, p): 

raise RuntimeError("Oops! We have a problem with our rescaling not preserving p-integrality!") 

 

Q.multiply_variable(ZZ(a/g), j, in_place = True) ## Ensures that the new b entry is divisible by a 

Q.add_symmetric(ZZ(-b/g), j, 0, in_place = True) ## Performs the cancellation 

 

 

elif (block_size == 2): 

a1 = 2 * Q[0,0] 

a2 = Q[0, 1] 

b1 = Q[1, 0] ## This is the same as a2 

b2 = 2 * Q[1, 1] 

 

big_det = (a1*b2 - a2*b1) 

small_det = big_det / (min_scale * min_scale) 

 

## Cancels out the rows/columns of the 2x2 block 

for j in range(block_size, n): 

a = Q[0, j] 

b = Q[1, j] 

 

## Ensures an integral result (scale jth row/column by big_det) 

Q.multiply_variable(big_det, j, in_place = True) 

 

## Performs the cancellation (by producing -big_det * jth row/column) 

Q.add_symmetric(ZZ(-(a*b2 - b*a2)), j, 0, in_place = True) 

Q.add_symmetric(ZZ(-(-a*b1 + b*a1)), j, 1, in_place = True) 

 

## Now remove the extra factor (non p-unit factor) in big_det we introduced above 

Q.divide_variable(ZZ(min_scale * min_scale), j, in_place = True) 

 

## DIAGNOSTIC 

#print "Cancelling out a 2x2 block:" 

#print "---------------------------" 

#print " a1 = " + str(a1) 

#print " a2 = " + str(a2) 

#print " b1 = " + str(b1) 

#print " b2 = " + str(b2) 

#print " big_det = " + str(big_det) 

#print " min_scale = " + str(min_scale) 

#print " small_det = " + str(small_det) 

#print " Q = \n", Q 

 

## Uses Cassels's proof to replace the remaining 2 x 2 block 

if (((1 + small_det) % 8) == 0): 

Q[0, 0] = 0 

Q[1, 1] = 0 

Q[0, 1] = min_scale 

elif (((5 + small_det) % 8) == 0): 

Q[0, 0] = min_scale 

Q[1, 1] = min_scale 

Q[0, 1] = min_scale 

else: 

raise RuntimeError("Error in LocalNormal: Impossible behavior for a 2x2 block! \n") 

 

 

## Check that the cancellation worked, extract the upper-left block, and trim Q to handle the next block. 

for i in range(block_size): 

for j in range(block_size, n): 

if Q[i,j] != 0: 

raise RuntimeError("Oops! The cancellation didn't work properly at entry (" + str(i) + ", " + str(j) + ").") 

Q_Jordan = Q_Jordan + Q.extract_variables(range(block_size)) 

Q = Q.extract_variables(range(block_size, n)) 

 

return Q_Jordan 

 

 

def jordan_blocks_by_scale_and_unimodular(self, p, safe_flag=True): 

""" 

Return a list of pairs `(s_i, L_i)` where `L_i` is a maximal 

`p^{s_i}`-unimodular Jordan component which is further decomposed into 

block diagonals of block size `\le 2`. 

 

For each `L_i` the 2x2 blocks are listed after the 1x1 blocks 

(which follows from the convention of the 

:meth:`local_normal_form` method). 

 

.. NOTE:: 

 

The decomposition of each `L_i` into smaller blocks is not unique! 

 

The ``safe_flag`` argument allows us to select whether we want a copy of 

the output, or the original output. By default ``safe_flag = True``, so we 

return a copy of the cached information. If this is set to ``False``, then 

the routine is much faster but the return values are vulnerable to being 

corrupted by the user. 

 

INPUT: 

 

- `p` -- a prime number > 0. 

 

OUTPUT: 

 

A list of pairs `(s_i, L_i)` where: 

 

- `s_i` is an integer, 

- `L_i` is a block-diagonal unimodular quadratic form over `\ZZ_p`. 

 

.. note:: 

 

These forms `L_i` are defined over the `p`-adic integers, but by a 

matrix over `\ZZ` (or `\QQ`?). 

 

EXAMPLES:: 

 

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

sage: Q.jordan_blocks_by_scale_and_unimodular(3) 

[(0, Quadratic form in 3 variables over Integer Ring with coefficients: 

[ 1 0 0 ] 

[ * 5 0 ] 

[ * * 7 ]), (2, Quadratic form in 1 variables over Integer Ring with coefficients: 

[ 1 ])] 

 

:: 

 

sage: Q2 = QuadraticForm(ZZ, 2, [1,1,1]) 

sage: Q2.jordan_blocks_by_scale_and_unimodular(2) 

[(-1, Quadratic form in 2 variables over Integer Ring with coefficients: 

[ 2 2 ] 

[ * 2 ])] 

sage: Q = Q2 + Q2.scale_by_factor(2) 

sage: Q.jordan_blocks_by_scale_and_unimodular(2) 

[(-1, Quadratic form in 2 variables over Integer Ring with coefficients: 

[ 2 2 ] 

[ * 2 ]), (0, Quadratic form in 2 variables over Integer Ring with coefficients: 

[ 2 2 ] 

[ * 2 ])] 

""" 

## Try to use the cached result 

try: 

if safe_flag: 

return copy.deepcopy(self.__jordan_blocks_by_scale_and_unimodular_dict[p]) 

else: 

return self.__jordan_blocks_by_scale_and_unimodular_dict[p] 

except Exception: 

## Initialize the global dictionary if it doesn't exist 

if not hasattr(self, '__jordan_blocks_by_scale_and_unimodular_dict'): 

self.__jordan_blocks_by_scale_and_unimodular_dict = {} 

 

 

## Deal with zero dim'l forms 

if self.dim() == 0: 

return [] 

 

 

## Find the Local Normal form of Q at p 

Q1 = self.local_normal_form(p) 

 

 

## Parse this into Jordan Blocks 

n = Q1.dim() 

tmp_Jordan_list = [] 

i = 0 

start_ind = 0 

if (n >= 2) and (Q1[0,1] != 0): 

start_scale = valuation(Q1[0,1], p) - 1 

else: 

start_scale = valuation(Q1[0,0], p) 

 

while (i < n): 

 

## Determine the size of the current block 

if (i == n-1) or (Q1[i,i+1] == 0): 

block_size = 1 

else: 

block_size = 2 

 

## Determine the valuation of the current block 

if block_size == 1: 

block_scale = valuation(Q1[i,i], p) 

else: 

block_scale = valuation(Q1[i,i+1], p) - 1 

 

## Process the previous block if the valuation increased 

if block_scale > start_scale: 

tmp_Jordan_list += [(start_scale, Q1.extract_variables(range(start_ind, i)).scale_by_factor(ZZ(1) / (QQ(p)**(start_scale))))] 

start_ind = i 

start_scale = block_scale 

 

## Increment the index 

i += block_size 

 

## Add the last block 

tmp_Jordan_list += [(start_scale, Q1.extract_variables(range(start_ind, n)).scale_by_factor(ZZ(1) / QQ(p)**(start_scale)))] 

 

 

## Cache the result 

self.__jordan_blocks_by_scale_and_unimodular_dict[p] = tmp_Jordan_list 

 

## Return the result 

return tmp_Jordan_list 

 

 

 

 

def jordan_blocks_in_unimodular_list_by_scale_power(self, p): 

""" 

Returns a list of Jordan components, whose component at index i 

should be scaled by the factor p^i. 

 

This is only defined for integer-valued quadratic forms 

(i.e. forms with base_ring ZZ), and the indexing only works 

correctly for p=2 when the form has an integer Gram matrix. 

 

INPUT: 

 

self -- a quadratic form over ZZ, which has integer Gram matrix if p == 2 

`p` -- a prime number > 0 

 

OUTPUT: 

 

a list of p-unimodular quadratic forms 

 

EXAMPLES:: 

 

sage: Q = QuadraticForm(ZZ, 3, [2, -2, 0, 3, -5, 4]) 

sage: Q.jordan_blocks_in_unimodular_list_by_scale_power(2) 

Traceback (most recent call last): 

... 

TypeError: Oops! The given quadratic form has a Jordan component with a negative scale exponent! 

This routine requires an integer-matrix quadratic form for the output indexing to work properly! 

 

sage: Q.scale_by_factor(2).jordan_blocks_in_unimodular_list_by_scale_power(2) 

[Quadratic form in 2 variables over Integer Ring with coefficients: 

[ 0 2 ] 

[ * 0 ], Quadratic form in 0 variables over Integer Ring with coefficients: 

, Quadratic form in 1 variables over Integer Ring with coefficients: 

[ 345 ]] 

 

sage: Q.jordan_blocks_in_unimodular_list_by_scale_power(3) 

[Quadratic form in 2 variables over Integer Ring with coefficients: 

[ 2 0 ] 

[ * 10 ], Quadratic form in 1 variables over Integer Ring with coefficients: 

[ 2 ]] 

""" 

## Sanity Check 

if self.base_ring() != ZZ: 

raise TypeError("Oops! This method only makes sense for integer-valued quadratic forms (i.e. defined over ZZ).") 

 

## Deal with zero dim'l forms 

if self.dim() == 0: 

return [] 

 

## Find the Jordan Decomposition 

list_of_jordan_pairs = self.jordan_blocks_by_scale_and_unimodular(p) 

scale_list = [P[0] for P in list_of_jordan_pairs] 

s_max = max(scale_list) 

if min(scale_list) < 0: 

raise TypeError("Oops! The given quadratic form has a Jordan component with a negative scale exponent!\n" \ 

+ "This routine requires an integer-matrix quadratic form for the output indexing to work properly!") 

 

## Make the new list of unimodular Jordan components 

zero_form = copy.deepcopy(self) 

zero_form.__init__(ZZ, 0) 

list_by_scale = [zero_form for _ in range(s_max+1)] 

for P in list_of_jordan_pairs: 

list_by_scale[P[0]] = P[1] 

 

## Return the new list 

return list_by_scale