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

""" 

Formal sums 

 

AUTHORS: 

 

- David Harvey (2006-09-20): changed FormalSum not to derive from 

"list" anymore, because that breaks new Element interface 

 

- Nick Alexander (2006-12-06): added test cases. 

 

- William Stein (2006, 2009): wrote the first version in 2006, documented it in 2009. 

 

- Volker Braun (2010-07-19): new-style coercions, documentation 

added. FormalSums now derives from UniqueRepresentation. 

 

FUNCTIONS: 

- ``FormalSums(ring)`` -- create the module of formal finite sums with 

coefficients in the given ring. 

 

- ``FormalSum(list of pairs (coeff, number))`` -- create a formal sum 

 

EXAMPLES:: 

 

sage: A = FormalSum([(1, 2/3)]); A 

2/3 

sage: B = FormalSum([(3, 1/5)]); B 

3*1/5 

sage: -B 

-3*1/5 

sage: A + B 

3*1/5 + 2/3 

sage: A - B 

-3*1/5 + 2/3 

sage: B*3 

9*1/5 

sage: 2*A 

2*2/3 

sage: list(2*A + A) 

[(3, 2/3)] 

 

TESTS:: 

 

sage: R = FormalSums(QQ) 

sage: loads(dumps(R)) == R 

True 

sage: a = R(2/3) + R(-5/7); a 

-5/7 + 2/3 

sage: loads(dumps(a)) == a 

True 

""" 

 

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

# Copyright (C) 2004 William Stein <wstein@gmail.com> 

# 

# 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/ 

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

from __future__ import print_function 

 

import sage.misc.misc 

import operator 

import sage.misc.latex 

 

from sage.modules.module import Module 

from sage.structure.element import ModuleElement 

from sage.structure.richcmp import richcmp 

from sage.rings.integer_ring import ZZ 

from sage.structure.parent import Parent 

from sage.structure.coerce import LeftModuleAction, RightModuleAction 

from sage.categories.action import PrecomposedAction 

from sage.structure.unique_representation import UniqueRepresentation 

 

 

class FormalSum(ModuleElement): 

""" 

A formal sum over a ring. 

""" 

def __init__(self, x, parent=None, check=True, reduce=True): 

""" 

INPUT: 

- ``x`` -- object 

- ``parent`` -- FormalSums(R) module (default: FormalSums(ZZ)) 

- ``check`` -- bool (default: True) if False, might not coerce 

coefficients into base ring, which can speed 

up constructing a formal sum. 

- ``reduce`` -- reduce (default: True) if False, do not 

combine common terms 

 

EXAMPLES:: 

 

sage: FormalSum([(1,2/3), (3,2/3), (-5, 7)]) 

4*2/3 - 5*7 

sage: a = FormalSum([(1,2/3), (3,2/3), (-5, 7)], reduce=False); a 

2/3 + 3*2/3 - 5*7 

sage: a.reduce() 

sage: a 

4*2/3 - 5*7 

sage: FormalSum([(1,2/3), (3,2/3), (-5, 7)], parent=FormalSums(GF(5))) 

4*2/3 

 

Notice below that the coefficient 5 doesn't get reduced modulo 5:: 

 

sage: FormalSum([(1,2/3), (3,2/3), (-5, 7)], parent=FormalSums(GF(5)), check=False) 

4*2/3 - 5*7 

 

Make sure we first reduce before checking coefficient types:: 

 

sage: x,y = var('x, y') 

sage: FormalSum([(1/2,x), (2,y)], FormalSums(QQ)) 

1/2*x + 2*y 

sage: FormalSum([(1/2,x), (2,y)], FormalSums(ZZ)) 

Traceback (most recent call last): 

... 

TypeError: no conversion of this rational to integer 

sage: FormalSum([(1/2,x), (1/2,x), (2,y)], FormalSums(ZZ)) 

x + 2*y 

""" 

if x == 0: 

