|
| 1 | +# algebraic-range |
| 2 | + |
| 3 | + |
| 4 | +[](https://github.com/Daniele-Gregori/PyPI-packages/actions/workflows/algebraic-range.yml) |
| 5 | +[](https://pypi.org/project/algebraic-range/) |
| 6 | +[](https://pypi.org/project/algebraic-range/) |
| 7 | +[](https://opensource.org/licenses/MIT) |
| 8 | + |
| 9 | +Generate ranges of algebraic numbers. |
| 10 | + |
| 11 | +Python port of the **Wolfram** [resource function "AlgebraicRange"](https://resources.wolframcloud.com/FunctionRepository/resources/AlgebraicRange/) resource function. |
| 12 | + |
| 13 | +Requires [SymPy](https://www.sympy.org/) ≥ 1.12. |
| 14 | + |
| 15 | +## Overview |
| 16 | + |
| 17 | +`algebraic_range` creates ranges made of [algebraic numbers](https://en.wikipedia.org/wiki/Algebraic_number). This extends the basic concept of `range()` to include, besides rational numbers, also roots — always restricted to the real domain. |
| 18 | + |
| 19 | +The first two arguments represent the bounds of the range (minimum and maximum values), while the optional third and fourth arguments (by default equal to 1 and 0) regulate the upper and lower bounds of the steps (differences between successive elements). |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +```python |
| 26 | +algebraic_range(x) # Sqrt[Range[1, x²]] for x ≥ 1 |
| 27 | +algebraic_range(x, y) # Sqrt[Range[x², y²]] for 0 ≤ x ≤ y |
| 28 | +algebraic_range(x, y, s) # step upper bound s, 0 < s ≤ y |
| 29 | +algebraic_range(x, y, s, d) # step lower bound d, 0 ≤ d ≤ s |
| 30 | +``` |
| 31 | + |
| 32 | +### Parameters |
| 33 | + |
| 34 | +| Parameter | Default | Description | |
| 35 | +|-----------|---------|-------------| |
| 36 | +| `r1` | *(required)* | Start of range (or single argument) | |
| 37 | +| `r2` | `None` | End of range | |
| 38 | +| `s` | `None` | Step upper bound (negative → descending) | |
| 39 | +| `d` | `0` | Step lower bound | |
| 40 | +| `root_order` | `2` | `int r` → orders 2..r; `[r]` → only order r; `[r1,r2,…]` → listed orders | |
| 41 | +| `step_method` | `"Outer"` | `"Outer"` or `"Root"` | |
| 42 | +| `farey_range` | `False` | Use Farey-sequence–based rational multipliers | |
| 43 | +| `formula_complexity_threshold` | `inf` | Discard elements above this complexity | |
| 44 | +| `algebraics_only` | `True` | Reject transcendental inputs | |
| 45 | + |
| 46 | +### Options |
| 47 | + |
| 48 | +#### `root_order` |
| 49 | + |
| 50 | +```python |
| 51 | +# Include square and cubic roots |
| 52 | +algebraic_range(2, root_order=3) |
| 53 | + |
| 54 | +# Only cubic roots |
| 55 | +algebraic_range(2, root_order=[3]) |
| 56 | + |
| 57 | +# Cubic and fifth roots |
| 58 | +algebraic_range(1, Rational(3, 2), root_order=[3, 5]) |
| 59 | +``` |
| 60 | + |
| 61 | +#### `step_method` |
| 62 | + |
| 63 | +```python |
| 64 | +# "Root" method: Sqrt[Range[x², y², s²]] |
| 65 | +algebraic_range(0, 3, Rational(1, 3), step_method="Root") |
| 66 | +``` |
| 67 | + |
| 68 | +The default `"Outer"` method uses the outer product construction. The `"Root"` method is generally a superset. |
| 69 | + |
| 70 | +#### `farey_range` |
| 71 | + |
| 72 | +```python |
| 73 | +algebraic_range(0, 3, Rational(1, 3), farey_range=True) |
| 74 | +``` |
| 75 | + |
| 76 | +Generalises `FareyRange` by combining algebraic ranges over all Farey-sequence steps. |
| 77 | + |
| 78 | +#### `formula_complexity_threshold` |
| 79 | + |
| 80 | +```python |
| 81 | +# Only keep simple expressions |
| 82 | +algebraic_range(4, root_order=4, formula_complexity_threshold=8) |
| 83 | +``` |
| 84 | + |
| 85 | +#### `algebraics_only` |
| 86 | + |
| 87 | +```python |
| 88 | +from sympy import sqrt, E |
| 89 | + |
| 90 | +# This raises NotAlgebraicError: |
| 91 | +# algebraic_range(0, 5, sqrt(E)) |
| 92 | + |
| 93 | +# Allow transcendental step: |
| 94 | +algebraic_range(0, 5, sqrt(E), algebraics_only=False) |
| 95 | +``` |
| 96 | + |
| 97 | +## Properties |
| 98 | + |
| 99 | +- **Extends `range()`**: `set(range(x, y+1))` ⊆ `set(algebraic_range(x, y))` |
| 100 | +- **Negative reflection**: `algebraic_range(-y, -x)` = `list(reversed([-v for v in algebraic_range(x, y)]))` |
| 101 | +- **All outputs are algebraic** (when `algebraics_only=True`) and real |
| 102 | +- **Sorted** in ascending order (or descending for negative step) |
| 103 | +- **No duplicates** |
| 104 | + |
| 105 | + |
| 106 | +## See also |
| 107 | + |
| 108 | +More details and examples can be found in the documentation for the original Wolfram Language resource function [`AlgebraicRange`](https://resources.wolframcloud.com/FunctionRepository/resources/AlgebraicRange/), contributed by the same author and vetted by the Wolfram Review Team. |
| 109 | + |
0 commit comments