Tropical algebra overloading: Difference between revisions

→‎{{header|REXX}}: added the computer programming language REXX.
(C++ implementation)
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 599:
5 %*% 8 %+% 5 %*% 7 == 13
5 %*% 8 %+% 5 %*% 7 == 5 %*% (8 %+% 7)) TRUE
</pre>
 
=={{header|REXX}}==
<lang rexx>/*REXX pgm demonstrates max tropical semi─ring with overloading: topAdd, topMul, topExp.*/
numeric digits 1000 /*be able to handle negative infinity. */
call negInf /*assign value of -∞ to a variable.*/
@comp= ' compared to '; @with= ' with '; @yields= ' ───► '
x= 2; y= -2
say 'max tropical (x) of ' x @with y @yields topMul(x, y)
x= -0.001; y= nInf
say 'max tropical (+) of ' x @with y @yields topAdd(x, y)
x= 0; y= nInf
say 'max tropical (x) of ' x @with y @yields topMul(x, y)
x= 1.5; y= -1
say 'max tropical (+) of ' x @with y @yields topAdd(x, y)
x= -0.5; y= 0
say 'max tropical (x) of' x @with y @yields topMul(x, y)
x= 5; y= 7
say 'max tropical (^) of ' x @with y @yields topExp(x, y)
x= 5; y= topAdd(8, 7)
say 'max tropical expression of ' x "(x) topAdd(8,7)" @yields topMul(x, y)
x= topMul(5, 8); y= topMul(5, 7)
say 'max tropical expression of ' "(x)(5,8) (+) (x)(5,7)" @yields topAdd(x, y)
x= 5; y= topAdd(8, 7)
a= topMul(5, 8); b= topMul(5, 7)
say 'max tropical comparison of ' x '(x)' @with "topAdd(8,7)" @comp ,
"(x)(5,8)" @with '(+) (x)(5,7)' @yields ,
topToF( topMul(x, y) == topAdd(a, b) )
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
ABnInf: if b=='' then b=a; __= negInf(); _= nInf(); return a==__ | a==_ | b==__ | b==_
notNum: call sayErr "argument isn't numeric or minus infinity:", arg(1) /*tell error.*/
negInf: negInf= '-1e' || (digits()-1); call nInf; return negInf /*simulate a -∞ value.*/
nInf: nInf= '-∞'; return nInf /*return the "diagraph": -∞ */
sayErr: say; say '***error***' arg(1) arg(2); say; exit 13 /*issue error message──►term*/
topAdd: procedure; parse arg a,b; return max(isRing(a),isRing(b)) /*simulate max add ƒ */
topToF: procedure; parse arg ?; return word('false true',1+?) /*return true | false.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
isNum: procedure; parse arg a; if ABnInf() then a= negInf() /*replace A with -∞?*/
return datatype(a, 'Num') /*is arg numeric? Ret 1 or 0*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
isRing: procedure; parse arg a; if ABnInf() then return negInf /*return -∞ */
if isNum(a) | a==negInf() then return a; call notNum a /*show error.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
topExp: procedure; parse arg a,b; if ABnInf() then return _ /*return the "diagraph": -∞ */
return isRing(a) * isRing(b) /*simulate exponentiation ƒ */
/*──────────────────────────────────────────────────────────────────────────────────────*/
topMul: procedure; parse arg a,b; if ABnInf() then return _ /*return the "diagraph": -∞ */
return isRing(a) + isRing(b) /*simulate multiplication ƒ */</lang>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
max tropical (x) of 2 with -2 ───► 0
max tropical (+) of -0.001 with -∞ ───► -0.001
max tropical (x) of 0 with -∞ ───► -∞
max tropical (+) of 1.5 with -1 ───► 1.5
max tropical (x) of -0.5 with 0 ───► -0.5
max tropical (^) of 5 with 7 ───► 35
max tropical expression of 5 (x) topAdd(8,7) ───► 13
max tropical expression of (x)(5,8) (+) (x)(5,7) ───► 13
max tropical comparison of 5 (x) with topAdd(8,7) compared to (x)(5,8) with (+) (x)(5,7) ───► true
</pre>