x = [] 

self._data = x 

if parent is None: 

parent = formal_sums 

ModuleElement.__init__(self, parent) 

assert isinstance(parent, parent.category().parent_class) 

if reduce: # first reduce 

self.reduce() 

if check: # then check 

k = parent.base_ring() 

try: 

self._data = [(k(t[0]), t[1]) for t in self._data] 

except (IndexError, KeyError) as msg: 

raise TypeError("%s\nInvalid formal sum"%msg) 

 

def __iter__(self): 

""" 

EXAMPLES:: 

 

sage: for z in FormalSum([(1,2), (5, 1000), (-3, 7)]): print(z) 

(1, 2) 

(-3, 7) 

(5, 1000) 

""" 

return iter(self._data) 

 

def __getitem__(self, n): 

""" 

EXAMPLES:: 

 

sage: v = FormalSum([(1,2), (5, 1000), (-3, 7)]); v 

2 - 3*7 + 5*1000 

sage: v[0] 

(1, 2) 

sage: v[1] 

(-3, 7) 

sage: v[2] 

(5, 1000) 

sage: list(v) 

[(1, 2), (-3, 7), (5, 1000)] 

""" 

return self._data[n] 

 

def __len__(self): 

""" 

EXAMPLES:: 

 

sage: v = FormalSum([(1,2), (5, 1000), (-3, 7)]); v 

2 - 3*7 + 5*1000 

sage: len(v) 

3 

""" 

return len(self._data) 

 

def _repr_(self): 

""" 

EXAMPLES:: 

 

sage: a = FormalSum([(1,2/3), (-3,4/5), (7,Mod(2,3))]) 

sage: a # random, since comparing Mod(2,3) and rationals ill-defined 

sage: a._repr_() # random 

'2/3 - 3*4/5 + 7*2' 

""" 

return sage.misc.misc.repr_lincomb([t, c] for c, t in self) 

 

def _latex_(self): 

""" 

EXAMPLES:: 

 

sage: latex(FormalSum([(1,2), (5, 8/9), (-3, 7)])) 

5\cdot \frac{8}{9} + 2 - 3\cdot 7 

""" 

symbols = [z[1] for z in self] 

coeffs = [z[0] for z in self] 

return sage.misc.latex.repr_lincomb(symbols, coeffs) 

# TODO: finish merging sage.misc.latex.repr_lincomb and 

# sage.misc.misc.repr_lincomb and use instead: 

# return sage.misc.misc.repr_lincomb([[t,c] for c,t in self], is_latex=True) 

 

def _richcmp_(left, right, op): 

""" 

Compare ``left`` and ``right``. 

 

INPUT: 

 

- ``right`` -- a :class:`FormalSum` with the same parent 

 

- ``op`` -- a comparison operator 

 

EXAMPLES:: 

 

sage: a = FormalSum([(1,3),(2,5)]); a 

3 + 2*5 

sage: b = FormalSum([(1,3),(2,7)]); b 

3 + 2*7 

sage: a != b 

True 

sage: a_QQ = FormalSum([(1,3),(2,5)],parent=FormalSums(QQ)) 

sage: a == a_QQ # a is coerced into FormalSums(QQ) 

True 

sage: a == 0 # 0 is coerced into a.parent()(0) 

False 

""" 

return richcmp(left._data, right._data, op) 

 

def _neg_(self): 

""" 

EXAMPLES:: 

 

sage: -FormalSum([(1,3),(2,5)]) 

-3 - 2*5 

""" 

return self.__class__([(-c, s) for (c, s) in self._data], check=False, parent=self.parent()) 

 

def _add_(self, other): 

""" 

EXAMPLES:: 

 

sage: FormalSum([(1,3/7),(2,5/8)]) + FormalSum([(1,3/7),(-2,5)]) # indirect doctest 

2*3/7 + 2*5/8 - 2*5 

""" 

