eager
broadcast_arrays(*args)
Broadcasts one or more arrays against one another.
Parameters
*args: array an arbitrary number of to-be broadcasted arrays.
Returns
out: List[array] a list of broadcasted arrays. Each array has the same shape. Element types are preserved.
Source code in src/finch/interface/eager.py
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 | |
broadcast_to(x, /, shape)
Broadcasts an array to a new shape.
Parameters
x: array The input tensor to be broadcasted. shape: Sequence[int] The target shape to which the input tensor should be broadcasted.
Returns
out: array
A tensor with the same data as x, but with the specified shape.
Source code in src/finch/interface/eager.py
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 | |
einop(prgm, /, **kwargs)
Execute an einsum expression using the specified array framework.
This function parses and executes einsum-like expressions with extended syntax that supports various operations beyond traditional Einstein summation notation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prgm
|
str
|
Einsum program string specifying the computation. The syntax supports: - Assignment: "C[i,j] = A[i,j] + B[j,i]" - Increment: "C[i,j] += A[i,k] * B[k,j]" - Reductions: "C[i] += A[i,j]", "C[i] max= A[i,j]", "C[i] &= A[i,j]" - Arithmetic operations: +, -, *, /, //, %, ** - Comparison operations: ==, !=, <, <=, >, >= - Logical operations: and, or, not - Bitwise operations: &, |, ^, <<, >> - Function calls and complex expressions with parentheses - Mathematical functions: abs, sqrt, exp, log, sin, cos, tan, etc. - Literal values: integers, floats, booleans, and complex numbers - Python operator precedence and parentheses for grouping |
required |
**kwargs
|
Any
|
Named arrays referenced in the einsum expression. The keys should match the tensor names used in the program string. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The result array from executing the einsum expression. |
Examples:
>>> rng = np.random.default_rng(42)
>>> A = rng.integers(0, 10, size=(3, 4))
>>> B = rng.integers(0, 10, size=(4, 3))
>>> # Matrix addition with transpose
>>> C = einop("C[i,j] = A[i,j] + B[j,i]", A=A, B=B)
>>> # Matrix multiplication
>>> D = einop("D[i,j] += A[i,k] * B[k,j]", A=A, B=B)
>>> # Min-Plus multiplication with shift
>>> E = einop("E[i] min= A[k,i] + D[k,j] << 1", A=A, D=D)
Source code in src/finch/interface/eager.py
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 | |
einsum(*args, **kwargs)
einsum(subscripts, *operands)
Evaluates the Einstein summation convention on the operands.
Using the Einstein summation convention, many common multi-dimensional,
linear algebraic array operations can be represented in a simple fashion.
In implicit mode einsum computes these values.
In explicit mode, einsum provides further flexibility to compute
other array operations that might not be considered classical Einstein
summation operations, by disabling, or forcing summation over specified
subscript labels.
See the notes and examples for clarification.
Parameters
subscripts : str Specifies the subscripts for summation as comma separated list of subscript labels. An implicit (classical Einstein summation) calculation is performed unless the explicit indicator '->' is included as well as subscript labels of the precise output form. operands : list of array_like These are the arrays for the operation.
Returns
output : ndarray The calculation based on the Einstein summation convention.
Notes
The Einstein summation convention can be used to compute
many multi-dimensional, linear algebraic array operations. einsum
provides a succinct way of representing these.
A non-exhaustive list of these operations,
which can be computed by einsum, is shown below along with examples:
- Trace of an array, :py:func:
numpy.trace. - Return a diagonal, :py:func:
numpy.diag. - Array axis summations, :py:func:
numpy.sum. - Transpositions and permutations, :py:func:
numpy.transpose. - Matrix multiplication and dot product, :py:func:
numpy.matmul:py:func:numpy.dot. - Vector inner and outer products, :py:func:
numpy.inner:py:func:numpy.outer. - Broadcasting, element-wise and scalar multiplication,
:py:func:
numpy.multiply. - Tensor contractions, :py:func:
numpy.tensordot. - Chained array operations, in efficient calculation order,
:py:func:
numpy.einsum_path.
The subscripts string is a comma-separated list of subscript labels,
where each label refers to a dimension of the corresponding operand.
Whenever a label is repeated it is summed, so np.einsum('i,i', a, b)
is equivalent to :py:func:np.inner(a,b) <numpy.inner>. If a label
appears only once, it is not summed, so np.einsum('i', a)
produces a view of a with no changes. A further example
np.einsum('ij,jk', a, b) describes traditional matrix multiplication
and is equivalent to :py:func:np.matmul(a,b) <numpy.matmul>.
Repeated subscript labels in one operand take the diagonal.
For example, np.einsum('ii', a) is equivalent to
:py:func:np.trace(a) <numpy.trace>.
In implicit mode, the chosen subscripts are important
since the axes of the output are reordered alphabetically. This
means that np.einsum('ij', a) doesn't affect a 2D array, while
np.einsum('ji', a) takes its transpose. Additionally,
np.einsum('ij,jk', a, b) returns a matrix multiplication, while,
np.einsum('ij,jh', a, b) returns the transpose of the
multiplication since subscript 'h' precedes subscript 'i'.
In explicit mode the output can be directly controlled by
specifying output subscript labels. This requires the
identifier '->' as well as the list of output subscript labels.
This feature increases the flexibility of the function since
summing can be disabled or forced when required. The call
np.einsum('i->', a) is like :py:func:np.sum(a) <numpy.sum>
if a is a 1-D array, and np.einsum('ii->i', a)
is like :py:func:np.diag(a) <numpy.diag> if a is a square 2-D array.
The difference is that einsum does not allow broadcasting by default.
Additionally np.einsum('ij,jh->ih', a, b) directly specifies the
order of the output subscript labels and therefore returns matrix
multiplication, unlike the example above in implicit mode.
To enable and control broadcasting, use an ellipsis. Default
NumPy-style broadcasting is done by adding an ellipsis
to the left of each term, like np.einsum('...ii->...i', a).
np.einsum('...i->...', a) is like
:py:func:np.sum(a, axis=-1) <numpy.sum> for array a of any shape.
To take the trace along the first and last axes,
you can do np.einsum('i...i', a), or to do a matrix-matrix
product with the left-most indices instead of rightmost, one can do
np.einsum('ij...,jk...->ik...', a, b).
einsum also provides an alternative way to provide the subscripts and
operands as einsum(op0, sublist0, op1, sublist1, ..., [sublistout]).
If the output shape is not provided in this format einsum will be
calculated in implicit mode, otherwise it will be performed explicitly.
The examples below have corresponding einsum calls with the two
parameter methods.
Examples
a = np.arange(25).reshape(5, 5) b = np.arange(5) c = np.arange(6).reshape(2, 3)
Trace of a matrix:
np.einsum("ii", a) np.int64(60) np.einsum(a, [0, 0]) np.int64(60) np.trace(a) np.int64(60)
Extract the diagonal (requires explicit form):
np.einsum("ii->i", a) array([ 0, 6, 12, 18, 24]) np.einsum(a, [0, 0], [0]) array([ 0, 6, 12, 18, 24]) np.diag(a) array([ 0, 6, 12, 18, 24])
Sum over an axis (requires explicit form):
np.einsum("ij->i", a) array([ 10, 35, 60, 85, 110]) np.einsum(a, [0, 1], [0]) array([ 10, 35, 60, 85, 110]) np.sum(a, axis=1) array([ 10, 35, 60, 85, 110])
For higher dimensional arrays summing a single axis can be done with ellipsis:
np.einsum("...j->...", a) array([ 10, 35, 60, 85, 110]) np.einsum(a, [Ellipsis, 1], [Ellipsis]) array([ 10, 35, 60, 85, 110])
Compute a matrix transpose, or reorder any number of axes:
np.einsum("ji", c) array([[0, 3], [1, 4], [2, 5]]) np.einsum("ij->ji", c) array([[0, 3], [1, 4], [2, 5]]) np.einsum(c, [1, 0]) array([[0, 3], [1, 4], [2, 5]]) np.transpose(c) array([[0, 3], [1, 4], [2, 5]])
Vector inner products:
np.einsum("i,i", b, b) np.int64(30) np.einsum(b, [0], b, [0]) np.int64(30) np.inner(b, b) np.int64(30)
Matrix vector multiplication:
np.einsum("ij,j", a, b) array([ 30, 80, 130, 180, 230]) np.einsum(a, [0, 1], b, [1]) array([ 30, 80, 130, 180, 230]) np.dot(a, b) array([ 30, 80, 130, 180, 230]) np.einsum("...j,j", a, b) array([ 30, 80, 130, 180, 230])
Broadcasting and scalar multiplication:
np.einsum("..., ...", 3, c) array([[ 0, 3, 6], [ 9, 12, 15]]) np.einsum(",ij", 3, c) array([[ 0, 3, 6], [ 9, 12, 15]]) np.einsum(3, [Ellipsis], c, [Ellipsis]) array([[ 0, 3, 6], [ 9, 12, 15]]) np.multiply(3, c) array([[ 0, 3, 6], [ 9, 12, 15]])
Vector outer product:
np.einsum("i,j", np.arange(2) + 1, b) array([[0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]) np.einsum(np.arange(2) + 1, [0], b, [1]) array([[0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]) np.outer(np.arange(2) + 1, b) array([[0, 1, 2, 3, 4], [0, 2, 4, 6, 8]])
Tensor contraction:
a = np.arange(60.0).reshape(3, 4, 5) b = np.arange(24.0).reshape(4, 3, 2) np.einsum("ijk,jil->kl", a, b) array([[4400., 4730.], [4532., 4874.], [4664., 5018.], [4796., 5162.], [4928., 5306.]]) np.einsum(a, [0, 1, 2], b, [1, 0, 3], [2, 3]) array([[4400., 4730.], [4532., 4874.], [4664., 5018.], [4796., 5162.], [4928., 5306.]]) np.tensordot(a, b, axes=([1, 0], [0, 1])) array([[4400., 4730.], [4532., 4874.], [4664., 5018.], [4796., 5162.], [4928., 5306.]])
Example of ellipsis use:
a = np.arange(6).reshape((3, 2)) b = np.arange(12).reshape((4, 3)) np.einsum("ki,jk->ij", a, b) array([[10, 28, 46, 64], [13, 40, 67, 94]]) np.einsum("ki,...k->i...", a, b) array([[10, 28, 46, 64], [13, 40, 67, 94]]) np.einsum("k...,jk", a, b) array([[10, 28, 46, 64], [13, 40, 67, 94]])
Source code in src/finch/interface/eager.py
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 | |
matmul(x1, x2)
Computes the matrix product.
Returns a LazyTensor if either x1 or x2 is a LazyTensor. Otherwise, computes the result eagerly.
Source code in src/finch/interface/eager.py
431 432 433 434 435 436 437 438 439 440 441 | |
matrix_power(x, n)
Computes the power of a matrix.
Source code in src/finch/interface/eager.py
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 | |
matrix_transpose(x)
Computes the transpose of a matrix or stack of matrices.
Source code in src/finch/interface/eager.py
444 445 446 447 448 449 450 | |
moveaxis(x, source, destination)
Moves array axes (dimensions) to new positions, while leaving other axes in their original positions.
Args
- x (array) - input array.
- source - Axes to move.
- destination - indices defining the desired positions for each respective source axis index.
Returns
- out (array) - an array containing reordered axes.
Source code in src/finch/interface/eager.py
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 | |
tensordot(x1, x2, /, *, axes=2)
Computes the tensordot operation.
Returns a LazyTensor if either x1 or x2 is a LazyTensor. Otherwise, computes the result eagerly.
Source code in src/finch/interface/eager.py
994 995 996 997 998 999 1000 1001 1002 1003 | |
vecdot(x1, x2, /, *, axis=-1)
Computes the (vector) dot product of two arrays.
Parameters
x1: array The first input tensor. x2: array The second input tensor. axis: int, optional The axis along which to compute the dot product. Default is -1 (last axis).
Returns
out: array
A tensor containing the dot product of x1 and x2 along the specified axis.
Source code in src/finch/interface/eager.py
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 | |