ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 20 Apr 2019 20:55:21 +0200Defining Polynomial from Importhttps://ask.sagemath.org/question/46282/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?
Sat, 20 Apr 2019 06:16:46 +0200https://ask.sagemath.org/question/46282/defining-polynomial-from-import/Answer by vdelecroix for <p>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...</p>
<pre><code>K = NumberField(y^2+1);
f = data[0][0];
f.change_ring(K);
f.is_irreducible()
</code></pre>
<p>But I get the error 'sage.symbolic.expression.Expression' object has no attribute 'is_irreducible'. How would I do this?</p>
https://ask.sagemath.org/question/46282/defining-polynomial-from-import/?answer=46286#post-id-46286It 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()
TrueSat, 20 Apr 2019 20:28:03 +0200https://ask.sagemath.org/question/46282/defining-polynomial-from-import/?answer=46286#post-id-46286Comment by nmbthr for <p>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</p>
<pre><code>sage: x = SR.var('x')
sage: p_symb = x^2 + x - 2
sage: x = polygen(QQ)
sage: p_pol = x^2 + x - 2
</code></pre>
<p>Or similarly</p>
<pre><code>sage: p_symb2 = SR('x^2 + x - 2')
sage: p_pol2 = QQ['x']('x^2 + x - 2')
</code></pre>
<p>You can check that these are indeed different</p>
<pre><code>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
</code></pre>
<p>Then, as you already experienced, the symbolic version has no is_irreducible method</p>
<pre><code>sage: p_symb.is_irreducible()
Traceback (most recent call last):
...
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'is_irreducible'
</code></pre>
<p>whereas</p>
<pre><code>sage: p_pol.is_irreducible()
False
sage: p_pol.factor()
(x - 1) * (x + 2)
</code></pre>
<p>Considering your input given as a list of lists of strings, you should do something like</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/46282/defining-polynomial-from-import/?comment=46290#post-id-46290I 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?Sat, 20 Apr 2019 20:34:50 +0200https://ask.sagemath.org/question/46282/defining-polynomial-from-import/?comment=46290#post-id-46290Comment by vdelecroix for <p>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</p>
<pre><code>sage: x = SR.var('x')
sage: p_symb = x^2 + x - 2
sage: x = polygen(QQ)
sage: p_pol = x^2 + x - 2
</code></pre>
<p>Or similarly</p>
<pre><code>sage: p_symb2 = SR('x^2 + x - 2')
sage: p_pol2 = QQ['x']('x^2 + x - 2')
</code></pre>
<p>You can check that these are indeed different</p>
<pre><code>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
</code></pre>
<p>Then, as you already experienced, the symbolic version has no is_irreducible method</p>
<pre><code>sage: p_symb.is_irreducible()
Traceback (most recent call last):
...
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'is_irreducible'
</code></pre>
<p>whereas</p>
<pre><code>sage: p_pol.is_irreducible()
False
sage: p_pol.factor()
(x - 1) * (x + 2)
</code></pre>
<p>Considering your input given as a list of lists of strings, you should do something like</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/46282/defining-polynomial-from-import/?comment=46291#post-id-46291Yes, 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.Sat, 20 Apr 2019 20:38:53 +0200https://ask.sagemath.org/question/46282/defining-polynomial-from-import/?comment=46291#post-id-46291Comment by nmbthr for <p>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</p>
<pre><code>sage: x = SR.var('x')
sage: p_symb = x^2 + x - 2
sage: x = polygen(QQ)
sage: p_pol = x^2 + x - 2
</code></pre>
<p>Or similarly</p>
<pre><code>sage: p_symb2 = SR('x^2 + x - 2')
sage: p_pol2 = QQ['x']('x^2 + x - 2')
</code></pre>
<p>You can check that these are indeed different</p>
<pre><code>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
</code></pre>
<p>Then, as you already experienced, the symbolic version has no is_irreducible method</p>
<pre><code>sage: p_symb.is_irreducible()
Traceback (most recent call last):
...
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'is_irreducible'
</code></pre>
<p>whereas</p>
<pre><code>sage: p_pol.is_irreducible()
False
sage: p_pol.factor()
(x - 1) * (x + 2)
</code></pre>
<p>Considering your input given as a list of lists of strings, you should do something like</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/46282/defining-polynomial-from-import/?comment=46292#post-id-46292Alright, 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.Sat, 20 Apr 2019 20:55:21 +0200https://ask.sagemath.org/question/46282/defining-polynomial-from-import/?comment=46292#post-id-46292