return self.__class__(self._data + other._data, check=False, parent=self.parent()) 

 

def _lmul_(self, s): 

""" 

EXAMPLES:: 

 

sage: FormalSum([(1,3/7),(-2,5)])*(-3) 

-3*3/7 + 6*5 

""" 

return self.__class__([(c*s, x) for (c, x) in self], check=False, parent=self.parent()) 

 

def _rmul_(self, s): 

""" 

EXAMPLES:: 

 

sage: -3*FormalSum([(1,3/7),(-2,5)]) 

-3*3/7 + 6*5 

""" 

return self.__class__([(s*c, x) for (c, x) in self], check=False, parent=self.parent()) 

 

def __bool__(self): 

""" 

EXAMPLES:: 

 

sage: bool(FormalSum([(1,3/7),(-2,5)])) 

True 

sage: bool(FormalSums(QQ)(0)) 

False 

sage: bool(FormalSums(QQ)(1)) 

True 

""" 

if not len(self._data): 

return False 

for c, _ in self._data: 

if not c.is_zero(): 

return True 

return False 

 

__nonzero__ = __bool__ 

 

def reduce(self): 

""" 

EXAMPLES:: 

 

sage: a = FormalSum([(-2,3), (2,3)], reduce=False); a 

-2*3 + 2*3 

sage: a.reduce() 

sage: a 

0 

""" 

if len(self) == 0: 

return 

v = [(x,c) for c, x in self if c!=0] 

if len(v) == 0: 

self._data = v 

return 

v.sort() 

w = [] 

last = v[0][0] 

coeff = v[0][1] 

for x, c in v[1:]: 

if x == last: 

coeff += c 

else: 

if coeff != 0: 

w.append((coeff, last)) 

last = x 

coeff = c 

if coeff != 0: 

w.append((coeff,last)) 

self._data = w 

 

class FormalSums(UniqueRepresentation, Module): 

""" 

The R-module of finite formal sums with coefficients in some ring R. 

 

EXAMPLES:: 

 

sage: FormalSums() 

Abelian Group of all Formal Finite Sums over Integer Ring 

sage: FormalSums(ZZ) 

Abelian Group of all Formal Finite Sums over Integer Ring 

sage: FormalSums(GF(7)) 

Abelian Group of all Formal Finite Sums over Finite Field of size 7 

sage: FormalSums(ZZ[sqrt(2)]) 

Abelian Group of all Formal Finite Sums over Order in Number Field in sqrt2 with defining polynomial x^2 - 2 

sage: FormalSums(GF(9,'a')) 

Abelian Group of all Formal Finite Sums over Finite Field in a of size 3^2 

 

TESTS:: 

 

sage: TestSuite(FormalSums(QQ)).run() 

 

""" 

Element = FormalSum 

@staticmethod 

def __classcall__(cls, base_ring = ZZ): 

""" 

Set the default value for the base ring. 

 

EXAMPLES:: 

 

sage: FormalSums(ZZ) == FormalSums() # indirect test 

True 

""" 

return UniqueRepresentation.__classcall__(cls, base_ring) 

 

def _repr_(self): 

""" 

EXAMPLES:: 

 

sage: FormalSums(GF(7)) 

Abelian Group of all Formal Finite Sums over Finite Field of size 7 

sage: FormalSums(GF(7))._repr_() 

'Abelian Group of all Formal Finite Sums over Finite Field of size 7' 

""" 

return "Abelian Group of all Formal Finite Sums over %s"%self.base_ring() 

 

def _element_constructor_(self, x, check=True, reduce=True): 

""" 

Make a formal sum in self from x. 

 

INPUT: 

 

- ``x`` -- formal sum, list or number 

 

- ``check`` -- bool (default: True) 

 

- ``reduce`` -- bool (default: True); whether to combine terms 

 

EXAMPLES:: 

 

sage: P = FormalSum([(1,2/3)]).parent() 

sage: P([(1,2/3), (5,-2/9)]) # indirect test 

5*-2/9 + 2/3 

""" 

