HomotopyContinuation.jl icon indicating copy to clipboard operation
HomotopyContinuation.jl copied to clipboard

InexactError on certain operating systems

Open NicholasWang9 opened this issue 5 months ago • 4 comments

Below is an example of code that throws an InexactError. I use Windows and see this error all the time while my coworkers using Mac have never experienced this. This error is intermittent and if I run the code enough times, it will eventually work (Some files take about 10-20 attempts to run while others don't run with 50+ attempts). From what I can tell, the failure rate increases with higher mixed_volumes.

using HomotopyContinuation

@var x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12
variablelist = [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12]

eqs = vcat(x1 * x2 * x3 * [x10, x11, x12] + x4 * x5 * x6 * [x7, x8, x9],
        x1 * x2 * x4 * [x10, x11, x12] + x3 * x5 * x6 * [x7, x8, x9],
        x1 * x2 * x5 * [x10, x11, x12] + x3 * x4 * x6 * [x7, x8, x9],
        x1 * x2 * x6 * [x10, x11, x12] + x3 * x4 * x5 * [x7, x8, x9],
        x1 * x3 * x4 * [x10, x11, x12] + x2 * x5 * x6 * [x7, x8, x9],
        x1 * x3 * x5 * [x10, x11, x12] + x2 * x4 * x6 * [x7, x8, x9],
        x1 * x3 * x6 * [x10, x11, x12] + x2 * x4 * x5 * [x7, x8, x9],
        x1 * x4 * x5 * [x10, x11, x12] + x2 * x3 * x6 * [x7, x8, x9],
        x1 * x4 * x6 * [x10, x11, x12] + x2 * x3 * x5 * [x7, x8, x9],
        x1 * x5 * x6 * [x10, x11, x12] + x2 * x3 * x4 * [x7, x8, x9])

sys = System(eqs,
             variables = variablelist)
homo_sols = HomotopyContinuation.solve(sys)
real_homo_sols = real_solutions(homo_sols)

The number in the function trunc(Int32, x) changes every run The error itself:

Computing mixed cells... 6752    Time: 0:00:08
   mixed_volume: 43423
ERROR: LoadError: InexactError: trunc(Int32, -496443133757380)
Stacktrace:
  [1] throw_inexacterror(::Symbol, ::Vararg{Any})
    @ Core .\boot.jl:750
  [2] checked_trunc_sint
    @ .\boot.jl:764 [inlined]
  [3] toInt32
    @ .\boot.jl:801 [inlined]
  [4] Int32
    @ .\boot.jl:891 [inlined]
  [5] convert
    @ .\number.jl:7 [inlined]
  [6] cconvert
    @ .\essentials.jl:687 [inlined]
  [7] set_si!
    @ .\gmp.jl:211 [inlined]
  [8] _broadcast_getindex_evalf
    @ .\broadcast.jl:678 [inlined]
  [9] _broadcast_getindex
    @ .\broadcast.jl:651 [inlined]
 [10] getindex
    @ .\broadcast.jl:610 [inlined]
 [11] macro expansion
    @ .\broadcast.jl:973 [inlined]
 [12] macro expansion
    @ .\simdloop.jl:77 [inlined]
 [13] copyto!
    @ .\broadcast.jl:972 [inlined]
 [14] copyto!
    @ .\broadcast.jl:925 [inlined]
 [15] copy
    @ .\broadcast.jl:897 [inlined]
 [16] materialize
    @ .\broadcast.jl:872 [inlined]
 [17] solve!(BSS::HomotopyContinuation.BinomialSystemSolver)
    @ HomotopyContinuation C:\Users\user\.julia\packages\HomotopyContinuation\Cibx5\src\binomial_system.jl:178
 [18] solve
    @ C:\Users\user\.julia\packages\HomotopyContinuation\Cibx5\src\binomial_system.jl:162 [inlined]
 [19] iterate
    @ C:\Users\user\.julia\packages\HomotopyContinuation\Cibx5\src\polyhedral.jl:80 [inlined]
 [20] _collect(cont::UnitRange{Int64}, itr::PolyhedralStartSolutionsIterator, ::Base.HasEltype, isz::Base.SizeUnknown)
    @ Base .\array.jl:729
 [21] collect
    @ .\array.jl:716 [inlined]
 [22] #solve#280
    @ C:\Users\user\.julia\packages\HomotopyContinuation\Cibx5\src\solve.jl:505 [inlined]
 [23] solve(args::System; show_progress::Bool, threading::Bool, catch_interrupt::Bool, target_parameters::Nothing, stop_early_cb::Function, transform_result::Nothing, transform_parameters::typeof(identity), flatten::Nothing, target_subspaces::Nothing, kwargs::@Kwargs{})
    @ HomotopyContinuation C:\Users\user\.julia\packages\HomotopyContinuation\Cibx5\src\solve.jl:491
 [24] solve(args::System)
    @ HomotopyContinuation C:\Users\user\.julia\packages\HomotopyContinuation\Cibx5\src\solve.jl:435
 [25] top-level scope
    @ MBE.jl:19
 [26] include(fname::String)
    @ Main .\sysimg.jl:38
 [27] top-level scope
    @ REPL[1]:1
in expression starting at MBE.jl:19

NicholasWang9 avatar Jun 09 '25 13:06 NicholasWang9

I think the issue may be with the default integer type in the different systems. On Mac it's Int64, on other operating systems it might be Int32, so my guess is that the issue is that the julia command is trying to make large integers, on Mac they're getting rounded correctly, and on non-mac they're not. Randomly, sometimes the random integer is not actually too large, so the computation proceeds as expected. Could the fix be to change the source code and replace Int32 with Int64? Not sure how that would affect everything else.

LukeOeding avatar Jun 09 '25 15:06 LukeOeding

This could be an issue, indeed. But even if you use the correct type, your system seems to large to be handled by solve. Try instead numerical irreducible decomposition . On my computer it runs smoothly!

PBrdng avatar Jun 11 '25 05:06 PBrdng

I am having a similar issue on this and related problems, both in Windows and Ubuntu. However, commenting out the following lines in function solve!(BSS::BinomialSystemSolver) of binomial_system.jl provides a workaround:

        # if !validate_result(BSS)
            # if Int == Int64
            #     MPZ.set_si!.(BSS.H_big, BSS.H)
            #     MPZ.set_si!.(BSS.U_big, BSS.U)
            # else
            #     MPZ.set_si64!.(BSS.H_big, BSS.H)
            #     MPZ.set_si64!.(BSS.U_big, BSS.U)
            # end
            # hnf!(BSS.H_big, BSS.U_big, BSS.A)
            # solve!(BSS, BSS.H_big, BSS.U_big)
        # end

This allows HomotopyContinuation to run on the example every time without issue, but I worry that this isn't a proper fix. It seems to imply that there is a problem with converting Ints to BigInts. The problematic line is MPZ.set_si!.(BSS.H_big, BSS.H).

NID works on this example as well, but it is often much slower than HomotopyContinuation.solve on other problems. The example in the original post doesn't have any nonsingular solutions.

arutkowski avatar Jun 18 '25 19:06 arutkowski

Interesting. I will look into it (note that I'm the only maintainer at this point, so it will take me some time. Sorry...)

PBrdng avatar Jun 23 '25 03:06 PBrdng