Reputation:
I have a dict that contains tuples as keys with a sympy expression and the number of arguments in each tuple and as value a customized translation. I want to check if the sympy expression is a trigonometric function i.e a function of those: https://omz-software.com/pythonista/sympy/modules/mpmath/functions/trigonometric.html
Do you guys know a nice command to check this? I could just think of a list in which I name [sin, cos, tan] etc and then check if key is contained in it. But I'd be really happy if there was a nicer solution.
Can one generally classify sympy expression and check to which class of expression they belong?
default_semantic_latex_table = {
(sympy.functions.elementary.trigonometric.sin, 1): FormatTemplate(r"\sin@{$0}"), (sympy.functions.special.polynomials.jacobi, 4): FormatTemplate(r"\JacobipolyP{{$0}{$1}{$2}@{$3}}"),
(sympy.functions.elementary.trigonometric.cos, 2): FormatTemplate(r"\cos@{$0,$2}")
}
for k, v in default_semantic_latex_table:
# check if key is instance of a sympy trigonometric function
# if so and the number of args is not equal to one, throw a warning
If the if condition evaluates to True, I want to check the second element of the tuple/the number of args. How would I do that?
I expected k
to be a tuple like (sympy.functions.special.polynomials.jacobi, 4)
yet if I test it k
turns out to be just sympy.functions.special.polynomials.jacobi
. How can I get the second tuple element?
I'd be really glad if someone could help!
Upvotes: 0
Views: 125
Reputation: 13150
You can import the TrigonometricFunction
base class from sympy and use the issubclass
method to perform the tests.
Note that you have to use dictionary_name.items()
in order to loop over the keys and values of a dictionary:
from sympy import *
import sympy
from sympy.functions.elementary.trigonometric import TrigonometricFunction
import warnings
default_semantic_latex_table = {
(sympy.functions.elementary.trigonometric.sin, 1): "a",
(sympy.functions.elementary.trigonometric.cos, 2): "b"
}
for k, v in default_semantic_latex_table.items():
t, n = k
if issubclass(t, TrigonometricFunction) and (n != 1):
warnings.warn("Your warning: function=%s with n=%s" % (t, n))
Upvotes: 1