New Math Functions in Go

Posted
Comments None

To generate the Go code for the new 39 math functions that were introduced in GeneXproTools with Mini-Release 1, I decided to use the VB.Net code instead of the C++ code like Glenn did for the Go Math Grammar (see the previous post "Support for the Go Language in GeneXproTools"). And the main reason for this choice is that I can use "Then", "End If", "Else" and "ElseIf" to put the curly braces of the Go language neatly in place.

There are also other reasons for choosing the VB.Net Grammar as template instead of the C++ Grammar and that includes the fact that both Go and VB.Net have built-in min and max functions, which, in this particular case of the new 39 math functions, means less modifications to the code as we are evaluating min and max values in quite a few places in the code.

So, in all, the required modifications to the VB.Net code include replacing "Then" by "{", "End If" by "}", "Else" by "} else {", "ElseIf" by "} else if", "If" by "if", "Return" by "return", "End Function" by "}", "<>" by "!=", "And" by "&&", "Min" by "math.Min", "Max" by "math.Max", "Constant SLACK As Double" by "const SLACK", "As Double =" and "As Integer =" by ":=" and then remove "Dim" and change the comment marks to "//".

And finally, we also need to take care of the function declarations, which are different in both programming languages. But again, these are easily done by replacing each of the four templates (one for each of the four types of functions: functions of 1, 2, 3, and 4 arguments) by the correct one.

So here's the Go code for the new 39 math functions that were added to the built-in math functions of GeneXproTools 5.0 with Mini-Release 1 (note however that the Go Programming Language will only be available in GeneXproTools with the next mini-release):

func gepRamp1(x float64) float64 {
    if x > 0.0 {
        return x
    } else {
        return 0.0
    }
}

func gepRamp2(x float64) float64 {
    if x > 0.0 {
        return 0.0
    } else {
        return x
    }
}

func gepRamp3(x float64) float64 {
    if x > 0.0 {
        return 0.0
    } else {
        return -x
    }
}

func gepRamp4(x float64) float64 {
    if x > 0.0 {
        return -x
    } else {
        return 0.0
    }
}

func gepStep1(x float64) float64 {
    if x > 0.0 {
        return 1.0
    } else {
        return -1.0
    }
}

func gepStep2(x float64) float64 {
    if x > 0.0 {
        return 1.0
    } else {
        return 0.0
    }
}

func gepStep3(x float64) float64 {
    if x >= 1.0 {
        return 1.0
    } else if x <= -1.0 {
        return -1.0
    } else {
        return x
    }
}

func gepStep4(x float64) float64 {
    if x >= 1.0 {
        return 1.0
    } else if x <= 0.0 {
        return 0.0
    } else {
        return x
    }
}

func gepCL2A(x, y float64) float64 {
    if x > 0.0 && y > 0.0 {
        return 1.0
    } else {
        return -1.0
    }
}

func gepCL2B(x, y float64) float64 {
    if x >= 0.0 && y < 0.0 {
        return -1.0
    } else {
        return 1.0
    }
}

func gepCL2C(x, y float64) float64 {
    if x > 1.0 && y < -1.0 {
        return -1.0
    } else {
        return 1.0
    }
}

func gepCL2D(x, y float64) float64 {
    if x > 0.0 && y > 0.0 {
        return 1.0
    } else {
        return 0.0
    }
}

func gepCL2E(x, y float64) float64 {
    if x >= 0.0 && y <= 0.0 {
        return 0.0
    } else {
        return 1.0
    }
}

func gepCL2F(x, y float64) float64 {
    if x > 1.0 && y < -1.0 {
        return 0.0
    } else {
        return 1.0
    }
}

func gepCL3A(x, y float64) float64 {
    if x > 0.0 && y < 0.0 {
        return 1.0
    } else if x < 0.0 && y > 0.0 {
        return -1.0
    } else {
        return 0.0
    }
}

func gepCL3B(x, y float64) float64 {
    if x >= 1.0 && y >= 1.0 {
        return 1.0
    } else if x <= -1.0 && y <= -1.0 {
        return -1.0
    } else {
        return 0.0
    }
}

func gepCL3C(x, y float64) float64 {
    if x > 0.0 && y > 0.0 {
        return 1.0
    } else if x < 0.0 && y < 0.0 {
        return -1.0
    } else {
        return 0.0
    }
}

