Posted
Comments None

The Excel VBA code for all the new 39 math functions to be added to the built-in math functions of GeneXproTools is extremely important for two main reasons. First of all, the Excel VBA code is being used to double check the generated code through the deployment of models and ensembles to Excel.

Second of all, because of the double checking, the VBA code can be used as a new template grammar for some of the programming languages that GeneXproTools supports, like Visual Basic, VB.Net and others.

As we will see below, the Excel VBA code for the new 39 math functions differs quite a bit from the C++ code. As it turns out, there are some transformations that are easily done with a simple Replace All, but others require extra typing. For example, the function declarations, although quite different, can be easily converted to VBA with find & replace; the IF THEN ELSE statements, however, need some attention as VBA requires the word "Then" after each If and ElseIf and also End If at the end of each If block.

Another difference is in the declaration and initialization of variables, which in Excel VBA require decoupling the variable declaration from its initialization and also the word "Dim" before the variable name and "As Double" or "As Long" after the variable/constant name. The return statement is also a pain, as VBA uses the name of the function for that purpose.

There are also other minor differences such as the comment marks, "And" instead of "&&", "<>" instead of "!=", and so on, but these are easily fixed with a simple Replace All. But you can take a look at the complete Excel VBA code below to check all the differences:

Function gepRamp1(ByVal x As Double) As Double
    If x > 0.0 Then
        gepRamp1 = x
    Else
        gepRamp1 = 0
    End If
End Function

Function gepRamp2(ByVal x As Double) As Double
    If x > 0.0 Then
        gepRamp2 = 0
    Else
        gepRamp2 = x
    End If
End Function

Function gepRamp3(ByVal x As Double) As Double
    If x > 0.0 Then
        gepRamp3 = 0
    Else
        gepRamp3 = -x
    End If
End Function

Function gepRamp4(ByVal x As Double) As Double
    If x > 0.0 Then
        gepRamp4 = -x
    Else
        gepRamp4 = 0
    End If
End Function

Function gepStep1(ByVal x As Double) As Double
    If x > 0.0 Then
        gepStep1 = 1
    Else
        gepStep1 = -1
    End If
End Function

Function gepStep2(ByVal x As Double) As Double
    If x > 0.0 Then
        gepStep2 = 1
    Else
        gepStep2 = 0
    End If
End Function

Function gepStep3(ByVal x As Double) As Double
    If x >= 1 Then
        gepStep3 = 1
    ElseIf x <= -1 Then
        gepStep3 = -1
    Else
        gepStep3 = x
    End If
End Function

Function gepStep4(ByVal x As Double) As Double
    If x >= 1 Then
        gepStep4 = 1
    ElseIf x <= 0 Then
        gepStep4 = 0
    Else
        gepStep4 = x
    End If
End Function

Function gepCL2A(ByVal x As Double, ByVal y As Double) As Double
    If x > 0.0 And y > 0.0 Then
        gepCL2A = 1
    Else
        gepCL2A = -1
    End If
End Function

Function gepCL2B(ByVal x As Double, ByVal y As Double) As Double
    If x >= 0 And y < 0.0 Then
        gepCL2B = -1
    Else
        gepCL2B = 1
    End If
End Function

Function gepCL2C(ByVal x As Double, ByVal y As Double) As Double
    If x > 1.0 And y < -1.0 Then
        gepCL2C = -1
    Else
        gepCL2C = 1
    End If
End Function

Function gepCL2D(ByVal x As Double, ByVal y As Double) As Double
    If x > 0.0 And y > 0.0 Then
        gepCL2D = 1
    Else
        gepCL2D = 0
    End If
End Function

Function gepCL2E(ByVal x As Double, ByVal y As Double) As Double
    If x >= 0 And y <= 0 Then
        gepCL2E = 0
    Else
        gepCL2E = 1
    End If
End Function

Function gepCL2F(ByVal x As Double, ByVal y As Double) As Double
    If x > 1.0 And y < -1.0 Then
        gepCL2F = 0
    Else
        gepCL2F = 1
    End If
End Function

Function gepCL3A(ByVal x As Double, ByVal y As Double) As Double
    If x > 0.0 And y < 0.0 Then
        gepCL3A = 1
    ElseIf x < 0.0 And y > 0.0 Then
        gepCL3A = -1
    Else
        gepCL3A = 0
    End If
End Function

Function gepCL3B(ByVal x As Double, ByVal y As Double) As Double
    If x >= 1 And y >= 1 Then
        gepCL3B = 1
    ElseIf x <= -1 And y <= -1 Then
        gepCL3B = -1
    Else
        gepCL3B = 0
    End If
End Function

Function gepCL3C(ByVal x As Double, ByVal y As Double) As Double
    If x > 0.0 And y > 0.0 Then
        gepCL3C = 1
    ElseIf x < 0.0 And y < 0.0 Then
        gepCL3C = -1
    Else
        gepCL3C = 0
    End If
End Function

Function gepMap3A(ByVal x As Double, ByVal y As Double) As Double
    Const SLACK As Double = 10.0
    If y < (x - SLACK) Then
        gepMap3A = -1
    ElseIf y > (x + SLACK) Then
        gepMap3A = 1
    Else
        gepMap3A = 0
    End If
End Function

Function gepMap3B(ByVal x As Double, ByVal y As Double, ByVal z As Double) As Double
    ' evaluate minValue(x,y) and maxValue(x,y)
    Dim minValue As Double
    Dim maxValue As Double
    minValue = x
    maxValue = y
    If minValue > y Then
        minValue = y
        maxValue = x
    End If
    
    If z < minValue Then
        gepMap3B = -1
    ElseIf z > maxValue Then
        gepMap3B = 1
    Else
        gepMap3B = 0
    End If
End Function

Function gepMap3C(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double) As Double
    ' evaluate minValue(a,b,c) and maxValue(a,b,c)
    '
    ' evaluate minValue(a,b,c)
    Dim minValue As Double
    minValue = a
    If minValue > b Then minValue = b
    If minValue > c Then minValue = c
    ' evaluate maxValue(a,b,c)
    Dim maxValue As Double
    maxValue = a
    If maxValue < b Then maxValue = b
    If maxValue < c Then maxValue = c

    If d < minValue Then
        gepMap3C = -1
    ElseIf d > maxValue Then
        gepMap3C = 1
    Else
        gepMap3C = 0
    End If
End Function

Function gepMap4A(ByVal x As Double, ByVal y As Double) As Double
    Const SLACK As Double = 10.0
    If y < (x - SLACK) Then
        gepMap4A = 0
    ElseIf y >= (x - SLACK) And y < x Then
        gepMap4A = 1
    ElseIf y >= x And y < (x + SLACK) Then
        gepMap4A = 2
    ElseIf y >= (x + SLACK) Then
        gepMap4A = 3
    End If
End Function

Function gepMap4B(ByVal x As Double, ByVal y As Double, ByVal z As Double) As Double
    ' evaluate minValue(x,y), maxValue(x,y) and midrange
    Dim minValue As Double
    Dim maxValue As Double
    minValue = x
    maxValue = y
    If minValue > y Then
        minValue = y
        maxValue = x
    End If
    Dim midrange As Double
    midrange = (maxValue + minValue)/2.0
    
    If z < minValue Then
        gepMap4B = 0
    ElseIf z >= minValue And z < midrange Then
        gepMap4B = 1
    ElseIf z >= midrange And z < maxValue Then
        gepMap4B = 2
    ElseIf z >= maxValue Then
        gepMap4B = 3
    End If
End Function

Function gepMap4C(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double) As Double
    ' evaluate minValue(a,b,c), maxValue(a,b,c) and midleValue(a,b,c)
    '
    ' evaluate minValue(a,b,c)
    Dim minValue As Double
    Dim argMin As Long
    minValue = a
    argMin = 0
    If minValue > b Then
        minValue = b
        argMin = 1
    End If
    If minValue > c Then
        minValue = c
        argMin = 2
    End If
    ' evaluate maxValue(a,b,c)
    Dim maxValue As Double
    Dim argMax As Long
    maxValue = a
    argMax = 0
    If maxValue < b Then
        maxValue = b
        argMax = 1
    End If
    If maxValue < c Then
        maxValue = c
        argMax = 2
    End If
    ' evaluate midleValue(a,b,c)
    Dim midleValue As Double
    midleValue = c
    If 0 <> argMin And 0 <> argMax Then
        midleValue = a
    End If
    If 1 <> argMin And 1 <> argMax Then
        midleValue = b
    End If

    If d < minValue Then
        gepMap4C = 0
    ElseIf d >= minValue And d < midleValue Then
        gepMap4C = 1
    ElseIf d >= midleValue And d < maxValue Then
        gepMap4C = 2
    ElseIf d >= maxValue Then
        gepMap4C = 3
    End If
End Function

Function gepMap5A(ByVal x As Double, ByVal y As Double) As Double
    Const SLACK As Double = 15.0
    If y < (x - SLACK) Then
        gepMap5A = 0
    ElseIf y >= (x - SLACK) And y < (x - SLACK/3.0) Then
        gepMap5A = 1
    ElseIf y >= (x - SLACK/3.0) And y < (x + SLACK/3.0) Then
        gepMap5A = 2
    ElseIf y >= (x + SLACK/3.0) And y < (x + SLACK) Then
        gepMap5A = 3
    ElseIf y >= (x + SLACK) Then
        gepMap5A = 4
    End If
End Function

Function gepMap5B(ByVal x As Double, ByVal y As Double, ByVal z As Double) As Double
    ' evaluate minValue(x,y), maxValue(x,y), midpoint1, midpoint2
    Dim minValue As Double
    Dim maxValue As Double
    minValue = x
    maxValue = y
    If minValue > y Then
        minValue = y
        maxValue = x
    End If
    Dim intervalLength As Double
    Dim midpoint1 As Double
    Dim midpoint2 As Double
    intervalLength = (maxValue - minValue)/3.0
    midpoint1 = minValue + intervalLength
    midpoint2 = minValue + 2.0*intervalLength
    
    If z < minValue Then
        gepMap5B = 0
    ElseIf z >= minValue And z < midpoint1 Then
        gepMap5B = 1
    ElseIf z >= midpoint1 And z < midpoint2 Then
        gepMap5B = 2
    ElseIf z >= midpoint2 And z < maxValue Then
        gepMap5B = 3
    ElseIf z >= maxValue Then
        gepMap5B = 4
    End If
End Function

Function gepMap5C(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double) As Double
    ' evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
    '
    ' evaluate minValue(a,b,c)
    Dim minValue As Double
    Dim argMin As Long
    minValue = a
    argMin = 0
    If minValue > b Then
        minValue = b
        argMin = 1
    End If
    If minValue > c Then
        minValue = c
        argMin = 2
    End If
    ' evaluate maxValue(a,b,c)
    Dim maxValue As Double
    Dim argMax As Long
    maxValue = a
    argMax = 0
    If maxValue < b Then
        maxValue = b
        argMax = 1
    End If
    If maxValue < c Then
        maxValue = c
        argMax = 2
    End If
    ' evaluate midleValue(a,b,c)
    Dim midleValue As Double
    midleValue = c
    If 0 <> argMin And 0 <> argMax Then midleValue = a
    If 1 <> argMin And 1 <> argMax Then midleValue = b
    Dim midrange1 As Double
    Dim midrange2 As Double
    midrange1 = (minValue + midleValue)/2.0
    midrange2 = (midleValue + maxValue)/2.0

    If d < minValue Then
        gepMap5C = 0
    ElseIf d >= minValue And d < midrange1 Then
        gepMap5C = 1
    ElseIf d >= midrange1 And d < midrange2 Then
        gepMap5C = 2
    ElseIf d >= midrange2 And d < maxValue Then
        gepMap5C = 3
    ElseIf d >= maxValue Then
        gepMap5C = 4
    End If
End Function

Function gepMap6A(ByVal x As Double, ByVal y As Double) As Double
    Const SLACK As Double = 10.0
    If y < (x - SLACK) Then
        gepMap6A = 0
    ElseIf y >= (x - SLACK) And y < (x - SLACK/2.0) Then
        gepMap6A = 1
    ElseIf y >= (x - SLACK/2.0) And y < x Then
        gepMap6A = 2
    ElseIf y >= x And y < (x + SLACK/2.0) Then
        gepMap6A = 3
    ElseIf y >= (x + SLACK/2.0) And y < (x + SLACK) Then
        gepMap6A = 4
    ElseIf y >= (x + SLACK) Then
        gepMap6A = 5
    End If
End Function

Function gepMap6B(ByVal x As Double, ByVal y As Double, ByVal z As Double) As Double
    ' evaluate minValue(x,y), maxValue(x,y), midrange, midpoint1, midpoint2
    Dim minValue As Double
    Dim maxValue As Double
    minValue = x
    maxValue = y
    If minValue > y Then
        minValue = y
        maxValue = x
    End If
    Dim midrange As Double
    Dim midpoint1 As Double
    Dim midpoint2 As Double
    midrange = (minValue + maxValue)/2.0
    midpoint1 = (minValue + midrange)/2.0
    midpoint2 = (midrange + maxValue)/2.0
    
    If z < minValue Then
        gepMap6B = 0
    ElseIf z >= minValue And z < midpoint1 Then
        gepMap6B = 1
    ElseIf z >= midpoint1 And z < midrange Then
        gepMap6B = 2
    ElseIf z >= midrange And z < midpoint2 Then
        gepMap6B = 3
    ElseIf z >= midpoint2 And z < maxValue Then
        gepMap6B = 4
    ElseIf z >= maxValue Then
        gepMap6B = 5
    End If
End Function

Function gepMap6C(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double) As Double
    ' evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
    '
    ' evaluate minValue(a,b,c)
    Dim minValue As Double
    Dim argMin As Long
    minValue = a
    argMin = 0
    If minValue > b Then
        minValue = b
        argMin = 1
    End If
    If minValue > c Then
        minValue = c
        argMin = 2
    End If
    ' evaluate maxValue(a,b,c)
    Dim maxValue As Double
    Dim argMax As Long
    maxValue = a
    argMax = 0
    If maxValue < b Then
        maxValue = b
        argMax = 1
    End If
    If maxValue < c Then
        maxValue = c
        argMax = 2
    End If
    ' evaluate midleValue(a,b,c)
    Dim midleValue As Double
    midleValue = c
    If 0 <> argMin And 0 <> argMax Then midleValue = a
    If 1 <> argMin And 1 <> argMax Then midleValue = b
    ' evaluate midrange1 and midrange2
    Dim midrange1 As Double
    Dim midrange2 As Double
    midrange1 = (minValue + midleValue)/2.0
    midrange2 = (midleValue + maxValue)/2.0

    If d < minValue Then
        gepMap6C = 0
    ElseIf d >= minValue And d < midrange1 Then
        gepMap6C = 1
    ElseIf d >= midrange1 And d < midleValue Then
        gepMap6C = 2
    ElseIf d >= midleValue And d < midrange2 Then
        gepMap6C = 3
    ElseIf d >= midrange2 And d < maxValue Then
        gepMap6C = 4
    ElseIf d >= maxValue Then
        gepMap6C = 5
    End If
End Function

Function gepECL3A(ByVal x As Double, ByVal y As Double, ByVal z As Double) As Double
    If y > x And z < x Then
        gepECL3A = 1
    ElseIf y < x And z > x Then
        gepECL3A = -1
    Else
        gepECL3A = 0
    End If
End Function

Function gepECL3B(ByVal x As Double, ByVal y As Double, ByVal z As Double) As Double
    If y > x And z > x Then
        gepECL3B = 1
    ElseIf y < x And z < x Then
        gepECL3B = -1
    Else
        gepECL3B = 0
    End If
End Function

Function gepECL3C(ByVal x As Double, ByVal y As Double, ByVal z As Double) As Double
    If y >= x And z >= x Then
        gepECL3C = 1
    ElseIf y <= -x And z <= -x Then
        gepECL3C = -1
    Else
        gepECL3C = 0
    End If
End Function

Function gepECL3D(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double) As Double
    ' evaluate minValue(a,b) and maxValue(a,b)
    Dim minValue As Double
    Dim maxValue As Double
    minValue = a
    maxValue = b
    If minValue > b Then
        minValue = b
        maxValue = a
    End If

    If c >= maxValue And d >= maxValue Then
        gepECL3D = 1
    ElseIf c <= minValue And d <= minValue Then
        gepECL3D = -1
    Else
        gepECL3D = 0
    End If
End Function

Function gepAMin2(ByVal x As Double, ByVal y As Double) As Double
    If x < y Then
        gepAMin2 = 0
    Else
        gepAMin2 = 1
    End If
End Function

Function gepAMin3(ByVal x As Double, ByVal y As Double, ByVal z As Double) As Double
    Dim temp As Double
    Dim argMin As Double
    temp = x
    argMin = 0    
    If temp >= y Then
        temp = y
        argMin = 1
    End If
    If temp >= z Then argMin = 2
    gepAMin3 = argMin
End Function

Function gepAMin4(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double) As Double
    Dim temp As Double
    Dim argMin As Double
    temp = a
    argMin = 0    
    If temp >= b Then
        temp = b
        argMin = 1
    End If
    If temp >= c Then
        temp = c
        argMin = 2
    End If
    If temp >= d Then argMin = 3
    gepAMin4 = argMin
End Function

Function gepAMax2(ByVal x As Double, ByVal y As Double) As Double
    If x >= y Then
        gepAMax2 = 0
    Else
        gepAMax2 = 1
    End If
End Function

Function gepAMax3(ByVal x As Double, ByVal y As Double, ByVal z As Double) As Double
    Dim temp As Double
    Dim argMax As Double
    temp = x
    argMax = 0    
    If temp < y Then
        temp = y
        argMax = 1
    End If
    If temp < z Then argMax = 2
    gepAMax3 = argMax
End Function

Function gepAMax4(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double) As Double
    Dim temp As Double
    Dim argMax As Double
    temp = a
    argMax = 0    
    If temp < b Then
        temp = b
        argMax = 1
    End If
    If temp < c Then
        temp = c
        argMax = 2
    End If
    If temp < d Then argMax = 3
    gepAMax4 = argMax
End Function

Author

Posted
Comments None

The C# code for the 39 new math functions to be added to the C# Grammar of GeneXproTools is almost a clone of the C++ code. I only changed the bits of code where min and max values are evaluated in order to use the native Min and Max functions of C#. However, I left the code for evaluating min and max values in the places where argmin and argmax values are calculated as it makes the code clearer and more uniform across all supported programming languages, which is essential for avoiding introducing new bugs and also for testing.

So here's the C# code (courtesy of the cool new feature of GeneXproTools) for all the new 39 math functions we are going to add to the built-in math functions of GeneXproTools as part of the new project "New Project: Multi-class Classification & Trading Strategies":

    double gepRamp1(double x)
    {
        if (x > 0.0)
            return x;
        else
            return 0.0;
    }

    double gepRamp2(double x)
    {
        if (x > 0.0)
            return 0.0;
        else
            return x;
    }

    double gepRamp3(double x)
    {
        if (x > 0.0)
            return 0.0;
        else
            return -x;
    }

    double gepRamp4(double x)
    {
        if (x > 0.0)
            return -x;
        else
            return 0.0;
    }

    double gepStep1(double x)
    {
        if (x > 0.0)
            return 1.0;
        else
            return -1.0;
    }

    double gepStep2(double x)
    {
        if (x > 0.0)
            return 1.0;
        else
            return 0.0;
    }

    double gepStep3(double x)
    {
        if (x >= 1.0)
            return 1.0;
        else
            if (x <= -1.0)
                return -1.0;
            else
                return x;
    }

    double gepStep4(double x)
    {
        if (x >= 1.0)
            return 1.0;
        else
            if (x <= 0.0)
                return 0.0;
            else
                return x;
    }

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

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

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

    double gepCL2D(double x, double y)
    {
        if (x > 0.0 && y > 0.0)
            return 1.0;
        else
            return 0.0;
    }

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

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

    double gepCL3A(double x, double y)
    {
        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;
    }

    double gepCL3B(double x, double y)
    {
        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;
    }

    double gepCL3C(double x, double y)
    {
        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;
    }

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

    double gepMap3B(double x, double y, double z)
    {
        double minValue = Math.Min(x,y);
        double maxValue = Math.Max(x,y);
        double outVal = 0.0;
        if (z < minValue)
            outVal = -1.0;
        else if (z > maxValue)
            outVal = 1.0;
        return outVal;
    }

    double gepMap3C(double a, double b, double c, double d)
    {
        double minValue = Math.Min(Math.Min(a,b),c);
        double maxValue = Math.Max(Math.Max(a,b),c);
        double outVal = 0.0;
        if (d < minValue)
            outVal = -1.0;
        else if (d > maxValue)
            outVal = 1.0;
        return outVal;
    }

    double gepMap4A(double x, double y)
    {
        const double SLACK = 10.0;
        double 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;
    }

    double gepMap4B(double x, double y, double z)
    {
        // evaluate minValue(x,y), maxValue(x,y) and midrange
        double minValue = Math.Min(x,y);
        double maxValue = Math.Max(x,y);
        double midrange = (maxValue + minValue)/2.0;
        
        double 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;
    }

    double gepMap4C(double a, double b, double c, double d)
    {
        // evaluate minValue(a,b,c), maxValue(a,b,c) and midleValue(a,b,c)
        //
        // evaluate minValue(a,b,c)
        double minValue = a;
        int argMin = 0;
        if (minValue > b)
        {
            minValue = b;
            argMin = 1;
        }
        if (minValue > c)
        {
            minValue = c;
            argMin = 2;
        }
        // evaluate maxValue(a,b,c)
        double maxValue = a;
        int argMax = 0;
        if (maxValue < b)
        {
            maxValue = b;
            argMax = 1;
        }
        if (maxValue < c)
        {
            maxValue = c;
            argMax = 2;
        }
        // evaluate midleValue(a,b,c)
        double midleValue = c;
        if (0 != argMin && 0 != argMax)
            midleValue = a;
        if (1 != argMin && 1 != argMax)
            midleValue = b;

        double 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;
    }

    double gepMap5A(double x, double y)
    {
        const double SLACK = 15.0;
        double 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;
    }

    double gepMap5B(double x, double y, double z)
    {
        // evaluate minValue(x,y), maxValue(x,y), midpoint1, midpoint2
        double minValue = Math.Min(x,y);
        double maxValue = Math.Max(x,y);
        double intervalLength = (maxValue - minValue)/3.0;
        double midpoint1 = minValue + intervalLength;
        double midpoint2 = minValue + 2.0*intervalLength;
        
        double 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;
    }

    double gepMap5C(double a, double b, double c, double d)
    {
        // evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
        //
        // evaluate minValue(a,b,c)
        double minValue = a;
        int argMin = 0;
        if (minValue > b)
        {
            minValue = b;
            argMin = 1;
        }
        if (minValue > c)
        {
            minValue = c;
            argMin = 2;
        }
        // evaluate maxValue(a,b,c)
        double maxValue = a;
        int argMax = 0;
        if (maxValue < b)
        {
            maxValue = b;
            argMax = 1;
        }
        if (maxValue < c)
        {
            maxValue = c;
            argMax = 2;
        }
        // evaluate midleValue(a,b,c)
        double midleValue = c;
        if (0 != argMin && 0 != argMax)
            midleValue = a;
        if (1 != argMin && 1 != argMax)
            midleValue = b;
        // evaluate midrange1 and midrange2
        double midrange1 = (minValue + midleValue)/2.0;
        double midrange2 = (midleValue + maxValue)/2.0;

        double 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;
    }

    double gepMap6A(double x, double y)
    {
        const double SLACK = 10.0;
        double 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;
    }

    double gepMap6B(double x, double y, double z)
    {
        // evaluate minValue(x,y), maxValue(x,y), midrange, midpoint1, midpoint2
        double minValue = Math.Min(x,y);
        double maxValue = Math.Max(x,y);
        double midrange = (minValue + maxValue)/2.0;
        double midpoint1 = (minValue + midrange)/2.0;
        double midpoint2 = (midrange + maxValue)/2.0;
        
        double 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;
    }

    double gepMap6C(double a, double b, double c, double d)
    {
        // evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
        //
        // evaluate minValue(a,b,c)
        double minValue = a;
        int argMin = 0;
        if (minValue > b)
        {
            minValue = b;
            argMin = 1;
        }
        if (minValue > c)
        {
            minValue = c;
            argMin = 2;
        }
        // evaluate maxValue(a,b,c)
        double maxValue = a;
        int argMax = 0;
        if (maxValue < b)
        {
            maxValue = b;
            argMax = 1;
        }
        if (maxValue < c)
        {
            maxValue = c;
            argMax = 2;
        }
        // evaluate midleValue(a,b,c)
        double midleValue = c;
        if (0 != argMin && 0 != argMax)
            midleValue = a;
        if (1 != argMin && 1 != argMax)
            midleValue = b;
        // evaluate midrange1 and midrange2
        double midrange1 = (minValue + midleValue)/2.0;
        double midrange2 = (midleValue + maxValue)/2.0;

        double 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;
    }

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

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

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

    double gepECL3D(double a, double b, double c, double d)
    {
        double minValue = Math.Min(a,b);
        double 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;
    }

    double gepAMin2(double x, double y)
    {
        if (x < y)
            return 0.0;
        else
            return 1.0;
    }

    double gepAMin3(double x, double y, double z)
    {
        double temp = x;
        double argMin = 0.0;    
        if (temp >= y)
        {
            temp = y;
            argMin = 1.0;
        }
        if (temp >= z)
        {
            argMin = 2.0;
        }
        return argMin;
    }

    double gepAMin4(double a, double b, double c, double d)
    {
        double temp = a;
        double 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;
    }

    double gepAMax2(double x, double y)
    {
        if (x >= y)
            return 0.0;
        else
            return 1.0;
    }

    double gepAMax3(double x, double y, double z)
    {
        double temp = x;
        double argMax = 0.0;    
        if (temp < y)
        {
            temp = y;
            argMax = 1.0;
        }
        if (temp < z)
        {
            argMax = 2.0;
        }
        return argMax;
    }

    double gepAMax4(double a, double b, double c, double d)
    {
        double temp = a;
        double 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

Posted
Comments None

As far as the new 39 math functions are concerned, the only difference between the C++ code and the C code is in the declaration of constants. And since in C we don't have constants, we'll have to declare the SLACK constants that are used in the code of some of the new mapper functions (Map3A, Map4A, Map5A and Map6A) as a double. So the C Grammar is slightly different from the C++ Grammar in this one aspect.

And here's the C code for all the new 39 math functions that are going to be added to the built-in math functions of GeneXproTools with this new mini-release (see how I'm generating the code automatically in the post "Function Design: Programming Languages"):

double gepRamp1(double x)
{
    if (x > 0.0)
        return x;
    else
        return 0.0;
}

double gepRamp2(double x)
{
    if (x > 0.0)
        return 0.0;
    else
        return x;
}

double gepRamp3(double x)
{
    if (x > 0.0)
        return 0.0;
    else
        return -x;
}

double gepRamp4(double x)
{
    if (x > 0.0)
        return -x;
    else
        return 0.0;
}

double gepStep1(double x)
{
    if (x > 0.0)
        return 1.0;
    else
        return -1.0;
}

double gepStep2(double x)
{
    if (x > 0.0)
        return 1.0;
    else
        return 0.0;
}

double gepStep3(double x)
{
    if (x >= 1.0)
        return 1.0;
    else
        if (x <= -1.0)
            return -1.0;
        else
            return x;
}

double gepStep4(double x)
{
    if (x >= 1.0)
        return 1.0;
    else
        if (x <= 0.0)
            return 0.0;
        else
            return x;
}

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

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

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

double gepCL2D(double x, double y)
{
    if (x > 0.0 && y > 0.0)
        return 1.0;
    else
        return 0.0;
}

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

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

double gepCL3A(double x, double y)
{
    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;
}

double gepCL3B(double x, double y)
{
    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;
}

double gepCL3C(double x, double y)
{
    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;
}

double gepMap3A(double x, double y)
{
    double SLACK = 10.0;
    double outVal = 0.0;
    if (y < (x - SLACK))
        outVal = -1.0;
    else if (y > (x + SLACK))
        outVal = 1.0;
    return outVal;
}

double gepMap3B(double x, double y, double z)
{
    // evaluate minValue(x,y) and maxValue(x,y)
    double minValue = x;
    double maxValue = y;
    if (minValue > y)
    {
        minValue = y;
        maxValue = x;
    }
    
    double outVal = 0.0;
    if (z < minValue)
        outVal = -1.0;
    else if (z > maxValue)
        outVal = 1.0;
    return outVal;
}

double gepMap3C(double a, double b, double c, double d)
{
    // evaluate minValue(a,b,c) and maxValue(a,b,c)
    //
    // evaluate minValue(a,b,c)
    double minValue = a;
    if (minValue > b)
        minValue = b;
    if (minValue > c)
        minValue = c;
    // evaluate maxValue(a,b,c)
    double maxValue = a;
    if (maxValue < b)
        maxValue = b;
    if (maxValue < c)
        maxValue = c;

    double outVal = 0.0;
    if (d < minValue)
        outVal = -1.0;
    else if (d > maxValue)
        outVal = 1.0;
    return outVal;
}

double gepMap4A(double x, double y)
{
    double SLACK = 10.0;
    double 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;
}

double gepMap4B(double x, double y, double z)
{
    // evaluate minValue(x,y), maxValue(x,y) and midrange
    double minValue = x;
    double maxValue = y;
    if (minValue > y)
    {
        minValue = y;
        maxValue = x;
    }
    double midrange = (maxValue + minValue)/2.0;
    
    double 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;
}

double gepMap4C(double a, double b, double c, double d)
{
    // evaluate minValue(a,b,c), maxValue(a,b,c) and midleValue(a,b,c)
    //
    // evaluate minValue(a,b,c)
    double minValue = a;
    int argMin = 0;
    if (minValue > b)
    {
        minValue = b;
        argMin = 1;
    }
    if (minValue > c)
    {
        minValue = c;
        argMin = 2;
    }
    // evaluate maxValue(a,b,c)
    double maxValue = a;
    int argMax = 0;
    if (maxValue < b)
    {
        maxValue = b;
        argMax = 1;
    }
    if (maxValue < c)
    {
        maxValue = c;
        argMax = 2;
    }
    // evaluate midleValue(a,b,c)
    double midleValue = c;
    if (0 != argMin && 0 != argMax)
        midleValue = a;
    if (1 != argMin && 1 != argMax)
        midleValue = b;

    double 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;
}

double gepMap5A(double x, double y)
{
    double SLACK = 15.0;
    double 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;
}

double gepMap5B(double x, double y, double z)
{
    // evaluate minValue(x,y), maxValue(x,y), midpoint1, midpoint2
    double minValue = x;
    double maxValue = y;
    if (minValue > y)
    {
        minValue = y;
        maxValue = x;
    }
    double intervalLength = (maxValue - minValue)/3.0;
    double midpoint1 = minValue + intervalLength;
    double midpoint2 = minValue + 2.0*intervalLength;
    
    double 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;
}

double gepMap5C(double a, double b, double c, double d)
{
    // evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
    //
    // evaluate minValue(a,b,c)
    double minValue = a;
    int argMin = 0;
    if (minValue > b)
    {
        minValue = b;
        argMin = 1;
    }
    if (minValue > c)
    {
        minValue = c;
        argMin = 2;
    }
    // evaluate maxValue(a,b,c)
    double maxValue = a;
    int argMax = 0;
    if (maxValue < b)
    {
        maxValue = b;
        argMax = 1;
    }
    if (maxValue < c)
    {
        maxValue = c;
        argMax = 2;
    }
    // evaluate midleValue(a,b,c)
    double midleValue = c;
    if (0 != argMin && 0 != argMax)
        midleValue = a;
    if (1 != argMin && 1 != argMax)
        midleValue = b;
    // evaluate midrange1 and midrange2
    double midrange1 = (minValue + midleValue)/2.0;
    double midrange2 = (midleValue + maxValue)/2.0;

    double 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;
}

double gepMap6A(double x, double y)
{
    double SLACK = 10.0;
    double 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;
}

double gepMap6B(double x, double y, double z)
{
    // evaluate minValue(x,y), maxValue(x,y), midrange, midpoint1, midpoint2
    double minValue = x;
    double maxValue = y;
    if (minValue > y)
    {
        minValue = y;
        maxValue = x;
    }
    double midrange = (minValue + maxValue)/2.0;
    double midpoint1 = (minValue + midrange)/2.0;
    double midpoint2 = (midrange + maxValue)/2.0;
    
    double 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;
}

double gepMap6C(double a, double b, double c, double d)
{
    // evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
    //
    // evaluate minValue(a,b,c)
    double minValue = a;
    int argMin = 0;
    if (minValue > b)
    {
        minValue = b;
        argMin = 1;
    }
    if (minValue > c)
    {
        minValue = c;
        argMin = 2;
    }
    // evaluate maxValue(a,b,c)
    double maxValue = a;
    int argMax = 0;
    if (maxValue < b)
    {
        maxValue = b;
        argMax = 1;
    }
    if (maxValue < c)
    {
        maxValue = c;
        argMax = 2;
    }
    // evaluate midleValue(a,b,c)
    double midleValue = c;
    if (0 != argMin && 0 != argMax)
        midleValue = a;
    if (1 != argMin && 1 != argMax)
        midleValue = b;
    // evaluate midrange1 and midrange2
    double midrange1 = (minValue + midleValue)/2.0;
    double midrange2 = (midleValue + maxValue)/2.0;

    double 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;
}

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

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

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

double gepECL3D(double a, double b, double c, double d)
{
    // evaluate minValue(a,b) and maxValue(a,b)
    double minValue = a;
    double maxValue = b;
    if (minValue > b)
    {
        minValue = b;
        maxValue = a;
    }

    if (c >= maxValue && d >= maxValue)
        return 1.0;
    else
        if (c <= minValue && d <= minValue)
            return -1.0;
        else return 0.0;
}

