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

""" 

Streams or Infinite Arrays 

 

This code is based on the work of Ralf Hemmecke and Martin Rubey's 

Aldor-Combinat, which can be found at 

http://www.risc.uni-linz.ac.at/people/hemmecke/aldor/combinat/index.html. 

In particular, the relevant section for this file can be found at 

http://www.risc.uni-linz.ac.at/people/hemmecke/AldorCombinat/combinatse12.html. 

""" 

import types 

from sage.structure.sage_object import SageObject 

 

 

def _integers_from(n): 

""" 

Returns a generator for the integers starting at n. 

 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import _integers_from 

sage: g = _integers_from(5) 

sage: [next(g) for i in range(5)] 

[5, 6, 7, 8, 9] 

""" 

while True: 

yield n 

n += 1 

 

def _apply_function(func, list): 

""" 

Returns an iterator for func(i) for i in list. 

 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import _apply_function 

sage: def square(l): 

....: l.append(l[-1]^2) 

....: return l[-1] 

... 

sage: l = [2] 

sage: g = _apply_function(square, l) 

sage: [next(g) for i in range(5)] 

[4, 16, 256, 65536, 4294967296] 

""" 

while True: 

try: 

yield func(list) 

except Exception: 

break 

 

def Stream(x=None, const=None): 

""" 

Returns a stream. 

 

EXAMPLES: We can create a constant stream by just passing a 

 

:: 

 

sage: from sage.combinat.species.stream import Stream 

sage: s = Stream(const=0) 

sage: [s[i] for i in range(10)] 

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

""" 

if const is not None: 

return Stream_class(const=const) 

elif hasattr(x, '__iter__'): 

return Stream_class(iter(x)) 

elif isinstance(x, (types.FunctionType, types.LambdaType)): 

return Stream_class(func=x) 

 

return Stream_class(iter([x,0])) 

 

class Stream_class(SageObject): 

""" 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import Stream 

sage: from builtins import zip 

sage: s = Stream(const=0) 

sage: len(s) 

1 

sage: [x for (x,i) in zip(s, range(4))] 

[0, 0, 0, 0] 

sage: len(s) 

1 

 

:: 

 

sage: s = Stream(const=4) 

sage: g = iter(s) 

sage: l1 = [x for (x,i) in zip(g, range(10))] 

sage: l = [4 for k in range(10)] 

sage: l == l1 

True 

 

:: 

 

sage: h = lambda l: 1 if len(l) < 2 else l[-1] + l[-2] 

sage: fib = Stream(h) 

sage: [x for (x,i) in zip(fib, range(11))] 

[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] 

 

:: 

 

sage: r = [4, 3, 5, 2, 6, 1, 1, 1, 1, 1] 

sage: l = [4, 3, 5, 2, 6, 1] 

sage: s = Stream(l) 

sage: s[3] = -1 

sage: [x for (x,i) in zip(s, r)] 

[4, 3, 5, -1, 6, 1, 1, 1, 1, 1] 

sage: s[5] = -2 

sage: [x for (x,i) in zip(s, r)] 

[4, 3, 5, -1, 6, -2, 1, 1, 1, 1] 

sage: s[6] = -3 

sage: [x for (x,i) in zip(s, r)] 

[4, 3, 5, -1, 6, -2, -3, 1, 1, 1] 

sage: s[8] = -4 

sage: [x for (x,i) in zip(s, r)] 

[4, 3, 5, -1, 6, -2, -3, 1, -4, 1] 

sage: a = Stream(const=0) 

sage: a[2] = 3 

sage: [x for (x,i) in zip(a, range(4))] 

[0, 0, 3, 0] 

""" 

 

def __init__(self, gen=None, const=None, func=None): 

