Exact numbers¶
An exact number domain carries the reference
machine's reals as RationalTensor values: tensors of exact
rationals, under which every operation the rationals are closed under is computed exactly and every
other is refused with InexactOperation.
exact
¶
Exact rational numbers for the reference machine.
The reference machine computes with tensors (quivers.qiec.numbers). A run
whose NumberDomain is exact carries
its Real and LogWeight values as RationalTensor: a tensor whose
entries are exact rational numbers (fractions.Fraction). The machine is the
same machine; only its numbers differ. Every tensor operation that reaches a
rational tensor is evaluated exactly when the field of rational
numbers is closed under it
(the field operations, comparisons, rounding to an integer, structural
operations such as indexing, stacking and reshaping, and sums and products
along axes), and refused with InexactOperation otherwise. So an exact run
either returns the exact value of a computation or names the first
operation, such as exp or log, whose result is in general not
rational.
Integers and truth values are exact in every run, so they stay ordinary
int64 and bool tensors; an operation mixing one with a rational tensor
reads its entries exactly. A floating operand that is not a rational tensor
carries no record of how it was rounded, so it is admitted only when every
entry is an integer, which a floating format holds exactly: the zeros, ones
and unit constants that machine code writes. Any other floating operand is
refused.
A host float enters an exact run as the shortest decimal that rounds to it
(decimal_fraction): the literal 0.1 is the rational 1/10, not the
binary fraction nearest to it. That is the decimal as written for any literal
of at most fifteen significant digits.
NOMINAL_DTYPE
module-attribute
¶
NOMINAL_DTYPE = torch.float64
The dtype a rational tensor reports, so code asking whether a value is floating reads it as a real number.
NOT_CLOSED
module-attribute
¶
NOT_CLOSED: frozenset[str] = frozenset({'exp', 'exp2', 'expm1', 'log', 'log2', 'log10', 'log1p', 'logaddexp', 'logaddexp2', 'logsumexp', 'logcumsumexp', 'logit', 'sqrt', 'rsqrt', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'atan2', 'sinh', 'cosh', 'tanh', 'asinh', 'acosh', 'atanh', 'erf', 'erfc', 'erfinv', 'lgamma', 'digamma', 'polygamma', 'mvlgamma', 'sigmoid', 'log_sigmoid_forward', 'softplus', 'gelu', 'silu', 'mish', 'elu', 'selu', '_softmax', '_log_softmax', 'special_ndtr', 'special_log_ndtr', 'special_ndtri', 'special_i0', 'special_i0e', 'special_i1', 'special_i1e', 'special_xlog1py', 'xlogy', 'linalg_cholesky_ex', 'linalg_eigh', 'linalg_inv_ex', 'linalg_solve_ex'})
Operations whose result is in general not rational.
The rationals are not closed under them: exp(1), log(2),
sqrt(2) and sigmoid(1) are irrational, and a Cholesky factor or an
eigendecomposition takes square roots. An exact run refuses each with this
reason. Every operation outside both this set and the exact table is refused
because no exact implementation is declared for it.
ExactScalar
¶
A host scalar an exact run reads without rounding.
HostRational
¶
A rational tensor as host data: a fraction, or nested tuples of them.
ExactInput
¶
ExactInput = Entries | Fraction | int | float | bool | Sequence['ExactInput']
Entries a rational tensor is built from: numbers (a float read by
decimal_fraction), nested sequences of them, or an object array.
DispatchArgument
¶
DispatchArgument = Tensor | Entries | Fraction | int | float | bool | str | None | dtype | device | layout | memory_format | Sequence['DispatchArgument']
An argument a dispatched tensor operation receives.
Operation
¶
An exact implementation of one dispatched tensor operation.
InexactOperation
¶
Bases: ArithmeticError
An exact run reached an operation it cannot evaluate exactly.
| PARAMETER | DESCRIPTION |
|---|---|
operation
|
The operation's name, such as
TYPE:
|
reason
|
Why it has no exact result.
TYPE:
|
Source code in src/quivers/core/exact.py
RationalTensor
¶
Bases: Tensor
A tensor of exact rational numbers.
It is a torch.Tensor, so the machine passes it wherever it passes a
number; its entries live in a numpy object array of fractions, and
every operation on it is evaluated exactly or refused (see the module
documentation). It reports float64 as its dtype and the CPU as its
device, and it never requires a gradient. It is built from its
entries, RationalTensor(entries).
entries
property
¶
entries: Entries
The exact entries.
| RETURNS | DESCRIPTION |
|---|---|
Entries
|
A numpy object array of fractions, shaped like the tensor. |
__torch_function__
classmethod
¶
__torch_function__(func: OpOverload, types: tuple[type, ...], args: tuple[DispatchArgument, ...] = (), kwargs: Mapping[str, DispatchArgument] | None = None) -> Tensor | tuple[Tensor, ...] | bool
Pass every function straight to dispatch, unwrapped.
| PARAMETER | DESCRIPTION |
|---|---|
func
|
The function.
TYPE:
|
types
|
The tensor subclasses among the operands.
TYPE:
|
args
|
Positional operands.
TYPE:
|
kwargs
|
Keyword operands.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor | tuple[Tensor, ...] | bool
|
The function's result, computed by |
Source code in src/quivers/core/exact.py
__new__
staticmethod
¶
__new__(entries: ExactInput) -> RationalTensor
Wrap exact entries as a tensor.
| PARAMETER | DESCRIPTION |
|---|---|
entries
|
The entries: fractions, integers, or floats read by
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RationalTensor
|
The tensor. |
Source code in src/quivers/core/exact.py
exact
¶
exact() -> HostRational
The tensor as host data.
| RETURNS | DESCRIPTION |
|---|---|
HostRational
|
A fraction for a scalar, nested tuples of fractions otherwise. |
__repr__
¶
Render the exact entries.
| PARAMETER | DESCRIPTION |
|---|---|
tensor_contents
|
Unused; accepted for
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
|
Source code in src/quivers/core/exact.py
__reduce_ex__
¶
__reduce_ex__(protocol: object) -> tuple[type[RationalTensor], tuple[HostRational]]
Pickle by the exact entries.
| PARAMETER | DESCRIPTION |
|---|---|
protocol
|
The pickle protocol.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
The class and its constructor argument. |
Source code in src/quivers/core/exact.py
__torch_dispatch__
classmethod
¶
__torch_dispatch__(func: OpOverload, types: tuple[type, ...], args: tuple[DispatchArgument, ...] = (), kwargs: Mapping[str, DispatchArgument] | None = None) -> Tensor | tuple[Tensor, ...] | bool
Evaluate one tensor operation exactly, or refuse it.
| PARAMETER | DESCRIPTION |
|---|---|
func
|
The operation.
TYPE:
|
types
|
The tensor subclasses among the operands.
TYPE:
|
args
|
Positional operands.
TYPE:
|
kwargs
|
Keyword operands.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor | tuple[Tensor, ...] | bool
|
The exact result. |
| RAISES | DESCRIPTION |
|---|---|
InexactOperation
|
If the rationals are not closed under the operation, it has no exact implementation, or an operand is a float that is not an exact integer. |
Source code in src/quivers/core/exact.py
decimal_fraction
¶
The rational a host float stands for: its shortest decimal.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A finite float.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Fraction
|
The shortest decimal that rounds to |
| RAISES | DESCRIPTION |
|---|---|
InexactOperation
|
If |
Source code in src/quivers/core/exact.py
exact_scalar
¶
exact_scalar(value: ExactScalar | float) -> Fraction
A host number as a fraction.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A fraction, integer, truth value, or float (read by
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Fraction
|
The number. |
Source code in src/quivers/core/exact.py
entries_of
¶
entries_of(value: DispatchArgument) -> Entries
The exact entries of one operand.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A rational tensor, an integer or truth tensor, a floating tensor or float whose entries are integers, or a host fraction or integer.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Entries
|
The entries as fractions. |
| RAISES | DESCRIPTION |
|---|---|
InexactOperation
|
If a floating operand has an entry that is not an integer, or the operand is not a number. |
Source code in src/quivers/core/exact.py
rational
¶
rational(value: ExactInput) -> RationalTensor
A rational tensor from host numbers.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A number or nested sequences of numbers; a float is read by
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RationalTensor
|
The tensor. |
Source code in src/quivers/core/exact.py
exact_value
¶
exact_value(value: Tensor) -> HostRational
The exact host form of a machine real.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A rational tensor, or an integer or truth tensor.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
HostRational
|
A fraction for a scalar, nested tuples of fractions otherwise. |
| RAISES | DESCRIPTION |
|---|---|
InexactOperation
|
If |