Skip to content

Commit

Permalink
More fixes for complex embedding (#1298)
Browse files Browse the repository at this point in the history
  • Loading branch information
thofma authored Nov 25, 2023
1 parent d05f266 commit 324916b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Hecke"
uuid = "3e1990a7-5d81-5526-99ce-9ba3ff248f21"
version = "0.22.7"
version = "0.22.8"

[deps]
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"
Expand Down
31 changes: 16 additions & 15 deletions src/NumField/ComplexEmbeddings/NfAbs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,26 @@ function _find_nearest_complex_embedding(K::AnticNumberField, x)
r = complex_embeddings(K)
diffs = [e(gen(K)) - x for e in r]
t = [abs(z) for z in diffs]
for i in 1:length(t)
for j in (i + 1):length(t)
if overlaps(t[i], t[j])
possible = [ (Float64(real(e.r)), Float64(imag(e.r))) for e in r]
s = IOBuffer()
for i in 1:length(possible)
@printf s "%.2f + i * %.2f" possible[i][1] possible[i][2]
if i < length(possible)
print(s, ", ")
end
_, i = findmin(t)
for j in 1:length(t)
if j == i
continue
end
if overlaps(t[i], t[j])
possible = [ (Float64(real(e.r)), Float64(imag(e.r))) for e in r]
s = IOBuffer()
for i in 1:length(possible)
@printf s "%.2f + i * %.2f" possible[i][1] possible[i][2]
if i < length(possible)
print(s, ", ")
end
ss = String(take!(s))
error("""Given approximation not close enough to a root. Possible roots are:
$ss
""")
end
ss = String(take!(s))
error("""Given approximation not close enough to a root. Possible roots are:
$ss
""")
end
end
_, i = findmin(t)
return r[i]
end

Expand Down
47 changes: 24 additions & 23 deletions src/NumField/ComplexEmbeddings/NfAbsNS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,43 +192,44 @@ end

function _find_nearest_complex_embedding(K::NfAbsNS, x)
r = complex_embeddings(K)
t = []
t = Vector{arb}[]
for e in r
embedded_gens = map(e, gens(K))
gen_diffs = map(abs, embedded_gens - x)
push!(t, gen_diffs)
end

_, i = findmin(map(y -> sum(y), t))

check_overlaps = y -> isempty(filter(x -> !overlaps(x...), y))
for i in 1:length(t)
for j in (i + 1):length(t)
if check_overlaps([(t[i][s], t[j][s]) for s in 1:length(gens(K))])
embedded_roots = [e.roots for e in r]
roots_to_tuple = x -> (Float64(real(x)), Float64(imag(x)))
possible = [map(roots_to_tuple, roots) for roots in embedded_roots]
s = IOBuffer()
for k in 1:length(possible)
for w in 1:length(possible[k])
@printf s "%.2f + i * %.2f" possible[k][w][1] possible[k][w][2]
if w < length(possible[k])
for j in 1:length(t)
if j == i
continue
end
if check_overlaps([(t[i][s], t[j][s]) for s in 1:length(gens(K))])
embedded_roots = [e.roots for e in r]
roots_to_tuple = x -> (Float64(real(x)), Float64(imag(x)))
possible = [map(roots_to_tuple, roots) for roots in embedded_roots]
s = IOBuffer()
for k in 1:length(possible)
for w in 1:length(possible[k])
@printf s "%.2f + i * %.2f" possible[k][w][1] possible[k][w][2]
if w < length(possible[k])
print(s, ", ")
end

end
if k < length(possible)
print(s, "\n")
end
end
ss = String(take!(s))
error("""Given approximation not close enough to a vector of roots. \n
Possible vector of roots are:
$ss
""")
if k < length(possible)
print(s, "\n")
end
end
ss = String(take!(s))
error("""Given approximation not close enough to a vector of roots. \n
Possible vector of roots are:
$ss
""")
end
end
_, i = findmin(map(y -> +(y...), t))

return r[i]
end

Expand Down
7 changes: 7 additions & 0 deletions test/NumField/ComplexEmbeddings/NfAbs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,11 @@
# This should be fast
K, a = cyclotomic_field(19^3, cached = false)
@test length(@inferred complex_embeddings(K)) == degree(K)

# Some issue
Qx, x = QQ["x"]
K, a = number_field(x^4 - 8)
e = complex_embedding(K, 1.68)
@test number_field(e) === K
@test is_real(e(a)) && real(e(a)) > 0
end
7 changes: 7 additions & 0 deletions test/NumField/ComplexEmbeddings/NfAbsNS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@
@test Set(complex_embeddings(K)) == Set([r1, r2, r3, r4])
@test_throws ErrorException Hecke.complex_embedding(K, [0.0, 0.0])

# Some issue

Qx, x = QQ["x"]
K, (a,) = number_field([x^4 - 8])
e = complex_embedding(K, [1.68])
@test number_field(e) === K
@test is_real(e(a)) && real(e(a)) > 0
end

2 comments on commit 324916b

@thofma
Copy link
Owner Author

@thofma thofma commented on 324916b Nov 25, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request updated: JuliaRegistries/General/95842

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.22.8 -m "<description of version>" 324916b72c8f05908482a678b82497c31e96adec
git push origin v0.22.8

Please sign in to comment.