passagemath-giac 10.5.33__cp311-cp311-musllinux_1_2_aarch64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- passagemath_giac-10.5.33.dist-info/METADATA +153 -0
- passagemath_giac-10.5.33.dist-info/RECORD +34 -0
- passagemath_giac-10.5.33.dist-info/WHEEL +5 -0
- passagemath_giac-10.5.33.dist-info/top_level.txt +2 -0
- passagemath_giac.libs/libcrypto-79ca7c2e.so.3 +0 -0
- passagemath_giac.libs/libcurl-16a78e59.so.4.8.0 +0 -0
- passagemath_giac.libs/libgcc_s-69c45f16.so.1 +0 -0
- passagemath_giac.libs/libgfortran-db0b6589.so.5.0.0 +0 -0
- passagemath_giac.libs/libgiac-6d0de4c7.so.0.0.0 +0 -0
- passagemath_giac.libs/libglpk-aec1f3c8.so.40.3.1 +0 -0
- passagemath_giac.libs/libgmp-8e78bd9b.so.10.5.0 +0 -0
- passagemath_giac.libs/libgsl-75bb3fcc.so.28.0.0 +0 -0
- passagemath_giac.libs/libgslcblas-e0debfeb.so.0.0.0 +0 -0
- passagemath_giac.libs/libintl-3f4788bb.so.8.4.0 +0 -0
- passagemath_giac.libs/libmpfi-8dd9bae1.so.0.0.0 +0 -0
- passagemath_giac.libs/libmpfr-5ff10580.so.6.2.1 +0 -0
- passagemath_giac.libs/libncursesw-13f4e49c.so.6.5 +0 -0
- passagemath_giac.libs/libopenblas_neoversen2p-r0-05b86cab.3.28.so +0 -0
- passagemath_giac.libs/libpari-gmp-tls-e657f88e.so.2.15.5 +0 -0
- passagemath_giac.libs/libreadline-ae76ac25.so.8.2 +0 -0
- passagemath_giac.libs/libssl-7d993165.so.3 +0 -0
- passagemath_giac.libs/libstdc++-1f1a71be.so.6.0.33 +0 -0
- sage/all__sagemath_giac.py +10 -0
- sage/interfaces/all__sagemath_giac.py +1 -0
- sage/interfaces/giac.py +1264 -0
- sage/libs/all__sagemath_giac.py +7 -0
- sage/libs/giac/__init__.py +356 -0
- sage/libs/giac/auto-methods.pxi +16982 -0
- sage/libs/giac/giac.cpython-311-aarch64-linux-musl.so +0 -0
- sage/libs/giac/giac.pxd +205 -0
- sage/libs/giac/giac.pyx +2074 -0
- sage/libs/giac/keywords.pxi +10 -0
- sage/libs/giac/misc.h +117 -0
- sage_wheels/bin/giac +0 -0
sage/interfaces/giac.py
ADDED
@@ -0,0 +1,1264 @@
|
|
1
|
+
# sage_setup: distribution = sagemath-giac
|
2
|
+
# sage.doctest: needs giac
|
3
|
+
r"""
|
4
|
+
Pexpect Interface to Giac
|
5
|
+
|
6
|
+
(You should prefer the cython interface: giacpy_sage and its libgiac command)
|
7
|
+
|
8
|
+
(adapted by F. Han from William Stein and Gregg Musiker maple's interface)
|
9
|
+
|
10
|
+
You must have the Giac interpreter installed
|
11
|
+
and available as the command ``giac`` in your PATH in
|
12
|
+
order to use this interface. You need a giac version
|
13
|
+
supporting "giac --sage" ( roughly after 0.9.1 ). In this case you do not have
|
14
|
+
to install any optional Sage packages. If giac is not already installed, you can
|
15
|
+
download binaries or sources or spkg (follow the sources link) from the homepage:
|
16
|
+
|
17
|
+
Homepage <https://www-fourier.ujf-grenoble.fr/~parisse/giac.html>
|
18
|
+
|
19
|
+
Type ``giac.[tab]`` for a list of all the functions
|
20
|
+
available from your Giac install. Type
|
21
|
+
``giac.[tab]?`` for Giac's help about a given
|
22
|
+
function. Type ``giac(...)`` to create a new Giac
|
23
|
+
object, and ``giac.eval(...)`` to run a string using
|
24
|
+
Giac (and get the result back as a string).
|
25
|
+
|
26
|
+
If the giac spkg is installed, you should find the full html documentation there::
|
27
|
+
|
28
|
+
$SAGE_LOCAL/share/giac/doc/en/cascmd_local/index.html
|
29
|
+
|
30
|
+
EXAMPLES::
|
31
|
+
|
32
|
+
sage: giac('3 * 5')
|
33
|
+
15
|
34
|
+
sage: giac.eval('ifactor(2005)')
|
35
|
+
'5*401'
|
36
|
+
sage: giac.ifactor(2005)
|
37
|
+
2005
|
38
|
+
sage: l=giac.ifactors(2005) ; l; l[2]
|
39
|
+
[5,1,401,1]
|
40
|
+
401
|
41
|
+
sage: giac.fsolve('x^2=cos(x)+4', 'x','0..5')
|
42
|
+
[1.9140206190...
|
43
|
+
sage: giac.factor('x^4 - y^4')
|
44
|
+
(x-y)*(x+y)*(x^2+y^2)
|
45
|
+
sage: R.<x,y>=QQ[];f=(x+y)^5;f2=giac(f);(f-f2).normal()
|
46
|
+
0
|
47
|
+
sage: x,y=giac('x,y'); giac.int(y/(cos(2*x)+cos(x)),x) # random
|
48
|
+
y*2*((-(tan(x/2)))/6+(-2*1/6/sqrt(3))*ln(abs(6*tan(x/2)-2*sqrt(3))/abs(6*tan(x/2)+2*sqrt(3))))
|
49
|
+
|
50
|
+
|
51
|
+
If the string "error" (case insensitive) occurs in the output of
|
52
|
+
anything from Giac, a :exc:`RuntimeError` exception is raised.
|
53
|
+
|
54
|
+
Tutorial
|
55
|
+
--------
|
56
|
+
|
57
|
+
AUTHORS:
|
58
|
+
|
59
|
+
- Gregg Musiker (2006-02-02): initial version.
|
60
|
+
|
61
|
+
- Frederic Han: adapted to giac.
|
62
|
+
|
63
|
+
- Marcelo Forets (2017-04-06): conversions and cleanup.
|
64
|
+
|
65
|
+
This tutorial is based on the Maple Tutorial for number theory from
|
66
|
+
http://www.math.mun.ca/~drideout/m3370/numtheory.html.
|
67
|
+
|
68
|
+
Syntax
|
69
|
+
~~~~~~~
|
70
|
+
|
71
|
+
There are several ways to use the Giac Interface in Sage. We will
|
72
|
+
discuss two of those ways in this tutorial.
|
73
|
+
|
74
|
+
|
75
|
+
#. If you have a giac expression such as
|
76
|
+
|
77
|
+
::
|
78
|
+
|
79
|
+
factor( (x^4-1));
|
80
|
+
|
81
|
+
We can write that in sage as
|
82
|
+
|
83
|
+
::
|
84
|
+
|
85
|
+
sage: giac('factor(x^4-1)')
|
86
|
+
(x-1)*(x+1)*(x^2+1)
|
87
|
+
|
88
|
+
Notice, there is no need to use a semicolon.
|
89
|
+
|
90
|
+
#. Since Sage is written in Python, we can also import giac
|
91
|
+
commands and write our scripts in a pythonic way. For example,
|
92
|
+
``factor()`` is a giac command, so we can also factor
|
93
|
+
in Sage using
|
94
|
+
|
95
|
+
::
|
96
|
+
|
97
|
+
sage: giac('(x^4-1)').factor()
|
98
|
+
(x-1)*(x+1)*(x^2+1)
|
99
|
+
|
100
|
+
where ``expression.command()`` means the same thing as
|
101
|
+
``command(expression)`` in Giac. We will use this
|
102
|
+
second type of syntax whenever possible, resorting to the first
|
103
|
+
when needed.
|
104
|
+
|
105
|
+
::
|
106
|
+
|
107
|
+
sage: giac('(x^12-1)/(x-1)').normal()
|
108
|
+
x^11+x^10+x^9+x^8+x^7+x^6+x^5+x^4+x^3+x^2+x+1
|
109
|
+
|
110
|
+
Some typical input
|
111
|
+
~~~~~~~~~~~~~~~~~~
|
112
|
+
|
113
|
+
The normal command will reduce a rational function to the
|
114
|
+
lowest terms. In giac, simplify is slower than normal because it
|
115
|
+
tries more sophisticated simplifications (ex algebraic extensions)
|
116
|
+
The factor command will factor a polynomial with
|
117
|
+
rational coefficients into irreducible factors over the ring of
|
118
|
+
integers (if your default configuration of giac (cf .xcasrc) has not
|
119
|
+
allowed square roots). So for example,
|
120
|
+
|
121
|
+
|
122
|
+
::
|
123
|
+
|
124
|
+
sage: giac('(x^12-1)').factor( )
|
125
|
+
(x-1)*(x+1)*(x^2+1)*(x^2-x+1)*(x^2+x+1)*(x^4-x^2+1)
|
126
|
+
|
127
|
+
::
|
128
|
+
|
129
|
+
sage: giac('(x^28-1)').factor( )
|
130
|
+
(x-1)*(x+1)*(x^2+1)*(x^6-x^5+x^4-x^3+x^2-x+1)*(x^6+x^5+x^4+x^3+x^2+x+1)*(x^12-x^10+x^8-x^6+x^4-x^2+1)
|
131
|
+
|
132
|
+
Giac console
|
133
|
+
~~~~~~~~~~~~~
|
134
|
+
|
135
|
+
Another important feature of giac is its online help. We can
|
136
|
+
access this through sage as well. After reading the description of
|
137
|
+
the command, you can press :kbd:`q` to immediately get back to your
|
138
|
+
original prompt.
|
139
|
+
|
140
|
+
Incidentally you can always get into a giac console by the
|
141
|
+
command ::
|
142
|
+
|
143
|
+
sage: giac.console() # not tested
|
144
|
+
sage: !giac # not tested
|
145
|
+
|
146
|
+
Note that the above two commands are slightly different, and the
|
147
|
+
first is preferred.
|
148
|
+
|
149
|
+
For example, for help on the giac command factors, we type ::
|
150
|
+
|
151
|
+
sage: giac.help('factors') # not tested
|
152
|
+
|
153
|
+
::
|
154
|
+
|
155
|
+
sage: # needs sage.symbolic
|
156
|
+
sage: alpha = giac((1+sqrt(5))/2)
|
157
|
+
sage: beta = giac(1-sqrt(5))/2
|
158
|
+
sage: f19 = alpha^19 - beta^19/sqrt(5)
|
159
|
+
sage: f19
|
160
|
+
(sqrt(5)/2+1/2)^19-((-sqrt(5)+1)/2)^19/sqrt(5)
|
161
|
+
sage: (f19-(5778*sqrt(5)+33825)/5).normal()
|
162
|
+
0
|
163
|
+
|
164
|
+
Function definitions
|
165
|
+
~~~~~~~~~~~~~~~~~~~~
|
166
|
+
|
167
|
+
Let's say we want to write a giac program now that squares a
|
168
|
+
number if it is positive and cubes it if it is negative. In giac,
|
169
|
+
that would look like
|
170
|
+
|
171
|
+
::
|
172
|
+
|
173
|
+
mysqcu := proc(x)
|
174
|
+
if x > 0 then x^2;
|
175
|
+
else x^3; fi;
|
176
|
+
end;
|
177
|
+
|
178
|
+
In Sage, we write
|
179
|
+
|
180
|
+
::
|
181
|
+
|
182
|
+
sage: mysqcu = giac('proc(x) if x > 0 then x^2 else x^3 fi end')
|
183
|
+
sage: mysqcu(5)
|
184
|
+
25
|
185
|
+
sage: mysqcu(-5)
|
186
|
+
-125
|
187
|
+
|
188
|
+
More complicated programs should be put in a separate file and
|
189
|
+
loaded.
|
190
|
+
|
191
|
+
Conversions
|
192
|
+
~~~~~~~~~~~~
|
193
|
+
|
194
|
+
The ``GiacElement.sage()`` method tries to convert a Giac object to a Sage
|
195
|
+
object. In many cases, it will just work. In particular, it should be able to
|
196
|
+
convert expressions entirely consisting of:
|
197
|
+
|
198
|
+
- numbers, i.e. integers, floats, complex numbers;
|
199
|
+
- functions and named constants also present in Sage, where Sage knows how to
|
200
|
+
translate the function or constant's name from Giac's
|
201
|
+
- symbolic variables whose names don't pathologically overlap with
|
202
|
+
objects already defined in Sage.
|
203
|
+
|
204
|
+
This method will not work when Giac's output includes functions unknown to Sage.
|
205
|
+
|
206
|
+
If you want to convert more complicated Giac expressions, you can
|
207
|
+
instead call ``GiacElement._sage_()`` and supply a translation dictionary::
|
208
|
+
|
209
|
+
sage: g = giac('NewFn(x)')
|
210
|
+
sage: g._sage_(locals={('NewFn', 1): sin}) # needs sage.symbolic
|
211
|
+
sin(x)
|
212
|
+
|
213
|
+
Moreover, new conversions can be permanently added using Pynac's
|
214
|
+
``register_symbol``, and this is the recommended approach for library code.
|
215
|
+
For more details, see the documentation for ``._sage_()``.
|
216
|
+
|
217
|
+
TESTS:
|
218
|
+
|
219
|
+
Test that conversion of symbolic functions with latex names works (:issue:`31047`)::
|
220
|
+
|
221
|
+
sage: # needs sage.symbolic
|
222
|
+
sage: var('phi')
|
223
|
+
phi
|
224
|
+
sage: function('Cp', latex_name='C_+')
|
225
|
+
Cp
|
226
|
+
sage: test = Cp(phi)._giac_()._sage_()
|
227
|
+
sage: test.operator() == Cp
|
228
|
+
True
|
229
|
+
sage: test.operator()._latex_() == 'C_+'
|
230
|
+
True
|
231
|
+
"""
|
232
|
+
|
233
|
+
#############################################################################
|
234
|
+
# Copyright (C) 2005 William Stein <wstein@gmail.com>
|
235
|
+
#
|
236
|
+
# Distributed under the terms of the GNU General Public License (GPL)
|
237
|
+
#
|
238
|
+
# https://www.gnu.org/licenses/
|
239
|
+
#############################################################################
|
240
|
+
|
241
|
+
import os
|
242
|
+
|
243
|
+
from sage.interfaces.expect import Expect, ExpectElement, ExpectFunction, FunctionElement, gc_disabled
|
244
|
+
|
245
|
+
import pexpect
|
246
|
+
|
247
|
+
from sage.cpython.string import bytes_to_str
|
248
|
+
from sage.env import DOT_SAGE
|
249
|
+
from sage.misc.pager import pager
|
250
|
+
from sage.misc.instancedoc import instancedoc
|
251
|
+
from sage.structure.richcmp import rich_to_bool
|
252
|
+
|
253
|
+
|
254
|
+
COMMANDS_CACHE = '%s/giac_commandlist_cache.sobj' % DOT_SAGE
|
255
|
+
|
256
|
+
|
257
|
+
class Giac(Expect):
|
258
|
+
r"""
|
259
|
+
Interface to the Giac interpreter.
|
260
|
+
|
261
|
+
You must have the optional Giac interpreter installed and available as the command ``giac`` in your PATH in order to use this interface. Try the command: print(giac._install_hints()) for more informations on giac installation.
|
262
|
+
|
263
|
+
Type ``giac.[tab]`` for a list of all the functions available from your Giac install.
|
264
|
+
Type ``giac.[tab]?`` for Giac's help about a given function.
|
265
|
+
Type ``giac(...)`` to create a new Giac object.
|
266
|
+
|
267
|
+
Full html documentation for giac is available from your giac installation at ``$PREFIX``/share/giac/doc/en/cascmd_en/index.html
|
268
|
+
|
269
|
+
EXAMPLES:
|
270
|
+
|
271
|
+
Any Giac instruction can be evaluated as a string by the giac command. You can access the giac functions by adding the ``giac.`` prefix to the usual Giac name.
|
272
|
+
|
273
|
+
::
|
274
|
+
|
275
|
+
sage: l=giac('normal((y+sqrt(2))^4)'); l
|
276
|
+
y^4+4*sqrt(2)*y^3+12*y^2+8*sqrt(2)*y+4
|
277
|
+
sage: f=giac('(u,v)->{ if (u<v){ [u,v] } else { [v,u] }}');f(1,2),f(3,1)
|
278
|
+
([1,2], [1,3])
|
279
|
+
|
280
|
+
The output of the giac command is a Giac object, and it can be used for another giac command.
|
281
|
+
|
282
|
+
::
|
283
|
+
|
284
|
+
sage: l.factors()
|
285
|
+
[y+sqrt(2),4]
|
286
|
+
sage: giac('(x^12-1)').factor( )
|
287
|
+
(x-1)*(x+1)*(x^2+1)*(x^2-x+1)*(x^2+x+1)*(x^4-x^2+1)
|
288
|
+
sage: giac('assume(y>0)'); giac('y^2=3').solve('y')
|
289
|
+
y
|
290
|
+
...[sqrt(3)]
|
291
|
+
|
292
|
+
You can create some Giac elements and avoid many quotes like this:
|
293
|
+
|
294
|
+
::
|
295
|
+
|
296
|
+
sage: # needs sage.symbolic
|
297
|
+
sage: x,y,z=giac('x,y,z');type(y)
|
298
|
+
<class 'sage.interfaces.giac.GiacElement'>
|
299
|
+
sage: I1=(1/(cos(2*y)+cos(y))).integral(y,0,pi/4).simplify()
|
300
|
+
sage: (I1-((-2*ln((sqrt(3)-3*tan(1/8*pi))/(sqrt(3)+3*tan(1/8*pi)))*sqrt(3)-3*tan(1/8*pi))/9)).normal()
|
301
|
+
0
|
302
|
+
sage: ((y+z*sqrt(5))*(y-sqrt(5)*z)).normal()
|
303
|
+
y^2-5*z^2
|
304
|
+
|
305
|
+
Polynomials or elements of SR can be evaluated directly by the giac interface.
|
306
|
+
|
307
|
+
::
|
308
|
+
|
309
|
+
sage: R.<a,b> = QQ[]; f = (2+a+b)
|
310
|
+
sage: p = giac.gcd(f^3+5*f^5,f^2+f^5); p; R(p.sage()) # needs sage.symbolic
|
311
|
+
sageVARa^2+2*sageVARa*sageVARb+4*sageVARa+sageVARb^2+4*sageVARb+4
|
312
|
+
a^2 + 2*a*b + b^2 + 4*a + 4*b + 4
|
313
|
+
|
314
|
+
Variable names in python and giac are independent::
|
315
|
+
|
316
|
+
sage: a = sqrt(2); giac('Digits:=30;a:=5'); a,giac('a'),giac(a),giac(a).evalf() # needs sage.symbolic
|
317
|
+
30
|
318
|
+
(sqrt(2), 5, sqrt(2), 1.41421356237309504880168872421)
|
319
|
+
|
320
|
+
TESTS::
|
321
|
+
|
322
|
+
sage: g = giac('euler_gamma').sage();g # needs sage.symbolic
|
323
|
+
euler_gamma
|
324
|
+
sage: g.n() # needs sage.symbolic
|
325
|
+
0.577215664901533
|
326
|
+
"""
|
327
|
+
def __init__(self, maxread=None, script_subdirectory=None, server=None, server_tmpdir=None, logfile=None):
|
328
|
+
"""
|
329
|
+
Create an instance of the Giac interpreter.
|
330
|
+
|
331
|
+
EXAMPLES::
|
332
|
+
|
333
|
+
sage: giac == loads(dumps(giac))
|
334
|
+
True
|
335
|
+
"""
|
336
|
+
Expect.__init__(self,
|
337
|
+
name='giac',
|
338
|
+
prompt='[0-9]*>> ',
|
339
|
+
command="giac --sage",
|
340
|
+
env={"LANG": "C"},
|
341
|
+
init_code=['maple_mode(0);I:=i;'], # coercion could be broken in maple_mode
|
342
|
+
script_subdirectory=script_subdirectory,
|
343
|
+
restart_on_ctrlc=False,
|
344
|
+
server=server,
|
345
|
+
server_tmpdir=server_tmpdir,
|
346
|
+
verbose_start=False,
|
347
|
+
logfile=logfile,
|
348
|
+
eval_using_file_cutoff=1000)
|
349
|
+
|
350
|
+
def _function_class(self):
|
351
|
+
"""
|
352
|
+
EXAMPLES::
|
353
|
+
|
354
|
+
sage: giac._function_class()
|
355
|
+
<class 'sage.interfaces.giac.GiacFunction'>
|
356
|
+
|
357
|
+
::
|
358
|
+
|
359
|
+
sage: type(giac.diff)
|
360
|
+
<class 'sage.interfaces.giac.GiacFunction'>
|
361
|
+
"""
|
362
|
+
return GiacFunction
|
363
|
+
|
364
|
+
def _keyboard_interrupt(self):
|
365
|
+
"""
|
366
|
+
The pexepect interface for giac has a very poor support of keyboard interruptions.
|
367
|
+
"""
|
368
|
+
print("Interrupting %s..." % self)
|
369
|
+
self._expect.sendline(chr(3)) # send ctrl-c
|
370
|
+
self._expect.expect(self._prompt)
|
371
|
+
# self._expect.expect(self._prompt)
|
372
|
+
raise RuntimeError("Ctrl-c pressed while running %s" % self)
|
373
|
+
|
374
|
+
def __reduce__(self):
|
375
|
+
"""
|
376
|
+
EXAMPLES::
|
377
|
+
|
378
|
+
sage: Giac().__reduce__()
|
379
|
+
(<function reduce_load_Giac at 0x...>, ())
|
380
|
+
sage: f, args = _
|
381
|
+
sage: f(*args)
|
382
|
+
Giac
|
383
|
+
"""
|
384
|
+
return reduce_load_Giac, tuple([])
|
385
|
+
|
386
|
+
def _read_in_file_command(self, filename):
|
387
|
+
r"""
|
388
|
+
Return the string used to read filename into Giac.
|
389
|
+
|
390
|
+
EXAMPLES::
|
391
|
+
|
392
|
+
sage: giac._read_in_file_command('test')
|
393
|
+
'read "test"'
|
394
|
+
|
395
|
+
::
|
396
|
+
|
397
|
+
sage: filename = tmp_filename()
|
398
|
+
sage: with open(filename,'w') as f:
|
399
|
+
....: _ = f.write('xx := 22;\n')
|
400
|
+
sage: giac.read(filename)
|
401
|
+
sage: giac.get('xx').strip()
|
402
|
+
'22'
|
403
|
+
"""
|
404
|
+
return 'read "%s"' % filename
|
405
|
+
|
406
|
+
def _quit_string(self):
|
407
|
+
"""
|
408
|
+
EXAMPLES::
|
409
|
+
|
410
|
+
sage: giac._quit_string()
|
411
|
+
'@d'
|
412
|
+
|
413
|
+
::
|
414
|
+
|
415
|
+
sage: m = Giac()
|
416
|
+
sage: a = m(2)
|
417
|
+
sage: m.is_running()
|
418
|
+
True
|
419
|
+
sage: m.quit()
|
420
|
+
sage: m.is_running()
|
421
|
+
False
|
422
|
+
"""
|
423
|
+
return '@d'
|
424
|
+
|
425
|
+
def _install_hints(self):
|
426
|
+
"""
|
427
|
+
Hints for installing Giac on your computer.
|
428
|
+
|
429
|
+
EXAMPLES::
|
430
|
+
|
431
|
+
sage: print(giac._install_hints())
|
432
|
+
In order...
|
433
|
+
"""
|
434
|
+
return r"""
|
435
|
+
|
436
|
+
In order to use the Giac interface you need to have Giac installed
|
437
|
+
and have a program called "giac" in your PATH. You need a giac version
|
438
|
+
supporting "giac --sage" ( roughly after 0.9.1 of march 2011). Some giac
|
439
|
+
instructions and the help's langage depend of you LANG variable. To obtain
|
440
|
+
inline help for giac commands, you also need to have the program "cas_help"
|
441
|
+
in your PATH.
|
442
|
+
|
443
|
+
|
444
|
+
If giac is not already installed, you can download binaries or sources
|
445
|
+
or a spkg ( for the spkg follow the sources link) from the homepage:
|
446
|
+
|
447
|
+
Homepage http://www-fourier.ujf-grenoble.fr/~parisse/giac.html
|
448
|
+
|
449
|
+
|
450
|
+
Full html documentation for giac is available from your giac installation at:
|
451
|
+
|
452
|
+
``$PREFIX``/share/giac/doc/en/cascmd_en/index.html
|
453
|
+
|
454
|
+
If you got giac from the spkg then ``$PREFIX`` is ``$SAGE_LOCAL``
|
455
|
+
"""
|
456
|
+
|
457
|
+
def expect(self):
|
458
|
+
"""
|
459
|
+
Return the pexpect object for this Giac session.
|
460
|
+
|
461
|
+
EXAMPLES::
|
462
|
+
|
463
|
+
sage: m = Giac()
|
464
|
+
sage: m.expect() is None
|
465
|
+
True
|
466
|
+
sage: m._start()
|
467
|
+
sage: m.expect()
|
468
|
+
Giac with PID ... running .../giac --sage
|
469
|
+
sage: m.quit()
|
470
|
+
"""
|
471
|
+
return self._expect
|
472
|
+
|
473
|
+
def console(self):
|
474
|
+
"""
|
475
|
+
Spawn a new Giac command-line session.
|
476
|
+
|
477
|
+
EXAMPLES::
|
478
|
+
|
479
|
+
sage: giac_console() # not tested - giac
|
480
|
+
...
|
481
|
+
Homepage http://www-fourier.ujf-grenoble.fr/~parisse/giac.html
|
482
|
+
Released under the GPL license 3.0 or above
|
483
|
+
See http://www.gnu.org for license details
|
484
|
+
-------------------------------------------------
|
485
|
+
Press CTRL and D simultaneously to finish session
|
486
|
+
Type ?commandname for help
|
487
|
+
0>>
|
488
|
+
"""
|
489
|
+
giac_console()
|
490
|
+
|
491
|
+
def completions(self, s):
|
492
|
+
"""
|
493
|
+
Return all commands that complete the command starting with the
|
494
|
+
string s.
|
495
|
+
|
496
|
+
EXAMPLES::
|
497
|
+
|
498
|
+
sage: c = giac.completions('cas')
|
499
|
+
sage: 'cas_setup' in c
|
500
|
+
True
|
501
|
+
"""
|
502
|
+
if self._expect is None:
|
503
|
+
self._start()
|
504
|
+
E = self._expect
|
505
|
+
E.sendline('%s%s%s' % (s, chr(63), chr(13)))
|
506
|
+
t = E.timeout
|
507
|
+
E.timeout = 0.3 # since some things have no completion
|
508
|
+
try:
|
509
|
+
E.expect('----')
|
510
|
+
except pexpect.TIMEOUT:
|
511
|
+
E.timeout = t
|
512
|
+
return []
|
513
|
+
E.timeout = t
|
514
|
+
v = bytes_to_str(E.before)
|
515
|
+
E.expect(self._prompt)
|
516
|
+
E.expect(self._prompt)
|
517
|
+
return v.split()[1:]
|
518
|
+
|
519
|
+
def _commands(self):
|
520
|
+
"""
|
521
|
+
Return list of all commands defined in Giac.
|
522
|
+
|
523
|
+
EXAMPLES::
|
524
|
+
|
525
|
+
sage: c = giac._commands()
|
526
|
+
sage: len(c) > 100
|
527
|
+
True
|
528
|
+
sage: 'Psi' in c
|
529
|
+
True
|
530
|
+
"""
|
531
|
+
try:
|
532
|
+
v = sum([self.completions(chr(65 + n)) for n in range(26)], []) + \
|
533
|
+
sum([self.completions(chr(97 + n)) for n in range(26)], [])
|
534
|
+
except RuntimeError:
|
535
|
+
print("\n" * 3)
|
536
|
+
print("*" * 70)
|
537
|
+
print("WARNING: You do not have a working version of Giac installed!")
|
538
|
+
print("*" * 70)
|
539
|
+
v = []
|
540
|
+
v.sort()
|
541
|
+
return v
|
542
|
+
|
543
|
+
def _tab_completion(self, verbose=True, use_disk_cache=True):
|
544
|
+
"""
|
545
|
+
Return a list of all the commands defined in Giac and optionally
|
546
|
+
(per default) store them to disk.
|
547
|
+
|
548
|
+
EXAMPLES::
|
549
|
+
|
550
|
+
sage: c = giac._tab_completion(use_disk_cache=False, verbose=False)
|
551
|
+
sage: len(c) > 100
|
552
|
+
True
|
553
|
+
sage: 'factors' in c
|
554
|
+
True
|
555
|
+
"""
|
556
|
+
try:
|
557
|
+
return self.__tab_completion
|
558
|
+
except AttributeError:
|
559
|
+
import sage.misc.persist
|
560
|
+
if use_disk_cache:
|
561
|
+
try:
|
562
|
+
self.__tab_completion = sage.misc.persist.load(COMMANDS_CACHE)
|
563
|
+
return self.__tab_completion
|
564
|
+
except OSError:
|
565
|
+
pass
|
566
|
+
if verbose:
|
567
|
+
print("\nBuilding Giac command completion list (this takes")
|
568
|
+
print("a few seconds only the first time you do it).")
|
569
|
+
print("To force rebuild later, delete %s." % COMMANDS_CACHE)
|
570
|
+
v = self._commands()
|
571
|
+
self.__tab_completion = v
|
572
|
+
if len(v) > 200:
|
573
|
+
# Giac is actually installed.
|
574
|
+
sage.misc.persist.save(v, COMMANDS_CACHE)
|
575
|
+
return v
|
576
|
+
|
577
|
+
def cputime(self, t=None):
|
578
|
+
r"""
|
579
|
+
Return the amount of CPU time that the Giac session has used.
|
580
|
+
|
581
|
+
If ``t`` is not None, then it returns the difference
|
582
|
+
between the current CPU time and ``t``.
|
583
|
+
|
584
|
+
EXAMPLES::
|
585
|
+
|
586
|
+
sage: t = giac.cputime()
|
587
|
+
sage: t # random
|
588
|
+
0.02
|
589
|
+
sage: x = giac('x')
|
590
|
+
sage: giac.diff(x^2, x)
|
591
|
+
2*x
|
592
|
+
sage: giac.cputime(t) # random
|
593
|
+
0.0
|
594
|
+
"""
|
595
|
+
if t is None:
|
596
|
+
return float(self('time()'))
|
597
|
+
return float(self('time() - %s' % float(t)))
|
598
|
+
|
599
|
+
def _eval_line(self, line, allow_use_file=True, wait_for_prompt=True, restart_if_needed=False):
|
600
|
+
"""
|
601
|
+
EXAMPLES::
|
602
|
+
|
603
|
+
sage: giac._eval_line('2+2')
|
604
|
+
'4'
|
605
|
+
|
606
|
+
sage: A = matrix([range(280)])
|
607
|
+
sage: GA = giac(A)
|
608
|
+
|
609
|
+
TESTS::
|
610
|
+
|
611
|
+
sage: h1 = 'int(sin(x)^2, x)'
|
612
|
+
sage: h2 = 'int(cos(x)^2, x)'
|
613
|
+
sage: giac_result = giac(h1) + giac(h2)
|
614
|
+
sage: bool(giac_result.sage() == x) # needs sage.symbolic
|
615
|
+
True
|
616
|
+
"""
|
617
|
+
with gc_disabled():
|
618
|
+
z = Expect._eval_line(self, line, allow_use_file=allow_use_file,
|
619
|
+
wait_for_prompt=wait_for_prompt)
|
620
|
+
if z.lower().find("error") != -1:
|
621
|
+
raise RuntimeError("an error occurred running a Giac command:\nINPUT:\n%s\nOUTPUT:\n%s" % (line, z))
|
622
|
+
lines = (line for line in z.splitlines()
|
623
|
+
if not line.startswith('Evaluation time:'))
|
624
|
+
return "\n".join(lines)
|
625
|
+
|
626
|
+
def eval(self, code, strip=True, **kwds):
|
627
|
+
r"""
|
628
|
+
Send the code x to the Giac interpreter.
|
629
|
+
Remark: To enable multi-lines codes in the notebook magic mode: ``%giac``,
|
630
|
+
the ``\n`` are removed before sending the code to giac.
|
631
|
+
|
632
|
+
INPUT:
|
633
|
+
|
634
|
+
- ``code`` -- str
|
635
|
+
- ``strip`` -- default is ``True`` and removes ``\n``
|
636
|
+
|
637
|
+
EXAMPLES::
|
638
|
+
|
639
|
+
sage: giac.eval("2+2;\n3")
|
640
|
+
'4,3'
|
641
|
+
sage: giac.eval("2+2;\n3",False)
|
642
|
+
'4\n3'
|
643
|
+
sage: s='g(x):={\nx+1;\nx+2;\n}'
|
644
|
+
sage: giac(s)
|
645
|
+
...x+1...x+2...
|
646
|
+
sage: giac.g(5)
|
647
|
+
7
|
648
|
+
"""
|
649
|
+
# we remove \n to enable multiline code in the notebook magic
|
650
|
+
# mode %giac
|
651
|
+
if strip:
|
652
|
+
code = code.replace("\n", "").strip()
|
653
|
+
return Expect.eval(self, code, strip=strip, **kwds).strip()
|
654
|
+
|
655
|
+
def set(self, var, value):
|
656
|
+
"""
|
657
|
+
Set the variable var to the given value.
|
658
|
+
|
659
|
+
EXAMPLES::
|
660
|
+
|
661
|
+
sage: giac.set('xx', '2')
|
662
|
+
sage: giac.get('xx')
|
663
|
+
'2'
|
664
|
+
"""
|
665
|
+
cmd = '%s:=%s:;' % (var, value) # if giac is not in maple mode ( maple_mode(0))
|
666
|
+
out = self.eval(cmd)
|
667
|
+
if out.find("error") != -1:
|
668
|
+
raise TypeError("error executing code in Giac\nCODE:\n\t%s\nGiac ERROR:\n\t%s" % (cmd, out))
|
669
|
+
|
670
|
+
def get(self, var):
|
671
|
+
"""
|
672
|
+
Get the value of the variable var.
|
673
|
+
|
674
|
+
EXAMPLES::
|
675
|
+
|
676
|
+
sage: giac.set('xx', '2')
|
677
|
+
sage: giac.get('xx')
|
678
|
+
'2'
|
679
|
+
"""
|
680
|
+
return self.eval('%s' % var)
|
681
|
+
|
682
|
+
def _object_class(self):
|
683
|
+
"""
|
684
|
+
Return the class of GiacElements.
|
685
|
+
|
686
|
+
EXAMPLES::
|
687
|
+
|
688
|
+
sage: giac._object_class()
|
689
|
+
<class 'sage.interfaces.giac.GiacElement'>
|
690
|
+
|
691
|
+
::
|
692
|
+
|
693
|
+
sage: m = giac(2)
|
694
|
+
sage: type(m)
|
695
|
+
<class 'sage.interfaces.giac.GiacElement'>
|
696
|
+
"""
|
697
|
+
return GiacElement
|
698
|
+
|
699
|
+
def _function_element_class(self):
|
700
|
+
"""
|
701
|
+
Return the GiacFunctionElement class.
|
702
|
+
|
703
|
+
EXAMPLES::
|
704
|
+
|
705
|
+
sage: giac._function_element_class()
|
706
|
+
<class 'sage.interfaces.giac.GiacFunctionElement'>
|
707
|
+
|
708
|
+
::
|
709
|
+
|
710
|
+
sage: two = giac(2)
|
711
|
+
sage: type(two.gcd)
|
712
|
+
<class 'sage.interfaces.giac.GiacFunctionElement'>
|
713
|
+
"""
|
714
|
+
return GiacFunctionElement
|
715
|
+
|
716
|
+
def _equality_symbol(self):
|
717
|
+
"""
|
718
|
+
Return the symbol used for equality testing in Giac.
|
719
|
+
|
720
|
+
EXAMPLES::
|
721
|
+
|
722
|
+
sage: giac._equality_symbol()
|
723
|
+
'=='
|
724
|
+
|
725
|
+
sage: giac(2) == giac(2)
|
726
|
+
True
|
727
|
+
"""
|
728
|
+
return '=='
|
729
|
+
|
730
|
+
def _true_symbol(self):
|
731
|
+
"""
|
732
|
+
Return the symbol used for truth in Giac.
|
733
|
+
|
734
|
+
EXAMPLES::
|
735
|
+
|
736
|
+
sage: giac._true_symbol()
|
737
|
+
'true'
|
738
|
+
|
739
|
+
::
|
740
|
+
|
741
|
+
sage: giac(2) == giac(2)
|
742
|
+
True
|
743
|
+
"""
|
744
|
+
return 'true'
|
745
|
+
|
746
|
+
def _assign_symbol(self):
|
747
|
+
"""
|
748
|
+
Return the symbol used for assignment in Giac.
|
749
|
+
|
750
|
+
EXAMPLES::
|
751
|
+
|
752
|
+
sage: giac._assign_symbol()
|
753
|
+
':='
|
754
|
+
"""
|
755
|
+
return ":="
|
756
|
+
|
757
|
+
def _help(self, string):
|
758
|
+
r"""
|
759
|
+
Return the Giac help on ``string``.
|
760
|
+
|
761
|
+
EXAMPLES::
|
762
|
+
|
763
|
+
sage: giac._help('gcd') # not tested ; output may vary (LANG)
|
764
|
+
"...gcd - greatest common divisor of polynomials...
|
765
|
+
"""
|
766
|
+
return os.popen('cas_help %s' % string).read()
|
767
|
+
# return os.popen('echo "?%s" | giac' % string).read()
|
768
|
+
|
769
|
+
def help(self, string):
|
770
|
+
"""
|
771
|
+
Display Giac help about string.
|
772
|
+
|
773
|
+
This is the same as typing "?string" in the Giac console.
|
774
|
+
|
775
|
+
INPUT:
|
776
|
+
|
777
|
+
- ``string`` -- string to search for in the giac help system
|
778
|
+
|
779
|
+
EXAMPLES::
|
780
|
+
|
781
|
+
sage: giac.help('Psi') # not tested - depends of giac and $LANG
|
782
|
+
Psi(a,n)=nth-derivative of the function DiGamma (=ln@Gamma) at point a (Psi(a,0)=Psi(a))...
|
783
|
+
"""
|
784
|
+
pager()(self._help(string))
|
785
|
+
|
786
|
+
def clear(self, var):
|
787
|
+
"""
|
788
|
+
Clear the variable named var.
|
789
|
+
|
790
|
+
EXAMPLES::
|
791
|
+
|
792
|
+
sage: giac.set('xx', '2')
|
793
|
+
sage: giac.get('xx')
|
794
|
+
'2'
|
795
|
+
sage: giac.clear('xx')
|
796
|
+
sage: giac.get('xx')
|
797
|
+
'xx'
|
798
|
+
"""
|
799
|
+
self.eval('purge(%s)' % var)
|
800
|
+
|
801
|
+
def version(self):
|
802
|
+
"""
|
803
|
+
Wrapper for giac's version().
|
804
|
+
|
805
|
+
EXAMPLES::
|
806
|
+
|
807
|
+
sage: giac.version()
|
808
|
+
"giac...
|
809
|
+
"""
|
810
|
+
return giac('version()')
|
811
|
+
|
812
|
+
|
813
|
+
@instancedoc
|
814
|
+
class GiacFunction(ExpectFunction):
|
815
|
+
def _instancedoc_(self):
|
816
|
+
"""
|
817
|
+
Return the Giac help for this function. This gets called when
|
818
|
+
doing ``?`` on ``self``.
|
819
|
+
|
820
|
+
EXAMPLES::
|
821
|
+
|
822
|
+
sage: giac.gcd.__doc__ # random
|
823
|
+
"gcd - greatest common divisor of polynomials...
|
824
|
+
"""
|
825
|
+
M = self._parent
|
826
|
+
return M._help(self._name)
|
827
|
+
|
828
|
+
|
829
|
+
@instancedoc
|
830
|
+
class GiacFunctionElement(FunctionElement):
|
831
|
+
def _instancedoc_(self):
|
832
|
+
"""
|
833
|
+
Return the Giac help for this function. This gets called when
|
834
|
+
doing ``?`` on ``self``.
|
835
|
+
|
836
|
+
EXAMPLES::
|
837
|
+
|
838
|
+
sage: two = giac(2)
|
839
|
+
sage: two.gcd.__doc__ # random
|
840
|
+
"...gcd - greatest common divisor of polynomials...
|
841
|
+
"""
|
842
|
+
return self._obj.parent()._help(self._name)
|
843
|
+
|
844
|
+
|
845
|
+
@instancedoc
|
846
|
+
class GiacElement(ExpectElement):
|
847
|
+
def __float__(self):
|
848
|
+
"""
|
849
|
+
Return a floating point version of ``self``.
|
850
|
+
|
851
|
+
EXAMPLES::
|
852
|
+
|
853
|
+
sage: float(giac(1/2))
|
854
|
+
0.5
|
855
|
+
sage: type(_)
|
856
|
+
<class 'float'>
|
857
|
+
"""
|
858
|
+
return float(giac.eval('evalf(%s)' % self.name()))
|
859
|
+
|
860
|
+
def unapply(self, var):
|
861
|
+
"""
|
862
|
+
Create a Giac function in the given arguments from a Giac symbol.
|
863
|
+
|
864
|
+
EXAMPLES::
|
865
|
+
|
866
|
+
sage: f=giac('y^3+1+t')
|
867
|
+
sage: g=(f.unapply('y,t'))
|
868
|
+
sage: g
|
869
|
+
(y,t)->y^3+1+t
|
870
|
+
sage: g(1,2)
|
871
|
+
4
|
872
|
+
"""
|
873
|
+
return giac('unapply(%s,%s)' % (self, var))
|
874
|
+
|
875
|
+
def __hash__(self):
|
876
|
+
"""
|
877
|
+
Return an integer representing the hash of ``self``.
|
878
|
+
|
879
|
+
These examples are optional, and require Giac to be installed. You
|
880
|
+
do not need to install any Sage packages for this.
|
881
|
+
|
882
|
+
EXAMPLES::
|
883
|
+
|
884
|
+
sage: m = giac('x^2+y^2')
|
885
|
+
sage: hash(m) # random
|
886
|
+
4614285348919569149
|
887
|
+
"""
|
888
|
+
return hash(giac.eval('string(%s);' % self.name()))
|
889
|
+
|
890
|
+
def _richcmp_(self, other, op):
|
891
|
+
"""
|
892
|
+
Compare equality between ``self`` and ``other``, using giac.
|
893
|
+
|
894
|
+
These examples are optional, and require Giac to be installed. You
|
895
|
+
do not need to install any Sage packages for this.
|
896
|
+
|
897
|
+
EXAMPLES::
|
898
|
+
|
899
|
+
sage: a = giac(5)
|
900
|
+
sage: b = giac(5)
|
901
|
+
sage: a == b
|
902
|
+
True
|
903
|
+
sage: a == 5
|
904
|
+
True
|
905
|
+
|
906
|
+
sage: c = giac(3)
|
907
|
+
sage: a == c
|
908
|
+
False
|
909
|
+
sage: a < c
|
910
|
+
False
|
911
|
+
sage: a < 6
|
912
|
+
True
|
913
|
+
sage: c <= a
|
914
|
+
True
|
915
|
+
|
916
|
+
TESTS::
|
917
|
+
|
918
|
+
sage: # needs sage.symbolic
|
919
|
+
sage: x = var('x')
|
920
|
+
sage: t = giac((x+1)^2)
|
921
|
+
sage: u = giac(x^2+2*x+1)
|
922
|
+
sage: u == t
|
923
|
+
False
|
924
|
+
"""
|
925
|
+
P = self.parent()
|
926
|
+
if P.eval("evalb(%s %s %s)" % (self.name(), P._equality_symbol(),
|
927
|
+
other.name())) == P._true_symbol():
|
928
|
+
return rich_to_bool(op, 0)
|
929
|
+
# (to be tested with giac). Maple does not allow comparing objects
|
930
|
+
# of different types and it raises an error in this case.
|
931
|
+
# We catch the error, and return True for <
|
932
|
+
try:
|
933
|
+
if P.eval("evalb(%s %s %s)" % (self.name(), P._lessthan_symbol(),
|
934
|
+
other.name())) == P._true_symbol():
|
935
|
+
return rich_to_bool(op, -1)
|
936
|
+
except RuntimeError as e:
|
937
|
+
msg = str(e)
|
938
|
+
if 'is not valid' in msg and 'to < or <=' in msg:
|
939
|
+
if (hash(str(self)) < hash(str(other))):
|
940
|
+
return rich_to_bool(op, -1)
|
941
|
+
else:
|
942
|
+
return rich_to_bool(op, 1)
|
943
|
+
else:
|
944
|
+
raise RuntimeError(e)
|
945
|
+
if P.eval("evalb(%s %s %s)" % (self.name(), P._greaterthan_symbol(), other.name())) == P._true_symbol():
|
946
|
+
return rich_to_bool(op, 1)
|
947
|
+
|
948
|
+
return NotImplemented
|
949
|
+
|
950
|
+
def _tab_completion(self):
|
951
|
+
"""
|
952
|
+
EXAMPLES::
|
953
|
+
|
954
|
+
sage: a = giac(2)
|
955
|
+
sage: 'sin' in a._tab_completion()
|
956
|
+
True
|
957
|
+
"""
|
958
|
+
return self.parent()._tab_completion()
|
959
|
+
|
960
|
+
def __len__(self):
|
961
|
+
"""
|
962
|
+
EXAMPLES::
|
963
|
+
|
964
|
+
sage: len(giac([1,2,3]))
|
965
|
+
3
|
966
|
+
"""
|
967
|
+
return int(self.size())
|
968
|
+
|
969
|
+
def __iter__(self):
|
970
|
+
"""
|
971
|
+
EXAMPLES::
|
972
|
+
|
973
|
+
sage: l = giac([1,2,3])
|
974
|
+
sage: list(iter(l))
|
975
|
+
[1, 2, 3]
|
976
|
+
"""
|
977
|
+
for i in range(len(self)): # zero-indexed if giac is maple_mode(0)
|
978
|
+
yield self[i]
|
979
|
+
|
980
|
+
def __del__(self):
|
981
|
+
"""
|
982
|
+
Note that clearing object is pointless since it wastes time.
|
983
|
+
(Ex: otherwise doing a=0 after a = (giac('x+y+z')^40).normal() is very slow )
|
984
|
+
|
985
|
+
EXAMPLES::
|
986
|
+
|
987
|
+
sage: a = giac(2)
|
988
|
+
sage: a.__del__()
|
989
|
+
sage: a
|
990
|
+
2
|
991
|
+
sage: del a
|
992
|
+
sage: a
|
993
|
+
Traceback (most recent call last):
|
994
|
+
...
|
995
|
+
NameError: name 'a' is not defined
|
996
|
+
"""
|
997
|
+
return
|
998
|
+
|
999
|
+
def _latex_(self):
|
1000
|
+
r"""
|
1001
|
+
You can output Giac expressions in latex.
|
1002
|
+
|
1003
|
+
EXAMPLES::
|
1004
|
+
|
1005
|
+
sage: M = matrix(QQ, [[1, 2], [3, 4]])
|
1006
|
+
sage: latex(M)
|
1007
|
+
\left(\begin{array}{rr}
|
1008
|
+
1 & 2 \\
|
1009
|
+
3 & 4
|
1010
|
+
\end{array}\right)
|
1011
|
+
sage: gM = giac(M)
|
1012
|
+
sage: latex(gM)
|
1013
|
+
\left...\begin{array}{cc}...1...&...2...\\...3...&...4...\end{array}\right...
|
1014
|
+
sage: gf = giac('(x^4 - y)/(y^2-3*x)')
|
1015
|
+
sage: latex(gf) # output changed slightly from 1.5.0-63 to 1.5.0-87
|
1016
|
+
\frac{...x^{4}...-...y...}{...y^{2}-3...x...}
|
1017
|
+
"""
|
1018
|
+
s = self.parent().eval('latex(%s)' % self.name())
|
1019
|
+
if s.startswith('"'):
|
1020
|
+
s = s[1:]
|
1021
|
+
if s.endswith('"'):
|
1022
|
+
s = s[:-1]
|
1023
|
+
return s.strip()
|
1024
|
+
|
1025
|
+
def _matrix_(self, R):
|
1026
|
+
r"""
|
1027
|
+
Return matrix over the (Sage) ring R determined by self, where self
|
1028
|
+
should be a Giac matrix.
|
1029
|
+
|
1030
|
+
.. WARNING:: It is slow, do not convert big matrices.
|
1031
|
+
|
1032
|
+
EXAMPLES::
|
1033
|
+
|
1034
|
+
sage: R.<x,y>=QQ[]
|
1035
|
+
sage: M=giac('matrix(4,4,(k,l)->(x^k-y^l))'); M
|
1036
|
+
matrix[[0,1-y,1-y^2,1-y^3],[x-1,x-y,x-y^2,x-y^3],[x^2-1,x^2-y,x^2-y^2,x^2-y^3],[x^3-1,x^3-y,x^3-y^2,x^3-y^3]]
|
1037
|
+
sage: M.eigenvals() # random
|
1038
|
+
0,0,(x^3+x^2+x-y^3-y^2-y+sqrt(x^6+2*x^5+3*x^4-14*x^3*y^3+2*x^3*y^2+2*x^3*y+6*x^3+2*x^2*y^3-14*x^2*y^2+2*x^2*y+5*x^2+2*x*y^3+2*x*y^2-14*x*y+4*x+y^6+2*y^5+3*y^4+6*y^3+5*y^2+4*y-12))/2,(x^3+x^2+x-y^3-y^2-y-sqrt(x^6+2*x^5+3*x^4-14*x^3*y^3+2*x^3*y^2+2*x^3*y+6*x^3+2*x^2*y^3-14*x^2*y^2+2*x^2*y+5*x^2+2*x*y^3+2*x*y^2-14*x*y+4*x+y^6+2*y^5+3*y^4+6*y^3+5*y^2+4*y-12))/2
|
1039
|
+
sage: Z=matrix(R,M);Z
|
1040
|
+
[ 0 -y + 1 -y^2 + 1 -y^3 + 1]
|
1041
|
+
[ x - 1 x - y -y^2 + x -y^3 + x]
|
1042
|
+
[ x^2 - 1 x^2 - y x^2 - y^2 -y^3 + x^2]
|
1043
|
+
[ x^3 - 1 x^3 - y x^3 - y^2 x^3 - y^3]
|
1044
|
+
sage: parent(Z)
|
1045
|
+
Full MatrixSpace of 4 by 4 dense matrices over Multivariate Polynomial Ring in x, y over Rational Field
|
1046
|
+
"""
|
1047
|
+
v = self.dim()
|
1048
|
+
n = int(v[0])
|
1049
|
+
m = int(v[1])
|
1050
|
+
|
1051
|
+
from sage.matrix.matrix_space import MatrixSpace
|
1052
|
+
M = MatrixSpace(R, n, m)
|
1053
|
+
entries = [[R(self[r, c]) for c in range(m)] for r in range(n)]
|
1054
|
+
return M(entries)
|
1055
|
+
|
1056
|
+
def _sage_(self, locals=None):
|
1057
|
+
r"""
|
1058
|
+
Convert a giac expression back to a Sage expression, if possible.
|
1059
|
+
|
1060
|
+
.. NOTE::
|
1061
|
+
|
1062
|
+
This method works successfully when Giac returns a result
|
1063
|
+
or list of results that consist only of:
|
1064
|
+
|
1065
|
+
- numbers, i.e. integers, floats, complex numbers;
|
1066
|
+
- functions and named constants also present in Sage, where:
|
1067
|
+
- Sage knows how to translate the function or constant's name
|
1068
|
+
from Giac's naming scheme through the ``symbol_table``, or
|
1069
|
+
- you provide a translation dictionary ``locals``.
|
1070
|
+
|
1071
|
+
New conversions can be added using Pynac's ``register_symbol``.
|
1072
|
+
This is the recommended approach for library code.
|
1073
|
+
|
1074
|
+
.. WARNING:: List conversion is slow.
|
1075
|
+
|
1076
|
+
EXAMPLES::
|
1077
|
+
|
1078
|
+
sage: m = giac('x^2 + 5*y')
|
1079
|
+
sage: m.sage() # needs sage.symbolic
|
1080
|
+
x^2 + 5*y
|
1081
|
+
|
1082
|
+
::
|
1083
|
+
|
1084
|
+
sage: m = giac('sin(2*sqrt(1-x^2)) * (1 - cos(1/x))^2')
|
1085
|
+
sage: m.trigexpand().sage() # needs sage.symbolic
|
1086
|
+
2*cos(sqrt(-x^2 + 1))*cos(1/x)^2*sin(sqrt(-x^2 + 1)) - 4*cos(sqrt(-x^2 + 1))*cos(1/x)*sin(sqrt(-x^2 + 1)) + 2*cos(sqrt(-x^2 + 1))*sin(sqrt(-x^2 + 1))
|
1087
|
+
|
1088
|
+
Converting a custom name using the ``locals`` dictionary::
|
1089
|
+
|
1090
|
+
sage: ex = giac('myFun(x)')
|
1091
|
+
sage: ex._sage_({('myFun', 1): sin}) # needs sage.symbolic
|
1092
|
+
sin(x)
|
1093
|
+
|
1094
|
+
Same but by adding a new entry to the ``symbol_table``::
|
1095
|
+
|
1096
|
+
sage: ex = giac('myFun(x)')
|
1097
|
+
sage: sage.symbolic.expression.register_symbol(sin, {'giac':'myFun'}) # needs sage.symbolic
|
1098
|
+
sage: ex._sage_() # needs sage.symbolic
|
1099
|
+
sin(x)
|
1100
|
+
|
1101
|
+
Conversion of lists::
|
1102
|
+
|
1103
|
+
sage: L = giac('solve((2/3)^x-2, x)'); L
|
1104
|
+
list[ln(2)/(ln(2)-ln(3))]
|
1105
|
+
sage: L.sage() # needs sage.symbolic
|
1106
|
+
[-log(2)/(log(3) - log(2))]
|
1107
|
+
|
1108
|
+
TESTS:
|
1109
|
+
|
1110
|
+
Check conversion of Booleans (:issue:`28705`)::
|
1111
|
+
|
1112
|
+
sage: giac('true')._sage_(), giac('false')._sage_() # needs sage.symbolic
|
1113
|
+
(True, False)
|
1114
|
+
|
1115
|
+
Check that variables and constants are not mixed up (:issue:`30133`)::
|
1116
|
+
|
1117
|
+
sage: ee, ii, pp = SR.var('e,i,pi') # needs sage.symbolic
|
1118
|
+
sage: giac(ee * ii * pp).sage().variables() # needs sage.symbolic
|
1119
|
+
(e, i, pi)
|
1120
|
+
sage: giac(e * i * pi).sage().variables() # needs sage.symbolic
|
1121
|
+
()
|
1122
|
+
"""
|
1123
|
+
from sage.symbolic.expression import symbol_table
|
1124
|
+
from sage.calculus.calculus import symbolic_expression_from_string, SR_parser_giac
|
1125
|
+
|
1126
|
+
if locals is None:
|
1127
|
+
locals = {}
|
1128
|
+
|
1129
|
+
result = repr(self) # string representation
|
1130
|
+
|
1131
|
+
if str(self.type()) not in ['DOM_LIST', 'vector', 'vecteur']:
|
1132
|
+
|
1133
|
+
# Merge the user-specified locals dictionary and the symbol_table
|
1134
|
+
# (locals takes priority)
|
1135
|
+
lsymbols = symbol_table['giac'].copy()
|
1136
|
+
lsymbols.update(locals)
|
1137
|
+
|
1138
|
+
try:
|
1139
|
+
return symbolic_expression_from_string(result, lsymbols,
|
1140
|
+
accept_sequence=True,
|
1141
|
+
parser=SR_parser_giac)
|
1142
|
+
except Exception:
|
1143
|
+
raise NotImplementedError("unable to parse Giac output: %s" % result)
|
1144
|
+
else:
|
1145
|
+
return [entry.sage() for entry in self]
|
1146
|
+
|
1147
|
+
def integral(self, var='x', min=None, max=None):
|
1148
|
+
r"""
|
1149
|
+
Return the integral of ``self`` with respect to the variable `x`.
|
1150
|
+
|
1151
|
+
INPUT:
|
1152
|
+
|
1153
|
+
- ``var`` -- variable
|
1154
|
+
|
1155
|
+
- ``min`` -- (default: ``None``)
|
1156
|
+
|
1157
|
+
- ``max`` -- (default: ``None``)
|
1158
|
+
|
1159
|
+
This returns the definite integral if xmin is not ``None``, otherwise
|
1160
|
+
an indefinite integral.
|
1161
|
+
|
1162
|
+
EXAMPLES::
|
1163
|
+
|
1164
|
+
sage: y=giac('y');f=(sin(2*y)/y).integral(y).simplify(); f
|
1165
|
+
Si(2*y)
|
1166
|
+
sage: f.diff(y).simplify()
|
1167
|
+
sin(2*y)/y
|
1168
|
+
|
1169
|
+
::
|
1170
|
+
|
1171
|
+
sage: f = giac('exp(x^2)').integral('x',0,1) ; f
|
1172
|
+
1.46265174...
|
1173
|
+
sage: x,y=giac('x'),giac('y');integrate(cos(x+y),'x=0..pi').simplify()
|
1174
|
+
-2*sin(y)
|
1175
|
+
"""
|
1176
|
+
if min is None:
|
1177
|
+
return giac('int(%s,%s)' % (self.name(), var))
|
1178
|
+
if max is None:
|
1179
|
+
raise ValueError("neither or both of min/max must be specified")
|
1180
|
+
return giac('int(%s,%s,%s,%s)' % (self.name(), var,
|
1181
|
+
giac(min), giac(max)))
|
1182
|
+
|
1183
|
+
integrate = integral
|
1184
|
+
|
1185
|
+
def sum(self, var, min=None, max=None):
|
1186
|
+
r"""
|
1187
|
+
Return the sum of ``self`` with respect to the variable `x`.
|
1188
|
+
|
1189
|
+
INPUT:
|
1190
|
+
|
1191
|
+
- ``var`` -- variable
|
1192
|
+
|
1193
|
+
- ``min`` -- (default: ``None``)
|
1194
|
+
|
1195
|
+
- ``max`` -- (default: ``None``)
|
1196
|
+
|
1197
|
+
This returns the definite integral if xmin is not ``None``, otherwise
|
1198
|
+
an indefinite integral.
|
1199
|
+
|
1200
|
+
EXAMPLES::
|
1201
|
+
|
1202
|
+
sage: giac('1/(1+k^2)').sum('k',-oo,+infinity).simplify()
|
1203
|
+
(pi*exp(pi)^2+pi)/(exp(pi)^2-1)
|
1204
|
+
"""
|
1205
|
+
if min is None:
|
1206
|
+
return giac('sum(%s,%s)' % (self.name(), var))
|
1207
|
+
if max is None:
|
1208
|
+
raise ValueError("neither or both of min/max must be specified")
|
1209
|
+
return giac('sum(%s,%s,%s,%s)' % (self.name(), var,
|
1210
|
+
giac(min), giac(max)))
|
1211
|
+
|
1212
|
+
|
1213
|
+
# An instance
|
1214
|
+
giac = Giac()
|
1215
|
+
|
1216
|
+
|
1217
|
+
def reduce_load_Giac():
|
1218
|
+
"""
|
1219
|
+
Return the giac object created in sage.interfaces.giac.
|
1220
|
+
|
1221
|
+
EXAMPLES::
|
1222
|
+
|
1223
|
+
sage: from sage.interfaces.giac import reduce_load_Giac
|
1224
|
+
sage: reduce_load_Giac()
|
1225
|
+
Giac
|
1226
|
+
"""
|
1227
|
+
return giac
|
1228
|
+
|
1229
|
+
|
1230
|
+
def giac_console():
|
1231
|
+
"""
|
1232
|
+
Spawn a new Giac command-line session.
|
1233
|
+
|
1234
|
+
EXAMPLES::
|
1235
|
+
|
1236
|
+
sage: giac.console() # not tested - giac
|
1237
|
+
...
|
1238
|
+
Homepage http://www-fourier.ujf-grenoble.fr/~parisse/giac.html
|
1239
|
+
Released under the GPL license 3.0 or above
|
1240
|
+
See http://www.gnu.org for license details
|
1241
|
+
-------------------------------------------------
|
1242
|
+
Press CTRL and D simultaneously to finish session
|
1243
|
+
Type ?commandname for help
|
1244
|
+
"""
|
1245
|
+
from sage.repl.rich_output.display_manager import get_display_manager
|
1246
|
+
if not get_display_manager().is_in_terminal():
|
1247
|
+
raise RuntimeError('Can use the console only in the terminal. Try %%giac magics instead.')
|
1248
|
+
os.system('giac')
|
1249
|
+
|
1250
|
+
|
1251
|
+
def __doctest_cleanup():
|
1252
|
+
"""
|
1253
|
+
EXAMPLES::
|
1254
|
+
|
1255
|
+
sage: from sage.interfaces.giac import __doctest_cleanup
|
1256
|
+
sage: m = giac(2)
|
1257
|
+
sage: giac.is_running()
|
1258
|
+
True
|
1259
|
+
sage: __doctest_cleanup()
|
1260
|
+
sage: giac.is_running()
|
1261
|
+
False
|
1262
|
+
"""
|
1263
|
+
import sage.interfaces.quit
|
1264
|
+
sage.interfaces.quit.expect_quitall()
|