Ask Your Question

Defining Polynomial from Import

asked 2019-04-19 23:16:46 -0500

nmbthr gravatar image

I am importing polynomials from LMFBD. So I have a set called data, which contains a polynomial in certain entries. So for example, data[0][0] may be a polynomial $x^2+x+1$. I want to check if this polynomial is irreducible over some number field $K$ I have defined. So I tried something like...

K = NumberField(y^2+1);
f = data[0][0];

But I get the error 'sage.symbolic.expression.Expression' object has no attribute 'is_irreducible'. How would I do this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-04-20 13:28:03 -0500

vdelecroix gravatar image

updated 2019-04-20 13:48:06 -0500

It is not clear how you constructed the data and you might have done it the wrong way. Your data does not seem to be a polynomial but a symbolic expression. Notice the difference in construction between

sage: x = SR.var('x')
sage: p_symb = x^2 + x - 2
sage: x = polygen(QQ)
sage: p_pol = x^2 + x - 2

Or similarly

sage: p_symb2 = SR('x^2 + x - 2')
sage: p_pol2 = QQ['x']('x^2 + x - 2')

You can check that these are indeed different

sage: parent(p_symb)
Symbolic Ring
sage: parent(p_symb2)
Symbolic Ring
sage: parent(p_pol)
Univariate Polynomial Ring in x over Rational Field
sage: parent(p_pol2)
Univariate Polynomial Ring in x over Rational Field

Then, as you already experienced, the symbolic version has no is_irreducible method

sage: p_symb.is_irreducible()
Traceback (most recent call last):
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'is_irreducible'


sage: p_pol.is_irreducible()
sage: p_pol.factor()
(x - 1) * (x + 2)

Considering your input given as a list of lists of strings, you should do something like

sage: data = [ ['x^2 + x + 1', 'x^2 - 1'], ['x - 1', 'x', 'x^3 - 1']]
sage: R = QQ['x']
sage: p = R(data[0][0])
sage: p
x^2 + x + 1
sage: p.is_irreducible()
edit flag offensive delete link more


I assume you mean I am doing this wrong (which I certainly know is the case) and not that the data is incorrect? The data is correct and imported but certainly coming from a .txt import from LMFBD, I know it could easily be read by Sage as text rather than the polynomial. I suppose the question is how to I pull the data which I have copied pasted and defined as 'data' to be read as a polynomial? Could I do something like f = K['x']['data[0][0]']? Essentially, if I have a polynomial x + 1 that is being read as symbolic in Sage, how do I tell it to instead read/redefine it as a polynomial?

nmbthr gravatar imagenmbthr ( 2019-04-20 13:34:50 -0500 )edit

Yes, sorry for being unclear: your data is certainly correct but the way you import it in Sage is certainly wrong. When you construct a polynomial there are basically two ways that I described above (the "symbolic way" and the "algebraic way"). I updated my answer to make something closer to what you need.

vdelecroix gravatar imagevdelecroix ( 2019-04-20 13:38:53 -0500 )edit

Alright, thank you so much! It had been driving me crazy and I figured it was this type of issue but could not find any documentation on how to convert Sage types.

nmbthr gravatar imagenmbthr ( 2019-04-20 13:55:21 -0500 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2019-04-19 23:16:46 -0500

Seen: 37 times

Last updated: Apr 20