Tropical algebra overloading: Difference between revisions

→‎{{header|REXX}}: simplified the showing of expressions, aligned output
m (→‎{{header|Raku}}: add constraint)
(→‎{{header|REXX}}: simplified the showing of expressions, aligned output)
Line 631:
'''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.*/
call negInf; @x= '(x)'; @a= '(+)'; @h= '(^)'; @e= 'expression'; @c= 'comparison'
numeric digits 1000 /*be able to handle negative infinity. */
callx= negInf 2 ; y= -2 ; say is(@x) LS(x) /*assign valueRS(y) of -∞ to a variable.*/$Mul(x,y)
@compx= ' compared-0.001 to '; y= nInf ; say is(@a) LS(x) @with= ' with '; RS(y) @yields= ' ───► '$Add(x,y)
x= 0 ; y= nInf ; say is(@x) LS(x) x= 2; RS(y) $Mul(x,y= -2)
sayx= 'max tropical 1.5 (x) ; y= of-1 ' ; say is(@a) LS(x) @with RS(y) @yields topMul$Add(x, y)
x= -0.5 ; y= 0 ; say is(@x) LS(x) x= -0.001; RS(y) $Mul(x,y= nInf)
sayx= 'max tropical 5 (+) ; y= of ' 7 ; say is(@h) LS(x) @with RS(y) @yields topAdd$Exp(x, y)
x= 5 ; y= $Add(8,7); say is(@e) LS(x @x) RS(@a"(8,7)") $Mul(x= 0; ,y= nInf)
sayx= 'max$Mul(5,8); tropicaly= $Mul(5,7); say is(x@e) of ' LS(@x"(5,8)" @a) RS(@withx'(5,7)') y @yields topMul$Add(x, y)
x= 5 ; xy= 1.5$Add(8,7); blanks= left('', y= -126)
saya= 'max$Mul(5,8); tropicalb= $Mul(5,7); say is(+@c) LS(x of ' @x ) @witha"(8,7)" y ' compared @yields topAdd(x, y)to'
say blanks LS(@x= -0."(5; ,8)") RS(@a @x'(5,7)') y= 0,
say 'max tropical (x) of' x @with y @yields topMul $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. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
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": -∞ */
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*/
topAdd$Add: procedure; parse arg a,b; return max(isRing(a),isRing(b)) /*simulate max add ƒ */
topToF$ToF: procedure; parse arg ?; return word('false true',1+?) /*return true | false.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
isNum$Exp: procedure; parse arg a,b; if ABnInf() then a= negInf() return _ /*replacereturn the A with"diagraph": -∞? */
return datatypeisRing(a, 'Num') * isRing(b) /*is arg numeric? Retsimulate 1exponentiation orƒ 0*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
isRing$Mul: procedure; parse arg a,b; if ABnInf() then return negInf _ /*return the "diagraph": -∞ */
ifreturn isNumisRing(a) |+ a==negInfisRing(b) then return a; call notNum a /*showsimulate multiplication ƒ error.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
topExpisNum: procedure; parse arg a,b; if ABnInf() then returna= negInf() _ /*returnreplace the "diagraph":A with -∞? */
return isRingdatatype(a, 'Num') * isRing(b) /*simulateArg numeric? Return exponentiation1 ƒor 0*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
topMulisRing: procedure; parse arg a,b; if ABnInf() then return _negInf /*return the "diagraph": -∞ */
returnif isRingisNum(a) +| isRinga==negInf(b) then return a; call notNum a /*simulate multiplication ƒshow error.*/</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) with topAdd(+)(8,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 5 (x)(5,8) with (+) (x)(58,7) ───► compared trueto
(x)(5,8) with (+) (x= )(5;,7) ───► y= 7true
</pre>