Tropical algebra overloading: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(8 intermediate revisions by 3 users not shown)
Line 81:
5 ^ 7: 35.0
5 (X) ( 8 (+) 7 ) = 5 (X) 8 (+) 5 (X) 7 is TRUE
</pre>
 
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
 
public class Program
{
public static void Main(string[] args)
{
var a = new Tropical(-2);
var b = new Tropical(-1);
var c = new Tropical(-0.5);
var d = new Tropical(-0.001);
var e = new Tropical(0);
var f = new Tropical(1.5);
var g = new Tropical(2);
var h = new Tropical(5);
var i = new Tropical(7);
var j = new Tropical(8);
var k = new Tropical(); // Represents -Inf
 
Console.WriteLine("2 x -2 = " + g.Multiply(a));
Console.WriteLine("-0.001 + -Inf = " + d.Add(k));
Console.WriteLine("0 x -Inf = " + e.Multiply(k));
Console.WriteLine("1.5 + -1 = " + f.Add(b));
Console.WriteLine("-0.5 x 0 = " + c.Multiply(e));
 
Console.WriteLine();
Console.WriteLine("5^7 = " + h.Power(7));
 
Console.WriteLine();
Console.WriteLine("5 * ( 8 + 7 ) = " + h.Multiply(j.Add(i)));
Console.WriteLine("5 * 8 + 5 * 7 = " + h.Multiply(j).Add(h.Multiply(i)));
}
}
 
public class Tropical
{
private double? number;
 
public Tropical(double number)
{
this.number = number;
}
 
public Tropical()
{
this.number = null; // Represents -Inf
}
 
public override string ToString()
{
return number.HasValue ? ((int)number.Value).ToString() : "-Inf";
}
 
public Tropical Add(Tropical other)
{
if (!number.HasValue) return other;
if (!other.number.HasValue) return this;
 
return number > other.number ? this : other;
}
 
public Tropical Multiply(Tropical other)
{
if (number.HasValue && other.number.HasValue)
{
return new Tropical(number.Value + other.number.Value);
}
 
return new Tropical();
}
 
public Tropical Power(int exponent)
{
if (exponent <= 0)
{
throw new ArgumentException("Power must be positive", nameof(exponent));
}
 
Tropical result = this;
for (int i = 1; i < exponent; i++)
{
result = result.Multiply(this);
}
 
return result;
}
}
</syntaxhighlight>
{{out}}
<pre>
2 x -2 = 0
-0.001 + -Inf = 0
0 x -Inf = -Inf
1.5 + -1 = 1
-0.5 x 0 = 0
 
5^7 = 35
 
5 * ( 8 + 7 ) = 13
5 * 8 + 5 * 7 = 13
 
</pre>
 
Line 499 ⟶ 605:
Expecting 5 ⊗ (8 ⊕ 7) == 13. Got 13.0 ✔
Expecting 5 ⊗ 8 ⊕ 5 ⊗ 7 == 13. Got 13.0 ✔
</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.Optional;
 
public final class TropicalAlgebra {
 
public static void main(String[] aArgs) {
final Tropical a = new Tropical(-2);
final Tropical b = new Tropical(-1);
final Tropical c = new Tropical(-0.5);
final Tropical d = new Tropical(-0.001);
final Tropical e = new Tropical(0);
final Tropical f = new Tropical(1.5);
final Tropical g = new Tropical(2);
final Tropical h = new Tropical(5);
final Tropical i = new Tropical(7);
final Tropical j = new Tropical(8);
final Tropical k = new Tropical();
System.out.println("2 x -2 = " + g.multiply(a));
System.out.println("-0.001 + -Inf = " + d.add(k));
System.out.println("0 x -Inf = " + e.multiply(k));
System.out.println("1.5 + -1 = " + f.add(b));
System.out.println("-0.5 x 0 = " + c.multiply(e));
System.out.println();
System.out.println("5^7 = " + h.power(7));
System.out.println();
System.out.println("5 * ( 8 + 7 ) = " + h.multiply(j.add(i)));
System.out.println("5 * 8 + 5 * 7 = " + h.multiply(j).add(h.multiply(i)));
}
 
}
 
