# Defining Polynomial from Import

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];
f.change_ring(K);
f.is_irreducible()


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

edit retag close merge delete

Sort by ยป oldest newest most voted

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'


whereas

sage: p_pol.is_irreducible()
False
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()
True

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?

( 2019-04-20 20:34:50 +0200 )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.

( 2019-04-20 20:38:53 +0200 )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.

( 2019-04-20 20:55:21 +0200 )edit