-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b69b141
commit 5eb7ae3
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* This module provides a structure that denotes tropical semi-ring. | ||
* | ||
* Additive and multiplication of `Tropical` variable x and y is defined as: | ||
* | ||
* - `add(x, y)` := `Math.min(x, y)`, | ||
* - `mul(x, y)` := `x + y`. | ||
* | ||
* This module exports `semiRing` which is about this mathematical structure. | ||
* | ||
* @packageDocumentation | ||
* @module | ||
*/ | ||
|
||
import { none, type Option, some } from "./option.ts"; | ||
import { abelSymbol } from "./type-class/abelian-group.ts"; | ||
import type { AbelianMonoid } from "./type-class/abelian-monoid.ts"; | ||
import type { Monoid } from "./type-class/monoid.ts"; | ||
import { semiGroupSymbol } from "./type-class/semi-group.ts"; | ||
import type { SemiRing } from "./type-class/semi-ring.ts"; | ||
|
||
declare const tropicalNominal: unique symbol; | ||
/** | ||
* A tropical semi-ring data which consists of finite numbers and positive infinity. | ||
*/ | ||
export type Tropical = number & { [tropicalNominal]: never }; | ||
|
||
/** | ||
* Transforms a number into a `Tropical`. | ||
* | ||
* @param num - Source integer. | ||
* @returns The new number of `Tropical`. | ||
* | ||
* # Throws | ||
* | ||
* It throws an error only if `num` is negative infinity or NaN. | ||
*/ | ||
export const fromNumber = (num: number): Tropical => { | ||
if (Number.NEGATIVE_INFINITY < num) { | ||
return num as Tropical; | ||
} | ||
throw new Error("tropical num must be finite or positive infinity"); | ||
}; | ||
|
||
/** | ||
* Checks and transforms a number into a `Tropical`. | ||
* | ||
* @param num - Source integer. | ||
* @returns The new number of `Tropical`, or none if failed. | ||
*/ | ||
export const fromNumberChecked = (num: number): Option<Tropical> => { | ||
if (Number.NEGATIVE_INFINITY < num) { | ||
return some(num as Tropical); | ||
} | ||
return none(); | ||
}; | ||
|
||
/** | ||
* Additive about `Math.min` on `Tropical`. | ||
*/ | ||
export const additive: AbelianMonoid<Tropical> = { | ||
identity: Number.POSITIVE_INFINITY as Tropical, | ||
combine: (l, r) => Math.min(l, r) as Tropical, | ||
[abelSymbol]: true, | ||
[semiGroupSymbol]: true, | ||
}; | ||
|
||
/** | ||
* Multiplication about `+` on `Tropical`. | ||
*/ | ||
export const multiplication: Monoid<Tropical> = { | ||
identity: 0 as Tropical, | ||
combine: (l, r) => (l + r) as Tropical, | ||
[semiGroupSymbol]: true, | ||
}; | ||
|
||
/** | ||
* Semi-ring about `Math.min` as additive and `+` as multiplication on `Tropical`. | ||
*/ | ||
export const semiRing: SemiRing<Tropical> = { additive, multiplication }; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { abelSymbol } from "./abelian-group.ts"; | ||
import type { Monoid } from "./monoid.ts"; | ||
|
||
export interface AbelianMonoid<T> extends Monoid<T> { | ||
[abelSymbol]: true; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { AbelianMonoid } from "./abelian-monoid.ts"; | ||
import type { Monoid } from "./monoid.ts"; | ||
|
||
/** | ||
* A `SemiRing` instance for `T` must satisfy these laws: | ||
* | ||
* - Additive is an abelian monoid. The identity of `additive` is called `zero`. | ||
* - Multiplication is a monoid. The identity of `multiplication` is called `one`. | ||
* - On multiplication, any element `x` is left and right annihilated by `zero`: | ||
* - `multiplication.combine(zero, x)` = `zero`, | ||
* - `multiplication.combine(x, zero)` = `zero`. | ||
*/ | ||
export interface SemiRing<T> { | ||
additive: AbelianMonoid<T>; | ||
multiplication: Monoid<T>; | ||
} |