linking own software to Macaulay2 (or running code on Macaulay2)

121 views
Skip to first unread message

Machiel Kolstein

unread,
Mar 2, 2017, 11:38:05 AM3/2/17
to Macaulay2

Hi, 

I have downloaded Macaulay2 because for my analysis I need to solve sets of 4-degree polynomials with 3 unknowns on an event-by-event basis. I was hoping Macaulay could help me with that. However, for this I need some automatic way to pass for each event the corresponding factors to Macaulay2 to do the calculation and give me the solutions. 
So, the easiest way would be to have my own C++ code doing my normal event analysis and then for each event call routines from Macaulay2. Is there a way to do this? Can I link Macaulay2 libraries to C++ code and make calls to Macaulay2 functions? I could not find anything about this in the documentation. 
Alternatively, is it possible to run code or a script while running the M2 program? Is there some special Macaulay2 scripting language that I can start-up from M2?
If not, what other ways are there to continuously feed Macaulay with my numbers and let it do the calculations? 

Best regards, 

Machiel Kolstein


Avís - Aviso - Legal Notice - (LOPD) - http://legal.ifae.es

Brian Pike

unread,
Mar 26, 2017, 11:56:13 PM3/26/17
to maca...@googlegroups.com
While you could find a way to link with Macaulay2 or some of the many libraries it uses, I would anticipate that to be tricky and fragile.  A far easier task would be to use Macaulay2's programming language to establish some sort of inter-process communication with the C++ program you're writing.

For example, say you had the file "socket_server.m2" containing this:
    someFunction = (a,b) -> a+b;
    someRing=ZZ[p,q];

    -- Server portion
    listener=openListener("$:5000");
    -- Block until a connection is received.  Open the connection
    conn=openInOut(listener);
    stdio<<"Connection opened!"<<endl<<flush;
    while (isOpen(conn) and not(atEndOfFile(conn))) do (
        stdio<<"Waiting for a command."<<endl<<flush;
        inString=read(conn);
        -- inString has an EOL, but should be a single line.
        inStringLines=lines(inString);
        if not(#inStringLines==1) then (
            stdio<<"Received "<<#inStringLines<<" lines of input; unexpected.  Aborting"<<endl<<flush;
            break;
        );
        inStringCmd=inStringLines#0;
        stdio<<"Received \""<<inStringCmd<<"\".  Will execute."<<endl<<flush;
        outString=toString(value(inStringCmd));
        stdio<<"  Cmd evaluated to \""<<outString<<"\". Sending reply."<<endl<<flush;
        conn<<outString<<endl<<flush;
    )
    stdio<<"Server finished with isOpen="<<isOpen(conn)<<" and atEndOfFile="<<atEndOfFile(conn)<<endl<<flush;
    close(conn)
    close(listener)
Then start M2 (in an appropriate directory), and run
    load "socket_server.m2"
In a second terminal, start netcat, connecting to port 5000 on localhost (e.g., run "nc localhost 5000").  In that terminal, type (don't paste) the following commands:
    1+2
    someFunction(1,3)
    factor(p^2-q^2)
    R=QQ[x,y]
    (x+y)^4
and then use ctrl-C or kill to terminate netcat.

The commands you sent via netcat will have been executed in Macaulay2, and the answer returned.  The netcat session will have looked like this:
    1+2
    3
    someFunction(1,3)
    4
    factor(p^2-q^2)
    (p-q)*(p+q)
    R=QQ[x,y]
    R
    (x+y)^4
    x^4+4*x^3*y+6*x^2*y^2+4*x*y^3+y^4
and the Macaulay2 program will have displayed this:
    Connection opened!
    Waiting for a command.
    Received "1+2".  Will execute.
      Cmd evaluated to "3". Sending reply.
    Waiting for a command.
    Received "someFunction(1,3)".  Will execute.
      Cmd evaluated to "4". Sending reply.
    Waiting for a command.
    Received "factor(p^2-q^2)".  Will execute.
      Cmd evaluated to "(p-q)*(p+q)". Sending reply.
    Waiting for a command.
    Received "R=QQ[x,y]".  Will execute.
      Cmd evaluated to "R". Sending reply.
    Waiting for a command.
    Received "(x+y)^4".  Will execute.
      Cmd evaluated to "x^4+4*x^3*y+6*x^2*y^2+4*x*y^3+y^4". Sending reply.
    Waiting for a command.
    Received 0 lines of input; unexpected.  Aborting
    Server finished with isOpen=true and atEndOfFile=true

This is just to give you some idea.  There are other IPC methods, and my program is buggy (e.g., try pasting a few lines into netcat at once).

Thanks,
Brian


--
You received this message because you are subscribed to the Google Groups "Macaulay2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to macaulay2+unsubscribe@googlegroups.com.
To post to this group, send email to maca...@googlegroups.com.
Visit this group at https://groups.google.com/group/macaulay2.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages