How to run Macaulay2 code through Sage interface

362 views
Skip to first unread message

Christian Stump

unread,
May 27, 2016, 1:20:52 PM5/27/16
to sage-support
Hi,

is there a way to achieve the below computation directly in Sage, possibly using the M2 interface ?

Thanks, Christian

$ sage -M2
Macaulay2, version 1.4
with packages: ConwayPolynomials, Elimination, IntegralClosure, LLLBases, PrimaryDecomposition, ReesAlgebra, TangentCone

i1 : loadPackage "Points";
i2 : M = matrix{{1,2,3},{4,5,6}};
o2 = | 1 2 3 |
     | 4 5 6 |
              2        3
o2 : Matrix ZZ  <--- ZZ
i3 : R = QQ[x,y,MonomialOrder=>Lex];
i4 : (Q,inG,G) = points(M,R)
              2           3        3      2
o4 = ({1, y, y }, ideal (y , x), {y  - 15y  + 74y - 120, x - y + 3})
o4 : Sequence


William Stein

unread,
May 27, 2016, 1:25:46 PM5/27/16
to sage-support
On Fri, May 27, 2016 at 10:20 AM, Christian Stump
<christi...@gmail.com> wrote:
> Hi,
>
> is there a way to achieve the below computation directly in Sage, possibly
> using the M2 interface ?

YES.

print macaulay2.eval("""
loadPackage "Points";
M = matrix{{1,2,3},{4,5,6}};
R = QQ[x,y,MonomialOrder=>Lex];
(Q,inG,G) = points(M,R)
""")

https://cloud.sagemath.com/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/files/support/2016-05-27-Macaulay2.sagews

>
> Thanks, Christian
>
> $ sage -M2
> Macaulay2, version 1.4
> with packages: ConwayPolynomials, Elimination, IntegralClosure, LLLBases,
> PrimaryDecomposition, ReesAlgebra, TangentCone
>
> i1 : loadPackage "Points";
> i2 : M = matrix{{1,2,3},{4,5,6}};
> o2 = | 1 2 3 |
> | 4 5 6 |
> 2 3
> o2 : Matrix ZZ <--- ZZ
> i3 : R = QQ[x,y,MonomialOrder=>Lex];
> i4 : (Q,inG,G) = points(M,R)
> 2 3 3 2
> o4 = ({1, y, y }, ideal (y , x), {y - 15y + 74y - 120, x - y + 3})
> o4 : Sequence
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-support...@googlegroups.com.
> To post to this group, send email to sage-s...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/d/optout.



--
William (http://wstein.org)

Christian Stump

unread,
May 27, 2016, 1:34:35 PM5/27/16
to sage-support
> YES.

thanks, that's the first step -- I did actually mean so that I get the output as Sage objects. Sorry for not saying that explicitly...

Dima Pasechnik

unread,
May 27, 2016, 10:22:23 PM5/27/16
to sage-support
Hi,
 I spent most of the last week at a Macaulay2 workshop, and actually playing with M2<->Sage interface :-)
 here you are:

On Friday, May 27, 2016 at 6:34:35 PM UTC+1, Christian Stump wrote:
> YES.

thanks, that's the first step -- I did actually mean so that I get the output as Sage objects. Sorry for not saying that explicitly...

sage: macaulay2.eval(""" 
....: loadPackage "Points"; 
....: M = matrix{{1,2,3},{4,5,6}}; 
....: R = QQ[x,y,MonomialOrder=>Lex]; 
....: (Q,inG,G) = points(M,R) 
....: """) 
sage: M=macaulay2('M').to_sage(); M
[1 2 3]
[4 5 6]
sage: type(M)
<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>
 sage: R=macaulay2('R').to_sage(); R