double gepAMin2(double x, double y)
{
    if (x < y)
        return 0.0;
    else
        return 1.0;
}

double gepAMin3(double x, double y, double z)
{
    double temp = x;
    double argMin = 0.0;    
    if (temp >= y)
    {
        temp = y;
        argMin = 1.0;
    }
    if (temp >= z)
    {
        argMin = 2.0;
    }
    return argMin;
}

double gepAMin4(double a, double b, double c, double d)
{
    double temp = a;
    double 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;
}

double gepAMax2(double x, double y)
{
    if (x >= y)
        return 0.0;
    else
        return 1.0;
}

double gepAMax3(double x, double y, double z)
{
    double temp = x;
    double argMax = 0.0;    
    if (temp < y)
    {
        temp = y;
        argMax = 1.0;
    }
    if (temp < z)
    {
        argMax = 2.0;
    }
    return argMax;
}

double gepAMax4(double a, double b, double c, double d)
{
    double temp = a;
    double 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

Posted
Comments None

The previous post "New Math Functions in C++" was a challenge in terms of composition. For whatever reason, the blog editor I was using (the blog editor of Microsoft Word) refused to publish my post if I included more than 3 functions! And I had 36 more to add!

The code I was trying to add was in C++ and the blog editor was supposed to nicely copy the code, keeping all the formatting, including the different colors for the keywords and comments. And in fact it did that nicely for all the C++ code snippets I've included in previous posts.

Notwithstanding, I knew I had to think of a different way of including the code of all the other languages not recognized by the blog editor as formatted code (for example, the Python code shown in the post "Function Design: Programming Languages" was formatted by hand and it was already a pain for just 3 functions!). And the above mentioned disaster with the C++ code for the previous post just made it crystal clear: There was no way I would be able to format all the code for all the 17 programming languages by hand!

So what I needed was a way to get the HTML that GeneXproTools uses in the Model Panel to show the model code in all the languages it supports! And as it turns out this is a nice feature to add to GeneXproTools for everyone to use! We will now all be able to post the code of our models on the web with the nice formatting and colors without much fuss. We will be releasing this already with the next mini-release as a bonus feature.

Now this seems obvious, doesn't it? But it never occurred to me before and I've posted enough code examples on the web using just plain text and thinking nothing of it (well, almost nothing, because I would commiserate about not being able to use the nice formatting). Sometimes it takes just doing things a little bit differently to come up with a good idea.

I hope you'll find this new feature of GeneXproTools useful and make good use of it. I for one will have plenty of opportunity to use it extensively over the next posts to show you the code for all the new 39 math functions in all the programming languages of GeneXproTools!

Author

Posted
Comments None

The C++ code for the 39 new functions that will be added to the built-in math functions of GeneXproTools is shown below. This code is being generated automatically by GeneXproTools using a custom tailored Karva program composed of all the 39 new functions and the C++ Grammar of GeneXproTools.

As explained in the previous post "Function Design: Programming Languages", the C++ Grammar is our reference language as it corresponds to the endogenous calculator of GeneXproTools, which is also in C++. All the code for the other programming languages supported by GeneXproTools is derived from the C++ Grammar or its descendants through a series of small transformations. Over the next posts I'll try and offer some comments that may be useful for creating Custom Grammars using the Built-in Grammars of GeneXproTools as a starting point.

double gepRamp1(double x)
{
    if (x > 0.0)
        return x;
    else
        return 0.0;
}

double gepRamp2(double x)
{
    if (x > 0.0)
        return 0.0;
    else
        return x;
}

double gepRamp3(double x)
{
    if (x > 0.0)
        return 0.0;
    else
        return -x;
}

double gepRamp4(double x)
{
    if (x > 0.0)
        return -x;
    else
        return 0.0;
}

double gepStep1(double x)
{
    if (x > 0.0)
        return 1.0;
    else
        return -1.0;
}

double gepStep2(double x)
{
    if (x > 0.0)
        return 1.0;
    else
        return 0.0;
}

double gepStep3(double x)
{
    if (x >= 1.0)
        return 1.0;
    else
        if (x <= -1.0)
            return -1.0;
        else
            return x;
}

double gepStep4(double x)
{
    if (x >= 1.0)
        return 1.0;
    else
        if (x <= 0.0)
            return 0.0;
        else
            return x;
}

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

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

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

double gepCL2D(double x, double y)
{
    if (x > 0.0 && y > 0.0)
        return 1.0;
    else
        return 0.0;
}

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

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

double gepCL3A(double x, double y)
{
    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;
}

double gepCL3B(double x, double y)
{
    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;
}

double gepCL3C(double x, double y)
{
    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;
}

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

double gepMap3B(double x, double y, double z)
{
    // evaluate minValue(x,y) and maxValue(x,y)
    double minValue = x;
    double maxValue = y;
    if (minValue > y)
    {
        minValue = y;
        maxValue = x;
    }
    
    double outVal = 0.0;
    if (z < minValue)
        outVal = -1.0;
    else if (z > maxValue)
        outVal = 1.0;
    return outVal;
}

double gepMap3C(double a, double b, double c, double d)
{
    // evaluate minValue(a,b,c) and maxValue(a,b,c)
    //
    // evaluate minValue(a,b,c)
    double minValue = a;
    if (minValue > b)
        minValue = b;
    if (minValue > c)
        minValue = c;
    // evaluate maxValue(a,b,c)
    double maxValue = a;
    if (maxValue < b)
        maxValue = b;
    if (maxValue < c)
        maxValue = c;

    double outVal = 0.0;
    if (d < minValue)
        outVal = -1.0;
    else if (d > maxValue)
        outVal = 1.0;
    return outVal;
}

double gepMap4A(double x, double y)
{
    const double SLACK = 10.0;
    double 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;
}

double gepMap4B(double x, double y, double z)
{
    // evaluate minValue(x,y), maxValue(x,y) and midrange
    double minValue = x;
    double maxValue = y;
    if (minValue > y)
    {
        minValue = y;
        maxValue = x;
    }
    double midrange = (maxValue + minValue)/2.0;
    
    double 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;
}

double gepMap4C(double a, double b, double c, double d)
{
    // evaluate minValue(a,b,c), maxValue(a,b,c) and midleValue(a,b,c)
    //
    // evaluate minValue(a,b,c)
    double minValue = a;
    int argMin = 0;
    if (minValue > b)
    {
        minValue = b;
        argMin = 1;
    }
    if (minValue > c)
    {
        minValue = c;
        argMin = 2;
    }
    // evaluate maxValue(a,b,c)
    double maxValue = a;
    int argMax = 0;
    if (maxValue < b)
    {
        maxValue = b;
        argMax = 1;
    }
    if (maxValue < c)
    {
        maxValue = c;
        argMax = 2;
    }
    // evaluate midleValue(a,b,c)
    double midleValue = c;
    if (0 != argMin && 0 != argMax)
        midleValue = a;
    if (1 != argMin && 1 != argMax)
        midleValue = b;

    double 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;
}

double gepMap5A(double x, double y)
{
    const double SLACK = 15.0;
    double 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;
}

double gepMap5B(double x, double y, double z)
{
    // evaluate minValue(x,y), maxValue(x,y), midpoint1, midpoint2
    double minValue = x;
    double maxValue = y;
    if (minValue > y)
    {
        minValue = y;
        maxValue = x;
    }
    double intervalLength = (maxValue - minValue)/3.0;
    double midpoint1 = minValue + intervalLength;
    double midpoint2 = minValue + 2.0*intervalLength;
    
    double 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;
}

double gepMap5C(double a, double b, double c, double d)
{
    // evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
    //
    // evaluate minValue(a,b,c)
    double minValue = a;
    int argMin = 0;
    if (minValue > b)
    {
        minValue = b;
        argMin = 1;
    }
    if (minValue > c)
    {
        minValue = c;
        argMin = 2;
    }
    // evaluate maxValue(a,b,c)
    double maxValue = a;
    int argMax = 0;
    if (maxValue < b)
    {
        maxValue = b;
        argMax = 1;
    }
    if (maxValue < c)
    {
        maxValue = c;
        argMax = 2;
    }
    // evaluate midleValue(a,b,c)
    double midleValue = c;
    if (0 != argMin && 0 != argMax)
        midleValue = a;
    if (1 != argMin && 1 != argMax)
        midleValue = b;
    // evaluate midrange1 and midrange2
    double midrange1 = (minValue + midleValue)/2.0;
    double midrange2 = (midleValue + maxValue)/2.0;

    double 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;
}

double gepMap6A(double x, double y)
{
    const double SLACK = 10.0;
    double 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;
}

double gepMap6B(double x, double y, double z)
{
    // evaluate minValue(x,y), maxValue(x,y), midrange, midpoint1, midpoint2
    double minValue = x;
    double maxValue = y;
    if (minValue > y)
    {
        minValue = y;
        maxValue = x;
    }
    double midrange = (minValue + maxValue)/2.0;
    double midpoint1 = (minValue + midrange)/2.0;
    double midpoint2 = (midrange + maxValue)/2.0;
    
    double 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;
}

double gepMap6C(double a, double b, double c, double d)
{
    // evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
    //
    // evaluate minValue(a,b,c)
    double minValue = a;
    int argMin = 0;
    if (minValue > b)
    {
        minValue = b;
        argMin = 1;
    }
    if (minValue > c)
    {
        minValue = c;
        argMin = 2;
    }
    // evaluate maxValue(a,b,c)
    double maxValue = a;
    int argMax = 0;
    if (maxValue < b)
    {
        maxValue = b;
        argMax = 1;
    }
    if (maxValue < c)
    {
        maxValue = c;
        argMax = 2;
    }
    // evaluate midleValue(a,b,c)
    double midleValue = c;
    if (0 != argMin && 0 != argMax)
        midleValue = a;
    if (1 != argMin && 1 != argMax)
        midleValue = b;
    // evaluate midrange1 and midrange2
    double midrange1 = (minValue + midleValue)/2.0;
    double midrange2 = (midleValue + maxValue)/2.0;

    double 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;
}

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

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

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

double gepECL3D(double a, double b, double c, double d)
{
    // evaluate minValue(a,b) and maxValue(a,b)
    double minValue = a;
    double maxValue = b;
    if (minValue > b)
    {
        minValue = b;
        maxValue = a;
    }

    if (c >= maxValue && d >= maxValue)
        return 1.0;
    else
        if (c <= minValue && d <= minValue)
            return -1.0;
        else return 0.0;
}

double gepAMin2(double x, double y)
{
    if (x < y)
        return 0.0;
    else
        return 1.0;
}

double gepAMin3(double x, double y, double z)
{
    double temp = x;
    double argMin = 0.0;    
    if (temp >= y)
    {
        temp = y;
        argMin = 1.0;
    }
    if (temp >= z)
    {
        argMin = 2.0;
    }
    return argMin;
}

double gepAMin4(double a, double b, double c, double d)
{
    double temp = a;
    double 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;
}

double gepAMax2(double x, double y)
{
    if (x >= y)
        return 0.0;
    else
        return 1.0;
}

double gepAMax3(double x, double y, double z)
{
    double temp = x;
    double argMax = 0.0;    
    if (temp < y)
    {
        temp = y;
        argMax = 1.0;
    }
    if (temp < z)
    {
        argMax = 2.0;
    }
    return argMax;
}

double gepAMax4(double a, double b, double c, double d)
{
    double temp = a;
    double 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

← Older Newer →