Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
PBrdng committed Jan 17, 2024
1 parent f62def3 commit 8d77b7f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
5 changes: 5 additions & 0 deletions docs/src/systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ fix_parameters(F::AbstractSystem, p)
```@docs
RandomizedSystem
```

## Real Systems
```@docs
is_real
```
47 changes: 36 additions & 11 deletions src/certification.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ is_real(C::AbstractSolutionCertificate) = C.real
is_complex(certificate::AbstractSolutionCertificate)
Returns `true` if `certificate` certifies that the certified solution interval
contains a true complex zero of the system.
contains a non-real complex zero of the system.
"""
is_complex(C::AbstractSolutionCertificate) = C.complex

Expand Down Expand Up @@ -236,6 +236,7 @@ function squared_distance_interval(
n = length(solution_candidate(cert))
d = zero(IntervalArithmetic.Interval{Float64})
for i = 1:n
@show cert

Check warning on line 239 in src/certification.jl

View check run for this annotation

Codecov / codecov/patch

src/certification.jl#L239

Added line #L239 was not covered by tests
yᵢ = IComplexF64(cert.I[i], a, b)
d +=
IntervalArithmetic.sqr(real(yᵢ) - real(reference_point[i])) +
Expand Down Expand Up @@ -320,7 +321,6 @@ function add_certificate!(
cert::AbstractSolutionCertificate,
)
d = squared_distance_interval(cert, distinct_sols.reference_point)

for match in IntervalTrees.intersect(distinct_sols.distinct_tree, d)
certᵢ = IntervalTrees.value(match)
if Bool(Arblib.overlaps(cert.I, certᵢ.I))
Expand Down Expand Up @@ -752,6 +752,8 @@ function _certify(
throw(ArgumentError("The given system expects parameters but none are given."))
end

is_real_system = ModelKit.is_real(F)

Check warning on line 755 in src/certification.jl

View check run for this annotation

Codecov / codecov/patch

src/certification.jl#L755

Added line #L755 was not covered by tests

distinct_sols =
DistinctSolutionCertificates(n; extended_certificate = extended_certificate)
ncertified = Threads.Atomic{Int}(0)
Expand Down Expand Up @@ -793,7 +795,8 @@ function _certify(
s,
Tp[tid],
Tcache[tid],
i;
i,
is_real_system;
extended_certificate = extended_certificate,
certify_solution_kwargs...,
)
Expand Down Expand Up @@ -853,7 +856,8 @@ function _certify(
s,
p,
cache,
i;
i,
is_real_system;
extended_certificate = extended_certificate,
certify_solution_kwargs...,
)
Expand Down Expand Up @@ -936,7 +940,8 @@ function certify_solution(
solution_candidate::AbstractVector,
cert_params::Union{Nothing,CertificationParameters},
cert_cache::CertificationCache,
index::Int;
index::Int,
is_real_system::Bool;
max_precision::Int = 256,
refine_solution::Bool = true,
extended_certificate::Bool = false,
Expand Down Expand Up @@ -966,13 +971,21 @@ function certify_solution(

certified, x₁, x₀, is_real = ε_inflation_krawczyk(x̃₀, cert_params, C, cert_cache)

if !is_real_system
is_real = false

Check warning on line 975 in src/certification.jl

View check run for this annotation

Codecov / codecov/patch

src/certification.jl#L974-L975

Added lines #L974 - L975 were not covered by tests
end

if certified
is_complex = any(xi -> !(0.0 in imag(xi)), x₁)
if is_complex
is_real = false

Check warning on line 981 in src/certification.jl

View check run for this annotation

Codecov / codecov/patch

src/certification.jl#L979-L981

Added lines #L979 - L981 were not covered by tests
end
if extended_certificate
return ExtendedSolutionCertificate(
solution_candidate = solution_candidate,
certified = true,
real = is_real,
complex = any(xi -> !(0.0 in imag(xi)), x₁),
complex = is_complex,
index = index,
prec = 53,
I = AcbMatrix(x₀; prec = 53),
Expand All @@ -986,7 +999,7 @@ function certify_solution(
solution_candidate = solution_candidate,
certified = true,
real = is_real,
complex = any(xi -> !(0.0 in imag(xi)), x₁),
complex = is_complex,
index = index,
prec = 53,
I = AcbMatrix(x₁; prec = 53),
Expand All @@ -1001,7 +1014,8 @@ function certify_solution(
x̃₀,
cert_params,
cert_cache,
index;
index,
is_real_system;
max_precision = max_precision,
extended_certificate = extended_certificate,
)
Expand All @@ -1013,7 +1027,8 @@ function extended_prec_certify_solution(
x̃₀::AbstractVector,
cert_params::Union{Nothing,CertificationParameters},
cert_cache::CertificationCache,
index::Int;
index::Int,
is_real_system::Bool;
max_precision::Int = 256,
extended_certificate::Bool = false,
)
Expand All @@ -1036,9 +1051,17 @@ function extended_prec_certify_solution(
certified, arb_x₁, arb_x₀, is_real =
arb_ε_inflation_krawczyk(arb_x̃₀, cert_params, arb_C, cert_cache; prec = prec)

if !is_real_system
is_real = false

Check warning on line 1055 in src/certification.jl

View check run for this annotation

Codecov / codecov/patch

src/certification.jl#L1054-L1055

Added lines #L1054 - L1055 were not covered by tests
end

if certified
I = AcbMatrix(n, 1; prec = prec)
Arblib.set!(I, arb_x₀)
is_complex = any(xi -> !Arblib.contains_zero(Arblib.imagref(xi)), arb_x₁)
if is_complex
is_real = false

Check warning on line 1063 in src/certification.jl

View check run for this annotation

Codecov / codecov/patch

src/certification.jl#L1061-L1063

Added lines #L1061 - L1063 were not covered by tests
end

if extended_certificate
I′ = AcbMatrix(n, 1; prec = prec)
Expand All @@ -1051,7 +1074,7 @@ function extended_prec_certify_solution(
solution_candidate = solution_candidate,
certified = true,
real = is_real,
complex = any(xi -> !Arblib.contains_zero(Arblib.imagref(xi)), arb_x₁),
complex = is_complex,
index = index,
prec = prec,
I = I,
Expand All @@ -1066,7 +1089,7 @@ function extended_prec_certify_solution(
solution_candidate = solution_candidate,
certified = true,
real = is_real,
complex = any(xi -> !Arblib.contains_zero(Arblib.imagref(xi)), arb_x₁),
complex = is_complex,
index = index,
prec = prec,
I = I,
Expand Down Expand Up @@ -1152,6 +1175,8 @@ function ε_inflation_krawczyk(x̃₀, p::Union{Nothing,CertificationParameters}
sqr_mul!(x₁, M, δx)
x₁ .= (x̃₀ .- Δx₀) .- x₁
certified = all2(isinterior, x₁, x₀)
@show x₀
@show x₁

Check warning on line 1179 in src/certification.jl

View check run for this annotation

Codecov / codecov/patch

src/certification.jl#L1178-L1179

Added lines #L1178 - L1179 were not covered by tests
is_real =
certified ? all2((x₁_i, x₀_i) -> isinterior(conj(x₁_i), x₀_i), x₁, x₀) : false
else
Expand Down
16 changes: 15 additions & 1 deletion src/model_kit/abstract_system_homotopy.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export AbstractSystem, AbstractHomotopy, jacobian
export AbstractSystem, AbstractHomotopy, jacobian, is_real

"""
AbstractSystem
Expand Down Expand Up @@ -75,6 +75,20 @@ function jacobian(F::AbstractSystem, x, p = nothing)
ModelKit.to_smallest_eltype(U)
end

"""
is_real(F::AbstractSystem)
Checks, if `F(ℝⁿ) ⊂ ℝᵐ`, where `(m,n) = size(F)`.
This is an algorithm that works with probability one:
It randomly samples `x ∈ ℝⁿ` and checks if `F(x) ∈ ℝᵐ`.
"""
function is_real(F::T) where {T<:AbstractSystem}
x = randn(Float64, size(F, 2))
u = F(x)
all(ui -> imag(ui) == 0, u)

Check warning on line 88 in src/model_kit/abstract_system_homotopy.jl

View check run for this annotation

Codecov / codecov/patch

src/model_kit/abstract_system_homotopy.jl#L85-L88

Added lines #L85 - L88 were not covered by tests
end


##############
## Homotopy ##
##############
Expand Down

0 comments on commit 8d77b7f

Please sign in to comment.