# New Math Functions in Octave

Posted

As far as the new 39 math functions go, the Octave code is very similar to the MATLAB code, so it makes sense to use the MATLAB Grammar as our starting point.

Besides replacing "end" with "endif" and adding "endfunction" to mark the end of functions, we just have to remove the comma that is used after "if" and "elseif" expressions and also indent the code.

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

function result = gepRamp1(x)
if (x > 0.0)
result = x;
else
result = 0.0;
endif
endfunction

function result = gepRamp2(x)
if (x > 0.0)
result = 0.0;
else
result = x;
endif
endfunction

function result = gepRamp3(x)
if (x > 0.0)
result = 0.0;
else
result = -x;
endif
endfunction

function result = gepRamp4(x)
if (x > 0.0)
result = -x;
else
result = 0.0;
endif
endfunction

function result = gepStep1(x)
if (x > 0.0)
result = 1.0;
else
result = -1.0;
endif
endfunction

function result = gepStep2(x)
if (x > 0.0)
result = 1.0;
else
result = 0.0;
endif
endfunction

function result = gepStep3(x)
if (x >= 1.0)
result = 1.0;
elseif (x <= -1.0)
result = -1.0;
else
result = x;
endif
endfunction

function result = gepStep4(x)
if (x >= 1.0)
result = 1.0;
elseif (x <= 0.0)
result = 0.0;
else
result = x;
endif
endfunction

function result = gepCL2A(x, y)
if (x > 0.0 && y > 0.0)
result = 1.0;
else
result = -1.0;
endif
endfunction

function result = gepCL2B(x, y)
if (x >= 0.0 && y < 0.0)
result = -1.0;
else
result = 1.0;
endif
endfunction

function result = gepCL2C(x, y)
if (x > 1.0 && y < -1.0)
result = -1.0;
else
result = 1.0;
endif
endfunction

function result = gepCL2D(x, y)
if (x > 0.0 && y > 0.0)
result = 1.0;
else
result = 0.0;
endif
endfunction

function result = gepCL2E(x, y)
if (x >= 0.0 && y <= 0.0)
result = 0.0;
else
result = 1.0;
endif
endfunction

function result = gepCL2F(x, y)
if (x > 1.0 && y < -1.0)
result = 0.0;
else
result = 1.0;
endif
endfunction

function result = gepCL3A(x, y)
if (x > 0.0 && y < 0.0)
result = 1.0;
elseif (x < 0.0 && y > 0.0)
result = -1.0;
else
result = 0.0;
endif
endfunction

function result = gepCL3B(x, y)
if (x >= 1.0 && y >= 1.0)
result = 1.0;
elseif (x <= -1.0 && y <= -1.0)
result = -1.0;
else
result = 0.0;
endif
endfunction

function result = gepCL3C(x, y)
if (x > 0.0 && y > 0.0)
result = 1.0;
elseif (x < 0.0 && y < 0.0)
result = -1.0;
else
result = 0.0;
endif
endfunction

function result = gepMap3A(x, y)
SLACK = 10.0;
if (y < (x - SLACK))
result = -1.0;
elseif (y > (x + SLACK))
result = 1.0;
else
result = 0.0;
endif
endfunction

function result = gepMap3B(x, y, z)
minValue = min(x,y);
maxValue = max(x,y);
if (z < minValue)
result = -1.0;
elseif (z > maxValue)
result = 1.0;
else
result = 0.0;
endif
endfunction

function result = gepMap3C(a, b, c, d)
minValue = min(min(a,b),c);
maxValue = max(max(a,b),c);
if (d < minValue)
result = -1.0;
elseif (d > maxValue)
result = 1.0;
else
result = 0.0;
endif
endfunction

function result = gepMap4A(x, y)
SLACK = 10.0;
if (y < (x - SLACK))
result = 0.0;
elseif (y >= (x - SLACK) && y < x)
result = 1.0;
elseif (y >= x && y < (x + SLACK))
result = 2.0;
elseif (y >= (x + SLACK))
result = 3.0;
endif
endfunction

function result = gepMap4B(x, y, z)
% evaluate minValue(x,y), maxValue(x,y) and midrange
minValue = min(x,y);
maxValue = max(x,y);
midrange = (maxValue + minValue)/2.0;