final class Tropical {
public Tropical(Number aNumber) {
if ( aNumber == null ) {
throw new IllegalArgumentException("Number cannot be null");
}
optional = Optional.of(aNumber);
}
public Tropical() {
optional = Optional.empty();
}
@Override
public String toString() {
if ( optional.isEmpty() ) {
return "-Inf";
}
String value = String.valueOf(optional.get());
final int index = value.indexOf(".");
if ( index >= 0 ) {
value = value.substring(0, index);
}
return value;
}
public Tropical add(Tropical aOther) {
if ( aOther.optional.isEmpty() ) {
return this;
}
if ( optional.isEmpty() ) {
return aOther;
}
if ( optional.get().doubleValue() > aOther.optional.get().doubleValue() ) {
return this;
}
return aOther;
}
public Tropical multiply(Tropical aOther) {
if ( optional.isPresent() && aOther.optional.isPresent() ) {
double result = optional.get().doubleValue() + aOther.optional.get().doubleValue();
return new Tropical(result);
}
return new Tropical();
}
public Tropical power(int aExponent) {
if ( aExponent <= 0 ) {
throw new IllegalArgumentException("Power must be positive");
}
Tropical result = this;;
for ( int i = 1; i < aExponent; i++ ) {
result = result.multiply(this);
}
return result;
}
private Optional<Number> optional;
}
</syntaxhighlight>
{{ out }}
<pre>
2 x -2 = 0
-0.001 + -Inf = -0
0 x -Inf = -Inf
1.5 + -1 = 1
-0.5 x 0 = -0
 
5^7 = 35
 
5 * ( 8 + 7 ) = 13
5 * 8 + 5 * 7 = 13
</pre>
 
Line 890 ⟶ 1,115:
max tropical comparison of 5 (x) with (+)(8,7) compared to
(x)(5,8) with (+) (x)(5,7) ───► true
</pre>
 
=={{header|RPL}}==
RPL does not support operator overloading so we need to use functions instead. As all stack-driven languages, RPL requires the user to deal with operator precedence.
MAXR EVAL '<span style="color:green>Inf</span>' STO
≪ 1 3 '''START''' ROT EVAL '''NEXT'''
'''IF''' DUP ABS <span style="color:green>Inf</span> == '''THEN''' SIGN '<span style="color:green>Inf</span>' * '''END'''
≫ ‘<span style="color:blue>TropOp</span>’ STO
≪ ≪ MAX ≫ <span style="color:blue>TropOp</span> ≫ ‘<span style="color:blue>TPLUS</span>’ STO
≪ ≪ + ≫ <span style="color:blue>TropOp</span> ≫ ‘<span style="color:blue>TMULT</span>’ STO
≪ ≪ * ≫ <span style="color:blue>TropOp</span> ≫ ‘<span style="color:blue>TPOWR</span>’ STO
 
2 -2 <span style="color:blue>TMULT</span>
-0.001 -<span style="color:green>Inf</span> <span style="color:blue>TPLUS</span>
0 -<span style="color:green>Inf</span> <span style="color:blue>TMULT</span>
1.5 -1 <span style="color:blue>TPLUS</span>
0.5 0 <span style="color:blue>TMULT</span>
5 7 <span style="color:blue>TPOWR</span>
8 7 <span style="color:blue>TPLUS</span> 5 <span style="color:blue>TMULT</span>
5 8 <span style="color:blue>TMULT</span> 5 7 <span style="color:blue>TMULT</span> <span style="color:blue>TPLUS</span>
 
{{out}}
<pre>
8: 0
7: -0.001
6: '-Inf'
5: 1.5
4: -0.5
3: 35
2: 13
1: 13
</pre>
 
Line 1,002 ⟶ 1,262:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var MinusInf = -1/0
 
class MaxTropical {
9,485

edits