catena.mathlib
mathlib.arithmetic
add_fractions(a, b)
Adds two fractions represented as tuples of (numerator, denominator). Reduces intermediate values by first dividing out gcd(q, s) before multiplying, then simplifying the resulting numerator. It also has the side effect of returning a canonicalized result when the input fractions are in their canonical form, since the denominator since the denominator is guaranteed to be positive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
IntPair
|
The first fraction as a tuple (numerator, denominator). |
required |
b
|
IntPair
|
The second fraction as a tuple (numerator, denominator). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IntPair |
IntPair
|
The result of the addition as a simplified fraction. |
Source code in catena/mathlib/arithmetic.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
multiply_fractions(a, b)
Multiplies two fractions represented as tuples of (numerator, denominator). Reduces intermediate values by first dividing out gcd(p, s) and gcd(r, q) before multiplying, then simplifying the resulting numerator. It also has the side effect of returning a canonicalized result, since the denominator is guaranteed to be positive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
IntPair
|
The first fraction as a tuple (numerator, denominator). |
required |
b
|
IntPair
|
The second fraction as a tuple (numerator, denominator). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IntPair |
IntPair
|
The result of the multiplication as a simplified fraction. |
Source code in catena/mathlib/arithmetic.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
sandwich_fraction(a, b)
Computes the "sandwich" of two fractions represented as tuples of (numerator, denominator). The sandwich is defined as (ps, qr) for fractions (p/q) and (r/s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
IntPair
|
The first fraction as a tuple (numerator, denominator). |
required |
b
|
IntPair
|
The second fraction as a tuple (numerator, denominator). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IntPair |
IntPair
|
The result of the sandwich operation as a fraction. |
Source code in catena/mathlib/arithmetic.py
123 124 125 126 127 128 129 130 131 132 133 134 135 | |
square_fraction(a)
Squares a fraction represented as a tuple of (numerator, denominator). It assumes that the fraction is already in its simplest form, so it simply squares the numerator and denominator without further reduction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
IntPair
|
The fraction to be squared as a tuple (numerator, denominator). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IntPair |
IntPair
|
The result of the squaring as a fraction. |
Source code in catena/mathlib/arithmetic.py
92 93 94 95 96 97 98 99 100 101 102 103 104 | |
uadd_fractions(p, q, r, s)
Adds two fractions represented as tuples of (numerator, denominator). Reduces intermediate values by first dividing out gcd(q, s) before multiplying, then simplifying the resulting numerator. It also has the side effect of returning a canonicalized result when the input fractions are in their canonical form, since the denominator since the denominator is guaranteed to be positive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
int
|
The numerator of the first fraction. |
required |
q
|
int
|
The denominator of the first fraction. |
required |
r
|
int
|
The numerator of the second fraction. |
required |
s
|
int
|
The denominator of the second fraction. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IntPair |
IntPair
|
The result of the addition as a simplified fraction. |
Source code in catena/mathlib/arithmetic.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
umultiply_fractions(p, q, r, s)
Multiplies two fractions represented as separate numerator and denominator integers. Reduces intermediate values by first dividing out gcd(p, s) and gcd(r, q) before multiplying, then simplifying the resulting numerator. It also has the side effect of returning a canonicalized result, since the denominator is guaranteed to be positive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
int
|
The numerator of the first fraction. |
required |
q
|
int
|
The denominator of the first fraction. |
required |
r
|
int
|
The numerator of the second fraction. |
required |
s
|
int
|
The denominator of the second fraction. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IntPair |
IntPair
|
The result of the multiplication as a simplified fraction. |
Source code in catena/mathlib/arithmetic.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
usandwich_fraction(p, q, r, s)
Computes the "sandwich" of two fractions represented as separate numerator and denominator integers. The sandwich is defined as (ps, qr) for fractions (p/q) and (r/s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
int
|
The numerator of the first fraction. |
required |
q
|
int
|
The denominator of the first fraction. |
required |
r
|
int
|
The numerator of the second fraction. |
required |
s
|
int
|
The denominator of the second fraction. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IntPair |
IntPair
|
The result of the sandwich operation as a fraction. |
Source code in catena/mathlib/arithmetic.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
usquare_fraction(p, q)
Squares a fraction represented as separate numerator and denominator integers. It assumes that the fraction is already in its simplest form, so it simply squares the numerator and denominator without further reduction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
int
|
The numerator of the fraction. |
required |
q
|
int
|
The denominator of the fraction. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IntPair |
IntPair
|
The result of the squaring as a fraction. |
Source code in catena/mathlib/arithmetic.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
mathlib.convert
from_decimal_to_rational(d)
Converts a decimal string to an exact rational number in lowest terms.
The string must represent a finite decimal (e.g. "3.14" or "42").
The resulting fraction is simplified via :func:~catena.mathlib.core.simplify.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
str
|
A finite decimal string to convert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Rational |
Rational
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in catena/mathlib/convert.py
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 | |
from_float_to_rational(f, limit_denominator=None)
Converts a floating-point number to a rational approximation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
float
|
The float to convert. |
required |
limit_denominator
|
int
|
If provided, the denominator of the
resulting fraction is bounded by this value. Defaults to None
(exact conversion via |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Rational |
Rational
|
A |
Source code in catena/mathlib/convert.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
from_float_to_scf(f, limit_denominator=None)
Converts a floating-point number to its simple continued fraction (SCF) representation.
Delegates to :func:from_float_to_rational followed by :func:from_rational_to_scf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
float
|
The float to convert. |
required |
limit_denominator
|
int
|
If provided, the rational approximation uses a denominator bounded by this value. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[int, list[int]]
|
tuple[int, list[int]]: A pair |
Source code in catena/mathlib/convert.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
from_rational_to_scf(r)
Converts a rational number to its simple continued fraction (SCF) representation.
Uses the Euclidean algorithm to compute the sequence of partial quotients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
Rational
|
The rational number to convert. Accepted types:
- |
required |
Returns:
| Type | Description |
|---|---|
int
|
tuple[int, list[int]]: A pair |
list[int]
|
the integer part and the list contains the remaining partial quotients. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in catena/mathlib/convert.py
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 | |
mathlib.core
canonicalize(p, q)
Canonicalizes a fraction by simplifying it and ensuring the denominator is positive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
int
|
The numerator. |
required |
q
|
int
|
The denominator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IntPair |
IntPair
|
A tuple containing the canonicalized numerator and denominator. |
Source code in catena/mathlib/core.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
euclidean_step(p, q)
Breaks down a division operation into quotient, remainder, and divisor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
int
|
The dividend. |
required |
q
|
int
|
The divisor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IntTriplet |
IntTriplet
|
A tuple containing the quotient, remainder, and divisor. |
Source code in catena/mathlib/core.py
61 62 63 64 65 66 67 68 69 70 71 72 | |
get_sign(n)
Returns the sign of a number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The number to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
1 if positive, -1 if negative, 0 if zero. |
Source code in catena/mathlib/core.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
quotent_sign(p, q)
Returns the sign of the quotient of a and b without performing the division.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
int
|
The numerator. |
required |
q
|
int
|
The denominator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int | None
|
1 if the quotient is positive, -1 if negative, 0 if zero, None if undefined (division by zero). |
Source code in catena/mathlib/core.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
simplify(p, q)
Simplifies a fraction by dividing both the numerator and denominator by their greatest common divisor (GCD). Signs are preserved: - If the numerator is negative, the simplified numerator will be negative. - If the denominator is negative, the simplified denominator will be negative. - If both are negative, both will be simplified to negative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
int
|
The numerator. |
required |
q
|
int
|
The denominator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IntPair |
IntPair
|
A tuple containing the simplified numerator and denominator. |
Source code in catena/mathlib/core.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
mathlib.metric
average_digit_count(arr)
Computes the average number of digits in the product of a sequence of positive integers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
Sequence[int]
|
A sequence of positive integers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The average number of digits in the product of the sequence. |
Source code in catena/mathlib/metric.py
45 46 47 48 49 50 51 52 53 54 55 | |
product_digit_count(arr)
Computes the total number of digits in the product of a sequence of positive integers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
Sequence[int]
|
A sequence of positive integers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The total number of digits in the product of the sequence. |
Source code in catena/mathlib/metric.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
product_digit_count_high_precision(arr)
Alternative (and much slower) implementation of product_digit_count using Decimal for high precision.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
Sequence[int]
|
A sequence of positive integers. |
required |
Returns: int: The total number of digits in the product of the sequence.
Source code in catena/mathlib/metric.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
mathlib.quadratic
normalize_quadratic_surd(P, Q, D)
Normalizes the quadratic surd (P + √D) / Q by multiplying the parameters
by a common factor k such that the resulting denominator is positive and
the radicand is non-negative. Preseves signs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
P
|
int
|
Coefficient of the surd. |
required |
Q
|
int
|
Denominator. |
required |
D
|
int
|
Radicand. |
required |
Returns:
| Type | Description |
|---|---|
int
|
tuple[int, int, int]: |
int
|
the normalized form of the original surd. |
Note
No typechecking is performed on the input. The function assumes that
Q is non-zero and D is non-negative.
Source code in catena/mathlib/quadratic.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
quadratic_roots_from_coefficients(A, B, C)
Returns the two real roots of the quadratic equation A·x² + B·x + C = 0
as :class:~decimal.Decimal values, or None if the discriminant is
negative. x₀ is the larger root and x₁ is the smaller root, so that x₀ ≥ x₁.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
int
|
Leading coefficient. |
required |
B
|
int
|
Linear coefficient. |
required |
C
|
int
|
Constant term. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Tuple[Decimal, Decimal]]
|
tuple[Decimal, Decimal] | None: |
Optional[Tuple[Decimal, Decimal]]
|
or |
Source code in catena/mathlib/quadratic.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
quadratic_surd_from_coefficients(A, B, C)
Converts the quadratic equation A·x² + B·x + C = 0 into the canonical
surd form (P + √D) / Q by reducing coefficients by their GCD and
normalising the sign so that Q > 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
int
|
Leading coefficient. |
required |
B
|
int
|
Linear coefficient. |
required |
C
|
int
|
Constant term. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Tuple[int, int, int]]
|
tuple[int, int, int] | None: |
Optional[Tuple[int, int, int]]
|
equals |
Source code in catena/mathlib/quadratic.py
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 | |
simplify_quadratic_surd(P, Q, D)
Simplifies the quadratic surd (P + √D) / Q by dividing the parameters
by their GCD g such that g | P, Q and g² | D. Preseves signs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
P
|
int
|
Coefficient of the surd. |
required |
Q
|
int
|
Denominator. |
required |
D
|
int
|
Radicand. |
required |
Returns:
| Type | Description |
|---|---|
int
|
tuple[int, int, int]: |
int
|
is the simplified form of the original surd. |
Source code in catena/mathlib/quadratic.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |