Skip to content
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

More semi-ring algebras #9

Merged
merged 6 commits into from
Sep 24, 2023
Merged

Conversation

ArrogantGao
Copy link
Contributor

I add more semi-ring algebras to the package, including the TropicalMaxMul, TropicalMinPlus and TropicalAndor, and I also give the original type Tropical an extra name TropicalMaxPlus, all tests passed for these algebras.

TropicalMinMul is not added, since $(\mathbb{R}^{-}, \cdot)$ do not have identity element.

I changed the rule about Bool type from

for T in [:Tropical, :TropicalMaxMul, :TropicalMinPlus, :CountingTropical]
    @eval begin
        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 ? inv(a) : zero(a)
    end
end

to

for T in [:Tropical, :TropicalMaxMul, :TropicalMinPlus, :CountingTropical]
    @eval begin
        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

The original one does not work well with TropicalMaxMul, since

julia> a = TropicalMaxMul(2.0)
2.0ₜ

julia> zero(TropicalMaxMul)
0ₜ

julia> a / zero(TropicalMaxMul)
Infₜ

julia> a ÷ zero(TropicalMaxMul)
NaNₜ

This change doesn't affect the original tests.

@ArrogantGao
Copy link
Contributor Author

ArrogantGao commented Sep 24, 2023

Another problem is that now all these different Tropical type are shown as xₜ, do we need to assign different indices to different types (indices like maxplus maybe to long)?

Copy link
Member

@GiggleLiu GiggleLiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a very solid PR, I just have a few minor suggestions. I wish we could show the definitions of these algebras in the README, e.g. a table with (+, *, 1, 0, subscript) as columns and different algebra types as rows.

test/runtests.jl Show resolved Hide resolved
src/tropical_maxplus.jl Outdated Show resolved Hide resolved
src/tropical_maxmul.jl Show resolved Hide resolved
src/tropical_andor.jl Show resolved Hide resolved
Base.div(a::$T, b::Bool) = b ? a : a / zero(a)
Base.div(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 ? inv(a) : zero(a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Please remove the lines commented out.

end
end

Base.show(io::IO, t::TropicalMinPlus) = Base.print(io, "$(t.n)ₜ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish they could be printed with a different subscript.

end
end

const TropicalMaxPlus = Tropical
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! This is exactly what I meant.

@GiggleLiu
Copy link
Member

Another comment: we'd better add a new abstract type AbstractSemiring as their supertype.

@GiggleLiu
Copy link
Member

About the subscripts, you may find a complete list of unicode here:
https://docs.julialang.org/en/v1/manual/unicode-input/

Among which I would suggest

  • using for TropicalMaxPlus and TropicalAndOr
  • using for TropicalMinPlus, because s and t have strong correlation when used in notation.
  • using for TropicalMaxMul, because x can be related to multiplication.

I do not have strong opinion on how they look like. It is more important to explain these convensions in the documentation.

@ArrogantGao
Copy link
Contributor Author

The following changes have been made:

  1. An abstract type AbstractSemiring <: Number is added as the super type of all the tropical algebras.
  2. Indices are changed, test sets are separated, and the test about TropicalMaxPlus are replaced by
@test Tropical == TropicalMaxPlus
@test TropicalF16 == TropicalMaxPlusF16
@test TropicalF32 == TropicalMaxPlusF32
@test TropicalF64 == TropicalMaxPlusF64

The latest test result is shown below (please see the indices):

     Testing Running tests...
trueₜ
Test Summary:   | Pass  Total  Time
tropical and or |   12     12  0.1s
3.0ₓ
Test Summary:    | Pass  Total  Time
tropical max mul |   44     44  0.2s
3.0ₛ
Test Summary:     | Pass  Total  Time
tropical min plus |   46     46  0.1s
3.0ₜ
Test Summary:     | Pass  Total  Time
tropical max plus |   50     50  0.1s
(3.0, 1.0)ₜ
Test Summary:     | Pass  Total  Time
counting_tropical |   25     25  0.1s
[ Info: SetupBuildDirectory: setting up build directory.
[ Info: Doctest: running doctests.
[ Info: Skipped ExpandTemplates step (doctest only).
[ Info: Skipped CrossReferences step (doctest only).
[ Info: Skipped CheckDocument step (doctest only).
[ Info: Skipped Populate step (doctest only).
[ Info: Skipped RenderDocument step (doctest only).
Test Summary:             | Pass  Total  Time
Doctests: TropicalNumbers |    1      1  5.4s
     Testing TropicalNumbers tests passed 
  1. Documents and the readme file are revised, I added the description about semiring, tropical geometry and each of the algebras.

@ArrogantGao
Copy link
Contributor Author

  1. All the export are moved to the TropicalNumber.jl file to avoid repeat exportation.

Copy link
Member

@GiggleLiu GiggleLiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, I think all the changes make sense to me. I forgot to mention not using error handling in the consctructor.

We should remove all error handling in the default constructor of TropicalMaxMul, otherwise the code can not run on GPU.
A better practise would be using unsafe_xxx as the constructor without error handling. However, here to make life easier, we can also assume users know the negative numbers are forbidden in this algebra.

Either approach is good to me.

@ArrogantGao
Copy link
Contributor Author

All the error handling are removed, here we may just assume all users know how to use it properly.

@codecov
Copy link

codecov bot commented Sep 24, 2023

Codecov Report

Merging #9 (32272ca) into master (6200c3b) will decrease coverage by 1.36%.
The diff coverage is 83.78%.

@@            Coverage Diff             @@
##           master       #9      +/-   ##
==========================================
- Coverage   86.41%   85.06%   -1.36%     
==========================================
  Files           3        6       +3     
  Lines          81      154      +73     
==========================================
+ Hits           70      131      +61     
- Misses         11       23      +12     
Files Changed Coverage Δ
src/TropicalNumbers.jl 84.00% <70.00%> (-9.75%) ⬇️
src/tropical_andor.jl 72.72% <72.72%> (ø)
src/tropical_maxmul.jl 83.33% <83.33%> (ø)
src/tropical_minplus.jl 92.85% <92.85%> (ø)
src/tropical_maxplus.jl 82.85% <100.00%> (ø)

@GiggleLiu GiggleLiu merged commit 1e1abc6 into TensorBFS:master Sep 24, 2023
2 of 4 checks passed
@GiggleLiu GiggleLiu mentioned this pull request Sep 24, 2023
@GiggleLiu
Copy link
Member

Good job. I just registered a new version v0.6.0. 🎉

@ArrogantGao
Copy link
Contributor Author

ArrogantGao commented Sep 24, 2023

Wow, thanks a lot.
Then I will work on the tensor element interface of CuTropicalGEMM.jl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants