# New Math Functions in Perl

Posted

For the Perl code of the new 39 math functions of GeneXproTools, using the PHP code as the starting point seemed like the right choice since both programming languages prefix their variables and constants with "\$". However, I decided to use the C++ code instead as I could easily replace "double", "int", and "const double" by "my". Moreover, since both C++ and Perl lack built-in min and max functions, I didn't have to make great changes to the C++ code, which again is important for debugging and testing. Then I just had to replace "else if" with "elsif" and the comment marks by "#".

The declaration of functions is also very different in both languages, but it's no trouble replacing the headers of all the four different types of functions (functions of 1, 2, 3, and 4 arguments) by the appropriate one.

The trickier part is placing all the curly braces correctly around all "if" and "else" expressions, which is a requirement of the Perl language.

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

sub gepRamp1
{
(my \$x) = \$_[0];
if (\$x > 0.0)
{
return \$x;
}
else
{
return 0.0;
}
}

sub gepRamp2
{
(my \$x) = \$_[0];
if (\$x > 0.0)
{
return 0.0;
}
else
{
return \$x;
}
}

sub gepRamp3
{
(my \$x) = \$_[0];
if (\$x > 0.0)
{
return 0.0;
}
else
{
return -\$x;
}
}

sub gepRamp4
{
(my \$x) = \$_[0];
if (\$x > 0.0)
{
return -\$x;
}
else
{
return 0.0;
}
}

sub gepStep1
{
(my \$x) = \$_[0];
if (\$x > 0.0)
{
return 1.0;
}
else
{
return -1.0;
}
}

sub gepStep2
{
(my \$x) = \$_[0];
if (\$x > 0.0)
{
return 1.0;
}
else
{
return 0.0;
}
}

sub gepStep3
{
(my \$x) = \$_[0];
if (\$x >= 1.0)
{
return 1.0;
}
elsif (\$x <= -1.0)
{
return -1.0;
}
else
{
return \$x;
}
}

sub gepStep4
{
(my \$x) = \$_[0];
if (\$x >= 1.0)
{
return 1.0;
}
elsif (\$x <= 0.0)
{
return 0.0;
}
else
{
return \$x;
}
}

sub gepCL2A
{
(my \$x, my \$y) = @_ ;
if (\$x > 0.0 && \$y > 0.0)
{
return 1.0;
}
else
{
return -1.0;
}
}

sub gepCL2B
{
(my \$x, my \$y) = @_ ;
if (\$x >= 0.0 && \$y < 0.0)
{
return -1.0;
}
else
{
return 1.0;
}
}

sub gepCL2C
{
(my \$x, my \$y) = @_ ;
if (\$x > 1.0 && \$y < -1.0)
{
return -1.0;
}
else
{
return 1.0;
}
}

sub gepCL2D
{
(my \$x, my \$y) = @_ ;
if (\$x > 0.0 && \$y > 0.0)
{
return 1.0;
}
else
{
return 0.0;
}
}

sub gepCL2E
{
(my \$x, my \$y) = @_ ;
if (\$x >= 0.0 && \$y <= 0.0)
{
return 0.0;
}
else
{
return 1.0;
}
}

sub gepCL2F
{
(my \$x, my \$y) = @_ ;
if (\$x > 1.0 && \$y < -1.0)
{
return 0.0;
}
else
{
return 1.0;
}
}

sub gepCL3A
{
(my \$x, my \$y) = @_ ;
if (\$x > 0.0 && \$y < 0.0)
{
return 1.0;
}
elsif (\$x < 0.0 && \$y > 0.0)
{
return -1.0;
}
else
{
return 0.0;
}
}

sub gepCL3B
{
(my \$x, my \$y) = @_ ;
if (\$x >= 1.0 && \$y >= 1.0)
{
return 1.0;
}
elsif (\$x <= -1.0 && \$y <= -1.0)
{
return -1.0;
}
else
{
return 0.0;
}
}

sub gepCL3C
{
(my \$x, my \$y) = @_ ;
if (\$x > 0.0 && \$y > 0.0)
{
return 1.0;
}
elsif (\$x < 0.0 && \$y < 0.0)
{
return -1.0;
}
else
{
return 0.0;
}
}

sub gepMap3A
{
(my \$x, my \$y) = @_ ;
my \$SLACK = 10.0;
my \$outVal = 0.0;
if (\$y < (\$x - \$SLACK))
{
\$outVal = -1.0;
}
elsif (\$y > (\$x + \$SLACK))
{
\$outVal = 1.0;
}
return \$outVal;
}

