catena.generators
Generators for partial-quotient sequences used in continued fraction expansions.
This module provides four callable generator types:
- :class:
Generator— a thin wrapper around any callablef: int -> intthat validates its output (positive integer) on every call. - :class:
CachedGenerator— aGeneratorthat memoises results in an :class:~catena.cache.OrdinalCacheto avoid redundant computation. - :class:
FiniteGenerator— aGeneratorbacked by a fixed sequence of integers stored as a compact :class:array.array(or a plain tuple for values exceeding the 64-bit range). Supports identity short-circuit: passing an existing :class:FiniteGeneratorreturns the same object unchanged. - :class:
PeriodicGenerator— aGeneratorthat produces an eventually periodic sequence from an aperiodic pre-period followed by an infinitely repeating period. Provides :meth:~PeriodicGenerator.cycle_quadratic_coefficientsand :meth:~PeriodicGenerator.quadratic_coefficientsto recover the quadratic equation satisfied by the represented irrational.
All types share the same calling convention: generator(n) returns the
n-th partial quotient (0-indexed), which must always be a strictly positive
integer.
CachedGenerator
Bases: Generator
A :class:Generator that memoises results in an
:class:~catena.cache.OrdinalCache.
On the first call for a given index n the underlying function is invoked and its result stored; subsequent calls for the same n are served directly from the cache.
Source code in catena/generators.py
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 | |
cache
property
The underlying :class:~catena.cache.BaseCache storing computed values.
cache_handler
property
The :class:~catena.cache.BaseCache managing the cache lifecycle.
__init__(generator, *args, seed=None, **kwargs)
Initialises the cached generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generator
|
Callable[[int], int] | CachedGenerator
|
A callable that maps a
non-negative index n to a strictly positive integer. If a
:class: |
required |
seed
|
Optional[dict]
|
Pre-computed |
None
|
Source code in catena/generators.py
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 | |
advance(n, copy_cache=False)
Advances the generator by n steps, optionally copying relevant cache entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative number of steps to advance. |
required |
copy_cache
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
CachedGenerator |
CachedGenerator
|
A new generator that produces the same sequence starting from the n-th term, with cache entries copied if requested. |
Raises:
ValueError: If n is negative.
Source code in catena/generators.py
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 | |
insert(fg, at, copy_cache=False, *args, **kwargs)
Inserts another generator into this one at a specified index, optionally copying relevant cache entries.
Args:
fg (FiniteGenerator): The generator to insert.
at (int): The non-negative index at which to insert the new generator.
The first term of fg will become the at-th term of the resulting sequence.
copy_cache (bool, optional): If True, cache entries for indices
greater than or equal to at are copied to the new generator,
adjusted to reflect the new indexing. If False (default),
the new generator starts with an empty cache.
Returns:
CachedGenerator: A new generator that produces the combined sequence with fg inserted
at the specified index, with cache entries copied if requested.
Raises:
ValueError: If at is negative.
TypeError: If fg is not a :class:FiniteGenerator.
Source code in catena/generators.py
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 | |
reset_cache()
Clears all entries from the cache, freeing the memoised results.
Source code in catena/generators.py
292 293 294 | |
FiniteGenerator
Bases: Generator, Sized
A :class:Generator backed by a fixed sequence of positive integers.
The sequence is stored as a compact :class:array.array using the
smallest unsigned typecode whose range covers all values in data
(B → 8-bit, H → 16-bit, I → 32-bit, L → 32/64-bit,
Q → 64-bit). If any value exceeds the 64-bit range the sequence
falls back to an immutable :class:tuple and is_compact is
False.
Source code in catena/generators.py
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 | |
dtype
property
Array typecode used for compact storage, or None if arbitrary-precision.
end
property
Last element of the sequence, or None if empty.
is_compact
property
True if the data is stored as an :class:array.array, False for tuple fallback.
max
property
Largest value in the sequence, or None if empty.
min
property
Smallest value in the sequence, or None if empty.
size
property
Number of elements in the sequence.
start
property
First element of the sequence, or None if empty.
view
property
A read-only :class:memoryview over the underlying array buffer.
Raises:
| Type | Description |
|---|---|
TypeError
|
If the data is stored as a tuple (arbitrary-precision fallback) and a memoryview cannot be constructed. |
__init__(data, dtype=None, *args, **kwargs)
Initialises the finite generator from a sequence of positive integers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[int] | FiniteGenerator
|
The sequence of strictly positive partial
quotients. If a :class: |
required |
dtype
|
str
|
Force a specific unsigned array typecode
(one of |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in catena/generators.py
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 | |
concatenate(fg)
Concatenates another FiniteGenerator to this one, returning a new FiniteGenerator that produces the combined sequence.
Equivalent to self.insert(fg, at=self.size).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fg
|
FiniteGenerator
|
The generator to concatenate. The first term of |
required |
Returns:
| Name | Type | Description |
|---|---|---|
FiniteGenerator |
Self
|
A new generator that produces the combined sequence with |
Source code in catena/generators.py
447 448 449 450 451 452 453 454 455 456 457 458 | |
Generator
A validated callable wrapper for partial-quotient generator functions.
Wraps any callable f(n: int) -> int and enforces that every call
returns a strictly positive integer. If the wrapped object is already a
:class:Generator instance, it is returned unchanged (see
:meth:__new__).
Source code in catena/generators.py
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 | |
__call__(n)
Calls the underlying generator and validates its output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative index of the partial quotient to retrieve. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The n-th partial quotient (strictly positive). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in catena/generators.py
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 | |
__init__(generator)
Initialises the generator wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generator
|
Callable[[int], int]
|
A callable that maps a non-negative index n to a strictly positive integer. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in catena/generators.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
__new__(generator, *args, **kwargs)
Returns the existing instance if generator is already a
:class:Generator, avoiding unnecessary double-wrapping.
Source code in catena/generators.py
171 172 173 174 175 176 177 178 | |
advance(n)
Advances the generator by n steps, returning a new generator that produces the same sequence starting from the n-th term.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative number of steps to advance. |
required |
Returns:
Generator: A new generator that produces the same sequence starting from the n-th term.
Raises:
ValueError: If n is negative.
Source code in catena/generators.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
insert(fg, at, *args, **kwargs)
Inserts another generator into this one at a specified index, returning a new generator that produces the combined sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fg
|
FiniteGenerator
|
The generator to insert. |
required |
at
|
int
|
The non-negative index at which to insert the new generator.
The first term of |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Generator |
Generator
|
A new generator that produces the combined sequence with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
Source code in catena/generators.py
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 | |
prepend(fg, *args, **kwargs)
Prepends another generator to this one, returning a new generator that produces the combined sequence.
Equivalent to self.insert(fg, at=0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fg
|
FiniteGenerator
|
The generator to prepend. The first term of |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Generator |
Generator
|
A new generator that produces the combined sequence with |
Source code in catena/generators.py
129 130 131 132 133 134 135 136 137 138 139 140 | |
PeriodicGenerator
Bases: Generator
A :class:Generator that produces a periodic sequence of positive integers.
The sequence is defined by a finite list of integers representing the pre-period followed by a finite list representing the period. For example, the simple continued fraction expansion of \((7+\sqrt{10})/4\) is \([2; 1, 1, 5, 1, 1, 1, 24, 1, 1, 1, 5, \ldots]\), where the integer part is \(2\), the pre-period is \([1, 1]\), and the period is \([5, 1, 1, 1, 24, 1, 1, 1]\).
Source code in catena/generators.py
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 | |
period
property
The finite sequence of positive integers representing the period.
pre_period
property
The finite sequence of positive integers representing the aperiodic pre-period.
__init__(period, pre_period=(), dtypes=(None, None), *args, **kwargs)
Initialises the periodic generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
period
|
Sequence[int] | FiniteGenerator | PeriodicGenerator
|
The finite sequence of positive integers
representing the periodic part of the continued fraction. If a :class: |
required |
pre_period
|
Sequence[int] | FiniteGenerator
|
The finite sequence of positive integers representing the aperiodic pre-period (default: empty). |
()
|
dtypes
|
Tuple[Optional[str], Optional[str]]
|
Force specific :class: |
(None, None)
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any value in |
ValueError
|
If |
Source code in catena/generators.py
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 | |
__new__(period, pre_period=(), *args, **kwargs)
Returns the existing instance if period is already a
:class:PeriodicGenerator with no pre-period, avoiding unnecessary
double-wrapping.
Source code in catena/generators.py
687 688 689 690 691 692 693 694 695 | |
cycle_quadratic_coefficients()
Returns the coefficients of the corresponding quadratic polynomial for the period only.
The quadratic polynomial is derived from the periodic part of the continued fraction expansion, and its roots correspond to the value of the infinite periodic continued fraction defined by the period (with a_0 = 0).
Returns:
| Type | Description |
|---|---|
int
|
tuple[int, int, int]: Coefficients (A, B, C) of the quadratic polynomial Ax^2 + Bx + C = 0, |
int
|
where A > 0 and gcd(A, B, C) = 1. |
Source code in catena/generators.py
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 | |
insert(fg, at, *args, **kwargs)
Inserts a finite generator into this periodic generator at a specified index, returning a new periodic generator that produces the combined sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fg
|
FiniteGenerator
|
The generator to insert. |
required |
at
|
int
|
The non-negative index at which to insert the new generator.
The first term of |
required |
Returns:
| Name | Type | Description |
|---|---|---|
PeriodicGenerator |
PeriodicGenerator
|
A new generator that produces the combined sequence with |
Source code in catena/generators.py
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 | |
quadratic_coefficients()
Returns the coefficients of the corresponding quadratic polynomial.
The quadratic polynomial is derived from the periodic part of the continued fraction expansion, and its roots correspond to the value of the infinite periodic continued fraction defined by the period and the pre-period (with a_0 = 0). If a pre-period is present, the coefficients are adjusted to account for it.
Returns:
| Type | Description |
|---|---|
int
|
tuple[int, int, int]: Coefficients (A, B, C) of the quadratic polynomial Ax^2 + Bx + C = 0, |
int
|
where A > 0 and gcd(A, B, C) = 1. |
Source code in catena/generators.py
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 | |