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

""" 

Wrapper for Singular's Polynomial Arithmetic 

  

AUTHOR: 

  

- Martin Albrecht (2009-07): refactoring 

""" 

  

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

# Copyright (C) 2009 Martin Albrecht <malb@informatik.uni-bremen.de> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

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

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

from __future__ import absolute_import 

  

from cysignals.signals cimport sig_on, sig_off 

  

cdef extern from *: # hack to get at cython macro 

int unlikely(int) 

  

import re 

plusminus_pattern = re.compile("([^\(^])([\+\-])") 

  

from sage.cpython.string cimport bytes_to_str, str_to_bytes 

  

from sage.libs.singular.decl cimport number, ideal 

from sage.libs.singular.decl cimport currRing, rChangeCurrRing 

from sage.libs.singular.decl cimport p_Copy, p_Add_q, p_Neg, pp_Mult_nn, p_GetCoeff, p_IsConstant, p_Cmp, pNext 

from sage.libs.singular.decl cimport p_GetMaxExp, pp_Mult_qq, pPower, p_String, p_GetExp, p_Deg, p_Totaldegree, p_WTotaldegree, p_WDegree 

from sage.libs.singular.decl cimport n_Delete, idInit, fast_map_common_subexp, id_Delete 

from sage.libs.singular.decl cimport omAlloc0, omStrDup, omFree 

from sage.libs.singular.decl cimport p_GetComp, p_SetComp 

from sage.libs.singular.decl cimport pSubst 

from sage.libs.singular.decl cimport p_Normalize 

  

  

from sage.libs.singular.singular cimport sa2si, si2sa, overflow_check 

  

from sage.misc.latex import latex 

  

cdef int singular_polynomial_check(poly *p, ring *r) except -1: 

""" 

Run consistency checks on ``p``. 

""" 

while p: 

if p_GetCoeff(p, r) == NULL: 

raise ZeroDivisionError("NULL pointer as coefficient.") 

p = p.next 

return 0 

  

cdef int singular_polynomial_add(poly **ret, poly *p, poly *q, ring *r): 

""" 

``ret[0] = p+q`` where ``p`` and ``p`` in ``r``. 

  

INPUT: 

  

- ``ret`` - a pointer to a Singular polynomial to store the result in 

- ``p`` - a Singular polynomial 

- ``q`` - a Singular polynomial 

- ``r`` - a Singular ring 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: x + y # indirect doctest 

x + y 

  

sage: x + P(0) 

x 

""" 

if(r != currRing): rChangeCurrRing(r) 

p = p_Copy(p, r) 

q = p_Copy(q, r) 

ret[0] = p_Add_q(p, q, r) 

return 0; 

  

cdef int singular_polynomial_sub(poly **ret, poly *p, poly *q, ring *r): 

""" 

``ret[0] = p-q`` where ``p`` and ``p`` in ``r``. 

  

INPUT: 

  

- ``ret`` - a pointer to a Singular polynomial to store the result in 

- ``p`` - a Singular polynomial 

- ``q`` - a Singular polynomial 

- ``r`` - a Singular ring 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: x - y # indirect doctest 

x - y 

  

sage: x + P(0) 

x 

""" 

if(r != currRing): rChangeCurrRing(r) 

p = p_Copy(p, r) 

q = p_Copy(q, r) 

ret[0] = p_Add_q(p, p_Neg(q, r), r) 

return 0; 

  

cdef int singular_polynomial_rmul(poly **ret, poly *p, RingElement n, ring *r): 

""" 

``ret[0] = n*p`` where ``n`` is a coefficient and ``p`` in ``r``. 

  

INPUT: 

  

- ``ret`` - a pointer to a Singular polynomial to store the result in 

- ``p`` - a Singular polynomial 

- ``n`` - a Sage coefficient 

- ``r`` - a Singular ring 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: 2*x # indirect doctest 

2*x 

  

sage: P(0)*x 

0 

""" 

if(r != currRing): rChangeCurrRing(r) 

cdef number *_n = sa2si(n, r) 

ret[0] = pp_Mult_nn(p, _n, r) 

n_Delete(&_n, r) 

return 0 

  

cdef int singular_polynomial_call(poly **ret, poly *p, ring *r, list args, poly *(*get_element)(object)): 

