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

""" 

Linear Expressions 

 

A linear expression is just a linear polynomial in some (fixed) 

variables (allowing a nonzero constant term). This class only implements 

linear expressions for others to use. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ); L 

Module of linear expressions in variables x, y, z over Rational Field 

sage: x + 2*y + 3*z + 4 

x + 2*y + 3*z + 4 

sage: L(4) 

0*x + 0*y + 0*z + 4 

 

You can also pass coefficients and a constant term to construct linear 

expressions:: 

 

sage: L([1, 2, 3], 4) 

x + 2*y + 3*z + 4 

sage: L([(1, 2, 3), 4]) 

x + 2*y + 3*z + 4 

sage: L([4, 1, 2, 3]) # note: constant is first in single-tuple notation 

x + 2*y + 3*z + 4 

 

The linear expressions are a module over the base ring, so you can 

add them and multiply them with scalars:: 

 

sage: m = x + 2*y + 3*z + 4 

sage: 2*m 

2*x + 4*y + 6*z + 8 

sage: m+m 

2*x + 4*y + 6*z + 8 

sage: m-m 

0*x + 0*y + 0*z + 0 

""" 

from six.moves import zip 

 

from sage.structure.parent import Parent 

from sage.structure.richcmp import richcmp 

from sage.structure.element import ModuleElement 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.misc.cachefunc import cached_method 

 

 

class LinearExpression(ModuleElement): 

""" 

A linear expression. 

 

A linear expression is just a linear polynomial in some (fixed) 

variables. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: m = L([1, 2, 3], 4); m 

x + 2*y + 3*z + 4 

sage: m2 = L([(1, 2, 3), 4]); m2 

x + 2*y + 3*z + 4 

sage: m3 = L([4, 1, 2, 3]); m3 # note: constant is first in single-tuple notation 

x + 2*y + 3*z + 4 

sage: m == m2 

True 

sage: m2 == m3 

True 

sage: L.zero() 

0*x + 0*y + 0*z + 0 

sage: a = L([12, 2/3, -1], -2) 

sage: a - m 

11*x - 4/3*y - 4*z - 6 

sage: LZ.<x,y,z> = LinearExpressionModule(ZZ) 

sage: a - LZ([2, -1, 3], 1) 

10*x + 5/3*y - 4*z - 3 

""" 

def __init__(self, parent, coefficients, constant, check=True): 

""" 

Initialize ``self``. 

 

TESTS:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: linear = L([1, 2, 3], 4) # indirect doctest 

sage: linear.parent() is L 

True 

 

sage: TestSuite(linear).run() 

""" 

super(LinearExpression, self).__init__(parent) 

self._coeffs = coefficients 

self._const = constant 

if check: 

if self._coeffs.parent() is not self.parent().ambient_module(): 

raise ValueError("coefficients are not in the ambient module") 

if not self._coeffs.is_immutable(): 

raise ValueError("coefficients are not immutable") 

if self._const.parent() is not self.parent().base_ring(): 

raise ValueError("the constant is not in the base ring") 

 

def A(self): 

""" 

Return the coefficient vector. 

 

OUTPUT: 

 

The coefficient vector of the linear expression. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: linear = L([1, 2, 3], 4); linear 

x + 2*y + 3*z + 4 

sage: linear.A() 

(1, 2, 3) 

sage: linear.b() 

4 

""" 

return self._coeffs 

 

def b(self): 

""" 

Return the constant term. 

 

OUTPUT: 

 

The constant term of the linear expression. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: linear = L([1, 2, 3], 4); linear 

x + 2*y + 3*z + 4 

sage: linear.A() 

(1, 2, 3) 

sage: linear.b() 

4 

""" 

return self._const 

 

constant_term = b 

 

def coefficients(self): 

""" 

Return all coefficients. 

 

OUTPUT: 

 

The constant (as first entry) and coefficients of the linear 

terms (as subsequent entries) in a list. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: linear = L([1, 2, 3], 4); linear 

x + 2*y + 3*z + 4 

sage: linear.coefficients() 

[4, 1, 2, 3] 

""" 

return [self._const] + list(self._coeffs) 

 

dense_coefficient_list = coefficients 

 