""" 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import Stream_class, Stream 

sage: s = Stream_class(const=4) 

sage: loads(dumps(s)) 

<sage.combinat.species.stream.Stream_class object at ...> 

 

:: 

 

sage: sorted(s.__dict__.items()) 

[('_constant', 4), 

('_gen', None), 

('_last_index', 0), 

('_list', [4]), 

('end_reached', True)] 

 

:: 

 

sage: s = Stream(ZZ) 

sage: sorted(s.__dict__.items()) 

[('_constant', None), 

('_gen', <generator object at 0x...>), 

('_last_index', -1), 

('_list', []), 

('end_reached', False)] 

""" 

#We define self._list up here so that 

#_apply_function can make use of it if 

#it needs to. 

self._list = [] 

 

 

if func is not None: 

if gen is not None: 

raise ValueError("you cannot specify both a function and a generator") 

gen = _apply_function(func, self._list) 

 

#Constant stream 

if const is not None: 

self._list = [const] 

self._last_index = 0 # last_index == len(self._list) - 1 

self._gen = None 

self._constant = const 

self.end_reached = True 

else: 

self._last_index = -1 # last_index == len(self._list) - 1 

self._gen = gen 

self._constant = const 

self.end_reached = False 

 

def __setitem__(self, i, t): 

""" 

Sets the ith entry of self to t. 

 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import Stream 

 

:: 

 

sage: s = Stream(const=0) 

sage: s[5] 

0 

sage: s.data() 

[0] 

sage: s[5] = 5 

sage: s[5] 

5 

sage: s.data() 

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

 

:: 

 

sage: s = Stream(ZZ) 

sage: s[10] 

-5 

sage: s.data() 

[0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5] 

sage: s[10] = 10 

sage: s.data() 

[0, 1, -1, 2, -2, 3, -3, 4, -4, 5, 10] 

""" 

#Compute all of the coefficients up to (and including) the ith one 

test = self[i] 

 

if i < len(self._list): 

#If we are here, we can just change the entry in self._list 

self._list[i] = t 

else: 

#If we are here, then the stream has become constant. We just 

#extend self._list with self._constant and then change the 

#last entry. 

self._list += [ self._constant ] * (i+1 - len(self._list)) 

self._last_index = i 

self._list[i] = t 

 

 

def set_gen(self, gen): 

""" 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import Stream 

sage: from builtins import zip 

sage: fib = Stream() 

sage: def g(): 

....: yield 1 

....: yield 1 

....: n = 0 

....: while True: 

....: yield fib[n] + fib[n+1] 

....: n += 1 

 

:: 

 

sage: fib.set_gen(g()) 

sage: [x for (x,i) in zip(fib, range(11))] 

[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] 

 

:: 

 

sage: l = [4,3,5,2,6,1] 

sage: s = Stream(l) 

sage: s[3] 

2 

sage: len(s) 

4 

sage: g = iter(l) 

sage: s.set_gen(g) 

sage: s[5] 

3 

sage: len(s) 

6 

""" 

self._gen = gen 

self.end_reached = False 

 

def map(self, f): 

""" 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import Stream 

sage: s = Stream(ZZ) 

sage: square = lambda x: x^2 

sage: ss = s.map(square) 

sage: [ss[i] for i in range(10)] 

[0, 1, 1, 4, 4, 9, 9, 16, 16, 25] 

 

TESTS:: 

 

sage: from builtins import zip 

sage: f = lambda l: 0 if len(l) == 0 else l[-1] + 1 

sage: o = Stream(f) 

sage: [x for (x,i) in zip(o, range(10))] 

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

sage: double = lambda z: 2*z 

sage: t = o.map(double) 

sage: [x for (x,i) in zip(t, range(10))] 

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18] 

 

:: 

 

sage: double = lambda z: 2*z 

sage: o = Stream([0,1,2,3]) 

sage: [x for (x,i) in zip(o, range(6))] 

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

sage: t = o.map(double) 

sage: [x for (x,i) in zip(t, range(6))] 

[0, 2, 4, 6, 6, 6] 

""" 

return Stream((f(x) for x in self)) 

 

def __getitem__(self, i): 

""" 

Returns the ith entry of self. 

 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import Stream 

sage: s = Stream(ZZ) 

sage: [s[i] for i in range(10)] 

[0, 1, -1, 2, -2, 3, -3, 4, -4, 5] 

sage: s[1] 

1 

 

:: 

 

sage: s = Stream([1,2,3]) 

sage: [s[i] for i in range(10)] 

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

 

:: 

 

sage: s = Stream(QQ) 

sage: s[10] 

-3 

""" 

if i <= self._last_index: 

return self._list[i] 

elif self.end_reached: 

if self._constant is not False: 

return self._constant 

else: 

raise IndexError("out of position") 

else: 

while self._last_index < i: 

try: 

self._list.append(next(self._gen)) 

self._last_index += 1 

except StopIteration: 

self.end_reached = True 

self._constant = self._list[-1] 

return self[i] 

 

return self._list[i] 

 

 

def __iter__(self): 

""" 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import Stream 

sage: s = Stream([1,2,3]) 

sage: g = iter(s) 

sage: [next(g) for i in range(5)] 

[1, 2, 3, 3, 3] 

""" 

i = 0 

while True: 

try: 

yield self[i] 

except IndexError: 

break 

i += 1 

 

def __len__(self): 

""" 

Returns the number of coefficients computed so far. 

 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import Stream 

sage: l = [4,3,5,7,4,1,9,7] 

sage: s = Stream(l) 

sage: s[3] 

7 

sage: len(s) 

4 

sage: s[3] 

7 

sage: len(s) 

4 

sage: s[1] 

3 

sage: len(s) 

4 

sage: s[4] 

4 

sage: len(s) 

5 

 

TESTS:: 

 

sage: l = ['Hello', ' ', 'World', '!'] 

sage: s = Stream(l) 

sage: len(s) 

0 

sage: s[2] 

'World' 

sage: len(s) 

3 

sage: u = "" 

sage: for i in range(len(s)): u += s[i] 

sage: u 

'Hello World' 

sage: v = "" 

sage: for i in range(10): v += s[i] 

sage: v 

'Hello World!!!!!!!' 

sage: len(s) 

4 

""" 

return len(self._list) 

 

number_computed = __len__ 

 

def data(self): 

""" 

Returns a list of all the coefficients computed so far. 

 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import Stream, _integers_from 

sage: s = Stream(_integers_from(3)) 

sage: s.data() 

[] 

sage: s[5] 

8 

sage: s.data() 

[3, 4, 5, 6, 7, 8] 

""" 

return self._list 

 

def is_constant(self): 

""" 

Returns True if and only if 

 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import Stream 

sage: s = Stream([1,2,3]) 

sage: s.is_constant() 

False 

sage: s[3] 

3 

sage: s.data() 

[1, 2, 3] 

sage: s.is_constant() 

True 

 

TESTS:: 

 

sage: l = [2,3,5,7,11,0] 

sage: s = Stream(l) 

sage: s.is_constant() 

False 

sage: s[3] 

7 

sage: s.is_constant() 

False 

sage: s[5] 

0 

sage: s.is_constant() 

False 

sage: s[6] 

0 

sage: s.is_constant() 

True 

 

:: 

 

sage: s = Stream(const='I am constant.') 

sage: s.is_constant() 

True 

""" 

return self.end_reached 

 

 

def stretch(self, k): 

""" 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import Stream 

sage: s = Stream(range(1, 10)) 

sage: s2 = s.stretch(2) 

sage: [s2[i] for i in range(10)] 

[1, 0, 2, 0, 3, 0, 4, 0, 5, 0] 

""" 

return Stream(self._stretch_gen(k)) 

 

def _stretch_gen(self, k): 

""" 

EXAMPLES:: 

 

sage: from sage.combinat.species.stream import Stream 

sage: s = Stream(range(1, 10)) 

sage: g = s._stretch_gen(2) 

sage: [next(g) for i in range(10)] 

[1, 0, 2, 0, 3, 0, 4, 0, 5, 0] 

""" 

yield self[0] 

for i in _integers_from(1): 

for j in range(k-1): 

yield 0 

yield self[i]