I am trying to write a function that takes the log of a boolean expression np.log(bool_variable)
. Basically I want the function to return 0
if bool_variable=True
or -np.inf
if bool_variable=False
. One easy way would be to use an if statement, but this could slow things down. Is there a way for me to use the function below and avoid the warning RuntimeWarning: divide by zero encountered in log
ONLY IN THIS FUNCTION? That is, I want this function to suppress that warning, but I would still like any other piece of code in the script to raise that warning if it happens.
def function(x):
return np.log(x <= 2)
Ideally I will be running a similar function many many times, so I am looking for the most efficient way of doing this.