Skip to content

Commit b575734

Browse files
author
Release Manager
committed
gh-36329: Implement call method for elements in CDGA's Fixes #36328 by implementing the method. - [X] The title is concise, informative, and self-explanatory. - [X] The description explains in detail what this PR is about. - [X] I have linked a relevant issue or discussion. - [X] I have created tests covering the changes. - [X] I have updated the documentation accordingly. URL: #36329 Reported by: miguelmarco Reviewer(s): Travis Scrimshaw
2 parents 4ec03a8 + 320f177 commit b575734

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/sage/algebras/commutative_dga.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,83 @@ def dict(self):
15811581
"""
15821582
return self.lift().dict()
15831583

1584+
def __call__(self, *values, **kwargs):
1585+
r"""
1586+
Evaluate the reduced expression of this element at ``x``, where ``x``
1587+
is either the tuple of values to evaluate in, a dictionary indicating
1588+
to which value is each generator evaluated, or keywords giving
1589+
the value to which generators should be evaluated.
1590+
1591+
INPUT:
1592+
1593+
- ``values`` -- (optional) either the values in which the variables
1594+
will be evaluated or a dictionary
1595+
1596+
OUTPUT:
1597+
1598+
this element evaluated at the given values
1599+
1600+
EXAMPLES::
1601+
1602+
sage: A.<x,y,z,t> = GradedCommutativeAlgebra(QQ, degrees=(1, 2, 2, 3))
1603+
sage: f = x*y - 5*y*z + 7*x*y^2*z^3*t
1604+
sage: f(3, y, x^2, x*z)
1605+
3*y
1606+
sage: f(x=3)
1607+
21*y^2*z^3*t - 5*y*z + 3*y
1608+
sage: f({x:3, z:x^2})
1609+
3*y
1610+
1611+
If the wrong number of values is provided, it results in an error::
1612+
1613+
sage: f(3, 5, y)
1614+
Traceback (most recent call last):
1615+
...
1616+
ValueError: number of arguments does not match number of variables in parent
1617+
1618+
It is also possible to use keywords like this::
1619+
1620+
sage: A.<x,y,z,t> = GradedCommutativeAlgebra(QQ, degrees=(1, 2, 2, 3))
1621+
sage: f = x*y - 5*y*z + 7*x*y^2*z^3*t
1622+
sage: f(x=3)
1623+
21*y^2*z^3*t - 5*y*z + 3*y
1624+
sage: f(t=x,y=z)
1625+
-5*z^2 + x*z
1626+
1627+
If both a dictionary and keywords are used, only the dictionary is
1628+
considered::
1629+
1630+
sage: A.<x,y,z,t> = GradedCommutativeAlgebra(QQ, degrees=(1, 2, 2, 3))
1631+
sage: f = x*y - 5*y*z + 7*x*y^2*z^3*t
1632+
sage: f({x:1}, t=x,y=z)
1633+
7*y^2*z^3*t - 5*y*z + y
1634+
"""
1635+
gens = self.parent().gens()
1636+
images = list(gens)
1637+
if values and not isinstance(values[0], dict):
1638+
for (i, p) in enumerate(values):
1639+
images[i] = p
1640+
if len(values) == 1 and isinstance(values[0], dict):
1641+
images = list(gens)
1642+
for (i, g) in enumerate(gens):
1643+
if g in values[0]:
1644+
images[i] = values[0][g]
1645+
elif len(values) == len(gens):
1646+
images = list(values)
1647+
elif values:
1648+
raise ValueError("number of arguments does not match number of variables in parent")
1649+
else:
1650+
images = list(gens)
1651+
for (i, g) in enumerate(gens):
1652+
gstr = str(g)
1653+
if gstr in kwargs:
1654+
images[i] = kwargs[gstr]
1655+
res = 0
1656+
for (m, c) in self.dict().items():
1657+
term = prod((gen**y for (y, gen) in zip(m, images)), c)
1658+
res += term
1659+
return res
1660+
15841661
def basis_coefficients(self, total=False):
15851662
"""
15861663
Return the coefficients of this homogeneous element with

0 commit comments

Comments
 (0)