Note
You must have magma installed on your computer for this interface to work. Magma is not free, so it is not included with Sage, but you can obtain it from http://magma.maths.usyd.edu.au/.
Type magma.[tab] for a list of all the functions available from your Magma install. Type magma.[tab]? for Magma’s help about a given function. Type magma(...) to create a new Magma object, and magma.eval(...) to run a string using Magma (and get the result back as a string).
Sage provides an interface to the Magma computational algebra system. This system provides extensive functionality for number theory, group theory, combinatorics and algebra.
The Magma interface offers three pieces of functionality:
Some Magma functions have optional “parameters”, which are arguments that in Magma go after a colon. In Sage, you pass these using named function arguments. For example,
sage: E = magma('EllipticCurve([0,1,1,-1,0])') # optional - magma
sage: E.Rank(Bound = 5) # optional - magma
0
Some Magma functions return more than one value. You can control how many you get using the nvals named parameter to a function call:
sage: n = magma(100) # optional - magma
sage: n.IsSquare(nvals = 1) # optional - magma
true
sage: n.IsSquare(nvals = 2) # optional - magma
(true, 10)
sage: n = magma(-2006) # optional - magma
sage: n.Factorization() # optional - magma
[ <2, 1>, <17, 1>, <59, 1> ]
sage: n.Factorization(nvals=2) # optional - magma
([ <2, 1>, <17, 1>, <59, 1> ], -1)
We verify that an obviously principal ideal is principal:
sage: _ = magma.eval('R<x> := PolynomialRing(RationalField())') # optional - magma
sage: O = magma.NumberField('x^2+23').MaximalOrder() # optional - magma
sage: I = magma('ideal<%s|%s.1>'%(O.name(),O.name())) # optional - magma
sage: I.IsPrincipal(nvals=2) # optional - magma
(true, [1, 0])
The Magma interface reads in even very long input (using files) in a robust manner.
sage: t = '"%s"'%10^10000 # ten thousand character string. # optional - magma
sage: a = magma.eval(t) # optional - magma
sage: a = magma(t) # optional - magma
We compute a space of modular forms with character.
sage: N = 20
sage: D = 20
sage: eps_top = fundamental_discriminant(D)
sage: eps = magma.KroneckerCharacter(eps_top, RationalField()) # optional - magma
sage: M2 = magma.ModularForms(eps) # optional - magma
sage: print M2 # optional - magma
Space of modular forms on Gamma_1(5) ...
sage: print M2.Basis() # optional - magma
[
1 + 10*q^2 + 20*q^3 + 20*q^5 + 60*q^7 + ...
q + q^2 + 2*q^3 + 3*q^4 + 5*q^5 + 2*q^6 + ...
]
In Sage/Python (and sort of C++) coercion of an element x into a structure S is denoted by S(x). This also works for the Magma interface:
sage: G = magma.DirichletGroup(20) # optional - magma
sage: G.AssignNames(['a', 'b']) # optional - magma
sage: (G.1).Modulus() # optional - magma
20
sage: e = magma.DirichletGroup(40)(G.1) # optional - magma
sage: print e # optional - magma
$.1
sage: print e.Modulus() # optional - magma
40
We coerce some polynomial rings into Magma:
sage: R.<y> = PolynomialRing(QQ)
sage: S = magma(R) # optional - magma
sage: print S # optional - magma
Univariate Polynomial Ring in y over Rational Field
sage: S.1 # optional - magma
y
This example illustrates that Sage doesn’t magically extend how Magma implicit coercion (what there is, at least) works. The errors below are the result of Magma having a rather limited automatic coercion system compared to Sage’s:
sage: R.<x> = ZZ[]
sage: x * 5
5*x
sage: x * 1.0
1.00000000000000*x
sage: x * (2/3)
2/3*x
sage: y = magma(x) # optional - magma
sage: y * 5 # optional - magma
5*x
sage: y * 1.0 # optional - magma
...
TypeError: unsupported operand parent(s) for '*': 'Magma' and 'Real Field with 53 bits of precision'
sage: y * (2/3) # optional - magma
...
TypeError: unsupported operand parent(s) for '*': 'Magma' and 'Rational Field'
AUTHORS:
Interface to the Magma interpreter.
Type magma.[tab] for a list of all the functions available from your Magma install. Type magma.[tab]? for Magma’s help about a given function. Type magma(...) to create a new Magma object, and magma.eval(...) to run a string using Magma (and get the result back as a string).
Note
If you do not own a local copy of Magma, try using the magma_free command instead, which uses the free demo web interface to Magma.
EXAMPLES:
You must use nvals = 0 to call a function that doesn’t return anything, otherwise you’ll get an error. (nvals is the number of return values.)
sage: magma.SetDefaultRealFieldPrecision(200, nvals=0) # magma >= v2.12; optional - magma
sage: magma.eval('1.1') # optional - magma
'1.1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
sage: magma.SetDefaultRealFieldPrecision(30, nvals=0) # optional - magma
Attach the given file to the running instance of Magma.
Attaching a file in Magma makes all intrinsics defined in the file available to the shell. Moreover, if the file doesn’t start with the freeze; command, then the file is reloaded whenever it is changed. Note that functions and procedures defined in the file are not available. For only those, use magma.load(filename).
INPUT:
EXAMPLES: Attaching a file that exists is fine:
sage: magma.attach('%s/data/extcode/magma/sage/basic.m'%Sage_ROOT) # optional - magma
Attaching a file that doesn’t exist raises an exception:
sage: magma.attach('%s/data/extcode/magma/sage/basic2.m'%Sage_ROOT) # optional - magma
...
RuntimeError: Error evaluating Magma code...
Attach the given spec file to the running instance of Magma.
This can attach numerous other files to the running Magma (see the Magma documentation for more details).
INPUT:
EXAMPLES:
sage: magma.attach_spec('%s/data/extcode/magma/spec'%SAGE_ROOT) # optional - magma
sage: magma.attach_spec('%s/data/extcode/magma/spec2'%SAGE_ROOT) # optional - magma
...
RuntimeError: Can't open package spec file .../data/extcode/magma/spec2 for reading (No such file or directory)
Get the verbosity level of a given algorithm class etc. in Magma.
INPUT:
Note
This method is provided to be consistent with the Magma naming convention.
EXAMPLES:
sage: magma.SetVerbose("Groebner", 2) # optional - magma
sage: magma.GetVerbose("Groebner") # optional - magma
2
Set the verbosity level for a given algorithm class etc. in Magma.
INPUT:
Note
This method is provided to be consistent with the Magma naming convention.
sage: magma.SetVerbose("Groebner", 2) # optional - magma
sage: magma.GetVerbose("Groebner") # optional - magma
2
Coerce x into this Magma interpreter interface.
INPUT:
OUTPUT: MagmaElement
EXAMPLES:
sage: magma(EllipticCurve('37a')) # optional - magma
Elliptic Curve defined by y^2 + y = x^3 - x over Rational Field
sage: magma('EllipticCurve([GF(5)|1,2,3,4,1])') # optional - magma
Elliptic Curve defined by y^2 + x*y + 3*y = x^3 + 2*x^2 + 4*x + 1 over GF(5)
sage: magma('PowerSeriesRing(Rationals())', 't') # optional - magma
Power series ring in t over Rational Field
sage: magma('PolynomialRing(RationalField(), 3)', 'x,y,z') # optional - magma
Polynomial ring of rank 3 over Rational Field
Lexicographical Order
Variables: x, y, z
We test a coercion between different Magma instances:
sage: m = Magma()
sage: n = Magma()
sage: a = n(m(2)) # optional - magma
sage: a.parent() is n # optional - magma
True
sage: a.parent() is m # optional - magma
False
We test caching:
sage: R.<x> = ZZ[] # optional - magma
sage: magma(R) is magma(R) # optional - magma
True
sage: m = Magma() # optional - magma
sage: m(R) # optional - magma
Univariate Polynomial Ring in x over Integer Ring
sage: m(R) is magma(R) # optional - magma
False
sage: R._magma_cache # optional - magma
{Magma: Univariate Polynomial Ring in x over Integer Ring,
Magma: Univariate Polynomial Ring in x over Integer Ring}
Return a formal wrapper around a Magma function, or raise an AttributeError if attrname starts with an underscore.
INPUT:
OUTPUT: MagmaFunction instance
EXAMPLES:
sage: g = magma.__getattr__('EllipticCurve')
sage: g
EllipticCurve
sage: type(g)
<class 'sage.interfaces.magma.MagmaFunction'>
In fact, __getattr__ is called implicitly in the following case:
sage: f = magma.EllipticCurve
sage: type(f)
<class 'sage.interfaces.magma.MagmaFunction'>
sage: f
EllipticCurve
INPUT:
EXAMPLES:
sage: Magma(maxread=1000, logfile=tmp_filename())
Magma
Used to pickle a magma interface instance.
Unpickling results in the default magma interpreter; this is a choice, and perhaps not the most logical one! It means that if you make two distinct magma interfaces, pickle both, then unpickle them, you get back exactly the same one. We illustrate this behavior below.
OUTPUT: function, empty tuple
EXAMPLES:
sage: loads(dumps(magma)) is magma
True
Unpickling always gives the default global magma interpreter:
sage: m1 = Magma(); m2 = Magma()
sage: m1 is m2
False
sage: loads(dumps(m1)) is loads(dumps(m2))
True
sage: loads(dumps(m1)) is magma
True
Returns the assignment symbol in Magma.
EXAMPLES:
sage: magma._assign_symbol()
':='
Tries to coerce to self by calling a special underscore method.
If no such method is defined, raises an AttributeError instead of a TypeError.
EXAMPLES:
sage: magma._coerce_from_special_method(-3/5) # optional - magma
-3/5
Note that AttributeError:
sage: magma._coerce_from_special_method('2 + 3') # optional - magma
...
AttributeError: 'str' object has no attribute '_magma_init_'
Evaluate the given code expression assuming that it outputs nvals distinct values. Return the resulting values as a tuple if nvals = 2.
INPUT:
OUTPUT: nvals distinct values
EXAMPLES:
sage: magma._do_call('SetVerbose("Groebner",2)', 0) # optional - magma
sage: magma._do_call('Factorization(-5)', 1) # optional - magma
[ <5, 1> ]
Here we get two outputs, as a tuple.
sage: magma._do_call('Factorization(-5)', 2) # optional - magma
([ <5, 1> ], -1)
You can also do this:
sage: F, sign = magma._do_call('Factorization(-5)', 2) # optional - magma
sage: F # optional - magma
[ <5, 1> ]
sage: sign # optional - magma
-1
An expression that has one value.
sage: magma._do_call('3^5', 1) # optional - magma
243
Returns the equality testing logical symbol in Magma.
EXAMPLES:
sage: magma._equality_symbol()
'eq'
Returns the string representation of “false” in Magma.
EXAMPLES:
sage: magma._false_symbol()
'false'
Returns the greater than testing logical symbol in Magma.
EXAMPLES:
sage: magma._greaterthan_symbol()
' gt '
Return the left sequence delimiter in Magma. Despite the name in this function, this is really the least painful choice.
EXAMPLES:
sage: magma._left_list_delim()
'['
Returns the less than testing logical symbol in Magma.
EXAMPLES:
sage: magma._lessthan_symbol()
' lt '
Return the next reference name. This is used internally to deal with Magma objects that would be deallocated before they are used in constructing another object.
OUTPUT: string
EXAMPLES:
sage: magma._next_ref_name()
'_sage_ref...'
Return the next available variable name in Magma.
OUTPUT: string
EXAMPLES:
sage: m = Magma()
sage: m._next_var_name() # optional - magma
'_sage_[1]'
sage: m._next_var_name() # optional - magma
'_sage_[2]'
sage: a = m(3/8); a # optional - magma
3/8
sage: a.name() # optional - magma
'_sage_[3]'
sage: m._next_var_name() # optional - magma
'_sage_[4]'
Return the Python class of elements of the Magma interface.
OUTPUT: a Python class
EXAMPLES:
sage: magma._object_class()
<class 'sage.interfaces.magma.MagmaElement'>
Used internally in the Magma interface to post-process the result of evaluating a string using a file. For Magma what this does is delete the first output line, since that is a verbose output line that Magma displays about loading a file.
INPUT:
OUTPUT: a string
EXAMPLES:
sage: magma._post_process_from_file("Loading ...\nHello")
'Hello'
sage: magma._post_process_from_file("Hello")
''
All input gets preparsed by calling this function before it gets evaluated.
EXAMPLES:
sage: magma._preparse_colon_equals = False
sage: magma._preparse('a = 5')
'a = 5'
sage: magma._preparse_colon_equals = True
sage: magma._preparse('a = 5')
'a := 5'
sage: magma._preparse('a = 5; b := 7; c =a+b;')
'a := 5; b := 7; c :=a+b;'
Return the command in Magma that reads in the contents of the given file.
INPUT:
OUTPUT:
EXAMPLES:
sage: magma._read_in_file_command('file.m')
'load "file.m";'
Return the right sequence delimiter in Magma. Despite the name in this function, this is really the least painful choice.
EXAMPLES:
sage: magma._right_list_delim()
']'
Initialize a Magma interface instance. This involves (1) setting up an obfuscated prompt, and (2) attaching the MAGMA_SPEC file (see EXTCODE_DIR/spec file (see sage.interfaces.magma.EXTCODE_DIR/spec).
EXAMPLES: This is not too exciting:
sage: magma._start() # optional - magma
Returns the string representation of “truth” in Magma.
EXAMPLES:
sage: magma._true_symbol()
'true'
Return s but wrapped by a call to SageCreateWithNames. This is just a very simple convenience function so that code is cleaner.
INPUT:
OUTPUT: string
EXAMPLES:
sage: magma._with_names('PolynomialRing(RationalField())', ['y']) # optional - magma
'SageCreateWithNames(PolynomialRing(RationalField()),["y"])'
Attach the given file to the running instance of Magma.
Attaching a file in Magma makes all intrinsics defined in the file available to the shell. Moreover, if the file doesn’t start with the freeze; command, then the file is reloaded whenever it is changed. Note that functions and procedures defined in the file are not available. For only those, use magma.load(filename).
INPUT:
EXAMPLES: Attaching a file that exists is fine:
sage: magma.attach('%s/data/extcode/magma/sage/basic.m'%Sage_ROOT) # optional - magma
Attaching a file that doesn’t exist raises an exception:
sage: magma.attach('%s/data/extcode/magma/sage/basic2.m'%Sage_ROOT) # optional - magma
...
RuntimeError: Error evaluating Magma code...
Attach the given spec file to the running instance of Magma.
This can attach numerous other files to the running Magma (see the Magma documentation for more details).
INPUT:
EXAMPLES:
sage: magma.attach_spec('%s/data/extcode/magma/spec'%SAGE_ROOT) # optional - magma
sage: magma.attach_spec('%s/data/extcode/magma/spec2'%SAGE_ROOT) # optional - magma
...
RuntimeError: Can't open package spec file .../data/extcode/magma/spec2 for reading (No such file or directory)
This is a wrapper around the Magma constructor
nameleft gens
returning nvals.
INPUT:
OUTPUT: a single magma object if nvals == 1; otherwise a tuple of nvals magma objects.
EXAMPLES: The bar_call function is used by the sub, quo, and ideal methods of Magma elements. Here we illustrate directly using bar_call to create quotients:
sage: V = magma.RModule(ZZ,3) # optional - magma
sage: V # optional - magma
RModule(IntegerRing(), 3)
sage: magma.bar_call(V, 'quo', [[1,2,3]], nvals=1) # optional - magma
RModule(IntegerRing(), 2)
sage: magma.bar_call(V, 'quo', [[1,2,3]], nvals=2) # optional - magma
(RModule(IntegerRing(), 2),
Mapping from: RModule(IntegerRing(), 3) to RModule(IntegerRing(), 2))
sage: magma.bar_call(V, 'quo', V, nvals=2) # optional - magma
(RModule(IntegerRing(), 0),
Mapping from: RModule(IntegerRing(), 3) to RModule(IntegerRing(), 0))
Change to the given directory.
INPUT:
EXAMPLES:
sage: magma.chdir('/') # optional - magma
sage: magma.eval('System("pwd")') # optional - magma
'/'
Clear the variable named var and make it available to be used again.
INPUT:
EXAMPLES:
sage: magma = Magma() # optional - magma
sage: magma.clear('foo') # sets foo to 0 in magma; optional - magma
sage: magma.eval('foo') # optional - magma
'0'
Because we cleared foo, it is set to be used as a variable name in the future:
sage: a = magma('10') # optional - magma
sage: a.name() # optional - magma
'foo'
The following tests that the whole variable clearing and freeing system is working correctly.
sage: magma = Magma() # optional - magma
sage: a = magma('100') # optional - magma
sage: a.name() # optional - magma
'_sage_[1]'
sage: del a # optional - magma
sage: b = magma('257') # optional - magma
sage: b.name() # optional - magma
'_sage_[1]'
sage: del b # optional - magma
sage: magma('_sage_[1]') # optional - magma
0
Run a command line Magma session. This session is completely separate from this Magma interface.
EXAMPLES:
sage: magma.console() # not tested
Magma V2.14-9 Sat Oct 11 2008 06:36:41 on one [Seed = 1157408761]
Type ? for help. Type <Ctrl>-D to quit.
>
Total time: 2.820 seconds, Total memory usage: 3.95MB
Return the CPU time in seconds that has elapsed since this Magma session started. This is a floating point number, computed by Magma.
If t is given, then instead return the floating point time from when t seconds had elapsed. This is useful for computing elapsed times between two points in a running program.
INPUT:
OUTPUT:
EXAMPLES:
sage: type(magma.cputime()) # optional - magma
<type 'float'>
sage: magma.cputime() # random, optional - magma
1.9399999999999999
sage: t = magma.cputime() # optional - magma
sage: magma.cputime(t) # random, optional - magma
0.02
Evaluate the given block x of code in Magma and return the output as a string.
INPUT:
OUTPUT: string
EXAMPLES: We evaluate a string that involves assigning to a variable and printing.
sage: magma.eval("a := 10;print 2+a;") # optional - magma
'12'
We evaluate a large input line (note that no weird output appears and that this works quickly).
sage: magma.eval("a := %s;"%(10^10000)) # optional - magma
"
Return result of evaluating a Magma function with given input, parameters, and asking for nvals as output.
INPUT:
OUTPUT: MagmaElement or tuple of nvals MagmaElement’s
EXAMPLES:
sage: magma.function_call('Factorization', 100) # optional - magma
[ <2, 2>, <5, 2> ]
sage: magma.function_call('NextPrime', 100, {'Proof':False}) # optional - magma
101
sage: magma.function_call('PolynomialRing', [QQ,2]) # optional - magma
Polynomial ring of rank 2 over Rational Field
Lexicographical Order
Variables: $.1, $.2
Next, we illustrate multiple return values:
sage: magma.function_call('IsSquare', 100) # optional - magma
true
sage: magma.function_call('IsSquare', 100, nvals=2) # optional - magma
(true, 10)
sage: magma.function_call('IsSquare', 100, nvals=3) # optional - magma
...
RuntimeError: Error evaluating Magma code...
Runtime error in :=: Expected to assign 3 value(s) but only computed 2 value(s)
Get the value of the variable var.
INPUT:
OUTPUT:
EXAMPLES:
sage: magma.set('abc', '2 + 3/5') # optional - magma
sage: magma.get('abc') # optional - magma
'13/5'
Get the verbosity level of a given algorithm class etc. in Magma.
INPUT:
EXAMPLES:
sage: magma.set_verbose("Groebner", 2) # optional - magma
sage: magma.get_verbose("Groebner") # optional - magma
2
Return Magma help on string s.
This returns what typing ?s would return in Magma.
INPUT:
OUTPUT: string
EXAMPLES:
sage: magma.help("NextPrime") # optional - magma
===============================================================================
PATH: /magma/ring-field-algebra/integer/prime/next-previous/NextPrime
KIND: Intrinsic
===============================================================================
NextPrime(n) : RngIntElt -> RngIntElt
NextPrime(n: parameter) : RngIntElt -> RngIntElt
...
Return the Magma ideal defined by L.
INPUT:
OUTPUT: The magma ideal generated by the elements of L.
EXAMPLES:
sage: R.<x,y> = QQ[]
sage: magma.ideal([x^2, y^3*x]) # optional - magma
Ideal of Polynomial ring of rank 2 over Rational Field
Graded Reverse Lexicographical Order
Variables: x, y
Basis:
[
x^2,
x*y^3
]
Load the file with given filename using the ‘load’ command in the Magma shell.
Loading a file in Magma makes all the functions and procedures in the file available. The file should not contain any intrinsics (or you’ll get errors). It also runs code in the file, which can produce output.
INPUT:
OUTPUT: output printed when loading the file
EXAMPLES:
sage: open(SAGE_TMP + 'a.m','w').write('function f(n) return n^2; end function;\nprint "hi";')
sage: print magma.load(SAGE_TMP + 'a.m') # optional - magma
Loading ".../.sage//temp/.../a.m"
hi
sage: magma('f(12)') # optional - magma
144
Create a new object with given value and gens.
INPUT:
OUTPUT: new Magma element that is equal to value with given gens
EXAMPLES:
sage: R = magma.objgens('PolynomialRing(Rationals(),2)', 'alpha,beta') # optional - magma
sage: R.gens() # optional - magma
[alpha, beta]
Because of how Magma works you can use this to change the variable names of the generators of an object:
sage: S = magma.objgens(R, 'X,Y') # optional - magma
sage: R # optional - magma
Polynomial ring of rank 2 over Rational Field
Lexicographical Order
Variables: X, Y
sage: S # optional - magma
Polynomial ring of rank 2 over Rational Field
Lexicographical Order
Variables: X, Y
Set the variable var to the given value in the Magma interpreter.
INPUT:
EXAMPLES:
sage: magma.set('abc', '2 + 3/5') # optional - magma
sage: magma('abc') # optional - magma
13/5
Set the verbosity level for a given algorithm, class, etc. in Magma.
INPUT:
EXAMPLES:
sage: magma.set_verbose("Groebner", 2) # optional - magma
sage: magma.get_verbose("Groebner") # optional - magma
2
Return a list of all Magma commands.
This is used as a hook to enable custom command completion.
Magma doesn’t provide any fast way to make a list of all commands, which is why caching is done by default. Note that an adverse impact of caching is that new commands are not picked up, e.g., user defined variables or functions.
INPUT:
OUTPUT: list of strings
EXAMPLES:
sage: len(magma.trait_names(verbose=False)) # random, optional - magma
7261
Return the version of Magma that you have in your PATH on your computer.
OUTPUT:
EXAMPLES:
sage: magma.version() # random, optional - magma
((2, 14, 9), 'V2.14-9')
EXAMPLES:
sage: G = magma.DirichletGroup(20) # optional - magma
sage: G.AssignNames(['a','b']) # optional - magma
sage: G.1 # optional - magma
a
sage: G.Elements() # optional - magma
[
1,
a,
b,
a*b
]
Coerce something into the object (using the Magma ! notation).
For function calls, use self.eval(...).
EXAMPLES:
sage: M = magma.RMatrixSpace(magma.IntegerRing(), 2, 2) # optional - magma
sage: A = M([1,2,3,4]); A # optional - magma
[1 2]
[3 4]
sage: type(A) # optional - magma
<class 'sage.interfaces.magma.MagmaElement'>
sage: A.Type() # optional - magma
ModMatRngElt
Quotient of division of self by other. This is denoted // (“div” in magma).
EXAMPLE:
sage: R.<x,y,z> = QQ[]
sage: magma(5)//magma(2) # optional - magma
2
sage: m = magma(x*z + x*y) # optional - magma
sage: n = magma(x) # optional - magma
sage: m//n # optional - magma
y + z
INPUT:
OUTPUT: a Magma function partially evaluated with self as the first input.
Note
If the input attrname starts with an underscore, an AttributeError is raised so that the actual Python _ method/value can be accessed.
EXAMPLES:
sage: n = magma(-15) # optional - magma
sage: type(n) # optional - magma
<class 'sage.interfaces.magma.MagmaElement'>
sage: f = n.__getattr__('Factorization') # optional - magma
sage: type(f) # optional - magma
<class 'sage.interfaces.magma.MagmaFunctionElement'>
sage: f # optional - magma
Partially evaluated Magma function or intrinsic 'Factorization'
...
Return iterator over this Magma element.
OUTPUT: generator object
Warning
Internally this constructs the list of elements in self in Magma, which is not a lazy operation. This is because Magma doesn’t have a notion of lazy iterators, unfortunately.
EXAMPLES:
sage: V = magma('VectorSpace(GF(3),2)') # optional - magma
sage: V # optional - magma
Full Vector space of degree 2 over GF(3)
sage: w = V.__iter__(); w # optional - magma
<generator object at ...>
sage: w.next() # optional - magma
(0 0)
sage: w.next() # optional - magma
(1 0)
sage: list(w) # optional - magma
[(2 0), (0 1), (1 1), (2 1), (0 2), (1 2), (2 2)]
Return cardinality of this Magma element.
This is the same as #self in Magma.
EXAMPLES:
sage: V = magma('VectorSpace(GF(3),2)') # optional - magma
sage: V # optional - magma
Full Vector space of degree 2 over GF(3)
sage: len(V) # optional - magma
9
sage: V.__len__() # optional - magma
9
Return True if self is nonzero according to Magma. If Magma can’t decide, i.e., raising an error then also return True.
EXAMPLES: We define a Magma vector space.
sage: V = magma('VectorSpace(GF(3),2)'); V # optional - magma
Full Vector space of degree 2 over GF(3)
The first generator is nonzero.
sage: V.gen(1).__nonzero__() # optional - magma
True
The zero element is zero.
sage: V(0).__nonzero__() # optional - magma
False
The space itself is nonzero (the default - in Magma no comparison to 0 is possible).
sage: V.__nonzero__() # optional - magma
True
We can also call bool which is the opposite of __nonzero__.
sage: bool(V(0)) # optional - magma
False
sage: bool(V.gen(1)) # optional - magma
True
sage: bool(V) # optional - magma
True
Return latex representation of self.
AUTHORS:
Types that are nicely latex include:
IMPLEMENTATION: Calls latex.m, which is in Sage_ROOT/data/extcode/magma/latex.m
EXAMPLES:
sage: latex(magma('-2/3')) # optional - magma
\frac{-2}{3}
sage: magma('-2/3')._latex_() # optional - magma
'\\frac{-2}{3}'
sage: magma.eval('R<x> := PolynomialRing(RationalField()); f := (x-17/2)^3;') # optional - magma
"
sage: latex(magma('f')) # optional - magma
x^{3}-\frac{51}{2}x^{2}+\frac{867}{4}x-\frac{4913}{8}
sage: latex(magma('(MatrixAlgebra(RationalField(),3)![0,2,3,4,5,6,7,8,9])^(-1)')) # optional - magma
\left(\begin{array}{ccc}-1&2&-1\\2&-7&4\\-1&\frac{14}{3}&\frac{-8}{3}\end{array}\right)
sage: magma.eval('K<a> := CyclotomicField(11)') # optional - magma
"
sage: latex(magma('a^3 + a - 17/3')) # optional - magma
\frac{-17}{3}+\zeta_{11}+\zeta_{11}^{3}
sage: latex(magma('EllipticCurve([1,2/3,3/4,4/5,-5/6])')) # optional - magma
y^2+xy+\frac{3}{4}y=x^3+\frac{2}{3}x^2+\frac{4}{5}x-\frac{5}{6}
sage: _=magma.eval('R<x> := PolynomialRing(RationalField())') # optional - magma
sage: _=magma.eval('K<a> := NumberField(x^3+17*x+2)') # optional - magma
sage: latex(magma('(1/3)*a^2 - 17/3*a + 2')) # optional - magma
2-\frac{17}{3}a+\frac{1}{3}a^{2}
Sage auto-detects the greek letters and puts backslashes in:
sage: _=magma.eval('R<x> := PolynomialRing(RationalField())') # optional - magma
sage: _=magma.eval('K<alpha> := NumberField(x^3+17*x+2)') # optional - magma
sage: latex(magma('(1/3)*alpha^2 - 17/3*alpha + 2')) # optional - magma
2-\frac{17}{3}\alpha+\frac{1}{3}\alpha^{2}
sage: _=magma.eval('R<alpha> := PolynomialRing(RationalField())') # optional - magma
sage: latex(magma('alpha^3-1/7*alpha + 3')) # optional - magma
\alpha^{3}-\frac{1}{7}\alpha+3
Finite field elements:
sage: _=magma.eval('K<a> := GF(27)') # optional - magma
sage: latex(magma('a^2+2')) # optional - magma
2+a^{2}
Printing of unnamed (dollar sign) generators works correctly:
sage: latex(magma('FiniteField(81).1^2+1')) # optional - magma
1+\$.1^{2}
Finite fields:
sage: latex(magma('FiniteField(3)')) # optional - magma
\mathbf{F}_{{3}}
sage: latex(magma('FiniteField(27)')) # optional - magma
\mathbf{F}_{{3}^{3}}
Power Series:
sage: _=magma.eval('R<x> := PowerSeriesRing(RationalField())') # optional - magma
sage: latex(magma('(1/(1+x))')) # optional - magma
1-x+x^{2}-x^{3}+x^{4}-x^{5}+x^{6}-x^{7}+x^{8}-x^{9}+x^{10}-x^{11}+x^{12}-x^{13}+x^{14}-x^{15}+x^{16}-x^{17}+x^{18}-x^{19}+O(x^{20})
sage: _=magma.eval('R<x> := PowerSeriesRing(RationalField())') # optional - magma
sage: latex(magma('(-1/(2+x + O(x^3)))')) # optional - magma
\frac{-1}{2}+\frac{1}{4}x-\frac{1}{8}x^{2}+O(x^{3})
p-adic Numbers:
sage: latex(magma('pAdicField(7,4)!9333294394/49')) # optional - magma
4\cdot{}7^{-2} + 5\cdot{}7^{-1} + 5+ 6\cdot{}7^{1} + O(7^{2})
Try to convert self into a polynomial in the univariate polynomial ring R.
EXAMPLES:
sage: R.<x> = QQ[]
sage: f = magma(x^2 + 2/3*x + 5) # optional - magma
sage: f # optional - magma
x^2 + 2/3*x + 5
sage: f.Type() # optional - magma
RngUPolElt
sage: f._polynomial_(R) # optional - magma
x^2 + 2/3*x + 5
Return a variable name that is a new reference to this particular MagmaElement in Magma. This keeps this object from being garbage collected by Magma, even if all the Sage references to it are freed.
Important special behavior: When _ref is used during an implicit call to _magma_init_, then the reference disappears after the coercion is done. More precisely, if the output of _ref() appears as part of the output of a call to _magma_init_ that is then going to be input to magma(...), then it is deleted in the Magma interface. The main use for this behavior is that in _magma_init_ it allows you to get a reference to one object, and use it exactly once in constructing a string to evaluate in Magma, without having to worry about that object being deallocated. There are more sophisticated ways that the same problem (with _magma_init_ and references) could have been solved, but this solution is much simpler and easier to understand than all others I came up with. If this doesn’t make sense, read the source code to _coerce_from_special_method, which is much shorter than this paragraph.
Warning
Use _ref sparingly, since it involves a full call to Magma, which can be slow.
OUTPUT: string
EXAMPLES:
sage: a = magma('-2/3') # optional - magma
sage: s = a._ref(); s # optional - magma
'_sage_ref...'
sage: magma(s) # optional - magma
-2/3
Return Sage version of this object. Use self.sage() to get the Sage version.
EXAMPLES: Enumerated Sets:
sage: a = magma('{1,2/3,-5/9}') # optional - magma
sage: a.sage() # optional - magma
{1, -5/9, 2/3}
sage: a._sage_() # optional - magma
{1, -5/9, 2/3}
sage: type(a.sage()) # optional - magma
<class 'sage.sets.set.Set_object_enumerated'>
sage: a = magma('{1,2/3,-5/9}'); a # optional - magma
{ -5/9, 2/3, 1 }
sage: a.Type() # optional - magma
SetEnum
sage: b = a.sage(); b # optional - magma
{1, -5/9, 2/3}
sage: type(b) # optional - magma
<class 'sage.sets.set.Set_object_enumerated'>
sage: c = magma(b); c # optional - magma
{ -5/9, 2/3, 1 }
sage: c.Type() # optional - magma
SetEnum
Multisets are converted to lists:
sage: m = magma('{* 1,2,2,2,4^^2,3 *}') # optional - magma
sage: z = m.sage(); z # optional - magma
[1, 2, 2, 2, 3, 4, 4]
sage: type(z) # optional - magma
<type 'list'>
Matrices:
sage: a = matrix(ZZ,3,3,[1..9])
sage: m = magma(a) # optional - magma
sage: b = m.sage(); b # optional - magma
[1 2 3]
[4 5 6]
[7 8 9]
sage: b == a # optional - magma
True
A nonsquare matrix:
sage: a = matrix(ZZ,2,3,[1..6])
sage: m = magma(a) # optional - magma
sage: m.sage() # optional - magma
[1 2 3]
[4 5 6]
EXAMPLES:
sage: G = magma.DirichletGroup(20) # optional - magma
sage: G.AssignNames(['a','b']) # optional - magma
sage: G.1 # optional - magma
a
sage: G.Elements() # optional - magma
[
1,
a,
b,
a*b
]
Evaluate self at the inputs.
INPUT:
OUTPUT: self(*args)
EXAMPLES:
sage: f = magma('Factorization') # optional - magma
sage: f.evaluate(15) # optional - magma
[ <3, 1>, <5, 1> ]
sage: f(15) # optional - magma
[ <3, 1>, <5, 1> ]
sage: f = magma('GCD') # optional - magma
sage: f.evaluate(15,20) # optional - magma
5
Evaluate self at the inputs.
INPUT:
OUTPUT: self(*args)
EXAMPLES:
sage: f = magma('Factorization') # optional - magma
sage: f.evaluate(15) # optional - magma
[ <3, 1>, <5, 1> ]
sage: f(15) # optional - magma
[ <3, 1>, <5, 1> ]
sage: f = magma('GCD') # optional - magma
sage: f.evaluate(15,20) # optional - magma
5
Return the n-th generator of this Magma element. Note that generators are 1-based in Magma rather than 0 based!
INPUT:
OUTPUT: MagmaElement
EXAMPLES:
sage: k.<a> = GF(9)
sage: magma(k).gen(1) # optional -- magma
a
sage: R.<s,t,w> = k[]
sage: m = magma(R) # optional -- magma
sage: m.gen(1) # optional -- magma
s
sage: m.gen(2) # optional -- magma
t
sage: m.gen(3) # optional -- magma
w
sage: m.gen(0) # optional -- magma
...
IndexError: index must be positive since Magma indexes are 1-based
sage: m.gen(4) # optional -- magma
...
IndexError: list index out of range
Return list of Magma variable names of the generators of self.
Note
As illustrated below, these are not the print names of the the generators of the Magma object, but special variable names in the Magma session that reference the generators.
EXAMPLES:
sage: R.<x,zw> = QQ[]
sage: S = magma(R) # optional - magma
sage: S.gen_names() # optional - magma
('_sage_[...]', '_sage_[...]')
sage: magma(S.gen_names()[1]) # optional - magma
zw
Return generators for self.
If self is named X is Magma, this function evaluates X.1, X.2, etc., in Magma until an error occurs. It then returns a Sage list of the resulting X.i. Note - I don’t think there is a Magma command that returns the list of valid X.i. There are numerous ad hoc functions for various classes but nothing systematic. This function gets around that problem. Again, this is something that should probably be reported to the Magma group and fixed there.
AUTHORS:
EXAMPLES:
sage: magma("VectorSpace(RationalField(),3)").gens() # optional - magma
[(1 0 0), (0 1 0), (0 0 1)]
sage: magma("AbelianGroup(EllipticCurve([1..5]))").gens() # optional - magma
[$.1]
Return value of a given Magma attribute. This is like selfattrname in Magma.
OUTPUT: MagmaElement
EXAMPLES:
sage: V = magma("VectorSpace(RationalField(),10)") # optional - magma
sage: V.set_magma_attribute('M','"hello"') # optional - magma
sage: V.get_magma_attribute('M') # optional - magma
hello
sage: V.M # optional - magma
hello
Return the ideal of self with given list of generators.
INPUT:
OUTPUT:
EXAMPLES:
sage: R = magma('PolynomialRing(RationalField())') # optional - magma
sage: R.assign_names(['x']) # optional - magma
sage: x = R.1 # optional - magma
sage: R.ideal([x^2 - 1, x^3 - 1]) # optional - magma
Ideal of Univariate Polynomial Ring in x over Rational Field generated by x - 1
Return the attributes of self, obtained by calling the ListAttributes function in Magma.
OUTPUT: list of strings
EXAMPLES: We observe that vector spaces in Magma have numerous funny and mysterious attributes.
sage: V = magma("VectorSpace(RationalField(),2)") # optional - magma
sage: V.list_attributes() # optional - magma
['Coroots', 'Roots', 'decomp', 'ssbasis', 'M', 'StrLocalData', 'eisen', 'weights', 'RootDatum', 'T', 'p']
Return signatures of all Magma intrinsics that can take self as the first argument, as strings.
INPUT:
OUTPUT: list of strings
EXAMPLES:
sage: v = magma('2/3').methods() # optional - magma
sage: v[0] # optional - magma
"'*'..."
Return the quotient of self by the given object or list of generators.
INPUT:
OUTPUT:
EXAMPLES:
sage: V = magma('VectorSpace(RationalField(),3)') # optional - magma
sage: V.quo([[1,2,3], [1,1,2]]) # optional - magma
(Full Vector space of degree 1 over Rational Field, Mapping from: Full Vector space of degree 3 over Rational Field to Full Vector space of degree 1 over Rational Field)
We illustrate quotienting out by an object instead of a list of generators:
sage: W = V.sub([ [1,2,3], [1,1,2] ]) # optional - magma
sage: V.quo(W) # optional - magma
(Full Vector space of degree 1 over Rational Field, Mapping from: Full Vector space of degree 3 over Rational Field to Full Vector space of degree 1 over Rational Field)
We quotient a ZZ module out by a submodule.
sage: V = magma.RModule(ZZ,3); V # optional - magma
RModule(IntegerRing(), 3)
sage: W, phi = V.quo([[1,2,3]]) # optional - magma
sage: W # optional - magma
RModule(IntegerRing(), 2)
sage: phi # optional - magma
Mapping from: RModule(IntegerRing(), 3) to RModule(IntegerRing(), 2)
INPUTS: attrname - string value - something coercible to a MagmaElement
EXAMPLES:
sage: V = magma("VectorSpace(RationalField(),2)") # optional - magma
sage: V.set_magma_attribute('M',10) # optional - magma
sage: V.get_magma_attribute('M') # optional - magma
10
sage: V.M # optional - magma
10
Return the sub-object of self with given gens.
INPUT:
EXAMPLES:
sage: V = magma('VectorSpace(RationalField(),3)') # optional - magma
sage: W = V.sub([ [1,2,3], [1,1,2] ]); W # optional - magma
Vector space of degree 3, dimension 2 over Rational Field
Generators:
(1 2 3)
(1 1 2)
Echelonized basis:
(1 0 1)
(0 1 1)
Return all Magma functions that have this Magma element as first input. This is used for tab completion.
Note
This function can unfortunately be slow if there are a very large number of functions, e.g., when self is an integer. (This could be fixed by the addition of an appropriate function to the Magma kernel, which is something that can only be done by the Magma developers.)
OUTPUT:
EXAMPLES:
sage: v = magma('2/3').trait_names() # optional - magma
sage: type(v[0]) # optional - magma
<type 'str'>
Return the result of calling this Magma function at given inputs.
Use the optional nvals keyword argument to specify that there are multiple return values.
EXAMPLES: We create a MagmaFunction:
sage: f = magma.Factorisation # optional - magma
sage: type(f) # optional - magma
<class 'sage.interfaces.magma.MagmaFunction'>
sage: f(-15) # optional - magma
[ <3, 1>, <5, 1> ]
We verify that the nvals argument works.
sage: f(-15, nvals=2) # optional - magma
([ <3, 1>, <5, 1> ], -1)
sage: f.__call__(-15, nvals=2) # optional - magma
([ <3, 1>, <5, 1> ], -1)
Return docstring about this function.
OUTPUT: string
EXAMPLES:
sage: f = magma.Factorisation
sage: type(f)
<class 'sage.interfaces.magma.MagmaFunction'>
sage: print f._sage_doc_() # optional - magma
Intrinsic 'Factorisation'
...
Return the result of calling this Magma function at given inputs.
Use the optional nvals keyword argument to specify that there are multiple return values.
EXAMPLES: We create a MagmaFunctionElement:
sage: n = magma(-15) # optional - magma
sage: f = n.Factorisation # optional - magma
sage: type(f) # optional - magma
<class 'sage.interfaces.magma.MagmaFunctionElement'>
sage: f() # optional - magma
[ <3, 1>, <5, 1> ]
We verify that the nvals argument works.
sage: f(nvals=2) # optional - magma
([ <3, 1>, <5, 1> ], -1)
This illustrates the more conventional way of calling a method on an object. It’s equivalent to the above, but done in all in one step.
sage: n.Factorization(nvals = 2) # optional - magma
([ <3, 1>, <5, 1> ], -1)
Return string representation of this partially evaluated function.
This is basically the docstring (as returned by _sage_doc_) unless self._name is the name of an attribute of the object, in which case this returns the value of that attribute.
EXAMPLES:
sage: magma(-15).Factorisation # optional - magma
Partially evaluated Magma function or intrinsic 'Factorisation'
...
We create a vector space, set its M attribute to a number, then display/get the attribute as a string.
sage: V = magma('VectorSpace(RationalField(),2)') # optional - magma
sage: V.set_magma_attribute('M', 290398) # optional - magma
sage: V.M # optional - magma
290398
sage: type(V.M) # optional - magma
<class 'sage.interfaces.magma.MagmaFunctionElement'>
sage: type(V.M.__repr__()) # optional - magma
<type 'str'>
Displaying a non-attribute function works as above.
sage: V.Dimension # optional - magma
Partially evaluated Magma function or intrinsic 'Dimension'
...
Return the docstring for this function of an element.
OUTPUT: string
EXAMPLES:
sage: n = magma(-15) # optional - magma
sage: f = n.Factorisation # optional - magma
sage: print f._sage_doc_() # optional - magma
(<RngIntElt> n) -> RngIntEltFact, RngIntElt, SeqEnum
...
sage: print n.Factorisation._sage_doc_() # optional - magma
(<RngIntElt> n) -> RngIntEltFact, RngIntElt, SeqEnum
...
Return directory that contains all the Magma extcode. This is put in a writable directory owned by the user, since when attached, Magma has to write sig and lck files.
Return True if x is of type MagmaElement, and False otherwise.
INPUT:
OUTPUT: bool
EXAMPLES:
sage: from sage.interfaces.magma import is_MagmaElement
sage: is_MagmaElement(2)
False
sage: is_MagmaElement(magma(2)) # optional - magma
True
Run a command line Magma session.
EXAMPLES:
sage: magma_console() # not tested
Magma V2.14-9 Sat Oct 11 2008 06:36:41 on one [Seed = 1157408761]
Type ? for help. Type <Ctrl>-D to quit.
>
Total time: 2.820 seconds, Total memory usage: 3.95MB
Return the version of Magma that you have in your PATH on your computer.
OUTPUT:
EXAMPLES:
sage: magma_version() # random, optional - magma
((2, 14, 9), 'V2.14-9')
Used in unpickling a Magma interface.
This functions just returns the global default Magma interface.
EXAMPLES:
sage: sage.interfaces.magma.reduce_load_Magma()
Magma