func gepMap3A(x, y float64) float64 {
    const SLACK = 10.0
    if y < (x - SLACK) {
        return -1.0
    } else if y > (x + SLACK) {
        return 1.0
    } else {
        return 0.0
    }
}

func gepMap3B(x, y, z float64) float64 {
    minValue := math.Min(x,y)
    maxValue := math.Max(x,y)
    if z < minValue {
        return -1.0
    } else if z > maxValue {
        return 1.0
    } else {
        return 0.0
    }
}

func gepMap3C(a, b, c, d float64) float64 {
    minValue := math.Min(math.Min(a,b),c)
    maxValue := math.Max(math.Max(a,b),c)
    if d < minValue {
        return -1.0
    } else if d > maxValue {
        return 1.0
    } else {
        return 0.0
    }
}

func gepMap4A(x, y float64) float64 {
    const SLACK = 10.0
    outVal := 0.0
    if y < (x - SLACK) {
        outVal = 0.0
    } else if y >= (x - SLACK) && y < x {
        outVal = 1.0
    } else if y >= x && y < (x + SLACK) {
        outVal = 2.0
    } else if y >= (x + SLACK) {
        outVal = 3.0
    }
    return outVal
}

func gepMap4B(x, y, z float64) float64 {
    // evaluate minValue(x,y), maxValue(x,y) and midrange
    minValue := math.Min(x,y)
    maxValue := math.Max(x,y)
    midrange := (maxValue + minValue)/2.0
    
    outVal := 0.0
    if z < minValue {
        outVal = 0.0
    } else if z >= minValue && z < midrange {
        outVal = 1.0
    } else if z >= midrange && z < maxValue {
        outVal = 2.0
    } else if z >= maxValue {
        outVal = 3.0
    }
    return outVal
}

func gepMap4C(a, b, c, d float64) float64 {
    // 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 && 0 != argMax {
        midleValue = a
    }
    if 1 != argMin && 1 != argMax {
        midleValue = b
    }

    outVal := 0.0
    if d < minValue {
        outVal = 0.0
    } else if d >= minValue && d < midleValue {
        outVal = 1.0
    } else if d >= midleValue && d < maxValue {
        outVal = 2.0
    } else if d >= maxValue {
        outVal = 3.0
    }
    return outVal
}

func gepMap5A(x, y float64) float64 {
    const SLACK = 15.0
    outVal := 0.0
    if y < (x - SLACK) {
        outVal = 0.0
    } else if y >= (x - SLACK) && y < (x - SLACK/3.0) {
        outVal = 1.0
    } else if y >= (x - SLACK/3.0) && y < (x + SLACK/3.0) {
        outVal = 2.0
    } else if y >= (x + SLACK/3.0) && y < (x + SLACK) {
        outVal = 3.0
    } else if y >= (x + SLACK) {
        outVal = 4.0
    }
    return outVal
}

func gepMap5B(x, y, z float64) float64 {
    // evaluate minValue(x,y), maxValue(x,y), midpoint1, midpoint2
    minValue := math.Min(x,y)
    maxValue := math.Max(x,y)
    intervalLength := (maxValue - minValue)/3.0
    midpoint1 := minValue + intervalLength
    midpoint2 := minValue + 2.0*intervalLength
    
    outVal := 0.0
    if z < minValue {
        outVal = 0.0
    } else if z >= minValue && z < midpoint1 {
        outVal = 1.0
    } else if z >= midpoint1 && z < midpoint2 {
        outVal = 2.0
    } else if z >= midpoint2 && z < maxValue {
        outVal = 3.0
    } else if z >= maxValue {
        outVal = 4.0
    }
    return outVal
}

func gepMap5C(a, b, c, d float64) float64 {
    // 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 && 0 != argMax {
        midleValue = a
    }
    if 1 != argMin && 1 != argMax {
        midleValue = b
    }
    midrange1 := (minValue + midleValue)/2.0
    midrange2 := (midleValue + maxValue)/2.0

    outVal := 0.0
    if d < minValue {
        outVal = 0.0
    } else if d >= minValue && d < midrange1 {
        outVal = 1.0
    } else if d >= midrange1 && d < midrange2 {
        outVal = 2.0
    } else if d >= midrange2 && d < maxValue {
        outVal = 3.0
    } else if d >= maxValue {
        outVal = 4.0
    }
    return outVal
}

