This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
magma.py
3022 lines (2287 loc) · 100 KB
/
magma.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
r"""
Interface to Magma
Sage provides an interface to the Magma computational algebra
system. This system provides extensive functionality for number
theory, group theory, combinatorics and algebra.
.. 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/.
The Magma interface offers three pieces of functionality:
#. ``magma_console()`` - A function that dumps you into an interactive command-line Magma session.
#. ``magma.new(obj)`` and alternatively ``magma(obj)`` - Creation of a Magma object from a Sage object ``obj``.
This provides a Pythonic interface to Magma. For example, if ``f=magma.new(10)``, then
``f.Factors()`` returns the prime factorization of 10 computed using Magma. If obj is a string containing
an arbitrary Magma expression, then the expression is evaluated in Magma to create a Magma object. An example
is ``magma.new('10 div 3')``, which returns Magma integer 3.
#. ``magma.eval(expr)`` - Evaluation of the Magma expression ``expr``, with the result returned as a string.
Type ``magma.[tab]`` for a list of all functions available from your Magma.
Type ``magma.Function?`` for Magma's help about the Magma ``Function``.
Parameters
----------
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
Multiple Return Values
----------------------
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])
Long Input
----------
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
Garbage Collection
------------------
There is a subtle point with the Magma interface, which arises from
how garbage collection works. Consider the following session:
First, create a matrix m in Sage::
sage: m=matrix(ZZ,2,[1,2,3,4]) # optional - magma
Then I create a corresponding matrix A in Magma::
sage: A = magma(m) # optional - magma
It is called _sage_[...] in Magma::
sage: s = A.name(); s # optional - magma
'_sage_[...]'
It's there::
sage: magma.eval(s) # optional - magma
'[1 2]\n[3 4]'
Now I delete the reference to that matrix::
sage: del A # optional - magma
Now _sage_[...] is "zeroed out" in the Magma session::
sage: magma.eval(s) # optional - magma
'0'
If Sage did not do this garbage collection, then every single time you
ever create any magma object from a sage object, e.g., by doing
magma(m), you would use up a lot of memory in that Magma session.
This would lead to a horrible memory leak situation, which would make
the Magma interface nearly useless for serious work.
Other Examples
--------------
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
Kronecker character -4 in modulus 40
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
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
$.1
sage: y * (2/3) # optional - magma
Traceback (most recent call last):
...
TypeError: Error evaluating Magma code.
...
Argument types given: RngUPolElt[RngInt], FldRatElt
AUTHORS:
- William Stein (2005): initial version
- William Stein (2006-02-28): added extensive tab completion and
interactive IPython documentation support.
- William Stein (2006-03-09): added nvals argument for
magma.functions...
"""
#*****************************************************************************
# Copyright (C) 2005 William Stein <wstein@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# The full text of the GPL is available at:
#
# http://www.gnu.org/licenses/
#*****************************************************************************
from __future__ import print_function
from __future__ import absolute_import
import re
import sys
from decorator import decorater
from sage.structure.parent import Parent
from .expect import console, Expect, ExpectElement, ExpectFunction, FunctionElement
PROMPT = ">>>"
SAGE_REF = "_sage_ref"
SAGE_REF_RE = re.compile(r'%s\d+' % SAGE_REF)
from sage.env import SAGE_EXTCODE, DOT_SAGE
import sage.misc.misc
import sage.misc.sage_eval
from sage.interfaces.tab_completion import ExtraTabCompletion
from sage.docs.instancedoc import instancedoc
INTRINSIC_CACHE = '%s/magma_intrinsic_cache.sobj' % DOT_SAGE
EXTCODE_DIR = None
def extcode_dir(iface=None):
"""
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.
EXAMPLES::
sage: sage.interfaces.magma.extcode_dir()
'...dir_.../data/'
"""
global EXTCODE_DIR
if not EXTCODE_DIR:
if iface is None or iface._server is None:
import shutil
tmp = sage.misc.temporary_file.tmp_dir()
shutil.copytree('%s/magma/' % SAGE_EXTCODE, tmp + '/data')
EXTCODE_DIR = "%s/data/" % tmp
else:
import os
tmp = iface._remote_tmpdir()
command = 'scp -q -r "%s/magma/" "%s:%s/data" 1>&2 2>/dev/null' % (SAGE_EXTCODE, iface._server, tmp)
try:
ans = os.system(command)
EXTCODE_DIR = "%s/data/" % tmp
if ans != 0:
raise IOError
except (OSError, IOError):
out_str = 'Tried to copy the file structure in "%s/magma/" to "%s:%s/data" and failed (possibly because scp is not installed in the system).\nFor the remote Magma to work you should populate the remote directory by some other method, or install scp in the system and retry.' % (SAGE_EXTCODE, iface._server, tmp)
from warnings import warn
warn(out_str)
return EXTCODE_DIR
class Magma(ExtraTabCompletion, Expect):
r"""
Interface to the Magma interpreter.
Type ``magma.[tab]`` for a list of all the functions
available from your Magma install. Type
``magma.Function?`` 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.
If you have ssh access to a remote installation of Magma, you can
also set the ``server`` parameter to use it.
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
"""
def __init__(self, script_subdirectory=None,
logfile=None, server=None, server_tmpdir=None,
user_config=False, seed=None, command=None):
"""
INPUT:
- ``script_subdirectory`` - directory where scripts
are read from
- ``logfile`` - output logged to this file
- ``server`` - address of remote server
- ``server_tmpdir`` - temporary directory to use in remote server
- ``user_config`` - if True, then local user
configuration files will be read by Magma. If False (the default),
then Magma is started with the -n option which suppresses user
configuration files.
- ``seed`` - Seed to use in the random number generator.
- ``command`` - (Default: 'magma') The command to execute to start Magma.
EXAMPLES::
sage: Magma(logfile=tmp_filename())
Magma
"""
if command is None:
import os
command = os.getenv('SAGE_MAGMA_COMMAND') or 'magma'
if not user_config:
command += ' -n'
# Obtain the parameters from the environment, to allow the magma = Magma() phrase
# to work with non-default parameters.
if seed is None:
import os
seed = os.getenv('SAGE_MAGMA_SEED')
Expect.__init__(self,
name="magma",
prompt=">>SAGE>>",
command=command,
server=server,
server_tmpdir=server_tmpdir,
script_subdirectory=script_subdirectory,
restart_on_ctrlc=False,
logfile=logfile,
eval_using_file_cutoff=100)
# We use "-n" above in the Magma startup command so
# local user startup configuration is not read.
self.__seq = 0
self.__ref = 0
self.__available_var = []
self.__cache = {}
self._preparse_colon_equals = False # if set to try, all "=" become ":=" (some users really appreciate this)
self._seed = seed
def set_seed(self, seed=None):
"""
Set the seed for the Magma interpreter.
The seed should be an integer.
EXAMPLES::
sage: m = Magma() # optional - magma
sage: m.set_seed(1) # optional - magma
1
sage: [m.Random(100) for i in range(5)] # optional - magma
[13, 55, 84, 100, 37]
"""
if seed is None:
seed = self.rand_seed()
self.eval('SetSeed(%d)' % seed)
self._seed = seed
return seed
def __reduce__(self):
"""
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
"""
return reduce_load_Magma, tuple([])
def _read_in_file_command(self, filename):
"""
Return the command in Magma that reads in the contents of the given
file.
INPUT:
- ``filename`` - string
OUTPUT:
- ``string`` - a magma command
EXAMPLES::
sage: magma._read_in_file_command('file.m')
'load "file.m";'
"""
return 'load "%s";' % filename
def _post_process_from_file(self, s):
r"""
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:
- ``s`` - a string
OUTPUT: a string
EXAMPLES::
sage: magma._post_process_from_file("Loading ...\nHello")
'Hello'
sage: magma._post_process_from_file("Hello")
''
"""
if not isinstance(s, str):
raise RuntimeError("Error evaluating object in %s:\n%s" % (self, s))
# Chop off the annoying "Loading ... " message that Magma
# always outputs no matter what.
i = s.find('\n')
if i == -1: # special case -- command produced no output, so no \n
return ''
return s[i + 1:]
def __getattr__(self, attrname):
"""
Return a formal wrapper around a Magma function, or raise an
AttributeError if attrname starts with an underscore.
INPUT:
- ``attrname`` - a string
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
"""
if attrname[:1] == "_":
raise AttributeError
return MagmaFunction(self, attrname)
def eval(self, x, strip=True, **kwds):
"""
Evaluate the given block x of code in Magma and return the output
as a string.
INPUT:
- ``x`` - string of code
- ``strip`` - ignored
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
''
Verify that :trac:`9705` is fixed::
sage: nl=chr(10) # newline character
sage: magma.eval( # optional - magma
....: "_<x>:=PolynomialRing(Rationals());"+nl+
....: "repeat"+nl+
....: " g:=3*b*x^4+18*c*x^3-6*b^2*x^2-6*b*c*x-b^3-9*c^2 where b:=Random([-10..10]) where c:=Random([-10..10]);"+nl+
....: "until g ne 0 and Roots(g) ne [];"+nl+
....: "print \"success\";")
'success'
Verify that :trac:`11401` is fixed::
sage: nl=chr(10) # newline character
sage: magma.eval("a:=3;"+nl+"b:=5;") == nl # optional - magma
True
sage: magma.eval("[a,b];") # optional - magma
'[ 3, 5 ]'
"""
x = self._preparse(x)
x = str(x).rstrip()
if len(x) == 0 or x[len(x) - 1] != ';':
x += ';'
ans = Expect.eval(self, x, **kwds).replace('\\\n', '')
if 'Runtime error' in ans or 'User error' in ans:
raise RuntimeError("Error evaluating Magma code.\nIN:%s\nOUT:%s" % (x, ans))
return ans
def _preparse(self, s):
"""
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;'
"""
try:
# this is in a try/except only because of the possibility
# of old pickled Magma interfaces.
if self._preparse_colon_equals:
s = s.replace(':=', '=').replace('=', ':=')
except AttributeError:
pass
return s
def _start(self):
"""
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
"""
self._change_prompt('>')
Expect._start(self)
self.eval('SetPrompt("%s"); SetLineEditor(false); SetColumns(0);' % PROMPT)
self._change_prompt(PROMPT)
self.expect().expect(PROMPT)
self.expect().expect(PROMPT)
self.expect().expect(PROMPT)
self.attach_spec(extcode_dir(self) + '/spec')
# set random seed
self.set_seed(self._seed)
def set(self, var, value):
"""
Set the variable var to the given value in the Magma interpreter.
INPUT:
- ``var`` - string; a variable name
- ``value`` - string; what to set var equal to
EXAMPLES::
sage: magma.set('abc', '2 + 3/5') # optional - magma
sage: magma('abc') # optional - magma
13/5
"""
out = self.eval("%s:=%s" % (var, value))
if out.lower().find("error") != -1:
raise TypeError("Error executing Magma code:\n%s" % out)
def get(self, var):
"""
Get the value of the variable var.
INPUT:
- ``var`` - string; name of a variable defined in the
Magma session
OUTPUT:
- ``string`` - string representation of the value of
the variable.
EXAMPLES::
sage: magma.set('abc', '2 + 3/5') # optional - magma
sage: magma.get('abc') # optional - magma
'13/5'
"""
return self.eval("%s" % var)
def objgens(self, value, gens):
"""
Create a new object with given value and gens.
INPUT:
- ``value`` - something coercible to an element of this Magma
interface
- ``gens`` - string; comma separated list of variable names
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
Order: Lexicographical
Variables: X, Y
sage: S # optional - magma
Polynomial ring of rank 2 over Rational Field
Order: Lexicographical
Variables: X, Y
"""
var = self._next_var_name()
value = self(value)
out = self.eval("_zsage_<%s> := %s; %s := _zsage_" % (gens,
value.name(),
var))
if out.lower().find("error") != -1:
raise TypeError("Error executing Magma code:\n%s" % out)
return self(var)
def __call__(self, x, gens=None):
"""
Coerce x into this Magma interpreter interface.
INPUT:
- ``x`` - object
- ``gens`` - string; names of generators of self,
separated by commas
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
Order: Lexicographical
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}
sage: P.<x,y> = PolynomialRing(GF(127)) # optional - magma
sage: m = Magma() # optional - magma
sage: m(P) # optional - magma
Polynomial ring of rank 2 over GF(127)
Order: Graded Reverse Lexicographical
Variables: x, y
sage: P._magma_cache # optional - magma
{Magma: Polynomial ring of rank 2 over GF(127)
Order: Graded Reverse Lexicographical
Variables: x, y}
"""
if isinstance(x, bool):
return Expect.__call__(self, 'true' if x else 'false')
if gens is not None: # get rid of this at some point -- it's weird
return self.objgens(x, gens)
# This is mostly about caching the Magma element in the object
# itself below. Note that it is *very* important that caching
# happen on the object itself, and not in a dictionary that is
# held by the Magma interface, since we want garbage collection
# of the objects in the Magma interface to work correctly.
has_cache = hasattr(x, '_magma_cache')
try:
if has_cache and self in x._magma_cache:
A = x._magma_cache[self]
if A._session_number == self._session_number:
return A
except AttributeError:
# This happens when x has _magma_cache as a cdef public object attribute.
x._magma_cache = {}
try:
if x in self.__cache:
A = self.__cache[x]
if A._session_number == self._session_number:
return A
except TypeError: # if x isn't hashable
pass
A = Expect.__call__(self, x)
if has_cache:
x._magma_cache[self] = A
else:
try: # use try/except here, because if x is cdef'd we won't be able to set this.
x._magma_cache = {self: A}
except AttributeError:
# Unfortunately, we *have* do have this __cache
# attribute, which can lead to "leaks" in the working
# Magma session. This is because it is critical that
# parent objects get cached, but sometimes they can't
# be cached in the object itself, because the object
# doesn't have a _magma_cache attribute. So in such
# cases when the object is a parent we cache it in
# the magma interface.
if isinstance(x, Parent):
self.__cache[x] = A
return A
def _coerce_from_special_method(self, x):
"""
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
Traceback (most recent call last):
...
AttributeError: 'str' object has no attribute '_magma_init_'
"""
s = x._magma_init_(self)
a = self(s)
# dereference all _sage_ref's used in this string.
while True:
z = SAGE_REF_RE.search(s)
if not z:
break
self.eval('delete %s;' % s[z.start():z.end()])
s = s[z.end()+1:]
return a
def _with_names(self, s, names):
"""
Return s but wrapped by a call to SageCreateWithNames. This is just
a very simple convenience function so that code is cleaner.
INPUT:
- ``s`` - string
- ``names`` - list of strings
OUTPUT: string
EXAMPLES::
sage: magma._with_names('PolynomialRing(RationalField())', ['y']) # optional - magma
'SageCreateWithNames(PolynomialRing(RationalField()),["y"])'
"""
return 'SageCreateWithNames(%s,[%s])' % (s, ','.join('"%s"' % x
for x in names))
def clear(self, var):
"""
Clear the variable named var and make it available to be used
again.
INPUT:
- ``var`` - a string
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
"""
self.__available_var.insert(0, var) # adds var to front of list
self.eval("%s:=0" % var)
def cputime(self, t=None):
"""
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:
- ``t`` - float (default: None); if not None, return
cputime since t
OUTPUT:
- ``float`` - seconds
EXAMPLES::
sage: type(magma.cputime()) # optional - magma
<... 'float'>
sage: magma.cputime() # random, optional - magma
1.9399999999999999
sage: t = magma.cputime() # optional - magma
sage: magma.cputime(t) # random, optional - magma
0.02
"""
if t:
return float(self.eval('Cputime(%s)' % t))
else:
return float(self.eval('Cputime()'))
def chdir(self, dir):
"""
Change the Magma interpreter's current working directory.
INPUT:
- ``dir`` -- a string
EXAMPLES::
sage: magma.chdir('/') # optional - magma
sage: magma.eval('System("pwd")') # optional - magma
'/'
"""
self.eval('ChangeDirectory("%s")' % dir, strip=False)
def attach(self, filename):
r"""
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:
- ``filename`` - a string
EXAMPLES: Attaching a file that exists is fine::
sage: SAGE_EXTCODE = SAGE_ENV['SAGE_EXTCODE'] # optional - magma
sage: magma.attach('%s/magma/sage/basic.m'%SAGE_EXTCODE) # optional - magma
Attaching a file that doesn't exist raises an exception::
sage: SAGE_EXTCODE = SAGE_ENV['SAGE_EXTCODE'] # optional - magma
sage: magma.attach('%s/magma/sage/basic2.m'%SAGE_EXTCODE) # optional - magma
Traceback (most recent call last):
...
RuntimeError: Error evaluating Magma code...
"""
self.eval('Attach("%s")' % filename)
Attach = attach
def attach_spec(self, filename):