-
Notifications
You must be signed in to change notification settings - Fork 3
Add types TropicalMaxMin and TropicalBitwise.
#16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
64d3cde
Add types `TropicalMaxMin` and `TropicalBitwise`.
samuelsonric 0a07cfd
fix CI
GiggleLiu 3758c60
Update src/tropical_bitwise.jl
GiggleLiu 067b613
update
GiggleLiu 6cab1ab
Merge https://github.com/samuelsonric/TropicalNumbers.jl into samuels…
GiggleLiu 75324a7
fix minplus
GiggleLiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,109 @@ | ||
| """ | ||
| TropicalBitwise{T} <: AbstractSemiring | ||
|
|
||
| TropicalBitwise is a semiring algebra, can be described by | ||
| * TropicalBitwise, (ℝ, |, &, 0, ~0). | ||
|
|
||
| It maps | ||
| * `+` to `|` | ||
| * `*` to `&` | ||
| * `0` to `0` | ||
| * `1` to ~0` | ||
|
|
||
| Example | ||
| ------------------------- | ||
| ```jldoctest; setup=:(using TropicalNumbers) | ||
| julia> TropicalBitwise(1) + TropicalBitwise(3) | ||
| 3ₛ | ||
|
|
||
| julia> TropicalBitwise(1) * TropicalBitwise(3) | ||
| 1ₛ | ||
|
|
||
| julia> zero(TropicalBitwiseI64) | ||
| 0ₛ | ||
|
|
||
| julia> one(TropicalBitwiseI64) | ||
| -1ₛ | ||
| ``` | ||
| """ | ||
| struct TropicalBitwise{T} <: AbstractSemiring | ||
| n::T | ||
| end | ||
|
|
||
| function TropicalBitwise(a::TropicalBitwise) | ||
| return TropicalBitwise(a.n) | ||
| end | ||
|
|
||
| function TropicalBitwise{T}(a::TropicalBitwise) where {T} | ||
| return TropicalBitwise{T}(a.n) | ||
| end | ||
|
|
||
| function Base.show(io::IO, a::TropicalBitwise) | ||
| print(io, "$(a.n)ₛ") | ||
| return | ||
| end | ||
|
|
||
| function Base.isapprox(a::TropicalBitwise, b::TropicalBitwise; kw...) | ||
| return isapprox(a.n, b.n; kw...) | ||
| end | ||
|
|
||
| function Base.promote_rule(::Type{TropicalBitwise{U}}, ::Type{TropicalBitwise{V}}) where {U, V} | ||
| W = promote_type(U, V) | ||
| return TropicalBitwise{W} | ||
| end | ||
|
|
||
| function Base.:+(a::TropicalBitwise, b::TropicalBitwise) | ||
| n = a.n | b.n | ||
| return TropicalBitwise(n) | ||
| end | ||
|
|
||
| function Base.:*(a::TropicalBitwise, b::TropicalBitwise) | ||
| n = a.n & b.n | ||
| return TropicalBitwise(n) | ||
| end | ||
|
|
||
| function Base.zero(::Type{T}) where {T <: TropicalBitwise} | ||
| return typemin(T) | ||
| end | ||
|
|
||
| function Base.zero(::T) where {T <: TropicalBitwise} | ||
| return zero(T) | ||
| end | ||
|
|
||
| function Base.one(::Type{T}) where {T <: TropicalBitwise} | ||
| return typemax(T) | ||
| end | ||
|
|
||
| function Base.one(::T) where {T <: TropicalBitwise} | ||
| return one(T) | ||
| end | ||
|
|
||
| function Base.typemin(::Type{TropicalBitwise{T}}) where {T} | ||
| n = zero(T) | ||
| return TropicalBitwise(n) | ||
| end | ||
|
|
||
| function Base.typemax(::Type{TropicalBitwise{T}}) where {T} | ||
| n = ~zero(T) | ||
| return TropicalBitwise(n) | ||
| end | ||
|
|
||
| function Base.:(==)(a::TropicalBitwise, b::TropicalBitwise) | ||
| return a.n == b.n | ||
| end | ||
|
|
||
| function Base.:>=(a::TropicalBitwise, b::TropicalBitwise) | ||
| return b.n <= a.n | ||
| end | ||
|
|
||
| function Base.:<=(a::TropicalBitwise, b::TropicalBitwise) | ||
| return a.n | b.n == b.n | ||
| end | ||
|
|
||
| function Base.:<(a::TropicalBitwise, b::TropicalBitwise) | ||
| return a != b && a <= b | ||
| end | ||
|
|
||
| function Base.:>(a::TropicalBitwise, b::TropicalBitwise) | ||
| return b < a | ||
| end | ||
This file contains hidden or 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,89 @@ | ||
| """ | ||
| TropicalMaxMin{T} <: AbstractSemiring | ||
|
|
||
| TropicalMaxMin is a semiring algebra, can be described by | ||
| * TropicalMaxMin, (ℝ, max, min, -Inf, Inf). | ||
|
|
||
| It maps | ||
| * `+` to `max` in regular algebra, | ||
| * `*` to `min` in regular algebra, | ||
| * `0` to `-Inf` in regular algebra (for integer content types, this is a small integer). | ||
| * `1` to `Inf` in regular algebra, (for integer content types, this is a large integer) | ||
|
|
||
| Example | ||
| ------------------------- | ||
| ```jldoctest; setup=:(using TropicalNumbers) | ||
| julia> TropicalMaxMin(1.0) + TropicalMaxMin(3.0) | ||
| 3.0ₛ | ||
|
|
||
| julia> TropicalMaxMin(1.0) * TropicalMaxMin(3.0) | ||
| 1.0ₛ | ||
|
|
||
| julia> zero(TropicalMaxMinF64) | ||
| -Infₛ | ||
|
|
||
| julia> one(TropicalMaxMinF64) | ||
| Infₛ | ||
| ``` | ||
| """ | ||
| struct TropicalMaxMin{T} <: AbstractSemiring | ||
| n::T | ||
| end | ||
|
|
||
| function TropicalMaxMin(a::TropicalMaxMin) | ||
| return TropicalMaxMin(a.n) | ||
| end | ||
|
|
||
| function TropicalMaxMin{T}(a::TropicalMaxMin) where {T} | ||
| return TropicalMaxMin{T}(a.n) | ||
| end | ||
|
|
||
| function Base.show(io::IO, a::TropicalMaxMin) | ||
| print(io, "$(a.n)ₛ") | ||
| return | ||
| end | ||
|
|
||
| function Base.isapprox(a::TropicalMaxMin, b::TropicalMaxMin; kw...) | ||
| return isapprox(a.n, b.n; kw...) | ||
| end | ||
|
|
||
| function Base.promote_rule(::Type{TropicalMaxMin{U}}, ::Type{TropicalMaxMin{V}}) where {U, V} | ||
| W = promote_type(U, V) | ||
| return TropicalMaxMin{W} | ||
| end | ||
|
|
||
| function Base.:+(a::TropicalMaxMin, b::TropicalMaxMin) | ||
| n = max(a.n, b.n) | ||
| return TropicalMaxMin(n) | ||
| end | ||
|
|
||
| function Base.:*(a::TropicalMaxMin, b::TropicalMaxMin) | ||
| n = min(a.n, b.n) | ||
| return TropicalMaxMin(n) | ||
| end | ||
|
|
||
| function Base.zero(::Type{T}) where {T <: TropicalMaxMin} | ||
| return typemin(T) | ||
| end | ||
|
|
||
| function Base.zero(::T) where {T <: TropicalMaxMin} | ||
| return zero(T) | ||
| end | ||
|
|
||
| function Base.one(::Type{T}) where {T <: TropicalMaxMin} | ||
| return typemax(T) | ||
| end | ||
|
|
||
| function Base.one(::T) where {T <: TropicalMaxMin} | ||
| return one(T) | ||
| end | ||
|
|
||
| function Base.typemin(::Type{TropicalMaxMin{T}}) where {T} | ||
| n = neginf(T) | ||
| return TropicalMaxMin(n) | ||
| end | ||
|
|
||
| function Base.typemax(::Type{TropicalMaxMin{T}}) where {T} | ||
| n = posinf(T) | ||
| return TropicalMaxMin(n) | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
| using Test | ||
| using TropicalNumbers | ||
|
|
||
| @testset "TropicalBitwise max mul" begin | ||
| @test TropicalBitwise(3) * TropicalBitwise(4) == TropicalBitwise(0) | ||
| @test TropicalBitwise(3) + TropicalBitwise(4) == TropicalBitwise(7) | ||
| @test TropicalBitwise(4) + TropicalBitwise(1) == TropicalBitwise(5) | ||
| @test zero(TropicalBitwise(2)) == TropicalBitwise(0) | ||
| @test one(TropicalBitwise(2)) == TropicalBitwise(~0) | ||
| @test TropicalBitwise(2) ≈ TropicalBitwise(2) | ||
| @test TropicalBitwiseI32(2).n isa Int32 | ||
| @test TropicalBitwiseI16(2).n isa Int16 | ||
| @test TropicalBitwiseI64(2).n isa Int64 | ||
|
|
||
| @test content(TropicalBitwise(3)) == 3 | ||
| @test TropicalBitwise{UInt32}(TropicalBitwise(0)) isa TropicalBitwise{UInt32} | ||
| println(TropicalBitwiseI64(3)) | ||
|
|
||
| # promote and convert | ||
| t1 = TropicalBitwise(2) | ||
| t2 = TropicalBitwise(UInt32(2)) | ||
| @test TropicalBitwiseI32(t1) === TropicalBitwise(Int32(2)) | ||
| @test promote(t1, t2) === (t1, t1) | ||
|
|
||
| @test content(TropicalBitwiseI64) == Int64 | ||
|
|
||
| @test promote(TropicalBitwise{Int64}(1), TropicalBitwise{UInt64}(2)) == (TropicalBitwise{Int64}(1), TropicalBitwise{Int64}(2)) | ||
| @test promote(TropicalBitwise{Int64}(1)) == (TropicalBitwise{Int64}(1),) | ||
| @test promote_type(TropicalBitwise{Int64}, TropicalBitwiseI32) == TropicalBitwiseI64 | ||
| @test promote_type(TropicalBitwise{Int64}, TropicalBitwiseI32, TropicalBitwise{UInt32}) == TropicalBitwiseI64 | ||
|
|
||
| x = TropicalBitwise(2) | ||
| @test x * true == x * one(x) | ||
| @test x * false == x * zero(x) | ||
| @test true * x == one(x) * x | ||
| @test false * x == zero(x) * x | ||
|
|
||
| @test TropicalBitwise(0) < TropicalBitwise(10) <= TropicalBitwise(10) <= TropicalBitwise(~0) | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.