Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Well, i tried to see if i can randomly reproduce the error using the same gcd function, and an ad-hoc version manufactured by using the factorization inside $\Bbb Z[i]$. Let's see. (I will use $j$ instead of $i$, which is already a good constant in this session...)

import random
import traceback

K.<j> = QuadraticField(-1)
OK = K.maximal_order()

def mygcd(c, d):
    """Factor both c, d in K, take common prime factors to the min power.
    """
    c_dic = dict(factor(c))    # a dict of the shape OK-prime -> its multiplicity in c
    d_dic = dict(factor(d))    # a dict of the shape OK-prime -> its multiplicity in d
    common_primes = list(set(c_dic).intersection(set(d_dic)))   # sets of dic takes the keys
    return prod(p^min(c_dic.get(p, 0), d_dic.get(p, 0)) for p in common_primes)

N = 2000
random.seed(int(2022))    # to be able to reproduce the output below

for trial in range(N):
    c0, c1, d0, d1 = [random.choice([1..100000]) for _ in [1..4]]   # OK has a random_element, but...
    c, d = OK(c0 + c1*j), OK(d0 + d1*j)

    try:
        ur_gcd = c.gcd(d)
        my_gcd = mygcd(c, d)
    except:
        print(f'ERROR :: c = {c} and d = {d}')
        traceback.print_exc()
        continue

    if ur_gcd / my_gcd not in (K(1), K(-1), j, -j):
        print(f'Difference for c = {c} and d = {d}')

    if trial < 5 or ur_gcd.norm() > 100:
        print(f'SAMPLE [{trial:^4}] :: c = {c} and d = {d} :: your gcd is {ur_gcd}')

And letting the code run, we get after some time:

SAMPLE [ 0  ] :: c = 37869*j + 69682 and d = 71534*j + 58014 :: your gcd is 1
SAMPLE [ 1  ] :: c = 76791*j + 40640 and d = 67918*j + 7968 :: your gcd is 1
SAMPLE [ 2  ] :: c = 92446*j + 90644 and d = 89791*j + 54050 :: your gcd is 1
SAMPLE [ 3  ] :: c = 41060*j + 83106 and d = 99746*j + 1607 :: your gcd is 1
SAMPLE [ 4  ] :: c = 40449*j + 56544 and d = 81951*j + 84700 :: your gcd is 1
SAMPLE [104 ] :: c = 97323*j + 1665 and d = 5127*j + 87933 :: your gcd is 3*j - 15
SAMPLE [232 ] :: c = 59669*j + 65809 and d = 59860*j + 97758 :: your gcd is -11*j + 15
SAMPLE [453 ] :: c = 43475*j + 17864 and d = 6830*j + 11211 :: your gcd is -8*j + 13
SAMPLE [543 ] :: c = 40251*j + 9137 and d = 19878*j + 18676 :: your gcd is -3*j - 11
SAMPLE [725 ] :: c = 25170*j + 75333 and d = 86913*j + 61644 :: your gcd is 3*j + 12
SAMPLE [1388] :: c = 13398*j + 11294 and d = 71427*j + 23222 :: your gcd is -4*j - 11
SAMPLE [1511] :: c = 37479*j + 45331 and d = 76297*j + 79313 :: your gcd is 13*j + 13
SAMPLE [1598] :: c = 63681*j + 14113 and d = 84612*j + 34576 :: your gcd is j + 63

(The first five samples, and those with a bigger gcd were printed only.)

In my case, the version shown by the arcolinux / arch one-click-install of sage is:

sage: version()
'SageMath version 9.6, Release Date: 2022-05-15'

To conclude: - There was no traceback error among the many trials. - The two results, the one of the two gcd-computations, the one provided by the gcd-method of an OK-instance, and the one defined ad-hoc by my hand using the unique factorization in the ring of gaussian integers, coincide (up to some unit).

As a final note, please try to use also an import traceback in the code that leads to the erratic breaks, then print in case of a catched exception the values and the types for the two objects involved, c and d.