Skip to content

Commit 5c51b7d

Browse files
author
Wimmerer
committed
improve semirings docs
1 parent b9dedff commit 5c51b7d

File tree

4 files changed

+80
-33
lines changed

4 files changed

+80
-33
lines changed

docs/src/semirings.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ There are over 200 built-in semirings available as constants in the `Semirings`
1818
- Boolean semiring $(\vee, \wedge)$ available as `Semirings.LOR_LAND`.
1919
- GF2, the two-element Galois Field $(\text{xor}, \wedge)$, available as `Semirings.LXOR_LAND`.
2020

21-
Below is the list of built-ins with the two binary operators as well as the domains available listed for each semiring.
21+
Below are the built-in semirings available, along with their associated monoids, binary operators, and types.
22+
For common semirings where the binary operator and monoid have equivalent julia functions, those functions are listed.
23+
2224

2325
!!! note
2426
In all cases the input and output types of the semirings are the same, **except** for cases where the "add" types and "multiply" output output types are boolean, such as in `LAND_GE`.
@@ -30,24 +32,23 @@ cd("..")
3032
using SuiteSparseGraphBLAS
3133
using Latexify
3234
head = ["Semiring", "⊕", "⊗", "Types"]
33-
v1 = filter((x) -> eval(Semirings, :(typeof($x) <: AbstractSemiring)), names(Semirings))
34-
v2 = []
35-
v3 = []
36-
for rig in v1
37-
rig = string(rig)
38-
rigsplit = split(rig, '_')
39-
monoidname = rigsplit[1] .* "_MONOID"
40-
push!(v2, "[" .* rigsplit[1] .* "_MONOID](https://juliasparse.github.io/SuiteSparseGraphBLAS.jl/dev/monoids/#SuiteSparseGraphBLAS.Monoids.$monoidname)")
41-
push!(v3, "[" .* rigsplit[2] .* "](https://juliasparse.github.io/SuiteSparseGraphBLAS.jl/dev/binaryops/#SuiteSparseGraphBLAS.BinaryOps.$(rigsplit[2]))")
35+
v1 = filter((x) -> getproperty(Semirings, x) isa SuiteSparseGraphBLAS.AbstractSemiring, names(Semirings))
36+
ops = getproperty.(Ref(Semirings), v1)
37+
v2 = convert(Vector{Any}, SuiteSparseGraphBLAS.addop.(ops))
38+
v3 = convert(Vector{Any}, SuiteSparseGraphBLAS.mulop.(ops))
39+
for i in 1:length(v2)
40+
if v2[i] === nothing
41+
x =
42+
v2[i] = replace(summary(SuiteSparseGraphBLAS.monoid(ops[i])), "SuiteSparseGraphBLAS.Monoids." => "")[1:end-2]
43+
end
44+
if v3[i] === nothing
45+
v3[i] = replace(summary(SuiteSparseGraphBLAS.binop(ops[i])), "SuiteSparseGraphBLAS.BinaryOps." => "")[1:end-2]
46+
end
4247
end
48+
v4 = SuiteSparseGraphBLAS.validtypes.(ops)
4349
44-
v4 = []
45-
46-
for op in v1
47-
op == :Semirings && continue
48-
op = getproperty(Semirings, op)
49-
push!(v4, SuiteSparseGraphBLAS.tolist(SuiteSparseGraphBLAS.validtypes(op)))
50-
end
5150
v1 = "`" .* string.(v1) .* "`"
51+
v2 = "`" .* string.(v2) .* "`"
52+
v3 = "`" .* string.(v3) .* "`"
5253
Latexify.mdtable(hcat(v1,v2,v3,v4); head, latex=false)
5354
```

src/operators/libgbops.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ ytype(monoid::libgb.GrB_Monoid) = ytype(operator(monoid))
1414
ztype(monoid::libgb.GrB_Monoid) = ztype(operator(monoid))
1515
Base.show(io::IO, ::MIME"text/plain", m::libgb.GrB_Monoid) = gxbprint(io, m)
1616

17-
18-
19-
20-
multiplyop(rig::libgb.GrB_Semiring) = libgb.GxB_Semiring_multiply(rig)
17+
mulop(rig::libgb.GrB_Semiring) = libgb.GxB_Semiring_multiply(rig)
2118
addop(rig::libgb.GrB_Semiring) = libgb.GxB_Semiring_add(rig)
22-
xtype(rig::libgb.GrB_Semiring) = xtype(multiplyop(rig))
23-
ytype(rig::libgb.GrB_Semiring) = ytype(multiplyop(rig))
19+
xtype(rig::libgb.GrB_Semiring) = xtype(mulop(rig))
20+
ytype(rig::libgb.GrB_Semiring) = ytype(mulop(rig))
2421
ztype(rig::libgb.GrB_Semiring) = ztype(addop(rig))
2522
Base.show(io::IO, ::MIME"text/plain", s::libgb.GrB_Semiring) = gxbprint(io, s)

src/operators/oplist.jl

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,12 @@ Monoids.BXNOR_MONOID
737737
op(::typeof(Monoids.BXNOR_MONOID)) = BinaryOps.BXNOR
738738
Monoids.Monoid(::typeof(!)) = Monoids.BXNOR_MONOID
739739

740+
mulop(rig::AbstractSemiring) = nothing
741+
addop(rig::AbstractSemiring) = nothing
742+
743+
# TODO:
744+
# The definitions below are less than ideal, these should likely be fields of the semiring
745+
# structs, not evaluated functions.
740746
for oplus [(:max, "MAX"), (:min, "MIN"), (:+, "PLUS"), (:*, "TIMES"), (:any, "ANY")]
741747
for otimes [
742748
(:/, "DIV"),
@@ -764,13 +770,48 @@ for oplus ∈ [(:max, "MAX"), (:min, "MIN"), (:+, "PLUS"), (:*, "TIMES"), (:any,
764770
(:+, "PLUS"),
765771
(:pair, "PAIR")
766772
]
767-
rig = Symbol(oplus[2], "_", otimes[2])
768-
funcquote = quote
769-
Semirings.Semiring(::typeof($(oplus[1])), ::typeof($(otimes[1]))) = $rig
770-
addop(::typeof($rig)) = $(oplus[1])
771-
mulop(::typeof($rig)) = $(otimes[1])
772-
end
773-
@eval($funcquote)
773+
rig = Symbol(oplus[2], "_", otimes[2])
774+
funcquote = quote
775+
Semirings.Semiring(::typeof($(oplus[1])), ::typeof($(otimes[1]))) = $rig
776+
addop(::typeof($rig)) = $(oplus[1])
777+
mulop(::typeof($rig)) = $(otimes[1])
778+
end
779+
@eval($funcquote)
780+
end
781+
end
782+
783+
for otimes [(:(==), "EQ"), (:(>=), "GE"), (:(>), "GT"), (:(<=), "LE"), (:(<), "LT"), (:(!=), "NE")]
784+
oplus = (:any, "ANY")
785+
rig = Symbol(oplus[2], "_", otimes[2])
786+
funcquote = quote
787+
Semirings.Semiring(::typeof($(oplus[1])), ::typeof($(otimes[1]))) = $rig
788+
addop(::typeof($rig)) = $(oplus[1])
789+
mulop(::typeof($rig)) = $(otimes[1])
790+
end
791+
@eval($funcquote)
792+
end
793+
for oplus [(:(==), "EQ"), (:, "LAND"), (:, "LOR"), (:lxor, "LXOR")]
794+
for otimes [
795+
(:(==), "EQ"),
796+
(:(>=), "GE"),
797+
(:(>), "GT"),
798+
(:(<=), "LE"),
799+
(:(<), "LT"),
800+
(:(!=), "NE"),
801+
(:first, "FIRST"),
802+
(:second, "SECOND"),
803+
(:, "LAND"),
804+
(:, "LOR"),
805+
(:lxor, "LXOR"),
806+
(:pair, "PAIR")
807+
]
808+
rig = Symbol(oplus[2], "_", otimes[2])
809+
funcquote = quote
810+
Semirings.Semiring(::typeof($(oplus[1])), ::typeof($(otimes[1]))) = $rig
811+
addop(::typeof($rig)) = $(oplus[1])
812+
mulop(::typeof($rig)) = $(otimes[1])
813+
end
814+
@eval($funcquote)
774815
end
775816
end
776817

src/operators/semirings.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ function _semiringnames(name)
1313
exportedname = Symbol(simple)
1414
return containername, exportedname
1515
end
16-
1716
function Semiring(name)
1817
containername, exportedname = _semiringnames(name)
1918
structquote = quote
@@ -36,7 +35,12 @@ end
3635

3736
SemiringUnion = Union{AbstractSemiring, TypedSemiring}
3837

39-
38+
function _monoidfromrigname(name)
39+
return split(name, "_")[2] * "_MONOID"
40+
end
41+
function _binopfromrigname(name)
42+
return split(name, "_")[3]
43+
end
4044

4145
#TODO: Rewrite
4246
function _createsemirings()
@@ -255,7 +259,11 @@ function _createsemirings()
255259
]
256260

257261
for name builtins
258-
Semiring(name)
262+
rig = Semiring(name)
263+
monoid = getproperty(Monoids, Symbol(_monoidfromrigname(name)))
264+
eval(:(monoid(::typeof($rig)) = $monoid))
265+
binop = getproperty(BinaryOps, Symbol(_binopfromrigname(name)))
266+
eval(:(binop(::typeof($rig)) = $binop))
259267
end
260268
end
261269

0 commit comments

Comments
 (0)