Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168749
Image: ubuntu2004
a=5 a
5
2==2
True
2>3
False
2**3
8
2^3
8
5/2
5/2
5//2
2
18%5
3
012
10
09
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_11.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("MDk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmpIc0DWu/___code___.py", line 2 _sage_const_09 = Integer(09) ^ SyntaxError: invalid token
sin?

File: /usr/local/sage/local/lib/python2.6/site-packages/sage/functions/trig.py

Type: <class ‘sage.functions.trig.Function_sin’>

Definition: sin(*args, coerce=True, hold=False, dont_call_method_on_arg=False)

Docstring:

The sine function.

EXAMPLES:

sage: sin(0)
0
sage: sin(x).subs(x==0)
0
sage: sin(2).n(100)
0.90929742682568169539601986591
sage: loads(dumps(sin))
sin
sin(0)
0
sin(x).subs(x==0)
0
sin(2).n(100)
0.90929742682568169539601986591
loads(dumps(sin))
sin
def is_even(n): return n%2==0
is_even(2)
True
is_even(123)
False
def is_divisible_by(number,divisor=2): return number%divisor==0
is_divisible_by(123,5)
False
is_divisible_by(123)
False
def myfunction(x): if x<0: return -1 elif x==0: return 0 else:return 1
myfunction(-2)
-1
myfunction(0)
0
myfunction(3)
1
money=10000 years=0 while money<20000: years=years+1 money=money*1.069
years
11
money
20833.1411965483
n=10;ans=1; for each in range(1,n+1): ans=ans*each
ans
3628800
for eachitem in ['name:vincent','age=30','job:teacher']: print eachitem
name:vincent age=30 job:teacher
x=fibonacci(10) len(x)
x Fibonacci numbers list(x) [1,1,2,3,5,8,13,21,34,55] x.show(11)
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_97.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("eApGaWJvbmFjY2kgbnVtYmVycwpsaXN0KHgpClsxLDEsMiwzLDUsOCwxMywyMSwzNCw1NV0KeC5zaG93KDExKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmpy2ANam/___code___.py", line 4 Fibonacci numbers ^ SyntaxError: invalid syntax
fibonacci?

File: /usr/local/sage/local/lib/python2.6/site-packages/sage/combinat/combinat.py

Type: <type ‘function’>

Definition: fibonacci(n, algorithm=’pari’)

Docstring:

Returns the n-th Fibonacci number. The Fibonacci sequence F_n is defined by the initial conditions F_1=F_2=1 and the recurrence relation F_{n+2} = F_{n+1} + F_n. For negative n we define F_n = (-1)^{n+1}F_{-n}, which is consistent with the recurrence relation.

INPUT:

  • algorithm - string:
  • "pari" - (default) - use the PARI C library’s fibo function.
  • "gap" - use GAP’s Fibonacci function

Note

PARI is tens to hundreds of times faster than GAP here; moreover, PARI works for every large input whereas GAP doesn’t.

EXAMPLES:

sage: fibonacci(10)
55
sage: fibonacci(10, algorithm='gap')
55
sage: fibonacci(-100)
-354224848179261915075
sage: fibonacci(100)
354224848179261915075
sage: fibonacci(0)
0
sage: fibonacci(1/2)
...
TypeError: no conversion of this rational to integer
2^3+123456789*81
9999999917
exp?

File: /usr/local/sage/local/lib/python2.6/site-packages/sage/functions/log.py

Type: <class ‘sage.functions.log.Function_exp’>

Definition: exp(x, coerce=True, hold=False, prec=None, dont_call_method_on_arg=False)

Docstring:

The exponential function, \exp(x) = e^x.

EXAMPLES:

sage: exp(-1)
e^(-1)
sage: exp(2)
e^2
sage: exp(2).n(100)
7.3890560989306502272304274606
sage: exp(x^2 + log(x))
e^(x^2 + log(x))
sage: exp(x^2 + log(x)).simplify()
x*e^(x^2)
sage: exp(2.5)
12.1824939607035
sage: exp(float(2.5))
12.182493960703473
sage: exp(RDF('2.5'))
12.1824939607
sage: exp(pi*I/2)
I
sage: exp(pi*I)
-1
sage: exp(8*pi*I)
1
sage: exp(7*pi*I/2)
-I

TEST:

sage: latex(exp(x))
e^{x}
sage: latex(exp(sqrt(x)))
e^{\sqrt{x}}
sage: latex(exp)
\exp
sage: latex(exp(sqrt(x))^x)
\left(e^{\sqrt{x}}\right)^{x}
sage: latex(exp(sqrt(x)^x))
e^{\left(\sqrt{x}^{x}\right)}

Test simplifications when taking powers of exp, #7264:

sage: var('a,b,c,I')
(a, b, c, I)
sage: model_exp = exp(I)**a*(b)
sage: sol1_l={b: 5.0, a: 1.1}
sage: model_exp.subs(sol1_l)
5.00000000000000*(e^I)^1.10000000000000
sage: exp(3)^I*exp(x)
(e^3)^I*e^x
sage: exp(x)*exp(x)
e^(2*x)
sage: exp(x)*exp(a)
e^(a + x)
sage: exp(x)*exp(a)^2
e^(2*a + x)

Another instance of the same problem, #7394:

sage: 2*sqrt(e)
2*sqrt(e)
exp(1/0.1^2)
2.68811714181610e43
def is_odd(n): return n%2==1
is_odd(1)
True
is_odd(2)
False
def myfunction(x,y): if x^2+y^2<=1: return 1 elif 1<x^2+y^2<=2: return 1/2 else: return 0
myfunction(2,2)
0
myfunction(1,1)
1/2
myfunction(0.2,0.3)
1
n=10 m=0 x=range(n) while x<=100: p=random() if p<0.2: x[m]=1 else: x[m]=0 m=m+1
Traceback (most recent call last): x[m]=1 File "", line 1, in <module> File "/tmp/tmpCaXFi7/___code___.py", line 6, in <module> exec compile(u'while x<=_sage_const_100 :\n p=random()\n if p<_sage_const_0p2 :\n x[m]=_sage_const_1 \n else:\n x[m]=_sage_const_0 \n m=m+_sage_const_1 ' + '\n', '', 'single') File "", line 6, in <module> IndexError: list assignment index out of range
epsilon=0.01 x_pre=infinity x=0.0 while abs(x-x_pre)<epsilon: e_pre=x x=(1-2*x)^(1/3)
x=0;x+=1;x
1
x=2;x**=2;x
4
x=[123,'xyz'];x+=[011];x
[123, 'xyz', 9]
x=y=z=123
x
123
y
123
z
123
x,y,z=1,2,3
x
1
y
2
z
3
x=666;y=888
x,y=y,x
x
888
y
666
x=1 x+=100
x
101
load myadd.sage x
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_7.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("bG9hZCBteWFkZC5zYWdlCng="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmplYmkAf/___code___.py", line 2, in <module> sage.misc.preparser.load(sage.misc.preparser.base64.b64decode("bXlhZGQuc2FnZQ=="),globals(),False) File "/usr/local/sage2/local/lib/python2.6/site-packages/sage/misc/preparser.py", line 1503, in load exec(preparse_file(open(filename).read()), globals) IOError: [Errno 2] No such file or directory: 'myadd.sage'
attach myadd.sage x
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_165.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("YXR0YWNoIG15YWRkLnNhZ2UKeA=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmp5WtWxM/___code___.py", line 2, in <module> sage.misc.preparser.load(sage.misc.preparser.base64.b64decode("bXlhZGQuc2FnZQ=="),globals(),True) File "/usr/local/sage/local/lib/python2.6/site-packages/sage/misc/preparser.py", line 1503, in load exec(preparse_file(open(filename).read()), globals) IOError: [Errno 2] No such file or directory: 'myadd.sage'
n=10 x=[int(random()*100 for each in range(n)] print "random numbers is",x for each in range(n): for k in range(n-1): if x[k]<x[k+1]: x[k],x[k+1]=x[k+1],x[k] print "Sorted is",x
Traceback (most recent call last): x[k],x[k+1]=x[k+1],x[k] File "", line 1, in <module> File "/tmp/tmpzYGE3B/___code___.py", line 4 x=[int(random()*_sage_const_100 for each in range(n)] ^ SyntaxError: invalid syntax
cdef extern from "test.c": int myfactorial(int n) def test(n): return myfactorial(n)
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_174.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("Y2RlZiBleHRlcm4gZnJvbSAidGVzdC5jIjoKICAgIGludCBteWZhY3RvcmlhbChpbnQgbikKZGVmIHRlc3Qobik6CiAgICByZXR1cm4gbXlmYWN0b3JpYWwobik="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmpGh86Xl/___code___.py", line 2 cdef extern from "test.c": ^ SyntaxError: invalid syntax
sttach test.apyx Compiling test.spyx... test(8)
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_176.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("c3R0YWNoIHRlc3QuYXB5eApDb21waWxpbmcgdGVzdC5zcHl4Li4uCnRlc3QoOCk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmpbkV1ro/___code___.py", line 3 sttach test.apyx ^ SyntaxError: invalid syntax
n=var('n') a_n=(1+1/n)**n a_n.limit(n=oo)
e
limit(sin(n*pi/3),n=oo)
ind
n=var('n') limit((n**2-1)/(n+5),n=oo)
+Infinity
x=var('x') diff(sin(x),x)
cos(x)
diff(sin(x),x,2)
-sin(x)
x,y=var('x,y') f=x^2*y+x*exp(y) f.diff(x,y)
2*x + e^y
var('x,a') f=exp(sin(a-a^2))/x f.derivative(x)
-e^(sin(-a^2 + a))/x^2
x=var('x') f=sin(x) taylor(f,x,0,6)
1/120*x^5 - 1/6*x^3 + x
x,y=var('x,y') f=sin(x*y) taylor(f,(x,0),(y,-1),4)
-1/2*(y + 1)*x^3 + 1/6*x^3 + (y + 1)*x - x
x=var('x') integral(x*sin(x**2),x)
-1/2*cos(x^2)
integral(x^2,x,0,1)
1/3
x=var('x') integral(x**(-2),1,infinity)
1
var('x,k,w')
(x, k, w)
f=x^3*exp(k*x)*sin(w*x) f.integrate(x)
-(((k^6*w + 3*k^4*w^3 + 3*k^2*w^5 + w^7)*x^3 - 24*k^3*w + 24*k*w^3 - 6*(k^5*w + 2*k^3*w^3 + k*w^5)*x^2 + 6*(3*k^4*w + 2*k^2*w^3 - w^5)*x)*e^(k*x)*cos(w*x) - ((k^7 + 3*k^5*w^2 + 3*k^3*w^4 + k*w^6)*x^3 - 6*k^4 + 36*k^2*w^2 - 6*w^4 - 3*(k^6 + k^4*w^2 - k^2*w^4 - w^6)*x^2 + 6*(k^5 - 2*k^3*w^2 - 3*k*w^4)*x)*e^(k*x)*sin(w*x))/(k^8 + 4*k^6*w^2 + 6*k^4*w^4 + 4*k^2*w^6 + w^8)
m=var('m') sum(1/(m*(m+1)),m,1,oo)
1
sum(1/(m*(m+1)),m,1,10)
10/11
sum(1/(m*(m+1)),m,1,100)
100/101
t=var('t') x=function('x',t) a,b=var('a,b') DE=a*diff(x,t)+b*x-1 desolve(DE,[x,t])
(c + e^(b*t/a)/b)*e^(-b*t/a)
plot(sin,(0,2*pi),color='red')
p=plot(sin(x),color=hue(1.0)) for a in range(2,8+1): p+=plot(sin(a*x),color=hue(1.0/a)) p.show(xmin=-1,xmax=1,ymin=-1,ymax=1)
var('t') parametric_plot((1-2*sin(t),t^2),(t,-4,4))
var('x,y') implicit_plot(x^2/4-y^2/9-1,(x,-5,5),(y,-6,6)).show(aspect_ratio=1)
var('a') polar_plot(a/5,(a,0,6*pi),color='green').show(aspect_ratio=1)
var('x,y') plot3d(x^2/16-y^2/9,(x,-10,10),(y,-10,10))
p=plot(x/(1+x),color=hue(1.0)) for m in range(2,8+1): p+=plot(m*x/(1+m^2*x^2),color=hue(1.0/m)) p.show(xmin=-1,xmax=1,ymin=-1,ymax=1)
var('t') parametric_plot((sin(3*t)*cos(t),sin(3*t)*sin(t)),(t,0,pi))
var('x,y') implicit_plot(x^2/16+y^2/4-1,(x,-8,8),(y,-4,4)).show(aspect_ratio=1)
var('a') polar_plot(a^2*sin(a^2),(a,0,6*pi),color='red').show(aspect_ratio=1)
m1=matrix(ZZ,[[1,2,3,],[4,5,6]]) m1.parent() m1
[1 2 3] [4 5 6]
m1/2
[1/2 1 3/2] [ 2 5/2 3]
m2=matrix(QQ,[[1,2,3,],[4,5,6]]) m2.parent() m2
[1 2 3] [4 5 6]
m2/2
[1/2 1 3/2] [ 2 5/2 3]
m3=matrix(RR,[[1,2,3,],[4,5,6]]) m3.parent() m3
[1.00000000000000 2.00000000000000 3.00000000000000] [4.00000000000000 5.00000000000000 6.00000000000000]
m3/2
[0.500000000000000 1.00000000000000 1.50000000000000] [ 2.00000000000000 2.50000000000000 3.00000000000000]
a=matrix(ZZ,[[3,1,1],[2,1,2]]) b=matrix(ZZ,[[1,-2,3],[4,5,-6]]) a+b
[ 4 -1 4] [ 6 6 -4]
a-b
[ 2 3 -2] [-2 -4 8]
a=matrix(ZZ,[[3,1,1],[2,1,2]]) a.transpose()
[3 2] [1 1] [1 2]
transpose(a.transpose())
[3 1 1] [2 1 2]
b=matrix(ZZ,[[1,-2,3],[4,5,-6]]) a*b.transpose()
[ 4 11] [ 6 1]
transpose(b)*a
[11 5 9] [ 4 3 8] [-3 -3 -9]
A=matrix(QQ,3,range(9));A
[0 1 2] [3 4 5] [6 7 8]
b=matrix(QQ,3,1,[1,2,3]);b
[1] [2] [3]
x=A\b;x
[-2/3] [ 1] [ 0]
A*x
[1] [2] [3]
Q=matrix\ ([[2,1,0,0,0,],[0,2,1,0,0],[0,0,2,1,0],[0,0,0,2,1],[0,0,0,0,2]]) Q^3
[ 8 12 6 1 0] [ 0 8 12 6 1] [ 0 0 8 12 6] [ 0 0 0 8 12] [ 0 0 0 0 8]
rank(Q)
5
Q.inverse()
[ 1/2 -1/4 1/8 -1/16 1/32] [ 0 1/2 -1/4 1/8 -1/16] [ 0 0 1/2 -1/4 1/8] [ 0 0 0 1/2 -1/4] [ 0 0 0 0 1/2]
Q^(-1)*Q
[1 0 0 0 0] [0 1 0 0 0] [0 0 1 0 0] [0 0 0 1 0] [0 0 0 0 1]
A=matrix(RR,[[1,2,5],[1,3,-2],[3,7,8],[1,4,-9]]) b=matrix(RR,[[1],[1],[3],[1]]) rank(A)
2
AA=matrix(RR,4,4) AA.set_block(0,0,A) AA.set_block(0,3,b) rank(AA)
2
A.rref()
[ 1.00000000000000 0.000000000000000 19.0000000000000] [0.000000000000000 1.00000000000000 -7.00000000000000] [0.000000000000000 0.000000000000000 0.000000000000000] [0.000000000000000 0.000000000000000 0.000000000000000]
s=A.right_kernel() ss=s.basis_matrix();ss
[ 1.00000000000000 -0.368421052631579 -0.0526315789473684]
x=A.solve_right(b);x
[ 1.00000000000000] [0.000000000000000] [0.000000000000000]
A=matrix(QQ,3,range(9));A[0,0]=2;A
[2 1 2] [3 4 5] [6 7 8]
b=matrix(QQ,1,3,[1,2,3]);b
[1 2 3]
x=b*A^(-1);x
[ 0 5/3 -2/3]
D=matrix(RR,4,[random() for each in range(16)]);D
[ 0.940118813085686 0.965476202692564 0.308800560307966 0.831573569224472] [ 0.830234709624040 0.450246242252960 0.570598270811050 0.454395010787615] [ 0.282073726465788 0.714070529426863 0.328100651923947 0.0721480109333678] [ 0.929933648170211 0.269682898269158 0.520346698401252 0.355973515003413]
D.det()
0.0501581984964803
var('a,b,c,d')
(a, b, c, d)
D=matrix\ ([[a^2,(a+1)^2,(a+2)^2,(a+3)^2],[b^2,(b+1)^2,(b+2)^2,(b+3)^2],\ [c^2,(c+1)^2,(c+2)^2,(c+3)^2],[d^2,(d+1)^2,(d+2)^2,(d+3)^2]]) D
[ a^2 (a + 1)^2 (a + 2)^2 (a + 3)^2] [ b^2 (b + 1)^2 (b + 2)^2 (b + 3)^2] [ c^2 (c + 1)^2 (c + 2)^2 (c + 3)^2] [ d^2 (d + 1)^2 (d + 2)^2 (d + 3)^2]
det(D)
0
A=matrix(RR,3,[random() for each in range(9)]);A
[ 0.864356202559331 0.633096841879541 0.924103099577427] [ 0.999474720641427 0.681717900332826 0.0881311159313142] [ 0.456144951079067 0.00281743139502155 0.904914069422016]
B=copy(A) B.set_row(0,[1,2,3]);B
[ 1.00000000000000 2.00000000000000 3.00000000000000] [ 0.999474720641427 0.681717900332826 0.0881311159313142] [ 0.456144951079067 0.00281743139502155 0.904914069422016]
C=copy(A);C[0,:]=A[0,:]+B[0,:];C
[ 1.86435620255933 2.63309684187954 3.92410309957743] [ 0.999474720641427 0.681717900332826 0.0881311159313142] [ 0.456144951079067 0.00281743139502155 0.904914069422016]
det(A)+det(B)
-2.33516918770469
det(C)
-2.33516918770469
D=matrix(RR,3,[1,10,100,2,40,800,3,90,2700]) b=matrix(RR,3,1,[-0.003,-0.005,-0.008]) D1=copy(D);D1[:,0]=b;a1=det(D1)/det(D);a1
-0.00416666666666667
D2=copy(D);D2[:,1]=b;a2=det(D2)/det(D);a2
0.000150000000000000
D3=copy(D);D3[:,2]=b;a3=det(D3)/det(D);a3
-3.33333333333334e-6
def h(t): return 13.6+a1*t+a2*t^2+a3*t^3
h(15)
13.5600000000000
h(40)
13.4600000000000