Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 602a5bc

Browse files
rwstdimpase
authored andcommitted
24283: Implement Expression.has_function(...)
1 parent cc60cfe commit 602a5bc

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/sage/symbolic/expression.pyx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5524,6 +5524,65 @@ cdef class Expression(Expression_abc):
55245524
cdef Expression p = self.coerce_in(pattern)
55255525
return self._gobj.has(p._gobj)
55265526

5527+
def has_function(self, arg, all=False):
5528+
"""
5529+
Return True if any or all function arguments are part
5530+
of this expression.
5531+
5532+
The argument must either be
5533+
5534+
- a function operator like ``sin``
5535+
5536+
- a Python list containing such operators
5537+
5538+
EXAMPLES::
5539+
5540+
sage: (1+sin(x)).has_function(sin)
5541+
True
5542+
sage: (1+x).has_function(sin)
5543+
False
5544+
sage: (1+sin(x)+cos(x)).has_function([sin,cos])
5545+
True
5546+
sage: (1+sin(x)+cos(x)).has_function([sin,tan])
5547+
True
5548+
sage: (1+sin(x)+cos(x)).has_function([sin,tan], all=True)
5549+
False
5550+
sage: (1+sin(x)+tan(x)).has_function([sin,tan], all=True)
5551+
True
5552+
sage: f = function('f')
5553+
sage: (f(x)+sin(x)+tan(x)).has_function([sin,tan,f], all=True)
5554+
True
5555+
sage: (1+x).has_function(x)
5556+
Traceback (most recent call last):
5557+
...
5558+
TypeError: argument must be function or list
5559+
sage: (1+sin(x)+cos(x)).has_function([sin,cos,x])
5560+
Traceback (most recent call last):
5561+
...
5562+
TypeError: arguments must be functions
5563+
"""
5564+
from .function import Function
5565+
cdef stdstring s
5566+
cdef vector[stdstring] vec
5567+
if isinstance(arg, Function):
5568+
s = arg.name()
5569+
vec = [s]
5570+
elif isinstance(arg, list):
5571+
for a in arg:
5572+
if isinstance(a, Function):
5573+
s = a.name()
5574+
vec.push_back(s)
5575+
else:
5576+
raise TypeError('arguments must be functions')
5577+
else:
5578+
raise TypeError('argument must be function or list')
5579+
5580+
sig_on()
5581+
try:
5582+
return has_function(self._gobj, vec, all is True)
5583+
finally:
5584+
sig_off()
5585+
55275586
def substitute(self, *args, **kwds):
55285587
"""
55295588
Substitute the given subexpressions in this expression.

src/sage/symbolic/pynac.pxi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ cdef extern from "pynac_wrap.h":
425425

426426
bint has_symbol "GiNaC::has_symbol" (GEx ex)
427427
bint has_symbol_or_function "GiNaC::has_symbol_or_function" (GEx ex)
428+
bint has_function "GiNaC::has_function" (GEx ex,
429+
vector[stdstring] & v, bint all) except +
428430

429431
GFunctionOptVector g_registered_functions \
430432
"GiNaC::function::registered_functions" ()

0 commit comments

Comments
 (0)