Tropical algebra overloading: Difference between revisions

Content added Content deleted
m (→‎{{header|Raku}}: add constraint)
(→‎{{header|REXX}}: simplified the showing of expressions, aligned output)
Line 631: Line 631:
'''REXX'''   doesn't support operator overloading,   so functions are needed to be used instead.
'''REXX'''   doesn't support operator overloading,   so functions are needed to be used instead.
<lang rexx>/*REXX pgm demonstrates max tropical semi─ring with overloading: topAdd, topMul, topExp.*/
<lang rexx>/*REXX pgm demonstrates max tropical semi─ring with overloading: topAdd, topMul, topExp.*/
call negInf; @x= '(x)'; @a= '(+)'; @h= '(^)'; @e= 'expression'; @c= 'comparison'
numeric digits 1000 /*be able to handle negative infinity. */
numeric digits 1000 /*be able to handle negative infinity. */
call negInf /*assign value of -∞ to a variable.*/
x= 2 ; y= -2 ; say is(@x) LS(x) RS(y) $Mul(x,y)
@comp= ' compared to '; @with= ' with '; @yields= ' ───► '
x= -0.001 ; y= nInf ; say is(@a) LS(x) RS(y) $Add(x,y)
x= 2; y= -2
x= 0 ; y= nInf ; say is(@x) LS(x) RS(y) $Mul(x,y)
say 'max tropical (x) of ' x @with y @yields topMul(x, y)
x= 1.5 ; y= -1 ; say is(@a) LS(x) RS(y) $Add(x,y)
x= -0.001; y= nInf
x= -0.5 ; y= 0 ; say is(@x) LS(x) RS(y) $Mul(x,y)
say 'max tropical (+) of ' x @with y @yields topAdd(x, y)
x= 5 ; y= 7 ; say is(@h) LS(x) RS(y) $Exp(x,y)
x= 0; y= nInf
x= 5 ; y= $Add(8,7); say is(@e) LS(x @x) RS(@a"(8,7)") $Mul(x,y)
say 'max tropical (x) of ' x @with y @yields topMul(x, y)
x= $Mul(5,8); y= $Mul(5,7); say is(@e) LS(@x"(5,8)" @a) RS(@x'(5,7)') $Add(x,y)
x= 1.5; y= -1
x= 5 ; y= $Add(8,7); blanks= left('', 26)
say 'max tropical (+) of ' x @with y @yields topAdd(x, y)
a= $Mul(5,8); b= $Mul(5,7); say is(@c) LS(x @x) @a"(8,7)" ' compared to'
x= -0.5; y= 0
say blanks LS(@x"(5,8)") RS(@a @x'(5,7)') ,
say 'max tropical (x) of' x @with y @yields topMul(x, y)
$ToF( $Mul(x,y) == $Add(a,b) )
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. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
ABnInf: if b=='' then b=a; __= negInf(); _= nInf(); return a==__ | a==_ | b==__ | b==_
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.*/
negInf: negInf= '-1e' || (digits()-1); call nInf; return negInf /*simulate a -∞ value.*/
nInf: nInf= '-∞'; return nInf /*return the "diagraph": -∞ */
nInf: nInf= '-∞'; return nInf /*return the "diagraph": -∞ */
notNum: call sayErr "argument isn't numeric or minus infinity:", arg(1) /*tell error.*/
is: return 'max tropical' center(arg(1), 10) "of" /*center what is to be shown*/
LS: return right( arg(1), 12) ' with ' /*pad left─side of equation*/
RS: return left( arg(1), 12) ' ───► ' /* " right─side " " */
sayErr: say; say '***error***' arg(1) arg(2); say; exit 13 /*issue error message──►term*/
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 ƒ */
$Add: 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.*/
$ToF: procedure; parse arg ?; return word('false true',1+?) /*return true false.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
isNum: procedure; parse arg a; if ABnInf() then a= negInf() /*replace A with -∞?*/
$Exp: procedure; parse arg a,b; if ABnInf() then return _ /*return the "diagraph": -∞ */
return datatype(a, 'Num') /*is arg numeric? Ret 1 or 0*/
return isRing(a) * isRing(b) /*simulate exponentiation ƒ */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
isRing: procedure; parse arg a; if ABnInf() then return negInf /*return -∞ */
$Mul: procedure; parse arg a,b; if ABnInf() then return _ /*return the "diagraph": -∞ */
if isNum(a) | a==negInf() then return a; call notNum a /*show error.*/
return isRing(a) + isRing(b) /*simulate multiplication ƒ */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
topExp: procedure; parse arg a,b; if ABnInf() then return _ /*return the "diagraph": -∞ */
isNum: procedure; parse arg a,b; if ABnInf() then a= negInf() /*replace A with -∞? */
return isRing(a) * isRing(b) /*simulate exponentiation ƒ */
return datatype(a, 'Num') /*Arg numeric? Return 1 or 0*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
topMul: procedure; parse arg a,b; if ABnInf() then return _ /*return the "diagraph": -∞ */
isRing: procedure; parse arg a,b; if ABnInf() then return negInf /*return -∞ */
return isRing(a) + isRing(b) /*simulate multiplication ƒ */</lang>
if isNum(a) | a==negInf() then return a; call notNum a /*show error.*/</lang>
{{out|output|text=&nbsp; when using the internal default input:}}
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
<pre>
max tropical (x) of 2 with -2 ───► 0
max tropical (x) of 2 with -2 ───► 0
max tropical (+) of -0.001 with -∞ ───► -0.001
max tropical (+) of -0.001 with -∞ ───► -0.001
max tropical (x) of 0 with -∞ ───► -∞
max tropical (x) of 0 with -∞ ───► -∞
max tropical (+) of 1.5 with -1 ───► 1.5
max tropical (+) of 1.5 with -1 ───► 1.5
max tropical (x) of -0.5 with 0 ───► -0.5
max tropical (x) of -0.5 with 0 ───► -0.5
max tropical (^) of 5 with 7 ───► 35
max tropical (^) of 5 with 7 ───► 35
max tropical expression of 5 (x) topAdd(8,7) ───► 13
max tropical expression of 5 (x) with (+)(8,7) ───► 13
max tropical expression of (x)(5,8) (+) (x)(5,7) ───► 13
max tropical expression of (x)(5,8) (+) with (x)(5,7) ───► 13
max tropical comparison of 5 (x) with topAdd(8,7) compared to (x)(5,8) with (+) (x)(5,7) ───► true
max tropical comparison of 5 (x) with (+)(8,7) compared to
(x)(5,8) with (+) (x)(5,7) ───► true
</pre>
</pre>