I can get the name of an "undefined" symbol:
import sympy as sym
knodef = sym.Symbol('k_{nodef}')
print(type(knodef))
sym.pprint(knodef.name)
<class 'sympy.core.symbol.Symbol'>
k_{nodef}
But as soon as I define it as an expression, I cannot get the name anymore.
kdef = sym.Symbol('k_{def}')
kdef = sym.tan(b*x)
print(type(kdef))
sym.pprint(kdef.name) # AttributeError: 'tan' object has no attribute 'name'
tan
<Error raised>
How can I get the name of kdef?
Similarly for functions...
knodef2 = sym.Function('k_{nodef2}')
#display(disp.Math(knodef2.name), type(knodef2))
print(type(knodef2))
sym.pprint(knodef2.name)
<class 'sympy.core.function.UndefinedFunction'>
k_{nodef2}
... (is there in this case an equivalent to defining the Symbol)?
As you demonstrate, the name attribute of Symbol and Function can be used to return a string form of the "name". For UndefinedFunction you can use .func.name to get that string, but for defined FunctionClass you can just use str on the func attribute:
>>> s=Symbol('s');f=Function('f');a=f(s);b=tan(s)
>>> s.name
's'
>>> f.name
'f'
>>> a.func.name
'f'
>>> str(b.func)
'tan'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With