|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import abc |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from typing import Union, Tuple, Any, FrozenSet |
| 7 | +from dataclasses import dataclass |
| 8 | + |
| 9 | + |
| 10 | +IntegralT = Union[int, np.int8, np.int16, np.int32, np.int64, np.uint8, |
| 11 | + np.uint16, np.uint32, np.uint64] |
| 12 | +INT_CLASSES = (int, np.int8, np.int16, np.int32, np.int64, np.uint8, |
| 13 | + np.uint16, np.uint32, np.uint64) |
| 14 | + |
| 15 | + |
| 16 | +ShapeComponentT = Union[IntegralT, "VeryLongAxis"] |
| 17 | +ShapeT = Tuple[ShapeComponentT, ...] |
| 18 | + |
| 19 | + |
| 20 | +class VeryLongAxis: |
| 21 | + """ |
| 22 | + Describes a shape which can be assumed to be very large. |
| 23 | + """ |
| 24 | + # TODO: Record the threshold over which an axis could be considered as |
| 25 | + # "VeryLong." |
| 26 | + |
| 27 | + |
| 28 | +@dataclass(frozen=True, repr=True, eq=True) |
| 29 | +class EinsumAxisAccess(abc.ABC): |
| 30 | + """ |
| 31 | + Base class for axis access types in an einsum expression. |
| 32 | + """ |
| 33 | + |
| 34 | + |
| 35 | +@dataclass(frozen=True, repr=True, eq=True) |
| 36 | +class FreeAxis(EinsumAxisAccess): |
| 37 | + """ |
| 38 | + Records the axis of an einsum argument over which contraction is not performed. |
| 39 | +
|
| 40 | + .. attribute:: output_index |
| 41 | +
|
| 42 | + Position of the corresponding index in the einsum's output. |
| 43 | + """ |
| 44 | + output_index: int |
| 45 | + |
| 46 | + |
| 47 | +@dataclass(frozen=True, repr=True, eq=True) |
| 48 | +class SummationAxis(EinsumAxisAccess): |
| 49 | + """ |
| 50 | + Records an index in an einsum expression over which reduction is performed. |
| 51 | + Sometimes also referred to as an axis with a corresponding "dummy index" in |
| 52 | + Ricci Calculus. |
| 53 | +
|
| 54 | + .. attribute:: index |
| 55 | +
|
| 56 | + An integer which is unique to a reduction index of an einsum. |
| 57 | + """ |
| 58 | + idx: int |
| 59 | + |
| 60 | + |
| 61 | +@dataclass(frozen=True, eq=True, repr=True) |
| 62 | +class Einsum: |
| 63 | + """ |
| 64 | + An einsum expression. |
| 65 | + """ |
| 66 | + arg_shapes: Tuple[ShapeT, ...] |
| 67 | + arg_dtypes: Tuple[np.dtype[Any], ...] |
| 68 | + access_descriptors: Tuple[Tuple[EinsumAxisAccess, ...], ...] |
| 69 | + use_matrix: Tuple[Tuple[FrozenSet[str], ...]] |
0 commit comments