Clearing/creating a ring in a loop?

asked 0 years ago

susan_in_Annapolis gravatar image

updated 0 years ago

slelievre gravatar image

I am having the ERROR x7 already used error in sage. I have something like this:

for i in range(0, 4):
     R = gen_ring(special flags, i)
     do_stuff_to_ring(R)

This code basically creates a ring with the variables ordered in weird ways. Like when i = 0, the order is x11, x2, x7... And the next time, the order would be x13, x7, x9, x2... or something like that, and the dimension of the ring variables is different too. The problem is that when I call gen_ring the SECOND time, when i = 1, it says that the ring already has a variable x7.

I've tried writing a special function to clear the variables, and I did reset(var) for every variable in the ring. I tried del var, and del ring, and also reset(R), but that did not work. I didn't try R = None. I will tomorrow! I am just looking for how to wipe the variables out of the namespace. If I do reset(), ALL the variables are cleared, including the special flags, and the loop counter even. Blergh!

Hopefully some one can help? This cannot be a usual thing to want to do...

Preview: (hide)

Comments

It is difficult to help without any specifics. Please add some (more or less minimal) complete runnable code that reproduces the error. If gen_ring creates the ring using strings for the names of generators, and do_stuff_to_ring(R) accesses the generators of R via R.gens() or R.gen(j) or R(gen_name) then it should work.

rburing gravatar imagerburing ( 0 years ago )

I certainly appreciate that comment. I'll see if I can draft a minimal example. The rings are created with the R = PolynomialRing(ZZ, my_order), and then R.inject_variables(). I believe it is the inject_variables that is creating the problem. But I have a list, and so I can't create the ring via R.<my_order> = PolynomialRing(). I appreciate that you commented, and I will try to get a working example.

susan_in_Annapolis gravatar imagesusan_in_Annapolis ( 0 years ago )

Okay, here is what I thought the minimal example was. But this is working in my Sage notebook. But the actual code base I am working in is a large series of sage files. Would that effect how variables are stored in the namespace, a notebook versus the sage environment?

def make_ring(flag):
    if flag == 0:
        R = PolynomialRing(ZZ, names = ['x3', 'x7', 'x12'], order = 'lex')
        R.inject_variables()
    else:
        R = PolynomialRing(ZZ, names = ['x7', 'x3', 'x4', 'x5'], order = 'lex')
        R.inject_variables()
    return R

for i in range(0, 2):
    R = make_ring(i)
    print(R)
susan_in_Annapolis gravatar imagesusan_in_Annapolis ( 0 years ago )

I'm not sure yet what the issue is. Could you please post the full error you get in your actual script? (You can edit your question and add the error at the end. To format something using a monospace font, select it and press the code button labeled 101010, next to the link button.)

rburing gravatar imagerburing ( 0 years ago )

I suggest not to use "inject_variables", but the methods "gen" and "gens".

FrédéricC gravatar imageFrédéricC ( 0 years ago )