I have a function (listed below) that has branching: if-elif-else.
def kdiv(a: list, b: list):
if a[0]*a[1] <= 0 and b[0]*b[1] <= 0:
return [float('-inf'), float('inf')]
elif a[1]<0:
if (not b[0]) and (b[0] < b[1]):
return [float('-inf'), a[1]/b[1]]
elif (not b[1]) and (b[0] < b[1]):
return [a[1]/b[0], float('inf')]
elif b[0] < 0 and 0 < b[1]:
return [[float('-inf'), a[1]/b[1]],
[a[1]/b[0], float('inf')]]
else:
raise Exception('The division is not defined!')
elif 0<a[0]:
if (not b[0]) and (b[0] < b[1]):
return [a[0]/b[1], float('inf')]
elif (not b[1]) and (b[0] < b[1]):
return [float('-inf'), a[0]/b[0]]
elif b[0] < 0 and 0 < b[1]:
return [[float('-inf'), a[0]/b[0]],
[a[0]/b[1], float('inf')]]
else:
raise Exception('The division is not defined!')
else:
raise Exception('The division is not defined!')
For a simple example, here is her output:
kdiv([3,5], [-2,3])
# [[-inf, -1.5], [1.0, inf]]
In my code this function is used quite often and is there no way to speed up the calculations? Note that there are 7 different outputs.