Ask Your Question
1

Attribute Error gcd

asked 2022-07-04 20:58:55 +0100

1571 gravatar image

updated 2022-07-04 21:38:57 +0100

Max Alekseyev gravatar image

Hello, I am running a program which is doing many computations of gcd of elements of Z[i] (Gaussian integers). It's doing computations of the kind (for many different c and d's):

K.<i> = QuadraticField(-1)
O = K.maximal_order()
u=O(c).gcd(O(d))

where c and d are some non-zero elements of Z[i].

The program strangely breaks a bit randomely (in particular, when I launch it twice it does not break at the same point, i.e. it breaks with different c and d's).

I get an attribute error of the type:

AttributeError                            Traceback (most recent call last)
~/.conda/envs/sage/lib/python3.10/site-packages/sage/rings/number_field/number_field_ideal.py in is_integral(self)
    961         try:
--> 962             return self.__is_integral
    963         except AttributeError:

~/.conda/envs/sage/lib/python3.10/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__getattr__ (build/cythonized/sage/structure/element.c:4826)()
    493         """
--> 494         return self.getattr_from_category(name)
    495 

~/.conda/envs/sage/lib/python3.10/site-packages/sage/structure/element.pyx in sage.structure.element.Element.getattr_from_category (build/cythonized/sage/structure/element.c:4938)()
    506             cls = P._abstract_element_class
--> 507         return getattr_from_other_class(self, cls, name)
    508 

~/.conda/envs/sage/lib/python3.10/site-packages/sage/cpython/getattr.pyx in sage.cpython.getattr.getattr_from_other_class (build/cythonized/sage/cpython/getattr.c:2702)()
    360         dummy_error_message.name = name
--> 361         raise AttributeError(dummy_error_message)
    362     attribute = <object>attr

AttributeError: 'NumberFieldFractionalIdeal' object has no attribute '_NumberFieldIdeal__pari_hnf'

Even more weird, when I try to compute separetely the gcd with the same c and d's that lead to this error, there is no problem. It does not make sense to me. My server has SageMath version 9.5, Release Date: 2022-01-30 Using Python 3.10.5.

Thanks for your help!

edit retag flag offensive close merge delete

Comments

Do you have an example code that reproduces this issue?

Max Alekseyev gravatar imageMax Alekseyev ( 2022-07-04 21:42:38 +0100 )edit

Not really. It is part of a large code of mine. But even if I launch it twice on the machine with the same parameters, I will get an error at a different stage of the program...

1571 gravatar image1571 ( 2022-07-04 21:50:37 +0100 )edit

You should try to write a simple code that reproduces the error, otherwise we can not help.

tmonteil gravatar imagetmonteil ( 2022-07-05 11:36:52 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-07-08 19:23:21 +0100

dan_fulea gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-07-04 20:58:55 +0100

Seen: 282 times

Last updated: Jul 08 '22