Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Project: Public Code
Views: 82

Introduction to Sage and Python

Worksheet

This is a public CoCalc worksheet. If you want to run it, you need to create a CoCalc account and a new project, then click on the "i" on top left and copy this worksheet to your project.

A worksheet is made of cells. Some contain text and some contain code that you can run directly.

5+12

Sage as a calculator

4/5 12//5 (4/5).n() 4./5. .8
pi.n(digits=300)
x = var('x') expr = 1 / (1-x^2) expr
show(expr)
latex(expr)
expr2 = - expr + expr^3 show(expr2)
show(expr2.simplify_rational())
expr2.subs(x = pi)
expr2.subs(x = pi).n()

The help system

How can you find what you are looking for?

--> The tab key and the "?"

A = matr
A = matrix([[1,2],[3,4]])
A
type(A)
A.parent()
A.
A.rank
A.rank?
A.rank??
A._rank_linbox

Some simple things

f(x) = x^3 + x - 4 f
f(7)
f.derivative()
plot(f)

Some code

b = 8 if b%2==0: a = b/2 else: a = 3*b +1
a
for i in xrange(3): print i
l = [2,4,5,68] for i in l: print i
a = 5 while a!=1: print a if a%2==0: a/=2 else: a=a*3+1
def my_function(a): if(a%2): return a*3+1 else: return a/2
my_function(5)

Some combinatorics

P = Permutations(4) P
list(P)
P = Permutations(10) P
P.cardinality()
P.random_element()
P.identity()
p = Permutation([1,5,3,4,2,7,6])
p
p.parent()
p.
p.number_of_descents()

Lets compute the average number of fixed points...

p.number_of_fixed_points()
P5 = Permutations(5)
mean([p.number_of_fixed_points() for p in P5])
P6 = Permutations(6)
mean([p.number_of_fixed_points() for p in P6])
P100 = Permutations(100)
mean([P100.random_element().number_of_fixed_points() for i in range(100)])
︠dcdd50dc-720c-40ea-b5b9-e165efbdb23e︠