""" 

``ret[0] = p(*args)`` where each entry in arg is a polynomial and ``p`` in ``r``. 

  

INPUT: 

  

- ``ret`` - a pointer to a Singular polynomial to store the result in 

- ``p`` - a Singular polynomial 

- ``r`` - a Singular ring 

- ``args`` - a list/tuple of elements which can be converted to 

Singular polynomials using the ``(get_element)`` function 

provided. 

- ``(*get_element)`` - a function to turn a Sage element into a 

Singular element. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: x(0,0,0) # indirect doctest 

0 

  

sage: (3*x*z)(x,x,x) 

3*x^2 

  

TESTS: 

  

Test that there is no memory leak in evaluating polynomials. Note 

that (lib)Singular has pre-allocated buckets, so we have to run a 

lot of iterations to fill those up first:: 

  

sage: import resource 

sage: import gc 

sage: F.<a> = GF(7^2) 

sage: R.<x,y> = F[] 

sage: p = x+2*y 

sage: def leak(N): 

....: before = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss 

....: gc.collect() 

....: for i in range(N): 

....: _ = p(a, a) 

....: after = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss 

....: return (after - before) * 1024 # ru_maxrss is in kilobytes 

  

Loop (at most 30 times) until we have 6 consecutive zeros when 

calling ``leak(10000)``. Depending on the operating system, it is 

possible to have several non-zero leak values in the beginning, but 

after a while we should get only zeros. The fact that we require 6 

zeros also means that Singular's pre-allocated buckets should not 

be sufficient if there really would be a memory leak. :: 

  

sage: zeros = 0 

sage: for i in range(30): # long time 

....: n = leak(10000) 

....: print("Leaked {} bytes".format(n)) 

....: if n == 0: 

....: zeros += 1 

....: if zeros >= 6: 

....: break 

....: else: 

....: zeros = 0 

Leaked... 

Leaked 0 bytes 

Leaked 0 bytes 

Leaked 0 bytes 

Leaked 0 bytes 

Leaked 0 bytes 

""" 

cdef long l = len(args) 

cdef ideal *to_id = idInit(l,1) 

for i from 0 <= i < l: 

to_id.m[i]= p_Copy( get_element(args[i]), r) 

  

cdef ideal *from_id=idInit(1,1) 

from_id.m[0] = p 

  

rChangeCurrRing(r) 

cdef ideal *res_id = fast_map_common_subexp(from_id, r, to_id, r) 

ret[0] = res_id.m[0] 

  

# Unsure why we have to normalize here. See #16958 

p_Normalize(ret[0], r) 

  

from_id.m[0] = NULL 

res_id.m[0] = NULL 

  

id_Delete(&to_id, r) 

id_Delete(&from_id, r) 

id_Delete(&res_id, r) 

  

return 0 

  

cdef int singular_polynomial_cmp(poly *p, poly *q, ring *r): 

""" 

Compare two Singular elements ``p`` and ``q`` in ``r``. 

  

INPUT: 

  

- ``p`` - a Singular polynomial 

- ``q`` - a Singular polynomial 

- ``r`` - a Singular ring 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = PolynomialRing(QQ,order='degrevlex') 

sage: x == x 

True 

  

sage: x > y 

True 

sage: y^2 > x 

True 

  

sage: (2/3*x^2 + 1/2*y + 3) > (2/3*x^2 + 1/4*y + 10) 

True 

""" 

cdef number *h 

cdef int ret = 0 

  

if(r != currRing): rChangeCurrRing(r) 

  

# handle special cases first (slight slowdown, as special cases 

# are - well - special 

if p == NULL: 

if q == NULL: 

# compare 0, 0 

return 0 

elif p_IsConstant(q,r): 

# compare 0, const 

return 1-2*r.cf.cfGreaterZero(p_GetCoeff(q,r), r.cf) # -1: <, 1: > # 

elif q == NULL: 

if p_IsConstant(p,r): 

# compare const, 0 

return -1+2*r.cf.cfGreaterZero(p_GetCoeff(p,r), r.cf) # -1: <, 1: > 

  

while ret==0 and p!=NULL and q!=NULL: 

ret = p_Cmp( p, q, r) 

  

if ret==0: 

h = r.cf.cfSub(p_GetCoeff(p, r),p_GetCoeff(q, r),r.cf) 

# compare coeffs 

ret = -1+r.cf.cfIsZero(h,r.cf)+2*r.cf.cfGreaterZero(h, r.cf) # -1: <, 0:==, 1: > 

n_Delete(&h, r) 

p = pNext(p) 

q = pNext(q) 

  

if ret==0: 

if p==NULL and q != NULL: 

ret = -1 

elif p!=NULL and q==NULL: 

ret = 1 

  

return ret 

  

cdef int singular_polynomial_mul(poly** ret, poly *p, poly *q, ring *r) except -1: 

""" 

``ret[0] = p*q`` where ``p`` and ``p`` in ``r``. 

  

INPUT: 

  

- ``ret`` - a pointer to a Singular polynomial to store the result in 

- ``p`` - a Singular polynomial 

- ``q`` - a Singular polynomial 

- ``r`` - a Singular ring 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: x*y # indirect doctest 

x*y 

  

sage: x * P(0) 

0 

""" 

