Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
225 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...