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

""" 

C Int Lists 

  

This is a class for fast basic operations with lists of C ints. It is 

similar to the double precision TimeSeries class. It has all the 

standard C int semantics, of course, including overflow. It is also 

similar to the Python list class, except all elements are C ints, 

which makes some operations much, much faster. For example, 

concatenating two IntLists can be over 10 times faster than 

concatenating the corresponding Python lists of ints, and taking 

slices is also much faster. 

  

AUTHOR: 

  

- William Stein, 2010-03 

""" 

  

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

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

# 

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

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

  

# Global parameter that sets the maximum number of entries of an IntList to print. 

max_print = 10 

  

from libc.string cimport memcpy 

from cysignals.memory cimport sig_malloc, sig_free 

from cysignals.signals cimport sig_on, sig_off 

  

from sage.rings.integer import Integer 

from sage.finance.time_series cimport TimeSeries 

from cpython.bytes cimport PyBytes_FromStringAndSize, PyBytes_AsString 

from sage.structure.richcmp cimport rich_to_bool 

  

  

cdef class IntList: 

""" 

A list of C int's. 

""" 

def __cinit__(self): 

""" 

Create new empty uninitialized IntList. 

  

EXAMPLES:: 

  

sage: stats.IntList(5) # indirect test 

[0, 0, 0, 0, 0] 

  

""" 

self._values = NULL 

  

def __init__(self, values): 

""" 

Create an initialized list of C ints. 

  

INPUT: 

  

- values -- int, long, Integer, list of integers, or a TimeSeries 

  

If the input is a time series or list of floats, then the 

integer parts of the intries are taken (not the floor). 

  

EXAMPLES:: 

  

sage: stats.IntList(8) 

[0, 0, 0, 0, 0, 0, 0, 0] 

  

sage: stats.IntList([1,5,-39392]) 

[1, 5, -39392] 

  

We check for overflow when creating the IntList:: 

  

sage: stats.IntList([1, 3, 2^32]) 

Traceback (most recent call last): 

... 

OverflowError: ... too large to convert to C long # 32-bit 

OverflowError: ... too large to convert to int # 64-bit 

  

Printing omits entries:: 

  

sage: stats.IntList(1000) 

[0, 0, 0, 0, 0 ... 0, 0, 0, 0, 0] 

  

Floats are truncated to their integer parts:: 

  

sage: stats.IntList([1.1, -2.6]) 

[1, -2] 

sage: stats.IntList(stats.TimeSeries([1.1, -2.6])) 

[1, -2] 

""" 

cdef TimeSeries T 

if isinstance(values, (int,long,Integer)): 

self._length = values 

values = None 

elif isinstance(values, TimeSeries): 

T = values 

self._length = T._length 

else: 

self._length = len(values) 

  

self._values = <int*> sig_malloc(sizeof(int)*self._length) 

if self._values == NULL: 

raise MemoryError 

cdef Py_ssize_t i 

if values is None: 

for i in range(self._length): 

self._values[i] = 0 

elif isinstance(values, TimeSeries): 

for i in range(self._length): 

self._values[i] = <int> T._values[i] 

else: 

for i in range(self._length): 

self._values[i] = values[i] 

  

def __richcmp__(IntList self, other, int op): 

""" 

Compare self and other. This has the same semantics 

as list comparison. 

  

EXAMPLES:: 

  

sage: v = stats.IntList([1,2,3]); w = stats.IntList([1,2]) 

sage: v < w 

False 

sage: w < v 

True 

sage: v == v 

True 

sage: w == w 

True 

""" 

cdef IntList _other 

cdef Py_ssize_t c, i 

if not isinstance(other, IntList): 

_other = IntList(other) 

else: 

_other = <IntList>other 

for i in range(min(self._length, _other._length)): 

d = self._values[i] - _other._values[i] 

if d: 

return rich_to_bool(op, -1 if d < 0 else 1) 

c = self._length - _other._length 

if c: 

return rich_to_bool(op, -1 if c < 0 else 1) 

return rich_to_bool(op, 0) 

  

