CoCalc Shared Filesoctave.py.patchOpen in CoCalc with one click!
Authors: David Cyganski, Bill Page
Views : 12
Description: fix a few problems with the Sage octave interface
1
--- octave.py.old 2016-01-04 16:26:50.188082453 +0000
2
+++ octave.py 2016-01-05 16:54:42.400050103 +0000
3
@@ -153,6 +153,7 @@
4
5
import os
6
from expect import Expect, ExpectElement
7
+from sage.misc.misc import verbose
8
9
10
class Octave(Expect):
11
@@ -181,8 +182,10 @@
12
"""
13
Expect.__init__(self,
14
name = 'octave',
15
- prompt = '>',
16
- command = "sage-native-execute octave --no-line-editing --silent",
17
+ # We want the prompt sequence to be unique to avoid confusion with syntax error messages containing >>>
18
+ prompt = 'octave\:\d+> ',
19
+ # We don't want any pagination of output
20
+ command = "sage-native-execute octave --no-line-editing --silent --eval 'PS2(PS1());more off' --persist",
21
maxread = maxread,
22
server = server,
23
server_tmpdir = server_tmpdir,
24
@@ -270,6 +273,57 @@
25
octave starts up.
26
* Darwin ports and fink have Octave as well.
27
"""
28
+ def _eval_line(self, line, reformat=True, allow_use_file=False,
29
+ wait_for_prompt=True, restart_if_needed=False):
30
+ """
31
+ EXAMPLES::
32
+
33
+ sage: print octave._eval_line('2+2') #optional - octave
34
+ ans = 4
35
+ """
36
+ if not wait_for_prompt:
37
+ return Expect._eval_line(self, line)
38
+ if line == '':
39
+ return ''
40
+ if self._expect is None:
41
+ self._start()
42
+ if allow_use_file and len(line)>3000:
43
+ return self._eval_line_using_file(line)
44
+ try:
45
+ E = self._expect
46
+ # debug
47
+ # self._synchronize(cmd='1+%s\n')
48
+ verbose("in = '%s'"%line,level=3)
49
+ E.sendline(line)
50
+ E.expect(self._prompt)
51
+ out = E.before
52
+ # debug
53
+ verbose("out = '%s'"%out,level=3)
54
+ except EOF:
55
+ if self._quit_string() in line:
56
+ return ''
57
+ except KeyboardInterrupt:
58
+ self._keyboard_interrupt()
59
+ if reformat:
60
+ if 'syntax error' in out:
61
+ raise SyntaxError(out)
62
+ out = "\n".join(out.splitlines()[1:])
63
+ return out
64
+
65
+ def _keyboard_interrupt(self):
66
+ print "CntrlC: Interrupting %s..."%self
67
+ if self._restart_on_ctrlc:
68
+ try:
69
+ self._expect.close(force=1)
70
+ except pexpect.ExceptionPexpect as msg:
71
+ raise pexpect.ExceptionPexpect( "THIS IS A BUG -- PLEASE REPORT. This should never happen.\n" + msg)
72
+ self._start()
73
+ raise KeyboardInterrupt("Restarting %s (WARNING: all variables defined in previous session are now invalid)"%self)
74
+ else:
75
+ self._expect.send('\003') # control-c
76
+ #self._expect.expect(self._prompt)
77
+ #self._expect.expect(self._prompt)
78
+ raise KeyboardInterrupt("Ctrl-c pressed while running %s"%self)
79
80
def quit(self, verbose=False):
81
"""
82
@@ -348,7 +402,7 @@
83
"""
84
cmd = '%s=%s;'%(var,value)
85
out = self.eval(cmd)
86
- if out.find("error") != -1:
87
+ if out.find("error") != -1 or out.find("Error") != -1:
88
raise TypeError("Error executing code in Octave\nCODE:\n\t%s\nOctave ERROR:\n\t%s"%(cmd, out))
89
90
def get(self, var):
91
@@ -622,6 +676,10 @@
92
sage: matrix(ZZ, A) # optional - octave
93
[1 2]
94
[3 4]
95
+ sage: A = octave('[1,2;3,4.5]') # optional - octave
96
+ sage: matrix(RR, A) # optional - octave
97
+ [1.00000000000000 2.00000000000000]
98
+ [3.00000000000000 4.50000000000000]
99
"""
100
oc = self.parent()
101
if not self.ismatrix():
102
@@ -638,7 +696,15 @@
103
w = [[to_complex(x,R) for x in row] for row in w]
104
105
from sage.matrix.all import MatrixSpace
106
- return MatrixSpace(R, nrows, ncols)(w)
107
+ s = str(self).strip()
108
+ v = s.split('\n ')
109
+ nrows = len(v)
110
+ if nrows == 0:
111
+ return MatrixSpace(R,0,0)(0)
112
+ ncols = len(v[0].split())
113
+ M = MatrixSpace(R, nrows, ncols)
114
+ v = sum([[x for x in w.split()] for w in v], [])
115
+ return M(v)
116
117
def _vector_(self, R=None):
118
r"""
119