ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 06 Jan 2018 11:16:35 -0600Using Singular's solve.libhttp://ask.sagemath.org/question/40490/using-singulars-solvelib/ I am trying to use Singular's `solve.lib` from Sage, but I am running into issues with getting the result out of Singular. I tried using the Singular interface and was able to make it this far:
sage: singular.lib('solve.lib')
sage: r = singular.ring('complex', '(x,y)', 'lp')
sage: I = singular.ideal('(x-5)*y/32', 'y^2-x^3-5*x-10')
sage: R = singular.fglm_solve(I)
sage: singular.setring(R)
at which point the result is stored in a list `rlist` inside of `R`. I can do `singular.eval('rlist')` and see the answer, but in order to get it back into Sage I would have to parse the resulting string. Is there a better way to get `rlist`?
Alternatively, I tried to use `libSingular` as follows:
sage: from sage.libs.singular.function import singular_function, lib
sage: lib('solve.lib')
sage: fglm_solve = singular_function('fglm_solve')
sage: P = PolynomialRing(QQ,'x,y',order='lex'); x,y = P.gens()
sage: I = Ideal((x-5)*y/32, y^2-x^3-5*x-10)
sage: R = fglm_solve(I)
// 'fglm_solve' created a ring, in which a list rlist of numbers (the
// complex solutions) is stored.
// To access the list of complex solutions, type (if the name R was assigned
// to the return value):
setring R; rlist;
<RingWrap>
But then `R` is a `<RingWrap>` instance and this time I don't even know how to go about seeing the answer. Also, I would prefer to work over the complex field, but I couldn't get `fglm_solve` to accept an ideal in `CC['x,y']`
Any help in either direction would be greatly appreciated.Fri, 05 Jan 2018 21:35:56 -0600http://ask.sagemath.org/question/40490/using-singulars-solvelib/Answer by jaebond for <p>I am trying to use Singular's <code>solve.lib</code> from Sage, but I am running into issues with getting the result out of Singular. I tried using the Singular interface and was able to make it this far:</p>
<pre><code>sage: singular.lib('solve.lib')
sage: r = singular.ring('complex', '(x,y)', 'lp')
sage: I = singular.ideal('(x-5)*y/32', 'y^2-x^3-5*x-10')
sage: R = singular.fglm_solve(I)
sage: singular.setring(R)
</code></pre>
<p>at which point the result is stored in a list <code>rlist</code> inside of <code>R</code>. I can do <code>singular.eval('rlist')</code> and see the answer, but in order to get it back into Sage I would have to parse the resulting string. Is there a better way to get <code>rlist</code>?</p>
<p>Alternatively, I tried to use <code>libSingular</code> as follows:</p>
<pre><code>sage: from sage.libs.singular.function import singular_function, lib
sage: lib('solve.lib')
sage: fglm_solve = singular_function('fglm_solve')
sage: P = PolynomialRing(QQ,'x,y',order='lex'); x,y = P.gens()
sage: I = Ideal((x-5)*y/32, y^2-x^3-5*x-10)
sage: R = fglm_solve(I)
// 'fglm_solve' created a ring, in which a list rlist of numbers (the
// complex solutions) is stored.
// To access the list of complex solutions, type (if the name R was assigned
// to the return value):
setring R; rlist;
<RingWrap>
</code></pre>
<p>But then <code>R</code> is a <code><RingWrap></code> instance and this time I don't even know how to go about seeing the answer. Also, I would prefer to work over the complex field, but I couldn't get <code>fglm_solve</code> to accept an ideal in <code>CC['x,y']</code></p>
<p>Any help in either direction would be greatly appreciated.</p>
http://ask.sagemath.org/question/40490/using-singulars-solvelib/?answer=40507#post-id-40507Looking in `src/sage/interfaces/singular.py` as suggested by @tmonteil, I noticed in some of the docstrings that the way to get at a Singular object is `singular('obj_name')`. Continuing the first example from the question, `rlist` can be accessed with `singular('rlist')`. Although it is still a Singular list, it can be indexed into, iterated over, etc. So something like the following will work:
sage: [[coord for coord in sol] for sol in singular('rlist')]
Again, the elements are still Singular objects, but one (perhaps rudimentary) way to convert them is
sage: [[sage_eval(coord._sage_repr()) for coord in sol] for sol in singular('rlist')]
Perhaps more robust than `sage_eval` and `._sage_repr()` would be using `.repart()` and `.impart()` (which are the Singular functions for real and imaginary part):
sage: [[ComplexNumber(coord.repart(), coord.impart()) for coord in sol] for sol in singular('rlist')]
Note that `ComplexNumber` has an optional `min_prec` argument to control the precision of the `ComplexField` used to create the number, so that if the computations are performed at a high precision in Singular, the same precision can be ensured in Sage.Sat, 06 Jan 2018 11:16:35 -0600http://ask.sagemath.org/question/40490/using-singulars-solvelib/?answer=40507#post-id-40507Answer by tmonteil for <p>I am trying to use Singular's <code>solve.lib</code> from Sage, but I am running into issues with getting the result out of Singular. I tried using the Singular interface and was able to make it this far:</p>
<pre><code>sage: singular.lib('solve.lib')
sage: r = singular.ring('complex', '(x,y)', 'lp')
sage: I = singular.ideal('(x-5)*y/32', 'y^2-x^3-5*x-10')
sage: R = singular.fglm_solve(I)
sage: singular.setring(R)
</code></pre>
<p>at which point the result is stored in a list <code>rlist</code> inside of <code>R</code>. I can do <code>singular.eval('rlist')</code> and see the answer, but in order to get it back into Sage I would have to parse the resulting string. Is there a better way to get <code>rlist</code>?</p>
<p>Alternatively, I tried to use <code>libSingular</code> as follows:</p>
<pre><code>sage: from sage.libs.singular.function import singular_function, lib
sage: lib('solve.lib')
sage: fglm_solve = singular_function('fglm_solve')
sage: P = PolynomialRing(QQ,'x,y',order='lex'); x,y = P.gens()
sage: I = Ideal((x-5)*y/32, y^2-x^3-5*x-10)
sage: R = fglm_solve(I)
// 'fglm_solve' created a ring, in which a list rlist of numbers (the
// complex solutions) is stored.
// To access the list of complex solutions, type (if the name R was assigned
// to the return value):
setring R; rlist;
<RingWrap>
</code></pre>
<p>But then <code>R</code> is a <code><RingWrap></code> instance and this time I don't even know how to go about seeing the answer. Also, I would prefer to work over the complex field, but I couldn't get <code>fglm_solve</code> to accept an ideal in <code>CC['x,y']</code></p>
<p>Any help in either direction would be greatly appreciated.</p>
http://ask.sagemath.org/question/40490/using-singulars-solvelib/?answer=40499#post-id-40499I will not solve your interface problem, but you could search for inspiration in the file `src/sage/interfaces/singular.py`.
I would however suggest the Sage way:
You can get the set of somutions by looking to the variety of the ideal:
sage: I
Ideal (1/32*x*y - 5/32*y, -x^3 - 5*x + y^2 - 10) of Multivariate Polynomial Ring in x, y over Rational Field
sage: I.variety()
[]
As you can see, there is no rational solution. So, let me suggest to solve the system over the algebraic field:
sage: P = PolynomialRing(QQbar,'x,y',order='lex'); x,y = P.gens()
sage: I = Ideal((x-5)*y/32, y^2-x^3-5*x-10)
sage: I
Ideal (1/32*x*y + (-5/32)*y, -x^3 + (-5)*x + y^2 - 10) of Multivariate Polynomial Ring in x, y over Algebraic Field
sage: I.variety()
[{y: -12.64911064067352?, x: 5},
{y: 0, x: -1.423318344753072?},
{y: 0, x: 0.7116591723765360? - 2.553306940593506?*I},
{y: 0, x: 0.7116591723765360? + 2.553306940593506?*I},
{y: 12.64911064067352?, x: 5}]
Sat, 06 Jan 2018 08:57:15 -0600http://ask.sagemath.org/question/40490/using-singulars-solvelib/?answer=40499#post-id-40499Comment by tmonteil for <p>I will not solve your interface problem, but you could search for inspiration in the file <code>src/sage/interfaces/singular.py</code>.</p>
<p>I would however suggest the Sage way:</p>
<p>You can get the set of somutions by looking to the variety of the ideal:</p>
<pre><code>sage: I
Ideal (1/32*x*y - 5/32*y, -x^3 - 5*x + y^2 - 10) of Multivariate Polynomial Ring in x, y over Rational Field
sage: I.variety()
[]
</code></pre>
<p>As you can see, there is no rational solution. So, let me suggest to solve the system over the algebraic field:</p>
<pre><code>sage: P = PolynomialRing(QQbar,'x,y',order='lex'); x,y = P.gens()
sage: I = Ideal((x-5)*y/32, y^2-x^3-5*x-10)
sage: I
Ideal (1/32*x*y + (-5/32)*y, -x^3 + (-5)*x + y^2 - 10) of Multivariate Polynomial Ring in x, y over Algebraic Field
sage: I.variety()
[{y: -12.64911064067352?, x: 5},
{y: 0, x: -1.423318344753072?},
{y: 0, x: 0.7116591723765360? - 2.553306940593506?*I},
{y: 0, x: 0.7116591723765360? + 2.553306940593506?*I},
{y: 12.64911064067352?, x: 5}]
</code></pre>
http://ask.sagemath.org/question/40490/using-singulars-solvelib/?comment=40505#post-id-40505Indeed, for the algebraic field, the computation is done by Sage itself, not singular.Sat, 06 Jan 2018 11:13:05 -0600http://ask.sagemath.org/question/40490/using-singulars-solvelib/?comment=40505#post-id-40505Comment by jaebond for <p>I will not solve your interface problem, but you could search for inspiration in the file <code>src/sage/interfaces/singular.py</code>.</p>
<p>I would however suggest the Sage way:</p>
<p>You can get the set of somutions by looking to the variety of the ideal:</p>
<pre><code>sage: I
Ideal (1/32*x*y - 5/32*y, -x^3 - 5*x + y^2 - 10) of Multivariate Polynomial Ring in x, y over Rational Field
sage: I.variety()
[]
</code></pre>
<p>As you can see, there is no rational solution. So, let me suggest to solve the system over the algebraic field:</p>
<pre><code>sage: P = PolynomialRing(QQbar,'x,y',order='lex'); x,y = P.gens()
sage: I = Ideal((x-5)*y/32, y^2-x^3-5*x-10)
sage: I
Ideal (1/32*x*y + (-5/32)*y, -x^3 + (-5)*x + y^2 - 10) of Multivariate Polynomial Ring in x, y over Algebraic Field
sage: I.variety()
[{y: -12.64911064067352?, x: 5},
{y: 0, x: -1.423318344753072?},
{y: 0, x: 0.7116591723765360? - 2.553306940593506?*I},
{y: 0, x: 0.7116591723765360? + 2.553306940593506?*I},
{y: 12.64911064067352?, x: 5}]
</code></pre>
http://ask.sagemath.org/question/40490/using-singulars-solvelib/?comment=40504#post-id-40504When I tried `I.variety()`, Sage printed `verbose 0 (3324: multi_polynomial_ideal.py, groebner_basis) Warning: falling back to very slow toy implementation.`, so it looks like Sage is still using Groebner bases, just with a slower implementation. Is this accurate?Sat, 06 Jan 2018 11:03:29 -0600http://ask.sagemath.org/question/40490/using-singulars-solvelib/?comment=40504#post-id-40504