sub gepMap3B
{
(my \$x, my \$y, my \$z) = @_ ;
# evaluate minValue(x,y) and maxValue(x,y)
my \$minValue = \$x;
my \$maxValue = \$y;
if (\$minValue > \$y)
{
\$minValue = \$y;
\$maxValue = \$x;
}

my \$outVal = 0.0;
if (\$z < \$minValue)
{
\$outVal = -1.0;
}
elsif (\$z > \$maxValue)
{
\$outVal = 1.0;
}
return \$outVal;
}

sub gepMap3C
{
(my \$a, my \$b, my \$c, my \$d) = @_ ;
# evaluate minValue(a,b,c) and maxValue(a,b,c)
#
# evaluate minValue(a,b,c)
my \$minValue = \$a;
if (\$minValue > \$b)
{
\$minValue = \$b;
}
if (\$minValue > \$c)
{
\$minValue = \$c;
}
# evaluate maxValue(a,b,c)
my \$maxValue = \$a;
if (\$maxValue < \$b)
{
\$maxValue = \$b;
}
if (\$maxValue < \$c)
{
\$maxValue = \$c;
}

my \$outVal = 0.0;
if (\$d < \$minValue)
{
\$outVal = -1.0;
}
elsif (\$d > \$maxValue)
{
\$outVal = 1.0;
}
return \$outVal;
}

sub gepMap4A
{
(my \$x, my \$y) = @_ ;
my \$SLACK = 10.0;
my \$outVal = 0.0;
if (\$y < (\$x - \$SLACK))
{
\$outVal = 0.0;
}
elsif (\$y >= (\$x - \$SLACK) && \$y < \$x)
{
\$outVal = 1.0;
}
elsif (\$y >= \$x && \$y < (\$x + \$SLACK))
{
\$outVal = 2.0;
}
elsif (\$y >= (\$x + \$SLACK))
{
\$outVal = 3.0;
}
return \$outVal;
}

sub gepMap4B
{
(my \$x, my \$y, my \$z) = @_ ;
# evaluate minValue(x,y), maxValue(x,y) and midrange
my \$minValue = \$x;
my \$maxValue = \$y;
if (\$minValue > \$y)
{
\$minValue = \$y;
\$maxValue = \$x;
}
my \$midrange = (\$maxValue + \$minValue)/2.0;

my \$outVal = 0.0;
if (\$z < \$minValue)
{
\$outVal = 0.0;
}
elsif (\$z >= \$minValue && \$z < \$midrange)
{
\$outVal = 1.0;
}
elsif (\$z >= \$midrange && \$z < \$maxValue)
{
\$outVal = 2.0;
}
elsif (\$z >= \$maxValue)
{
\$outVal = 3.0;
}
return \$outVal;
}

sub gepMap4C
{
(my \$a, my \$b, my \$c, my \$d) = @_ ;
# evaluate minValue(a,b,c), maxValue(a,b,c) and midleValue(a,b,c)
#
# evaluate minValue(a,b,c)
my \$minValue = \$a;
my \$argMin = 0;
if (\$minValue > \$b)
{
\$minValue = \$b;
\$argMin = 1;
}
if (\$minValue > \$c)
{
\$minValue = \$c;
\$argMin = 2;
}
# evaluate maxValue(a,b,c)
my \$maxValue = \$a;
my \$argMax = 0;
if (\$maxValue < \$b)
{
\$maxValue = \$b;
\$argMax = 1;
}
if (\$maxValue < \$c)
{
\$maxValue = \$c;
\$argMax = 2;
}
# evaluate midleValue(a,b,c)
my \$midleValue = \$c;
if (0 != \$argMin && 0 != \$argMax)
{
\$midleValue = \$a;
}
if (1 != \$argMin && 1 != \$argMax)
{
\$midleValue = \$b;
}

my \$outVal = 0.0;
if (\$d < \$minValue)
{
\$outVal = 0.0;
}
elsif (\$d >= \$minValue && \$d < \$midleValue)
{
\$outVal = 1.0;
}
elsif (\$d >= \$midleValue && \$d < \$maxValue)
{
\$outVal = 2.0;
}
elsif (\$d >= \$maxValue)
{
\$outVal = 3.0;
}
return \$outVal;
}

sub gepMap5A
{
(my \$x, my \$y) = @_ ;
my \$SLACK = 15.0;
my \$outVal = 0.0;
if (\$y < (\$x - \$SLACK))
{
\$outVal = 0.0;
}
elsif (\$y >= (\$x - \$SLACK) && \$y < (\$x - \$SLACK/3.0))
{
\$outVal = 1.0;
}
elsif (\$y >= (\$x - \$SLACK/3.0) && \$y < (\$x + \$SLACK/3.0))
{
\$outVal = 2.0;
}
elsif (\$y >= (\$x + \$SLACK/3.0) && \$y < (\$x + \$SLACK))
{
\$outVal = 3.0;
}
elsif (\$y >= (\$x + \$SLACK))
{
\$outVal = 4.0;
}
return \$outVal;
}

sub gepMap5B
{
(my \$x, my \$y, my \$z) = @_ ;
# evaluate minValue(x,y), maxValue(x,y), midpoint1, midpoint2
my \$minValue = \$x;
my \$maxValue = \$y;
if (\$minValue > \$y)
{
\$minValue = \$y;
\$maxValue = \$x;
}
my \$intervalLength = (\$maxValue - \$minValue)/3.0;
my \$midpoint1 = \$minValue + \$intervalLength;
my \$midpoint2 = \$minValue + 2.0*\$intervalLength;

my \$outVal = 0.0;
if (\$z < \$minValue)
{
\$outVal = 0.0;
}
elsif (\$z >= \$minValue && \$z < \$midpoint1)
{
\$outVal = 1.0;
}
elsif (\$z >= \$midpoint1 && \$z < \$midpoint2)
{
\$outVal = 2.0;
}
elsif (\$z >= \$midpoint2 && \$z < \$maxValue)
{
\$outVal = 3.0;
}
elsif (\$z >= \$maxValue)
{
\$outVal = 4.0;
}
return \$outVal;
}

sub gepMap5C
{
(my \$a, my \$b, my \$c, my \$d) = @_ ;
# evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
#
# evaluate minValue(a,b,c)
my \$minValue = \$a;
my \$argMin = 0;
if (\$minValue > \$b)
{
\$minValue = \$b;
\$argMin = 1;
}
if (\$minValue > \$c)
{
\$minValue = \$c;
\$argMin = 2;
}
# evaluate maxValue(a,b,c)
my \$maxValue = \$a;
my \$argMax = 0;
if (\$maxValue < \$b)
{
\$maxValue = \$b;
\$argMax = 1;
}
if (\$maxValue < \$c)
{
\$maxValue = \$c;
\$argMax = 2;
}
# evaluate midleValue(a,b,c)
my \$midleValue = \$c;
if (0 != \$argMin && 0 != \$argMax)
{
\$midleValue = \$a;
}
if (1 != \$argMin && 1 != \$argMax)
{
\$midleValue = \$b;
}
# evaluate midrange1 and midrange2
my \$midrange1 = (\$minValue + \$midleValue)/2.0;
my \$midrange2 = (\$midleValue + \$maxValue)/2.0;

my \$outVal = 0.0;
if (\$d < \$minValue)
{
\$outVal = 0.0;
}
elsif (\$d >= \$minValue && \$d < \$midrange1)
{
\$outVal = 1.0;
}
elsif (\$d >= \$midrange1 && \$d < \$midrange2)
{
\$outVal = 2.0;
}
elsif (\$d >= \$midrange2 && \$d < \$maxValue)
{
\$outVal = 3.0;
}
elsif (\$d >= \$maxValue)
{
\$outVal = 4.0;
}
return \$outVal;
}

sub gepMap6A
{
(my \$x, my \$y) = @_ ;
my \$SLACK = 10.0;
my \$outVal = 0.0;
if (\$y < (\$x - \$SLACK))
{
\$outVal = 0.0;
}
elsif (\$y >= (\$x - \$SLACK) && \$y < (\$x - \$SLACK/2.0))
{
\$outVal = 1.0;
}
elsif (\$y >= (\$x - \$SLACK/2.0) && \$y < \$x)
{
\$outVal = 2.0;
}
elsif (\$y >= \$x && \$y < (\$x + \$SLACK/2.0))
{
\$outVal = 3.0;
}
elsif (\$y >= (\$x + \$SLACK/2.0) && \$y < (\$x + \$SLACK))
{
\$outVal = 4.0;
}
elsif (\$y >= (\$x + \$SLACK))
{
\$outVal = 5.0;
}
return \$outVal;
}

sub gepMap6B
{
(my \$x, my \$y, my \$z) = @_ ;
# evaluate minValue(x,y), maxValue(x,y), midrange, midpoint1, midpoint2
my \$minValue = \$x;
my \$maxValue = \$y;
if (\$minValue > \$y)
{
\$minValue = \$y;
\$maxValue = \$x;
}
my \$midrange = (\$minValue + \$maxValue)/2.0;
my \$midpoint1 = (\$minValue + \$midrange)/2.0;
my \$midpoint2 = (\$midrange + \$maxValue)/2.0;

my \$outVal = 0.0;
if (\$z < \$minValue)
{
\$outVal = 0.0;
}
elsif (\$z >= \$minValue && \$z < \$midpoint1)
{
\$outVal = 1.0;
}
elsif (\$z >= \$midpoint1 && \$z < \$midrange)
{
\$outVal = 2.0;
}
elsif (\$z >= \$midrange && \$z < \$midpoint2)
{
\$outVal = 3.0;
}
elsif (\$z >= \$midpoint2 && \$z < \$maxValue)
{
\$outVal = 4.0;
}
elsif (\$z >= \$maxValue)
{
\$outVal = 5.0;
}
return \$outVal;
}

sub gepMap6C
{
(my \$a, my \$b, my \$c, my \$d) = @_ ;
# evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
#
# evaluate minValue(a,b,c)
my \$minValue = \$a;
my \$argMin = 0;
if (\$minValue > \$b)
{
\$minValue = \$b;
\$argMin = 1;
}
if (\$minValue > \$c)
{
\$minValue = \$c;
\$argMin = 2;
}
# evaluate maxValue(a,b,c)
my \$maxValue = \$a;
my \$argMax = 0;
if (\$maxValue < \$b)
{
\$maxValue = \$b;
\$argMax = 1;
}
if (\$maxValue < \$c)
{
\$maxValue = \$c;
\$argMax = 2;
}
# evaluate midleValue(a,b,c)
my \$midleValue = \$c;
if (0 != \$argMin && 0 != \$argMax)
{
\$midleValue = \$a;
}
if (1 != \$argMin && 1 != \$argMax)
{
\$midleValue = \$b;
}
# evaluate midrange1 and midrange2
my \$midrange1 = (\$minValue + \$midleValue)/2.0;
my \$midrange2 = (\$midleValue + \$maxValue)/2.0;

my \$outVal = 0.0;
if (\$d < \$minValue)
{
\$outVal = 0.0;
}
elsif (\$d >= \$minValue && \$d < \$midrange1)
{
\$outVal = 1.0;
}
elsif (\$d >= \$midrange1 && \$d < \$midleValue)
{
\$outVal = 2.0;
}
elsif (\$d >= \$midleValue && \$d < \$midrange2)
{
\$outVal = 3.0;
}
elsif (\$d >= \$midrange2 && \$d < \$maxValue)
{
\$outVal = 4.0;
}
elsif (\$d >= \$maxValue)
{
\$outVal = 5.0;
}
return \$outVal;
}

sub gepECL3A
{
(my \$x, my \$y, my \$z) = @_ ;
if (\$y > \$x && \$z < \$x)
{
return 1.0;
}
elsif (\$y < \$x && \$z > \$x)
{
return -1.0;
}
else
{
return 0.0;
}
}

sub gepECL3B
{
(my \$x, my \$y, my \$z) = @_ ;
if (\$y > \$x && \$z > \$x)
{
return 1.0;
}
elsif (\$y < \$x && \$z < \$x)
{
return -1.0;
}
else
{
return 0.0;
}
}

sub gepECL3C
{
(my \$x, my \$y, my \$z) = @_ ;
if (\$y >= \$x && \$z >= \$x)
{
return 1.0;
}
elsif (\$y <= -\$x && \$z <= -\$x)
{
return -1.0;
}
else
{
return 0.0;
}
}

sub gepECL3D
{
(my \$a, my \$b, my \$c, my \$d) = @_ ;
# evaluate minValue(a,b) and maxValue(a,b)
my \$minValue = \$a;
my \$maxValue = \$b;
if (\$minValue > \$b)
{
\$minValue = \$b;
\$maxValue = \$a;
}

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

sub gepAMin2
{
(my \$x, my \$y) = @_ ;
if (\$x < \$y)
{
return 0.0;
}
else
{
return 1.0;
}
}

sub gepAMin3
{
(my \$x, my \$y, my \$z) = @_ ;
my \$temp = \$x;
my \$argMin = 0.0;
if (\$temp >= \$y)
{
\$temp = \$y;
\$argMin = 1.0;
}
if (\$temp >= \$z)
{
\$argMin = 2.0;
}
return \$argMin;
}

sub gepAMin4
{
(my \$a, my \$b, my \$c, my \$d) = @_ ;
my \$temp = \$a;
my \$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;
}

sub gepAMax2
{
(my \$x, my \$y) = @_ ;
if (\$x >= \$y)
{
return 0.0;
}
else
{
return 1.0;
}
}

sub gepAMax3
{
(my \$x, my \$y, my \$z) = @_ ;
my \$temp = \$x;
my \$argMax = 0.0;
if (\$temp < \$y)
{
\$temp = \$y;
\$argMax = 1.0;
}
if (\$temp < \$z)
{
\$argMax = 2.0;
}
return \$argMax;
}

sub gepAMax4
{
(my \$a, my \$b, my \$c, my \$d) = @_ ;
my \$temp = \$a;
my \$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