# New Math Functions in C#

Posted

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