Einsum parser#

ivy.utils.einsum_parser.convert_interleaved_input(operands)[source]#

Convert ‘interleaved’ input to standard einsum input.

Return type:

Tuple[str, List[Any]]

ivy.utils.einsum_parser.convert_subscripts(old_sub, symbol_map)[source]#

Convert user custom subscripts list to subscript string according to symbol_map.

Return type:

str

Examples

>>>  oe.parser.convert_subscripts(['abc', 'def'], {'abc':'a', 'def':'b'})
'ab'
>>> oe.parser.convert_subscripts([Ellipsis, object], {object:'a'})
'...a'
ivy.utils.einsum_parser.find_output_shape(inputs, shapes, output)[source]#

Find the output shape for given inputs, shapes and output string, taking into account broadcasting.

Return type:

Tuple[int, ...]

Examples

>>> oe.parser.find_output_shape(["ab", "bc"], [(2, 3), (3, 4)], "ac")
(2, 4)

# Broadcasting is accounted for >>> oe.parser.find_output_shape([“a”, “a”], [(4, ), (1, )], “a”) (4,)

ivy.utils.einsum_parser.find_output_str(subscripts)[source]#

Find the output string for the inputs subscripts under canonical einstein summation rules.That is, repeated indices are summed over by default.

Return type:

str

Examples

>>> oe.parser.find_output_str("ab,bc")
'ac'
>>> oe.parser.find_output_str("a,b")
'ab'
>>> oe.parser.find_output_str("a,a,b,b")
''
ivy.utils.einsum_parser.gen_unused_symbols(used, n)[source]#

Generate n symbols that are not already in used.

Examples: `python list(oe.parser.gen_unused_symbols("abd", 2)) #> ['c', 'e'] `

Return type:

Iterator[str]

ivy.utils.einsum_parser.get_symbol(i)[source]#

Get the symbol corresponding to int i - runs through the usual 52 letters before resorting to unicode characters, starting at chr(192) and skipping surrogates. Examples:

``python :rtype: :py:class:`str

get_symbol(2) #> ‘c’

get_symbol(200) #> ‘Ŕ’

get_symbol(20000) #> ‘京’ ```

ivy.utils.einsum_parser.has_valid_einsum_chars_only(einsum_str)[source]#

Check if einsum_str contains only valid characters for numpy einsum. Examples:

``python :rtype: :py:class:`bool

has_valid_einsum_chars_only(“abAZ”) #> True

has_valid_einsum_chars_only(“Över”) #> False ```

ivy.utils.einsum_parser.is_valid_einsum_char(x)[source]#

Check if the character x is valid for numpy einsum. Examples:

``python :rtype: :py:class:`bool

is_valid_einsum_char(“a”) #> True

is_valid_einsum_char(“Ǵ”) #> False ```

ivy.utils.einsum_parser.legalise_einsum_expr(*operands)[source]#

Reproduction of einsum c side einsum parsing in python. Parameters: Intakes the same inputs as contract_path, but NOT the keyword args. The only.

supported keyword argument is: - shapes - (bool, optional) Whether

parse_einsum_input should assume arrays (the default) or array shapes have been supplied.

Return type:

str

Returns:

einsum_eqn (str) – Legalised einsum equation

Examples

The operand list is simplified to reduce printing:

>>> a = np.random.rand(4, 4)
>>> b = np.random.rand(4, 4, 4)
>>> legalise_einsum_eqn(('...a,...a->...', a, b))
'za,xza->xz'
>>> parse_einsum_input((a, [Ellipsis, 0], b, [Ellipsis, 0]))
'za,xza->xz'
ivy.utils.einsum_parser.possibly_convert_to_numpy(x)[source]#

Convert things without a ‘shape’ to ndarrays, but leave everything else.

Return type:

Any

Examples

>>> oe.parser.possibly_convert_to_numpy(5)
array(5)
>>> oe.parser.possibly_convert_to_numpy([5, 3])
array([5, 3])
>>> oe.parser.possibly_convert_to_numpy(np.array([5, 3]))
array([5, 3])

# Any class with a shape is passed through >>> class Shape: … def __init__(self, shape): … self.shape = shape …

>>> myshape = Shape((5, 5))
>>> oe.parser.possibly_convert_to_numpy(myshape)
<__main__.Shape object at 0x10f850710>

This should have hopefully given you an overview of the einsum_parser submodule, if you have any questions, please feel free to reach out on our discord!