Multivariate Polynomial Ring in x, y over Rational Field
sage: type(R)
<type 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomialRing_libsingular'>
sage: Q=macaulay2('Q').to_sage(); Q
[1, y, y^2]
sage: type(Q)
<type 'list'>
sage: type(Q[1])
<type 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
sage: inG=macaulay2('inG').to_sage(); inG
Ideal (y^3, x) of Multivariate Polynomial Ring in x, y over Rational Field
sage: G=macaulay2('G').to_sage(); G
[y^3 - 15*y^2 + 74*y - 120, x - y + 3]


HTH,
Dima

Christian Stump

unread,
May 29, 2016, 2:09:10 AM5/29/16
to sage-support
> I spent most of the last week at a Macaulay2 workshop, and actually playing with M2<->Sage interface :-)
> here you are:

Awesome, thanks!

saad khalid

unread,
Jun 2, 2016, 2:37:21 PM6/2/16
to sage-support
Not sure if this is helpful:
http://doc.sagemath.org/html/en/reference/interfaces/sage/interfaces/macaulay2.html

Sage also has some functions that do the same thing as M2 (I believe they run on Singular). For eg, Groebner basis:
http://combinat.sagemath.org/doc/reference/polynomial_rings/sage/rings/polynomial/multi_polynomial_ideal.html

William Stein

unread,
Jun 2, 2016, 3:09:36 PM6/2/16
to sage-support
As explained there you can even use M2 for some GB routines, e.g.,

sage: I = sage.rings.ideal.Katsura(P,3) # regenerate to prevent caching
sage: I.groebner_basis('macaulay2:gb') # optional - macaulay2

saad khalid

unread,
Jun 23, 2016, 12:27:30 PM6/23/16
to sage-support
This is on the same subject, so I thought I'd ask:

I was trying to use the Sage/M2 interface that Dima had been using, but I couldn't quite get it to work for me. Here is what I was trying to convert to sage:
macaulay2.eval("""
K = toField(QQ[zet]/(zet^6 + zet^5 + zet^4 + zet^3 + zet^2 + zet + 1))
A=matrix{{zet,0,0},{0,zet^3,0},{0,0,zet^2}}
needsPackage "
InvariantRing"
G=generateGroup({A},K)
P = molienSeries G
 """
)



I tried doing
G=macaulay2('G').to_sage(); G

A
=macaulay2('A').to_sage(); A

P
=macaulay2('P').to_sage(); P

Though, I ran them all separately. They all gave me error messages. The last one is the most important to me, just for ease of use(typing out long equations and picking out typos seems to take up a large portion of my time...), and the error I get is that I can not convert Divide to a sage object.

Also, it seems to be that most of the functionality of M2 is not integrated into Sage, in the sense that, to use these functions, you have to run M2 within a sage cell, and then you have to use a function to convert it to a sage object. What is preventing us from just fully integrating all the M2 functions into Sage? Obviously still using M2 as a backend, but just to make the interface between the two cleaner. Is there an actual technical issue in the way, or has no one wanted to do it?

Dima Pasechnik

unread,
Jun 23, 2016, 6:49:17 PM6/23/16
to sage-support


On Thursday, June 23, 2016 at 5:27:30 PM UTC+1, saad khalid wrote:
This is on the same subject, so I thought I'd ask:

I was trying to use the Sage/M2 interface that Dima had been using, but I couldn't quite get it to work for me. Here is what I was trying to convert to sage:
macaulay2.eval("""
K = toField(QQ[zet]/(zet^6 + zet^5 + zet^4 + zet^3 + zet^2 + zet + 1))
A=matrix{{zet,0,0},{0,zet^3,0},{0,0,zet^2}}
needsPackage "
InvariantRing"
G=generateGroup({A},K)
P = molienSeries G
 """
)


this works for me.
 

I tried doing
G=macaulay2('G').to_sage(); G

A
=macaulay2('A').to_sage(); A

P
=macaulay2('P').to_sage(); P

Though, I ran them all separately. They all gave me error messages. The last one is the most important to me, just for ease of use(typing out long equations and picking out typos seems to take up a large portion of my time...), and the error I get is that I can not convert Divide to a sage object.

note that you can do this computation (computing Molien series of an ordinary representation of a finite group)
using GAP. This would work much faster (in particular in M2's package InvariantRing, the generateGroup is extermely inefficiently implemented). Although if you are after the ring of invariants itself, then
indeed this would be  the way to go, using M2.

The root of the error you are stuck with seems to be here:

sage: m1=macaulay2('ring numerator P')
sage: m1
ZZ[T, Degrees => {{}}, MonomialOrder => {MonomialSize => 32}, DegreeRank => 0, Inverses => true, Global => false]
                                        {Weights => {-1}   }
                                        {GroupLex => 1     }
                                        {Position => Up    }
sage: m1.to_sage()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-ba2795ae293a> in <module>()
----> 1 m1.to_sage()

/home/dima/software/sage/local/lib/python2.7/site-packages/sage/interfaces/macaulay2.pyc in to_sage(self)
   1101                 # Check that we are dealing with default degrees, i.e. 1's.
   1102                 if self.degrees().any("x -> x != {1}").to_sage():
-> 1103                     raise ValueError("cannot convert Macaulay2 polynomial ring with non-default degrees to Sage")
   1104                 #Handle the term order
   1105                 external_string = self.external_string()

ValueError: cannot convert Macaulay2 polynomial ring with non-default degrees to Sage
sage:

probably not so hard to fix. 


Also, it seems to be that most of the functionality of M2 is not integrated into Sage, in the sense that, to use these functions, you have to run M2 within a sage cell, and then you have to use a function to convert it to a sage object. What is preventing us from just fully integrating all the M2 functions into Sage? Obviously still using M2 as a backend, but just to make the interface between the two cleaner. Is there an actual technical issue in the way, or has no one wanted to do it?

I recently discussed  M2/sage things with M2  devs, in particular with Anton Leykin, and this seems to be
doable to use M2  to provide an alternative (to Singular) commutative algebra backend.

You can also think about implementing more conversions between M2 and Sage objects. Indeed, it's just sheer lack of resources for such a project that prevents it from going forward.

Dima


 

saad khalid

unread,
Jun 24, 2016, 12:39:45 AM6/24/16
to sage-support
Ahh yes, sorry, I misspoke. The first part that I ran in the macaulay2.eval was working fine, it was the sage/m2 conversion that was not working for me.

Oh okay! I will try to use the GAP interface. It's setup is similar to the macaulay2 interface, isn't it? As in, I'll be doing gap.eval("the commands I want to run in GAP"). I'm not really trying to get the ring of invariants, I'm just wanting to get the formula for the molien series, ideally as a sage object so that I won't have to retype the formula again after it is computed. I wasn't really sure how to do the generateGroup command in GAP, though I'm sure it's obvious, I just wasn't sure what to use.

Ahh, I see... maybe it's not sure how to compute the degree when it's of type "Divide". If I get the chance, I'll look into that! Thanks!

If we already have a lot of functionality from Singular directly implemented in Sage, what is left that M2 could do that Singular couldn't? I'm only asking because I thought that Singular and M2 were pretty similar in what they did, I just didn't know how much of singular Sage could directly use (without using an interface like we do with M2 right now). Or, really, I guess the real question is, how well integrated are our algebraic geometry and commutative algebra functions? Is it primarily through interfaces like the Macaulay2 interface above, or is it integrated into Sage, in the same way that, for example, a lot of calculus functions are? If there is a way to do what I am trying to do right now without using the M2 interface and have it automatically work as Sage objects, then I would obviously prefer that. From what I can see, you have to run functions through singular, like singular.ring(...) or something like that. Please correct me if I'm wrong or missing something but it seems to me that, at least looking into the future, it would be beneficial to integrate those functions directly into Sage, right? I think it would be a bit more user friendly. Is these a reason the singular functions are implemented the way they are now? Or is this another case of lacking resources to improve it right now?

Also, what types of conversions are we missing between M2 and Sage? I take it the converting an object of type Divide is one of the things we're missing right now?

Thanks for all the information, I'm new to all of this
Reply all
Reply to author
Forward
0 new messages