Tropical algebra overloading: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) 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. */ |
||
x= 2 ; y= -2 ; say is(@x) LS(x) RS(y) $Mul(x,y) |
|||
x= -0.001 ; y= nInf ; say is(@a) LS(x) RS(y) $Add(x,y) |
|||
x= 0 ; y= nInf ; say is(@x) LS(x) RS(y) $Mul(x,y) |
|||
x= 1.5 ; y= -1 ; say is(@a) LS(x) RS(y) $Add(x,y) |
|||
x= -0.5 ; y= 0 ; say is(@x) LS(x) RS(y) $Mul(x,y) |
|||
x= 5 ; y= 7 ; say is(@h) LS(x) RS(y) $Exp(x,y) |
|||
x= 5 ; y= $Add(8,7); say is(@e) LS(x @x) RS(@a"(8,7)") $Mul(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= 5 ; y= $Add(8,7); blanks= left('', 26) |
|||
a= $Mul(5,8); b= $Mul(5,7); say is(@c) LS(x @x) @a"(8,7)" ' compared to' |
|||
say blanks LS(@x"(5,8)") RS(@a @x'(5,7)') , |
|||
$ToF( $Mul(x,y) == $Add(a,b) ) |
|||
⚫ | |||
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==_ |
||
⚫ | |||
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": -∞ */ |
||
⚫ | |||
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*/ |
||
$Add: procedure; parse arg a,b; return max(isRing(a),isRing(b)) /*simulate max add ƒ */ |
|||
$ToF: procedure; parse arg ?; return word('false true',1+?) /*return true │ false.*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
$Exp: procedure; parse arg a,b; if ABnInf() then return _ /*return the "diagraph": -∞ */ |
|||
return |
return isRing(a) * isRing(b) /*simulate exponentiation ƒ */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
$Mul: procedure; parse arg a,b; if ABnInf() then return _ /*return the "diagraph": -∞ */ |
|||
return isRing(a) + isRing(b) /*simulate multiplication ƒ */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
isNum: procedure; parse arg a,b; if ABnInf() then a= negInf() /*replace A with -∞? */ |
|||
return |
return datatype(a, 'Num') /*Arg numeric? Return 1 or 0*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
isRing: procedure; parse arg a,b; if ABnInf() then return negInf /*return -∞ */ |
|||
if isNum(a) | a==negInf() then return a; call notNum a /*show error.*/</lang> |
|||
{{out|output|text= when using the internal default input:}} |
{{out|output|text= when using the internal default input:}} |
||
<pre> |
<pre> |
||
max tropical (x) of 2 with -2 |
max tropical (x) of 2 with -2 ───► 0 |
||
max tropical (+) of -0.001 with -∞ |
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 |
max tropical (+) of 1.5 with -1 ───► 1.5 |
||
max tropical (x) of -0.5 with 0 |
max tropical (x) of -0.5 with 0 ───► -0.5 |
||
max tropical (^) of 5 with 7 |
max tropical (^) of 5 with 7 ───► 35 |
||
max tropical expression of 5 (x) |
max tropical expression of 5 (x) with (+)(8,7) ───► 13 |
||
max tropical expression of |
max tropical expression of (x)(5,8) (+) with (x)(5,7) ───► 13 |
||
max tropical comparison of |
max tropical comparison of 5 (x) with (+)(8,7) compared to |
||
⚫ | |||
</pre> |
</pre> |
||