Skip to content

Commit 27b147a

Browse files
authored
Fallback for weird LAPACK error (#43)
* Fallback for weird LAPACK error * Fix format
1 parent ef60a05 commit 27b147a

File tree

5 files changed

+129
-75
lines changed

5 files changed

+129
-75
lines changed

src/schur.jl

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,26 @@ function condition_number(sf::Schur, I)
88
for i in I
99
select[i] = 1
1010
end
11-
return LinearAlgebra.LAPACK.trsen!(
12-
'E',
13-
'N',
14-
select,
15-
copy(sf.T),
16-
copy(sf.Z),
17-
)[4]
11+
try
12+
return LinearAlgebra.LAPACK.trsen!(
13+
'E',
14+
'N',
15+
select,
16+
copy(sf.T),
17+
copy(sf.Z),
18+
)[4]
19+
catch err
20+
if err isa LinearAlgebra.LAPACKException
21+
ε = eps(real(eltype(sf.values)))
22+
@warn(
23+
"`LAPACK.trsen!` throwed an exception for `$(I)` so using default tolerance ``"
24+
)
25+
# Not sure why this happens, see `test/schur` for an example
26+
return ε
27+
else
28+
rethrow(err)
29+
end
30+
end
1831
end
1932

2033
# Manocha, D. & Demmel, J. Algorithms for intersecting parametric and algebraic curves II: multiple intersections

test/commutativetests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include("macro.jl")
22
include("groebner.jl")
3+
include("schur.jl")
34
include("solve.jl")
45
include("variety.jl")

test/schur.jl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module TestSchur
2+
3+
using Test
4+
5+
include("utils.jl")
6+
7+
function test_lapack_exception()
8+
A = [
9+
-1.96262e-16 0 0 0
10+
-1.17757e-16 0 0 0
11+
0 0 0 0
12+
0 0 0 0
13+
]
14+
B = [
15+
0 0 0 -3.92523e-17
16+
1 0 0 -1.17757e-16
17+
0 1 0 0
18+
0 0 1 0
19+
]
20+
λ = [0.285173013664907, 0.714826986335093]
21+
# Throws `LAPACKException(1)`
22+
el = SS._solve_multiplication_matrices([A, B], λ, schur_solver)
23+
testelements(el, [[0, 0]]; atol = 1e-10)
24+
return
25+
end
26+
27+
function _test_cgt96_e51(solver)
28+
ɛ = 1e-4
29+
= [
30+
1-ɛ 0
31+
0 1+ɛ
32+
]
33+
J = [
34+
0 1
35+
1 0
36+
]
37+
Z = zeros(2, 2)
38+
A = [
39+
Iɛ Z
40+
Z J
41+
]
42+
B = [
43+
J Z
44+
Z Iɛ
45+
]
46+
α = 0.219
47+
testelements(
48+
SS._solve_multiplication_matrices([A, B], [α, 1 - α], solver),
49+
[[1.0, -1.0], [1.0, 1.0], [-1.0, 1.0]];
50+
rtol = 1e-7,
51+
)
52+
return
53+
end
54+
55+
# Example 5.1 of CGT97
56+
function test_cgt96_e51()
57+
@testset "Schur" begin
58+
_test_cgt96_e51(schur_solver)
59+
end
60+
# @testset "Newton" begin
61+
# _test_cgt96_e51(newton_solver)
62+
# end
63+
end
64+
65+
function runtests(args...)
66+
for name in names(@__MODULE__; all = true)
67+
if startswith("$name", "test_")
68+
@testset "$(name)" begin
69+
getfield(@__MODULE__, name)(args...)
70+
end
71+
end
72+
end
73+
end
74+
75+
end
76+
77+
TestSchur.runtests()

test/solve.jl

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,7 @@ end
3030
@test sort(sort.(clusters); by = first) == [[1, 5, 6], [2]]
3131
end
3232

33-
function testelements(X, Y; atol = Base.rtoldefault(Float64), kwargs...)
34-
@test length(X) == length(Y)
35-
for y in Y
36-
@test any(x -> isapprox(x, y; atol = atol, kwargs...), X)
37-
end
38-
end
39-
function testelementstypes(X, Y; kwargs...)
40-
testelements(X, Y; kwargs...)
41-
for T in [Rational{Int}, Float64]
42-
if X isa FixedVariablesSet
43-
U = T
44-
else
45-
U = float(T)
46-
end
47-
V = similar(X, T)
48-
testelements(V, Y; kwargs...)
49-
@test eltype(V) == Vector{U}
50-
@test collect(V) isa Vector{Vector{U}}
51-
end
52-
end
53-
54-
# We use a fixed RNG in the tests to decrease nondeterminism. There is still nondeterminism in LAPACK though
55-
using Random
56-
schur_solver = ReorderedSchurMultiplicationMatricesSolver(
57-
sqrt(eps(Float64)),
58-
MersenneTwister(0),
59-
)
60-
newton_solver = NewtonTypeDiagonalization()
33+
include("utils.jl")
6134

6235
function zero_dimensional_ideal(solver)
6336
Mod.@polyvar x y z
@@ -141,46 +114,6 @@ end
141114
projective_zero_dimensional_ideal(newton_solver)
142115
end
143116

144-
function cgt96_e51(solver)
145-
ɛ = 1e-4
146-
= [
147-
1-ɛ 0
148-
0 1+ɛ
149-
]
150-
J = [
151-
0 1
152-
1 0
153-
]
154-
Z = zeros(2, 2)
155-
A = [
156-
Iɛ Z
157-
Z J
158-
]
159-
B = [
160-
J Z
161-
Z Iɛ
162-
]
163-
α = 0.219
164-
return testelements(
165-
SemialgebraicSets._solve_multiplication_matrices(
166-
[A, B],
167-
[α, 1 - α],
168-
solver,
169-
),
170-
[[1.0, -1.0], [1.0, 1.0], [-1.0, 1.0]];
171-
rtol = 1e-7,
172-
)
173-
end
174-
175-
@testset "Example 5.1 of CGT97" begin
176-
@testset "Schur" begin
177-
cgt96_e51(schur_solver)
178-
end
179-
# @testset "Newton" begin
180-
# cgt96_e51(newton_solver)
181-
# end
182-
end
183-
184117
function md95_e43(solver)
185118
Mod.@polyvar x y
186119
V = @set x^2 + 4y^4 == 4y^2 && 3x^4 + y^2 == 5x^3 solver

test/utils.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import SemialgebraicSets as SS
2+
3+
function testelements(X, Y; atol = Base.rtoldefault(Float64), kwargs...)
4+
@test length(X) == length(Y)
5+
for y in Y
6+
@test any(x -> isapprox(x, y; atol = atol, kwargs...), X)
7+
end
8+
end
9+
function testelementstypes(X, Y; kwargs...)
10+
testelements(X, Y; kwargs...)
11+
for T in [Rational{Int}, Float64]
12+
if X isa FixedVariablesSet
13+
U = T
14+
else
15+
U = float(T)
16+
end
17+
V = similar(X, T)
18+
testelements(V, Y; kwargs...)
19+
@test eltype(V) == Vector{U}
20+
@test collect(V) isa Vector{Vector{U}}
21+
end
22+
end
23+
24+
# We use a fixed RNG in the tests to decrease nondeterminism. There is still nondeterminism in LAPACK though
25+
using Random
26+
schur_solver = SS.ReorderedSchurMultiplicationMatricesSolver(
27+
sqrt(eps(Float64)),
28+
MersenneTwister(0),
29+
)
30+
newton_solver = SS.NewtonTypeDiagonalization()

0 commit comments

Comments
 (0)