Search code examples
magma-ca

How do I create new intrinsics in MAGMA (computer algebra system)?


I am having trouble creating intrinsics in MAGMA. For example, the following code in MAGMA:

intrinsic Square(x::FldComElt) -> FldComElt
{Returns the square of x (x^2).}
return x^2;
end intrinsic;

produced the following error:

User error: Illegal intrinsic

I don't understand the error as to why this is an illegal intrinsic. Can someone help me to create intrinsics in MAGMA?

For example, the following code in MAGMA:

intrinsic Square(x::FldComElt) -> FldComElt
{Returns the square of x (x^2).}
return x^2;
end intrinsic;
Square(12);

produced the following errors in output:

User error: Illegal intrinsic

User error: Identifier 'Square' has not been declared or assigned

rather than the following output:

144

Can this error be fixed?


Solution

  • Intrinsics should be defined in a package file, and imported. For details, see http://magma.maths.usyd.edu.au/magma/handbook/text/24#164

    In your example, the code looks good, I would just change 'FldComElt' to 'RngIntElt' in order to square the integer 12.

    Save lines 1--4 of your code (the intrinsic definition) as a .m file. In the same directory, if you call

    Attach("example.m");
    Square(12);
    

    you should get output 144.

    Note: a) this cannot be done in the MAGMA online calculator, as you need directories; b) your example would be better suited as a function:

    function Square(x)
    // Returns the square of x (x^2).
    return x^2;
    end function;
    
    Square(12);
    

    so no extra file would be necessary (and the online calculator works fine). However, if you are creating and sharing detailed routines, intrinsics are the way to go.