summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2024-05-27 15:08:47 +0200
committerNao Pross <np@0hm.ch>2024-05-27 15:08:47 +0200
commitc8e87e1d188029ca726381d191472c26b92f9fd6 (patch)
tree4cdf7268db7c34df4fa4b97bd09de45936364051
parentFix bug with shapes (diff)
downloadpolymatrix-c8e87e1d188029ca726381d191472c26b92f9fd6.tar.gz
polymatrix-c8e87e1d188029ca726381d191472c26b92f9fd6.zip
Create poly.what_is to inspect variable that have been given names with poly.give_name
Diffstat (limited to '')
-rw-r--r--polymatrix/__init__.py2
-rw-r--r--polymatrix/expression/__init__.py21
2 files changed, 22 insertions, 1 deletions
diff --git a/polymatrix/__init__.py b/polymatrix/__init__.py
index d0f53ee..b055833 100644
--- a/polymatrix/__init__.py
+++ b/polymatrix/__init__.py
@@ -27,6 +27,7 @@ from polymatrix.expression import (
block_diag as internal_block_diag,
lower_triangular as internal_lower_triangular,
give_name as internal_give_name,
+ what_is as internal_what_is,
)
from polymatrix.expression.to import (
@@ -54,6 +55,7 @@ concatenate = internal_concatenate
block_diag = internal_block_diag
lower_triangular = internal_lower_triangular
give_name = internal_give_name
+what_is = internal_what_is
to_constant_repr = internal_to_constant
to_matrix_repr = from_polymatrix
diff --git a/polymatrix/expression/__init__.py b/polymatrix/expression/__init__.py
index eac0a07..b44aa3e 100644
--- a/polymatrix/expression/__init__.py
+++ b/polymatrix/expression/__init__.py
@@ -7,6 +7,7 @@ import polymatrix.expression.impl
from polymatrix.utils.getstacklines import get_stack_lines
from polymatrix.expression.expression import init_expression, Expression
+from polymatrix.expression.mixins.namedexprmixin import NamedExprMixin
from polymatrix.expression.init import (
init_arange_expr,
@@ -133,4 +134,22 @@ def lower_triangular(vector: Expression):
def give_name(expr: Expression, name: str):
- return init_expression(init_named_expr(expr, name))
+ """ Give a name to an expression. """
+ return init_expression(init_named_expr(expr.underlying, name))
+
+
+def what_is(expr: Expression) -> Expression:
+ """
+ Opposite of :py:fn:`give_name`. Query what is inside a named expression.
+ Example:
+ .. py:
+
+ p = poly.give_name(x ** 2 + 1, "p(x)")
+ print(p) # shows "p(x)"
+ print(poly.what_is(p)) # shows "(x ** 2) + 1"
+ """
+
+ if not isinstance(expr.underlying, NamedExprMixin):
+ raise TypeError(f"{expr} is not a named expression!")
+
+ return init_expression(expr.underlying.underlying)