def __dealloc__(self): 

""" 

Deallocate memory used by the IntList, if it was allocated. 

""" 

sig_free(self._values) 

  

def __repr__(self): 

""" 

Return string representation of this IntList. 

  

EXAMPLES:: 

  

sage: a = stats.IntList([1..15]); a.__repr__() 

'[1, 2, 3, 4, 5 ... 11, 12, 13, 14, 15]' 

sage: sage.stats.intlist.max_print = 20 

sage: a.__repr__() 

'[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]' 

sage: sage.stats.intlist.max_print = 10 

sage: a.__repr__() 

'[1, 2, 3, 4, 5 ... 11, 12, 13, 14, 15]' 

""" 

if len(self) > max_print: 

v0 = self[:max_print//2] 

v1 = self[-max_print//2:] 

return '[' + ', '.join([str(x) for x in v0]) + ' ... ' + \ 

', '.join([str(x) for x in v1]) + ']' 

else: 

return str(self.list()) 

  

def __getitem__(self, i): 

""" 

Return i-th entry or slice of self, following standard Python 

semantics. The returned slice is an intlist, and the returned 

entry is a Python int. 

  

INPUT: 

  

- i -- integer or slice 

  

EXAMPLES:: 

  

sage: a = stats.IntList([0..9]); a 

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

sage: a[5] 

5 

sage: a[-2] 

8 

sage: a[5:-2] 

[5, 6, 7] 

sage: type(a[5:-2]) 

<... 'sage.stats.intlist.IntList'> 

sage: type(a[5]) 

<... 'int'> 

""" 

cdef Py_ssize_t start, stop, step, j 

cdef IntList t 

if isinstance(i, slice): 

start = 0 if (i.start is None) else i.start 

stop = self._length if (i.stop is None) else i.stop 

step = 1 if (i.step is None) else i.step 

if start < 0: 

start += self._length 

if start < 0: start = 0 

elif start >= self._length: 

start = self._length - 1 

if stop < 0: 

stop += self._length 

if stop < 0: stop = 0 

elif stop > self._length: 

stop = self._length 

if start >= stop: 

return new_int_list(0) 

if step < 0: 

step = -step 

t = new_int_list((stop-start)/step) 

for j from 0 <= j < (stop-start)/step: 

t._values[j] = self._values[stop-1 - j*step] 

elif step > 1: 

t = new_int_list((stop-start)/step) 

for j from 0 <= j < (stop-start)/step: 

t._values[j] = self._values[j*step+start] 

else: 

t = new_int_list(stop-start) 

memcpy(t._values, self._values + start, sizeof(int)*t._length) 

return t 

else: 

j = i 

if j < 0: 

j += self._length 

if j < 0: 

raise IndexError("IntList index out of range") 

elif j >= self._length: 

raise IndexError("IntList index out of range") 

return self._values[j] 

  

def __setitem__(self, Py_ssize_t i, int x): 

""" 

Set the i-th entry of self, following standard Python semantics. 

  

INPUT: 

  

- i -- an integer 

- x -- an int 

  

EXAMPLES:: 

  

sage: a = stats.IntList([-2,3,7,-4]) 

sage: a[1] = 10393; a 

[-2, 10393, 7, -4] 

sage: a[-1] = -10; a 

[-2, 10393, 7, -10] 

sage: a[100] 

Traceback (most recent call last): 

... 

IndexError: IntList index out of range 

sage: a[-100] 

Traceback (most recent call last): 

... 

IndexError: IntList index out of range 

""" 

if i < 0: 

i += self._length 

if i < 0: 

raise IndexError("index out of range") 

elif i >= self._length: 

raise IndexError("index out of range") 

self._values[i] = x 

  

def __reduce__(self): 

""" 

Used in pickling int lists. 

  

EXAMPLES:: 

  

sage: a = stats.IntList([-2,3,7,-4]) 

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

True 

  

  

sage: v = stats.IntList([1,-3]) 

sage: v.__reduce__() # py2 

(<built-in function unpickle_intlist_v1>, ('...', 2)) 

sage: v.__reduce__() # py3 

(<built-in function unpickle_intlist_v1>, (b'...', 2)) 

sage: loads(dumps(v)) == v 

True 

  

Note that dumping and loading with compress False is much faster, though 

dumping with compress True can save a lot of space:: 

  

sage: v = stats.IntList([1..10^5]) 

sage: loads(dumps(v, compress=False),compress=False) == v 

True 

  

""" 

buf = PyBytes_FromStringAndSize(<char*>self._values, self._length*sizeof(int)/sizeof(char)) 

return unpickle_intlist_v1, (buf, self._length) 

  

def list(self): 

""" 

Return Python list version of self with Python ints as entries. 

  

EXAMPLES:: 

  

sage: a = stats.IntList([1..15]); a 

[1, 2, 3, 4, 5 ... 11, 12, 13, 14, 15] 

sage: a.list() 

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 

sage: list(a) == a.list() 

True 

sage: type(a.list()[0]) 

<... 'int'> 

""" 

cdef Py_ssize_t i 

return [self._values[i] for i in range(self._length)] 

  

cpdef int sum(self): 

""" 

Return the sum of the entries of self. 

  

EXAMPLES:: 

  

sage: stats.IntList([1..100]).sum() 

5050 

  

Note that there can be overflow, since the entries are C ints:: 

  

sage: a = stats.IntList([2^30,2^30]); a 

[1073741824, 1073741824] 

sage: a.sum() 

-2147483648 

""" 

cdef Py_ssize_t i 

cdef int s=0 

sig_on() 

for i in range(self._length): 

s += self._values[i] 

sig_off() 

return s 

  

cpdef int prod(self): 

""" 

Return the product of the entries of self. 

  

EXAMPLES:: 

  

sage: a = stats.IntList([1..10]); a 

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

sage: a.prod() 

3628800 

sage: factorial(10) 

3628800 

  

Note that there can be overflow:: 

  

sage: a = stats.IntList([2^30, 2]); a 

[1073741824, 2] 

sage: a.prod() 

-2147483648 

""" 

cdef Py_ssize_t i 

cdef int s=1 

sig_on() 

for i in range(self._length): 

s *= self._values[i] 

sig_off() 

return s 

  

def __len__(self): 

""" 

Return the number of entries in this time series. 

  

OUTPUT: 

  

Python integer 

  

EXAMPLES:: 

  

sage: len(stats.IntList([1..15])) 

15 

sage: len(stats.IntList([])) 

0 

sage: len(stats.IntList(10^6)) 

1000000 

""" 

return self._length 

  

def __add__(left, right): 

""" 

Concatenate the integer lists self and right. 

  

EXAMPLES:: 

  

sage: stats.IntList([-2,3,5]) + stats.IntList([1,1,17]) 

[-2, 3, 5, 1, 1, 17] 

""" 

if not isinstance(right, IntList): 

raise TypeError("right operand must be an int list") 

if not isinstance(left, IntList): 

raise TypeError("left operand must be an int list") 

cdef IntList R = right 

cdef IntList L = left 

cdef IntList t = new_int_list(L._length + R._length) 

memcpy(t._values, L._values, sizeof(int)*L._length) 

memcpy(t._values + L._length, R._values, sizeof(int)*R._length) 

return t 

  

def min(self, bint index=False): 

""" 

Return the smallest value in this integer list. If this 

series has length 0 we raise a ValueError. 

  

INPUT: 

  

- index -- bool (default: False); if True, also return 

index of minimal entry. 

  

OUTPUT: 

  

- float -- smallest value 

- integer -- index of smallest value; only returned if 

index=True 

  

EXAMPLES:: 

  

sage: v = stats.IntList([1,-4,3,-2,-4]) 

sage: v.min() 

-4 

sage: v.min(index=True) 

(-4, 1) 

""" 

if self._length == 0: 

raise ValueError("min() arg is an empty sequence") 

cdef Py_ssize_t i, j 

cdef int s = self._values[0] 

j = 0 

for i in range(1, self._length): 

if self._values[i] < s: 

s = self._values[i] 

j = i 

if index: 

return s, j 

else: 

return s 

  

def max(self, bint index=False): 

""" 

Return the largest value in this time series. If this series 

has length 0 we raise a ValueError 

  

INPUT: 

  

- index -- bool (default: False); if True, also return 

index of maximum entry. 

  

OUTPUT: 

  

- int -- largest value 

- int -- index of largest value; only returned if index=True 

  

EXAMPLES:: 

  

sage: v = stats.IntList([1,-4,3,-2,-4,3]) 

sage: v.max() 

3 

sage: v.max(index=True) 

(3, 2) 

""" 

if self._length == 0: 

raise ValueError("max() arg is an empty sequence") 

cdef Py_ssize_t i, j = 0 

cdef int s = self._values[0] 

for i in range(1,self._length): 

if self._values[i] > s: 

s = self._values[i] 

j = i 

if index: 

return s, j 

else: 

return s 

  

def time_series(self): 

""" 

Return TimeSeries version of self, which involves changing 

each entry to a double. 

  

EXAMPLES:: 

  

sage: T = stats.IntList([-2,3,5]).time_series(); T 

[-2.0000, 3.0000, 5.0000] 

sage: type(T) 

<... 'sage.finance.time_series.TimeSeries'> 

""" 

cdef TimeSeries T = TimeSeries.__new__(TimeSeries) 

# We just reach into the data structure underlying T, since we 

# want this function to be *very* fast. 

T._length = self._length 

T._values = <double*> sig_malloc(sizeof(double)*self._length) 

cdef Py_ssize_t i 

for i in range(self._length): 

T._values[i] = self._values[i] 

return T 

  

def plot(self, *args, **kwds): 

""" 

Return a plot of this IntList. This just constructs the 

corresponding double-precision floating point TimeSeries 

object, passing on all arguments. 

  

EXAMPLES:: 

  

sage: stats.IntList([3,7,19,-2]).plot() 

Graphics object consisting of 1 graphics primitive 

sage: stats.IntList([3,7,19,-2]).plot(color='red',pointsize=50,points=True) 

Graphics object consisting of 1 graphics primitive 

""" 

return self.time_series().plot(*args, **kwds) 

  

def plot_histogram(self, *args, **kwds): 

""" 

Return a histogram plot of this IntList. This just constructs 

the corresponding double-precision floating point TimeSeries object, 

and plots it, passing on all arguments. 

  

EXAMPLES:: 

  

sage: stats.IntList([1..15]).plot_histogram() 

Graphics object consisting of 50 graphics primitives 

""" 

return self.time_series().plot_histogram(*args, **kwds) 

  

  

cdef IntList new_int_list(Py_ssize_t length): 

""" 

Function that is used internally to quickly create a new intlist 

without initializing any of the allocated memory. 

  

INPUT: 

  

- length -- a nonnegative integer 

  

OUTPUT: 

  

- an IntList. 

""" 

if length < 0: 

raise ValueError("length must be nonnegative") 

cdef IntList t = IntList.__new__(IntList) 

t._length = length 

t._values = <int*> sig_malloc(sizeof(int)*length) 

return t 

  

  

def unpickle_intlist_v1(bytes v, Py_ssize_t n): 

""" 

Version 1 unpickle method. 

  

INPUT: 

  

- ``v`` -- a raw char buffer 

  

EXAMPLES:: 

  

sage: v = stats.IntList([1,2,3]) 

sage: s = v.__reduce__()[1][0] 

sage: type(s) 

<... 'str'> 

sage: sage.stats.intlist.unpickle_intlist_v1(s, 3) 

[1, 2, 3] 

sage: sage.stats.intlist.unpickle_intlist_v1(s+s,6) 

[1, 2, 3, 1, 2, 3] 

sage: sage.stats.intlist.unpickle_intlist_v1('',0) 

[] 

""" 

cdef IntList t = new_int_list(n) 

memcpy(t._values, PyBytes_AsString(v), n*sizeof(int)) 

return t