Skip to content

Commit

Permalink
feat: implement indexed_to_symbol from TR-008 (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer authored Aug 3, 2021
1 parent c4e4198 commit b2df83f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/symplot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# cspell:ignore rpartition
# pylint: disable=redefined-builtin
"""Create interactive plots for `sympy` expressions.
Expand All @@ -7,6 +8,10 @@
The module is only available here, under the documentation. If this feature
turns out to be popular, it can be published as an independent package.
The package also provides other helpful functions, like
:func:`substitute_indexed_symbols`, that are useful when visualizing
`sympy` expressions.
"""

import inspect
Expand All @@ -25,6 +30,7 @@

import sympy as sp
from ipywidgets.widgets import FloatSlider, IntSlider
from sympy.printing.latex import translate

try:
from IPython.lib.pretty import PrettyPrinter
Expand Down Expand Up @@ -276,3 +282,26 @@ def _extract_slider_symbols(
)
ordered_symbols = sorted(expression.free_symbols, key=lambda s: s.name)
return tuple(s for s in ordered_symbols if s != plot_symbol)


def _indexed_to_symbol(idx: sp.Indexed) -> sp.Symbol:
base_name, _, _ = str(idx).rpartition("[")
subscript = ",".join(map(str, idx.indices))
if len(idx.indices) > 1:
base_name = translate(base_name)
subscript = "_{" + subscript + "}"
return sp.Symbol(f"{base_name}{subscript}")


def substitute_indexed_symbols(expression: sp.Expr) -> sp.Expr:
"""Substitute `~sympy.tensor.indexed.IndexedBase` with symbols.
See :doc:`compwa-org:report/008` for more info.
"""
return expression.subs(
{
s: _indexed_to_symbol(s)
for s in expression.free_symbols
if isinstance(s, sp.Indexed)
}
)

0 comments on commit b2df83f

Please sign in to comment.