# [sage-support] Using Objects from Sage in Macaulay2? Or perhaps doing dynamic coding?

```Hello everyone!

So, the title is almost a misnomer, because I don't really want to use an
object from Sage in M2 as much as I want to just use a number from my
python code in M2. Let me explain what I mean. I have a list in M2 with
close to 5,000 matrix groups. Lets call the list MatrixList, and it has
length 5000. I would like M2 to compute the molien series for all 5000 of
these matrix groups. So, ideally I could write a for loop in M2 that does
something like:```
```
SeriesList = []
for j from 0 to (5000) do SeriesList = SeriesList|[toString molienSeries
MatrixList#j]

This would make an empty list called SeriesList, then it would compute the
molien series of the j'th element of MatrixList, convert that to a string,
and then append that string to SeriesList, So, in the end, SeriesList would
have 5000 elements, each elements being the molien series for the
corresponding matrix group. The issue, however, is that trying to do so
many at once makes M2 crash. It crashes when I try doing it purely in M2,
it crashes when I use the sage interface to M2, and it crashes when I try
using SMC and the Sage/M2 interface (this is my preferred method though).
My solution so far as been the following (this is particularly good for me
since I use SMC): After a lot of testing, I know that even though doing a
loop from 0 to 5000 doesn't work, doing it in chunks of 200 tends to work
fine. So, I make 25 smaller empty lists in M2, called SeriesList1,
SeriesList2, etc, and then do something like this:
macaulay2.eval("for j from 0 to (199) do SeriesList1 =
Serieslist1|[toString molienSeries MatrixList#j]")
List1 = macaulay2.eval("SeriesList1")
save(List1, "List1")
macaulay2.eval("SeriesList1 = []")

So, this does exactly what the code above did, except it only does it for
the first 200 elements, then it saves a string representation of those 200
molienseries as a file "List1", and then it goes and clears SeriesList1 in
M2, to clear up the RAM. Running this takes a couple hours usually, and
then I run the same thing, except from 200 to 399, and I save the file as
List2, etc. The Main problem I have is that I have to start each one
manually. As in, I have to keep checking on it until it's done computing,
and then run the next one. I would much prefer being able to create some
loop so that it just keeps doing this entire process for me, in chunks of
200, for all 5000 elements, and saves it into a bunch of different files.

I don't know how to do this, however. My original thought had been that I
would just write something like this:

List = []
for i in [0..24]:
macaulay2.eval("for j from i*200 to ((i+1)*(200) - 1) do SeriesList =
Serieslist|[toString molienSeries MatrixList#j]") //this doesn't work
List.append(macaulay2.eval("SeriesList")
save(List[i],"List%s", i) //this doesn't work
macaulay2.eval("SeriesList = []") //just to clear the list, otherwise
the RAM usage gets too high

So, I could use the iterator in the for loop in the python code to change
the range of the for loop in macaulay2. However, this doens't work, because
"i", the iterator in the python for loop, isn't a variable in M2. Also, I
was hoping that i could use the i from the for loop to make different file
names. So, when i = 1, it would save the file as List1, and then in the
next loop around it would save it as List2, etc. Neither of those things
work. Do you know how I could make that work? Somehow changing what happens
inside of M2 with numbers that are defined in Sage? And dynamically
creating file names?

I had some idea of how I could try to do this. My guess was to use the
exec() command. From what I understand, the exec command runs strings as
python code. So, I could create a list of the 25 versions of the above
code, where it would have 0 through 24 already plugged in for "i" in each
version of the code. Then I could run do

for i in [0..24]: exec(ListOfCommands[i])

What do you guys think? Is this a viable method? Is this "dangerous"
somehow? Do you have any tips or alternative methods to do this? Instead of
using exec, it would be very interesting to know if i could somehow get
variables defined outside of M2 to be used within M2. The reason I want to
make this fully automated is so that i can leave it running over night. if
I only had to do this with one set of 5000 matrix groups, I'd probably do
it by hand, but I'm going to be generating lots of different sets of
different sizes of matrix groups, and I want to have a fairly easy method
where I can just leave the code running and have it do it all. Thanks!

--
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