# HG changeset patch # User Frederic Han # Date 1319722875 -7200 # Node ID 09d5827cbbab6b4f91374a3788ade23e294bf966 # Parent da3684078800b499914ba5616cef046e752166b1 trac 10940: sagegiac addons 2 diff -r da3684078800 -r 09d5827cbbab sage/interfaces/all.py --- a/sage/interfaces/all.py Mon Jun 06 00:21:47 2011 +0200 +++ b/sage/interfaces/all.py Thu Oct 27 15:41:15 2011 +0200 @@ -40,7 +40,7 @@ from r import r, r_console, R, r_version, is_RElement from read_data import read_data -interfaces = ['gap', 'gap3', 'gp', 'mathematica', 'gnuplot', \ +interfaces = ['gap', 'gap3', 'giac', 'gp', 'mathematica', 'gnuplot', \ 'kash', 'magma', 'macaulay2', 'maple', 'maxima', \ 'mathematica', 'mwrank', 'octave', 'r', \ 'singular', 'sage0', 'sage'] diff -r da3684078800 -r 09d5827cbbab sage/interfaces/giac.py --- a/sage/interfaces/giac.py Mon Jun 06 00:21:47 2011 +0200 +++ b/sage/interfaces/giac.py Thu Oct 27 15:41:15 2011 +0200 @@ -32,15 +32,16 @@ '5*401' sage: giac.ifactor(2005) # optional - giac 2005 - sage: giac.ifactors(2005) # optional - giac - [5,1,401,1] + sage: l=giac.ifactors(2005) ; l; l[2] # optional - giac + [5,1,401,1] + 401 sage: giac.fsolve('x^2=cos(x)+4', 'x','0..5') # optional - giac 1.9140206190... sage: giac.factor('x^5 - y^5') # optional - giac (x-y)*(x^4+x^3*y+x^2*y^2+x*y^3+y^4) sage: R.=QQ[];f=(x+y)^5;f2=giac(f);(f-f2).normal() #optional - giac 0 - sage: x,y=giac('x'),giac('y'); giac.int(y/(cos(2*x)+cos(x)),x) #optional - giac + sage: x,y=giac('x,y'); giac.int(y/(cos(2*x)+cos(x)),x) #random; optional - giac 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)))) @@ -147,8 +148,8 @@ sage: f19 = alpha^19 - beta^19/sqrt(5) # optional - giac sage: f19 # optional - giac (sqrt(5)/2+1/2)^19-((-sqrt(5)+1)/2)^19/sqrt(5) - sage: f19.normal() # optional - giac - (5778*sqrt(5)+33825)/5 + sage: (f19-(5778*sqrt(5)+33825)/5).normal() # optional - giac + 0 Let's say we want to write a giac program now that squares a number if it is positive and cubes it if it is negative. In giac, @@ -235,10 +236,11 @@ :: - sage: x,y,z=tuple(giac('x,y,z'));type(y) # optional - giac + sage: x,y,z=giac('x,y,z');type(y) # optional - giac - sage: (1/(cos(2*y)+cos(y))).integral(y,0,pi/4).simplify() #optional - giac - (-2*ln((sqrt(3)-3*tan(1/8*pi))/(sqrt(3)+3*tan(1/8*pi)))*sqrt(3)-3*tan(1/8*pi))/9 + sage: I1=(1/(cos(2*y)+cos(y))).integral(y,0,pi/4).simplify() #optional - giac + 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() # optional - giac + 0 sage: ((y+z*sqrt(5))*(y-sqrt(5)*z)).normal() # optional - giac y^2-5*z^2 @@ -568,6 +570,37 @@ return z + def eval(self, code, strip=True, **kwds): + """ + Send the code x to the Giac interpreter. + Remark: To enable multi-lines codes in the notebook magic mode: %giac, + the \\n are removed before sending the code to giac. + + INPUT: + code -- str + strip -- Default is True and removes \n + + EXAMPLES: + sage: giac.eval("2+2;\n3") #optional - giac + '4,3' + sage: giac.eval("2+2;\n3",False) # optional - giac + '4\n3' + sage: s='g(x):={\nx+1;\nx+2;\n}' + sage: giac(s) # optional - giac + (x)->{ + x+1; + x+2; + } + sage: giac.g(5) # optional - giac + 7 + """ + #we remove \n to enable multiline code in the notebook magic mode %giac + if strip: + code = code.replace("\n","").strip() + ans = Expect.eval(self, code, strip=strip, **kwds).strip() + return ans + + def set(self, var, value): """ @@ -801,8 +834,8 @@ EXAMPLES:: sage: m = giac('x^2+y^2') # optional - giac - sage: hash(m) # optional - giac - -895227139 + sage: hash(m) # random; optional - giac + 4614285348919569149 """ return hash(giac.eval('string(%s);'%self.name())) @@ -959,15 +992,16 @@ def _matrix_(self, R): r""" Return matrix over the (Sage) ring R determined by self, where self - should be a Giac matrix. + should be a Giac matrix. + Warning: It is slow, don't convert big matrices. EXAMPLES:: sage: R.=QQ[] sage: M=giac('matrix(4,4,(k,l)->(x^k-y^l))'); M # optional - giac 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]] - sage: M.eigenvals() # optional - giac - 0,0,(x^3+x^2+x-y^3-y^2-y+sqrt(-14*x^3*y^3+2*x^3*y^2+2*x^3*y+2*x^2*y^3-14*x^2*y^2+2*x^2*y+2*x*y^3+2*x*y^2-14*x*y+y^6+2*y^5+3*y^4+6*y^3+5*y^2+x^6+2*x^5+3*x^4+6*x^3+5*x^2+4*y+4*x-12))/2,(x^3+x^2+x-y^3-y^2-y-sqrt(-14*x^3*y^3+2*x^3*y^2+2*x^3*y+2*x^2*y^3-14*x^2*y^2+2*x^2*y+2*x*y^3+2*x*y^2-14*x*y+y^6+2*y^5+3*y^4+6*y^3+5*y^2+x^6+2*x^5+3*x^4+6*x^3+5*x^2+4*y+4*x-12))/2 + sage: M.eigenvals() # random; optional - giac + 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 sage: Z=matrix(M,R);Z # optional - giac [ 0 -y + 1 -y^2 + 1 -y^3 + 1] [ x - 1 x - y -y^2 + x -y^3 + x] @@ -992,7 +1026,8 @@ Convert a giac expression back to a Sage expression. This currently does not implement a parser for the Giac output language, - therefore only very simple expressions will convert successfully. + therefore only very simple expressions will convert successfully. + Warning: List conversion is slow. EXAMPLE:: @@ -1008,13 +1043,15 @@ """ result = repr(self) - - try: - from sage.symbolic.all import SR - return SR(result) - except: - raise NotImplementedError, "Unable to parse Giac output: %s" % result - + if str(self.type()) != 'DOM_LIST' : + try: + from sage.symbolic.all import SR + return SR(result) + except: + raise NotImplementedError, "Unable to parse Giac output: %s" % result + else: + return [entry.sage() for entry in self] + def integral(self, var='x', min=None, max=None): r""" Return the integral of self with respect to the variable x.