func gepMap6A(x, y float64) float64 {
    const SLACK = 10.0
    outVal := 0.0
    if y < (x - SLACK) {
        outVal = 0.0
    } else if y >= (x - SLACK) && y < (x - SLACK/2.0) {
        outVal = 1.0
    } else if y >= (x - SLACK/2.0) && y < x {
        outVal = 2.0
    } else if y >= x && y < (x + SLACK/2.0) {
        outVal = 3.0
    } else if y >= (x + SLACK/2.0) && y < (x + SLACK) {
        outVal = 4.0
    } else if y >= (x + SLACK) {
        outVal = 5.0
    }
    return outVal
}

func gepMap6B(x, y, z float64) float64 {
    minValue := math.Min(x,y)
    maxValue := math.Max(x,y)
    midrange := (minValue + maxValue)/2.0
    midpoint1 := (minValue + midrange)/2.0
    midpoint2 := (midrange + maxValue)/2.0
    
    outVal := 0.0
    if z < minValue {
        outVal = 0.0
    } else if z >= minValue && z < midpoint1 {
        outVal = 1.0
    } else if z >= midpoint1 && z < midrange {
        outVal = 2.0
    } else if z >= midrange && z < midpoint2 {
        outVal = 3.0
    } else if z >= midpoint2 && z < maxValue {
        outVal = 4.0
    } else if z >= maxValue {
        outVal = 5.0
    }
    return outVal
}

func gepMap6C(a, b, c, d float64) float64 {
    // 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 && 0 != argMax {
        midleValue = a
    }
    if 1 != argMin && 1 != argMax {
        midleValue = b
    }
    // evaluate midrange1 and midrange2
    midrange1 := (minValue + midleValue)/2.0
    midrange2 := (midleValue + maxValue)/2.0

    outVal := 0.0
    if d < minValue {
        outVal = 0.0
    } else if d >= minValue && d < midrange1 {
        outVal = 1.0
    } else if d >= midrange1 && d < midleValue {
        outVal = 2.0
    } else if d >= midleValue && d < midrange2 {
        outVal = 3.0
    } else if d >= midrange2 && d < maxValue {
        outVal = 4.0
    } else if d >= maxValue {
        outVal = 5.0
    }
    return outVal
}

func gepECL3A(x, y, z float64) float64 {
    if y > x && z < x {
        return 1.0
    } else if y < x && z > x {
        return -1.0
    } else {
        return 0.0
    }
}

func gepECL3B(x, y, z float64) float64 {
    if y > x && z > x {
        return 1.0
    } else if y < x && z < x {
        return -1.0
    } else {
        return 0.0
    }
}

func gepECL3C(x, y, z float64) float64 {
    if y >= x && z >= x {
        return 1.0
    } else if y <= -x && z <= -x {
        return -1.0
    } else {
        return 0.0
    }
}

func gepECL3D(a, b, c, d float64) float64 {
    minValue := math.Min(a,b)
    maxValue := math.Max(a,b)
    if c >= maxValue && d >= maxValue {
        return 1.0
    } else if c <= minValue && d <= minValue {
        return -1.0
    } else {
        return 0.0
    }
}

func gepAMin2(x, y float64) float64 {
    if x < y {
        return 0.0
    } else {
        return 1.0
    }
}

func gepAMin3(x, y, z float64) float64 {
    temp := x
    argMin := 0.0
    if temp >= y {
        temp = y
        argMin = 1.0
    }
    if temp >= z {
        argMin = 2.0
    }
    return argMin
}

func gepAMin4(a, b, c, d float64) float64 {
    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
}

func gepAMax2(x, y float64) float64 {
    if x >= y {
        return 0.0
    } else {
        return 1.0
    }
}

func gepAMax3(x, y, z float64) float64 {
    temp := x
    argMax := 0.0
    if temp < y {
        temp = y
        argMax = 1.0
    }
    if temp < z {
        argMax = 2.0
    }
    return argMax
}

func gepAMax4(a, b, c, d float64) float64 {
    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 →