New Math Functions in Python

Posted
Comments None

The Python code for the new 39 math functions of GeneXproTools was easily generated from the VB.Net code.

The IF THEN ELSE construct of Python with the colon after the if expression is easily created by replacing "Then" by the colon, "Else" by "else:" and "ElseIf" by "elif"; removing the "End If" line was also easy.

I also ended up putting parentheses both around if expressions and "and" clauses but they are not a requirement of the language.

Then I just had to remove the keywords "Dim", "As Double", "As Integer", and "Const"; replace "Return" by "return"; "If" by "if"; the comment marks by "#"; "<>" by "!=" and "And" by "and".

And finally, the function headers and footers are also different in both programming languages, but the use of templates to generate the grammars greatly simplifies the translation.

Here's the Python code for the new 39 math functions that were added to the built-in math functions of GeneXproTools 5.0 with Mini-Release 1:

def gepRamp1(x):
    if (x > 0.0):
        return x
    else:
        return 0.0

def gepRamp2(x):
    if (x > 0.0):
        return 0.0
    else:
        return x

def gepRamp3(x):
    if (x > 0.0):
        return 0.0
    else:
        return -x

def gepRamp4(x):
    if (x > 0.0):
        return -x
    else:
        return 0.0

def gepStep1(x):
    if (x > 0.0):
        return 1.0
    else:
        return -1.0

def gepStep2(x):
    if (x > 0.0):
        return 1.0
    else:
        return 0.0

def gepStep3(x):
    if (x >= 1.0):
        return 1.0
    elif (x <= -1.0):
        return -1.0
    else:
        return x

def gepStep4(x):
    if (x >= 1.0):
        return 1.0
    elif (x <= 0.0):
        return 0.0
    else:
        return x

def gepCL2A(x, y):
    if ((x > 0.0) and (y > 0.0)):
        return 1.0
    else:
        return -1.0

def gepCL2B(x, y):
    if ((x >= 0.0) and (y < 0.0)):
        return -1.0
    else:
        return 1.0

def gepCL2C(x, y):
    if ((x > 1.0) and (y < -1.0)):
        return -1.0
    else:
        return 1.0

def gepCL2D(x, y):
    if ((x > 0.0) and (y > 0.0)):
        return 1.0
    else:
        return 0.0

def gepCL2E(x, y):
    if ((x >= 0.0) and (y <= 0.0)):
        return 0.0
    else:
        return 1.0

def gepCL2F(x, y):
    if ((x > 1.0) and (y < -1.0)):
        return 0.0
    else:
        return 1.0

def gepCL3A(x, y):
    if ((x > 0.0) and (y < 0.0)):
        return 1.0
    elif ((x < 0.0) and (y > 0.0)):
        return -1.0
    else:
        return 0.0

def gepCL3B(x, y):
    if ((x >= 1.0) and (y >= 1.0)):
        return 1.0
    elif ((x <= -1.0) and (y <= -1.0)):
        return -1.0
    else:
        return 0.0

def gepCL3C(x, y):
    if ((x > 0.0) and (y > 0.0)):
        return 1.0
    elif ((x < 0.0) and (y < 0.0)):
        return -1.0
    else:
        return 0.0

def gepMap3A(x, y):
    SLACK = 10.0
    if (y < (x - SLACK)):
        return -1.0
    elif (y > (x + SLACK)):
        return 1.0
    else:
        return 0.0

def gepMap3B(x, y, z):
    minValue = min(x,y)
    maxValue = max(x,y)
    if (z < minValue):
        return -1.0
    elif (z > maxValue):
        return 1.0
    else:
        return 0.0

def gepMap3C(a, b, c, d):
    minValue = min(a,b,c)
    maxValue = max(a,b,c)
    if (d < minValue):
        return -1.0
    elif (d > maxValue):
        return 1.0
    else:
        return 0.0

def gepMap4A(x, y):
    SLACK = 10.0
    if (y < (x - SLACK)):
        return 0.0
    elif ((y >= (x - SLACK)) and (y < x)):
        return 1.0
    elif ((y >= x) and (y < (x + SLACK))):
        return 2.0
    elif (y >= (x + SLACK)):
        return 3.0

def gepMap4B(x, y, z):
    # evaluate minValue(x,y), maxValue(x,y)) and (midrange
    minValue = min(x,y)
    maxValue = max(x,y)
    midrange = (maxValue + minValue)/2.0
    
    if (z < minValue):
        return 0.0
    elif ((z >= minValue) and (z < midrange)):
        return 1.0
    elif ((z >= midrange) and (z < maxValue)):
        return 2.0
    elif (z >= maxValue):
        return 3.0

def gepMap4C(a, b, c, d):
    # evaluate minValue(a,b,c), maxValue(a,b,c) and midleValue(a,b,c)
    #
    # evaluate minValue(a,b,c) and argMin(a,b,c)
    minValue = a
    argMin = 0
    if (minValue > b):
        minValue = b
        argMin = 1
    if (minValue > c):
        minValue = c
        argMin = 2
    # evaluate maxValue(a,b,c) and argMax(a,b,c)
    maxValue = a
    argMax = 0
    if (maxValue < b):
        maxValue = b
        argMax = 1
    if (maxValue < c):
        maxValue = c
        argMax = 2
    # evaluate midleValue(a,b,c)
    midleValue = c
    if ((0 != argMin) and (0 != argMax)):
        midleValue = a
    if ((1 != argMin) and (1 != argMax)):
        midleValue = b

    if (d < minValue):
        return 0.0
    elif ((d >= minValue) and (d < midleValue)):
        return 1.0
    elif ((d >= midleValue) and (d < maxValue)):
        return 2.0
    elif (d >= maxValue):
        return 3.0

def gepMap5A(x, y):
    SLACK = 15.0
    if (y < (x - SLACK)):
        return 0.0
    elif ((y >= (x - SLACK)) and (y < (x - SLACK/3.0))):
        return 1.0
    elif ((y >= (x - SLACK/3.0)) and (y < (x + SLACK/3.0))):
        return 2.0
    elif ((y >= (x + SLACK/3.0)) and (y < (x + SLACK))):
        return 3.0
    elif (y >= (x + SLACK)):
        return 4.0

def gepMap5B(x, y, z):
    # evaluate minValue(x,y), maxValue(x,y), midpoint1, midpoint2
    minValue = min(x,y)
    maxValue = max(x,y)
    intervalLength = (maxValue - minValue)/3.0
    midpoint1 = minValue + intervalLength
    midpoint2 = minValue + 2.0*intervalLength
    
    if (z < minValue):
        return 0.0
    elif ((z >= minValue) and (z < midpoint1)):
        return 1.0
    elif ((z >= midpoint1) and (z < midpoint2)):
        return 2.0
    elif ((z >= midpoint2) and (z < maxValue)):
        return 3.0
    elif (z >= maxValue):
        return 4.0

def gepMap5C(a, b, c, d):
    # evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
    #
    # evaluate minValue(a,b,c) and argMin(a,b,c)
    minValue = a
    argMin = 0
    if (minValue > b):
        minValue = b
        argMin = 1
    if (minValue > c):
        minValue = c
        argMin = 2
    # evaluate maxValue(a,b,c) and argMax(a,b,c)
    maxValue = a
    argMax = 0
    if (maxValue < b):
        maxValue = b
        argMax = 1
    if (maxValue < c):
        maxValue = c
        argMax = 2
    # evaluate midleValue(a,b,c)
    midleValue = c
    if ((0 != argMin) and (0 != argMax)):
        midleValue = a
    if ((1 != argMin) and (1 != argMax)):
        midleValue = b
    midrange1 = (minValue + midleValue)/2.0
    midrange2 = (midleValue + maxValue)/2.0

    if (d < minValue):
        return 0.0
    elif ((d >= minValue) and (d < midrange1)):
        return 1.0
    elif ((d >= midrange1) and (d < midrange2)):
        return 2.0
    elif ((d >= midrange2) and (d < maxValue)):
        return 3.0
    elif (d >= maxValue):
        return 4.0

def gepMap6A(x, y):
    SLACK = 10.0
    if (y < (x - SLACK)):
        return 0.0
    elif ((y >= (x - SLACK)) and (y < (x - SLACK/2.0))):
        return 1.0
    elif ((y >= (x - SLACK/2.0)) and (y < x)):
        return 2.0
    elif ((y >= x) and (y < (x + SLACK/2.0))):
        return 3.0
    elif ((y >= (x + SLACK/2.0)) and (y < (x + SLACK))):
        return 4.0
    elif (y >= (x + SLACK)):
        return 5.0