if isinstance(x, FormalSum): 

P = x.parent() 

if P is self: 

return x 

else: 

x = x._data 

if isinstance(x, list): 

return self.element_class(x, check=check,reduce=reduce,parent=self) 

if x == 0: 

return self.element_class([], check=False, reduce=False, parent=self) 

else: 

return self.element_class([(self.base_ring()(1), x)], check=False, reduce=False, parent=self) 

 

def _coerce_map_from_(self, X): 

r""" 

Return whether there is a coercion from ``X`` 

 

EXAMPLES:: 

 

sage: FormalSums(QQ).has_coerce_map_from( FormalSums(ZZ) ) # indirect test 

True 

 

sage: FormalSums(ZZ).get_action(QQ) # indirect test 

Right scalar multiplication by Rational Field on Abelian Group of all Formal Finite Sums over Rational Field 

with precomposition on left by Coercion map: 

From: Abelian Group of all Formal Finite Sums over Integer Ring 

To: Abelian Group of all Formal Finite Sums over Rational Field 

""" 

if isinstance(X,FormalSums): 

if self.base_ring().has_coerce_map_from(X.base_ring()): 

return True 

return False 

 

def base_extend(self, R): 

""" 

EXAMPLES:: 

 

sage: F7 = FormalSums(ZZ).base_extend(GF(7)); F7 

Abelian Group of all Formal Finite Sums over Finite Field of size 7 

 

The following tests against a bug that was fixed at :trac:`18795`:: 

 

sage: isinstance(F7, F7.category().parent_class) 

True 

""" 

if self.base_ring().has_coerce_map_from(R): 

return self 

elif R.has_coerce_map_from(self.base_ring()): 

return FormalSums(R) 

 

def _get_action_(self, other, op, self_is_left): 

""" 

EXAMPLES:: 

 

sage: A = FormalSums(RR); A.get_action(RR) # indirect doctest 

Right scalar multiplication by Real Field with 53 bits of precision on Abelian Group of all Formal Finite Sums over Real Field with 53 bits of precision 

 

sage: A = FormalSums(ZZ); A.get_action(QQ) 

Right scalar multiplication by Rational Field on Abelian Group of all Formal Finite Sums over Rational Field 

with precomposition on left by Coercion map: 

From: Abelian Group of all Formal Finite Sums over Integer Ring 

To: Abelian Group of all Formal Finite Sums over Rational Field 

sage: A = FormalSums(QQ); A.get_action(ZZ) 

Right scalar multiplication by Integer Ring on Abelian Group of all Formal Finite Sums over Rational Field 

""" 

if op is operator.mul and isinstance(other, Parent): 

extended = self.base_extend(other) 

if self_is_left: 

action = RightModuleAction(other, extended) 

if extended is not self: 

action = PrecomposedAction(action, extended._internal_coerce_map_from(self), None) 

else: 

action = LeftModuleAction(other, extended) 

if extended is not self: 

action = PrecomposedAction(action, None, extended._internal_coerce_map_from(self)) 

return action 

 

def _an_element_(self, check=False, reduce=False): 

""" 

EXAMPLES:: 

 

sage: FormalSums(ZZ).an_element() # indirect test 

1 

sage: FormalSums(QQ).an_element() 

1/2*1 

sage: QQ.an_element() 

1/2 

""" 

return self.element_class([(self.base_ring().an_element(), 1)], 

check=check, reduce=reduce, parent=self) 

 

 

formal_sums = FormalSums() 

 

# Formal sums now derives from UniqueRepresentation, which makes the 

# factory function unnecessary. This is why the name was changed from 

# class FormalSums_generic to class FormalSums. 

from sage.structure.sage_object import register_unpickle_override 

register_unpickle_override('sage.structure.formal_sum', 'FormalSums_generic', FormalSums)