diff --git a/src/Normaliz.jl b/src/Normaliz.jl index 8e4fcc1..65174d3 100644 --- a/src/Normaliz.jl +++ b/src/Normaliz.jl @@ -22,6 +22,10 @@ function Base.convert(::Type{BigInt},x::NmzInteger) return result end Base.convert(::Type{NmzRational},x::Int64) = NmzRational(x,1) +Base.convert(::Type{NmzRational},x::BigInt) = + NmzRational(convert(NmzInteger,x), NmzInteger(1)) +Base.convert(::Type{NmzRational},x::NmzInteger) = + NmzRational(x, NmzInteger(1)) Base.://(x::NmzInteger,y::NmzInteger) = NmzRational(x,y) Base.convert(::Type{NmzRational},x::Rational{BigInt}) = convert(NmzInteger,x.num) // convert(NmzInteger,x.den) @@ -99,17 +103,37 @@ get_boolean_cone_property(cone, property::Symbol) = function _normaliz_input(input::AbstractDict) input_pairs = collect(pairs(input)) input_keys = String[] - for (key, _) in input_pairs + input_matrices = NmzMatrix{NmzRational}[] + for (key, matrix) in input_pairs key isa Symbol || throw(ArgumentError("Normaliz cone input keys must be Symbols")) push!(input_keys, String(key)) + push!(input_matrices, _normaliz_input_matrix(key, matrix)) end input_keys = CxxWrap.StdLib.StdVector{CxxWrap.StdLib.StdString}(input_keys) - input_matrices = CxxRef.([matrix for (_, matrix) in input_pairs]) + input_matrices = CxxRef.(input_matrices) return input_keys, input_matrices end -function NmzMatrix{T}(x::Matrix{S}) where S where T +_normaliz_input_matrix(key::Symbol, matrix::NmzMatrix{NmzRational}) = matrix + +function _normaliz_input_matrix(key::Symbol, matrix::AbstractMatrix) + try + return NmzMatrix{NmzRational}(matrix) + catch err + throw(ArgumentError( + "Normaliz cone input value for :$key must be convertible " * + "to NmzMatrix{NmzRational}")) + end +end + +function _normaliz_input_matrix(key::Symbol, value) + throw(ArgumentError( + "Normaliz cone input values must be matrices; " * + "value for :$key has type $(typeof(value))")) +end + +function NmzMatrix{T}(x::AbstractMatrix{S}) where S where T s = size(x) mat = NmzMatrix{T}(s[1],s[2]) for i in 1:s[1], j in 1:s[2] diff --git a/test/runtests.jl b/test/runtests.jl index ec4ad58..91cb84b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -60,6 +60,35 @@ end @test string(input_matrices[cone_index][][1, 1]) == "1/2" @test size(input_matrices[grading_index][]) == (1, 2) + input_keys, input_matrices = Normaliz._normaliz_input( + Dict(:cone => [1//2 2 ; 3 5], :grading => [1 1])) + input_key_strings = collect(input_keys) + cone_index = findfirst(==("cone"), input_key_strings) + grading_index = findfirst(==("grading"), input_key_strings) + @test input_matrices[cone_index][] isa Normaliz.NmzMatrix{Normaliz.NmzRational} + @test string(input_matrices[cone_index][][1, 1]) == "1/2" + @test size(input_matrices[grading_index][]) == (1, 2) + + large_integer = big(2)^80 + 1 + input_keys, input_matrices = Normaliz._normaliz_input( + Dict(:cone => BigInt[large_integer 2; 3 4])) + @test string(input_matrices[1][][1, 1]) == string(large_integer) + + integer_matrix = Normaliz.NmzMatrix{Normaliz.NmzInteger}([1 2 ; 3 4]) + input_keys, input_matrices = Normaliz._normaliz_input(Dict(:cone => integer_matrix)) + @test input_matrices[1][] isa Normaliz.NmzMatrix{Normaliz.NmzRational} + @test string(input_matrices[1][][1, 1]) == "1" + + yy = Normaliz.LongLongCone(Dict(:cone => [1//2 2 ; 3 5], :grading => [1 1])) + @test yy isa Normaliz.Cone + + err = @test_throws ArgumentError Normaliz._normaliz_input(Dict(:cone => "not a matrix")) + @test occursin("cone", err.value.msg) + @test occursin("Normaliz cone input values", err.value.msg) + + err = @test_throws ArgumentError Normaliz._normaliz_input(Dict(:cone => ["x" ;;])) + @test occursin("NmzRational", err.value.msg) + mismatched_keys = CxxWrap.StdLib.StdVector{CxxWrap.StdLib.StdString}(["cone", "grading"]) err = @test_throws ErrorException Normaliz._LongLongCone(mismatched_keys, input_matrices[1:1]) @test err.value.msg == "Normaliz cone input keys and matrices must have the same length"