I am trying to remap And in sympy when using lambdify, but it seems not working...
import sympy
x, y = sympy.symbols('x y')
f = sympy.And(x, y)
def my_and(x, y):
print("inside my_and")
return x and y
f_fn = sympy.lambdify([x, y], f, {"And": my_and})
print(f_fn(True, False))
The result will not display "inside my_and", which means that my_and doesn't work.
Other functions like sin work well as follows. I don't where it goes wrong...
import sympy
x = sympy.symbols('x')
f = sympy.sin(x)
def my_sin(x):
print("inside my_sin")
return 0.
f_fn = sympy.lambdify([x], f, {"sin": my_sin})
print(f_fn(0.))
This one will output exactly inside my_sin and 0.
I want to remap And to my function, so what sould I do?
help(f_fn)forsinexample shows:and after poking around as bit I found the mapping is defined the
__globals__of this function:The help for your 'and' example:
while there's a similar mapping:
but I think this doesn't work because,
andinx and yis a Python keyword, in an operator role, not a function. It can't be mapped to something else.It's the normal short-circuited
andof Python:Just realized I used
and, while you usedAnd:There's even less of a connection between the mapping and function.
Mappings like
{"And": my_and}don't affect the code translation directly. That is they don't producerather they determine how the Python code is run.
If I omit the mapping
the code is
f_and1.__globals__gives a whole raft ofnumpyfunctions, includingand the behavior is
numpywise:lambdify([x,y],f,[{"And": my_and},'numpy'])also gives me thelogical_andtranslation.