[GAP Forum] GAP to MATLAB solution

dmitrii.pasechnik at cs.ox.ac.uk dmitrii.pasechnik at cs.ox.ac.uk
Fri Oct 18 15:56:08 BST 2019


Dear Joey, dear Robert,

On Thu, Oct 17, 2019 at 10:17:25PM +0000, Iverson, Joseph W [MATH] wrote:
> 
> Thanks to everyone for their help with my question. In addition to the
> suggestions already made, I am forwarding a solution from Dr. Robert
> Lazar (with his kind permission). Basically, you can run GAP inside of
> Sage, and then use Sage to export a *.mat file. I guess this is
> possible because Sage is built on Python, and enough people do linear
> algebra with Python that one of them eventually figured out how to
> make it talk to MATLAB.

Thanks for pointing this out, I didn't know that scipy can write *.mat
files, apologies.

> 
> See below for sample Sage code to export a GAP matrix as a *.mat file.

One remark about this code is that one should use
libgap() rather than gap() callsr. Indeed, libgap() is a much faster and more
robust interface, allowing one to keep GAP objects in shared with
SageMath memory, rather than sending then across with Unix pipes (that's
what gap() does, and it's not too robust and gets slow with big volumes
of data).

gap() interface is obsolete, and will be fully replaced with libgap() -
we're not too far from achieving this.

Another remark is that exchanging objects with MATLAB via *.mat files
does not scale too well, as was pointed out in this thread already, I
think. However, MATLAB has its own Python library, which one can install
into SageMath's Python, and send/receive MATLAB objects from Python.
See
https://uk.mathworks.com/help/matlab/matlab_external/get-started-with-matlab-engine-for-python.html
It appears to be much more suitable for computations involving repeatedly
creating GAP data and send it to MATLAB for processing.
At least it's appears from docs that it exchanges data in memory, thus
much faster than one can even hope to achieve with files.

I must say I never tried this, but would be willing to help if needed.

https://uk.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html

Best wishes,
Dmitrii
> 
> All the best, Joey Iverson
> 
> Assistant Professor Department of Mathematics Iowa State University
> 
> 
> From: Robert Lazar <tyrus400 at gmail.com> Date: Thursday, October 17,
> 2019 at 12:35 PM To: "Iverson, Joseph W [MATH]" <jwi at iastate.edu>
> Subject: Re: Robert Lazar// Dr.Songs old student
> 
> Note: This should be run inside sage. If you run this on butlers
> server you will get links to download the .mat. If you are working in
> gap first you can convert that code to run gap through sage and I can
> assist with that as well.
> 
> import scipy.io<http://scipy.io/> as sio import numpy as np A =
> gap([[1,0],[0,1]]) A= matrix(CC,A) sio.savemat('test_A.mat', {'A':A})
> 
> B = gap([[E(4),0],[0,E(4)]]) B= matrix(CC,B) sio.savemat('test_B.mat',
> {'B':B})
> 
> 
> Explanation:
> 
> Matrix(CC,A) converts the cyclotomic to a matrix approximation over
> the complex numbers.
> 
> Please let me know if you need something more specific!
> 
> 
> 
> On Oct 17, 2019, at 11:08 AM, Iverson, Joseph W [MATH]
> <jwi at iastate.edu<mailto:jwi at iastate.edu>> wrote: Hi Robert,
> 
> Thanks, that's great! How about a really simple example like
> A:=[[1,0],[0,1];
> 
> Or to involve some cyclotomics: B:=[[E(4),0],[0,E(4)]];
> 
> If I can just get the basic syntax, then I think I can figure out the
> rest for myself.
> 
> Thanks again for your help!
> 
> All the best, Joey
> 
> Assistant Professor Department of Mathematics Iowa State University
> 
> 
> On 10/17/19, 6:59 AM, "Robert Lazar"
> <tyrus400 at gmail.com<mailto:tyrus400 at gmail.com>> wrote:
> 
>    Hey Dr.Iverson,
> 
>    I saw your posts in the gap forum. I think I might be able to help
>    or at least try. There is a python package called
>    scipy.io<http://scipy.io> that can save as mat files.
> 
>    If you have the code in gap I should have some time to rewrite it
>    accessing it via python (it’s as simple as wrapping the function in
>    gap()) and save the files. Dr.Butlers sage sever has all of the
>    necessary packages.
> 
>    If you have a smaller example it would be easier. So we can test it
>    quicker.
> 
>    Best regards, Robert
>    _______________________________________________ Forum mailing list
>    Forum at gap-system.org
>    https://mail.gap-system.org/mailman/listinfo/forum



More information about the Forum mailing list