if(r != currRing): rChangeCurrRing(r) 

cdef unsigned long le = p_GetMaxExp(p, r) 

cdef unsigned long lr = p_GetMaxExp(q, r) 

cdef unsigned long esum = le + lr 

overflow_check(esum, r) 

ret[0] = pp_Mult_qq(p, q, r) 

return 0; 

  

cdef int singular_polynomial_div_coeff(poly** ret, poly *p, poly *q, ring *r) except -1: 

""" 

``ret[0] = p/n`` where ``p`` and ``q`` in ``r`` and ``q`` constant. 

  

The last condition is not checked. 

  

INPUT: 

  

- ``ret`` - a pointer to a Singular polynomial to store the result in 

- ``p`` - a Singular polynomial 

- ``q`` - a constant Singular polynomial 

- ``r`` - a Singular ring 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: x/2 # indirect doctest 

1/2*x 

  

sage: x/0 

Traceback (most recent call last): 

... 

ZeroDivisionError: rational division by zero 

""" 

if q == NULL: 

raise ZeroDivisionError 

sig_on() 

cdef number *n = p_GetCoeff(q, r) 

n = r.cf.cfInvers(n,r.cf) 

ret[0] = pp_Mult_nn(p, n, r) 

n_Delete(&n, r) 

sig_off() 

return 0 

  

cdef int singular_polynomial_pow(poly **ret, poly *p, unsigned long exp, ring *r) except -1: 

""" 

``ret[0] = p**exp`` where ``p`` in ``r`` and ``exp`` > 0. 

  

The last condition is not checked. 

  

INPUT: 

  

- ``ret`` - a pointer to a Singular polynomial to store the result in 

- ``p`` - a Singular polynomial 

- ``exp`` - integer 

- ``r`` - a Singular ring 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: f = 3*x*y + 5/2*z 

sage: f*f == f^2 # indirect doctest 

True 

sage: f^2 

9*x^2*y^2 + 15*x*y*z + 25/4*z^2 

sage: f^0 

1 

sage: f^(2^60) 

Traceback (most recent call last): 

... 

OverflowError: ... 

""" 

cdef unsigned long v = p_GetMaxExp(p, r) 

v = v * exp 

overflow_check(v, r) 

  

if(r != currRing): rChangeCurrRing(r) 

cdef int count = singular_polynomial_length_bounded(p,15) 

if count >= 15 or exp > 15: 

sig_on() 

ret[0] = pPower( p_Copy(p,r), exp) 

if count >= 15 or exp > 15: 

sig_off() 

return 0 

  

cdef int singular_polynomial_neg(poly **ret, poly *p, ring *r): 

""" 

``ret[0] = -p where ``p`` in ``r``. 

  

The last condition is not checked. 

  

INPUT: 

  

- ``ret`` - a pointer to a Singular polynomial to store the result in 

- ``p`` - a Singular polynomial 

- ``r`` - a Singular ring 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: f = 3*x*y + 5/2*z 

sage: -f # indirect doctest 

-3*x*y - 5/2*z 

sage: -P(0) 

0 

""" 

if(r != currRing): rChangeCurrRing(r) 

ret[0] = p_Neg(p_Copy(p,r),r) 

return 0 

  

cdef object singular_polynomial_str(poly *p, ring *r): 

""" 

Return the string representation of ``p``. 

  

INPUT: 

  

- ``p`` - a Singular polynomial 

- ``r`` - a Singular ring 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = ZZ[] 

sage: str(x) # indirect doctest 

'x' 

sage: str(10*x) 

'10*x' 

""" 

if(r!=currRing): rChangeCurrRing(r) 

  

s = bytes_to_str(p_String(p, r, r)) 

s = plusminus_pattern.sub("\\1 \\2 ", s) 

return s 

  

  

cdef object singular_polynomial_latex(poly *p, ring *r, object base, object latex_gens): 

r""" 

Return the LaTeX string representation of ``p``. 

  

INPUT: 

  

- ``p`` - a Singular polynomial 

- ``r`` - a Singular ring 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: latex(x) # indirect doctest 

x 

sage: latex(10*x^2 + 1/2*y) 

10 x^{2} + \frac{1}{2} y 

  

Demonstrate that coefficients over non-atomic representated rings are 

properly parenthesized (:trac:`11186`):: 

  

sage: x = var('x') 

sage: K.<z> = QQ.extension(x^2 + x + 1) 

sage: P.<v,w> = K[] 

sage: latex((z+1)*v*w - z*w^2 + z*v + z^2*w + z + 1) 

\left(z + 1\right) v w - z w^{2} + z v + \left(-z - 1\right) w + z + 1 

""" 

