Last active
December 7, 2015 10:26
-
-
Save matarillo/7502d98b8d46f56b4546 to your computer and use it in GitHub Desktop.
MinPlus semi-ring (http://d.hatena.ne.jp/m-hiyama/20151202/1449016930)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace fstropical | |
type MinPlus = | |
struct | |
val value: float | |
new(x: float) = { value = x } | |
override this.ToString() = | |
if this.value = System.Double.PositiveInfinity then "∞" else string this.value | |
static member Zero = new MinPlus(System.Double.PositiveInfinity) | |
static member One = new MinPlus(0.0) | |
static member (+) (x: MinPlus, y: MinPlus) = new MinPlus(min x.value y.value) | |
static member (*) (x: MinPlus, y: MinPlus) = new MinPlus(x.value + y.value) | |
static member op_Explicit (x: float) = new MinPlus(x) | |
end | |
module NumericLiteralQ = | |
let FromZero() = new MinPlus(0.0) | |
let FromOne() = new MinPlus(1.0) | |
let FromInt32 (i: int) = new MinPlus(float i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open LanguagePrimitives | |
open fstropical | |
[<EntryPoint>] | |
let main argv = | |
printfn "%s" "Custom Numeric Literal Prefix" | |
printf "(0Q, 1Q, 15Q) = " | |
printfn "%A" (0Q, 1Q, 15Q) // (0, 1, 15) | |
printfn "" | |
printfn "%s" "Explicit Type Conversion" | |
printf "(MinPlus 0.0, MinPlus 1.0, MinPlus Double.PositiveInfinity) = " | |
printfn "%A" (MinPlus 0.0, MinPlus 1.0, MinPlus Double.PositiveInfinity) // (0, 1, ∞) | |
printfn "" | |
printfn "%s" "Tropical Identity Elements" | |
printf "(MinPlus.Zero, MinPlus.One) = " | |
printfn "%A" (MinPlus.Zero, MinPlus.One) // (∞, 0) | |
printfn "" | |
printfn "%s" "Tropical Identity Elements (Generic)" | |
let (generic0: MinPlus, generic1: MinPlus) = (GenericZero, GenericOne) | |
printf "(GenericZero, GenericOne) = " | |
printfn "%A" (generic0, generic1) // (∞, 0) | |
printfn "" | |
let inf = MinPlus Double.PositiveInfinity | |
printfn "1 + 1 = %A" (1Q + 1Q) // 1 | |
printfn "2 + 3 = %A" (2Q + 3Q) // 2 | |
printfn "2 * 3 = %A" (2Q * 3Q) // 5 | |
printfn "2 * 0 = %A" (2Q * 0Q) // 2 | |
printfn "3 + ∞ = %A" (3Q + inf) // 3 | |
printfn "3 * ∞ = %A" (3Q * inf) // ∞ | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment