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

Commit

Permalink
24283: Implement Expression.has_function(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
rwst authored and dimpase committed Dec 4, 2021
1 parent cc60cfe commit 602a5bc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/sage/symbolic/expression.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5524,6 +5524,65 @@ cdef class Expression(Expression_abc):
cdef Expression p = self.coerce_in(pattern)
return self._gobj.has(p._gobj)

def has_function(self, arg, all=False):
"""
Return True if any or all function arguments are part
of this expression.
The argument must either be
- a function operator like ``sin``
- a Python list containing such operators
EXAMPLES::
sage: (1+sin(x)).has_function(sin)
True
sage: (1+x).has_function(sin)
False
sage: (1+sin(x)+cos(x)).has_function([sin,cos])
True
sage: (1+sin(x)+cos(x)).has_function([sin,tan])
True
sage: (1+sin(x)+cos(x)).has_function([sin,tan], all=True)
False
sage: (1+sin(x)+tan(x)).has_function([sin,tan], all=True)
True
sage: f = function('f')
sage: (f(x)+sin(x)+tan(x)).has_function([sin,tan,f], all=True)
True
sage: (1+x).has_function(x)
Traceback (most recent call last):
...
TypeError: argument must be function or list
sage: (1+sin(x)+cos(x)).has_function([sin,cos,x])
Traceback (most recent call last):
...
TypeError: arguments must be functions
"""
from .function import Function
cdef stdstring s
cdef vector[stdstring] vec
if isinstance(arg, Function):
s = arg.name()
vec = [s]
elif isinstance(arg, list):
for a in arg:
if isinstance(a, Function):
s = a.name()
vec.push_back(s)
else:
raise TypeError('arguments must be functions')
else:
raise TypeError('argument must be function or list')

sig_on()
try:
return has_function(self._gobj, vec, all is True)
finally:
sig_off()

def substitute(self, *args, **kwds):
"""
Substitute the given subexpressions in this expression.
Expand Down
2 changes: 2 additions & 0 deletions src/sage/symbolic/pynac.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ cdef extern from "pynac_wrap.h":

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

GFunctionOptVector g_registered_functions \
"GiNaC::function::registered_functions" ()
Expand Down

0 comments on commit 602a5bc

Please sign in to comment.