poly = "" 

cdef unsigned long e 

cdef int n = r.N, j 

cdef int atomic_repr = base._repr_option('element_is_atomic') 

while p: 

  

# First determine the multinomial: 

multi = "" 

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

e = p_GetExp(p, j, r) 

if e > 0: 

multi += " "+latex_gens[j-1] 

if e > 1: 

multi += "^{%d}"%e 

multi = multi.lstrip().rstrip() 

  

# Next determine coefficient of multinomial 

c = si2sa( p_GetCoeff(p, r), r, base) 

if len(multi) == 0: 

multi = latex(c) 

elif c != 1: 

if c == -1: 

multi = "- %s"%(multi) 

else: 

sc = latex(c) 

# Add parenthesis if the coefficient consists of terms divided by +, - 

# (starting with - is not enough) and is not the constant term 

if not atomic_repr and multi and (sc.find("+") != -1 or sc[1:].find("-") != -1): 

sc = "\\left(%s\\right)"%sc 

multi = "%s %s"%(sc,multi) 

  

# Now add on coefficiented multinomials 

if len(poly) > 0: 

poly = poly + " + " 

poly = poly + multi 

  

p = pNext(p) 

  

poly = poly.lstrip().rstrip() 

poly = poly.replace("+ -","- ") 

  

if len(poly) == 0: 

return "0" 

return poly 

  

cdef object singular_polynomial_str_with_changed_varnames(poly *p, ring *r, object varnames): 

cdef char **_names 

cdef char **_orig_names 

cdef int i 

  

if len(varnames) != r.N: 

raise TypeError("len(varnames) doesn't equal self.parent().ngens()") 

  

_names = <char**>omAlloc0(sizeof(char*)*r.N) 

for i from 0 <= i < r.N: 

_name = str_to_bytes(varnames[i]) 

_names[i] = omStrDup(_name) 

  

_orig_names = r.names 

r.names = _names 

s = singular_polynomial_str(p, r) 

r.names = _orig_names 

  

for i from 0 <= i < r.N: 

omFree(_names[i]) 

omFree(_names) 

return s 

  

cdef long singular_polynomial_deg(poly *p, poly *x, ring *r): 

cdef long _deg, deg 

  

deg = -1 

_deg = -1  

if p == NULL: 

return -1 

if(r != currRing): rChangeCurrRing(r) 

if x == NULL: 

while p:  

_deg = p_WDegree(p,r) 

 

if _deg > deg: 

deg = _deg 

p = pNext(p) 

return deg 

  

cdef int i = 0 

for i in range(1,r.N+1): 

if p_GetExp(x, i, r): 

break 

while p: 

_deg = p_GetExp(p,i,r) 

if _deg > deg: 

deg = _deg 

p = pNext(p) 

return deg 

  

cdef int singular_polynomial_length_bounded(poly *p, int bound): 

""" 

Return the number of monomials in ``p`` but stop counting at 

``bound``. 

  

This is useful to estimate whether a certain calculation will take 

long or not. 

  

INPUT: 

  

- ``p`` - a Singular polynomial 

- ``bound`` - an integer > 0 

""" 

cdef int count = 0 

while p != NULL and count < bound: 

p = pNext(p) 

count += 1 

return count 

  

cdef int singular_vector_maximal_component(poly *v, ring *r) except -1: 

""" 

returns the maximal module component of the vector ``v``. 

INPUT: 

  

- ``v`` - a polynomial/vector 

- ``r`` - a ring 

""" 

cdef int res=0 

while v!=NULL: 

res=max(p_GetComp(v, r), res) 

v = pNext(v) 

return res 

  

cdef int singular_polynomial_subst(poly **p, int var_index, poly *value, ring *r) except -1: 

""" 

Substitute variable ``var_index`` with ``value`` in ``p``. 

  

INPUT: 

  

- ``p`` - a polynomial 

- ``var_index`` - an integer < ngens (zero based indexing) 

- ``value`` - a polynomial 

- ``r`` - a ring 

""" 

  

if p_IsConstant(value, r): 

p[0] = pSubst(p[0], var_index+1, value) 

return 0 

  

cdef unsigned long exp = p_GetExp(p[0], var_index+1, r) * p_GetMaxExp(value, r) 

  

overflow_check(exp, r) 

if(r != currRing): 

rChangeCurrRing(r) 

  

cdef int count = singular_polynomial_length_bounded(p[0], 15) 

if unlikely(count >= 15 or exp > 15): sig_on() 

p[0] = pSubst(p[0], var_index+1, value) 

if unlikely(count >= 15 or exp > 15): sig_off() 

return 0