indices(spec)
ΒΆ
Create multiple index varaibles at once.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spec |
int or str |
|
required |
Returns:
Type | Description |
---|---|
tuple |
Multiple single-index tensor expressions. |
Examples:
\[i\]
\[k\]
\[\#_0\]
Source code in funfact/lang/_tsrex.py
def indices(spec):
'''Create multiple index varaibles at once.
Args:
spec (int or str):
- If `int`, indicate the number of 'anonymous' index variables to
create. These anonymous variables will be numbered sequentially
starting from 0, and are guaranteed to be unique within runtime.
- If `str`, must be a
comma/space-delimited list of symbols in the format as described in
[funfact.index][].
Returns:
tuple: Multiple single-index tensor expressions.
Example:
>>> i, j = indices(2); i
$$i$$
>>> i, j, k = indices('i, j, k'); k
$$k$$
>>> I = indices(9); I[0]
$$\\#_0$$
'''
if isinstance(spec, int) and spec >= 0:
return [index() for i in range(spec)]
elif isinstance(spec, str):
return [index(s) for s in re.split(r'[,\s]+', spec)]
else:
raise RuntimeError(f'Cannot create indices from {spec}.')