Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
name = "TropicalNumbers"
uuid = "b3a74e9c-7526-4576-a4eb-79c0d4c32334"
authors = ["GiggleLiu"]
version = "0.6.4"
authors = ["GiggleLiu"]

[deps]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
Random = "1.11.0"
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Documenter"]
97 changes: 12 additions & 85 deletions src/TropicalNumbers.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module TropicalNumbers

export content, neginf, posinf
using Random
using Random: Repetition, Sampler

export content, neginf, posinf, inf, imp, not, fia, fli, fri, fii, inf_fast, ldiv_fast, imp_fast
export TropicalTypes, AbstractSemiring

export TropicalAndOr
Expand All @@ -12,43 +15,22 @@ export TropicalMaxMin, TropicalMaxMinF64, TropicalMaxMinF32, TropicalMaxMinF16,
export TropicalBitwise, TropicalBitwiseI64, TropicalBitwiseI32, TropicalBitwiseI16
export CountingTropical, CountingTropicalF16, CountingTropicalF32, CountingTropicalF64, CountingTropicalI16, CountingTropicalI32, CountingTropicalI64

neginf(::Type{T}) where {T} = typemin(T)
neginf(::Type{T}) where {T <: Integer} = T(-999999)
neginf(::Type{Int16}) = Int16(-16384)
neginf(::Type{Int8}) = Int8(-64)
posinf(::Type{T}) where {T} = -neginf(T)

"""
AbstractSemiring <: Number

A [`Semiring`](https://en.wikipedia.org/wiki/Semiring) is a set R equipped with two binary operations + and ⋅, called addition and multiplication, such that:

* (R, +) is a monoid with identity element called 0;
* (R, ⋅) is a monoid with identity element called 1;
* Addition is commutative;
* Multiplication by the additive identity 0 annihilates ;
* Multiplication left- and right-distributes over addition;
* Explicitly stated, (R, +) is a commutative monoid.

[`Tropical number`](https://en.wikipedia.org/wiki/Tropical_geometry) are a set of semiring algebras, described as
* (R, +, ⋅, 0, 1).
where R is the set, + and ⋅ are the opeartions and 0, 1 are their identity element, respectively.

In this package, the following tropical algebras are implemented:
* TropicalAndOr, ([T, F], or, and, false, true);
* Tropical (TropicalMaxPlus), (ℝ, max, +, -Inf, 0);
* TropicalMinPlus, (ℝ, min, +, Inf, 0);
* TropicalMaxMul, (ℝ⁺, max, ⋅, 0, 1).

We implemented fast tropical matrix multiplication in [`TropicalGEMM`](https://github.com/TensorBFS/TropicalGEMM.jl/) and [`CuTropicalGEMM`](https://github.com/ArrogantGao/CuTropicalGEMM.jl/).
"""
abstract type AbstractSemiring <: Number end

include("abstract_semiring_algebra.jl")
include("semiring.jl")
include("tropical_maxplus.jl")
include("tropical_andor.jl")
include("tropical_minplus.jl")
include("tropical_maxmul.jl")
include("tropical_maxmin.jl")
include("tropical_bitwise.jl")
include("counting_tropical.jl")

const TropicalTypes{T} = Union{CountingTropical{T}, Tropical{T}}
const TropicalMaxPlus = Tropical
const = inf

# alias
# defining constants like `TropicalF64`.
Expand All @@ -69,59 +51,4 @@ for NBIT in [16, 32, 64]
@eval const $(Symbol(:CountingTropical, :I, NBIT)) = CountingTropical{$(Symbol(:Int, NBIT)),$(Symbol(:Float, NBIT))}
end

# alias
for T in [:Tropical, :TropicalMaxMul, :TropicalMaxMin, :CountingTropical]
for OP in [:>, :<, :(==), :>=, :<=, :isless]
@eval Base.$OP(a::$T, b::$T) = $OP(a.n, b.n)
end
@eval begin
content(x::$T) = x.n
content(x::Type{$T{X}}) where X = X
Base.isapprox(x::AbstractArray{<:$T}, y::AbstractArray{<:$T}; kwargs...) = all(isapprox.(x, y; kwargs...))
Base.show(io::IO, ::MIME"text/plain", t::$T) = Base.show(io, t)
Base.isnan(x::$T) = isnan(content(x))
end
end

for T in [:TropicalMinPlus, :TropicalBitwise]
@eval begin
content(x::$T) = x.n
content(x::Type{$T{X}}) where X = X
Base.isapprox(x::AbstractArray{<:$T}, y::AbstractArray{<:$T}; kwargs...) = all(isapprox.(x, y; kwargs...))
Base.show(io::IO, ::MIME"text/plain", t::$T) = Base.show(io, t)
Base.isnan(x::$T) = isnan(content(x))
end
end

for T in [:TropicalAndOr]
for OP in [:>, :<, :(==), :>=, :<=, :isless]
@eval Base.$OP(a::$T, b::$T) = $OP(a.n, b.n)
end
@eval begin
content(x::$T) = x.n
Base.isapprox(x::AbstractArray{<:$T}, y::AbstractArray{<:$T}; kwargs...) = all(isapprox.(x, y; kwargs...))
Base.show(io::IO, ::MIME"text/plain", t::$T) = Base.show(io, t)
Base.isnan(x::$T) = isnan(content(x))
end
end

for T in [:Tropical, :TropicalMinPlus, :TropicalMaxMul, :CountingTropical]
@eval begin
# this is for CUDA matmul
Base.:(*)(a::$T, b::Bool) = b ? a : zero(a)
Base.:(*)(b::Bool, a::$T) = b ? a : zero(a)
Base.:(/)(a::$T, b::Bool) = b ? a : a / zero(a)
Base.:(/)(b::Bool, a::$T) = b ? inv(a) : zero(a)
Base.div(a::$T, b::Bool) = b ? a : a ÷ zero(a)
Base.div(b::Bool, a::$T) = b ? one(a) ÷ a : zero(a)
end
end

for T in [:TropicalMaxMin, :TropicalBitwise]
@eval begin
Base.:(*)(a::$T, b::Bool) = b ? a : zero(a)
Base.:(*)(b::Bool, a::$T) = b ? a : zero(a)
end
end

end # module
Loading