def monomial_coefficients(self, copy=True): 

""" 

Return a dictionary whose keys are indices of basis elements in 

the support of ``self`` and whose values are the corresponding 

coefficients. 

 

INPUT: 

 

- ``copy`` -- ignored 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: linear = L([1, 2, 3], 4) 

sage: sorted(linear.monomial_coefficients().items()) 

[(0, 1), (1, 2), (2, 3), ('b', 4)] 

""" 

zero = self.parent().base_ring().zero() 

d = {i: v for i, v in enumerate(self._coeffs) if v != zero} 

if self._const != zero: 

d['b'] = self._const 

return d 

 

def _repr_vector(self, variable='x'): 

""" 

Return a string representation. 

 

INPUT: 

 

- ``variable`` -- string; the name of the variable vector 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: L([1, 2, 3], 4)._repr_vector() 

'(1, 2, 3) x + 4 = 0' 

sage: L([-1, -2, -3], -4)._repr_vector('u') 

'(-1, -2, -3) u - 4 = 0' 

""" 

atomic_repr = self.parent().base_ring()._repr_option('element_is_atomic') 

constant = repr(self._const) 

if not atomic_repr: 

constant = '({0})'.format(constant) 

constant = '+ {0}'.format(constant).replace('+ -', '- ') 

return '{0} {1} {2} = 0'.format(repr(self._coeffs), variable, constant) 

 

def _repr_linear(self, include_zero=True, include_constant=True, multiplication='*'): 

""" 

Return a representation as a linear polynomial. 

 

INPUT: 

 

- ``include_zero`` -- whether to include terms with zero 

coefficient 

 

- ``include_constant`` -- whether to include the constant 

term 

 

- ``multiplication`` -- string (optional, default: ``*``); the 

multiplication symbol to use 

 

OUTPUT: 

 

A string. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: L([1, 2, 3], 4)._repr_linear() 

'x + 2*y + 3*z + 4' 

sage: L([-1, -2, -3], -4)._repr_linear() 

'-x - 2*y - 3*z - 4' 

sage: L([0, 0, 0], 1)._repr_linear() 

'0*x + 0*y + 0*z + 1' 

sage: L([0, 0, 0], 0)._repr_linear() 

'0*x + 0*y + 0*z + 0' 

 

sage: R.<u,v> = QQ[] 

sage: L.<x,y,z> = LinearExpressionModule(R) 

sage: L([-u+v+1, -3*u-2, 3], -4*u+v)._repr_linear() 

'(-u + v + 1)*x + (-3*u - 2)*y + 3*z - 4*u + v' 

 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: L([1, 0, 3], 0)._repr_linear() 

'x + 0*y + 3*z + 0' 

sage: L([1, 0, 3], 0)._repr_linear(include_zero=False) 

'x + 3*z' 

sage: L([1, 0, 3], 1)._repr_linear(include_constant=False, multiplication='.') 

'x + 0.y + 3.z' 

sage: L([1, 0, 3], 1)._repr_linear(include_zero=False, include_constant=False) 

'x + 3*z' 

sage: L([0, 0, 0], 0)._repr_linear(include_zero=False) 

'0' 

""" 

atomic_repr = self.parent().base_ring()._repr_option('element_is_atomic') 

names = [multiplication + n for n in self.parent()._names] 

terms = list(zip(self._coeffs, names)) 

if include_constant: 

terms += [(self._const, '')] 

if not include_zero: 

terms = [t for t in terms if t[0] != 0] 

if len(terms) == 0: 

return '0' 

summands = [] 

for coeff, name in terms: 

coeff = str(coeff) 

if not atomic_repr and name != '' and any(c in coeff for c in ['+', '-']): 

coeff = '({0})'.format(coeff) 

summands.append(coeff + name) 

s = ' ' + ' + '.join(summands) 

s = s.replace(' + -', ' - ') 

s = s.replace(' 1' + multiplication, ' ') 

s = s.replace(' -1' + multiplication, ' -') 

return s[1:] 

 

_repr_ = _repr_linear 

 

def _add_(self, other): 

""" 

Add two linear expressions. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: a = L([1, 2, 3], 4) 

sage: b = L([-1, 3, -3], 0) 

sage: a + b 

0*x + 5*y + 0*z + 4 

sage: a - b 

2*x - y + 6*z + 4 

""" 

const = self._const + other._const 

coeffs = self._coeffs + other._coeffs 

coeffs.set_immutable() 

return self.__class__(self.parent(), coeffs, const) 

 

def _lmul_(self, scalar): 

""" 

Multiply a linear expression by a scalar. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: a = L([1, 2, 3], 4); a 

x + 2*y + 3*z + 4 

sage: 2 * a 

2*x + 4*y + 6*z + 8 

sage: a * 2 

2*x + 4*y + 6*z + 8 

sage: -a 

-x - 2*y - 3*z - 4 

sage: RDF(1) * a 

1.0*x + 2.0*y + 3.0*z + 4.0 

 

TESTS:: 

 

sage: a._lmul_(2) 

2*x + 4*y + 6*z + 8 

""" 

const = scalar * self._const 

coeffs = scalar * self._coeffs 

coeffs.set_immutable() 

return self.__class__(self.parent(), coeffs, const) 

 

def _acted_upon_(self, scalar, self_on_left): 

""" 

Action by scalars that do not live in the base ring. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: a = x + 2*y + 3*z + 4 

sage: a * RDF(3) 

3.0*x + 6.0*y + 9.0*z + 12.0 

""" 

base_ring = scalar.base_ring() 

parent = self.parent().change_ring(base_ring) 

changed = parent(self) 

return changed._rmul_(scalar) 

 

def change_ring(self, base_ring): 

""" 

Change the base ring of this linear expression. 

 

INPUT: 

 

- ``base_ring`` -- a ring; the new base ring 

 

OUTPUT: 

 

A new linear expression over the new base ring. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: a = x + 2*y + 3*z + 4; a 

x + 2*y + 3*z + 4 

sage: a.change_ring(RDF) 

1.0*x + 2.0*y + 3.0*z + 4.0 

""" 

P = self.parent() 

if P.base_ring() is base_ring: 

return self 

return P.change_ring(base_ring)(self) 

 

def __hash__(self): 

r""" 

TESTS:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x> = LinearExpressionModule(QQ) 

sage: hash(L([0,1])) 

3430019387558 # 64-bit 

-1659481946 # 32-bit 

""" 

return hash(self._coeffs) ^ hash(self._const) 

 

def _richcmp_(self, other, op): 

""" 

Compare two linear expressions. 

 

INPUT: 

 

- ``other`` -- another linear expression (will be enforced by 

the coercion framework) 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x> = LinearExpressionModule(QQ) 

sage: x == L([0, 1]) 

True 

sage: x == x + 1 

False 

 

sage: M.<x> = LinearExpressionModule(ZZ) 

sage: L.gen(0) == M.gen(0) # because there is a conversion 

True 

sage: L.gen(0) == L(M.gen(0)) # this is the conversion 

True 

 

sage: x == 'test' 

False 

""" 

return richcmp((self._coeffs, self._const), 

(other._coeffs, other._const), op) 

 

def evaluate(self, point): 

""" 

Evaluate the linear expression. 

 

INPUT: 

 

- ``point`` -- list/tuple/iterable of coordinates; the 

coordinates of a point 

 

OUTPUT: 

 

The linear expression `Ax + b` evaluated at the point `x`. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y> = LinearExpressionModule(QQ) 

sage: ex = 2*x + 3* y + 4 

sage: ex.evaluate([1,1]) 

9 

sage: ex([1,1]) # syntactic sugar 

9 

sage: ex([pi, e]) 

2*pi + 3*e + 4 

""" 

try: 

point = self.parent().ambient_module()(point) 

except TypeError: 

from sage.matrix.constructor import vector 

point = vector(point) 

return self._coeffs * point + self._const 

 

__call__ = evaluate 

 

 

class LinearExpressionModule(Parent, UniqueRepresentation): 

""" 

The module of linear expressions. 

 

This is the module of linear polynomials which is the parent for 

linear expressions. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) 

sage: L 

Module of linear expressions in variables x, y, z over Rational Field 

sage: L.an_element() 

x + 0*y + 0*z + 0 

""" 

Element = LinearExpression 

 

def __init__(self, base_ring, names=tuple()): 

""" 

Initialize ``self``. 

 

TESTS:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) 

sage: type(L) 

<class 'sage.geometry.linear_expression.LinearExpressionModule_with_category'> 

sage: L.base_ring() 

Rational Field 

 

sage: TestSuite(L).run() 

 

sage: L = LinearExpressionModule(QQ) 

sage: TestSuite(L).run() 

""" 

from sage.categories.modules import Modules 

super(LinearExpressionModule, self).__init__(base_ring, category=Modules(base_ring).WithBasis().FiniteDimensional()) 

self._names = names 

 

@cached_method 

def basis(self): 

""" 

Return a basis of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) 

sage: list(L.basis()) 

[x + 0*y + 0*z + 0, 

0*x + y + 0*z + 0, 

0*x + 0*y + z + 0, 

0*x + 0*y + 0*z + 1] 

""" 

from sage.sets.family import Family 

gens = self.gens() 

d = {i: g for i, g in enumerate(gens)} 

d['b'] = self.element_class(self, self.ambient_module().zero(), 

self.base_ring().one()) 

return Family(list(range(len(gens))) + ['b'], lambda i: d[i]) 

 

@cached_method 

def ngens(self): 

""" 

Return the number of linear variables. 

 

OUTPUT: 

 

An integer. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) 

sage: L.ngens() 

3 

""" 

return len(self._names) 

 

@cached_method 

def gens(self): 

""" 

Return the generators of ``self``. 

 

OUTPUT: 

 

A tuple of linear expressions, one for each linear variable. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) 

sage: L.gens() 

(x + 0*y + 0*z + 0, 0*x + y + 0*z + 0, 0*x + 0*y + z + 0) 

""" 

from sage.matrix.constructor import identity_matrix 

identity = identity_matrix(self.base_ring(), self.ngens()) 

return tuple(self(e, 0) for e in identity.rows()) 

 

def gen(self, i): 

""" 

Return the `i`-th generator. 

 

INPUT: 

 

- ``i`` -- integer 

 

OUTPUT: 

 

A linear expression. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) 

sage: L.gen(0) 

x + 0*y + 0*z + 0 

""" 

return self.gens()[i] 

 

def _element_constructor_(self, arg0, arg1=None): 

""" 

The element constructor. 

 

This is part of the Sage parent/element framework. 

 

TESTS:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) 

 

Construct from coefficients and constant term:: 

 

sage: L._element_constructor_([1, 2, 3], 4) 

x + 2*y + 3*z + 4 

sage: L._element_constructor_(vector(ZZ, [1, 2, 3]), 4) 

x + 2*y + 3*z + 4 

 

Construct constant linear expression term:: 

 

sage: L._element_constructor_(4) 

0*x + 0*y + 0*z + 4 

 

Construct from list/tuple/iterable:: 

 

sage: L._element_constructor_(vector([4, 1, 2, 3])) 

x + 2*y + 3*z + 4 

 

Construct from a pair ``(coefficients, constant)``:: 

 

sage: L([(1, 2, 3), 4]) 

x + 2*y + 3*z + 4 

 

Construct from linear expression:: 

 

sage: M = LinearExpressionModule(ZZ, ('u', 'v', 'w')) 

sage: m = M([1, 2, 3], 4) 

sage: L._element_constructor_(m) 

x + 2*y + 3*z + 4 

""" 

R = self.base_ring() 

if arg1 is None: 

if arg0 in R: 

const = arg0 

coeffs = self.ambient_module().zero() 

elif isinstance(arg0, LinearExpression): 

# Construct from linear expression 

const = arg0.b() 

coeffs = arg0.A() 

elif isinstance(arg0, (list, tuple)) and len(arg0) == 2 and isinstance(arg0[0], (list, tuple)): 

# Construct from pair 

coeffs = arg0[0] 

const = arg0[1] 

else: 

# Construct from list/tuple/iterable:: 

try: 

arg0 = arg0.dense_coefficient_list() 

except AttributeError: 

arg0 = list(arg0) 

const = arg0[0] 

coeffs = arg0[1:] 

else: 

# arg1 is not None, construct from coefficients and constant term 

coeffs = list(arg0) 

const = arg1 

coeffs = self.ambient_module()(coeffs) 

coeffs.set_immutable() 

const = R(const) 

return self.element_class(self, coeffs, const) 

 

def random_element(self): 

""" 

Return a random element. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x,y,z> = LinearExpressionModule(QQ) 

sage: L.random_element() 

-1/2*x - 1/95*y + 1/2*z - 12 

""" 

A = self.ambient_module().random_element() 

b = self.base_ring().random_element() 

return self(A, b) 

 

@cached_method 

def ambient_module(self): 

""" 

Return the ambient module. 

 

.. SEEALSO:: 

 

:meth:`ambient_vector_space` 

 

OUTPUT: 

 

The domain of the linear expressions as a free module over the 

base ring. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) 

sage: L.ambient_module() 

Vector space of dimension 3 over Rational Field 

sage: M = LinearExpressionModule(ZZ, ('r', 's')) 

sage: M.ambient_module() 

Ambient free module of rank 2 over the principal ideal domain Integer Ring 

sage: M.ambient_vector_space() 

Vector space of dimension 2 over Rational Field 

""" 

from sage.modules.all import FreeModule 

return FreeModule(self.base_ring(), self.ngens()) 

 

@cached_method 

def ambient_vector_space(self): 

""" 

Return the ambient vector space. 

 

.. SEEALSO:: 

 

:meth:`ambient_module` 

 

OUTPUT: 

 

The vector space (over the fraction field of the base ring) 

where the linear expressions live. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) 

sage: L.ambient_vector_space() 

Vector space of dimension 3 over Rational Field 

sage: M = LinearExpressionModule(ZZ, ('r', 's')) 

sage: M.ambient_module() 

Ambient free module of rank 2 over the principal ideal domain Integer Ring 

sage: M.ambient_vector_space() 

Vector space of dimension 2 over Rational Field 

""" 

from sage.modules.all import VectorSpace 

field = self.base_ring().fraction_field() 

return VectorSpace(field, self.ngens()) 

 

def _coerce_map_from_(self, P): 

""" 

Return whether there is a coercion. 

 

TESTS:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x> = LinearExpressionModule(QQ) 

sage: M.<y> = LinearExpressionModule(ZZ) 

sage: L.coerce_map_from(M) 

Coercion map: 

From: Module of linear expressions in variable y over Integer Ring 

To: Module of linear expressions in variable x over Rational Field 

sage: M.coerce_map_from(L) 

 

sage: M.coerce_map_from(ZZ) 

Coercion map: 

From: Integer Ring 

To: Module of linear expressions in variable y over Integer Ring 

sage: M.coerce_map_from(QQ) 

""" 

if self.base().has_coerce_map_from(P): 

return True 

try: 

return self.ngens() == P.ngens() and \ 

self.base().has_coerce_map_from(P.base()) 

except AttributeError: 

pass 

return super(LinearExpressionModule, self)._coerce_map_from_(P) 

 

def _repr_(self): 

""" 

Return a string representation. 

 

OUTPUT: 

 

A string. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: L.<x> = LinearExpressionModule(QQ); L 

Module of linear expressions in variable x over Rational Field 

""" 

return 'Module of linear expressions in variable{2} {0} over {1}'.format( 

', '.join(self._names), self.base_ring(), 's' if self.ngens() > 1 else '') 

 

def change_ring(self, base_ring): 

""" 

Return a new module with a changed base ring. 

 

INPUT: 

 

- ``base_ring`` -- a ring; the new base ring 

 

OUTPUT: 

 

A new linear expression over the new base ring. 

 

EXAMPLES:: 

 

sage: from sage.geometry.linear_expression import LinearExpressionModule 

sage: M.<y> = LinearExpressionModule(ZZ) 

sage: L = M.change_ring(QQ); L 

Module of linear expressions in variable y over Rational Field 

 

TESTS:: 

 

sage: L.change_ring(QQ) is L 

True 

""" 

return LinearExpressionModule(base_ring, self._names)