if (z < minValue)
result = 0.0;
elseif (z >= minValue && z < midrange)
result = 1.0;
elseif (z >= midrange && z < maxValue)
result = 2.0;
elseif (z >= maxValue)
result = 3.0;
endif
endfunction

function result = gepMap4C(a, b, c, d)
% evaluate minValue(a,b,c), maxValue(a,b,c) and midleValue(a,b,c)
%
% evaluate minValue(a,b,c) and argMin(a,b,c)
minValue = a;
argMin = 0;
if (minValue > b)
minValue = b;
argMin = 1;
endif
if (minValue > c)
minValue = c;
argMin = 2;
endif
% evaluate maxValue(a,b,c) and argMax(a,b,c)
maxValue = a;
argMax = 0;
if (maxValue < b)
maxValue = b;
argMax = 1;
endif
if (maxValue < c)
maxValue = c;
argMax = 2;
endif
% evaluate midleValue(a,b,c)
midleValue = c;
if (0 ~= argMin && 0 ~= argMax)
midleValue = a;
endif
if (1 ~= argMin && 1 ~= argMax)
midleValue = b;
endif

if (d < minValue)
result = 0.0;
elseif (d >= minValue && d < midleValue)
result = 1.0;
elseif (d >= midleValue && d < maxValue)
result = 2.0;
elseif (d >= maxValue)
result = 3.0;
endif
endfunction

function result = gepMap5A(x, y)
SLACK = 15.0;
if (y < (x - SLACK))
result = 0.0;
elseif (y >= (x - SLACK) && y < (x - SLACK/3.0))
result = 1.0;
elseif (y >= (x - SLACK/3.0) && y < (x + SLACK/3.0))
result = 2.0;
elseif (y >= (x + SLACK/3.0) && y < (x + SLACK))
result = 3.0;
elseif (y >= (x + SLACK))
result = 4.0;
endif
endfunction

function result = gepMap5B(x, y, z)
% evaluate minValue(x,y), maxValue(x,y), midpoint1, midpoint2
minValue = min(x,y);
maxValue = max(x,y);
intervalLength = (maxValue - minValue)/3.0;
midpoint1 = minValue + intervalLength;
midpoint2 = minValue + 2.0*intervalLength;

if (z < minValue)
result = 0.0;
elseif (z >= minValue && z < midpoint1)
result = 1.0;
elseif (z >= midpoint1 && z < midpoint2)
result = 2.0;
elseif (z >= midpoint2 && z < maxValue)
result = 3.0;
elseif (z >= maxValue)
result = 4.0;
endif
endfunction

function result = gepMap5C(a, b, c, d)
% evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
%
% evaluate minValue(a,b,c) and argMin(a,b,c)
minValue = a;
argMin = 0;
if (minValue > b)
minValue = b;
argMin = 1;
endif
if (minValue > c)
minValue = c;
argMin = 2;
endif
% evaluate maxValue(a,b,c) and argMax(a,b,c)
maxValue = a;
argMax = 0;
if (maxValue < b)
maxValue = b;
argMax = 1;
endif
if (maxValue < c)
maxValue = c;
argMax = 2;
endif
% evaluate midleValue(a,b,c)
midleValue = c;
if (0 ~= argMin && 0 ~= argMax)
midleValue = a;
endif
if (1 ~= argMin && 1 ~= argMax)
midleValue = b;
endif
midrange1 = (minValue + midleValue)/2.0;
midrange2 = (midleValue + maxValue)/2.0;

if (d < minValue)
result = 0.0;
elseif (d >= minValue && d < midrange1)
result = 1.0;
elseif (d >= midrange1 && d < midrange2)
result = 2.0;
elseif (d >= midrange2 && d < maxValue)
result = 3.0;
elseif (d >= maxValue)
result = 4.0;
endif
endfunction

function result = gepMap6A(x, y)
SLACK = 10.0;
if (y < (x - SLACK))
result = 0.0;
elseif (y >= (x - SLACK) && y < (x - SLACK/2.0))
result = 1.0;
elseif (y >= (x - SLACK/2.0) && y < x)
result = 2.0;
elseif (y >= x && y < (x + SLACK/2.0))
result = 3.0;
elseif (y >= (x + SLACK/2.0) && y < (x + SLACK))
result = 4.0;
elseif (y >= (x + SLACK))
result = 5.0;
endif
endfunction

function result = gepMap6B(x, y, z)
minValue = min(x,y);
maxValue = max(x,y);
midrange = (minValue + maxValue)/2.0;
midpoint1 = (minValue + midrange)/2.0;
midpoint2 = (midrange + maxValue)/2.0;

if (z < minValue)
result = 0.0;
elseif (z >= minValue && z < midpoint1)
result = 1.0;
elseif (z >= midpoint1 && z < midrange)
result = 2.0;
elseif (z >= midrange && z < midpoint2)
result = 3.0;
elseif (z >= midpoint2 && z < maxValue)
result = 4.0;
elseif (z >= maxValue)
result = 5.0;
endif
endfunction

function result = gepMap6C(a, b, c, d)
% evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
%
% evaluate minValue(a,b,c) and argMin(a,b,c)
minValue = a;
argMin = 0;
if (minValue > b)
minValue = b;
argMin = 1;
endif
if (minValue > c)
minValue = c;
argMin = 2;
endif
% evaluate maxValue(a,b,c) and argMax(a,b,c)
maxValue = a;
argMax = 0;
if (maxValue < b)
maxValue = b;
argMax = 1;
endif
if (maxValue < c)
maxValue = c;
argMax = 2;
endif
% evaluate midleValue(a,b,c)
midleValue = c;
if (0 ~= argMin && 0 ~= argMax)
midleValue = a;
endif
if (1 ~= argMin && 1 ~= argMax)
midleValue = b;
endif
% evaluate midrange1 and midrange2
midrange1 = (minValue + midleValue)/2.0;
midrange2 = (midleValue + maxValue)/2.0;

if (d < minValue)
result = 0.0;
elseif (d >= minValue && d < midrange1)
result = 1.0;
elseif (d >= midrange1 && d < midleValue)
result = 2.0;
elseif (d >= midleValue && d < midrange2)
result = 3.0;
elseif (d >= midrange2 && d < maxValue)
result = 4.0;
elseif (d >= maxValue)
result = 5.0;
endif
endfunction

function result = gepECL3A(x, y, z)
if (y > x && z < x)
result = 1.0;
elseif (y < x && z > x)
result = -1.0;
else
result = 0.0;
endif
endfunction

function result = gepECL3B(x, y, z)
if (y > x && z > x)
result = 1.0;
elseif (y < x && z < x)
result = -1.0;
else
result = 0.0;
endif
endfunction

function result = gepECL3C(x, y, z)
if (y >= x && z >= x)
result = 1.0;
elseif (y <= -x && z <= -x)
result = -1.0;
else
result = 0.0;
endif
endfunction

function result = gepECL3D(a, b, c, d)
minValue = min(a,b);
maxValue = max(a,b);
if (c >= maxValue && d >= maxValue)
result = 1.0;
elseif (c <= minValue && d <= minValue)
result = -1.0;
else
result = 0.0;
endif
endfunction

function result = gepAMin2(x, y)
if (x < y)
result = 0.0;
else
result = 1.0;
endif
endfunction

function result = gepAMin3(x, y, z)
temp = x;
argMin = 0.0;
if (temp >= y)
temp = y;
argMin = 1.0;
endif
if (temp >= z)
argMin = 2.0;
endif
result = argMin;
endfunction

function result = gepAMin4(a, b, c, d)
temp = a;
argMin = 0.0;
if (temp >= b)
temp = b;
argMin = 1.0;
endif
if (temp >= c)
temp = c;
argMin = 2.0;
endif
if (temp >= d)
argMin = 3.0;
endif
result = argMin;
endfunction

function result = gepAMax2(x, y)
if (x >= y)
result = 0.0;
else
result = 1.0;
endif
endfunction

function result = gepAMax3(x, y, z)
temp = x;
argMax = 0.0;
if (temp < y)
temp = y;
argMax = 1.0;
endif
if (temp < z)
argMax = 2.0;
endif
result = argMax;
endfunction

function result = gepAMax4(a, b, c, d)
temp = a;
argMax = 0.0;
if (temp < b)
temp = b;
argMax = 1.0;
endif
if (temp < c)
temp = c;
argMax = 2.0;
endif
if (temp < d)
argMax = 3.0;
endif
result = argMax;
endfunction

Author