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

""" 

Variable Substitution, Multiplication, Division, Scaling 

 

""" 

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

# 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 

 

 

def swap_variables(self, r, s, in_place = False): 

""" 

Switch the variables `x_r` and `x_s` in the quadratic form 

(replacing the original form if the in_place flag is True). 

 

INPUT: 

 

`r`, `s` -- integers >= 0 

 

OUTPUT: 

 

a QuadraticForm (by default, otherwise none) 

 

EXAMPLES:: 

 

sage: Q = QuadraticForm(ZZ, 4, range(1,11)) 

sage: Q 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 1 2 3 4 ] 

[ * 5 6 7 ] 

[ * * 8 9 ] 

[ * * * 10 ] 

 

 

sage: Q.swap_variables(0,2) 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 8 6 3 9 ] 

[ * 5 2 7 ] 

[ * * 1 4 ] 

[ * * * 10 ] 

 

 

sage: Q.swap_variables(0,2).swap_variables(0,2) 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 1 2 3 4 ] 

[ * 5 6 7 ] 

[ * * 8 9 ] 

[ * * * 10 ] 

 

""" 

if not in_place: 

Q = copy.deepcopy(self) 

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

Q.swap_variables(r,s,in_place=True) 

return Q 

 

else: 

## Switch diagonal elements 

tmp = self[r,r] 

self[r,r] = self[s,s] 

self[s,s] = tmp 

 

## Switch off-diagonal elements 

for i in range(self.dim()): 

if (i != r) and (i != s): 

tmp = self[r,i] 

self[r,i] = self[s,i] 

self[s,i] = tmp 

 

 

def multiply_variable(self, c, i, in_place = False): 

""" 

Replace the variables `x_i` by `c*x_i` in the quadratic form 

(replacing the original form if the in_place flag is True). 

 

Here `c` must be an element of the base_ring defining the 

quadratic form. 

 

INPUT: 

 

`c` -- an element of Q.base_ring() 

 

`i` -- an integer >= 0 

 

OUTPUT: 

 

a QuadraticForm (by default, otherwise none) 

 

EXAMPLES:: 

 

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

sage: Q.multiply_variable(5,0) 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 25 0 0 0 ] 

[ * 9 0 0 ] 

[ * * 5 0 ] 

[ * * * 7 ] 

 

""" 

if not in_place: 

Q = copy.deepcopy(self) 

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

Q.multiply_variable(c,i,in_place=True) 

return Q 

 

else: 

## Stretch the diagonal element 

tmp = c * c * self[i,i] 

self[i,i] = tmp 

 

## Switch off-diagonal elements 

for k in range(self.dim()): 

if (k != i): 

tmp = c * self[k,i] 

self[k,i] = tmp 

 

 

def divide_variable(self, c, i, in_place = False): 

""" 

Replace the variables `x_i` by `(x_i)/c` in the quadratic form 

(replacing the original form if the in_place flag is True). 

 

Here `c` must be an element of the base_ring defining the 

quadratic form, and the division must be defined in the base 

ring. 

 

INPUT: 

 

`c` -- an element of Q.base_ring() 

 

`i` -- an integer >= 0 

 

OUTPUT: 

 

a QuadraticForm (by default, otherwise none) 

 

EXAMPLES:: 

 

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

sage: Q.divide_variable(3,1) 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 1 0 0 0 ] 

[ * 1 0 0 ] 

[ * * 5 0 ] 

[ * * * 7 ] 

 

""" 

if not in_place: 

Q = copy.deepcopy(self) 

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

Q.divide_variable(c,i,in_place=True) 

return Q 

 

else: 

## Stretch the diagonal element 

tmp = self[i,i] / (c*c) 

self[i,i] = tmp 

 

## Switch off-diagonal elements 

for k in range(self.dim()): 

if (k != i): 

tmp = self[k,i] / c 

self[k,i] = tmp 

 

 

def scale_by_factor(self, c, change_value_ring_flag=False): 

""" 

Scale the values of the quadratic form by the number `c`, if 

this is possible while still being defined over its base ring. 

 

If the flag is set to true, then this will alter the value ring 

to be the field of fractions of the original ring (if necessary). 

 

INPUT: 

 

`c` -- a scalar in the fraction field of the value ring of the form. 

 

OUTPUT: 

 

A quadratic form of the same dimension 

 

EXAMPLES:: 

 

sage: Q = DiagonalQuadraticForm(ZZ, [3,9,18,27]) 

sage: Q.scale_by_factor(3) 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 9 0 0 0 ] 

[ * 27 0 0 ] 

[ * * 54 0 ] 

[ * * * 81 ] 

 

sage: Q.scale_by_factor(1/3) 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 1 0 0 0 ] 

[ * 3 0 0 ] 

[ * * 6 0 ] 

[ * * * 9 ] 

 

""" 

## Try to scale the coefficients while staying in the ring of values. 

new_coeff_list = [x*c for x in self.coefficients()] 

 

## Check if we can preserve the value ring and return result. -- USE THE BASE_RING FOR NOW... 

R = self.base_ring() 

try: 

list2 = [R(x) for x in new_coeff_list] 

# This is a hack: we would like to use QuadraticForm here, but 

# it doesn't work by scoping reasons. 

Q = self.__class__(R, self.dim(), list2) 

return Q 

except Exception: 

if not change_value_ring_flag: 

raise TypeError("Oops! We could not rescale the lattice in this way and preserve its defining ring.") 

else: 

raise UntestedCode("This code is not tested by current doctests!") 

F = R.fraction_field() 

list2 = [F(x) for x in new_coeff_list] 

Q = copy.deepcopy(self) 

Q.__init__(self.dim(), F, list2, R) ## DEFINE THIS! IT WANTS TO SET THE EQUIVALENCE RING TO R, BUT WITH COEFFS IN F. 

#Q.set_equivalence_ring(R) 

return Q 

 

 

def extract_variables(self, var_indices): 

""" 

Extract the variables (in order) whose indices are listed in 

var_indices, to give a new quadratic form. 

 

INPUT: 

 

var_indices -- a list of integers >= 0 

 

OUTPUT: 

 

a QuadraticForm 

 

EXAMPLES:: 

 

sage: Q = QuadraticForm(ZZ, 4, range(10)); Q 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 0 1 2 3 ] 

[ * 4 5 6 ] 

[ * * 7 8 ] 

[ * * * 9 ] 

sage: Q.extract_variables([1,3]) 

Quadratic form in 2 variables over Integer Ring with coefficients: 

[ 4 6 ] 

[ * 9 ] 

 

""" 

m = len(var_indices) 

Q = copy.deepcopy(self) 

Q.__init__(self.base_ring(), m) 

for i in range(m): 

for j in range(i, m): 

Q[i,j] = self[ var_indices[i], var_indices[j] ] 

 

return Q 

 

 

 

def elementary_substitution(self, c, i, j, in_place = False): ## CHECK THIS!!! 

""" 

Perform the substitution `x_i --> x_i + c*x_j` (replacing the 

original form if the in_place flag is True). 

 

INPUT: 

 

`c` -- an element of Q.base_ring() 

 

`i`, `j` -- integers >= 0 

 

OUTPUT: 

 

a QuadraticForm (by default, otherwise none) 

 

EXAMPLES:: 

 

sage: Q = QuadraticForm(ZZ, 4, range(1,11)) 

sage: Q 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 1 2 3 4 ] 

[ * 5 6 7 ] 

[ * * 8 9 ] 

[ * * * 10 ] 

 

sage: Q.elementary_substitution(c=1, i=0, j=3) 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 1 2 3 6 ] 

[ * 5 6 9 ] 

[ * * 8 12 ] 

[ * * * 15 ] 

 

:: 

 

sage: R = QuadraticForm(ZZ, 4, range(1,11)) 

sage: R 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 1 2 3 4 ] 

[ * 5 6 7 ] 

[ * * 8 9 ] 

[ * * * 10 ] 

 

:: 

 

sage: M = Matrix(ZZ, 4, 4, [1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1]) 

sage: M 

[1 0 0 1] 

[0 1 0 0] 

[0 0 1 0] 

[0 0 0 1] 

sage: R(M) 

Quadratic form in 4 variables over Integer Ring with coefficients: 

[ 1 2 3 6 ] 

[ * 5 6 9 ] 

[ * * 8 12 ] 

[ * * * 15 ] 

 

""" 

if not in_place: 

Q = copy.deepcopy(self) 

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

Q.elementary_substitution(c, i, j, True) 

return Q 

 

else: 

## Adjust the a_{k,j} coefficients 

ij_old = self[i,j] ## Store this since it's overwritten, but used in the a_{j,j} computation! 

for k in range(self.dim()): 

if (k != i) and (k != j): 

ans = self[j,k] + c*self[i,k] 

self[j,k] = ans 

elif (k == j): 

ans = self[j,k] + c*ij_old + c*c*self[i,i] 

self[j,k] = ans 

else: 

ans = self[j,k] + 2*c*self[i,k] 

self[j,k] = ans 

 

 

 

def add_symmetric(self, c, i, j, in_place = False): 

""" 

Performs the substitution `x_j --> x_j + c*x_i`, which has the 

effect (on associated matrices) of symmetrically adding 

`c * j`-th row/column to the `i`-th row/column. 

 

NOTE: This is meant for compatibility with previous code, 

which implemented a matrix model for this class. It is used 

in the local_normal_form() method. 

 

 

INPUT: 

 

`c` -- an element of Q.base_ring() 

 

`i`, `j` -- integers >= 0 

 

OUTPUT: 

 

a QuadraticForm (by default, otherwise none) 

 

EXAMPLES:: 

 

sage: Q = QuadraticForm(ZZ, 3, range(1,7)); Q 

Quadratic form in 3 variables over Integer Ring with coefficients: 

[ 1 2 3 ] 

[ * 4 5 ] 

[ * * 6 ] 

sage: Q.add_symmetric(-1, 1, 0) 

Quadratic form in 3 variables over Integer Ring with coefficients: 

[ 1 0 3 ] 

[ * 3 2 ] 

[ * * 6 ] 

sage: Q.add_symmetric(-3/2, 2, 0) ## ERROR: -3/2 isn't in the base ring ZZ 

Traceback (most recent call last): 

... 

RuntimeError: Oops! This coefficient can't be coerced to an element of the base ring for the quadratic form. 

 

:: 

 

sage: Q = QuadraticForm(QQ, 3, range(1,7)); Q 

Quadratic form in 3 variables over Rational Field with coefficients: 

[ 1 2 3 ] 

[ * 4 5 ] 

[ * * 6 ] 

sage: Q.add_symmetric(-3/2, 2, 0) 

Quadratic form in 3 variables over Rational Field with coefficients: 

[ 1 2 0 ] 

[ * 4 2 ] 

[ * * 15/4 ] 

 

""" 

return self.elementary_substitution(c, j, i, in_place)