def gepMap6B(x, y, z):
    minValue = min(x,y)
    maxValue = max(x,y)
    midrange = (minValue + maxValue)/2.0
    midpoint1 = (minValue + midrange)/2.0
    midpoint2 = (midrange + maxValue)/2.0
    
    if (z < minValue):
        return 0.0
    elif ((z >= minValue) and (z < midpoint1)):
        return 1.0
    elif ((z >= midpoint1) and (z < midrange)):
        return 2.0
    elif ((z >= midrange) and (z < midpoint2)):
        return 3.0
    elif ((z >= midpoint2) and (z < maxValue)):
        return 4.0
    elif (z >= maxValue):
        return 5.0

def gepMap6C(a, b, c, d):
    # evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
    #
    # evaluate minValue(a,b,c) and argMin(a,b,c)
    minValue = a
    argMin = 0
    if (minValue > b):
        minValue = b
        argMin = 1
    if (minValue > c):
        minValue = c
        argMin = 2
    # evaluate maxValue(a,b,c) and argMax(a,b,c)
    maxValue = a
    argMax = 0
    if (maxValue < b):
        maxValue = b
        argMax = 1
    if (maxValue < c):
        maxValue = c
        argMax = 2
    # evaluate midleValue(a,b,c)
    midleValue = c
    if ((0 != argMin) and (0 != argMax)):
        midleValue = a
    if ((1 != argMin) and (1 != argMax)):
        midleValue = b
    # evaluate midrange1 and midrange2
    midrange1 = (minValue + midleValue)/2.0
    midrange2 = (midleValue + maxValue)/2.0

    if (d < minValue):
        return 0.0
    elif ((d >= minValue) and (d < midrange1)):
        return 1.0
    elif ((d >= midrange1) and (d < midleValue)):
        return 2.0
    elif ((d >= midleValue) and (d < midrange2)):
        return 3.0
    elif ((d >= midrange2) and (d < maxValue)):
        return 4.0
    elif (d >= maxValue):
        return 5.0

def gepECL3A(x, y, z):
    if ((y > x) and (z < x)):
        return 1.0
    elif ((y < x) and (z > x)):
        return -1.0
    else:
        return 0.0

def gepECL3B(x, y, z):
    if ((y > x) and (z > x)):
        return 1.0
    elif ((y < x) and (z < x)):
        return -1.0
    else:
        return 0.0

def gepECL3C(x, y, z):
    if ((y >= x) and (z >= x)):
        return 1.0
    elif ((y <= -x) and (z <= -x)):
        return -1.0
    else:
        return 0.0

def gepECL3D(a, b, c, d):
    minValue = min(a,b)
    maxValue = max(a,b)
    if ((c >= maxValue) and (d >= maxValue)):
        return 1.0
    elif ((c <= minValue) and (d <= minValue)):
        return -1.0
    else:
        return 0.0

def gepAMin2(x, y):
    if (x < y):
        return 0.0
    else:
        return 1.0

def gepAMin3(x, y, z):
    temp = x
    argMin = 0.0
    if (temp >= y):
        temp = y
        argMin = 1.0
    if (temp >= z):
        argMin = 2.0
    return argMin

def gepAMin4(a, b, c, d):
    temp = a
    argMin = 0.0
    if (temp >= b):
        temp = b
        argMin = 1.0
    if (temp >= c):
        temp = c
        argMin = 2.0
    if (temp >= d):
        argMin = 3.0
    return argMin

def gepAMax2(x, y):
    if (x >= y):
        return 0.0
    else:
        return 1.0

def gepAMax3(x, y, z):
    temp = x
    argMax = 0.0
    if (temp < y):
        temp = y
        argMax = 1.0
    if (temp < z):
        argMax = 2.0
    return argMax

def gepAMax4(a, b, c, d):
    temp = a
    argMax = 0.0
    if (temp < b):
        temp = b
        argMax = 1.0
    if (temp < c):
        temp = c
        argMax = 2.0
    if (temp < d):
        argMax = 3.0
    return argMax

Author

Comments

There are currently no comments on this article.

Comment

your_ip_is_blacklisted_by sbl.spamhaus.org

← Older Newer →