Skip to content

Commit 9989590

Browse files
authored
Add format check (#36)
1 parent d211700 commit 9989590

File tree

17 files changed

+632
-250
lines changed

17 files changed

+632
-250
lines changed

.JuliaFormatter.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Configuration file for JuliaFormatter.jl
2+
# For more information, see: https://domluna.github.io/JuliaFormatter.jl/stable/config/
3+
4+
always_for_in = true
5+
always_use_return = true
6+
margin = 80
7+
remove_extra_newlines = true
8+
separate_kwargs_with_semicolon = true
9+
short_to_long_function_def = true

.github/workflows/format_check.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
ame: format-check
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- release-*
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: julia-actions/setup-julia@latest
14+
with:
15+
version: '1'
16+
- uses: actions/checkout@v3
17+
- name: Format check
18+
shell: julia --color=yes {0}
19+
run: |
20+
using Pkg
21+
# If you update the version, also update the style guide docs.
22+
Pkg.add(PackageSpec(name="JuliaFormatter", version="1"))
23+
using JuliaFormatter
24+
format("src", verbose=true)
25+
format("test", verbose=true)
26+
out = String(read(Cmd(`git diff`)))
27+
if isempty(out)
28+
exit(0)
29+
end
30+
@error "Some files have not been formatted !!!"
31+
write(stdout, out)
32+
exit(1)

Project.toml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,3 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1313
MultivariatePolynomials = "0.4.2"
1414
MutableArithmetics = "0.3.1, 1"
1515
julia = "1.6"
16-
17-
[extras]
18-
DynamicPolynomials = "7c1d4256-1411-5781-91ec-d7bc3513ac07"
19-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
20-
TypedPolynomials = "afbbf031-7a57-5f58-a1b9-b774a0fad08d"
21-
22-
[targets]
23-
test = ["Test", "DynamicPolynomials", "TypedPolynomials"]

src/SemialgebraicSets.jl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@ const MP = MultivariatePolynomials
1010

1111
const APL = AbstractPolynomialLike
1212

13-
export AbstractSemialgebraicSet, AbstractBasicSemialgebraicSet, AbstractAlgebraicSet
14-
export FullSpace, AlgebraicSet, BasicSemialgebraicSet, addequality!, addinequality!
13+
export AbstractSemialgebraicSet,
14+
AbstractBasicSemialgebraicSet, AbstractAlgebraicSet
15+
export FullSpace,
16+
AlgebraicSet, BasicSemialgebraicSet, addequality!, addinequality!
1517

1618
# Semialgebraic set described by polynomials with coefficients in T
1719
abstract type AbstractSemialgebraicSet end
1820

1921
abstract type AbstractBasicSemialgebraicSet <: AbstractSemialgebraicSet end
2022
abstract type AbstractAlgebraicSet <: AbstractBasicSemialgebraicSet end
2123

22-
addinequality!(S::AbstractAlgebraicSet, p) = throw(ArgumentError("Cannot add inequality to an algebraic set"))
23-
24-
struct FullSpace <: AbstractAlgebraicSet
24+
function addinequality!(S::AbstractAlgebraicSet, p)
25+
throw(ArgumentError("Cannot add inequality to an algebraic set"))
2526
end
27+
28+
struct FullSpace <: AbstractAlgebraicSet end
2629
function Base.show(io::IO, ::FullSpace)
27-
print(io, "R^n")
30+
return print(io, "R^n")
2831
end
2932
nequalities(::FullSpace) = 0
3033
equalities(::FullSpace) = []
@@ -39,7 +42,14 @@ Base.intersect(S::FullSpace, T::AbstractAlgebraicSet) = T
3942
Base.intersect(S::FullSpace, T::FullSpace) = S
4043

4144
# If `intersect(S, T)` is not implemented, this method will `StackOverflow`.
42-
Base.intersect(S::AbstractSemialgebraicSet, T::AbstractSemialgebraicSet, args...; kws...) = intersect(intersect(S, T), args...; kws...)
45+
function Base.intersect(
46+
S::AbstractSemialgebraicSet,
47+
T::AbstractSemialgebraicSet,
48+
args...;
49+
kws...,
50+
)
51+
return intersect(intersect(S, T), args...; kws...)
52+
end
4353
# The keywords are only used when transforming `Element`
4454
# into `BasicSemialgebraicSet`.
4555
Base.intersect(set::AbstractSemialgebraicSet; kws...) = set

src/basic.jl

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,94 @@
11
export inequalities, basicsemialgebraicset
22

3-
struct BasicSemialgebraicSet{T, PT<:APL{T}, AT <: AbstractAlgebraicSet} <: AbstractBasicSemialgebraicSet
3+
struct BasicSemialgebraicSet{T,PT<:APL{T},AT<:AbstractAlgebraicSet} <:
4+
AbstractBasicSemialgebraicSet
45
V::AT
56
p::Vector{PT}
67
end
7-
function BasicSemialgebraicSet{T, PT}() where {T, PT<:APL{T}}
8-
BasicSemialgebraicSet(AlgebraicSet{T, PT}(), PT[])
8+
function BasicSemialgebraicSet{T,PT}() where {T,PT<:APL{T}}
9+
return BasicSemialgebraicSet(AlgebraicSet{T,PT}(), PT[])
910
end
10-
function BasicSemialgebraicSet(V::AlgebraicSet{T, PT, A, S}, p::Vector{PT}) where {T, PT<:APL{T}, A, S<:AbstractAlgebraicSolver}
11-
BasicSemialgebraicSet{T, PT, typeof(V)}(V, p)
11+
function BasicSemialgebraicSet(
12+
V::AlgebraicSet{T,PT,A,S},
13+
p::Vector{PT},
14+
) where {T,PT<:APL{T},A,S<:AbstractAlgebraicSolver}
15+
return BasicSemialgebraicSet{T,PT,typeof(V)}(V, p)
1216
end
13-
function BasicSemialgebraicSet(V::AlgebraicSet{T, PT, A, SO, U}, p::Vector{PS}) where {T, PT<:APL{T}, S, PS<:APL{S}, A, SO<:AbstractAlgebraicSolver, U}
17+
function BasicSemialgebraicSet(
18+
V::AlgebraicSet{T,PT,A,SO,U},
19+
p::Vector{PS},
20+
) where {T,PT<:APL{T},S,PS<:APL{S},A,SO<:AbstractAlgebraicSolver,U}
1421
ST = promote_type(T, S)
1522
PST = promote_type(PT, PS)
16-
BasicSemialgebraicSet(convert(AlgebraicSet{ST, PST, A, SO, U}, V), Vector{PST}(p))
23+
return BasicSemialgebraicSet(
24+
convert(AlgebraicSet{ST,PST,A,SO,U}, V),
25+
Vector{PST}(p),
26+
)
1727
end
1828
#BasicSemialgebraicSet{T, PT<:APL{T}}(V::AlgebraicSet{T, PT}, p::Vector{PT}) = BasicSemialgebraicSet{T, PT}(V, p)
1929
function basicsemialgebraicset(V, p)
20-
BasicSemialgebraicSet(V, p)
30+
return BasicSemialgebraicSet(V, p)
2131
end
2232

23-
MP.similar_type(::Type{BasicSemialgebraicSet{S, PS, AT}}, T::Type) where {S, PS, AT} = BasicSemialgebraicSet{T, MP.similar_type(PS, T), MP.similar_type(AT, T)}
33+
function MP.similar_type(
34+
::Type{BasicSemialgebraicSet{S,PS,AT}},
35+
T::Type,
36+
) where {S,PS,AT}
37+
return BasicSemialgebraicSet{
38+
T,
39+
MP.similar_type(PS, T),
40+
MP.similar_type(AT, T),
41+
}
42+
end
2443

25-
function Base.convert(::Type{BasicSemialgebraicSet{T, PT, AT}}, set::BasicSemialgebraicSet) where {T, PT, AT}
26-
return BasicSemialgebraicSet{T, PT, AT}(set.V, set.p)
44+
function Base.convert(
45+
::Type{BasicSemialgebraicSet{T,PT,AT}},
46+
set::BasicSemialgebraicSet,
47+
) where {T,PT,AT}
48+
return BasicSemialgebraicSet{T,PT,AT}(set.V, set.p)
2749
end
2850

29-
MP.variables(S::BasicSemialgebraicSet{T, PT, FullSpace}) where {T, PT<:APL{T}} = MP.variables(S.p)
30-
MP.variables(S::BasicSemialgebraicSet) = sort(union(MP.variables(S.V), MP.variables(S.p)), rev=true)
51+
function MP.variables(
52+
S::BasicSemialgebraicSet{T,PT,FullSpace},
53+
) where {T,PT<:APL{T}}
54+
return MP.variables(S.p)
55+
end
56+
function MP.variables(S::BasicSemialgebraicSet)
57+
return sort(union(MP.variables(S.V), MP.variables(S.p)); rev = true)
58+
end
3159
nequalities(S::BasicSemialgebraicSet) = nequalities(S.V)
3260
equalities(S::BasicSemialgebraicSet) = equalities(S.V)
3361
addequality!(S::BasicSemialgebraicSet, p) = addequality!(S.V, p)
3462
ninequalities(S::BasicSemialgebraicSet) = length(S.p)
3563
inequalities(S::BasicSemialgebraicSet) = S.p
3664
addinequality!(S::BasicSemialgebraicSet, p) = push!(S.p, p)
3765

38-
Base.intersect(S::BasicSemialgebraicSet, T::BasicSemialgebraicSet) = BasicSemialgebraicSet(S.V T.V, [S.p; T.p])
39-
Base.intersect(S::BasicSemialgebraicSet, T::AbstractAlgebraicSet) = BasicSemialgebraicSet(S.V T, copy(S.p))
66+
function Base.intersect(S::BasicSemialgebraicSet, T::BasicSemialgebraicSet)
67+
return BasicSemialgebraicSet(S.V T.V, [S.p; T.p])
68+
end
69+
function Base.intersect(S::BasicSemialgebraicSet, T::AbstractAlgebraicSet)
70+
return BasicSemialgebraicSet(S.V T, copy(S.p))
71+
end
4072
Base.intersect(S::BasicSemialgebraicSet, ::FullSpace) = S
41-
Base.intersect(T::AbstractAlgebraicSet, S::BasicSemialgebraicSet) = intersect(S, T)
73+
function Base.intersect(T::AbstractAlgebraicSet, S::BasicSemialgebraicSet)
74+
return intersect(S, T)
75+
end
4276

4377
function Base.show(io::IO, V::BasicSemialgebraicSet)
44-
print(io, "{ (", join(variables(V), ", "), ") | ",
45-
join(string.(equalities(V)) .* " = 0", ", "))
78+
print(
79+
io,
80+
"{ (",
81+
join(variables(V), ", "),
82+
") | ",
83+
join(string.(equalities(V)) .* " = 0", ", "),
84+
)
4685
if nequalities(V) > 0
4786
print(io, ", ")
4887
end
49-
print(io, join(string.(inequalities(V)) .* " ≥ 0", ", "), " }")
88+
return print(io, join(string.(inequalities(V)) .* " ≥ 0", ", "), " }")
5089
end
5190
function Base.show(io::IO, mime::MIME"text/plain", V::BasicSemialgebraicSet)
5291
print(io, "Basic semialgebraic Set defined by ")
5392
_show_els(io, "equalit", nequalities(V), equalities(V), "=")
54-
_show_els(io, "inequalit", ninequalities(V), inequalities(V), "")
93+
return _show_els(io, "inequalit", ninequalities(V), inequalities(V), "")
5594
end

src/fix.jl

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
export FixedVariablesSet
22

3-
struct FixedVariablesIdeal{V<:AbstractVariable, T<:Number, MT<:AbstractMonomialLike} <: AbstractPolynomialIdeal
4-
substitutions::Union{Nothing, Dict{V, T}}
5-
end
6-
function Base.convert(::Type{FixedVariablesIdeal{V, T, MT}}, ideal::FixedVariablesIdeal{V, T, MT}) where {V<:AbstractVariable, T<:Number, MT<:AbstractMonomialLike}
3+
struct FixedVariablesIdeal{
4+
V<:AbstractVariable,
5+
T<:Number,
6+
MT<:AbstractMonomialLike,
7+
} <: AbstractPolynomialIdeal
8+
substitutions::Union{Nothing,Dict{V,T}}
9+
end
10+
function Base.convert(
11+
::Type{FixedVariablesIdeal{V,T,MT}},
12+
ideal::FixedVariablesIdeal{V,T,MT},
13+
) where {V<:AbstractVariable,T<:Number,MT<:AbstractMonomialLike}
714
return ideal
815
end
9-
function Base.convert(::Type{FixedVariablesIdeal{V, T, MT}}, ideal::FixedVariablesIdeal{V, S, MT}) where {V, S, T, MT}
16+
function Base.convert(
17+
::Type{FixedVariablesIdeal{V,T,MT}},
18+
ideal::FixedVariablesIdeal{V,S,MT},
19+
) where {V,S,T,MT}
1020
subs = ideal.substitutions
1121
if subs !== nothing
12-
subs = convert(Dict{V, T}, subs)
22+
subs = convert(Dict{V,T}, subs)
1323
end
14-
return FixedVariablesIdeal{V, T, MT}(subs)
24+
return FixedVariablesIdeal{V,T,MT}(subs)
1525
end
1626

17-
function MP.variables(ideal::FixedVariablesIdeal{V}) where V
27+
function MP.variables(ideal::FixedVariablesIdeal{V}) where {V}
1828
if generate_nonzero_constant(ideal)
1929
return V[]
2030
else
21-
return sort(collect(keys(ideal.substitutions)), rev=true)
31+
return sort(collect(keys(ideal.substitutions)); rev = true)
2232
end
2333
end
2434
# In that case, the ideal can generate any polynomial.
@@ -34,15 +44,26 @@ function Base.rem(p::AbstractPolynomialLike, I::FixedVariablesIdeal)
3444
end
3545
end
3646

37-
struct FixedVariablesSet{V, T, MT} <: AbstractAlgebraicSet
38-
ideal::FixedVariablesIdeal{V, T, MT}
39-
end
40-
MP.similar_type(::Type{FixedVariablesSet{V, S, MT}}, T::Type) where {V, S, MT} = FixedVariablesSet{V, T, MT}
41-
function Base.convert(::Type{FixedVariablesSet{V, T, MT}}, set::FixedVariablesSet{V, T, MT}) where {V, T, MT}
47+
struct FixedVariablesSet{V,T,MT} <: AbstractAlgebraicSet
48+
ideal::FixedVariablesIdeal{V,T,MT}
49+
end
50+
function MP.similar_type(
51+
::Type{FixedVariablesSet{V,S,MT}},
52+
T::Type,
53+
) where {V,S,MT}
54+
return FixedVariablesSet{V,T,MT}
55+
end
56+
function Base.convert(
57+
::Type{FixedVariablesSet{V,T,MT}},
58+
set::FixedVariablesSet{V,T,MT},
59+
) where {V,T,MT}
4260
return set
4361
end
44-
function Base.convert(::Type{FixedVariablesSet{V, T, MT}}, set::FixedVariablesSet{V, S, MT}) where {V, S, T, MT}
45-
return FixedVariablesSet(convert(FixedVariablesIdeal{V, T, MT}, set.ideal))
62+
function Base.convert(
63+
::Type{FixedVariablesSet{V,T,MT}},
64+
set::FixedVariablesSet{V,S,MT},
65+
) where {V,S,T,MT}
66+
return FixedVariablesSet(convert(FixedVariablesIdeal{V,T,MT}, set.ideal))
4667
end
4768

4869
ideal(set::FixedVariablesSet, args...) = set.ideal
@@ -54,14 +75,17 @@ function nequalities(set::FixedVariablesSet)
5475
return length(set.ideal.substitutions)
5576
end
5677
end
57-
function equalities(set::FixedVariablesSet{V, T, MT}) where {V, T, MT}
78+
function equalities(set::FixedVariablesSet{V,T,MT}) where {V,T,MT}
5879
if set.ideal.substitutions === nothing
5980
return [constant_term(one(T), MT)]
6081
else
6182
return [key - value for (key, value) in set.ideal.substitutions]
6283
end
6384
end
64-
function Base.intersect(V::FixedVariablesSet{V1, T1, MT1}, W::FixedVariablesSet{V2, T2, MT2}) where {V1, V2, T1, T2, MT1, MT2}
85+
function Base.intersect(
86+
V::FixedVariablesSet{V1,T1,MT1},
87+
W::FixedVariablesSet{V2,T2,MT2},
88+
) where {V1,V2,T1,T2,MT1,MT2}
6589
# For `DynamicPolynomials`, they have the same type and for
6690
# `TypedPolynomials`, promoting would give `Monomial`.
6791
VT = V1 == V2 ? V1 : AbstractVariable
@@ -76,14 +100,14 @@ function Base.intersect(V::FixedVariablesSet{V1, T1, MT1}, W::FixedVariablesSet{
76100
end
77101
return a
78102
end
79-
sub = Dict{VT, T}()
103+
sub = Dict{VT,T}()
80104
merge!(combine, sub, V.ideal.substitutions)
81105
merge!(combine, sub, W.ideal.substitutions)
82106
if has_dup
83107
sub = nothing
84108
end
85109
end
86-
ideal = FixedVariablesIdeal{VT, T, promote_type(MT1, MT2)}(sub)
110+
ideal = FixedVariablesIdeal{VT,T,promote_type(MT1, MT2)}(sub)
87111
return FixedVariablesSet(ideal)
88112
end
89113
function Base.intersect(V::AlgebraicSet, W::FixedVariablesSet)
@@ -103,12 +127,12 @@ end
103127
# Assumes `isempty(V)`
104128
function only_point(V::FixedVariablesSet)
105129
subs = collect(V.ideal.substitutions)
106-
sort!(subs, rev = true, by = sub -> sub[1])
130+
sort!(subs; rev = true, by = sub -> sub[1])
107131
return [sub[2] for sub in subs]
108132
end
109133

110-
Base.eltype(::FixedVariablesSet{V, T}) where {V, T} = Vector{T}
111-
function Base.iterate(V::FixedVariablesSet, state=nothing)
134+
Base.eltype(::FixedVariablesSet{V,T}) where {V,T} = Vector{T}
135+
function Base.iterate(V::FixedVariablesSet, state = nothing)
112136
if state === nothing && !isempty(V)
113137
return only_point(V), true
114138
else
@@ -117,7 +141,7 @@ function Base.iterate(V::FixedVariablesSet, state=nothing)
117141
end
118142
Base.length(V::FixedVariablesSet) = isempty(V) ? 0 : 1
119143

120-
struct FixedVariable{V<:AbstractVariable, T}
144+
struct FixedVariable{V<:AbstractVariable,T}
121145
variable::V
122146
value::T
123147
end
@@ -130,6 +154,13 @@ end
130154

131155
function Base.intersect(el::FixedVariable; kws...)
132156
subs = Dict(el.variable => el.value)
133-
return FixedVariablesSet(FixedVariablesIdeal{
134-
typeof(el.variable), typeof(el.value), typeof(el.variable)}(subs))
157+
return FixedVariablesSet(
158+
FixedVariablesIdeal{
159+
typeof(el.variable),
160+
typeof(el.value),
161+
typeof(el.variable),
162+
}(
163+
subs,
164+
),
165+
)
135166
end

0 commit comments

Comments
 (0)