This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
/
fricas.py
738 lines (577 loc) · 23.8 KB
/
fricas.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
r"""
Interface to FriCAS
.. TODO::
- Evaluation using a file is not done. Any input line with more
than a few thousand characters would hang the system, so currently it
automatically raises an exception.
- All completions of a given command.
- Interactive help.
FriCAS is a free GPL-compatible (modified BSD license) general
purpose computer algebra system based on Axiom. The FriCAS
website can be found at http://fricas.sourceforge.net/.
AUTHORS:
- Mike Hansen (2009-02): Split off the FriCAS interface from
the Axiom interface.
If the string "error" (case insensitive) occurs in the output of
anything from FriCAS, a RuntimeError exception is raised.
EXAMPLES: We evaluate a very simple expression in FriCAS.
::
sage: fricas('3 * 5') # optional - fricas
15
sage: a = fricas(3) * fricas(5); a # optional - fricas
15
The type of a is FriCASElement, i.e., an element of the FriCAS
interpreter.
::
sage: type(a) # optional - fricas
<class 'sage.interfaces.fricas.FriCASElement'>
sage: a.parent() # optional - fricas
FriCAS
The underlying FriCAS type of a is also available, via the type
method::
sage: a.type() # optional - fricas
PositiveInteger
We factor `x^5 - y^5` in FriCAS in several different ways.
The first way yields a FriCAS object.
::
sage: F = fricas.factor('x^5 - y^5'); F # optional - fricas
4 3 2 2 3 4
- (y - x)(y + x y + x y + x y + x )
sage: type(F) # optional - fricas
<class 'sage.interfaces.fricas.FriCASElement'>
sage: F.type() # optional - fricas
Factored(Polynomial(Integer))
Note that FriCAS objects are normally displayed using "ASCII art".
::
sage: a = fricas(2/3); a # optional - fricas
2
-
3
sage: a = fricas('x^2 + 3/7'); a # optional - fricas
2 3
x + -
7
The ``fricas.eval`` command evaluates an expression in
FriCAS and returns the result as a string. This is exact as if we
typed in the given line of code to FriCAS; the return value is what
FriCAS would print out.
::
sage: print(fricas.eval('factor(x^5 - y^5)')) # optional - fricas
4 3 2 2 3 4
- (y - x)(y + x y + x y + x y + x )
We can create the polynomial `f` as a FriCAS polynomial,
then call the factor method on it. Notice that the notation
``f.factor()`` is consistent with how the rest of Sage
works.
::
sage: f = fricas('x^5 - y^5') # optional - fricas
sage: f^2 # optional - fricas
10 5 5 10
y - 2x y + x
sage: f.factor() # optional - fricas
4 3 2 2 3 4
- (y - x)(y + x y + x y + x y + x )
Control-C interruption works well with the FriCAS interface. For
example, try the following sum but with a much bigger range, and hit
control-C.
::
sage: f = fricas('(x^5 - y^5)^10000') # not tested - fricas
Interrupting FriCAS...
...
<type 'exceptions.TypeError'>: Ctrl-c pressed while running FriCAS
::
sage: fricas('1/100 + 1/101') # optional - fricas
201
-----
10100
sage: a = fricas('(1 + sqrt(2))^5'); a # optional - fricas
+-+
29\|2 + 41
TESTS:
We check to make sure the subst method works with keyword
arguments.
::
sage: a = fricas(x+2); a #optional - fricas
x + 2
sage: a.subst(x=3) #optional - fricas
5
We verify that FriCAS floating point numbers can be converted to
Python floats.
::
sage: float(fricas(2)) #optional - fricas
2.0
"""
###########################################################################
# Copyright (C) 2008 Mike Hansen <mhansen@gmail.com>
# 2007 Bill Page
# 2006 William Stein <wstein@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
# The full text of the GPL is available at:
#
# http://www.gnu.org/licenses/
###########################################################################
# from __future__ import print_function
# from __future__ import absolute_import
from sage.interfaces.axiom import PanAxiomElement, PanAxiomFunctionElement, PanAxiomExpectFunction
from sage.interfaces.tab_completion import ExtraTabCompletion
from sage.interfaces.expect import Expect, ExpectElement, FunctionElement, ExpectFunction
from sage.env import DOT_SAGE
import re
import six
FRICAS_SINGLE_LINE_START = 3 # where the output starts
FRICAS_MULTI_LINE_START = 2
FRICAS_LINE_LENGTH = 245 # length of a line
FRICAS_LINE_BREAK = "\r\n" # line ending
# beware that lisp distinguishes between ' and ".
FRICAS_INIT_CODE = (
")lisp (princ \"running FriCAS intialisation code\")",
")set functions compile on",
")set output length " + str(FRICAS_LINE_LENGTH),
")set message autoload off",
")lisp (setf |$ioHook|"
" (lambda (x &optional args)"
" (when (member x '(|startAlgebraOutput| |endOfAlgebraOutput|"
" |startKeyedMsg| |endOfKeyedMsg|))"
" (let ((typetime (member (car args) '(S2GL0012 S2GL0013 S2GL0014))))"
" (cond"
" ((and (eq x '|startKeyedMsg|) typetime) (princ \"|startTypeTime|\"))"
" ((and (eq x '|endOfKeyedMsg|) typetime) (princ \"|endOfTypeTime|\"))"
" (t (prin1 x))))"
" (princ #\\Newline))))")
FRICAS_LINENUMBER_OFF_CODE = ")lisp (setf |$IOindex| NIL)"
FRICAS_FIRST_PROMPT = "\(1\) -> "
FRICAS_LINENUMBER_OFF_PROMPT = "\(NIL\) -> "
class FriCAS(ExtraTabCompletion, Expect):
"""
Interface to a FriCAS interpreter.
"""
def __init__(self, name='fricas', command='fricas -nox -noclef',
script_subdirectory=None, logfile=None,
server=None, server_tmpdir=None):
"""
Create an instance of the FriCAS interpreter.
TESTS::
sage: fricas == loads(dumps(fricas))
True
"""
eval_using_file_cutoff = 600
assert max(len(c) for c in FRICAS_INIT_CODE) < eval_using_file_cutoff
self.__eval_using_file_cutoff = eval_using_file_cutoff
self._COMMANDS_CACHE = '%s/%s_commandlist_cache.sobj'%(DOT_SAGE, name)
Expect.__init__(self,
name = name,
prompt = FRICAS_FIRST_PROMPT,
command = command,
script_subdirectory = script_subdirectory,
server=server,
server_tmpdir=server_tmpdir,
restart_on_ctrlc = False,
verbose_start = False,
init_code = FRICAS_INIT_CODE,
logfile = logfile,
eval_using_file_cutoff=eval_using_file_cutoff)
def _start(self):
"""
Start the FriCAS interpreter and switch off the linenumbers.
EXAMPLES::
sage: a = FriCAS()
sage: a.is_running()
False
sage: a._start() #optional - axiom
sage: a.is_running() #optional - axiom
True
sage: a.quit() #optional - axiom
"""
Expect._start(self)
# switching off the line numbers also modified the prompt
self._prompt = FRICAS_LINENUMBER_OFF_PROMPT
self.eval(FRICAS_LINENUMBER_OFF_CODE)
def eval(self, code, strip=True, synchronize=False, locals=None, allow_use_file=True,
split_lines="nofile", **kwds):
"""Send the code to the FriCAS interpreter and return the pretty
printed output as a string.
INPUT:
- ``code`` -- text to evaluate
"""
output = Expect.eval(self, code, strip=strip, synchronize=synchronize,
locals=locals, allow_use_file=allow_use_file, split_lines=split_lines,
**kwds)
# print "running eval", code
# print output
m = re.match("\r\n\|startAlgebraOutput\|\r\n(.*)\r\n\|endOfAlgebraOutput\|", output, flags = re.DOTALL)
if m:
lines = m.groups()[0].split(FRICAS_LINE_BREAK)
if max(len(line) for line in lines) < FRICAS_LINE_LENGTH:
return "\r\n".join(line[FRICAS_SINGLE_LINE_START:] for line in lines)
else:
return "\r\n".join(line[FRICAS_MULTI_LINE_START:] for line in lines)
else:
# print "TODO"
return output
def set(self, var, value):
"""Set the variable var to the given value.
INPUT:
- var, value: strings, the first representing a valid FriCAS
variable identifier, the second a FriCAS expression.
OUTPUT: None
EXAMPLES::
sage: fricas.set('xx', '2') #optional - fricas
sage: fricas.get('xx') #optional - fricas
'2'
"""
# print "running set", var, value
if not isinstance(var, str) or not isinstance(value, str):
raise TypeError
self._eval_line('%s := %s;'%(var, value))
def get(self, var):
r"""
Get the string representation of the value of a variable.
This is only used for display.
EXAMPLES::
sage: fricas.set('xx', '2') #optional - fricas
sage: fricas.get('xx') #optional - fricas
'2'
sage: a = fricas('(1 + sqrt(2))^5') #optional - fricas
sage: fricas.get(a.name()) #optional - fricas
' +-+\r\n29\\|2 + 41'
"""
# print "running get", var
return self.eval(str(var))
def _assign_symbol(self):
"""Returns the symbol used for setting a variable.
This would be used if `self.set` from Interface would not be
overloaded.
"""
return ":="
def _equality_symbol(self):
"""Returns the equality testing logical symbol in FriCAS.
EXAMPLES::
sage: a = fricas(x==6); a #optional fricas
x= 6
"""
return "="
def __repr__(self):
"""
EXAMPLES::
sage: fricas
FriCAS
"""
return "FriCAS"
def __reduce__(self):
"""
EXAMPLES::
sage: fricas.__reduce__()
(<function reduce_load_fricas at 0x...>, ())
sage: f, args = _
sage: f(*args)
FriCAS
"""
return reduce_load_fricas, tuple([])
def _function_class(self):
"""
Return the FriCASExpectFunction class.
EXAMPLES::
sage: fricas._function_class()
<class 'sage.interfaces.fricas.FriCASExpectFunction'>
sage: type(fricas.gcd)
<class 'sage.interfaces.fricas.FriCASExpectFunction'>
"""
return FriCASExpectFunction
def _object_class(self):
"""
EXAMPLES::
sage: fricas._object_class()
<class 'sage.interfaces.fricas.FriCASElement'>
sage: type(fricas(2)) #optional - fricas
<class 'sage.interfaces.fricas.FriCASElement'>
"""
return FriCASElement
def _function_element_class(self):
"""
Returns the FriCAS function element class.
EXAMPLES::
sage: fricas._function_element_class()
<class 'sage.interfaces.fricas.FriCASFunctionElement'>
sage: type(fricas(2).gcd) #optional - fricas
<class 'sage.interfaces.fricas.FriCASFunctionElement'>
"""
return FriCASFunctionElement
def console(self):
"""
Spawn a new FriCAS command-line session.
EXAMPLES::
sage: fricas.console() #not tested
FriCAS (AXIOM fork) Computer Algebra System
Version: FriCAS 1.0.5
Timestamp: Thursday February 19, 2009 at 06:57:33
-----------------------------------------------------------------------------
Issue )copyright to view copyright notices.
Issue )summary for a summary of useful system commands.
Issue )quit to leave AXIOM and return to shell.
-----------------------------------------------------------------------------
"""
fricas_console()
class FriCASElement(ExpectElement):
def __getitem__(self, n):
raise NotImplementedError
def __int__(self):
return int(self.sage())
def bool(self):
raise NotImplementedError
def __nonzero__(self):
raise NotImplementedError
def __long__(self):
raise NotImplementedError
def __float__(self):
return float(self.sage())
def _integer_(self, ZZ=None):
raise NotImplementedError
def _rational_(self):
raise NotImplementedError
def gen(self, n):
raise NotImplementedError
def _get_1d_output(self):
"""Return the output from FriCAS as a string without the quotes.
TESTS:
We test that strings are returned properly:
sage: r = fricas('concat([concat(string(i)," ") for i in 0..299])')._get_1d_output()
sage: r == " ".join([str(i) for i in range(300)]) + ' '
True
sage: fricas('concat([string(1) for i in 1..5])')._get_1d_output() == "1"*5
True
sage: fricas('concat([string(1) for i in 1..10000])')._get_1d_output() == "1"*10000
True
"""
P = self._check_valid()
return P.eval('unparse(%s::InputForm)' %self._name).replace("\r\n", "")[1:-1]
def type(self):
"""
Return the type of the object in FriCAS as a FriCAS object.
"""
P = self._check_valid()
return P(self._get_type_str())
def _get_type_str(self):
P = self._check_valid()
output = P._eval_line(self._name + ";")
m = re.match("\r\n\|startTypeTime\|\r\n *Type: (.*)\r\n\|endOfTypeTime\|", output, flags = re.DOTALL)
return m.groups()[0]
def _get_type(self):
"""
TODO: use fricas.eval("dom(%s)") instead. Then we can also use
")set message type off" to get very clean communication!
"""
import ast
def to_list_of_lists(node):
if isinstance(node, ast.Name):
return node.id
elif isinstance(node, ast.Num):
return node.n
elif isinstance(node, ast.List):
return [to_list_of_lists(arg) for arg in node.elts]
elif isinstance(node, ast.Call):
return [node.func.id] + [to_list_of_lists(arg) for arg in node.args]
# the fricas union type displays as Union(domain, ...), or Union(fail: failed, ...)
# for ast.parse this would be a syntax error
A = ast.parse(self._get_type_str().replace("...", "_").replace(": ", "_"))
assert len(A.body) == 1
return to_list_of_lists(A.body[0].value)
def _get_sage_type(self, type):
from sage.rings.all import ZZ, QQ, QQbar, PolynomialRing, RDF
from sage.rings.fraction_field import FractionField
from sage.rings.finite_rings.integer_mod_ring import Integers
from sage.rings.real_mpfr import RealField
from sage.symbolic.ring import SR
if type in ["Integer", "NonNegativeInteger", "PositiveInteger"]:
return ZZ
elif type == "String":
return str
elif type == "Float":
prec = max(self.mantissa().length()._sage_(), 53)
return RealField(prec)
elif type == "DoubleFloat":
return RDF
elif type == "AlgebraicNumber":
return QQbar
elif isinstance(type, list):
if type[0] == "Union":
return self._get_sage_type(type[1])
elif type[0] == "IntegerMod":
return Integers(ZZ(type[1]))
elif type[0] == "Fraction":
return FractionField(self._get_sage_type(type[1]))
elif type[0] == "Expression":
return SR
elif type[0] == "Polynomial":
# this is a workaround, since in sage we always have to specify the variables
return SR
raise NotImplementedError
def _sage_(self):
"""
Convert self to a Sage object.
TESTS:
Floats:
sage: fricas(2.1234).sage() # optional - fricas
2.12340000000000
sage: _.parent() # optional - fricas
Real Field with 53 bits of precision
sage: a = RealField(100)(pi)
sage: fricas(a).sage() # optional - fricas
3.1415926535897932384626433833
sage: _.parent() # optional - fricas
Real Field with 100 bits of precision
sage: fricas(a).sage() == a # optional - fricas
True
sage: fricas(2.0).sage() # optional - fricas
2.00000000000000
sage: _.parent() # optional - fricas
Real Field with 53 bits of precision
Algebraic numbers:
sage: a = fricas('(1 + sqrt(2))^5'); a # optional - fricas
+-+
29\|2 + 41
sage: b = a.sage(); b # optional - fricas
82.0121933088198?
sage: b.radical_expression() # optional - fricas
29*sqrt(2) + 41
Integers modulo n:
sage: fricas("((42^17)^1783)::IntegerMod(5^(5^5))").sage() == Integers(5^(5^5))((42^17)^1783) # optional - fricas
True
We can also convert Fricas's polynomials to Sage polynomials.
sage: a = fricas(x^2 + 1); a._get_type() # optional - fricas
['Polynomial', 'Integer']
sage: a.sage() # optional - fricas
x^2 + 1
sage: _.parent() # optional - fricas
Univariate Polynomial Ring in x over Integer Ring
sage: fricas('x^2 + y^2 + 1/2').sage() # optional - fricas
y^2 + x^2 + 1/2
sage: _.parent() # optional - fricas
Multivariate Polynomial Ring in y, x over Rational Field
sage: a = fricas("1$Polynomial Integer").sage() # optional - fricas
1
Rational functions:
sage: fricas("x^2 + 1/z").sage() # optional - fricas
x^2 + 1/z
Expressions:
sage: fricas("sin(x+y)/exp(z)*log(1+%e)").sage() # optional - fricas
e^(-z)*log(e + 1)*sin(x + y)
sage: fricas("factorial(n)").sage() # optional - fricas
factorial(n)
sage: fricas("guessPade [1,1,2,3,5,8]") # optional - fricas
n 1
[[[x ]- ----------]]
2
x + x - 1
"""
from sage.rings.all import ZZ, QQ, QQbar, PolynomialRing, RDF
from sage.rings.fraction_field import FractionField
from sage.rings.finite_rings.integer_mod_ring import Integers
from sage.rings.real_mpfr import RealField
from sage.symbolic.ring import SR
from sage.misc.sage_eval import sage_eval
type = self._get_type()
if type in ["Integer", "NonNegativeInteger", "PositiveInteger"]:
return ZZ(self._get_1d_output())
elif type == "String":
return self._get_1d_output()
elif type == "Float":
prec = max(self.mantissa().length().sage(), 53)
R = RealField(prec)
x, e, b = self._get_1d_output().lstrip('float(').rstrip(')').split(',')
return R(ZZ(x)*ZZ(b)**ZZ(e))
elif type == "DoubleFloat":
return RDF(self._get_1d_output())
elif type == "AlgebraicNumber":
s = self._get_1d_output()[:-len("::AlgebraicNumber()")]
return sage_eval("QQbar(" + s + ")")
elif isinstance(type, list):
if type[0] == "List":
P = self._check_valid()
n = P.new('# %s'%self.name()).sage()
return [P.new('%s(%s)'%(self._name, k)).sage() for k in range(1, n+1)]
elif type[0] == "IntegerMod":
# one might be tempted not to go via unparse and
# InputForm here, but it turns out to be safer to do
# it.
n = self._get_1d_output()[len("index("):]
n = n[:n.find(")")]
return self._get_sage_type(type)(n)
elif type[0] == "Fraction":
return self.numer().sage()/self.denom().sage()
elif type[0] == "Polynomial":
base_ring = self._get_sage_type(type[1])
vars = self.variables()._get_1d_output()[1:-1]
if vars == "":
return base_ring(self._get_1d_output())
else:
R = PolynomialRing(base_ring, vars)
return R(self._get_1d_output())
elif type[0] == "Expression":
if type[1] == "Integer":
s = self._get_1d_output()
return SR(s)
elif type[0] == 'DistributedMultivariatePolynomial':
base_ring = self._get_sage_type(type[2])
vars = type[1]
R = PolynomialRing(base_ring, vars)
return R(self._get_1d_output())
class FriCASFunctionElement(PanAxiomFunctionElement):
pass
class FriCASExpectFunction(PanAxiomExpectFunction):
pass
def is_FriCASElement(x):
"""
Return True if x is of type FriCASElement.
EXAMPLES::
sage: from sage.interfaces.fricas import is_FriCASElement #optional - fricas
sage: is_FriCASElement(fricas(2)) #optional - fricas
True
sage: is_FriCASElement(2) #optional - fricas
False
"""
return isinstance(x, FriCASElement)
fricas = FriCAS()
def reduce_load_fricas():
"""
Returns the FriCAS interface object defined in
sage.interfaces.fricas.
EXAMPLES::
sage: from sage.interfaces.fricas import reduce_load_fricas
sage: reduce_load_fricas()
FriCAS
"""
return fricas
import os
def fricas_console():
"""
Spawn a new FriCAS command-line session.
EXAMPLES::
sage: fricas_console() #not tested
FriCAS (AXIOM fork) Computer Algebra System
Version: FriCAS 1.0.5
Timestamp: Thursday February 19, 2009 at 06:57:33
-----------------------------------------------------------------------------
Issue )copyright to view copyright notices.
Issue )summary for a summary of useful system commands.
Issue )quit to leave AXIOM and return to shell.
-----------------------------------------------------------------------------
"""
from sage.repl.rich_output.display_manager import get_display_manager
if not get_display_manager().is_in_terminal():
raise RuntimeError('Can use the console only in the terminal. Try %%fricas magics instead.')
os.system('fricas -nox')
def __doctest_cleanup():
"""
EXAMPLES::
sage: from sage.interfaces.fricas import __doctest_cleanup #optional - fricas
sage: a = FriCAS() #optional - fricas
sage: two = a(2) #optional - fricas
sage: a.is_running() #optional - fricas
True
sage: __doctest_cleanup() #optional - fricas
sage: a.is_running() #optional - fricas
False
"""
import sage.interfaces.quit
sage.interfaces.quit.expect_quitall()